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.contains(D); 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.contains(QT); 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.contains(D); 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, Expr *Alignment, SourceRange SR) { 3134 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3135 return; 3136 if (Alignment && 3137 (Alignment->isTypeDependent() || Alignment->isValueDependent() || 3138 Alignment->isInstantiationDependent() || 3139 Alignment->containsUnexpandedParameterPack())) 3140 // Apply later when we have a usable value. 3141 return; 3142 if (Allocator && 3143 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3144 Allocator->isInstantiationDependent() || 3145 Allocator->containsUnexpandedParameterPack())) 3146 return; 3147 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3148 Allocator, Alignment, SR); 3149 VD->addAttr(A); 3150 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3151 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3152 } 3153 3154 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3155 SourceLocation Loc, ArrayRef<Expr *> VarList, 3156 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3157 assert(Clauses.size() <= 2 && "Expected at most two clauses."); 3158 Expr *Alignment = nullptr; 3159 Expr *Allocator = nullptr; 3160 if (Clauses.empty()) { 3161 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3162 // allocate directives that appear in a target region must specify an 3163 // allocator clause unless a requires directive with the dynamic_allocators 3164 // clause is present in the same compilation unit. 3165 if (LangOpts.OpenMPIsDevice && 3166 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3167 targetDiag(Loc, diag::err_expected_allocator_clause); 3168 } else { 3169 for (const OMPClause *C : Clauses) 3170 if (const auto *AC = dyn_cast<OMPAllocatorClause>(C)) 3171 Allocator = AC->getAllocator(); 3172 else if (const auto *AC = dyn_cast<OMPAlignClause>(C)) 3173 Alignment = AC->getAlignment(); 3174 else 3175 llvm_unreachable("Unexpected clause on allocate directive"); 3176 } 3177 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3178 getAllocatorKind(*this, DSAStack, Allocator); 3179 SmallVector<Expr *, 8> Vars; 3180 for (Expr *RefExpr : VarList) { 3181 auto *DE = cast<DeclRefExpr>(RefExpr); 3182 auto *VD = cast<VarDecl>(DE->getDecl()); 3183 3184 // Check if this is a TLS variable or global register. 3185 if (VD->getTLSKind() != VarDecl::TLS_None || 3186 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3187 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3188 !VD->isLocalVarDecl())) 3189 continue; 3190 3191 // If the used several times in the allocate directive, the same allocator 3192 // must be used. 3193 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3194 AllocatorKind, Allocator)) 3195 continue; 3196 3197 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3198 // If a list item has a static storage type, the allocator expression in the 3199 // allocator clause must be a constant expression that evaluates to one of 3200 // the predefined memory allocator values. 3201 if (Allocator && VD->hasGlobalStorage()) { 3202 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3203 Diag(Allocator->getExprLoc(), 3204 diag::err_omp_expected_predefined_allocator) 3205 << Allocator->getSourceRange(); 3206 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3207 VarDecl::DeclarationOnly; 3208 Diag(VD->getLocation(), 3209 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3210 << VD; 3211 continue; 3212 } 3213 } 3214 3215 Vars.push_back(RefExpr); 3216 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, Alignment, 3217 DE->getSourceRange()); 3218 } 3219 if (Vars.empty()) 3220 return nullptr; 3221 if (!Owner) 3222 Owner = getCurLexicalContext(); 3223 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3224 D->setAccess(AS_public); 3225 Owner->addDecl(D); 3226 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3227 } 3228 3229 Sema::DeclGroupPtrTy 3230 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3231 ArrayRef<OMPClause *> ClauseList) { 3232 OMPRequiresDecl *D = nullptr; 3233 if (!CurContext->isFileContext()) { 3234 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3235 } else { 3236 D = CheckOMPRequiresDecl(Loc, ClauseList); 3237 if (D) { 3238 CurContext->addDecl(D); 3239 DSAStack->addRequiresDecl(D); 3240 } 3241 } 3242 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3243 } 3244 3245 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc, 3246 OpenMPDirectiveKind DKind, 3247 ArrayRef<std::string> Assumptions, 3248 bool SkippedClauses) { 3249 if (!SkippedClauses && Assumptions.empty()) 3250 Diag(Loc, diag::err_omp_no_clause_for_directive) 3251 << llvm::omp::getAllAssumeClauseOptions() 3252 << llvm::omp::getOpenMPDirectiveName(DKind); 3253 3254 auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc); 3255 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) { 3256 OMPAssumeScoped.push_back(AA); 3257 return; 3258 } 3259 3260 // Global assumes without assumption clauses are ignored. 3261 if (Assumptions.empty()) 3262 return; 3263 3264 assert(DKind == llvm::omp::Directive::OMPD_assumes && 3265 "Unexpected omp assumption directive!"); 3266 OMPAssumeGlobal.push_back(AA); 3267 3268 // The OMPAssumeGlobal scope above will take care of new declarations but 3269 // we also want to apply the assumption to existing ones, e.g., to 3270 // declarations in included headers. To this end, we traverse all existing 3271 // declaration contexts and annotate function declarations here. 3272 SmallVector<DeclContext *, 8> DeclContexts; 3273 auto *Ctx = CurContext; 3274 while (Ctx->getLexicalParent()) 3275 Ctx = Ctx->getLexicalParent(); 3276 DeclContexts.push_back(Ctx); 3277 while (!DeclContexts.empty()) { 3278 DeclContext *DC = DeclContexts.pop_back_val(); 3279 for (auto *SubDC : DC->decls()) { 3280 if (SubDC->isInvalidDecl()) 3281 continue; 3282 if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) { 3283 DeclContexts.push_back(CTD->getTemplatedDecl()); 3284 for (auto *S : CTD->specializations()) 3285 DeclContexts.push_back(S); 3286 continue; 3287 } 3288 if (auto *DC = dyn_cast<DeclContext>(SubDC)) 3289 DeclContexts.push_back(DC); 3290 if (auto *F = dyn_cast<FunctionDecl>(SubDC)) { 3291 F->addAttr(AA); 3292 continue; 3293 } 3294 } 3295 } 3296 } 3297 3298 void Sema::ActOnOpenMPEndAssumesDirective() { 3299 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!"); 3300 OMPAssumeScoped.pop_back(); 3301 } 3302 3303 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3304 ArrayRef<OMPClause *> ClauseList) { 3305 /// For target specific clauses, the requires directive cannot be 3306 /// specified after the handling of any of the target regions in the 3307 /// current compilation unit. 3308 ArrayRef<SourceLocation> TargetLocations = 3309 DSAStack->getEncounteredTargetLocs(); 3310 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3311 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3312 for (const OMPClause *CNew : ClauseList) { 3313 // Check if any of the requires clauses affect target regions. 3314 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3315 isa<OMPUnifiedAddressClause>(CNew) || 3316 isa<OMPReverseOffloadClause>(CNew) || 3317 isa<OMPDynamicAllocatorsClause>(CNew)) { 3318 Diag(Loc, diag::err_omp_directive_before_requires) 3319 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3320 for (SourceLocation TargetLoc : TargetLocations) { 3321 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3322 << "target"; 3323 } 3324 } else if (!AtomicLoc.isInvalid() && 3325 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3326 Diag(Loc, diag::err_omp_directive_before_requires) 3327 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3328 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3329 << "atomic"; 3330 } 3331 } 3332 } 3333 3334 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3335 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3336 ClauseList); 3337 return nullptr; 3338 } 3339 3340 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3341 const ValueDecl *D, 3342 const DSAStackTy::DSAVarData &DVar, 3343 bool IsLoopIterVar) { 3344 if (DVar.RefExpr) { 3345 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3346 << getOpenMPClauseName(DVar.CKind); 3347 return; 3348 } 3349 enum { 3350 PDSA_StaticMemberShared, 3351 PDSA_StaticLocalVarShared, 3352 PDSA_LoopIterVarPrivate, 3353 PDSA_LoopIterVarLinear, 3354 PDSA_LoopIterVarLastprivate, 3355 PDSA_ConstVarShared, 3356 PDSA_GlobalVarShared, 3357 PDSA_TaskVarFirstprivate, 3358 PDSA_LocalVarPrivate, 3359 PDSA_Implicit 3360 } Reason = PDSA_Implicit; 3361 bool ReportHint = false; 3362 auto ReportLoc = D->getLocation(); 3363 auto *VD = dyn_cast<VarDecl>(D); 3364 if (IsLoopIterVar) { 3365 if (DVar.CKind == OMPC_private) 3366 Reason = PDSA_LoopIterVarPrivate; 3367 else if (DVar.CKind == OMPC_lastprivate) 3368 Reason = PDSA_LoopIterVarLastprivate; 3369 else 3370 Reason = PDSA_LoopIterVarLinear; 3371 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3372 DVar.CKind == OMPC_firstprivate) { 3373 Reason = PDSA_TaskVarFirstprivate; 3374 ReportLoc = DVar.ImplicitDSALoc; 3375 } else if (VD && VD->isStaticLocal()) 3376 Reason = PDSA_StaticLocalVarShared; 3377 else if (VD && VD->isStaticDataMember()) 3378 Reason = PDSA_StaticMemberShared; 3379 else if (VD && VD->isFileVarDecl()) 3380 Reason = PDSA_GlobalVarShared; 3381 else if (D->getType().isConstant(SemaRef.getASTContext())) 3382 Reason = PDSA_ConstVarShared; 3383 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3384 ReportHint = true; 3385 Reason = PDSA_LocalVarPrivate; 3386 } 3387 if (Reason != PDSA_Implicit) { 3388 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3389 << Reason << ReportHint 3390 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3391 } else if (DVar.ImplicitDSALoc.isValid()) { 3392 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3393 << getOpenMPClauseName(DVar.CKind); 3394 } 3395 } 3396 3397 static OpenMPMapClauseKind 3398 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3399 bool IsAggregateOrDeclareTarget) { 3400 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3401 switch (M) { 3402 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3403 Kind = OMPC_MAP_alloc; 3404 break; 3405 case OMPC_DEFAULTMAP_MODIFIER_to: 3406 Kind = OMPC_MAP_to; 3407 break; 3408 case OMPC_DEFAULTMAP_MODIFIER_from: 3409 Kind = OMPC_MAP_from; 3410 break; 3411 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3412 Kind = OMPC_MAP_tofrom; 3413 break; 3414 case OMPC_DEFAULTMAP_MODIFIER_present: 3415 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description] 3416 // If implicit-behavior is present, each variable referenced in the 3417 // construct in the category specified by variable-category is treated as if 3418 // it had been listed in a map clause with the map-type of alloc and 3419 // map-type-modifier of present. 3420 Kind = OMPC_MAP_alloc; 3421 break; 3422 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3423 case OMPC_DEFAULTMAP_MODIFIER_last: 3424 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3425 case OMPC_DEFAULTMAP_MODIFIER_none: 3426 case OMPC_DEFAULTMAP_MODIFIER_default: 3427 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3428 // IsAggregateOrDeclareTarget could be true if: 3429 // 1. the implicit behavior for aggregate is tofrom 3430 // 2. it's a declare target link 3431 if (IsAggregateOrDeclareTarget) { 3432 Kind = OMPC_MAP_tofrom; 3433 break; 3434 } 3435 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3436 } 3437 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3438 return Kind; 3439 } 3440 3441 namespace { 3442 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3443 DSAStackTy *Stack; 3444 Sema &SemaRef; 3445 bool ErrorFound = false; 3446 bool TryCaptureCXXThisMembers = false; 3447 CapturedStmt *CS = nullptr; 3448 const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 3449 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3450 llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete]; 3451 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 3452 ImplicitMapModifier[DefaultmapKindNum]; 3453 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3454 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3455 3456 void VisitSubCaptures(OMPExecutableDirective *S) { 3457 // Check implicitly captured variables. 3458 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3459 return; 3460 if (S->getDirectiveKind() == OMPD_atomic || 3461 S->getDirectiveKind() == OMPD_critical || 3462 S->getDirectiveKind() == OMPD_section || 3463 S->getDirectiveKind() == OMPD_master || 3464 S->getDirectiveKind() == OMPD_masked || 3465 isOpenMPLoopTransformationDirective(S->getDirectiveKind())) { 3466 Visit(S->getAssociatedStmt()); 3467 return; 3468 } 3469 visitSubCaptures(S->getInnermostCapturedStmt()); 3470 // Try to capture inner this->member references to generate correct mappings 3471 // and diagnostics. 3472 if (TryCaptureCXXThisMembers || 3473 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3474 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3475 [](const CapturedStmt::Capture &C) { 3476 return C.capturesThis(); 3477 }))) { 3478 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3479 TryCaptureCXXThisMembers = true; 3480 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3481 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3482 } 3483 // In tasks firstprivates are not captured anymore, need to analyze them 3484 // explicitly. 3485 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3486 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3487 for (OMPClause *C : S->clauses()) 3488 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3489 for (Expr *Ref : FC->varlists()) 3490 Visit(Ref); 3491 } 3492 } 3493 } 3494 3495 public: 3496 void VisitDeclRefExpr(DeclRefExpr *E) { 3497 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3498 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3499 E->isInstantiationDependent()) 3500 return; 3501 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3502 // Check the datasharing rules for the expressions in the clauses. 3503 if (!CS) { 3504 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3505 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3506 Visit(CED->getInit()); 3507 return; 3508 } 3509 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3510 // Do not analyze internal variables and do not enclose them into 3511 // implicit clauses. 3512 return; 3513 VD = VD->getCanonicalDecl(); 3514 // Skip internally declared variables. 3515 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3516 !Stack->isImplicitTaskFirstprivate(VD)) 3517 return; 3518 // Skip allocators in uses_allocators clauses. 3519 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3520 return; 3521 3522 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3523 // Check if the variable has explicit DSA set and stop analysis if it so. 3524 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3525 return; 3526 3527 // Skip internally declared static variables. 3528 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3529 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3530 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3531 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3532 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3533 !Stack->isImplicitTaskFirstprivate(VD)) 3534 return; 3535 3536 SourceLocation ELoc = E->getExprLoc(); 3537 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3538 // The default(none) clause requires that each variable that is referenced 3539 // in the construct, and does not have a predetermined data-sharing 3540 // attribute, must have its data-sharing attribute explicitly determined 3541 // by being listed in a data-sharing attribute clause. 3542 if (DVar.CKind == OMPC_unknown && 3543 (Stack->getDefaultDSA() == DSA_none || 3544 Stack->getDefaultDSA() == DSA_firstprivate) && 3545 isImplicitOrExplicitTaskingRegion(DKind) && 3546 VarsWithInheritedDSA.count(VD) == 0) { 3547 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3548 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3549 DSAStackTy::DSAVarData DVar = 3550 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3551 InheritedDSA = DVar.CKind == OMPC_unknown; 3552 } 3553 if (InheritedDSA) 3554 VarsWithInheritedDSA[VD] = E; 3555 return; 3556 } 3557 3558 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3559 // If implicit-behavior is none, each variable referenced in the 3560 // construct that does not have a predetermined data-sharing attribute 3561 // and does not appear in a to or link clause on a declare target 3562 // directive must be listed in a data-mapping attribute clause, a 3563 // data-haring attribute clause (including a data-sharing attribute 3564 // clause on a combined construct where target. is one of the 3565 // constituent constructs), or an is_device_ptr clause. 3566 OpenMPDefaultmapClauseKind ClauseKind = 3567 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3568 if (SemaRef.getLangOpts().OpenMP >= 50) { 3569 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3570 OMPC_DEFAULTMAP_MODIFIER_none; 3571 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3572 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3573 // Only check for data-mapping attribute and is_device_ptr here 3574 // since we have already make sure that the declaration does not 3575 // have a data-sharing attribute above 3576 if (!Stack->checkMappableExprComponentListsForDecl( 3577 VD, /*CurrentRegionOnly=*/true, 3578 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3579 MapExprComponents, 3580 OpenMPClauseKind) { 3581 auto MI = MapExprComponents.rbegin(); 3582 auto ME = MapExprComponents.rend(); 3583 return MI != ME && MI->getAssociatedDeclaration() == VD; 3584 })) { 3585 VarsWithInheritedDSA[VD] = E; 3586 return; 3587 } 3588 } 3589 } 3590 if (SemaRef.getLangOpts().OpenMP > 50) { 3591 bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) == 3592 OMPC_DEFAULTMAP_MODIFIER_present; 3593 if (IsModifierPresent) { 3594 if (llvm::find(ImplicitMapModifier[ClauseKind], 3595 OMPC_MAP_MODIFIER_present) == 3596 std::end(ImplicitMapModifier[ClauseKind])) { 3597 ImplicitMapModifier[ClauseKind].push_back( 3598 OMPC_MAP_MODIFIER_present); 3599 } 3600 } 3601 } 3602 3603 if (isOpenMPTargetExecutionDirective(DKind) && 3604 !Stack->isLoopControlVariable(VD).first) { 3605 if (!Stack->checkMappableExprComponentListsForDecl( 3606 VD, /*CurrentRegionOnly=*/true, 3607 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef 3608 StackComponents, 3609 OpenMPClauseKind) { 3610 if (SemaRef.LangOpts.OpenMP >= 50) 3611 return !StackComponents.empty(); 3612 // Variable is used if it has been marked as an array, array 3613 // section, array shaping or the variable iself. 3614 return StackComponents.size() == 1 || 3615 std::all_of( 3616 std::next(StackComponents.rbegin()), 3617 StackComponents.rend(), 3618 [](const OMPClauseMappableExprCommon:: 3619 MappableComponent &MC) { 3620 return MC.getAssociatedDeclaration() == 3621 nullptr && 3622 (isa<OMPArraySectionExpr>( 3623 MC.getAssociatedExpression()) || 3624 isa<OMPArrayShapingExpr>( 3625 MC.getAssociatedExpression()) || 3626 isa<ArraySubscriptExpr>( 3627 MC.getAssociatedExpression())); 3628 }); 3629 })) { 3630 bool IsFirstprivate = false; 3631 // By default lambdas are captured as firstprivates. 3632 if (const auto *RD = 3633 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3634 IsFirstprivate = RD->isLambda(); 3635 IsFirstprivate = 3636 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3637 if (IsFirstprivate) { 3638 ImplicitFirstprivate.emplace_back(E); 3639 } else { 3640 OpenMPDefaultmapClauseModifier M = 3641 Stack->getDefaultmapModifier(ClauseKind); 3642 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3643 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3644 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3645 } 3646 return; 3647 } 3648 } 3649 3650 // OpenMP [2.9.3.6, Restrictions, p.2] 3651 // A list item that appears in a reduction clause of the innermost 3652 // enclosing worksharing or parallel construct may not be accessed in an 3653 // explicit task. 3654 DVar = Stack->hasInnermostDSA( 3655 VD, 3656 [](OpenMPClauseKind C, bool AppliedToPointee) { 3657 return C == OMPC_reduction && !AppliedToPointee; 3658 }, 3659 [](OpenMPDirectiveKind K) { 3660 return isOpenMPParallelDirective(K) || 3661 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3662 }, 3663 /*FromParent=*/true); 3664 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3665 ErrorFound = true; 3666 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3667 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3668 return; 3669 } 3670 3671 // Define implicit data-sharing attributes for task. 3672 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3673 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3674 (Stack->getDefaultDSA() == DSA_firstprivate && 3675 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3676 !Stack->isLoopControlVariable(VD).first) { 3677 ImplicitFirstprivate.push_back(E); 3678 return; 3679 } 3680 3681 // Store implicitly used globals with declare target link for parent 3682 // target. 3683 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3684 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3685 Stack->addToParentTargetRegionLinkGlobals(E); 3686 return; 3687 } 3688 } 3689 } 3690 void VisitMemberExpr(MemberExpr *E) { 3691 if (E->isTypeDependent() || E->isValueDependent() || 3692 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3693 return; 3694 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3695 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3696 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3697 if (!FD) 3698 return; 3699 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3700 // Check if the variable has explicit DSA set and stop analysis if it 3701 // so. 3702 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3703 return; 3704 3705 if (isOpenMPTargetExecutionDirective(DKind) && 3706 !Stack->isLoopControlVariable(FD).first && 3707 !Stack->checkMappableExprComponentListsForDecl( 3708 FD, /*CurrentRegionOnly=*/true, 3709 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3710 StackComponents, 3711 OpenMPClauseKind) { 3712 return isa<CXXThisExpr>( 3713 cast<MemberExpr>( 3714 StackComponents.back().getAssociatedExpression()) 3715 ->getBase() 3716 ->IgnoreParens()); 3717 })) { 3718 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3719 // A bit-field cannot appear in a map clause. 3720 // 3721 if (FD->isBitField()) 3722 return; 3723 3724 // Check to see if the member expression is referencing a class that 3725 // has already been explicitly mapped 3726 if (Stack->isClassPreviouslyMapped(TE->getType())) 3727 return; 3728 3729 OpenMPDefaultmapClauseModifier Modifier = 3730 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3731 OpenMPDefaultmapClauseKind ClauseKind = 3732 getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD); 3733 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3734 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3735 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3736 return; 3737 } 3738 3739 SourceLocation ELoc = E->getExprLoc(); 3740 // OpenMP [2.9.3.6, Restrictions, p.2] 3741 // A list item that appears in a reduction clause of the innermost 3742 // enclosing worksharing or parallel construct may not be accessed in 3743 // an explicit task. 3744 DVar = Stack->hasInnermostDSA( 3745 FD, 3746 [](OpenMPClauseKind C, bool AppliedToPointee) { 3747 return C == OMPC_reduction && !AppliedToPointee; 3748 }, 3749 [](OpenMPDirectiveKind K) { 3750 return isOpenMPParallelDirective(K) || 3751 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3752 }, 3753 /*FromParent=*/true); 3754 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3755 ErrorFound = true; 3756 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3757 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3758 return; 3759 } 3760 3761 // Define implicit data-sharing attributes for task. 3762 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3763 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3764 !Stack->isLoopControlVariable(FD).first) { 3765 // Check if there is a captured expression for the current field in the 3766 // region. Do not mark it as firstprivate unless there is no captured 3767 // expression. 3768 // TODO: try to make it firstprivate. 3769 if (DVar.CKind != OMPC_unknown) 3770 ImplicitFirstprivate.push_back(E); 3771 } 3772 return; 3773 } 3774 if (isOpenMPTargetExecutionDirective(DKind)) { 3775 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3776 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3777 Stack->getCurrentDirective(), 3778 /*NoDiagnose=*/true)) 3779 return; 3780 const auto *VD = cast<ValueDecl>( 3781 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3782 if (!Stack->checkMappableExprComponentListsForDecl( 3783 VD, /*CurrentRegionOnly=*/true, 3784 [&CurComponents]( 3785 OMPClauseMappableExprCommon::MappableExprComponentListRef 3786 StackComponents, 3787 OpenMPClauseKind) { 3788 auto CCI = CurComponents.rbegin(); 3789 auto CCE = CurComponents.rend(); 3790 for (const auto &SC : llvm::reverse(StackComponents)) { 3791 // Do both expressions have the same kind? 3792 if (CCI->getAssociatedExpression()->getStmtClass() != 3793 SC.getAssociatedExpression()->getStmtClass()) 3794 if (!((isa<OMPArraySectionExpr>( 3795 SC.getAssociatedExpression()) || 3796 isa<OMPArrayShapingExpr>( 3797 SC.getAssociatedExpression())) && 3798 isa<ArraySubscriptExpr>( 3799 CCI->getAssociatedExpression()))) 3800 return false; 3801 3802 const Decl *CCD = CCI->getAssociatedDeclaration(); 3803 const Decl *SCD = SC.getAssociatedDeclaration(); 3804 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3805 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3806 if (SCD != CCD) 3807 return false; 3808 std::advance(CCI, 1); 3809 if (CCI == CCE) 3810 break; 3811 } 3812 return true; 3813 })) { 3814 Visit(E->getBase()); 3815 } 3816 } else if (!TryCaptureCXXThisMembers) { 3817 Visit(E->getBase()); 3818 } 3819 } 3820 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3821 for (OMPClause *C : S->clauses()) { 3822 // Skip analysis of arguments of implicitly defined firstprivate clause 3823 // for task|target directives. 3824 // Skip analysis of arguments of implicitly defined map clause for target 3825 // directives. 3826 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3827 C->isImplicit() && 3828 !isOpenMPTaskingDirective(Stack->getCurrentDirective()))) { 3829 for (Stmt *CC : C->children()) { 3830 if (CC) 3831 Visit(CC); 3832 } 3833 } 3834 } 3835 // Check implicitly captured variables. 3836 VisitSubCaptures(S); 3837 } 3838 3839 void VisitOMPLoopTransformationDirective(OMPLoopTransformationDirective *S) { 3840 // Loop transformation directives do not introduce data sharing 3841 VisitStmt(S); 3842 } 3843 3844 void VisitStmt(Stmt *S) { 3845 for (Stmt *C : S->children()) { 3846 if (C) { 3847 // Check implicitly captured variables in the task-based directives to 3848 // check if they must be firstprivatized. 3849 Visit(C); 3850 } 3851 } 3852 } 3853 3854 void visitSubCaptures(CapturedStmt *S) { 3855 for (const CapturedStmt::Capture &Cap : S->captures()) { 3856 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3857 continue; 3858 VarDecl *VD = Cap.getCapturedVar(); 3859 // Do not try to map the variable if it or its sub-component was mapped 3860 // already. 3861 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3862 Stack->checkMappableExprComponentListsForDecl( 3863 VD, /*CurrentRegionOnly=*/true, 3864 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3865 OpenMPClauseKind) { return true; })) 3866 continue; 3867 DeclRefExpr *DRE = buildDeclRefExpr( 3868 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3869 Cap.getLocation(), /*RefersToCapture=*/true); 3870 Visit(DRE); 3871 } 3872 } 3873 bool isErrorFound() const { return ErrorFound; } 3874 ArrayRef<Expr *> getImplicitFirstprivate() const { 3875 return ImplicitFirstprivate; 3876 } 3877 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK, 3878 OpenMPMapClauseKind MK) const { 3879 return ImplicitMap[DK][MK]; 3880 } 3881 ArrayRef<OpenMPMapModifierKind> 3882 getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const { 3883 return ImplicitMapModifier[Kind]; 3884 } 3885 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3886 return VarsWithInheritedDSA; 3887 } 3888 3889 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3890 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3891 // Process declare target link variables for the target directives. 3892 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3893 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3894 Visit(E); 3895 } 3896 } 3897 }; 3898 } // namespace 3899 3900 static void handleDeclareVariantConstructTrait(DSAStackTy *Stack, 3901 OpenMPDirectiveKind DKind, 3902 bool ScopeEntry) { 3903 SmallVector<llvm::omp::TraitProperty, 8> Traits; 3904 if (isOpenMPTargetExecutionDirective(DKind)) 3905 Traits.emplace_back(llvm::omp::TraitProperty::construct_target_target); 3906 if (isOpenMPTeamsDirective(DKind)) 3907 Traits.emplace_back(llvm::omp::TraitProperty::construct_teams_teams); 3908 if (isOpenMPParallelDirective(DKind)) 3909 Traits.emplace_back(llvm::omp::TraitProperty::construct_parallel_parallel); 3910 if (isOpenMPWorksharingDirective(DKind)) 3911 Traits.emplace_back(llvm::omp::TraitProperty::construct_for_for); 3912 if (isOpenMPSimdDirective(DKind)) 3913 Traits.emplace_back(llvm::omp::TraitProperty::construct_simd_simd); 3914 Stack->handleConstructTrait(Traits, ScopeEntry); 3915 } 3916 3917 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3918 switch (DKind) { 3919 case OMPD_parallel: 3920 case OMPD_parallel_for: 3921 case OMPD_parallel_for_simd: 3922 case OMPD_parallel_sections: 3923 case OMPD_parallel_master: 3924 case OMPD_teams: 3925 case OMPD_teams_distribute: 3926 case OMPD_teams_distribute_simd: { 3927 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3928 QualType KmpInt32PtrTy = 3929 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3930 Sema::CapturedParamNameType Params[] = { 3931 std::make_pair(".global_tid.", KmpInt32PtrTy), 3932 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3933 std::make_pair(StringRef(), QualType()) // __context with shared vars 3934 }; 3935 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3936 Params); 3937 break; 3938 } 3939 case OMPD_target_teams: 3940 case OMPD_target_parallel: 3941 case OMPD_target_parallel_for: 3942 case OMPD_target_parallel_for_simd: 3943 case OMPD_target_teams_distribute: 3944 case OMPD_target_teams_distribute_simd: { 3945 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3946 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3947 QualType KmpInt32PtrTy = 3948 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3949 QualType Args[] = {VoidPtrTy}; 3950 FunctionProtoType::ExtProtoInfo EPI; 3951 EPI.Variadic = true; 3952 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3953 Sema::CapturedParamNameType Params[] = { 3954 std::make_pair(".global_tid.", KmpInt32Ty), 3955 std::make_pair(".part_id.", KmpInt32PtrTy), 3956 std::make_pair(".privates.", VoidPtrTy), 3957 std::make_pair( 3958 ".copy_fn.", 3959 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3960 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3961 std::make_pair(StringRef(), QualType()) // __context with shared vars 3962 }; 3963 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3964 Params, /*OpenMPCaptureLevel=*/0); 3965 // Mark this captured region as inlined, because we don't use outlined 3966 // function directly. 3967 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3968 AlwaysInlineAttr::CreateImplicit( 3969 Context, {}, AttributeCommonInfo::AS_Keyword, 3970 AlwaysInlineAttr::Keyword_forceinline)); 3971 Sema::CapturedParamNameType ParamsTarget[] = { 3972 std::make_pair(StringRef(), QualType()) // __context with shared vars 3973 }; 3974 // Start a captured region for 'target' with no implicit parameters. 3975 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3976 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3977 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3978 std::make_pair(".global_tid.", KmpInt32PtrTy), 3979 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3980 std::make_pair(StringRef(), QualType()) // __context with shared vars 3981 }; 3982 // Start a captured region for 'teams' or 'parallel'. Both regions have 3983 // the same implicit parameters. 3984 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3985 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3986 break; 3987 } 3988 case OMPD_target: 3989 case OMPD_target_simd: { 3990 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3991 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3992 QualType KmpInt32PtrTy = 3993 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3994 QualType Args[] = {VoidPtrTy}; 3995 FunctionProtoType::ExtProtoInfo EPI; 3996 EPI.Variadic = true; 3997 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3998 Sema::CapturedParamNameType Params[] = { 3999 std::make_pair(".global_tid.", KmpInt32Ty), 4000 std::make_pair(".part_id.", KmpInt32PtrTy), 4001 std::make_pair(".privates.", VoidPtrTy), 4002 std::make_pair( 4003 ".copy_fn.", 4004 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4005 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4006 std::make_pair(StringRef(), QualType()) // __context with shared vars 4007 }; 4008 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4009 Params, /*OpenMPCaptureLevel=*/0); 4010 // Mark this captured region as inlined, because we don't use outlined 4011 // function directly. 4012 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4013 AlwaysInlineAttr::CreateImplicit( 4014 Context, {}, AttributeCommonInfo::AS_Keyword, 4015 AlwaysInlineAttr::Keyword_forceinline)); 4016 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4017 std::make_pair(StringRef(), QualType()), 4018 /*OpenMPCaptureLevel=*/1); 4019 break; 4020 } 4021 case OMPD_atomic: 4022 case OMPD_critical: 4023 case OMPD_section: 4024 case OMPD_master: 4025 case OMPD_masked: 4026 case OMPD_tile: 4027 case OMPD_unroll: 4028 break; 4029 case OMPD_loop: 4030 // TODO: 'loop' may require additional parameters depending on the binding. 4031 // Treat similar to OMPD_simd/OMPD_for for now. 4032 case OMPD_simd: 4033 case OMPD_for: 4034 case OMPD_for_simd: 4035 case OMPD_sections: 4036 case OMPD_single: 4037 case OMPD_taskgroup: 4038 case OMPD_distribute: 4039 case OMPD_distribute_simd: 4040 case OMPD_ordered: 4041 case OMPD_target_data: 4042 case OMPD_dispatch: { 4043 Sema::CapturedParamNameType Params[] = { 4044 std::make_pair(StringRef(), QualType()) // __context with shared vars 4045 }; 4046 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4047 Params); 4048 break; 4049 } 4050 case OMPD_task: { 4051 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4052 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4053 QualType KmpInt32PtrTy = 4054 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4055 QualType Args[] = {VoidPtrTy}; 4056 FunctionProtoType::ExtProtoInfo EPI; 4057 EPI.Variadic = true; 4058 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4059 Sema::CapturedParamNameType Params[] = { 4060 std::make_pair(".global_tid.", KmpInt32Ty), 4061 std::make_pair(".part_id.", KmpInt32PtrTy), 4062 std::make_pair(".privates.", VoidPtrTy), 4063 std::make_pair( 4064 ".copy_fn.", 4065 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4066 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4067 std::make_pair(StringRef(), QualType()) // __context with shared vars 4068 }; 4069 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4070 Params); 4071 // Mark this captured region as inlined, because we don't use outlined 4072 // function directly. 4073 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4074 AlwaysInlineAttr::CreateImplicit( 4075 Context, {}, AttributeCommonInfo::AS_Keyword, 4076 AlwaysInlineAttr::Keyword_forceinline)); 4077 break; 4078 } 4079 case OMPD_taskloop: 4080 case OMPD_taskloop_simd: 4081 case OMPD_master_taskloop: 4082 case OMPD_master_taskloop_simd: { 4083 QualType KmpInt32Ty = 4084 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4085 .withConst(); 4086 QualType KmpUInt64Ty = 4087 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4088 .withConst(); 4089 QualType KmpInt64Ty = 4090 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4091 .withConst(); 4092 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4093 QualType KmpInt32PtrTy = 4094 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4095 QualType Args[] = {VoidPtrTy}; 4096 FunctionProtoType::ExtProtoInfo EPI; 4097 EPI.Variadic = true; 4098 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4099 Sema::CapturedParamNameType Params[] = { 4100 std::make_pair(".global_tid.", KmpInt32Ty), 4101 std::make_pair(".part_id.", KmpInt32PtrTy), 4102 std::make_pair(".privates.", VoidPtrTy), 4103 std::make_pair( 4104 ".copy_fn.", 4105 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4106 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4107 std::make_pair(".lb.", KmpUInt64Ty), 4108 std::make_pair(".ub.", KmpUInt64Ty), 4109 std::make_pair(".st.", KmpInt64Ty), 4110 std::make_pair(".liter.", KmpInt32Ty), 4111 std::make_pair(".reductions.", VoidPtrTy), 4112 std::make_pair(StringRef(), QualType()) // __context with shared vars 4113 }; 4114 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4115 Params); 4116 // Mark this captured region as inlined, because we don't use outlined 4117 // function directly. 4118 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4119 AlwaysInlineAttr::CreateImplicit( 4120 Context, {}, AttributeCommonInfo::AS_Keyword, 4121 AlwaysInlineAttr::Keyword_forceinline)); 4122 break; 4123 } 4124 case OMPD_parallel_master_taskloop: 4125 case OMPD_parallel_master_taskloop_simd: { 4126 QualType KmpInt32Ty = 4127 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4128 .withConst(); 4129 QualType KmpUInt64Ty = 4130 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4131 .withConst(); 4132 QualType KmpInt64Ty = 4133 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4134 .withConst(); 4135 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4136 QualType KmpInt32PtrTy = 4137 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4138 Sema::CapturedParamNameType ParamsParallel[] = { 4139 std::make_pair(".global_tid.", KmpInt32PtrTy), 4140 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4141 std::make_pair(StringRef(), QualType()) // __context with shared vars 4142 }; 4143 // Start a captured region for 'parallel'. 4144 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4145 ParamsParallel, /*OpenMPCaptureLevel=*/0); 4146 QualType Args[] = {VoidPtrTy}; 4147 FunctionProtoType::ExtProtoInfo EPI; 4148 EPI.Variadic = true; 4149 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4150 Sema::CapturedParamNameType Params[] = { 4151 std::make_pair(".global_tid.", KmpInt32Ty), 4152 std::make_pair(".part_id.", KmpInt32PtrTy), 4153 std::make_pair(".privates.", VoidPtrTy), 4154 std::make_pair( 4155 ".copy_fn.", 4156 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4157 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4158 std::make_pair(".lb.", KmpUInt64Ty), 4159 std::make_pair(".ub.", KmpUInt64Ty), 4160 std::make_pair(".st.", KmpInt64Ty), 4161 std::make_pair(".liter.", KmpInt32Ty), 4162 std::make_pair(".reductions.", VoidPtrTy), 4163 std::make_pair(StringRef(), QualType()) // __context with shared vars 4164 }; 4165 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4166 Params, /*OpenMPCaptureLevel=*/1); 4167 // Mark this captured region as inlined, because we don't use outlined 4168 // function directly. 4169 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4170 AlwaysInlineAttr::CreateImplicit( 4171 Context, {}, AttributeCommonInfo::AS_Keyword, 4172 AlwaysInlineAttr::Keyword_forceinline)); 4173 break; 4174 } 4175 case OMPD_distribute_parallel_for_simd: 4176 case OMPD_distribute_parallel_for: { 4177 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4178 QualType KmpInt32PtrTy = 4179 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4180 Sema::CapturedParamNameType Params[] = { 4181 std::make_pair(".global_tid.", KmpInt32PtrTy), 4182 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4183 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4184 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4185 std::make_pair(StringRef(), QualType()) // __context with shared vars 4186 }; 4187 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4188 Params); 4189 break; 4190 } 4191 case OMPD_target_teams_distribute_parallel_for: 4192 case OMPD_target_teams_distribute_parallel_for_simd: { 4193 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4194 QualType KmpInt32PtrTy = 4195 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4196 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4197 4198 QualType Args[] = {VoidPtrTy}; 4199 FunctionProtoType::ExtProtoInfo EPI; 4200 EPI.Variadic = true; 4201 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4202 Sema::CapturedParamNameType Params[] = { 4203 std::make_pair(".global_tid.", KmpInt32Ty), 4204 std::make_pair(".part_id.", KmpInt32PtrTy), 4205 std::make_pair(".privates.", VoidPtrTy), 4206 std::make_pair( 4207 ".copy_fn.", 4208 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4209 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4210 std::make_pair(StringRef(), QualType()) // __context with shared vars 4211 }; 4212 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4213 Params, /*OpenMPCaptureLevel=*/0); 4214 // Mark this captured region as inlined, because we don't use outlined 4215 // function directly. 4216 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4217 AlwaysInlineAttr::CreateImplicit( 4218 Context, {}, AttributeCommonInfo::AS_Keyword, 4219 AlwaysInlineAttr::Keyword_forceinline)); 4220 Sema::CapturedParamNameType ParamsTarget[] = { 4221 std::make_pair(StringRef(), QualType()) // __context with shared vars 4222 }; 4223 // Start a captured region for 'target' with no implicit parameters. 4224 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4225 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4226 4227 Sema::CapturedParamNameType ParamsTeams[] = { 4228 std::make_pair(".global_tid.", KmpInt32PtrTy), 4229 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4230 std::make_pair(StringRef(), QualType()) // __context with shared vars 4231 }; 4232 // Start a captured region for 'target' with no implicit parameters. 4233 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4234 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4235 4236 Sema::CapturedParamNameType ParamsParallel[] = { 4237 std::make_pair(".global_tid.", KmpInt32PtrTy), 4238 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4239 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4240 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4241 std::make_pair(StringRef(), QualType()) // __context with shared vars 4242 }; 4243 // Start a captured region for 'teams' or 'parallel'. Both regions have 4244 // the same implicit parameters. 4245 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4246 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4247 break; 4248 } 4249 4250 case OMPD_teams_distribute_parallel_for: 4251 case OMPD_teams_distribute_parallel_for_simd: { 4252 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4253 QualType KmpInt32PtrTy = 4254 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4255 4256 Sema::CapturedParamNameType ParamsTeams[] = { 4257 std::make_pair(".global_tid.", KmpInt32PtrTy), 4258 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4259 std::make_pair(StringRef(), QualType()) // __context with shared vars 4260 }; 4261 // Start a captured region for 'target' with no implicit parameters. 4262 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4263 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4264 4265 Sema::CapturedParamNameType ParamsParallel[] = { 4266 std::make_pair(".global_tid.", KmpInt32PtrTy), 4267 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4268 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4269 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4270 std::make_pair(StringRef(), QualType()) // __context with shared vars 4271 }; 4272 // Start a captured region for 'teams' or 'parallel'. Both regions have 4273 // the same implicit parameters. 4274 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4275 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4276 break; 4277 } 4278 case OMPD_target_update: 4279 case OMPD_target_enter_data: 4280 case OMPD_target_exit_data: { 4281 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4282 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4283 QualType KmpInt32PtrTy = 4284 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4285 QualType Args[] = {VoidPtrTy}; 4286 FunctionProtoType::ExtProtoInfo EPI; 4287 EPI.Variadic = true; 4288 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4289 Sema::CapturedParamNameType Params[] = { 4290 std::make_pair(".global_tid.", KmpInt32Ty), 4291 std::make_pair(".part_id.", KmpInt32PtrTy), 4292 std::make_pair(".privates.", VoidPtrTy), 4293 std::make_pair( 4294 ".copy_fn.", 4295 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4296 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4297 std::make_pair(StringRef(), QualType()) // __context with shared vars 4298 }; 4299 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4300 Params); 4301 // Mark this captured region as inlined, because we don't use outlined 4302 // function directly. 4303 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4304 AlwaysInlineAttr::CreateImplicit( 4305 Context, {}, AttributeCommonInfo::AS_Keyword, 4306 AlwaysInlineAttr::Keyword_forceinline)); 4307 break; 4308 } 4309 case OMPD_threadprivate: 4310 case OMPD_allocate: 4311 case OMPD_taskyield: 4312 case OMPD_barrier: 4313 case OMPD_taskwait: 4314 case OMPD_cancellation_point: 4315 case OMPD_cancel: 4316 case OMPD_flush: 4317 case OMPD_depobj: 4318 case OMPD_scan: 4319 case OMPD_declare_reduction: 4320 case OMPD_declare_mapper: 4321 case OMPD_declare_simd: 4322 case OMPD_declare_target: 4323 case OMPD_end_declare_target: 4324 case OMPD_requires: 4325 case OMPD_declare_variant: 4326 case OMPD_begin_declare_variant: 4327 case OMPD_end_declare_variant: 4328 case OMPD_metadirective: 4329 llvm_unreachable("OpenMP Directive is not allowed"); 4330 case OMPD_unknown: 4331 default: 4332 llvm_unreachable("Unknown OpenMP directive"); 4333 } 4334 DSAStack->setContext(CurContext); 4335 handleDeclareVariantConstructTrait(DSAStack, DKind, /* ScopeEntry */ true); 4336 } 4337 4338 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4339 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4340 } 4341 4342 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4343 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4344 getOpenMPCaptureRegions(CaptureRegions, DKind); 4345 return CaptureRegions.size(); 4346 } 4347 4348 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4349 Expr *CaptureExpr, bool WithInit, 4350 bool AsExpression) { 4351 assert(CaptureExpr); 4352 ASTContext &C = S.getASTContext(); 4353 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4354 QualType Ty = Init->getType(); 4355 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4356 if (S.getLangOpts().CPlusPlus) { 4357 Ty = C.getLValueReferenceType(Ty); 4358 } else { 4359 Ty = C.getPointerType(Ty); 4360 ExprResult Res = 4361 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4362 if (!Res.isUsable()) 4363 return nullptr; 4364 Init = Res.get(); 4365 } 4366 WithInit = true; 4367 } 4368 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4369 CaptureExpr->getBeginLoc()); 4370 if (!WithInit) 4371 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4372 S.CurContext->addHiddenDecl(CED); 4373 Sema::TentativeAnalysisScope Trap(S); 4374 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4375 return CED; 4376 } 4377 4378 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4379 bool WithInit) { 4380 OMPCapturedExprDecl *CD; 4381 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4382 CD = cast<OMPCapturedExprDecl>(VD); 4383 else 4384 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4385 /*AsExpression=*/false); 4386 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4387 CaptureExpr->getExprLoc()); 4388 } 4389 4390 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4391 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4392 if (!Ref) { 4393 OMPCapturedExprDecl *CD = buildCaptureDecl( 4394 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4395 /*WithInit=*/true, /*AsExpression=*/true); 4396 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4397 CaptureExpr->getExprLoc()); 4398 } 4399 ExprResult Res = Ref; 4400 if (!S.getLangOpts().CPlusPlus && 4401 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4402 Ref->getType()->isPointerType()) { 4403 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4404 if (!Res.isUsable()) 4405 return ExprError(); 4406 } 4407 return S.DefaultLvalueConversion(Res.get()); 4408 } 4409 4410 namespace { 4411 // OpenMP directives parsed in this section are represented as a 4412 // CapturedStatement with an associated statement. If a syntax error 4413 // is detected during the parsing of the associated statement, the 4414 // compiler must abort processing and close the CapturedStatement. 4415 // 4416 // Combined directives such as 'target parallel' have more than one 4417 // nested CapturedStatements. This RAII ensures that we unwind out 4418 // of all the nested CapturedStatements when an error is found. 4419 class CaptureRegionUnwinderRAII { 4420 private: 4421 Sema &S; 4422 bool &ErrorFound; 4423 OpenMPDirectiveKind DKind = OMPD_unknown; 4424 4425 public: 4426 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4427 OpenMPDirectiveKind DKind) 4428 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4429 ~CaptureRegionUnwinderRAII() { 4430 if (ErrorFound) { 4431 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4432 while (--ThisCaptureLevel >= 0) 4433 S.ActOnCapturedRegionError(); 4434 } 4435 } 4436 }; 4437 } // namespace 4438 4439 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4440 // Capture variables captured by reference in lambdas for target-based 4441 // directives. 4442 if (!CurContext->isDependentContext() && 4443 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4444 isOpenMPTargetDataManagementDirective( 4445 DSAStack->getCurrentDirective()))) { 4446 QualType Type = V->getType(); 4447 if (const auto *RD = Type.getCanonicalType() 4448 .getNonReferenceType() 4449 ->getAsCXXRecordDecl()) { 4450 bool SavedForceCaptureByReferenceInTargetExecutable = 4451 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4452 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4453 /*V=*/true); 4454 if (RD->isLambda()) { 4455 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4456 FieldDecl *ThisCapture; 4457 RD->getCaptureFields(Captures, ThisCapture); 4458 for (const LambdaCapture &LC : RD->captures()) { 4459 if (LC.getCaptureKind() == LCK_ByRef) { 4460 VarDecl *VD = LC.getCapturedVar(); 4461 DeclContext *VDC = VD->getDeclContext(); 4462 if (!VDC->Encloses(CurContext)) 4463 continue; 4464 MarkVariableReferenced(LC.getLocation(), VD); 4465 } else if (LC.getCaptureKind() == LCK_This) { 4466 QualType ThisTy = getCurrentThisType(); 4467 if (!ThisTy.isNull() && 4468 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4469 CheckCXXThisCapture(LC.getLocation()); 4470 } 4471 } 4472 } 4473 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4474 SavedForceCaptureByReferenceInTargetExecutable); 4475 } 4476 } 4477 } 4478 4479 static bool checkOrderedOrderSpecified(Sema &S, 4480 const ArrayRef<OMPClause *> Clauses) { 4481 const OMPOrderedClause *Ordered = nullptr; 4482 const OMPOrderClause *Order = nullptr; 4483 4484 for (const OMPClause *Clause : Clauses) { 4485 if (Clause->getClauseKind() == OMPC_ordered) 4486 Ordered = cast<OMPOrderedClause>(Clause); 4487 else if (Clause->getClauseKind() == OMPC_order) { 4488 Order = cast<OMPOrderClause>(Clause); 4489 if (Order->getKind() != OMPC_ORDER_concurrent) 4490 Order = nullptr; 4491 } 4492 if (Ordered && Order) 4493 break; 4494 } 4495 4496 if (Ordered && Order) { 4497 S.Diag(Order->getKindKwLoc(), 4498 diag::err_omp_simple_clause_incompatible_with_ordered) 4499 << getOpenMPClauseName(OMPC_order) 4500 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4501 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4502 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4503 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4504 return true; 4505 } 4506 return false; 4507 } 4508 4509 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4510 ArrayRef<OMPClause *> Clauses) { 4511 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(), 4512 /* ScopeEntry */ false); 4513 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4514 DSAStack->getCurrentDirective() == OMPD_critical || 4515 DSAStack->getCurrentDirective() == OMPD_section || 4516 DSAStack->getCurrentDirective() == OMPD_master || 4517 DSAStack->getCurrentDirective() == OMPD_masked) 4518 return S; 4519 4520 bool ErrorFound = false; 4521 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4522 *this, ErrorFound, DSAStack->getCurrentDirective()); 4523 if (!S.isUsable()) { 4524 ErrorFound = true; 4525 return StmtError(); 4526 } 4527 4528 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4529 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4530 OMPOrderedClause *OC = nullptr; 4531 OMPScheduleClause *SC = nullptr; 4532 SmallVector<const OMPLinearClause *, 4> LCs; 4533 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4534 // This is required for proper codegen. 4535 for (OMPClause *Clause : Clauses) { 4536 if (!LangOpts.OpenMPSimd && 4537 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4538 Clause->getClauseKind() == OMPC_in_reduction) { 4539 // Capture taskgroup task_reduction descriptors inside the tasking regions 4540 // with the corresponding in_reduction items. 4541 auto *IRC = cast<OMPInReductionClause>(Clause); 4542 for (Expr *E : IRC->taskgroup_descriptors()) 4543 if (E) 4544 MarkDeclarationsReferencedInExpr(E); 4545 } 4546 if (isOpenMPPrivate(Clause->getClauseKind()) || 4547 Clause->getClauseKind() == OMPC_copyprivate || 4548 (getLangOpts().OpenMPUseTLS && 4549 getASTContext().getTargetInfo().isTLSSupported() && 4550 Clause->getClauseKind() == OMPC_copyin)) { 4551 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4552 // Mark all variables in private list clauses as used in inner region. 4553 for (Stmt *VarRef : Clause->children()) { 4554 if (auto *E = cast_or_null<Expr>(VarRef)) { 4555 MarkDeclarationsReferencedInExpr(E); 4556 } 4557 } 4558 DSAStack->setForceVarCapturing(/*V=*/false); 4559 } else if (isOpenMPLoopTransformationDirective( 4560 DSAStack->getCurrentDirective())) { 4561 assert(CaptureRegions.empty() && 4562 "No captured regions in loop transformation directives."); 4563 } else if (CaptureRegions.size() > 1 || 4564 CaptureRegions.back() != OMPD_unknown) { 4565 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4566 PICs.push_back(C); 4567 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4568 if (Expr *E = C->getPostUpdateExpr()) 4569 MarkDeclarationsReferencedInExpr(E); 4570 } 4571 } 4572 if (Clause->getClauseKind() == OMPC_schedule) 4573 SC = cast<OMPScheduleClause>(Clause); 4574 else if (Clause->getClauseKind() == OMPC_ordered) 4575 OC = cast<OMPOrderedClause>(Clause); 4576 else if (Clause->getClauseKind() == OMPC_linear) 4577 LCs.push_back(cast<OMPLinearClause>(Clause)); 4578 } 4579 // Capture allocator expressions if used. 4580 for (Expr *E : DSAStack->getInnerAllocators()) 4581 MarkDeclarationsReferencedInExpr(E); 4582 // OpenMP, 2.7.1 Loop Construct, Restrictions 4583 // The nonmonotonic modifier cannot be specified if an ordered clause is 4584 // specified. 4585 if (SC && 4586 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4587 SC->getSecondScheduleModifier() == 4588 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4589 OC) { 4590 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4591 ? SC->getFirstScheduleModifierLoc() 4592 : SC->getSecondScheduleModifierLoc(), 4593 diag::err_omp_simple_clause_incompatible_with_ordered) 4594 << getOpenMPClauseName(OMPC_schedule) 4595 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4596 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4597 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4598 ErrorFound = true; 4599 } 4600 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4601 // If an order(concurrent) clause is present, an ordered clause may not appear 4602 // on the same directive. 4603 if (checkOrderedOrderSpecified(*this, Clauses)) 4604 ErrorFound = true; 4605 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4606 for (const OMPLinearClause *C : LCs) { 4607 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4608 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4609 } 4610 ErrorFound = true; 4611 } 4612 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4613 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4614 OC->getNumForLoops()) { 4615 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4616 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4617 ErrorFound = true; 4618 } 4619 if (ErrorFound) { 4620 return StmtError(); 4621 } 4622 StmtResult SR = S; 4623 unsigned CompletedRegions = 0; 4624 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4625 // Mark all variables in private list clauses as used in inner region. 4626 // Required for proper codegen of combined directives. 4627 // TODO: add processing for other clauses. 4628 if (ThisCaptureRegion != OMPD_unknown) { 4629 for (const clang::OMPClauseWithPreInit *C : PICs) { 4630 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4631 // Find the particular capture region for the clause if the 4632 // directive is a combined one with multiple capture regions. 4633 // If the directive is not a combined one, the capture region 4634 // associated with the clause is OMPD_unknown and is generated 4635 // only once. 4636 if (CaptureRegion == ThisCaptureRegion || 4637 CaptureRegion == OMPD_unknown) { 4638 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4639 for (Decl *D : DS->decls()) 4640 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4641 } 4642 } 4643 } 4644 } 4645 if (ThisCaptureRegion == OMPD_target) { 4646 // Capture allocator traits in the target region. They are used implicitly 4647 // and, thus, are not captured by default. 4648 for (OMPClause *C : Clauses) { 4649 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4650 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4651 ++I) { 4652 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4653 if (Expr *E = D.AllocatorTraits) 4654 MarkDeclarationsReferencedInExpr(E); 4655 } 4656 continue; 4657 } 4658 } 4659 } 4660 if (ThisCaptureRegion == OMPD_parallel) { 4661 // Capture temp arrays for inscan reductions and locals in aligned 4662 // clauses. 4663 for (OMPClause *C : Clauses) { 4664 if (auto *RC = dyn_cast<OMPReductionClause>(C)) { 4665 if (RC->getModifier() != OMPC_REDUCTION_inscan) 4666 continue; 4667 for (Expr *E : RC->copy_array_temps()) 4668 MarkDeclarationsReferencedInExpr(E); 4669 } 4670 if (auto *AC = dyn_cast<OMPAlignedClause>(C)) { 4671 for (Expr *E : AC->varlists()) 4672 MarkDeclarationsReferencedInExpr(E); 4673 } 4674 } 4675 } 4676 if (++CompletedRegions == CaptureRegions.size()) 4677 DSAStack->setBodyComplete(); 4678 SR = ActOnCapturedRegionEnd(SR.get()); 4679 } 4680 return SR; 4681 } 4682 4683 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4684 OpenMPDirectiveKind CancelRegion, 4685 SourceLocation StartLoc) { 4686 // CancelRegion is only needed for cancel and cancellation_point. 4687 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4688 return false; 4689 4690 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4691 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4692 return false; 4693 4694 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4695 << getOpenMPDirectiveName(CancelRegion); 4696 return true; 4697 } 4698 4699 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4700 OpenMPDirectiveKind CurrentRegion, 4701 const DeclarationNameInfo &CurrentName, 4702 OpenMPDirectiveKind CancelRegion, 4703 OpenMPBindClauseKind BindKind, 4704 SourceLocation StartLoc) { 4705 if (Stack->getCurScope()) { 4706 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4707 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4708 bool NestingProhibited = false; 4709 bool CloseNesting = true; 4710 bool OrphanSeen = false; 4711 enum { 4712 NoRecommend, 4713 ShouldBeInParallelRegion, 4714 ShouldBeInOrderedRegion, 4715 ShouldBeInTargetRegion, 4716 ShouldBeInTeamsRegion, 4717 ShouldBeInLoopSimdRegion, 4718 } Recommend = NoRecommend; 4719 if (isOpenMPSimdDirective(ParentRegion) && 4720 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4721 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4722 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4723 CurrentRegion != OMPD_scan))) { 4724 // OpenMP [2.16, Nesting of Regions] 4725 // OpenMP constructs may not be nested inside a simd region. 4726 // OpenMP [2.8.1,simd Construct, Restrictions] 4727 // An ordered construct with the simd clause is the only OpenMP 4728 // construct that can appear in the simd region. 4729 // Allowing a SIMD construct nested in another SIMD construct is an 4730 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4731 // message. 4732 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4733 // The only OpenMP constructs that can be encountered during execution of 4734 // a simd region are the atomic construct, the loop construct, the simd 4735 // construct and the ordered construct with the simd clause. 4736 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4737 ? diag::err_omp_prohibited_region_simd 4738 : diag::warn_omp_nesting_simd) 4739 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4740 return CurrentRegion != OMPD_simd; 4741 } 4742 if (ParentRegion == OMPD_atomic) { 4743 // OpenMP [2.16, Nesting of Regions] 4744 // OpenMP constructs may not be nested inside an atomic region. 4745 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4746 return true; 4747 } 4748 if (CurrentRegion == OMPD_section) { 4749 // OpenMP [2.7.2, sections Construct, Restrictions] 4750 // Orphaned section directives are prohibited. That is, the section 4751 // directives must appear within the sections construct and must not be 4752 // encountered elsewhere in the sections region. 4753 if (ParentRegion != OMPD_sections && 4754 ParentRegion != OMPD_parallel_sections) { 4755 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4756 << (ParentRegion != OMPD_unknown) 4757 << getOpenMPDirectiveName(ParentRegion); 4758 return true; 4759 } 4760 return false; 4761 } 4762 // Allow some constructs (except teams and cancellation constructs) to be 4763 // orphaned (they could be used in functions, called from OpenMP regions 4764 // with the required preconditions). 4765 if (ParentRegion == OMPD_unknown && 4766 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4767 CurrentRegion != OMPD_cancellation_point && 4768 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4769 return false; 4770 if (CurrentRegion == OMPD_cancellation_point || 4771 CurrentRegion == OMPD_cancel) { 4772 // OpenMP [2.16, Nesting of Regions] 4773 // A cancellation point construct for which construct-type-clause is 4774 // taskgroup must be nested inside a task construct. A cancellation 4775 // point construct for which construct-type-clause is not taskgroup must 4776 // be closely nested inside an OpenMP construct that matches the type 4777 // specified in construct-type-clause. 4778 // A cancel construct for which construct-type-clause is taskgroup must be 4779 // nested inside a task construct. A cancel construct for which 4780 // construct-type-clause is not taskgroup must be closely nested inside an 4781 // OpenMP construct that matches the type specified in 4782 // construct-type-clause. 4783 NestingProhibited = 4784 !((CancelRegion == OMPD_parallel && 4785 (ParentRegion == OMPD_parallel || 4786 ParentRegion == OMPD_target_parallel)) || 4787 (CancelRegion == OMPD_for && 4788 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4789 ParentRegion == OMPD_target_parallel_for || 4790 ParentRegion == OMPD_distribute_parallel_for || 4791 ParentRegion == OMPD_teams_distribute_parallel_for || 4792 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4793 (CancelRegion == OMPD_taskgroup && 4794 (ParentRegion == OMPD_task || 4795 (SemaRef.getLangOpts().OpenMP >= 50 && 4796 (ParentRegion == OMPD_taskloop || 4797 ParentRegion == OMPD_master_taskloop || 4798 ParentRegion == OMPD_parallel_master_taskloop)))) || 4799 (CancelRegion == OMPD_sections && 4800 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4801 ParentRegion == OMPD_parallel_sections))); 4802 OrphanSeen = ParentRegion == OMPD_unknown; 4803 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) { 4804 // OpenMP 5.1 [2.22, Nesting of Regions] 4805 // A masked region may not be closely nested inside a worksharing, loop, 4806 // atomic, task, or taskloop region. 4807 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4808 isOpenMPGenericLoopDirective(ParentRegion) || 4809 isOpenMPTaskingDirective(ParentRegion); 4810 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4811 // OpenMP [2.16, Nesting of Regions] 4812 // A critical region may not be nested (closely or otherwise) inside a 4813 // critical region with the same name. Note that this restriction is not 4814 // sufficient to prevent deadlock. 4815 SourceLocation PreviousCriticalLoc; 4816 bool DeadLock = Stack->hasDirective( 4817 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4818 const DeclarationNameInfo &DNI, 4819 SourceLocation Loc) { 4820 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4821 PreviousCriticalLoc = Loc; 4822 return true; 4823 } 4824 return false; 4825 }, 4826 false /* skip top directive */); 4827 if (DeadLock) { 4828 SemaRef.Diag(StartLoc, 4829 diag::err_omp_prohibited_region_critical_same_name) 4830 << CurrentName.getName(); 4831 if (PreviousCriticalLoc.isValid()) 4832 SemaRef.Diag(PreviousCriticalLoc, 4833 diag::note_omp_previous_critical_region); 4834 return true; 4835 } 4836 } else if (CurrentRegion == OMPD_barrier) { 4837 // OpenMP 5.1 [2.22, Nesting of Regions] 4838 // A barrier region may not be closely nested inside a worksharing, loop, 4839 // task, taskloop, critical, ordered, atomic, or masked region. 4840 NestingProhibited = 4841 isOpenMPWorksharingDirective(ParentRegion) || 4842 isOpenMPGenericLoopDirective(ParentRegion) || 4843 isOpenMPTaskingDirective(ParentRegion) || 4844 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4845 ParentRegion == OMPD_parallel_master || 4846 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4847 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4848 !isOpenMPParallelDirective(CurrentRegion) && 4849 !isOpenMPTeamsDirective(CurrentRegion)) { 4850 // OpenMP 5.1 [2.22, Nesting of Regions] 4851 // A loop region that binds to a parallel region or a worksharing region 4852 // may not be closely nested inside a worksharing, loop, task, taskloop, 4853 // critical, ordered, atomic, or masked region. 4854 NestingProhibited = 4855 isOpenMPWorksharingDirective(ParentRegion) || 4856 isOpenMPGenericLoopDirective(ParentRegion) || 4857 isOpenMPTaskingDirective(ParentRegion) || 4858 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4859 ParentRegion == OMPD_parallel_master || 4860 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4861 Recommend = ShouldBeInParallelRegion; 4862 } else if (CurrentRegion == OMPD_ordered) { 4863 // OpenMP [2.16, Nesting of Regions] 4864 // An ordered region may not be closely nested inside a critical, 4865 // atomic, or explicit task region. 4866 // An ordered region must be closely nested inside a loop region (or 4867 // parallel loop region) with an ordered clause. 4868 // OpenMP [2.8.1,simd Construct, Restrictions] 4869 // An ordered construct with the simd clause is the only OpenMP construct 4870 // that can appear in the simd region. 4871 NestingProhibited = ParentRegion == OMPD_critical || 4872 isOpenMPTaskingDirective(ParentRegion) || 4873 !(isOpenMPSimdDirective(ParentRegion) || 4874 Stack->isParentOrderedRegion()); 4875 Recommend = ShouldBeInOrderedRegion; 4876 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4877 // OpenMP [2.16, Nesting of Regions] 4878 // If specified, a teams construct must be contained within a target 4879 // construct. 4880 NestingProhibited = 4881 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4882 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4883 ParentRegion != OMPD_target); 4884 OrphanSeen = ParentRegion == OMPD_unknown; 4885 Recommend = ShouldBeInTargetRegion; 4886 } else if (CurrentRegion == OMPD_scan) { 4887 // OpenMP [2.16, Nesting of Regions] 4888 // If specified, a teams construct must be contained within a target 4889 // construct. 4890 NestingProhibited = 4891 SemaRef.LangOpts.OpenMP < 50 || 4892 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4893 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4894 ParentRegion != OMPD_parallel_for_simd); 4895 OrphanSeen = ParentRegion == OMPD_unknown; 4896 Recommend = ShouldBeInLoopSimdRegion; 4897 } 4898 if (!NestingProhibited && 4899 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4900 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4901 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4902 // OpenMP [5.1, 2.22, Nesting of Regions] 4903 // distribute, distribute simd, distribute parallel worksharing-loop, 4904 // distribute parallel worksharing-loop SIMD, loop, parallel regions, 4905 // including any parallel regions arising from combined constructs, 4906 // omp_get_num_teams() regions, and omp_get_team_num() regions are the 4907 // only OpenMP regions that may be strictly nested inside the teams 4908 // region. 4909 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4910 !isOpenMPDistributeDirective(CurrentRegion) && 4911 CurrentRegion != OMPD_loop; 4912 Recommend = ShouldBeInParallelRegion; 4913 } 4914 if (!NestingProhibited && CurrentRegion == OMPD_loop) { 4915 // OpenMP [5.1, 2.11.7, loop Construct, Restrictions] 4916 // If the bind clause is present on the loop construct and binding is 4917 // teams then the corresponding loop region must be strictly nested inside 4918 // a teams region. 4919 NestingProhibited = BindKind == OMPC_BIND_teams && 4920 ParentRegion != OMPD_teams && 4921 ParentRegion != OMPD_target_teams; 4922 Recommend = ShouldBeInTeamsRegion; 4923 } 4924 if (!NestingProhibited && 4925 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4926 // OpenMP 4.5 [2.17 Nesting of Regions] 4927 // The region associated with the distribute construct must be strictly 4928 // nested inside a teams region 4929 NestingProhibited = 4930 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4931 Recommend = ShouldBeInTeamsRegion; 4932 } 4933 if (!NestingProhibited && 4934 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4935 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4936 // OpenMP 4.5 [2.17 Nesting of Regions] 4937 // If a target, target update, target data, target enter data, or 4938 // target exit data construct is encountered during execution of a 4939 // target region, the behavior is unspecified. 4940 NestingProhibited = Stack->hasDirective( 4941 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4942 SourceLocation) { 4943 if (isOpenMPTargetExecutionDirective(K)) { 4944 OffendingRegion = K; 4945 return true; 4946 } 4947 return false; 4948 }, 4949 false /* don't skip top directive */); 4950 CloseNesting = false; 4951 } 4952 if (NestingProhibited) { 4953 if (OrphanSeen) { 4954 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4955 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4956 } else { 4957 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4958 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4959 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4960 } 4961 return true; 4962 } 4963 } 4964 return false; 4965 } 4966 4967 struct Kind2Unsigned { 4968 using argument_type = OpenMPDirectiveKind; 4969 unsigned operator()(argument_type DK) { return unsigned(DK); } 4970 }; 4971 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4972 ArrayRef<OMPClause *> Clauses, 4973 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4974 bool ErrorFound = false; 4975 unsigned NamedModifiersNumber = 0; 4976 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4977 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4978 SmallVector<SourceLocation, 4> NameModifierLoc; 4979 for (const OMPClause *C : Clauses) { 4980 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4981 // At most one if clause without a directive-name-modifier can appear on 4982 // the directive. 4983 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4984 if (FoundNameModifiers[CurNM]) { 4985 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4986 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4987 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4988 ErrorFound = true; 4989 } else if (CurNM != OMPD_unknown) { 4990 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4991 ++NamedModifiersNumber; 4992 } 4993 FoundNameModifiers[CurNM] = IC; 4994 if (CurNM == OMPD_unknown) 4995 continue; 4996 // Check if the specified name modifier is allowed for the current 4997 // directive. 4998 // At most one if clause with the particular directive-name-modifier can 4999 // appear on the directive. 5000 if (!llvm::is_contained(AllowedNameModifiers, CurNM)) { 5001 S.Diag(IC->getNameModifierLoc(), 5002 diag::err_omp_wrong_if_directive_name_modifier) 5003 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 5004 ErrorFound = true; 5005 } 5006 } 5007 } 5008 // If any if clause on the directive includes a directive-name-modifier then 5009 // all if clauses on the directive must include a directive-name-modifier. 5010 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 5011 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 5012 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 5013 diag::err_omp_no_more_if_clause); 5014 } else { 5015 std::string Values; 5016 std::string Sep(", "); 5017 unsigned AllowedCnt = 0; 5018 unsigned TotalAllowedNum = 5019 AllowedNameModifiers.size() - NamedModifiersNumber; 5020 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 5021 ++Cnt) { 5022 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 5023 if (!FoundNameModifiers[NM]) { 5024 Values += "'"; 5025 Values += getOpenMPDirectiveName(NM); 5026 Values += "'"; 5027 if (AllowedCnt + 2 == TotalAllowedNum) 5028 Values += " or "; 5029 else if (AllowedCnt + 1 != TotalAllowedNum) 5030 Values += Sep; 5031 ++AllowedCnt; 5032 } 5033 } 5034 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 5035 diag::err_omp_unnamed_if_clause) 5036 << (TotalAllowedNum > 1) << Values; 5037 } 5038 for (SourceLocation Loc : NameModifierLoc) { 5039 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 5040 } 5041 ErrorFound = true; 5042 } 5043 return ErrorFound; 5044 } 5045 5046 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 5047 SourceLocation &ELoc, 5048 SourceRange &ERange, 5049 bool AllowArraySection) { 5050 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 5051 RefExpr->containsUnexpandedParameterPack()) 5052 return std::make_pair(nullptr, true); 5053 5054 // OpenMP [3.1, C/C++] 5055 // A list item is a variable name. 5056 // OpenMP [2.9.3.3, Restrictions, p.1] 5057 // A variable that is part of another variable (as an array or 5058 // structure element) cannot appear in a private clause. 5059 RefExpr = RefExpr->IgnoreParens(); 5060 enum { 5061 NoArrayExpr = -1, 5062 ArraySubscript = 0, 5063 OMPArraySection = 1 5064 } IsArrayExpr = NoArrayExpr; 5065 if (AllowArraySection) { 5066 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 5067 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 5068 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5069 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5070 RefExpr = Base; 5071 IsArrayExpr = ArraySubscript; 5072 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 5073 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 5074 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 5075 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 5076 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5077 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5078 RefExpr = Base; 5079 IsArrayExpr = OMPArraySection; 5080 } 5081 } 5082 ELoc = RefExpr->getExprLoc(); 5083 ERange = RefExpr->getSourceRange(); 5084 RefExpr = RefExpr->IgnoreParenImpCasts(); 5085 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 5086 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 5087 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 5088 (S.getCurrentThisType().isNull() || !ME || 5089 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 5090 !isa<FieldDecl>(ME->getMemberDecl()))) { 5091 if (IsArrayExpr != NoArrayExpr) { 5092 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 5093 << ERange; 5094 } else { 5095 S.Diag(ELoc, 5096 AllowArraySection 5097 ? diag::err_omp_expected_var_name_member_expr_or_array_item 5098 : diag::err_omp_expected_var_name_member_expr) 5099 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 5100 } 5101 return std::make_pair(nullptr, false); 5102 } 5103 return std::make_pair( 5104 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 5105 } 5106 5107 namespace { 5108 /// Checks if the allocator is used in uses_allocators clause to be allowed in 5109 /// target regions. 5110 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 5111 DSAStackTy *S = nullptr; 5112 5113 public: 5114 bool VisitDeclRefExpr(const DeclRefExpr *E) { 5115 return S->isUsesAllocatorsDecl(E->getDecl()) 5116 .getValueOr( 5117 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 5118 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 5119 } 5120 bool VisitStmt(const Stmt *S) { 5121 for (const Stmt *Child : S->children()) { 5122 if (Child && Visit(Child)) 5123 return true; 5124 } 5125 return false; 5126 } 5127 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 5128 }; 5129 } // namespace 5130 5131 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 5132 ArrayRef<OMPClause *> Clauses) { 5133 assert(!S.CurContext->isDependentContext() && 5134 "Expected non-dependent context."); 5135 auto AllocateRange = 5136 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 5137 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 5138 DeclToCopy; 5139 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 5140 return isOpenMPPrivate(C->getClauseKind()); 5141 }); 5142 for (OMPClause *Cl : PrivateRange) { 5143 MutableArrayRef<Expr *>::iterator I, It, Et; 5144 if (Cl->getClauseKind() == OMPC_private) { 5145 auto *PC = cast<OMPPrivateClause>(Cl); 5146 I = PC->private_copies().begin(); 5147 It = PC->varlist_begin(); 5148 Et = PC->varlist_end(); 5149 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 5150 auto *PC = cast<OMPFirstprivateClause>(Cl); 5151 I = PC->private_copies().begin(); 5152 It = PC->varlist_begin(); 5153 Et = PC->varlist_end(); 5154 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 5155 auto *PC = cast<OMPLastprivateClause>(Cl); 5156 I = PC->private_copies().begin(); 5157 It = PC->varlist_begin(); 5158 Et = PC->varlist_end(); 5159 } else if (Cl->getClauseKind() == OMPC_linear) { 5160 auto *PC = cast<OMPLinearClause>(Cl); 5161 I = PC->privates().begin(); 5162 It = PC->varlist_begin(); 5163 Et = PC->varlist_end(); 5164 } else if (Cl->getClauseKind() == OMPC_reduction) { 5165 auto *PC = cast<OMPReductionClause>(Cl); 5166 I = PC->privates().begin(); 5167 It = PC->varlist_begin(); 5168 Et = PC->varlist_end(); 5169 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 5170 auto *PC = cast<OMPTaskReductionClause>(Cl); 5171 I = PC->privates().begin(); 5172 It = PC->varlist_begin(); 5173 Et = PC->varlist_end(); 5174 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 5175 auto *PC = cast<OMPInReductionClause>(Cl); 5176 I = PC->privates().begin(); 5177 It = PC->varlist_begin(); 5178 Et = PC->varlist_end(); 5179 } else { 5180 llvm_unreachable("Expected private clause."); 5181 } 5182 for (Expr *E : llvm::make_range(It, Et)) { 5183 if (!*I) { 5184 ++I; 5185 continue; 5186 } 5187 SourceLocation ELoc; 5188 SourceRange ERange; 5189 Expr *SimpleRefExpr = E; 5190 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 5191 /*AllowArraySection=*/true); 5192 DeclToCopy.try_emplace(Res.first, 5193 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 5194 ++I; 5195 } 5196 } 5197 for (OMPClause *C : AllocateRange) { 5198 auto *AC = cast<OMPAllocateClause>(C); 5199 if (S.getLangOpts().OpenMP >= 50 && 5200 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 5201 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 5202 AC->getAllocator()) { 5203 Expr *Allocator = AC->getAllocator(); 5204 // OpenMP, 2.12.5 target Construct 5205 // Memory allocators that do not appear in a uses_allocators clause cannot 5206 // appear as an allocator in an allocate clause or be used in the target 5207 // region unless a requires directive with the dynamic_allocators clause 5208 // is present in the same compilation unit. 5209 AllocatorChecker Checker(Stack); 5210 if (Checker.Visit(Allocator)) 5211 S.Diag(Allocator->getExprLoc(), 5212 diag::err_omp_allocator_not_in_uses_allocators) 5213 << Allocator->getSourceRange(); 5214 } 5215 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5216 getAllocatorKind(S, Stack, AC->getAllocator()); 5217 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5218 // For task, taskloop or target directives, allocation requests to memory 5219 // allocators with the trait access set to thread result in unspecified 5220 // behavior. 5221 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5222 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5223 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5224 S.Diag(AC->getAllocator()->getExprLoc(), 5225 diag::warn_omp_allocate_thread_on_task_target_directive) 5226 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5227 } 5228 for (Expr *E : AC->varlists()) { 5229 SourceLocation ELoc; 5230 SourceRange ERange; 5231 Expr *SimpleRefExpr = E; 5232 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5233 ValueDecl *VD = Res.first; 5234 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5235 if (!isOpenMPPrivate(Data.CKind)) { 5236 S.Diag(E->getExprLoc(), 5237 diag::err_omp_expected_private_copy_for_allocate); 5238 continue; 5239 } 5240 VarDecl *PrivateVD = DeclToCopy[VD]; 5241 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5242 AllocatorKind, AC->getAllocator())) 5243 continue; 5244 // Placeholder until allocate clause supports align modifier. 5245 Expr *Alignment = nullptr; 5246 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5247 Alignment, E->getSourceRange()); 5248 } 5249 } 5250 } 5251 5252 namespace { 5253 /// Rewrite statements and expressions for Sema \p Actions CurContext. 5254 /// 5255 /// Used to wrap already parsed statements/expressions into a new CapturedStmt 5256 /// context. DeclRefExpr used inside the new context are changed to refer to the 5257 /// captured variable instead. 5258 class CaptureVars : public TreeTransform<CaptureVars> { 5259 using BaseTransform = TreeTransform<CaptureVars>; 5260 5261 public: 5262 CaptureVars(Sema &Actions) : BaseTransform(Actions) {} 5263 5264 bool AlwaysRebuild() { return true; } 5265 }; 5266 } // namespace 5267 5268 static VarDecl *precomputeExpr(Sema &Actions, 5269 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E, 5270 StringRef Name) { 5271 Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E)); 5272 VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr, 5273 dyn_cast<DeclRefExpr>(E->IgnoreImplicit())); 5274 auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess( 5275 Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {}))); 5276 Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false); 5277 BodyStmts.push_back(NewDeclStmt); 5278 return NewVar; 5279 } 5280 5281 /// Create a closure that computes the number of iterations of a loop. 5282 /// 5283 /// \param Actions The Sema object. 5284 /// \param LogicalTy Type for the logical iteration number. 5285 /// \param Rel Comparison operator of the loop condition. 5286 /// \param StartExpr Value of the loop counter at the first iteration. 5287 /// \param StopExpr Expression the loop counter is compared against in the loop 5288 /// condition. \param StepExpr Amount of increment after each iteration. 5289 /// 5290 /// \return Closure (CapturedStmt) of the distance calculation. 5291 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy, 5292 BinaryOperator::Opcode Rel, 5293 Expr *StartExpr, Expr *StopExpr, 5294 Expr *StepExpr) { 5295 ASTContext &Ctx = Actions.getASTContext(); 5296 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy); 5297 5298 // Captured regions currently don't support return values, we use an 5299 // out-parameter instead. All inputs are implicit captures. 5300 // TODO: Instead of capturing each DeclRefExpr occurring in 5301 // StartExpr/StopExpr/Step, these could also be passed as a value capture. 5302 QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy); 5303 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy}, 5304 {StringRef(), QualType()}}; 5305 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5306 5307 Stmt *Body; 5308 { 5309 Sema::CompoundScopeRAII CompoundScope(Actions); 5310 CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext); 5311 5312 // Get the LValue expression for the result. 5313 ImplicitParamDecl *DistParam = CS->getParam(0); 5314 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr( 5315 DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5316 5317 SmallVector<Stmt *, 4> BodyStmts; 5318 5319 // Capture all referenced variable references. 5320 // TODO: Instead of computing NewStart/NewStop/NewStep inside the 5321 // CapturedStmt, we could compute them before and capture the result, to be 5322 // used jointly with the LoopVar function. 5323 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start"); 5324 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop"); 5325 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step"); 5326 auto BuildVarRef = [&](VarDecl *VD) { 5327 return buildDeclRefExpr(Actions, VD, VD->getType(), {}); 5328 }; 5329 5330 IntegerLiteral *Zero = IntegerLiteral::Create( 5331 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {}); 5332 Expr *Dist; 5333 if (Rel == BO_NE) { 5334 // When using a != comparison, the increment can be +1 or -1. This can be 5335 // dynamic at runtime, so we need to check for the direction. 5336 Expr *IsNegStep = AssertSuccess( 5337 Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero)); 5338 5339 // Positive increment. 5340 Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp( 5341 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5342 ForwardRange = AssertSuccess( 5343 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange)); 5344 Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp( 5345 nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep))); 5346 5347 // Negative increment. 5348 Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp( 5349 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5350 BackwardRange = AssertSuccess( 5351 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange)); 5352 Expr *NegIncAmount = AssertSuccess( 5353 Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep))); 5354 Expr *BackwardDist = AssertSuccess( 5355 Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount)); 5356 5357 // Use the appropriate case. 5358 Dist = AssertSuccess(Actions.ActOnConditionalOp( 5359 {}, {}, IsNegStep, BackwardDist, ForwardDist)); 5360 } else { 5361 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) && 5362 "Expected one of these relational operators"); 5363 5364 // We can derive the direction from any other comparison operator. It is 5365 // non well-formed OpenMP if Step increments/decrements in the other 5366 // directions. Whether at least the first iteration passes the loop 5367 // condition. 5368 Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp( 5369 nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5370 5371 // Compute the range between first and last counter value. 5372 Expr *Range; 5373 if (Rel == BO_GE || Rel == BO_GT) 5374 Range = AssertSuccess(Actions.BuildBinOp( 5375 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5376 else 5377 Range = AssertSuccess(Actions.BuildBinOp( 5378 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5379 5380 // Ensure unsigned range space. 5381 Range = 5382 AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range)); 5383 5384 if (Rel == BO_LE || Rel == BO_GE) { 5385 // Add one to the range if the relational operator is inclusive. 5386 Range = AssertSuccess(Actions.BuildBinOp( 5387 nullptr, {}, BO_Add, Range, 5388 Actions.ActOnIntegerConstant(SourceLocation(), 1).get())); 5389 } 5390 5391 // Divide by the absolute step amount. 5392 Expr *Divisor = BuildVarRef(NewStep); 5393 if (Rel == BO_GE || Rel == BO_GT) 5394 Divisor = 5395 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor)); 5396 Dist = AssertSuccess( 5397 Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor)); 5398 5399 // If there is not at least one iteration, the range contains garbage. Fix 5400 // to zero in this case. 5401 Dist = AssertSuccess( 5402 Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero)); 5403 } 5404 5405 // Assign the result to the out-parameter. 5406 Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp( 5407 Actions.getCurScope(), {}, BO_Assign, DistRef, Dist)); 5408 BodyStmts.push_back(ResultAssign); 5409 5410 Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false)); 5411 } 5412 5413 return cast<CapturedStmt>( 5414 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5415 } 5416 5417 /// Create a closure that computes the loop variable from the logical iteration 5418 /// number. 5419 /// 5420 /// \param Actions The Sema object. 5421 /// \param LoopVarTy Type for the loop variable used for result value. 5422 /// \param LogicalTy Type for the logical iteration number. 5423 /// \param StartExpr Value of the loop counter at the first iteration. 5424 /// \param Step Amount of increment after each iteration. 5425 /// \param Deref Whether the loop variable is a dereference of the loop 5426 /// counter variable. 5427 /// 5428 /// \return Closure (CapturedStmt) of the loop value calculation. 5429 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy, 5430 QualType LogicalTy, 5431 DeclRefExpr *StartExpr, Expr *Step, 5432 bool Deref) { 5433 ASTContext &Ctx = Actions.getASTContext(); 5434 5435 // Pass the result as an out-parameter. Passing as return value would require 5436 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to 5437 // invoke a copy constructor. 5438 QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy); 5439 Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy}, 5440 {"Logical", LogicalTy}, 5441 {StringRef(), QualType()}}; 5442 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5443 5444 // Capture the initial iterator which represents the LoopVar value at the 5445 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update 5446 // it in every iteration, capture it by value before it is modified. 5447 VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl()); 5448 bool Invalid = Actions.tryCaptureVariable(StartVar, {}, 5449 Sema::TryCapture_ExplicitByVal, {}); 5450 (void)Invalid; 5451 assert(!Invalid && "Expecting capture-by-value to work."); 5452 5453 Expr *Body; 5454 { 5455 Sema::CompoundScopeRAII CompoundScope(Actions); 5456 auto *CS = cast<CapturedDecl>(Actions.CurContext); 5457 5458 ImplicitParamDecl *TargetParam = CS->getParam(0); 5459 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr( 5460 TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5461 ImplicitParamDecl *IndvarParam = CS->getParam(1); 5462 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr( 5463 IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5464 5465 // Capture the Start expression. 5466 CaptureVars Recap(Actions); 5467 Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr)); 5468 Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step)); 5469 5470 Expr *Skip = AssertSuccess( 5471 Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef)); 5472 // TODO: Explicitly cast to the iterator's difference_type instead of 5473 // relying on implicit conversion. 5474 Expr *Advanced = 5475 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip)); 5476 5477 if (Deref) { 5478 // For range-based for-loops convert the loop counter value to a concrete 5479 // loop variable value by dereferencing the iterator. 5480 Advanced = 5481 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced)); 5482 } 5483 5484 // Assign the result to the output parameter. 5485 Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {}, 5486 BO_Assign, TargetRef, Advanced)); 5487 } 5488 return cast<CapturedStmt>( 5489 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5490 } 5491 5492 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) { 5493 ASTContext &Ctx = getASTContext(); 5494 5495 // Extract the common elements of ForStmt and CXXForRangeStmt: 5496 // Loop variable, repeat condition, increment 5497 Expr *Cond, *Inc; 5498 VarDecl *LIVDecl, *LUVDecl; 5499 if (auto *For = dyn_cast<ForStmt>(AStmt)) { 5500 Stmt *Init = For->getInit(); 5501 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) { 5502 // For statement declares loop variable. 5503 LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl()); 5504 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) { 5505 // For statement reuses variable. 5506 assert(LCAssign->getOpcode() == BO_Assign && 5507 "init part must be a loop variable assignment"); 5508 auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS()); 5509 LIVDecl = cast<VarDecl>(CounterRef->getDecl()); 5510 } else 5511 llvm_unreachable("Cannot determine loop variable"); 5512 LUVDecl = LIVDecl; 5513 5514 Cond = For->getCond(); 5515 Inc = For->getInc(); 5516 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) { 5517 DeclStmt *BeginStmt = RangeFor->getBeginStmt(); 5518 LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl()); 5519 LUVDecl = RangeFor->getLoopVariable(); 5520 5521 Cond = RangeFor->getCond(); 5522 Inc = RangeFor->getInc(); 5523 } else 5524 llvm_unreachable("unhandled kind of loop"); 5525 5526 QualType CounterTy = LIVDecl->getType(); 5527 QualType LVTy = LUVDecl->getType(); 5528 5529 // Analyze the loop condition. 5530 Expr *LHS, *RHS; 5531 BinaryOperator::Opcode CondRel; 5532 Cond = Cond->IgnoreImplicit(); 5533 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) { 5534 LHS = CondBinExpr->getLHS(); 5535 RHS = CondBinExpr->getRHS(); 5536 CondRel = CondBinExpr->getOpcode(); 5537 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) { 5538 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands"); 5539 LHS = CondCXXOp->getArg(0); 5540 RHS = CondCXXOp->getArg(1); 5541 switch (CondCXXOp->getOperator()) { 5542 case OO_ExclaimEqual: 5543 CondRel = BO_NE; 5544 break; 5545 case OO_Less: 5546 CondRel = BO_LT; 5547 break; 5548 case OO_LessEqual: 5549 CondRel = BO_LE; 5550 break; 5551 case OO_Greater: 5552 CondRel = BO_GT; 5553 break; 5554 case OO_GreaterEqual: 5555 CondRel = BO_GE; 5556 break; 5557 default: 5558 llvm_unreachable("unexpected iterator operator"); 5559 } 5560 } else 5561 llvm_unreachable("unexpected loop condition"); 5562 5563 // Normalize such that the loop counter is on the LHS. 5564 if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) || 5565 cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) { 5566 std::swap(LHS, RHS); 5567 CondRel = BinaryOperator::reverseComparisonOp(CondRel); 5568 } 5569 auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit()); 5570 5571 // Decide the bit width for the logical iteration counter. By default use the 5572 // unsigned ptrdiff_t integer size (for iterators and pointers). 5573 // TODO: For iterators, use iterator::difference_type, 5574 // std::iterator_traits<>::difference_type or decltype(it - end). 5575 QualType LogicalTy = Ctx.getUnsignedPointerDiffType(); 5576 if (CounterTy->isIntegerType()) { 5577 unsigned BitWidth = Ctx.getIntWidth(CounterTy); 5578 LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false); 5579 } 5580 5581 // Analyze the loop increment. 5582 Expr *Step; 5583 if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) { 5584 int Direction; 5585 switch (IncUn->getOpcode()) { 5586 case UO_PreInc: 5587 case UO_PostInc: 5588 Direction = 1; 5589 break; 5590 case UO_PreDec: 5591 case UO_PostDec: 5592 Direction = -1; 5593 break; 5594 default: 5595 llvm_unreachable("unhandled unary increment operator"); 5596 } 5597 Step = IntegerLiteral::Create( 5598 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {}); 5599 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) { 5600 if (IncBin->getOpcode() == BO_AddAssign) { 5601 Step = IncBin->getRHS(); 5602 } else if (IncBin->getOpcode() == BO_SubAssign) { 5603 Step = 5604 AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS())); 5605 } else 5606 llvm_unreachable("unhandled binary increment operator"); 5607 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) { 5608 switch (CondCXXOp->getOperator()) { 5609 case OO_PlusPlus: 5610 Step = IntegerLiteral::Create( 5611 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5612 break; 5613 case OO_MinusMinus: 5614 Step = IntegerLiteral::Create( 5615 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {}); 5616 break; 5617 case OO_PlusEqual: 5618 Step = CondCXXOp->getArg(1); 5619 break; 5620 case OO_MinusEqual: 5621 Step = AssertSuccess( 5622 BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1))); 5623 break; 5624 default: 5625 llvm_unreachable("unhandled overloaded increment operator"); 5626 } 5627 } else 5628 llvm_unreachable("unknown increment expression"); 5629 5630 CapturedStmt *DistanceFunc = 5631 buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step); 5632 CapturedStmt *LoopVarFunc = buildLoopVarFunc( 5633 *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt)); 5634 DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue, 5635 {}, nullptr, nullptr, {}, nullptr); 5636 return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc, 5637 LoopVarFunc, LVRef); 5638 } 5639 5640 StmtResult Sema::ActOnOpenMPLoopnest(Stmt *AStmt) { 5641 // Handle a literal loop. 5642 if (isa<ForStmt>(AStmt) || isa<CXXForRangeStmt>(AStmt)) 5643 return ActOnOpenMPCanonicalLoop(AStmt); 5644 5645 // If not a literal loop, it must be the result of a loop transformation. 5646 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(AStmt); 5647 assert( 5648 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) && 5649 "Loop transformation directive expected"); 5650 return LoopTransform; 5651 } 5652 5653 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 5654 CXXScopeSpec &MapperIdScopeSpec, 5655 const DeclarationNameInfo &MapperId, 5656 QualType Type, 5657 Expr *UnresolvedMapper); 5658 5659 /// Perform DFS through the structure/class data members trying to find 5660 /// member(s) with user-defined 'default' mapper and generate implicit map 5661 /// clauses for such members with the found 'default' mapper. 5662 static void 5663 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack, 5664 SmallVectorImpl<OMPClause *> &Clauses) { 5665 // Check for the deault mapper for data members. 5666 if (S.getLangOpts().OpenMP < 50) 5667 return; 5668 SmallVector<OMPClause *, 4> ImplicitMaps; 5669 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) { 5670 auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]); 5671 if (!C) 5672 continue; 5673 SmallVector<Expr *, 4> SubExprs; 5674 auto *MI = C->mapperlist_begin(); 5675 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End; 5676 ++I, ++MI) { 5677 // Expression is mapped using mapper - skip it. 5678 if (*MI) 5679 continue; 5680 Expr *E = *I; 5681 // Expression is dependent - skip it, build the mapper when it gets 5682 // instantiated. 5683 if (E->isTypeDependent() || E->isValueDependent() || 5684 E->containsUnexpandedParameterPack()) 5685 continue; 5686 // Array section - need to check for the mapping of the array section 5687 // element. 5688 QualType CanonType = E->getType().getCanonicalType(); 5689 if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) { 5690 const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts()); 5691 QualType BaseType = 5692 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 5693 QualType ElemType; 5694 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 5695 ElemType = ATy->getElementType(); 5696 else 5697 ElemType = BaseType->getPointeeType(); 5698 CanonType = ElemType; 5699 } 5700 5701 // DFS over data members in structures/classes. 5702 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types( 5703 1, {CanonType, nullptr}); 5704 llvm::DenseMap<const Type *, Expr *> Visited; 5705 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain( 5706 1, {nullptr, 1}); 5707 while (!Types.empty()) { 5708 QualType BaseType; 5709 FieldDecl *CurFD; 5710 std::tie(BaseType, CurFD) = Types.pop_back_val(); 5711 while (ParentChain.back().second == 0) 5712 ParentChain.pop_back(); 5713 --ParentChain.back().second; 5714 if (BaseType.isNull()) 5715 continue; 5716 // Only structs/classes are allowed to have mappers. 5717 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl(); 5718 if (!RD) 5719 continue; 5720 auto It = Visited.find(BaseType.getTypePtr()); 5721 if (It == Visited.end()) { 5722 // Try to find the associated user-defined mapper. 5723 CXXScopeSpec MapperIdScopeSpec; 5724 DeclarationNameInfo DefaultMapperId; 5725 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier( 5726 &S.Context.Idents.get("default"))); 5727 DefaultMapperId.setLoc(E->getExprLoc()); 5728 ExprResult ER = buildUserDefinedMapperRef( 5729 S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId, 5730 BaseType, /*UnresolvedMapper=*/nullptr); 5731 if (ER.isInvalid()) 5732 continue; 5733 It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first; 5734 } 5735 // Found default mapper. 5736 if (It->second) { 5737 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType, 5738 VK_LValue, OK_Ordinary, E); 5739 OE->setIsUnique(/*V=*/true); 5740 Expr *BaseExpr = OE; 5741 for (const auto &P : ParentChain) { 5742 if (P.first) { 5743 BaseExpr = S.BuildMemberExpr( 5744 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5745 NestedNameSpecifierLoc(), SourceLocation(), P.first, 5746 DeclAccessPair::make(P.first, P.first->getAccess()), 5747 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5748 P.first->getType(), VK_LValue, OK_Ordinary); 5749 BaseExpr = S.DefaultLvalueConversion(BaseExpr).get(); 5750 } 5751 } 5752 if (CurFD) 5753 BaseExpr = S.BuildMemberExpr( 5754 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5755 NestedNameSpecifierLoc(), SourceLocation(), CurFD, 5756 DeclAccessPair::make(CurFD, CurFD->getAccess()), 5757 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5758 CurFD->getType(), VK_LValue, OK_Ordinary); 5759 SubExprs.push_back(BaseExpr); 5760 continue; 5761 } 5762 // Check for the "default" mapper for data members. 5763 bool FirstIter = true; 5764 for (FieldDecl *FD : RD->fields()) { 5765 if (!FD) 5766 continue; 5767 QualType FieldTy = FD->getType(); 5768 if (FieldTy.isNull() || 5769 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType())) 5770 continue; 5771 if (FirstIter) { 5772 FirstIter = false; 5773 ParentChain.emplace_back(CurFD, 1); 5774 } else { 5775 ++ParentChain.back().second; 5776 } 5777 Types.emplace_back(FieldTy, FD); 5778 } 5779 } 5780 } 5781 if (SubExprs.empty()) 5782 continue; 5783 CXXScopeSpec MapperIdScopeSpec; 5784 DeclarationNameInfo MapperId; 5785 if (OMPClause *NewClause = S.ActOnOpenMPMapClause( 5786 C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), 5787 MapperIdScopeSpec, MapperId, C->getMapType(), 5788 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5789 SubExprs, OMPVarListLocTy())) 5790 Clauses.push_back(NewClause); 5791 } 5792 } 5793 5794 StmtResult Sema::ActOnOpenMPExecutableDirective( 5795 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5796 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5797 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5798 StmtResult Res = StmtError(); 5799 OpenMPBindClauseKind BindKind = OMPC_BIND_unknown; 5800 if (const OMPBindClause *BC = 5801 OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses)) 5802 BindKind = BC->getBindKind(); 5803 // First check CancelRegion which is then used in checkNestingOfRegions. 5804 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5805 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5806 BindKind, StartLoc)) 5807 return StmtError(); 5808 5809 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5810 VarsWithInheritedDSAType VarsWithInheritedDSA; 5811 bool ErrorFound = false; 5812 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5813 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5814 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master && 5815 Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) { 5816 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5817 5818 // Check default data sharing attributes for referenced variables. 5819 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5820 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5821 Stmt *S = AStmt; 5822 while (--ThisCaptureLevel >= 0) 5823 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5824 DSAChecker.Visit(S); 5825 if (!isOpenMPTargetDataManagementDirective(Kind) && 5826 !isOpenMPTaskingDirective(Kind)) { 5827 // Visit subcaptures to generate implicit clauses for captured vars. 5828 auto *CS = cast<CapturedStmt>(AStmt); 5829 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5830 getOpenMPCaptureRegions(CaptureRegions, Kind); 5831 // Ignore outer tasking regions for target directives. 5832 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5833 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5834 DSAChecker.visitSubCaptures(CS); 5835 } 5836 if (DSAChecker.isErrorFound()) 5837 return StmtError(); 5838 // Generate list of implicitly defined firstprivate variables. 5839 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5840 5841 SmallVector<Expr *, 4> ImplicitFirstprivates( 5842 DSAChecker.getImplicitFirstprivate().begin(), 5843 DSAChecker.getImplicitFirstprivate().end()); 5844 const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 5845 SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete]; 5846 SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 5847 ImplicitMapModifiers[DefaultmapKindNum]; 5848 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers> 5849 ImplicitMapModifiersLoc[DefaultmapKindNum]; 5850 // Get the original location of present modifier from Defaultmap clause. 5851 SourceLocation PresentModifierLocs[DefaultmapKindNum]; 5852 for (OMPClause *C : Clauses) { 5853 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C)) 5854 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present) 5855 PresentModifierLocs[DMC->getDefaultmapKind()] = 5856 DMC->getDefaultmapModifierLoc(); 5857 } 5858 for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) { 5859 auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC); 5860 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5861 ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap( 5862 Kind, static_cast<OpenMPMapClauseKind>(I)); 5863 ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end()); 5864 } 5865 ArrayRef<OpenMPMapModifierKind> ImplicitModifier = 5866 DSAChecker.getImplicitMapModifier(Kind); 5867 ImplicitMapModifiers[VC].append(ImplicitModifier.begin(), 5868 ImplicitModifier.end()); 5869 std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]), 5870 ImplicitModifier.size(), PresentModifierLocs[VC]); 5871 } 5872 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5873 for (OMPClause *C : Clauses) { 5874 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5875 for (Expr *E : IRC->taskgroup_descriptors()) 5876 if (E) 5877 ImplicitFirstprivates.emplace_back(E); 5878 } 5879 // OpenMP 5.0, 2.10.1 task Construct 5880 // [detach clause]... The event-handle will be considered as if it was 5881 // specified on a firstprivate clause. 5882 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5883 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5884 } 5885 if (!ImplicitFirstprivates.empty()) { 5886 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5887 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5888 SourceLocation())) { 5889 ClausesWithImplicit.push_back(Implicit); 5890 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5891 ImplicitFirstprivates.size(); 5892 } else { 5893 ErrorFound = true; 5894 } 5895 } 5896 // OpenMP 5.0 [2.19.7] 5897 // If a list item appears in a reduction, lastprivate or linear 5898 // clause on a combined target construct then it is treated as 5899 // if it also appears in a map clause with a map-type of tofrom 5900 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target && 5901 isOpenMPTargetExecutionDirective(Kind)) { 5902 SmallVector<Expr *, 4> ImplicitExprs; 5903 for (OMPClause *C : Clauses) { 5904 if (auto *RC = dyn_cast<OMPReductionClause>(C)) 5905 for (Expr *E : RC->varlists()) 5906 if (!isa<DeclRefExpr>(E->IgnoreParenImpCasts())) 5907 ImplicitExprs.emplace_back(E); 5908 } 5909 if (!ImplicitExprs.empty()) { 5910 ArrayRef<Expr *> Exprs = ImplicitExprs; 5911 CXXScopeSpec MapperIdScopeSpec; 5912 DeclarationNameInfo MapperId; 5913 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5914 OMPC_MAP_MODIFIER_unknown, SourceLocation(), MapperIdScopeSpec, 5915 MapperId, OMPC_MAP_tofrom, 5916 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5917 Exprs, OMPVarListLocTy(), /*NoDiagnose=*/true)) 5918 ClausesWithImplicit.emplace_back(Implicit); 5919 } 5920 } 5921 for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) { 5922 int ClauseKindCnt = -1; 5923 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) { 5924 ++ClauseKindCnt; 5925 if (ImplicitMap.empty()) 5926 continue; 5927 CXXScopeSpec MapperIdScopeSpec; 5928 DeclarationNameInfo MapperId; 5929 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5930 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5931 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I], 5932 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true, 5933 SourceLocation(), SourceLocation(), ImplicitMap, 5934 OMPVarListLocTy())) { 5935 ClausesWithImplicit.emplace_back(Implicit); 5936 ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() != 5937 ImplicitMap.size(); 5938 } else { 5939 ErrorFound = true; 5940 } 5941 } 5942 } 5943 // Build expressions for implicit maps of data members with 'default' 5944 // mappers. 5945 if (LangOpts.OpenMP >= 50) 5946 processImplicitMapsWithDefaultMappers(*this, DSAStack, 5947 ClausesWithImplicit); 5948 } 5949 5950 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5951 switch (Kind) { 5952 case OMPD_parallel: 5953 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5954 EndLoc); 5955 AllowedNameModifiers.push_back(OMPD_parallel); 5956 break; 5957 case OMPD_simd: 5958 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5959 VarsWithInheritedDSA); 5960 if (LangOpts.OpenMP >= 50) 5961 AllowedNameModifiers.push_back(OMPD_simd); 5962 break; 5963 case OMPD_tile: 5964 Res = 5965 ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5966 break; 5967 case OMPD_unroll: 5968 Res = ActOnOpenMPUnrollDirective(ClausesWithImplicit, AStmt, StartLoc, 5969 EndLoc); 5970 break; 5971 case OMPD_for: 5972 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5973 VarsWithInheritedDSA); 5974 break; 5975 case OMPD_for_simd: 5976 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5977 EndLoc, VarsWithInheritedDSA); 5978 if (LangOpts.OpenMP >= 50) 5979 AllowedNameModifiers.push_back(OMPD_simd); 5980 break; 5981 case OMPD_sections: 5982 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5983 EndLoc); 5984 break; 5985 case OMPD_section: 5986 assert(ClausesWithImplicit.empty() && 5987 "No clauses are allowed for 'omp section' directive"); 5988 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5989 break; 5990 case OMPD_single: 5991 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5992 EndLoc); 5993 break; 5994 case OMPD_master: 5995 assert(ClausesWithImplicit.empty() && 5996 "No clauses are allowed for 'omp master' directive"); 5997 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5998 break; 5999 case OMPD_masked: 6000 Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc, 6001 EndLoc); 6002 break; 6003 case OMPD_critical: 6004 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 6005 StartLoc, EndLoc); 6006 break; 6007 case OMPD_parallel_for: 6008 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 6009 EndLoc, VarsWithInheritedDSA); 6010 AllowedNameModifiers.push_back(OMPD_parallel); 6011 break; 6012 case OMPD_parallel_for_simd: 6013 Res = ActOnOpenMPParallelForSimdDirective( 6014 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6015 AllowedNameModifiers.push_back(OMPD_parallel); 6016 if (LangOpts.OpenMP >= 50) 6017 AllowedNameModifiers.push_back(OMPD_simd); 6018 break; 6019 case OMPD_parallel_master: 6020 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 6021 StartLoc, EndLoc); 6022 AllowedNameModifiers.push_back(OMPD_parallel); 6023 break; 6024 case OMPD_parallel_sections: 6025 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 6026 StartLoc, EndLoc); 6027 AllowedNameModifiers.push_back(OMPD_parallel); 6028 break; 6029 case OMPD_task: 6030 Res = 6031 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6032 AllowedNameModifiers.push_back(OMPD_task); 6033 break; 6034 case OMPD_taskyield: 6035 assert(ClausesWithImplicit.empty() && 6036 "No clauses are allowed for 'omp taskyield' directive"); 6037 assert(AStmt == nullptr && 6038 "No associated statement allowed for 'omp taskyield' directive"); 6039 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 6040 break; 6041 case OMPD_barrier: 6042 assert(ClausesWithImplicit.empty() && 6043 "No clauses are allowed for 'omp barrier' directive"); 6044 assert(AStmt == nullptr && 6045 "No associated statement allowed for 'omp barrier' directive"); 6046 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 6047 break; 6048 case OMPD_taskwait: 6049 assert(ClausesWithImplicit.empty() && 6050 "No clauses are allowed for 'omp taskwait' directive"); 6051 assert(AStmt == nullptr && 6052 "No associated statement allowed for 'omp taskwait' directive"); 6053 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 6054 break; 6055 case OMPD_taskgroup: 6056 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 6057 EndLoc); 6058 break; 6059 case OMPD_flush: 6060 assert(AStmt == nullptr && 6061 "No associated statement allowed for 'omp flush' directive"); 6062 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 6063 break; 6064 case OMPD_depobj: 6065 assert(AStmt == nullptr && 6066 "No associated statement allowed for 'omp depobj' directive"); 6067 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 6068 break; 6069 case OMPD_scan: 6070 assert(AStmt == nullptr && 6071 "No associated statement allowed for 'omp scan' directive"); 6072 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 6073 break; 6074 case OMPD_ordered: 6075 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 6076 EndLoc); 6077 break; 6078 case OMPD_atomic: 6079 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 6080 EndLoc); 6081 break; 6082 case OMPD_teams: 6083 Res = 6084 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6085 break; 6086 case OMPD_target: 6087 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 6088 EndLoc); 6089 AllowedNameModifiers.push_back(OMPD_target); 6090 break; 6091 case OMPD_target_parallel: 6092 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 6093 StartLoc, EndLoc); 6094 AllowedNameModifiers.push_back(OMPD_target); 6095 AllowedNameModifiers.push_back(OMPD_parallel); 6096 break; 6097 case OMPD_target_parallel_for: 6098 Res = ActOnOpenMPTargetParallelForDirective( 6099 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6100 AllowedNameModifiers.push_back(OMPD_target); 6101 AllowedNameModifiers.push_back(OMPD_parallel); 6102 break; 6103 case OMPD_cancellation_point: 6104 assert(ClausesWithImplicit.empty() && 6105 "No clauses are allowed for 'omp cancellation point' directive"); 6106 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 6107 "cancellation point' directive"); 6108 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 6109 break; 6110 case OMPD_cancel: 6111 assert(AStmt == nullptr && 6112 "No associated statement allowed for 'omp cancel' directive"); 6113 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 6114 CancelRegion); 6115 AllowedNameModifiers.push_back(OMPD_cancel); 6116 break; 6117 case OMPD_target_data: 6118 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 6119 EndLoc); 6120 AllowedNameModifiers.push_back(OMPD_target_data); 6121 break; 6122 case OMPD_target_enter_data: 6123 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 6124 EndLoc, AStmt); 6125 AllowedNameModifiers.push_back(OMPD_target_enter_data); 6126 break; 6127 case OMPD_target_exit_data: 6128 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 6129 EndLoc, AStmt); 6130 AllowedNameModifiers.push_back(OMPD_target_exit_data); 6131 break; 6132 case OMPD_taskloop: 6133 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6134 EndLoc, VarsWithInheritedDSA); 6135 AllowedNameModifiers.push_back(OMPD_taskloop); 6136 break; 6137 case OMPD_taskloop_simd: 6138 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6139 EndLoc, VarsWithInheritedDSA); 6140 AllowedNameModifiers.push_back(OMPD_taskloop); 6141 if (LangOpts.OpenMP >= 50) 6142 AllowedNameModifiers.push_back(OMPD_simd); 6143 break; 6144 case OMPD_master_taskloop: 6145 Res = ActOnOpenMPMasterTaskLoopDirective( 6146 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6147 AllowedNameModifiers.push_back(OMPD_taskloop); 6148 break; 6149 case OMPD_master_taskloop_simd: 6150 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 6151 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6152 AllowedNameModifiers.push_back(OMPD_taskloop); 6153 if (LangOpts.OpenMP >= 50) 6154 AllowedNameModifiers.push_back(OMPD_simd); 6155 break; 6156 case OMPD_parallel_master_taskloop: 6157 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 6158 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6159 AllowedNameModifiers.push_back(OMPD_taskloop); 6160 AllowedNameModifiers.push_back(OMPD_parallel); 6161 break; 6162 case OMPD_parallel_master_taskloop_simd: 6163 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 6164 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6165 AllowedNameModifiers.push_back(OMPD_taskloop); 6166 AllowedNameModifiers.push_back(OMPD_parallel); 6167 if (LangOpts.OpenMP >= 50) 6168 AllowedNameModifiers.push_back(OMPD_simd); 6169 break; 6170 case OMPD_distribute: 6171 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 6172 EndLoc, VarsWithInheritedDSA); 6173 break; 6174 case OMPD_target_update: 6175 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 6176 EndLoc, AStmt); 6177 AllowedNameModifiers.push_back(OMPD_target_update); 6178 break; 6179 case OMPD_distribute_parallel_for: 6180 Res = ActOnOpenMPDistributeParallelForDirective( 6181 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6182 AllowedNameModifiers.push_back(OMPD_parallel); 6183 break; 6184 case OMPD_distribute_parallel_for_simd: 6185 Res = ActOnOpenMPDistributeParallelForSimdDirective( 6186 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6187 AllowedNameModifiers.push_back(OMPD_parallel); 6188 if (LangOpts.OpenMP >= 50) 6189 AllowedNameModifiers.push_back(OMPD_simd); 6190 break; 6191 case OMPD_distribute_simd: 6192 Res = ActOnOpenMPDistributeSimdDirective( 6193 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6194 if (LangOpts.OpenMP >= 50) 6195 AllowedNameModifiers.push_back(OMPD_simd); 6196 break; 6197 case OMPD_target_parallel_for_simd: 6198 Res = ActOnOpenMPTargetParallelForSimdDirective( 6199 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6200 AllowedNameModifiers.push_back(OMPD_target); 6201 AllowedNameModifiers.push_back(OMPD_parallel); 6202 if (LangOpts.OpenMP >= 50) 6203 AllowedNameModifiers.push_back(OMPD_simd); 6204 break; 6205 case OMPD_target_simd: 6206 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6207 EndLoc, VarsWithInheritedDSA); 6208 AllowedNameModifiers.push_back(OMPD_target); 6209 if (LangOpts.OpenMP >= 50) 6210 AllowedNameModifiers.push_back(OMPD_simd); 6211 break; 6212 case OMPD_teams_distribute: 6213 Res = ActOnOpenMPTeamsDistributeDirective( 6214 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6215 break; 6216 case OMPD_teams_distribute_simd: 6217 Res = ActOnOpenMPTeamsDistributeSimdDirective( 6218 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6219 if (LangOpts.OpenMP >= 50) 6220 AllowedNameModifiers.push_back(OMPD_simd); 6221 break; 6222 case OMPD_teams_distribute_parallel_for_simd: 6223 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6224 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6225 AllowedNameModifiers.push_back(OMPD_parallel); 6226 if (LangOpts.OpenMP >= 50) 6227 AllowedNameModifiers.push_back(OMPD_simd); 6228 break; 6229 case OMPD_teams_distribute_parallel_for: 6230 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 6231 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6232 AllowedNameModifiers.push_back(OMPD_parallel); 6233 break; 6234 case OMPD_target_teams: 6235 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 6236 EndLoc); 6237 AllowedNameModifiers.push_back(OMPD_target); 6238 break; 6239 case OMPD_target_teams_distribute: 6240 Res = ActOnOpenMPTargetTeamsDistributeDirective( 6241 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6242 AllowedNameModifiers.push_back(OMPD_target); 6243 break; 6244 case OMPD_target_teams_distribute_parallel_for: 6245 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6246 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6247 AllowedNameModifiers.push_back(OMPD_target); 6248 AllowedNameModifiers.push_back(OMPD_parallel); 6249 break; 6250 case OMPD_target_teams_distribute_parallel_for_simd: 6251 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6252 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6253 AllowedNameModifiers.push_back(OMPD_target); 6254 AllowedNameModifiers.push_back(OMPD_parallel); 6255 if (LangOpts.OpenMP >= 50) 6256 AllowedNameModifiers.push_back(OMPD_simd); 6257 break; 6258 case OMPD_target_teams_distribute_simd: 6259 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 6260 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6261 AllowedNameModifiers.push_back(OMPD_target); 6262 if (LangOpts.OpenMP >= 50) 6263 AllowedNameModifiers.push_back(OMPD_simd); 6264 break; 6265 case OMPD_interop: 6266 assert(AStmt == nullptr && 6267 "No associated statement allowed for 'omp interop' directive"); 6268 Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc); 6269 break; 6270 case OMPD_dispatch: 6271 Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc, 6272 EndLoc); 6273 break; 6274 case OMPD_loop: 6275 Res = ActOnOpenMPGenericLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6276 EndLoc, VarsWithInheritedDSA); 6277 break; 6278 case OMPD_declare_target: 6279 case OMPD_end_declare_target: 6280 case OMPD_threadprivate: 6281 case OMPD_allocate: 6282 case OMPD_declare_reduction: 6283 case OMPD_declare_mapper: 6284 case OMPD_declare_simd: 6285 case OMPD_requires: 6286 case OMPD_declare_variant: 6287 case OMPD_begin_declare_variant: 6288 case OMPD_end_declare_variant: 6289 llvm_unreachable("OpenMP Directive is not allowed"); 6290 case OMPD_unknown: 6291 default: 6292 llvm_unreachable("Unknown OpenMP directive"); 6293 } 6294 6295 ErrorFound = Res.isInvalid() || ErrorFound; 6296 6297 // Check variables in the clauses if default(none) or 6298 // default(firstprivate) was specified. 6299 if (DSAStack->getDefaultDSA() == DSA_none || 6300 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6301 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 6302 for (OMPClause *C : Clauses) { 6303 switch (C->getClauseKind()) { 6304 case OMPC_num_threads: 6305 case OMPC_dist_schedule: 6306 // Do not analyse if no parent teams directive. 6307 if (isOpenMPTeamsDirective(Kind)) 6308 break; 6309 continue; 6310 case OMPC_if: 6311 if (isOpenMPTeamsDirective(Kind) && 6312 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 6313 break; 6314 if (isOpenMPParallelDirective(Kind) && 6315 isOpenMPTaskLoopDirective(Kind) && 6316 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 6317 break; 6318 continue; 6319 case OMPC_schedule: 6320 case OMPC_detach: 6321 break; 6322 case OMPC_grainsize: 6323 case OMPC_num_tasks: 6324 case OMPC_final: 6325 case OMPC_priority: 6326 case OMPC_novariants: 6327 case OMPC_nocontext: 6328 // Do not analyze if no parent parallel directive. 6329 if (isOpenMPParallelDirective(Kind)) 6330 break; 6331 continue; 6332 case OMPC_ordered: 6333 case OMPC_device: 6334 case OMPC_num_teams: 6335 case OMPC_thread_limit: 6336 case OMPC_hint: 6337 case OMPC_collapse: 6338 case OMPC_safelen: 6339 case OMPC_simdlen: 6340 case OMPC_sizes: 6341 case OMPC_default: 6342 case OMPC_proc_bind: 6343 case OMPC_private: 6344 case OMPC_firstprivate: 6345 case OMPC_lastprivate: 6346 case OMPC_shared: 6347 case OMPC_reduction: 6348 case OMPC_task_reduction: 6349 case OMPC_in_reduction: 6350 case OMPC_linear: 6351 case OMPC_aligned: 6352 case OMPC_copyin: 6353 case OMPC_copyprivate: 6354 case OMPC_nowait: 6355 case OMPC_untied: 6356 case OMPC_mergeable: 6357 case OMPC_allocate: 6358 case OMPC_read: 6359 case OMPC_write: 6360 case OMPC_update: 6361 case OMPC_capture: 6362 case OMPC_seq_cst: 6363 case OMPC_acq_rel: 6364 case OMPC_acquire: 6365 case OMPC_release: 6366 case OMPC_relaxed: 6367 case OMPC_depend: 6368 case OMPC_threads: 6369 case OMPC_simd: 6370 case OMPC_map: 6371 case OMPC_nogroup: 6372 case OMPC_defaultmap: 6373 case OMPC_to: 6374 case OMPC_from: 6375 case OMPC_use_device_ptr: 6376 case OMPC_use_device_addr: 6377 case OMPC_is_device_ptr: 6378 case OMPC_nontemporal: 6379 case OMPC_order: 6380 case OMPC_destroy: 6381 case OMPC_inclusive: 6382 case OMPC_exclusive: 6383 case OMPC_uses_allocators: 6384 case OMPC_affinity: 6385 case OMPC_bind: 6386 continue; 6387 case OMPC_allocator: 6388 case OMPC_flush: 6389 case OMPC_depobj: 6390 case OMPC_threadprivate: 6391 case OMPC_uniform: 6392 case OMPC_unknown: 6393 case OMPC_unified_address: 6394 case OMPC_unified_shared_memory: 6395 case OMPC_reverse_offload: 6396 case OMPC_dynamic_allocators: 6397 case OMPC_atomic_default_mem_order: 6398 case OMPC_device_type: 6399 case OMPC_match: 6400 case OMPC_when: 6401 default: 6402 llvm_unreachable("Unexpected clause"); 6403 } 6404 for (Stmt *CC : C->children()) { 6405 if (CC) 6406 DSAChecker.Visit(CC); 6407 } 6408 } 6409 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 6410 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 6411 } 6412 for (const auto &P : VarsWithInheritedDSA) { 6413 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 6414 continue; 6415 ErrorFound = true; 6416 if (DSAStack->getDefaultDSA() == DSA_none || 6417 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6418 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 6419 << P.first << P.second->getSourceRange(); 6420 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 6421 } else if (getLangOpts().OpenMP >= 50) { 6422 Diag(P.second->getExprLoc(), 6423 diag::err_omp_defaultmap_no_attr_for_variable) 6424 << P.first << P.second->getSourceRange(); 6425 Diag(DSAStack->getDefaultDSALocation(), 6426 diag::note_omp_defaultmap_attr_none); 6427 } 6428 } 6429 6430 if (!AllowedNameModifiers.empty()) 6431 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 6432 ErrorFound; 6433 6434 if (ErrorFound) 6435 return StmtError(); 6436 6437 if (!CurContext->isDependentContext() && 6438 isOpenMPTargetExecutionDirective(Kind) && 6439 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 6440 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 6441 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 6442 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 6443 // Register target to DSA Stack. 6444 DSAStack->addTargetDirLocation(StartLoc); 6445 } 6446 6447 return Res; 6448 } 6449 6450 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 6451 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 6452 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 6453 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 6454 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 6455 assert(Aligneds.size() == Alignments.size()); 6456 assert(Linears.size() == LinModifiers.size()); 6457 assert(Linears.size() == Steps.size()); 6458 if (!DG || DG.get().isNull()) 6459 return DeclGroupPtrTy(); 6460 6461 const int SimdId = 0; 6462 if (!DG.get().isSingleDecl()) { 6463 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6464 << SimdId; 6465 return DG; 6466 } 6467 Decl *ADecl = DG.get().getSingleDecl(); 6468 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6469 ADecl = FTD->getTemplatedDecl(); 6470 6471 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6472 if (!FD) { 6473 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 6474 return DeclGroupPtrTy(); 6475 } 6476 6477 // OpenMP [2.8.2, declare simd construct, Description] 6478 // The parameter of the simdlen clause must be a constant positive integer 6479 // expression. 6480 ExprResult SL; 6481 if (Simdlen) 6482 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 6483 // OpenMP [2.8.2, declare simd construct, Description] 6484 // The special this pointer can be used as if was one of the arguments to the 6485 // function in any of the linear, aligned, or uniform clauses. 6486 // The uniform clause declares one or more arguments to have an invariant 6487 // value for all concurrent invocations of the function in the execution of a 6488 // single SIMD loop. 6489 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 6490 const Expr *UniformedLinearThis = nullptr; 6491 for (const Expr *E : Uniforms) { 6492 E = E->IgnoreParenImpCasts(); 6493 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6494 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 6495 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6496 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6497 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 6498 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 6499 continue; 6500 } 6501 if (isa<CXXThisExpr>(E)) { 6502 UniformedLinearThis = E; 6503 continue; 6504 } 6505 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6506 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6507 } 6508 // OpenMP [2.8.2, declare simd construct, Description] 6509 // The aligned clause declares that the object to which each list item points 6510 // is aligned to the number of bytes expressed in the optional parameter of 6511 // the aligned clause. 6512 // The special this pointer can be used as if was one of the arguments to the 6513 // function in any of the linear, aligned, or uniform clauses. 6514 // The type of list items appearing in the aligned clause must be array, 6515 // pointer, reference to array, or reference to pointer. 6516 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 6517 const Expr *AlignedThis = nullptr; 6518 for (const Expr *E : Aligneds) { 6519 E = E->IgnoreParenImpCasts(); 6520 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6521 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6522 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6523 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6524 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6525 ->getCanonicalDecl() == CanonPVD) { 6526 // OpenMP [2.8.1, simd construct, Restrictions] 6527 // A list-item cannot appear in more than one aligned clause. 6528 if (AlignedArgs.count(CanonPVD) > 0) { 6529 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6530 << 1 << getOpenMPClauseName(OMPC_aligned) 6531 << E->getSourceRange(); 6532 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 6533 diag::note_omp_explicit_dsa) 6534 << getOpenMPClauseName(OMPC_aligned); 6535 continue; 6536 } 6537 AlignedArgs[CanonPVD] = E; 6538 QualType QTy = PVD->getType() 6539 .getNonReferenceType() 6540 .getUnqualifiedType() 6541 .getCanonicalType(); 6542 const Type *Ty = QTy.getTypePtrOrNull(); 6543 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 6544 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 6545 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 6546 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 6547 } 6548 continue; 6549 } 6550 } 6551 if (isa<CXXThisExpr>(E)) { 6552 if (AlignedThis) { 6553 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6554 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 6555 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 6556 << getOpenMPClauseName(OMPC_aligned); 6557 } 6558 AlignedThis = E; 6559 continue; 6560 } 6561 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6562 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6563 } 6564 // The optional parameter of the aligned clause, alignment, must be a constant 6565 // positive integer expression. If no optional parameter is specified, 6566 // implementation-defined default alignments for SIMD instructions on the 6567 // target platforms are assumed. 6568 SmallVector<const Expr *, 4> NewAligns; 6569 for (Expr *E : Alignments) { 6570 ExprResult Align; 6571 if (E) 6572 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 6573 NewAligns.push_back(Align.get()); 6574 } 6575 // OpenMP [2.8.2, declare simd construct, Description] 6576 // The linear clause declares one or more list items to be private to a SIMD 6577 // lane and to have a linear relationship with respect to the iteration space 6578 // of a loop. 6579 // The special this pointer can be used as if was one of the arguments to the 6580 // function in any of the linear, aligned, or uniform clauses. 6581 // When a linear-step expression is specified in a linear clause it must be 6582 // either a constant integer expression or an integer-typed parameter that is 6583 // specified in a uniform clause on the directive. 6584 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 6585 const bool IsUniformedThis = UniformedLinearThis != nullptr; 6586 auto MI = LinModifiers.begin(); 6587 for (const Expr *E : Linears) { 6588 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 6589 ++MI; 6590 E = E->IgnoreParenImpCasts(); 6591 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6592 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6593 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6594 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6595 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6596 ->getCanonicalDecl() == CanonPVD) { 6597 // OpenMP [2.15.3.7, linear Clause, Restrictions] 6598 // A list-item cannot appear in more than one linear clause. 6599 if (LinearArgs.count(CanonPVD) > 0) { 6600 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6601 << getOpenMPClauseName(OMPC_linear) 6602 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 6603 Diag(LinearArgs[CanonPVD]->getExprLoc(), 6604 diag::note_omp_explicit_dsa) 6605 << getOpenMPClauseName(OMPC_linear); 6606 continue; 6607 } 6608 // Each argument can appear in at most one uniform or linear clause. 6609 if (UniformedArgs.count(CanonPVD) > 0) { 6610 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6611 << getOpenMPClauseName(OMPC_linear) 6612 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 6613 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 6614 diag::note_omp_explicit_dsa) 6615 << getOpenMPClauseName(OMPC_uniform); 6616 continue; 6617 } 6618 LinearArgs[CanonPVD] = E; 6619 if (E->isValueDependent() || E->isTypeDependent() || 6620 E->isInstantiationDependent() || 6621 E->containsUnexpandedParameterPack()) 6622 continue; 6623 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 6624 PVD->getOriginalType(), 6625 /*IsDeclareSimd=*/true); 6626 continue; 6627 } 6628 } 6629 if (isa<CXXThisExpr>(E)) { 6630 if (UniformedLinearThis) { 6631 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6632 << getOpenMPClauseName(OMPC_linear) 6633 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 6634 << E->getSourceRange(); 6635 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 6636 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 6637 : OMPC_linear); 6638 continue; 6639 } 6640 UniformedLinearThis = E; 6641 if (E->isValueDependent() || E->isTypeDependent() || 6642 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6643 continue; 6644 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 6645 E->getType(), /*IsDeclareSimd=*/true); 6646 continue; 6647 } 6648 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6649 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6650 } 6651 Expr *Step = nullptr; 6652 Expr *NewStep = nullptr; 6653 SmallVector<Expr *, 4> NewSteps; 6654 for (Expr *E : Steps) { 6655 // Skip the same step expression, it was checked already. 6656 if (Step == E || !E) { 6657 NewSteps.push_back(E ? NewStep : nullptr); 6658 continue; 6659 } 6660 Step = E; 6661 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 6662 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6663 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6664 if (UniformedArgs.count(CanonPVD) == 0) { 6665 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 6666 << Step->getSourceRange(); 6667 } else if (E->isValueDependent() || E->isTypeDependent() || 6668 E->isInstantiationDependent() || 6669 E->containsUnexpandedParameterPack() || 6670 CanonPVD->getType()->hasIntegerRepresentation()) { 6671 NewSteps.push_back(Step); 6672 } else { 6673 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 6674 << Step->getSourceRange(); 6675 } 6676 continue; 6677 } 6678 NewStep = Step; 6679 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 6680 !Step->isInstantiationDependent() && 6681 !Step->containsUnexpandedParameterPack()) { 6682 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 6683 .get(); 6684 if (NewStep) 6685 NewStep = 6686 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 6687 } 6688 NewSteps.push_back(NewStep); 6689 } 6690 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 6691 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 6692 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 6693 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 6694 const_cast<Expr **>(Linears.data()), Linears.size(), 6695 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 6696 NewSteps.data(), NewSteps.size(), SR); 6697 ADecl->addAttr(NewAttr); 6698 return DG; 6699 } 6700 6701 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 6702 QualType NewType) { 6703 assert(NewType->isFunctionProtoType() && 6704 "Expected function type with prototype."); 6705 assert(FD->getType()->isFunctionNoProtoType() && 6706 "Expected function with type with no prototype."); 6707 assert(FDWithProto->getType()->isFunctionProtoType() && 6708 "Expected function with prototype."); 6709 // Synthesize parameters with the same types. 6710 FD->setType(NewType); 6711 SmallVector<ParmVarDecl *, 16> Params; 6712 for (const ParmVarDecl *P : FDWithProto->parameters()) { 6713 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 6714 SourceLocation(), nullptr, P->getType(), 6715 /*TInfo=*/nullptr, SC_None, nullptr); 6716 Param->setScopeInfo(0, Params.size()); 6717 Param->setImplicit(); 6718 Params.push_back(Param); 6719 } 6720 6721 FD->setParams(Params); 6722 } 6723 6724 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) { 6725 if (D->isInvalidDecl()) 6726 return; 6727 FunctionDecl *FD = nullptr; 6728 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6729 FD = UTemplDecl->getTemplatedDecl(); 6730 else 6731 FD = cast<FunctionDecl>(D); 6732 assert(FD && "Expected a function declaration!"); 6733 6734 // If we are instantiating templates we do *not* apply scoped assumptions but 6735 // only global ones. We apply scoped assumption to the template definition 6736 // though. 6737 if (!inTemplateInstantiation()) { 6738 for (AssumptionAttr *AA : OMPAssumeScoped) 6739 FD->addAttr(AA); 6740 } 6741 for (AssumptionAttr *AA : OMPAssumeGlobal) 6742 FD->addAttr(AA); 6743 } 6744 6745 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 6746 : TI(&TI), NameSuffix(TI.getMangledName()) {} 6747 6748 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 6749 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 6750 SmallVectorImpl<FunctionDecl *> &Bases) { 6751 if (!D.getIdentifier()) 6752 return; 6753 6754 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6755 6756 // Template specialization is an extension, check if we do it. 6757 bool IsTemplated = !TemplateParamLists.empty(); 6758 if (IsTemplated & 6759 !DVScope.TI->isExtensionActive( 6760 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 6761 return; 6762 6763 IdentifierInfo *BaseII = D.getIdentifier(); 6764 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 6765 LookupOrdinaryName); 6766 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 6767 6768 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 6769 QualType FType = TInfo->getType(); 6770 6771 bool IsConstexpr = 6772 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr; 6773 bool IsConsteval = 6774 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval; 6775 6776 for (auto *Candidate : Lookup) { 6777 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 6778 FunctionDecl *UDecl = nullptr; 6779 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) { 6780 auto *FTD = cast<FunctionTemplateDecl>(CandidateDecl); 6781 if (FTD->getTemplateParameters()->size() == TemplateParamLists.size()) 6782 UDecl = FTD->getTemplatedDecl(); 6783 } else if (!IsTemplated) 6784 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 6785 if (!UDecl) 6786 continue; 6787 6788 // Don't specialize constexpr/consteval functions with 6789 // non-constexpr/consteval functions. 6790 if (UDecl->isConstexpr() && !IsConstexpr) 6791 continue; 6792 if (UDecl->isConsteval() && !IsConsteval) 6793 continue; 6794 6795 QualType UDeclTy = UDecl->getType(); 6796 if (!UDeclTy->isDependentType()) { 6797 QualType NewType = Context.mergeFunctionTypes( 6798 FType, UDeclTy, /* OfBlockPointer */ false, 6799 /* Unqualified */ false, /* AllowCXX */ true); 6800 if (NewType.isNull()) 6801 continue; 6802 } 6803 6804 // Found a base! 6805 Bases.push_back(UDecl); 6806 } 6807 6808 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 6809 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 6810 // If no base was found we create a declaration that we use as base. 6811 if (Bases.empty() && UseImplicitBase) { 6812 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); 6813 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 6814 BaseD->setImplicit(true); 6815 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 6816 Bases.push_back(BaseTemplD->getTemplatedDecl()); 6817 else 6818 Bases.push_back(cast<FunctionDecl>(BaseD)); 6819 } 6820 6821 std::string MangledName; 6822 MangledName += D.getIdentifier()->getName(); 6823 MangledName += getOpenMPVariantManglingSeparatorStr(); 6824 MangledName += DVScope.NameSuffix; 6825 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 6826 6827 VariantII.setMangledOpenMPVariantName(true); 6828 D.SetIdentifier(&VariantII, D.getBeginLoc()); 6829 } 6830 6831 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 6832 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 6833 // Do not mark function as is used to prevent its emission if this is the 6834 // only place where it is used. 6835 EnterExpressionEvaluationContext Unevaluated( 6836 *this, Sema::ExpressionEvaluationContext::Unevaluated); 6837 6838 FunctionDecl *FD = nullptr; 6839 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6840 FD = UTemplDecl->getTemplatedDecl(); 6841 else 6842 FD = cast<FunctionDecl>(D); 6843 auto *VariantFuncRef = DeclRefExpr::Create( 6844 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 6845 /* RefersToEnclosingVariableOrCapture */ false, 6846 /* NameLoc */ FD->getLocation(), FD->getType(), 6847 ExprValueKind::VK_PRValue); 6848 6849 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6850 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 6851 Context, VariantFuncRef, DVScope.TI, 6852 /*NothingArgs=*/nullptr, /*NothingArgsSize=*/0, 6853 /*NeedDevicePtrArgs=*/nullptr, /*NeedDevicePtrArgsSize=*/0, 6854 /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0); 6855 for (FunctionDecl *BaseFD : Bases) 6856 BaseFD->addAttr(OMPDeclareVariantA); 6857 } 6858 6859 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 6860 SourceLocation LParenLoc, 6861 MultiExprArg ArgExprs, 6862 SourceLocation RParenLoc, Expr *ExecConfig) { 6863 // The common case is a regular call we do not want to specialize at all. Try 6864 // to make that case fast by bailing early. 6865 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 6866 if (!CE) 6867 return Call; 6868 6869 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 6870 if (!CalleeFnDecl) 6871 return Call; 6872 6873 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 6874 return Call; 6875 6876 ASTContext &Context = getASTContext(); 6877 std::function<void(StringRef)> DiagUnknownTrait = [this, 6878 CE](StringRef ISATrait) { 6879 // TODO Track the selector locations in a way that is accessible here to 6880 // improve the diagnostic location. 6881 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6882 << ISATrait; 6883 }; 6884 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6885 getCurFunctionDecl(), DSAStack->getConstructTraits()); 6886 6887 QualType CalleeFnType = CalleeFnDecl->getType(); 6888 6889 SmallVector<Expr *, 4> Exprs; 6890 SmallVector<VariantMatchInfo, 4> VMIs; 6891 while (CalleeFnDecl) { 6892 for (OMPDeclareVariantAttr *A : 6893 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6894 Expr *VariantRef = A->getVariantFuncRef(); 6895 6896 VariantMatchInfo VMI; 6897 OMPTraitInfo &TI = A->getTraitInfo(); 6898 TI.getAsVariantMatchInfo(Context, VMI); 6899 if (!isVariantApplicableInContext(VMI, OMPCtx, 6900 /* DeviceSetOnly */ false)) 6901 continue; 6902 6903 VMIs.push_back(VMI); 6904 Exprs.push_back(VariantRef); 6905 } 6906 6907 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6908 } 6909 6910 ExprResult NewCall; 6911 do { 6912 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6913 if (BestIdx < 0) 6914 return Call; 6915 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6916 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6917 6918 { 6919 // Try to build a (member) call expression for the current best applicable 6920 // variant expression. We allow this to fail in which case we continue 6921 // with the next best variant expression. The fail case is part of the 6922 // implementation defined behavior in the OpenMP standard when it talks 6923 // about what differences in the function prototypes: "Any differences 6924 // that the specific OpenMP context requires in the prototype of the 6925 // variant from the base function prototype are implementation defined." 6926 // This wording is there to allow the specialized variant to have a 6927 // different type than the base function. This is intended and OK but if 6928 // we cannot create a call the difference is not in the "implementation 6929 // defined range" we allow. 6930 Sema::TentativeAnalysisScope Trap(*this); 6931 6932 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6933 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6934 BestExpr = MemberExpr::CreateImplicit( 6935 Context, MemberCall->getImplicitObjectArgument(), 6936 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6937 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6938 } 6939 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6940 ExecConfig); 6941 if (NewCall.isUsable()) { 6942 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6943 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6944 QualType NewType = Context.mergeFunctionTypes( 6945 CalleeFnType, NewCalleeFnDecl->getType(), 6946 /* OfBlockPointer */ false, 6947 /* Unqualified */ false, /* AllowCXX */ true); 6948 if (!NewType.isNull()) 6949 break; 6950 // Don't use the call if the function type was not compatible. 6951 NewCall = nullptr; 6952 } 6953 } 6954 } 6955 6956 VMIs.erase(VMIs.begin() + BestIdx); 6957 Exprs.erase(Exprs.begin() + BestIdx); 6958 } while (!VMIs.empty()); 6959 6960 if (!NewCall.isUsable()) 6961 return Call; 6962 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6963 } 6964 6965 Optional<std::pair<FunctionDecl *, Expr *>> 6966 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6967 Expr *VariantRef, OMPTraitInfo &TI, 6968 unsigned NumAppendArgs, 6969 SourceRange SR) { 6970 if (!DG || DG.get().isNull()) 6971 return None; 6972 6973 const int VariantId = 1; 6974 // Must be applied only to single decl. 6975 if (!DG.get().isSingleDecl()) { 6976 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6977 << VariantId << SR; 6978 return None; 6979 } 6980 Decl *ADecl = DG.get().getSingleDecl(); 6981 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6982 ADecl = FTD->getTemplatedDecl(); 6983 6984 // Decl must be a function. 6985 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6986 if (!FD) { 6987 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6988 << VariantId << SR; 6989 return None; 6990 } 6991 6992 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6993 return FD->hasAttrs() && 6994 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6995 FD->hasAttr<TargetAttr>()); 6996 }; 6997 // OpenMP is not compatible with CPU-specific attributes. 6998 if (HasMultiVersionAttributes(FD)) { 6999 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 7000 << SR; 7001 return None; 7002 } 7003 7004 // Allow #pragma omp declare variant only if the function is not used. 7005 if (FD->isUsed(false)) 7006 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 7007 << FD->getLocation(); 7008 7009 // Check if the function was emitted already. 7010 const FunctionDecl *Definition; 7011 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 7012 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 7013 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 7014 << FD->getLocation(); 7015 7016 // The VariantRef must point to function. 7017 if (!VariantRef) { 7018 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 7019 return None; 7020 } 7021 7022 auto ShouldDelayChecks = [](Expr *&E, bool) { 7023 return E && (E->isTypeDependent() || E->isValueDependent() || 7024 E->containsUnexpandedParameterPack() || 7025 E->isInstantiationDependent()); 7026 }; 7027 // Do not check templates, wait until instantiation. 7028 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 7029 TI.anyScoreOrCondition(ShouldDelayChecks)) 7030 return std::make_pair(FD, VariantRef); 7031 7032 // Deal with non-constant score and user condition expressions. 7033 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 7034 bool IsScore) -> bool { 7035 if (!E || E->isIntegerConstantExpr(Context)) 7036 return false; 7037 7038 if (IsScore) { 7039 // We warn on non-constant scores and pretend they were not present. 7040 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 7041 << E; 7042 E = nullptr; 7043 } else { 7044 // We could replace a non-constant user condition with "false" but we 7045 // will soon need to handle these anyway for the dynamic version of 7046 // OpenMP context selectors. 7047 Diag(E->getExprLoc(), 7048 diag::err_omp_declare_variant_user_condition_not_constant) 7049 << E; 7050 } 7051 return true; 7052 }; 7053 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 7054 return None; 7055 7056 QualType AdjustedFnType = FD->getType(); 7057 if (NumAppendArgs) { 7058 if (isa<FunctionNoProtoType>(FD->getType())) { 7059 Diag(FD->getLocation(), diag::err_omp_declare_variant_prototype_required) 7060 << SR; 7061 return None; 7062 } 7063 // Adjust the function type to account for an extra omp_interop_t for each 7064 // specified in the append_args clause. 7065 const TypeDecl *TD = nullptr; 7066 LookupResult Result(*this, &Context.Idents.get("omp_interop_t"), 7067 SR.getBegin(), Sema::LookupOrdinaryName); 7068 if (LookupName(Result, getCurScope())) { 7069 NamedDecl *ND = Result.getFoundDecl(); 7070 TD = dyn_cast_or_null<TypeDecl>(ND); 7071 } 7072 if (!TD) { 7073 Diag(SR.getBegin(), diag::err_omp_interop_type_not_found) << SR; 7074 return None; 7075 } 7076 QualType InteropType = QualType(TD->getTypeForDecl(), 0); 7077 auto *PTy = cast<FunctionProtoType>(FD->getType()); 7078 if (PTy->isVariadic()) { 7079 Diag(FD->getLocation(), diag::err_omp_append_args_with_varargs) << SR; 7080 return None; 7081 } 7082 llvm::SmallVector<QualType, 8> Params; 7083 Params.append(PTy->param_type_begin(), PTy->param_type_end()); 7084 Params.insert(Params.end(), NumAppendArgs, InteropType); 7085 AdjustedFnType = Context.getFunctionType(PTy->getReturnType(), Params, 7086 PTy->getExtProtoInfo()); 7087 } 7088 7089 // Convert VariantRef expression to the type of the original function to 7090 // resolve possible conflicts. 7091 ExprResult VariantRefCast = VariantRef; 7092 if (LangOpts.CPlusPlus) { 7093 QualType FnPtrType; 7094 auto *Method = dyn_cast<CXXMethodDecl>(FD); 7095 if (Method && !Method->isStatic()) { 7096 const Type *ClassType = 7097 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 7098 FnPtrType = Context.getMemberPointerType(AdjustedFnType, ClassType); 7099 ExprResult ER; 7100 { 7101 // Build adrr_of unary op to correctly handle type checks for member 7102 // functions. 7103 Sema::TentativeAnalysisScope Trap(*this); 7104 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 7105 VariantRef); 7106 } 7107 if (!ER.isUsable()) { 7108 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7109 << VariantId << VariantRef->getSourceRange(); 7110 return None; 7111 } 7112 VariantRef = ER.get(); 7113 } else { 7114 FnPtrType = Context.getPointerType(AdjustedFnType); 7115 } 7116 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 7117 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 7118 ImplicitConversionSequence ICS = TryImplicitConversion( 7119 VariantRef, FnPtrType.getUnqualifiedType(), 7120 /*SuppressUserConversions=*/false, AllowedExplicit::None, 7121 /*InOverloadResolution=*/false, 7122 /*CStyle=*/false, 7123 /*AllowObjCWritebackConversion=*/false); 7124 if (ICS.isFailure()) { 7125 Diag(VariantRef->getExprLoc(), 7126 diag::err_omp_declare_variant_incompat_types) 7127 << VariantRef->getType() 7128 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 7129 << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange(); 7130 return None; 7131 } 7132 VariantRefCast = PerformImplicitConversion( 7133 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 7134 if (!VariantRefCast.isUsable()) 7135 return None; 7136 } 7137 // Drop previously built artificial addr_of unary op for member functions. 7138 if (Method && !Method->isStatic()) { 7139 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 7140 if (auto *UO = dyn_cast<UnaryOperator>( 7141 PossibleAddrOfVariantRef->IgnoreImplicit())) 7142 VariantRefCast = UO->getSubExpr(); 7143 } 7144 } 7145 7146 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 7147 if (!ER.isUsable() || 7148 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 7149 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7150 << VariantId << VariantRef->getSourceRange(); 7151 return None; 7152 } 7153 7154 // The VariantRef must point to function. 7155 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 7156 if (!DRE) { 7157 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7158 << VariantId << VariantRef->getSourceRange(); 7159 return None; 7160 } 7161 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 7162 if (!NewFD) { 7163 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7164 << VariantId << VariantRef->getSourceRange(); 7165 return None; 7166 } 7167 7168 // Check if function types are compatible in C. 7169 if (!LangOpts.CPlusPlus) { 7170 QualType NewType = 7171 Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType()); 7172 if (NewType.isNull()) { 7173 Diag(VariantRef->getExprLoc(), 7174 diag::err_omp_declare_variant_incompat_types) 7175 << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0) 7176 << VariantRef->getSourceRange(); 7177 return None; 7178 } 7179 if (NewType->isFunctionProtoType()) { 7180 if (FD->getType()->isFunctionNoProtoType()) 7181 setPrototype(*this, FD, NewFD, NewType); 7182 else if (NewFD->getType()->isFunctionNoProtoType()) 7183 setPrototype(*this, NewFD, FD, NewType); 7184 } 7185 } 7186 7187 // Check if variant function is not marked with declare variant directive. 7188 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 7189 Diag(VariantRef->getExprLoc(), 7190 diag::warn_omp_declare_variant_marked_as_declare_variant) 7191 << VariantRef->getSourceRange(); 7192 SourceRange SR = 7193 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 7194 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 7195 return None; 7196 } 7197 7198 enum DoesntSupport { 7199 VirtFuncs = 1, 7200 Constructors = 3, 7201 Destructors = 4, 7202 DeletedFuncs = 5, 7203 DefaultedFuncs = 6, 7204 ConstexprFuncs = 7, 7205 ConstevalFuncs = 8, 7206 }; 7207 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 7208 if (CXXFD->isVirtual()) { 7209 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7210 << VirtFuncs; 7211 return None; 7212 } 7213 7214 if (isa<CXXConstructorDecl>(FD)) { 7215 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7216 << Constructors; 7217 return None; 7218 } 7219 7220 if (isa<CXXDestructorDecl>(FD)) { 7221 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7222 << Destructors; 7223 return None; 7224 } 7225 } 7226 7227 if (FD->isDeleted()) { 7228 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7229 << DeletedFuncs; 7230 return None; 7231 } 7232 7233 if (FD->isDefaulted()) { 7234 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7235 << DefaultedFuncs; 7236 return None; 7237 } 7238 7239 if (FD->isConstexpr()) { 7240 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7241 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 7242 return None; 7243 } 7244 7245 // Check general compatibility. 7246 if (areMultiversionVariantFunctionsCompatible( 7247 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 7248 PartialDiagnosticAt(SourceLocation(), 7249 PartialDiagnostic::NullDiagnostic()), 7250 PartialDiagnosticAt( 7251 VariantRef->getExprLoc(), 7252 PDiag(diag::err_omp_declare_variant_doesnt_support)), 7253 PartialDiagnosticAt(VariantRef->getExprLoc(), 7254 PDiag(diag::err_omp_declare_variant_diff) 7255 << FD->getLocation()), 7256 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 7257 /*CLinkageMayDiffer=*/true)) 7258 return None; 7259 return std::make_pair(FD, cast<Expr>(DRE)); 7260 } 7261 7262 void Sema::ActOnOpenMPDeclareVariantDirective( 7263 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI, 7264 ArrayRef<Expr *> AdjustArgsNothing, 7265 ArrayRef<Expr *> AdjustArgsNeedDevicePtr, 7266 ArrayRef<OMPDeclareVariantAttr::InteropType> AppendArgs, 7267 SourceLocation AdjustArgsLoc, SourceLocation AppendArgsLoc, 7268 SourceRange SR) { 7269 7270 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7271 // An adjust_args clause or append_args clause can only be specified if the 7272 // dispatch selector of the construct selector set appears in the match 7273 // clause. 7274 7275 SmallVector<Expr *, 8> AllAdjustArgs; 7276 llvm::append_range(AllAdjustArgs, AdjustArgsNothing); 7277 llvm::append_range(AllAdjustArgs, AdjustArgsNeedDevicePtr); 7278 7279 if (!AllAdjustArgs.empty() || !AppendArgs.empty()) { 7280 VariantMatchInfo VMI; 7281 TI.getAsVariantMatchInfo(Context, VMI); 7282 if (!llvm::is_contained( 7283 VMI.ConstructTraits, 7284 llvm::omp::TraitProperty::construct_dispatch_dispatch)) { 7285 if (!AllAdjustArgs.empty()) 7286 Diag(AdjustArgsLoc, diag::err_omp_clause_requires_dispatch_construct) 7287 << getOpenMPClauseName(OMPC_adjust_args); 7288 if (!AppendArgs.empty()) 7289 Diag(AppendArgsLoc, diag::err_omp_clause_requires_dispatch_construct) 7290 << getOpenMPClauseName(OMPC_append_args); 7291 return; 7292 } 7293 } 7294 7295 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions] 7296 // Each argument can only appear in a single adjust_args clause for each 7297 // declare variant directive. 7298 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars; 7299 7300 for (Expr *E : AllAdjustArgs) { 7301 E = E->IgnoreParenImpCasts(); 7302 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 7303 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 7304 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 7305 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 7306 FD->getParamDecl(PVD->getFunctionScopeIndex()) 7307 ->getCanonicalDecl() == CanonPVD) { 7308 // It's a parameter of the function, check duplicates. 7309 if (!AdjustVars.insert(CanonPVD).second) { 7310 Diag(DRE->getLocation(), diag::err_omp_adjust_arg_multiple_clauses) 7311 << PVD; 7312 return; 7313 } 7314 continue; 7315 } 7316 } 7317 } 7318 // Anything that is not a function parameter is an error. 7319 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) << FD << 0; 7320 return; 7321 } 7322 7323 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit( 7324 Context, VariantRef, &TI, const_cast<Expr **>(AdjustArgsNothing.data()), 7325 AdjustArgsNothing.size(), 7326 const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()), 7327 AdjustArgsNeedDevicePtr.size(), 7328 const_cast<OMPDeclareVariantAttr::InteropType *>(AppendArgs.data()), 7329 AppendArgs.size(), SR); 7330 FD->addAttr(NewAttr); 7331 } 7332 7333 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 7334 Stmt *AStmt, 7335 SourceLocation StartLoc, 7336 SourceLocation EndLoc) { 7337 if (!AStmt) 7338 return StmtError(); 7339 7340 auto *CS = cast<CapturedStmt>(AStmt); 7341 // 1.2.2 OpenMP Language Terminology 7342 // Structured block - An executable statement with a single entry at the 7343 // top and a single exit at the bottom. 7344 // The point of exit cannot be a branch out of the structured block. 7345 // longjmp() and throw() must not violate the entry/exit criteria. 7346 CS->getCapturedDecl()->setNothrow(); 7347 7348 setFunctionHasBranchProtectedScope(); 7349 7350 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7351 DSAStack->getTaskgroupReductionRef(), 7352 DSAStack->isCancelRegion()); 7353 } 7354 7355 namespace { 7356 /// Iteration space of a single for loop. 7357 struct LoopIterationSpace final { 7358 /// True if the condition operator is the strict compare operator (<, > or 7359 /// !=). 7360 bool IsStrictCompare = false; 7361 /// Condition of the loop. 7362 Expr *PreCond = nullptr; 7363 /// This expression calculates the number of iterations in the loop. 7364 /// It is always possible to calculate it before starting the loop. 7365 Expr *NumIterations = nullptr; 7366 /// The loop counter variable. 7367 Expr *CounterVar = nullptr; 7368 /// Private loop counter variable. 7369 Expr *PrivateCounterVar = nullptr; 7370 /// This is initializer for the initial value of #CounterVar. 7371 Expr *CounterInit = nullptr; 7372 /// This is step for the #CounterVar used to generate its update: 7373 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 7374 Expr *CounterStep = nullptr; 7375 /// Should step be subtracted? 7376 bool Subtract = false; 7377 /// Source range of the loop init. 7378 SourceRange InitSrcRange; 7379 /// Source range of the loop condition. 7380 SourceRange CondSrcRange; 7381 /// Source range of the loop increment. 7382 SourceRange IncSrcRange; 7383 /// Minimum value that can have the loop control variable. Used to support 7384 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 7385 /// since only such variables can be used in non-loop invariant expressions. 7386 Expr *MinValue = nullptr; 7387 /// Maximum value that can have the loop control variable. Used to support 7388 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 7389 /// since only such variables can be used in non-loop invariant expressions. 7390 Expr *MaxValue = nullptr; 7391 /// true, if the lower bound depends on the outer loop control var. 7392 bool IsNonRectangularLB = false; 7393 /// true, if the upper bound depends on the outer loop control var. 7394 bool IsNonRectangularUB = false; 7395 /// Index of the loop this loop depends on and forms non-rectangular loop 7396 /// nest. 7397 unsigned LoopDependentIdx = 0; 7398 /// Final condition for the non-rectangular loop nest support. It is used to 7399 /// check that the number of iterations for this particular counter must be 7400 /// finished. 7401 Expr *FinalCondition = nullptr; 7402 }; 7403 7404 /// Helper class for checking canonical form of the OpenMP loops and 7405 /// extracting iteration space of each loop in the loop nest, that will be used 7406 /// for IR generation. 7407 class OpenMPIterationSpaceChecker { 7408 /// Reference to Sema. 7409 Sema &SemaRef; 7410 /// Does the loop associated directive support non-rectangular loops? 7411 bool SupportsNonRectangular; 7412 /// Data-sharing stack. 7413 DSAStackTy &Stack; 7414 /// A location for diagnostics (when there is no some better location). 7415 SourceLocation DefaultLoc; 7416 /// A location for diagnostics (when increment is not compatible). 7417 SourceLocation ConditionLoc; 7418 /// A source location for referring to loop init later. 7419 SourceRange InitSrcRange; 7420 /// A source location for referring to condition later. 7421 SourceRange ConditionSrcRange; 7422 /// A source location for referring to increment later. 7423 SourceRange IncrementSrcRange; 7424 /// Loop variable. 7425 ValueDecl *LCDecl = nullptr; 7426 /// Reference to loop variable. 7427 Expr *LCRef = nullptr; 7428 /// Lower bound (initializer for the var). 7429 Expr *LB = nullptr; 7430 /// Upper bound. 7431 Expr *UB = nullptr; 7432 /// Loop step (increment). 7433 Expr *Step = nullptr; 7434 /// This flag is true when condition is one of: 7435 /// Var < UB 7436 /// Var <= UB 7437 /// UB > Var 7438 /// UB >= Var 7439 /// This will have no value when the condition is != 7440 llvm::Optional<bool> TestIsLessOp; 7441 /// This flag is true when condition is strict ( < or > ). 7442 bool TestIsStrictOp = false; 7443 /// This flag is true when step is subtracted on each iteration. 7444 bool SubtractStep = false; 7445 /// The outer loop counter this loop depends on (if any). 7446 const ValueDecl *DepDecl = nullptr; 7447 /// Contains number of loop (starts from 1) on which loop counter init 7448 /// expression of this loop depends on. 7449 Optional<unsigned> InitDependOnLC; 7450 /// Contains number of loop (starts from 1) on which loop counter condition 7451 /// expression of this loop depends on. 7452 Optional<unsigned> CondDependOnLC; 7453 /// Checks if the provide statement depends on the loop counter. 7454 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 7455 /// Original condition required for checking of the exit condition for 7456 /// non-rectangular loop. 7457 Expr *Condition = nullptr; 7458 7459 public: 7460 OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular, 7461 DSAStackTy &Stack, SourceLocation DefaultLoc) 7462 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular), 7463 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 7464 /// Check init-expr for canonical loop form and save loop counter 7465 /// variable - #Var and its initialization value - #LB. 7466 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 7467 /// Check test-expr for canonical form, save upper-bound (#UB), flags 7468 /// for less/greater and for strict/non-strict comparison. 7469 bool checkAndSetCond(Expr *S); 7470 /// Check incr-expr for canonical loop form and return true if it 7471 /// does not conform, otherwise save loop step (#Step). 7472 bool checkAndSetInc(Expr *S); 7473 /// Return the loop counter variable. 7474 ValueDecl *getLoopDecl() const { return LCDecl; } 7475 /// Return the reference expression to loop counter variable. 7476 Expr *getLoopDeclRefExpr() const { return LCRef; } 7477 /// Source range of the loop init. 7478 SourceRange getInitSrcRange() const { return InitSrcRange; } 7479 /// Source range of the loop condition. 7480 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 7481 /// Source range of the loop increment. 7482 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 7483 /// True if the step should be subtracted. 7484 bool shouldSubtractStep() const { return SubtractStep; } 7485 /// True, if the compare operator is strict (<, > or !=). 7486 bool isStrictTestOp() const { return TestIsStrictOp; } 7487 /// Build the expression to calculate the number of iterations. 7488 Expr *buildNumIterations( 7489 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7490 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7491 /// Build the precondition expression for the loops. 7492 Expr * 7493 buildPreCond(Scope *S, Expr *Cond, 7494 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7495 /// Build reference expression to the counter be used for codegen. 7496 DeclRefExpr * 7497 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7498 DSAStackTy &DSA) const; 7499 /// Build reference expression to the private counter be used for 7500 /// codegen. 7501 Expr *buildPrivateCounterVar() const; 7502 /// Build initialization of the counter be used for codegen. 7503 Expr *buildCounterInit() const; 7504 /// Build step of the counter be used for codegen. 7505 Expr *buildCounterStep() const; 7506 /// Build loop data with counter value for depend clauses in ordered 7507 /// directives. 7508 Expr * 7509 buildOrderedLoopData(Scope *S, Expr *Counter, 7510 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7511 SourceLocation Loc, Expr *Inc = nullptr, 7512 OverloadedOperatorKind OOK = OO_Amp); 7513 /// Builds the minimum value for the loop counter. 7514 std::pair<Expr *, Expr *> buildMinMaxValues( 7515 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7516 /// Builds final condition for the non-rectangular loops. 7517 Expr *buildFinalCondition(Scope *S) const; 7518 /// Return true if any expression is dependent. 7519 bool dependent() const; 7520 /// Returns true if the initializer forms non-rectangular loop. 7521 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 7522 /// Returns true if the condition forms non-rectangular loop. 7523 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 7524 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 7525 unsigned getLoopDependentIdx() const { 7526 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 7527 } 7528 7529 private: 7530 /// Check the right-hand side of an assignment in the increment 7531 /// expression. 7532 bool checkAndSetIncRHS(Expr *RHS); 7533 /// Helper to set loop counter variable and its initializer. 7534 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 7535 bool EmitDiags); 7536 /// Helper to set upper bound. 7537 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 7538 SourceRange SR, SourceLocation SL); 7539 /// Helper to set loop increment. 7540 bool setStep(Expr *NewStep, bool Subtract); 7541 }; 7542 7543 bool OpenMPIterationSpaceChecker::dependent() const { 7544 if (!LCDecl) { 7545 assert(!LB && !UB && !Step); 7546 return false; 7547 } 7548 return LCDecl->getType()->isDependentType() || 7549 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 7550 (Step && Step->isValueDependent()); 7551 } 7552 7553 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 7554 Expr *NewLCRefExpr, 7555 Expr *NewLB, bool EmitDiags) { 7556 // State consistency checking to ensure correct usage. 7557 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 7558 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7559 if (!NewLCDecl || !NewLB || NewLB->containsErrors()) 7560 return true; 7561 LCDecl = getCanonicalDecl(NewLCDecl); 7562 LCRef = NewLCRefExpr; 7563 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 7564 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7565 if ((Ctor->isCopyOrMoveConstructor() || 7566 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7567 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7568 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 7569 LB = NewLB; 7570 if (EmitDiags) 7571 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 7572 return false; 7573 } 7574 7575 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 7576 llvm::Optional<bool> LessOp, 7577 bool StrictOp, SourceRange SR, 7578 SourceLocation SL) { 7579 // State consistency checking to ensure correct usage. 7580 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 7581 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7582 if (!NewUB || NewUB->containsErrors()) 7583 return true; 7584 UB = NewUB; 7585 if (LessOp) 7586 TestIsLessOp = LessOp; 7587 TestIsStrictOp = StrictOp; 7588 ConditionSrcRange = SR; 7589 ConditionLoc = SL; 7590 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 7591 return false; 7592 } 7593 7594 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 7595 // State consistency checking to ensure correct usage. 7596 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 7597 if (!NewStep || NewStep->containsErrors()) 7598 return true; 7599 if (!NewStep->isValueDependent()) { 7600 // Check that the step is integer expression. 7601 SourceLocation StepLoc = NewStep->getBeginLoc(); 7602 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 7603 StepLoc, getExprAsWritten(NewStep)); 7604 if (Val.isInvalid()) 7605 return true; 7606 NewStep = Val.get(); 7607 7608 // OpenMP [2.6, Canonical Loop Form, Restrictions] 7609 // If test-expr is of form var relational-op b and relational-op is < or 7610 // <= then incr-expr must cause var to increase on each iteration of the 7611 // loop. If test-expr is of form var relational-op b and relational-op is 7612 // > or >= then incr-expr must cause var to decrease on each iteration of 7613 // the loop. 7614 // If test-expr is of form b relational-op var and relational-op is < or 7615 // <= then incr-expr must cause var to decrease on each iteration of the 7616 // loop. If test-expr is of form b relational-op var and relational-op is 7617 // > or >= then incr-expr must cause var to increase on each iteration of 7618 // the loop. 7619 Optional<llvm::APSInt> Result = 7620 NewStep->getIntegerConstantExpr(SemaRef.Context); 7621 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 7622 bool IsConstNeg = 7623 Result && Result->isSigned() && (Subtract != Result->isNegative()); 7624 bool IsConstPos = 7625 Result && Result->isSigned() && (Subtract == Result->isNegative()); 7626 bool IsConstZero = Result && !Result->getBoolValue(); 7627 7628 // != with increment is treated as <; != with decrement is treated as > 7629 if (!TestIsLessOp.hasValue()) 7630 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 7631 if (UB && (IsConstZero || 7632 (TestIsLessOp.getValue() ? 7633 (IsConstNeg || (IsUnsigned && Subtract)) : 7634 (IsConstPos || (IsUnsigned && !Subtract))))) { 7635 SemaRef.Diag(NewStep->getExprLoc(), 7636 diag::err_omp_loop_incr_not_compatible) 7637 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 7638 SemaRef.Diag(ConditionLoc, 7639 diag::note_omp_loop_cond_requres_compatible_incr) 7640 << TestIsLessOp.getValue() << ConditionSrcRange; 7641 return true; 7642 } 7643 if (TestIsLessOp.getValue() == Subtract) { 7644 NewStep = 7645 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 7646 .get(); 7647 Subtract = !Subtract; 7648 } 7649 } 7650 7651 Step = NewStep; 7652 SubtractStep = Subtract; 7653 return false; 7654 } 7655 7656 namespace { 7657 /// Checker for the non-rectangular loops. Checks if the initializer or 7658 /// condition expression references loop counter variable. 7659 class LoopCounterRefChecker final 7660 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 7661 Sema &SemaRef; 7662 DSAStackTy &Stack; 7663 const ValueDecl *CurLCDecl = nullptr; 7664 const ValueDecl *DepDecl = nullptr; 7665 const ValueDecl *PrevDepDecl = nullptr; 7666 bool IsInitializer = true; 7667 bool SupportsNonRectangular; 7668 unsigned BaseLoopId = 0; 7669 bool checkDecl(const Expr *E, const ValueDecl *VD) { 7670 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 7671 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 7672 << (IsInitializer ? 0 : 1); 7673 return false; 7674 } 7675 const auto &&Data = Stack.isLoopControlVariable(VD); 7676 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 7677 // The type of the loop iterator on which we depend may not have a random 7678 // access iterator type. 7679 if (Data.first && VD->getType()->isRecordType()) { 7680 SmallString<128> Name; 7681 llvm::raw_svector_ostream OS(Name); 7682 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7683 /*Qualified=*/true); 7684 SemaRef.Diag(E->getExprLoc(), 7685 diag::err_omp_wrong_dependency_iterator_type) 7686 << OS.str(); 7687 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 7688 return false; 7689 } 7690 if (Data.first && !SupportsNonRectangular) { 7691 SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency); 7692 return false; 7693 } 7694 if (Data.first && 7695 (DepDecl || (PrevDepDecl && 7696 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 7697 if (!DepDecl && PrevDepDecl) 7698 DepDecl = PrevDepDecl; 7699 SmallString<128> Name; 7700 llvm::raw_svector_ostream OS(Name); 7701 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7702 /*Qualified=*/true); 7703 SemaRef.Diag(E->getExprLoc(), 7704 diag::err_omp_invariant_or_linear_dependency) 7705 << OS.str(); 7706 return false; 7707 } 7708 if (Data.first) { 7709 DepDecl = VD; 7710 BaseLoopId = Data.first; 7711 } 7712 return Data.first; 7713 } 7714 7715 public: 7716 bool VisitDeclRefExpr(const DeclRefExpr *E) { 7717 const ValueDecl *VD = E->getDecl(); 7718 if (isa<VarDecl>(VD)) 7719 return checkDecl(E, VD); 7720 return false; 7721 } 7722 bool VisitMemberExpr(const MemberExpr *E) { 7723 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 7724 const ValueDecl *VD = E->getMemberDecl(); 7725 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 7726 return checkDecl(E, VD); 7727 } 7728 return false; 7729 } 7730 bool VisitStmt(const Stmt *S) { 7731 bool Res = false; 7732 for (const Stmt *Child : S->children()) 7733 Res = (Child && Visit(Child)) || Res; 7734 return Res; 7735 } 7736 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 7737 const ValueDecl *CurLCDecl, bool IsInitializer, 7738 const ValueDecl *PrevDepDecl = nullptr, 7739 bool SupportsNonRectangular = true) 7740 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 7741 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer), 7742 SupportsNonRectangular(SupportsNonRectangular) {} 7743 unsigned getBaseLoopId() const { 7744 assert(CurLCDecl && "Expected loop dependency."); 7745 return BaseLoopId; 7746 } 7747 const ValueDecl *getDepDecl() const { 7748 assert(CurLCDecl && "Expected loop dependency."); 7749 return DepDecl; 7750 } 7751 }; 7752 } // namespace 7753 7754 Optional<unsigned> 7755 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 7756 bool IsInitializer) { 7757 // Check for the non-rectangular loops. 7758 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 7759 DepDecl, SupportsNonRectangular); 7760 if (LoopStmtChecker.Visit(S)) { 7761 DepDecl = LoopStmtChecker.getDepDecl(); 7762 return LoopStmtChecker.getBaseLoopId(); 7763 } 7764 return llvm::None; 7765 } 7766 7767 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 7768 // Check init-expr for canonical loop form and save loop counter 7769 // variable - #Var and its initialization value - #LB. 7770 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 7771 // var = lb 7772 // integer-type var = lb 7773 // random-access-iterator-type var = lb 7774 // pointer-type var = lb 7775 // 7776 if (!S) { 7777 if (EmitDiags) { 7778 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 7779 } 7780 return true; 7781 } 7782 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7783 if (!ExprTemp->cleanupsHaveSideEffects()) 7784 S = ExprTemp->getSubExpr(); 7785 7786 InitSrcRange = S->getSourceRange(); 7787 if (Expr *E = dyn_cast<Expr>(S)) 7788 S = E->IgnoreParens(); 7789 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7790 if (BO->getOpcode() == BO_Assign) { 7791 Expr *LHS = BO->getLHS()->IgnoreParens(); 7792 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7793 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7794 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7795 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7796 EmitDiags); 7797 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 7798 } 7799 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7800 if (ME->isArrow() && 7801 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7802 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7803 EmitDiags); 7804 } 7805 } 7806 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 7807 if (DS->isSingleDecl()) { 7808 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 7809 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 7810 // Accept non-canonical init form here but emit ext. warning. 7811 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 7812 SemaRef.Diag(S->getBeginLoc(), 7813 diag::ext_omp_loop_not_canonical_init) 7814 << S->getSourceRange(); 7815 return setLCDeclAndLB( 7816 Var, 7817 buildDeclRefExpr(SemaRef, Var, 7818 Var->getType().getNonReferenceType(), 7819 DS->getBeginLoc()), 7820 Var->getInit(), EmitDiags); 7821 } 7822 } 7823 } 7824 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7825 if (CE->getOperator() == OO_Equal) { 7826 Expr *LHS = CE->getArg(0); 7827 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7828 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7829 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7830 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7831 EmitDiags); 7832 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 7833 } 7834 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7835 if (ME->isArrow() && 7836 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7837 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7838 EmitDiags); 7839 } 7840 } 7841 } 7842 7843 if (dependent() || SemaRef.CurContext->isDependentContext()) 7844 return false; 7845 if (EmitDiags) { 7846 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 7847 << S->getSourceRange(); 7848 } 7849 return true; 7850 } 7851 7852 /// Ignore parenthesizes, implicit casts, copy constructor and return the 7853 /// variable (which may be the loop variable) if possible. 7854 static const ValueDecl *getInitLCDecl(const Expr *E) { 7855 if (!E) 7856 return nullptr; 7857 E = getExprAsWritten(E); 7858 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 7859 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7860 if ((Ctor->isCopyOrMoveConstructor() || 7861 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7862 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7863 E = CE->getArg(0)->IgnoreParenImpCasts(); 7864 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 7865 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 7866 return getCanonicalDecl(VD); 7867 } 7868 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 7869 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7870 return getCanonicalDecl(ME->getMemberDecl()); 7871 return nullptr; 7872 } 7873 7874 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 7875 // Check test-expr for canonical form, save upper-bound UB, flags for 7876 // less/greater and for strict/non-strict comparison. 7877 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 7878 // var relational-op b 7879 // b relational-op var 7880 // 7881 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 7882 if (!S) { 7883 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 7884 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 7885 return true; 7886 } 7887 Condition = S; 7888 S = getExprAsWritten(S); 7889 SourceLocation CondLoc = S->getBeginLoc(); 7890 auto &&CheckAndSetCond = [this, IneqCondIsCanonical]( 7891 BinaryOperatorKind Opcode, const Expr *LHS, 7892 const Expr *RHS, SourceRange SR, 7893 SourceLocation OpLoc) -> llvm::Optional<bool> { 7894 if (BinaryOperator::isRelationalOp(Opcode)) { 7895 if (getInitLCDecl(LHS) == LCDecl) 7896 return setUB(const_cast<Expr *>(RHS), 7897 (Opcode == BO_LT || Opcode == BO_LE), 7898 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7899 if (getInitLCDecl(RHS) == LCDecl) 7900 return setUB(const_cast<Expr *>(LHS), 7901 (Opcode == BO_GT || Opcode == BO_GE), 7902 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7903 } else if (IneqCondIsCanonical && Opcode == BO_NE) { 7904 return setUB(const_cast<Expr *>(getInitLCDecl(LHS) == LCDecl ? RHS : LHS), 7905 /*LessOp=*/llvm::None, 7906 /*StrictOp=*/true, SR, OpLoc); 7907 } 7908 return llvm::None; 7909 }; 7910 llvm::Optional<bool> Res; 7911 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) { 7912 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm(); 7913 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(), 7914 RBO->getOperatorLoc()); 7915 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7916 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(), 7917 BO->getSourceRange(), BO->getOperatorLoc()); 7918 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7919 if (CE->getNumArgs() == 2) { 7920 Res = CheckAndSetCond( 7921 BinaryOperator::getOverloadedOpcode(CE->getOperator()), CE->getArg(0), 7922 CE->getArg(1), CE->getSourceRange(), CE->getOperatorLoc()); 7923 } 7924 } 7925 if (Res.hasValue()) 7926 return *Res; 7927 if (dependent() || SemaRef.CurContext->isDependentContext()) 7928 return false; 7929 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 7930 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 7931 return true; 7932 } 7933 7934 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 7935 // RHS of canonical loop form increment can be: 7936 // var + incr 7937 // incr + var 7938 // var - incr 7939 // 7940 RHS = RHS->IgnoreParenImpCasts(); 7941 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 7942 if (BO->isAdditiveOp()) { 7943 bool IsAdd = BO->getOpcode() == BO_Add; 7944 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7945 return setStep(BO->getRHS(), !IsAdd); 7946 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 7947 return setStep(BO->getLHS(), /*Subtract=*/false); 7948 } 7949 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 7950 bool IsAdd = CE->getOperator() == OO_Plus; 7951 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 7952 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7953 return setStep(CE->getArg(1), !IsAdd); 7954 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 7955 return setStep(CE->getArg(0), /*Subtract=*/false); 7956 } 7957 } 7958 if (dependent() || SemaRef.CurContext->isDependentContext()) 7959 return false; 7960 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7961 << RHS->getSourceRange() << LCDecl; 7962 return true; 7963 } 7964 7965 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 7966 // Check incr-expr for canonical loop form and return true if it 7967 // does not conform. 7968 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 7969 // ++var 7970 // var++ 7971 // --var 7972 // var-- 7973 // var += incr 7974 // var -= incr 7975 // var = var + incr 7976 // var = incr + var 7977 // var = var - incr 7978 // 7979 if (!S) { 7980 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7981 return true; 7982 } 7983 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7984 if (!ExprTemp->cleanupsHaveSideEffects()) 7985 S = ExprTemp->getSubExpr(); 7986 7987 IncrementSrcRange = S->getSourceRange(); 7988 S = S->IgnoreParens(); 7989 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7990 if (UO->isIncrementDecrementOp() && 7991 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7992 return setStep(SemaRef 7993 .ActOnIntegerConstant(UO->getBeginLoc(), 7994 (UO->isDecrementOp() ? -1 : 1)) 7995 .get(), 7996 /*Subtract=*/false); 7997 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7998 switch (BO->getOpcode()) { 7999 case BO_AddAssign: 8000 case BO_SubAssign: 8001 if (getInitLCDecl(BO->getLHS()) == LCDecl) 8002 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 8003 break; 8004 case BO_Assign: 8005 if (getInitLCDecl(BO->getLHS()) == LCDecl) 8006 return checkAndSetIncRHS(BO->getRHS()); 8007 break; 8008 default: 8009 break; 8010 } 8011 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 8012 switch (CE->getOperator()) { 8013 case OO_PlusPlus: 8014 case OO_MinusMinus: 8015 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 8016 return setStep(SemaRef 8017 .ActOnIntegerConstant( 8018 CE->getBeginLoc(), 8019 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 8020 .get(), 8021 /*Subtract=*/false); 8022 break; 8023 case OO_PlusEqual: 8024 case OO_MinusEqual: 8025 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 8026 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 8027 break; 8028 case OO_Equal: 8029 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 8030 return checkAndSetIncRHS(CE->getArg(1)); 8031 break; 8032 default: 8033 break; 8034 } 8035 } 8036 if (dependent() || SemaRef.CurContext->isDependentContext()) 8037 return false; 8038 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 8039 << S->getSourceRange() << LCDecl; 8040 return true; 8041 } 8042 8043 static ExprResult 8044 tryBuildCapture(Sema &SemaRef, Expr *Capture, 8045 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8046 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 8047 return Capture; 8048 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 8049 return SemaRef.PerformImplicitConversion( 8050 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 8051 /*AllowExplicit=*/true); 8052 auto I = Captures.find(Capture); 8053 if (I != Captures.end()) 8054 return buildCapture(SemaRef, Capture, I->second); 8055 DeclRefExpr *Ref = nullptr; 8056 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 8057 Captures[Capture] = Ref; 8058 return Res; 8059 } 8060 8061 /// Calculate number of iterations, transforming to unsigned, if number of 8062 /// iterations may be larger than the original type. 8063 static Expr * 8064 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 8065 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 8066 bool TestIsStrictOp, bool RoundToStep, 8067 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8068 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8069 if (!NewStep.isUsable()) 8070 return nullptr; 8071 llvm::APSInt LRes, SRes; 8072 bool IsLowerConst = false, IsStepConst = false; 8073 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 8074 LRes = *Res; 8075 IsLowerConst = true; 8076 } 8077 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 8078 SRes = *Res; 8079 IsStepConst = true; 8080 } 8081 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 8082 ((!TestIsStrictOp && LRes.isNonNegative()) || 8083 (TestIsStrictOp && LRes.isStrictlyPositive())); 8084 bool NeedToReorganize = false; 8085 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 8086 if (!NoNeedToConvert && IsLowerConst && 8087 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 8088 NoNeedToConvert = true; 8089 if (RoundToStep) { 8090 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 8091 ? LRes.getBitWidth() 8092 : SRes.getBitWidth(); 8093 LRes = LRes.extend(BW + 1); 8094 LRes.setIsSigned(true); 8095 SRes = SRes.extend(BW + 1); 8096 SRes.setIsSigned(true); 8097 LRes -= SRes; 8098 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 8099 LRes = LRes.trunc(BW); 8100 } 8101 if (TestIsStrictOp) { 8102 unsigned BW = LRes.getBitWidth(); 8103 LRes = LRes.extend(BW + 1); 8104 LRes.setIsSigned(true); 8105 ++LRes; 8106 NoNeedToConvert = 8107 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 8108 // truncate to the original bitwidth. 8109 LRes = LRes.trunc(BW); 8110 } 8111 NeedToReorganize = NoNeedToConvert; 8112 } 8113 llvm::APSInt URes; 8114 bool IsUpperConst = false; 8115 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 8116 URes = *Res; 8117 IsUpperConst = true; 8118 } 8119 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 8120 (!RoundToStep || IsStepConst)) { 8121 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 8122 : URes.getBitWidth(); 8123 LRes = LRes.extend(BW + 1); 8124 LRes.setIsSigned(true); 8125 URes = URes.extend(BW + 1); 8126 URes.setIsSigned(true); 8127 URes -= LRes; 8128 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 8129 NeedToReorganize = NoNeedToConvert; 8130 } 8131 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 8132 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 8133 // unsigned. 8134 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 8135 !LCTy->isDependentType() && LCTy->isIntegerType()) { 8136 QualType LowerTy = Lower->getType(); 8137 QualType UpperTy = Upper->getType(); 8138 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 8139 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 8140 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 8141 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 8142 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 8143 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 8144 Upper = 8145 SemaRef 8146 .PerformImplicitConversion( 8147 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8148 CastType, Sema::AA_Converting) 8149 .get(); 8150 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 8151 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 8152 } 8153 } 8154 if (!Lower || !Upper || NewStep.isInvalid()) 8155 return nullptr; 8156 8157 ExprResult Diff; 8158 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 8159 // 1]). 8160 if (NeedToReorganize) { 8161 Diff = Lower; 8162 8163 if (RoundToStep) { 8164 // Lower - Step 8165 Diff = 8166 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 8167 if (!Diff.isUsable()) 8168 return nullptr; 8169 } 8170 8171 // Lower - Step [+ 1] 8172 if (TestIsStrictOp) 8173 Diff = SemaRef.BuildBinOp( 8174 S, DefaultLoc, BO_Add, Diff.get(), 8175 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8176 if (!Diff.isUsable()) 8177 return nullptr; 8178 8179 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8180 if (!Diff.isUsable()) 8181 return nullptr; 8182 8183 // Upper - (Lower - Step [+ 1]). 8184 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 8185 if (!Diff.isUsable()) 8186 return nullptr; 8187 } else { 8188 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 8189 8190 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 8191 // BuildBinOp already emitted error, this one is to point user to upper 8192 // and lower bound, and to tell what is passed to 'operator-'. 8193 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 8194 << Upper->getSourceRange() << Lower->getSourceRange(); 8195 return nullptr; 8196 } 8197 8198 if (!Diff.isUsable()) 8199 return nullptr; 8200 8201 // Upper - Lower [- 1] 8202 if (TestIsStrictOp) 8203 Diff = SemaRef.BuildBinOp( 8204 S, DefaultLoc, BO_Sub, Diff.get(), 8205 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8206 if (!Diff.isUsable()) 8207 return nullptr; 8208 8209 if (RoundToStep) { 8210 // Upper - Lower [- 1] + Step 8211 Diff = 8212 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 8213 if (!Diff.isUsable()) 8214 return nullptr; 8215 } 8216 } 8217 8218 // Parentheses (for dumping/debugging purposes only). 8219 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8220 if (!Diff.isUsable()) 8221 return nullptr; 8222 8223 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 8224 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 8225 if (!Diff.isUsable()) 8226 return nullptr; 8227 8228 return Diff.get(); 8229 } 8230 8231 /// Build the expression to calculate the number of iterations. 8232 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 8233 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 8234 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8235 QualType VarType = LCDecl->getType().getNonReferenceType(); 8236 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8237 !SemaRef.getLangOpts().CPlusPlus) 8238 return nullptr; 8239 Expr *LBVal = LB; 8240 Expr *UBVal = UB; 8241 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 8242 // max(LB(MinVal), LB(MaxVal)) 8243 if (InitDependOnLC) { 8244 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1]; 8245 if (!IS.MinValue || !IS.MaxValue) 8246 return nullptr; 8247 // OuterVar = Min 8248 ExprResult MinValue = 8249 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8250 if (!MinValue.isUsable()) 8251 return nullptr; 8252 8253 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8254 IS.CounterVar, MinValue.get()); 8255 if (!LBMinVal.isUsable()) 8256 return nullptr; 8257 // OuterVar = Min, LBVal 8258 LBMinVal = 8259 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 8260 if (!LBMinVal.isUsable()) 8261 return nullptr; 8262 // (OuterVar = Min, LBVal) 8263 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 8264 if (!LBMinVal.isUsable()) 8265 return nullptr; 8266 8267 // OuterVar = Max 8268 ExprResult MaxValue = 8269 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8270 if (!MaxValue.isUsable()) 8271 return nullptr; 8272 8273 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8274 IS.CounterVar, MaxValue.get()); 8275 if (!LBMaxVal.isUsable()) 8276 return nullptr; 8277 // OuterVar = Max, LBVal 8278 LBMaxVal = 8279 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 8280 if (!LBMaxVal.isUsable()) 8281 return nullptr; 8282 // (OuterVar = Max, LBVal) 8283 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 8284 if (!LBMaxVal.isUsable()) 8285 return nullptr; 8286 8287 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 8288 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 8289 if (!LBMin || !LBMax) 8290 return nullptr; 8291 // LB(MinVal) < LB(MaxVal) 8292 ExprResult MinLessMaxRes = 8293 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 8294 if (!MinLessMaxRes.isUsable()) 8295 return nullptr; 8296 Expr *MinLessMax = 8297 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 8298 if (!MinLessMax) 8299 return nullptr; 8300 if (TestIsLessOp.getValue()) { 8301 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 8302 // LB(MaxVal)) 8303 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8304 MinLessMax, LBMin, LBMax); 8305 if (!MinLB.isUsable()) 8306 return nullptr; 8307 LBVal = MinLB.get(); 8308 } else { 8309 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 8310 // LB(MaxVal)) 8311 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8312 MinLessMax, LBMax, LBMin); 8313 if (!MaxLB.isUsable()) 8314 return nullptr; 8315 LBVal = MaxLB.get(); 8316 } 8317 } 8318 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 8319 // min(UB(MinVal), UB(MaxVal)) 8320 if (CondDependOnLC) { 8321 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1]; 8322 if (!IS.MinValue || !IS.MaxValue) 8323 return nullptr; 8324 // OuterVar = Min 8325 ExprResult MinValue = 8326 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8327 if (!MinValue.isUsable()) 8328 return nullptr; 8329 8330 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8331 IS.CounterVar, MinValue.get()); 8332 if (!UBMinVal.isUsable()) 8333 return nullptr; 8334 // OuterVar = Min, UBVal 8335 UBMinVal = 8336 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 8337 if (!UBMinVal.isUsable()) 8338 return nullptr; 8339 // (OuterVar = Min, UBVal) 8340 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 8341 if (!UBMinVal.isUsable()) 8342 return nullptr; 8343 8344 // OuterVar = Max 8345 ExprResult MaxValue = 8346 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8347 if (!MaxValue.isUsable()) 8348 return nullptr; 8349 8350 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8351 IS.CounterVar, MaxValue.get()); 8352 if (!UBMaxVal.isUsable()) 8353 return nullptr; 8354 // OuterVar = Max, UBVal 8355 UBMaxVal = 8356 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 8357 if (!UBMaxVal.isUsable()) 8358 return nullptr; 8359 // (OuterVar = Max, UBVal) 8360 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 8361 if (!UBMaxVal.isUsable()) 8362 return nullptr; 8363 8364 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 8365 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 8366 if (!UBMin || !UBMax) 8367 return nullptr; 8368 // UB(MinVal) > UB(MaxVal) 8369 ExprResult MinGreaterMaxRes = 8370 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 8371 if (!MinGreaterMaxRes.isUsable()) 8372 return nullptr; 8373 Expr *MinGreaterMax = 8374 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 8375 if (!MinGreaterMax) 8376 return nullptr; 8377 if (TestIsLessOp.getValue()) { 8378 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 8379 // UB(MaxVal)) 8380 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 8381 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 8382 if (!MaxUB.isUsable()) 8383 return nullptr; 8384 UBVal = MaxUB.get(); 8385 } else { 8386 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 8387 // UB(MaxVal)) 8388 ExprResult MinUB = SemaRef.ActOnConditionalOp( 8389 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 8390 if (!MinUB.isUsable()) 8391 return nullptr; 8392 UBVal = MinUB.get(); 8393 } 8394 } 8395 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 8396 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 8397 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8398 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8399 if (!Upper || !Lower) 8400 return nullptr; 8401 8402 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8403 Step, VarType, TestIsStrictOp, 8404 /*RoundToStep=*/true, Captures); 8405 if (!Diff.isUsable()) 8406 return nullptr; 8407 8408 // OpenMP runtime requires 32-bit or 64-bit loop variables. 8409 QualType Type = Diff.get()->getType(); 8410 ASTContext &C = SemaRef.Context; 8411 bool UseVarType = VarType->hasIntegerRepresentation() && 8412 C.getTypeSize(Type) > C.getTypeSize(VarType); 8413 if (!Type->isIntegerType() || UseVarType) { 8414 unsigned NewSize = 8415 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 8416 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 8417 : Type->hasSignedIntegerRepresentation(); 8418 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 8419 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 8420 Diff = SemaRef.PerformImplicitConversion( 8421 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 8422 if (!Diff.isUsable()) 8423 return nullptr; 8424 } 8425 } 8426 if (LimitedType) { 8427 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 8428 if (NewSize != C.getTypeSize(Type)) { 8429 if (NewSize < C.getTypeSize(Type)) { 8430 assert(NewSize == 64 && "incorrect loop var size"); 8431 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 8432 << InitSrcRange << ConditionSrcRange; 8433 } 8434 QualType NewType = C.getIntTypeForBitwidth( 8435 NewSize, Type->hasSignedIntegerRepresentation() || 8436 C.getTypeSize(Type) < NewSize); 8437 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 8438 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 8439 Sema::AA_Converting, true); 8440 if (!Diff.isUsable()) 8441 return nullptr; 8442 } 8443 } 8444 } 8445 8446 return Diff.get(); 8447 } 8448 8449 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 8450 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8451 // Do not build for iterators, they cannot be used in non-rectangular loop 8452 // nests. 8453 if (LCDecl->getType()->isRecordType()) 8454 return std::make_pair(nullptr, nullptr); 8455 // If we subtract, the min is in the condition, otherwise the min is in the 8456 // init value. 8457 Expr *MinExpr = nullptr; 8458 Expr *MaxExpr = nullptr; 8459 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 8460 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 8461 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 8462 : CondDependOnLC.hasValue(); 8463 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 8464 : InitDependOnLC.hasValue(); 8465 Expr *Lower = 8466 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8467 Expr *Upper = 8468 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8469 if (!Upper || !Lower) 8470 return std::make_pair(nullptr, nullptr); 8471 8472 if (TestIsLessOp.getValue()) 8473 MinExpr = Lower; 8474 else 8475 MaxExpr = Upper; 8476 8477 // Build minimum/maximum value based on number of iterations. 8478 QualType VarType = LCDecl->getType().getNonReferenceType(); 8479 8480 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8481 Step, VarType, TestIsStrictOp, 8482 /*RoundToStep=*/false, Captures); 8483 if (!Diff.isUsable()) 8484 return std::make_pair(nullptr, nullptr); 8485 8486 // ((Upper - Lower [- 1]) / Step) * Step 8487 // Parentheses (for dumping/debugging purposes only). 8488 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8489 if (!Diff.isUsable()) 8490 return std::make_pair(nullptr, nullptr); 8491 8492 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8493 if (!NewStep.isUsable()) 8494 return std::make_pair(nullptr, nullptr); 8495 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 8496 if (!Diff.isUsable()) 8497 return std::make_pair(nullptr, nullptr); 8498 8499 // Parentheses (for dumping/debugging purposes only). 8500 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8501 if (!Diff.isUsable()) 8502 return std::make_pair(nullptr, nullptr); 8503 8504 // Convert to the ptrdiff_t, if original type is pointer. 8505 if (VarType->isAnyPointerType() && 8506 !SemaRef.Context.hasSameType( 8507 Diff.get()->getType(), 8508 SemaRef.Context.getUnsignedPointerDiffType())) { 8509 Diff = SemaRef.PerformImplicitConversion( 8510 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 8511 Sema::AA_Converting, /*AllowExplicit=*/true); 8512 } 8513 if (!Diff.isUsable()) 8514 return std::make_pair(nullptr, nullptr); 8515 8516 if (TestIsLessOp.getValue()) { 8517 // MinExpr = Lower; 8518 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 8519 Diff = SemaRef.BuildBinOp( 8520 S, DefaultLoc, BO_Add, 8521 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 8522 Diff.get()); 8523 if (!Diff.isUsable()) 8524 return std::make_pair(nullptr, nullptr); 8525 } else { 8526 // MaxExpr = Upper; 8527 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 8528 Diff = SemaRef.BuildBinOp( 8529 S, DefaultLoc, BO_Sub, 8530 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8531 Diff.get()); 8532 if (!Diff.isUsable()) 8533 return std::make_pair(nullptr, nullptr); 8534 } 8535 8536 // Convert to the original type. 8537 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 8538 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 8539 Sema::AA_Converting, 8540 /*AllowExplicit=*/true); 8541 if (!Diff.isUsable()) 8542 return std::make_pair(nullptr, nullptr); 8543 8544 Sema::TentativeAnalysisScope Trap(SemaRef); 8545 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 8546 if (!Diff.isUsable()) 8547 return std::make_pair(nullptr, nullptr); 8548 8549 if (TestIsLessOp.getValue()) 8550 MaxExpr = Diff.get(); 8551 else 8552 MinExpr = Diff.get(); 8553 8554 return std::make_pair(MinExpr, MaxExpr); 8555 } 8556 8557 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 8558 if (InitDependOnLC || CondDependOnLC) 8559 return Condition; 8560 return nullptr; 8561 } 8562 8563 Expr *OpenMPIterationSpaceChecker::buildPreCond( 8564 Scope *S, Expr *Cond, 8565 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8566 // Do not build a precondition when the condition/initialization is dependent 8567 // to prevent pessimistic early loop exit. 8568 // TODO: this can be improved by calculating min/max values but not sure that 8569 // it will be very effective. 8570 if (CondDependOnLC || InitDependOnLC) 8571 return SemaRef.PerformImplicitConversion( 8572 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 8573 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8574 /*AllowExplicit=*/true).get(); 8575 8576 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 8577 Sema::TentativeAnalysisScope Trap(SemaRef); 8578 8579 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 8580 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 8581 if (!NewLB.isUsable() || !NewUB.isUsable()) 8582 return nullptr; 8583 8584 ExprResult CondExpr = 8585 SemaRef.BuildBinOp(S, DefaultLoc, 8586 TestIsLessOp.getValue() ? 8587 (TestIsStrictOp ? BO_LT : BO_LE) : 8588 (TestIsStrictOp ? BO_GT : BO_GE), 8589 NewLB.get(), NewUB.get()); 8590 if (CondExpr.isUsable()) { 8591 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 8592 SemaRef.Context.BoolTy)) 8593 CondExpr = SemaRef.PerformImplicitConversion( 8594 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8595 /*AllowExplicit=*/true); 8596 } 8597 8598 // Otherwise use original loop condition and evaluate it in runtime. 8599 return CondExpr.isUsable() ? CondExpr.get() : Cond; 8600 } 8601 8602 /// Build reference expression to the counter be used for codegen. 8603 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 8604 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 8605 DSAStackTy &DSA) const { 8606 auto *VD = dyn_cast<VarDecl>(LCDecl); 8607 if (!VD) { 8608 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 8609 DeclRefExpr *Ref = buildDeclRefExpr( 8610 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 8611 const DSAStackTy::DSAVarData Data = 8612 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 8613 // If the loop control decl is explicitly marked as private, do not mark it 8614 // as captured again. 8615 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 8616 Captures.insert(std::make_pair(LCRef, Ref)); 8617 return Ref; 8618 } 8619 return cast<DeclRefExpr>(LCRef); 8620 } 8621 8622 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 8623 if (LCDecl && !LCDecl->isInvalidDecl()) { 8624 QualType Type = LCDecl->getType().getNonReferenceType(); 8625 VarDecl *PrivateVar = buildVarDecl( 8626 SemaRef, DefaultLoc, Type, LCDecl->getName(), 8627 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 8628 isa<VarDecl>(LCDecl) 8629 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 8630 : nullptr); 8631 if (PrivateVar->isInvalidDecl()) 8632 return nullptr; 8633 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 8634 } 8635 return nullptr; 8636 } 8637 8638 /// Build initialization of the counter to be used for codegen. 8639 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 8640 8641 /// Build step of the counter be used for codegen. 8642 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 8643 8644 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 8645 Scope *S, Expr *Counter, 8646 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 8647 Expr *Inc, OverloadedOperatorKind OOK) { 8648 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 8649 if (!Cnt) 8650 return nullptr; 8651 if (Inc) { 8652 assert((OOK == OO_Plus || OOK == OO_Minus) && 8653 "Expected only + or - operations for depend clauses."); 8654 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 8655 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 8656 if (!Cnt) 8657 return nullptr; 8658 } 8659 QualType VarType = LCDecl->getType().getNonReferenceType(); 8660 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8661 !SemaRef.getLangOpts().CPlusPlus) 8662 return nullptr; 8663 // Upper - Lower 8664 Expr *Upper = TestIsLessOp.getValue() 8665 ? Cnt 8666 : tryBuildCapture(SemaRef, LB, Captures).get(); 8667 Expr *Lower = TestIsLessOp.getValue() 8668 ? tryBuildCapture(SemaRef, LB, Captures).get() 8669 : Cnt; 8670 if (!Upper || !Lower) 8671 return nullptr; 8672 8673 ExprResult Diff = calculateNumIters( 8674 SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 8675 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures); 8676 if (!Diff.isUsable()) 8677 return nullptr; 8678 8679 return Diff.get(); 8680 } 8681 } // namespace 8682 8683 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 8684 assert(getLangOpts().OpenMP && "OpenMP is not active."); 8685 assert(Init && "Expected loop in canonical form."); 8686 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 8687 if (AssociatedLoops > 0 && 8688 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 8689 DSAStack->loopStart(); 8690 OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true, 8691 *DSAStack, ForLoc); 8692 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 8693 if (ValueDecl *D = ISC.getLoopDecl()) { 8694 auto *VD = dyn_cast<VarDecl>(D); 8695 DeclRefExpr *PrivateRef = nullptr; 8696 if (!VD) { 8697 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 8698 VD = Private; 8699 } else { 8700 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 8701 /*WithInit=*/false); 8702 VD = cast<VarDecl>(PrivateRef->getDecl()); 8703 } 8704 } 8705 DSAStack->addLoopControlVariable(D, VD); 8706 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 8707 if (LD != D->getCanonicalDecl()) { 8708 DSAStack->resetPossibleLoopCounter(); 8709 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 8710 MarkDeclarationsReferencedInExpr( 8711 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 8712 Var->getType().getNonLValueExprType(Context), 8713 ForLoc, /*RefersToCapture=*/true)); 8714 } 8715 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8716 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 8717 // Referenced in a Construct, C/C++]. The loop iteration variable in the 8718 // associated for-loop of a simd construct with just one associated 8719 // for-loop may be listed in a linear clause with a constant-linear-step 8720 // that is the increment of the associated for-loop. The loop iteration 8721 // variable(s) in the associated for-loop(s) of a for or parallel for 8722 // construct may be listed in a private or lastprivate clause. 8723 DSAStackTy::DSAVarData DVar = 8724 DSAStack->getTopDSA(D, /*FromParent=*/false); 8725 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 8726 // is declared in the loop and it is predetermined as a private. 8727 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 8728 OpenMPClauseKind PredeterminedCKind = 8729 isOpenMPSimdDirective(DKind) 8730 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 8731 : OMPC_private; 8732 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8733 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 8734 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 8735 DVar.CKind != OMPC_private))) || 8736 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 8737 DKind == OMPD_master_taskloop || 8738 DKind == OMPD_parallel_master_taskloop || 8739 isOpenMPDistributeDirective(DKind)) && 8740 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8741 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 8742 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 8743 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 8744 << getOpenMPClauseName(DVar.CKind) 8745 << getOpenMPDirectiveName(DKind) 8746 << getOpenMPClauseName(PredeterminedCKind); 8747 if (DVar.RefExpr == nullptr) 8748 DVar.CKind = PredeterminedCKind; 8749 reportOriginalDsa(*this, DSAStack, D, DVar, 8750 /*IsLoopIterVar=*/true); 8751 } else if (LoopDeclRefExpr) { 8752 // Make the loop iteration variable private (for worksharing 8753 // constructs), linear (for simd directives with the only one 8754 // associated loop) or lastprivate (for simd directives with several 8755 // collapsed or ordered loops). 8756 if (DVar.CKind == OMPC_unknown) 8757 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 8758 PrivateRef); 8759 } 8760 } 8761 } 8762 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 8763 } 8764 } 8765 8766 /// Called on a for stmt to check and extract its iteration space 8767 /// for further processing (such as collapsing). 8768 static bool checkOpenMPIterationSpace( 8769 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 8770 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 8771 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 8772 Expr *OrderedLoopCountExpr, 8773 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8774 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 8775 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8776 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind); 8777 // OpenMP [2.9.1, Canonical Loop Form] 8778 // for (init-expr; test-expr; incr-expr) structured-block 8779 // for (range-decl: range-expr) structured-block 8780 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S)) 8781 S = CanonLoop->getLoopStmt(); 8782 auto *For = dyn_cast_or_null<ForStmt>(S); 8783 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 8784 // Ranged for is supported only in OpenMP 5.0. 8785 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 8786 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 8787 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 8788 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 8789 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 8790 if (TotalNestedLoopCount > 1) { 8791 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 8792 SemaRef.Diag(DSA.getConstructLoc(), 8793 diag::note_omp_collapse_ordered_expr) 8794 << 2 << CollapseLoopCountExpr->getSourceRange() 8795 << OrderedLoopCountExpr->getSourceRange(); 8796 else if (CollapseLoopCountExpr) 8797 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8798 diag::note_omp_collapse_ordered_expr) 8799 << 0 << CollapseLoopCountExpr->getSourceRange(); 8800 else 8801 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8802 diag::note_omp_collapse_ordered_expr) 8803 << 1 << OrderedLoopCountExpr->getSourceRange(); 8804 } 8805 return true; 8806 } 8807 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 8808 "No loop body."); 8809 8810 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA, 8811 For ? For->getForLoc() : CXXFor->getForLoc()); 8812 8813 // Check init. 8814 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 8815 if (ISC.checkAndSetInit(Init)) 8816 return true; 8817 8818 bool HasErrors = false; 8819 8820 // Check loop variable's type. 8821 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 8822 // OpenMP [2.6, Canonical Loop Form] 8823 // Var is one of the following: 8824 // A variable of signed or unsigned integer type. 8825 // For C++, a variable of a random access iterator type. 8826 // For C, a variable of a pointer type. 8827 QualType VarType = LCDecl->getType().getNonReferenceType(); 8828 if (!VarType->isDependentType() && !VarType->isIntegerType() && 8829 !VarType->isPointerType() && 8830 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 8831 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 8832 << SemaRef.getLangOpts().CPlusPlus; 8833 HasErrors = true; 8834 } 8835 8836 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 8837 // a Construct 8838 // The loop iteration variable(s) in the associated for-loop(s) of a for or 8839 // parallel for construct is (are) private. 8840 // The loop iteration variable in the associated for-loop of a simd 8841 // construct with just one associated for-loop is linear with a 8842 // constant-linear-step that is the increment of the associated for-loop. 8843 // Exclude loop var from the list of variables with implicitly defined data 8844 // sharing attributes. 8845 VarsWithImplicitDSA.erase(LCDecl); 8846 8847 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 8848 8849 // Check test-expr. 8850 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 8851 8852 // Check incr-expr. 8853 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 8854 } 8855 8856 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 8857 return HasErrors; 8858 8859 // Build the loop's iteration space representation. 8860 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 8861 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 8862 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 8863 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 8864 (isOpenMPWorksharingDirective(DKind) || 8865 isOpenMPGenericLoopDirective(DKind) || 8866 isOpenMPTaskLoopDirective(DKind) || 8867 isOpenMPDistributeDirective(DKind) || 8868 isOpenMPLoopTransformationDirective(DKind)), 8869 Captures); 8870 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 8871 ISC.buildCounterVar(Captures, DSA); 8872 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 8873 ISC.buildPrivateCounterVar(); 8874 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 8875 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 8876 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 8877 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 8878 ISC.getConditionSrcRange(); 8879 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 8880 ISC.getIncrementSrcRange(); 8881 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 8882 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 8883 ISC.isStrictTestOp(); 8884 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 8885 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 8886 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 8887 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 8888 ISC.buildFinalCondition(DSA.getCurScope()); 8889 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 8890 ISC.doesInitDependOnLC(); 8891 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 8892 ISC.doesCondDependOnLC(); 8893 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 8894 ISC.getLoopDependentIdx(); 8895 8896 HasErrors |= 8897 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 8898 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 8899 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 8900 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 8901 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 8902 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 8903 if (!HasErrors && DSA.isOrderedRegion()) { 8904 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 8905 if (CurrentNestedLoopCount < 8906 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 8907 DSA.getOrderedRegionParam().second->setLoopNumIterations( 8908 CurrentNestedLoopCount, 8909 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 8910 DSA.getOrderedRegionParam().second->setLoopCounter( 8911 CurrentNestedLoopCount, 8912 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 8913 } 8914 } 8915 for (auto &Pair : DSA.getDoacrossDependClauses()) { 8916 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 8917 // Erroneous case - clause has some problems. 8918 continue; 8919 } 8920 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 8921 Pair.second.size() <= CurrentNestedLoopCount) { 8922 // Erroneous case - clause has some problems. 8923 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 8924 continue; 8925 } 8926 Expr *CntValue; 8927 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 8928 CntValue = ISC.buildOrderedLoopData( 8929 DSA.getCurScope(), 8930 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8931 Pair.first->getDependencyLoc()); 8932 else 8933 CntValue = ISC.buildOrderedLoopData( 8934 DSA.getCurScope(), 8935 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8936 Pair.first->getDependencyLoc(), 8937 Pair.second[CurrentNestedLoopCount].first, 8938 Pair.second[CurrentNestedLoopCount].second); 8939 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 8940 } 8941 } 8942 8943 return HasErrors; 8944 } 8945 8946 /// Build 'VarRef = Start. 8947 static ExprResult 8948 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8949 ExprResult Start, bool IsNonRectangularLB, 8950 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8951 // Build 'VarRef = Start. 8952 ExprResult NewStart = IsNonRectangularLB 8953 ? Start.get() 8954 : tryBuildCapture(SemaRef, Start.get(), Captures); 8955 if (!NewStart.isUsable()) 8956 return ExprError(); 8957 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 8958 VarRef.get()->getType())) { 8959 NewStart = SemaRef.PerformImplicitConversion( 8960 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 8961 /*AllowExplicit=*/true); 8962 if (!NewStart.isUsable()) 8963 return ExprError(); 8964 } 8965 8966 ExprResult Init = 8967 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8968 return Init; 8969 } 8970 8971 /// Build 'VarRef = Start + Iter * Step'. 8972 static ExprResult buildCounterUpdate( 8973 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8974 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8975 bool IsNonRectangularLB, 8976 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8977 // Add parentheses (for debugging purposes only). 8978 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8979 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8980 !Step.isUsable()) 8981 return ExprError(); 8982 8983 ExprResult NewStep = Step; 8984 if (Captures) 8985 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8986 if (NewStep.isInvalid()) 8987 return ExprError(); 8988 ExprResult Update = 8989 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 8990 if (!Update.isUsable()) 8991 return ExprError(); 8992 8993 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 8994 // 'VarRef = Start (+|-) Iter * Step'. 8995 if (!Start.isUsable()) 8996 return ExprError(); 8997 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 8998 if (!NewStart.isUsable()) 8999 return ExprError(); 9000 if (Captures && !IsNonRectangularLB) 9001 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 9002 if (NewStart.isInvalid()) 9003 return ExprError(); 9004 9005 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 9006 ExprResult SavedUpdate = Update; 9007 ExprResult UpdateVal; 9008 if (VarRef.get()->getType()->isOverloadableType() || 9009 NewStart.get()->getType()->isOverloadableType() || 9010 Update.get()->getType()->isOverloadableType()) { 9011 Sema::TentativeAnalysisScope Trap(SemaRef); 9012 9013 Update = 9014 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 9015 if (Update.isUsable()) { 9016 UpdateVal = 9017 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 9018 VarRef.get(), SavedUpdate.get()); 9019 if (UpdateVal.isUsable()) { 9020 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 9021 UpdateVal.get()); 9022 } 9023 } 9024 } 9025 9026 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 9027 if (!Update.isUsable() || !UpdateVal.isUsable()) { 9028 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 9029 NewStart.get(), SavedUpdate.get()); 9030 if (!Update.isUsable()) 9031 return ExprError(); 9032 9033 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 9034 VarRef.get()->getType())) { 9035 Update = SemaRef.PerformImplicitConversion( 9036 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 9037 if (!Update.isUsable()) 9038 return ExprError(); 9039 } 9040 9041 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 9042 } 9043 return Update; 9044 } 9045 9046 /// Convert integer expression \a E to make it have at least \a Bits 9047 /// bits. 9048 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 9049 if (E == nullptr) 9050 return ExprError(); 9051 ASTContext &C = SemaRef.Context; 9052 QualType OldType = E->getType(); 9053 unsigned HasBits = C.getTypeSize(OldType); 9054 if (HasBits >= Bits) 9055 return ExprResult(E); 9056 // OK to convert to signed, because new type has more bits than old. 9057 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 9058 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 9059 true); 9060 } 9061 9062 /// Check if the given expression \a E is a constant integer that fits 9063 /// into \a Bits bits. 9064 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 9065 if (E == nullptr) 9066 return false; 9067 if (Optional<llvm::APSInt> Result = 9068 E->getIntegerConstantExpr(SemaRef.Context)) 9069 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 9070 return false; 9071 } 9072 9073 /// Build preinits statement for the given declarations. 9074 static Stmt *buildPreInits(ASTContext &Context, 9075 MutableArrayRef<Decl *> PreInits) { 9076 if (!PreInits.empty()) { 9077 return new (Context) DeclStmt( 9078 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 9079 SourceLocation(), SourceLocation()); 9080 } 9081 return nullptr; 9082 } 9083 9084 /// Build preinits statement for the given declarations. 9085 static Stmt * 9086 buildPreInits(ASTContext &Context, 9087 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 9088 if (!Captures.empty()) { 9089 SmallVector<Decl *, 16> PreInits; 9090 for (const auto &Pair : Captures) 9091 PreInits.push_back(Pair.second->getDecl()); 9092 return buildPreInits(Context, PreInits); 9093 } 9094 return nullptr; 9095 } 9096 9097 /// Build postupdate expression for the given list of postupdates expressions. 9098 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 9099 Expr *PostUpdate = nullptr; 9100 if (!PostUpdates.empty()) { 9101 for (Expr *E : PostUpdates) { 9102 Expr *ConvE = S.BuildCStyleCastExpr( 9103 E->getExprLoc(), 9104 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 9105 E->getExprLoc(), E) 9106 .get(); 9107 PostUpdate = PostUpdate 9108 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 9109 PostUpdate, ConvE) 9110 .get() 9111 : ConvE; 9112 } 9113 } 9114 return PostUpdate; 9115 } 9116 9117 /// Called on a for stmt to check itself and nested loops (if any). 9118 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 9119 /// number of collapsed loops otherwise. 9120 static unsigned 9121 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 9122 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 9123 DSAStackTy &DSA, 9124 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 9125 OMPLoopBasedDirective::HelperExprs &Built) { 9126 unsigned NestedLoopCount = 1; 9127 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) && 9128 !isOpenMPLoopTransformationDirective(DKind); 9129 9130 if (CollapseLoopCountExpr) { 9131 // Found 'collapse' clause - calculate collapse number. 9132 Expr::EvalResult Result; 9133 if (!CollapseLoopCountExpr->isValueDependent() && 9134 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 9135 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 9136 } else { 9137 Built.clear(/*Size=*/1); 9138 return 1; 9139 } 9140 } 9141 unsigned OrderedLoopCount = 1; 9142 if (OrderedLoopCountExpr) { 9143 // Found 'ordered' clause - calculate collapse number. 9144 Expr::EvalResult EVResult; 9145 if (!OrderedLoopCountExpr->isValueDependent() && 9146 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 9147 SemaRef.getASTContext())) { 9148 llvm::APSInt Result = EVResult.Val.getInt(); 9149 if (Result.getLimitedValue() < NestedLoopCount) { 9150 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 9151 diag::err_omp_wrong_ordered_loop_count) 9152 << OrderedLoopCountExpr->getSourceRange(); 9153 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 9154 diag::note_collapse_loop_count) 9155 << CollapseLoopCountExpr->getSourceRange(); 9156 } 9157 OrderedLoopCount = Result.getLimitedValue(); 9158 } else { 9159 Built.clear(/*Size=*/1); 9160 return 1; 9161 } 9162 } 9163 // This is helper routine for loop directives (e.g., 'for', 'simd', 9164 // 'for simd', etc.). 9165 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9166 unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount); 9167 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops); 9168 if (!OMPLoopBasedDirective::doForAllLoops( 9169 AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)), 9170 SupportsNonPerfectlyNested, NumLoops, 9171 [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount, 9172 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA, 9173 &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) { 9174 if (checkOpenMPIterationSpace( 9175 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 9176 NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr, 9177 VarsWithImplicitDSA, IterSpaces, Captures)) 9178 return true; 9179 if (Cnt > 0 && Cnt >= NestedLoopCount && 9180 IterSpaces[Cnt].CounterVar) { 9181 // Handle initialization of captured loop iterator variables. 9182 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 9183 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 9184 Captures[DRE] = DRE; 9185 } 9186 } 9187 return false; 9188 }, 9189 [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) { 9190 Stmt *DependentPreInits = Transform->getPreInits(); 9191 if (!DependentPreInits) 9192 return; 9193 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) { 9194 auto *D = cast<VarDecl>(C); 9195 DeclRefExpr *Ref = buildDeclRefExpr(SemaRef, D, D->getType(), 9196 Transform->getBeginLoc()); 9197 Captures[Ref] = Ref; 9198 } 9199 })) 9200 return 0; 9201 9202 Built.clear(/* size */ NestedLoopCount); 9203 9204 if (SemaRef.CurContext->isDependentContext()) 9205 return NestedLoopCount; 9206 9207 // An example of what is generated for the following code: 9208 // 9209 // #pragma omp simd collapse(2) ordered(2) 9210 // for (i = 0; i < NI; ++i) 9211 // for (k = 0; k < NK; ++k) 9212 // for (j = J0; j < NJ; j+=2) { 9213 // <loop body> 9214 // } 9215 // 9216 // We generate the code below. 9217 // Note: the loop body may be outlined in CodeGen. 9218 // Note: some counters may be C++ classes, operator- is used to find number of 9219 // iterations and operator+= to calculate counter value. 9220 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 9221 // or i64 is currently supported). 9222 // 9223 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 9224 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 9225 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 9226 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 9227 // // similar updates for vars in clauses (e.g. 'linear') 9228 // <loop body (using local i and j)> 9229 // } 9230 // i = NI; // assign final values of counters 9231 // j = NJ; 9232 // 9233 9234 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 9235 // the iteration counts of the collapsed for loops. 9236 // Precondition tests if there is at least one iteration (all conditions are 9237 // true). 9238 auto PreCond = ExprResult(IterSpaces[0].PreCond); 9239 Expr *N0 = IterSpaces[0].NumIterations; 9240 ExprResult LastIteration32 = 9241 widenIterationCount(/*Bits=*/32, 9242 SemaRef 9243 .PerformImplicitConversion( 9244 N0->IgnoreImpCasts(), N0->getType(), 9245 Sema::AA_Converting, /*AllowExplicit=*/true) 9246 .get(), 9247 SemaRef); 9248 ExprResult LastIteration64 = widenIterationCount( 9249 /*Bits=*/64, 9250 SemaRef 9251 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 9252 Sema::AA_Converting, 9253 /*AllowExplicit=*/true) 9254 .get(), 9255 SemaRef); 9256 9257 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 9258 return NestedLoopCount; 9259 9260 ASTContext &C = SemaRef.Context; 9261 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 9262 9263 Scope *CurScope = DSA.getCurScope(); 9264 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 9265 if (PreCond.isUsable()) { 9266 PreCond = 9267 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 9268 PreCond.get(), IterSpaces[Cnt].PreCond); 9269 } 9270 Expr *N = IterSpaces[Cnt].NumIterations; 9271 SourceLocation Loc = N->getExprLoc(); 9272 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 9273 if (LastIteration32.isUsable()) 9274 LastIteration32 = SemaRef.BuildBinOp( 9275 CurScope, Loc, BO_Mul, LastIteration32.get(), 9276 SemaRef 9277 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9278 Sema::AA_Converting, 9279 /*AllowExplicit=*/true) 9280 .get()); 9281 if (LastIteration64.isUsable()) 9282 LastIteration64 = SemaRef.BuildBinOp( 9283 CurScope, Loc, BO_Mul, LastIteration64.get(), 9284 SemaRef 9285 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9286 Sema::AA_Converting, 9287 /*AllowExplicit=*/true) 9288 .get()); 9289 } 9290 9291 // Choose either the 32-bit or 64-bit version. 9292 ExprResult LastIteration = LastIteration64; 9293 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 9294 (LastIteration32.isUsable() && 9295 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 9296 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 9297 fitsInto( 9298 /*Bits=*/32, 9299 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 9300 LastIteration64.get(), SemaRef)))) 9301 LastIteration = LastIteration32; 9302 QualType VType = LastIteration.get()->getType(); 9303 QualType RealVType = VType; 9304 QualType StrideVType = VType; 9305 if (isOpenMPTaskLoopDirective(DKind)) { 9306 VType = 9307 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 9308 StrideVType = 9309 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 9310 } 9311 9312 if (!LastIteration.isUsable()) 9313 return 0; 9314 9315 // Save the number of iterations. 9316 ExprResult NumIterations = LastIteration; 9317 { 9318 LastIteration = SemaRef.BuildBinOp( 9319 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 9320 LastIteration.get(), 9321 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9322 if (!LastIteration.isUsable()) 9323 return 0; 9324 } 9325 9326 // Calculate the last iteration number beforehand instead of doing this on 9327 // each iteration. Do not do this if the number of iterations may be kfold-ed. 9328 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 9329 ExprResult CalcLastIteration; 9330 if (!IsConstant) { 9331 ExprResult SaveRef = 9332 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 9333 LastIteration = SaveRef; 9334 9335 // Prepare SaveRef + 1. 9336 NumIterations = SemaRef.BuildBinOp( 9337 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 9338 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9339 if (!NumIterations.isUsable()) 9340 return 0; 9341 } 9342 9343 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 9344 9345 // Build variables passed into runtime, necessary for worksharing directives. 9346 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 9347 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9348 isOpenMPDistributeDirective(DKind) || 9349 isOpenMPGenericLoopDirective(DKind) || 9350 isOpenMPLoopTransformationDirective(DKind)) { 9351 // Lower bound variable, initialized with zero. 9352 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 9353 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 9354 SemaRef.AddInitializerToDecl(LBDecl, 9355 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9356 /*DirectInit*/ false); 9357 9358 // Upper bound variable, initialized with last iteration number. 9359 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 9360 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 9361 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 9362 /*DirectInit*/ false); 9363 9364 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 9365 // This will be used to implement clause 'lastprivate'. 9366 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 9367 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 9368 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 9369 SemaRef.AddInitializerToDecl(ILDecl, 9370 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9371 /*DirectInit*/ false); 9372 9373 // Stride variable returned by runtime (we initialize it to 1 by default). 9374 VarDecl *STDecl = 9375 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 9376 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 9377 SemaRef.AddInitializerToDecl(STDecl, 9378 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 9379 /*DirectInit*/ false); 9380 9381 // Build expression: UB = min(UB, LastIteration) 9382 // It is necessary for CodeGen of directives with static scheduling. 9383 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 9384 UB.get(), LastIteration.get()); 9385 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9386 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 9387 LastIteration.get(), UB.get()); 9388 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 9389 CondOp.get()); 9390 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 9391 9392 // If we have a combined directive that combines 'distribute', 'for' or 9393 // 'simd' we need to be able to access the bounds of the schedule of the 9394 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 9395 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 9396 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9397 // Lower bound variable, initialized with zero. 9398 VarDecl *CombLBDecl = 9399 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 9400 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 9401 SemaRef.AddInitializerToDecl( 9402 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9403 /*DirectInit*/ false); 9404 9405 // Upper bound variable, initialized with last iteration number. 9406 VarDecl *CombUBDecl = 9407 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 9408 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 9409 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 9410 /*DirectInit*/ false); 9411 9412 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 9413 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 9414 ExprResult CombCondOp = 9415 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 9416 LastIteration.get(), CombUB.get()); 9417 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 9418 CombCondOp.get()); 9419 CombEUB = 9420 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 9421 9422 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 9423 // We expect to have at least 2 more parameters than the 'parallel' 9424 // directive does - the lower and upper bounds of the previous schedule. 9425 assert(CD->getNumParams() >= 4 && 9426 "Unexpected number of parameters in loop combined directive"); 9427 9428 // Set the proper type for the bounds given what we learned from the 9429 // enclosed loops. 9430 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 9431 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 9432 9433 // Previous lower and upper bounds are obtained from the region 9434 // parameters. 9435 PrevLB = 9436 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 9437 PrevUB = 9438 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 9439 } 9440 } 9441 9442 // Build the iteration variable and its initialization before loop. 9443 ExprResult IV; 9444 ExprResult Init, CombInit; 9445 { 9446 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 9447 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 9448 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 9449 isOpenMPGenericLoopDirective(DKind) || 9450 isOpenMPTaskLoopDirective(DKind) || 9451 isOpenMPDistributeDirective(DKind) || 9452 isOpenMPLoopTransformationDirective(DKind)) 9453 ? LB.get() 9454 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9455 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 9456 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 9457 9458 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9459 Expr *CombRHS = 9460 (isOpenMPWorksharingDirective(DKind) || 9461 isOpenMPGenericLoopDirective(DKind) || 9462 isOpenMPTaskLoopDirective(DKind) || 9463 isOpenMPDistributeDirective(DKind)) 9464 ? CombLB.get() 9465 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9466 CombInit = 9467 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 9468 CombInit = 9469 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 9470 } 9471 } 9472 9473 bool UseStrictCompare = 9474 RealVType->hasUnsignedIntegerRepresentation() && 9475 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 9476 return LIS.IsStrictCompare; 9477 }); 9478 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 9479 // unsigned IV)) for worksharing loops. 9480 SourceLocation CondLoc = AStmt->getBeginLoc(); 9481 Expr *BoundUB = UB.get(); 9482 if (UseStrictCompare) { 9483 BoundUB = 9484 SemaRef 9485 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 9486 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9487 .get(); 9488 BoundUB = 9489 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 9490 } 9491 ExprResult Cond = 9492 (isOpenMPWorksharingDirective(DKind) || 9493 isOpenMPGenericLoopDirective(DKind) || 9494 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || 9495 isOpenMPLoopTransformationDirective(DKind)) 9496 ? SemaRef.BuildBinOp(CurScope, CondLoc, 9497 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 9498 BoundUB) 9499 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9500 NumIterations.get()); 9501 ExprResult CombDistCond; 9502 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9503 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9504 NumIterations.get()); 9505 } 9506 9507 ExprResult CombCond; 9508 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9509 Expr *BoundCombUB = CombUB.get(); 9510 if (UseStrictCompare) { 9511 BoundCombUB = 9512 SemaRef 9513 .BuildBinOp( 9514 CurScope, CondLoc, BO_Add, BoundCombUB, 9515 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9516 .get(); 9517 BoundCombUB = 9518 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 9519 .get(); 9520 } 9521 CombCond = 9522 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9523 IV.get(), BoundCombUB); 9524 } 9525 // Loop increment (IV = IV + 1) 9526 SourceLocation IncLoc = AStmt->getBeginLoc(); 9527 ExprResult Inc = 9528 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 9529 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 9530 if (!Inc.isUsable()) 9531 return 0; 9532 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 9533 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 9534 if (!Inc.isUsable()) 9535 return 0; 9536 9537 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 9538 // Used for directives with static scheduling. 9539 // In combined construct, add combined version that use CombLB and CombUB 9540 // base variables for the update 9541 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 9542 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9543 isOpenMPGenericLoopDirective(DKind) || 9544 isOpenMPDistributeDirective(DKind) || 9545 isOpenMPLoopTransformationDirective(DKind)) { 9546 // LB + ST 9547 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 9548 if (!NextLB.isUsable()) 9549 return 0; 9550 // LB = LB + ST 9551 NextLB = 9552 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 9553 NextLB = 9554 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 9555 if (!NextLB.isUsable()) 9556 return 0; 9557 // UB + ST 9558 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 9559 if (!NextUB.isUsable()) 9560 return 0; 9561 // UB = UB + ST 9562 NextUB = 9563 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 9564 NextUB = 9565 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 9566 if (!NextUB.isUsable()) 9567 return 0; 9568 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9569 CombNextLB = 9570 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 9571 if (!NextLB.isUsable()) 9572 return 0; 9573 // LB = LB + ST 9574 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 9575 CombNextLB.get()); 9576 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 9577 /*DiscardedValue*/ false); 9578 if (!CombNextLB.isUsable()) 9579 return 0; 9580 // UB + ST 9581 CombNextUB = 9582 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 9583 if (!CombNextUB.isUsable()) 9584 return 0; 9585 // UB = UB + ST 9586 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 9587 CombNextUB.get()); 9588 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 9589 /*DiscardedValue*/ false); 9590 if (!CombNextUB.isUsable()) 9591 return 0; 9592 } 9593 } 9594 9595 // Create increment expression for distribute loop when combined in a same 9596 // directive with for as IV = IV + ST; ensure upper bound expression based 9597 // on PrevUB instead of NumIterations - used to implement 'for' when found 9598 // in combination with 'distribute', like in 'distribute parallel for' 9599 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 9600 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 9601 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9602 DistCond = SemaRef.BuildBinOp( 9603 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 9604 assert(DistCond.isUsable() && "distribute cond expr was not built"); 9605 9606 DistInc = 9607 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 9608 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9609 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 9610 DistInc.get()); 9611 DistInc = 9612 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 9613 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9614 9615 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 9616 // construct 9617 ExprResult NewPrevUB = PrevUB; 9618 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 9619 if (!SemaRef.Context.hasSameType(UB.get()->getType(), 9620 PrevUB.get()->getType())) { 9621 NewPrevUB = SemaRef.BuildCStyleCastExpr( 9622 DistEUBLoc, 9623 SemaRef.Context.getTrivialTypeSourceInfo(UB.get()->getType()), 9624 DistEUBLoc, NewPrevUB.get()); 9625 if (!NewPrevUB.isUsable()) 9626 return 0; 9627 } 9628 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, 9629 UB.get(), NewPrevUB.get()); 9630 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9631 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), NewPrevUB.get(), UB.get()); 9632 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 9633 CondOp.get()); 9634 PrevEUB = 9635 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 9636 9637 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 9638 // parallel for is in combination with a distribute directive with 9639 // schedule(static, 1) 9640 Expr *BoundPrevUB = PrevUB.get(); 9641 if (UseStrictCompare) { 9642 BoundPrevUB = 9643 SemaRef 9644 .BuildBinOp( 9645 CurScope, CondLoc, BO_Add, BoundPrevUB, 9646 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9647 .get(); 9648 BoundPrevUB = 9649 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 9650 .get(); 9651 } 9652 ParForInDistCond = 9653 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9654 IV.get(), BoundPrevUB); 9655 } 9656 9657 // Build updates and final values of the loop counters. 9658 bool HasErrors = false; 9659 Built.Counters.resize(NestedLoopCount); 9660 Built.Inits.resize(NestedLoopCount); 9661 Built.Updates.resize(NestedLoopCount); 9662 Built.Finals.resize(NestedLoopCount); 9663 Built.DependentCounters.resize(NestedLoopCount); 9664 Built.DependentInits.resize(NestedLoopCount); 9665 Built.FinalsConditions.resize(NestedLoopCount); 9666 { 9667 // We implement the following algorithm for obtaining the 9668 // original loop iteration variable values based on the 9669 // value of the collapsed loop iteration variable IV. 9670 // 9671 // Let n+1 be the number of collapsed loops in the nest. 9672 // Iteration variables (I0, I1, .... In) 9673 // Iteration counts (N0, N1, ... Nn) 9674 // 9675 // Acc = IV; 9676 // 9677 // To compute Ik for loop k, 0 <= k <= n, generate: 9678 // Prod = N(k+1) * N(k+2) * ... * Nn; 9679 // Ik = Acc / Prod; 9680 // Acc -= Ik * Prod; 9681 // 9682 ExprResult Acc = IV; 9683 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 9684 LoopIterationSpace &IS = IterSpaces[Cnt]; 9685 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 9686 ExprResult Iter; 9687 9688 // Compute prod 9689 ExprResult Prod = 9690 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9691 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 9692 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 9693 IterSpaces[K].NumIterations); 9694 9695 // Iter = Acc / Prod 9696 // If there is at least one more inner loop to avoid 9697 // multiplication by 1. 9698 if (Cnt + 1 < NestedLoopCount) 9699 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 9700 Acc.get(), Prod.get()); 9701 else 9702 Iter = Acc; 9703 if (!Iter.isUsable()) { 9704 HasErrors = true; 9705 break; 9706 } 9707 9708 // Update Acc: 9709 // Acc -= Iter * Prod 9710 // Check if there is at least one more inner loop to avoid 9711 // multiplication by 1. 9712 if (Cnt + 1 < NestedLoopCount) 9713 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 9714 Iter.get(), Prod.get()); 9715 else 9716 Prod = Iter; 9717 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 9718 Acc.get(), Prod.get()); 9719 9720 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 9721 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 9722 DeclRefExpr *CounterVar = buildDeclRefExpr( 9723 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 9724 /*RefersToCapture=*/true); 9725 ExprResult Init = 9726 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 9727 IS.CounterInit, IS.IsNonRectangularLB, Captures); 9728 if (!Init.isUsable()) { 9729 HasErrors = true; 9730 break; 9731 } 9732 ExprResult Update = buildCounterUpdate( 9733 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 9734 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 9735 if (!Update.isUsable()) { 9736 HasErrors = true; 9737 break; 9738 } 9739 9740 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 9741 ExprResult Final = 9742 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 9743 IS.CounterInit, IS.NumIterations, IS.CounterStep, 9744 IS.Subtract, IS.IsNonRectangularLB, &Captures); 9745 if (!Final.isUsable()) { 9746 HasErrors = true; 9747 break; 9748 } 9749 9750 if (!Update.isUsable() || !Final.isUsable()) { 9751 HasErrors = true; 9752 break; 9753 } 9754 // Save results 9755 Built.Counters[Cnt] = IS.CounterVar; 9756 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 9757 Built.Inits[Cnt] = Init.get(); 9758 Built.Updates[Cnt] = Update.get(); 9759 Built.Finals[Cnt] = Final.get(); 9760 Built.DependentCounters[Cnt] = nullptr; 9761 Built.DependentInits[Cnt] = nullptr; 9762 Built.FinalsConditions[Cnt] = nullptr; 9763 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 9764 Built.DependentCounters[Cnt] = 9765 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9766 Built.DependentInits[Cnt] = 9767 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9768 Built.FinalsConditions[Cnt] = IS.FinalCondition; 9769 } 9770 } 9771 } 9772 9773 if (HasErrors) 9774 return 0; 9775 9776 // Save results 9777 Built.IterationVarRef = IV.get(); 9778 Built.LastIteration = LastIteration.get(); 9779 Built.NumIterations = NumIterations.get(); 9780 Built.CalcLastIteration = SemaRef 9781 .ActOnFinishFullExpr(CalcLastIteration.get(), 9782 /*DiscardedValue=*/false) 9783 .get(); 9784 Built.PreCond = PreCond.get(); 9785 Built.PreInits = buildPreInits(C, Captures); 9786 Built.Cond = Cond.get(); 9787 Built.Init = Init.get(); 9788 Built.Inc = Inc.get(); 9789 Built.LB = LB.get(); 9790 Built.UB = UB.get(); 9791 Built.IL = IL.get(); 9792 Built.ST = ST.get(); 9793 Built.EUB = EUB.get(); 9794 Built.NLB = NextLB.get(); 9795 Built.NUB = NextUB.get(); 9796 Built.PrevLB = PrevLB.get(); 9797 Built.PrevUB = PrevUB.get(); 9798 Built.DistInc = DistInc.get(); 9799 Built.PrevEUB = PrevEUB.get(); 9800 Built.DistCombinedFields.LB = CombLB.get(); 9801 Built.DistCombinedFields.UB = CombUB.get(); 9802 Built.DistCombinedFields.EUB = CombEUB.get(); 9803 Built.DistCombinedFields.Init = CombInit.get(); 9804 Built.DistCombinedFields.Cond = CombCond.get(); 9805 Built.DistCombinedFields.NLB = CombNextLB.get(); 9806 Built.DistCombinedFields.NUB = CombNextUB.get(); 9807 Built.DistCombinedFields.DistCond = CombDistCond.get(); 9808 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 9809 9810 return NestedLoopCount; 9811 } 9812 9813 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 9814 auto CollapseClauses = 9815 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 9816 if (CollapseClauses.begin() != CollapseClauses.end()) 9817 return (*CollapseClauses.begin())->getNumForLoops(); 9818 return nullptr; 9819 } 9820 9821 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 9822 auto OrderedClauses = 9823 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 9824 if (OrderedClauses.begin() != OrderedClauses.end()) 9825 return (*OrderedClauses.begin())->getNumForLoops(); 9826 return nullptr; 9827 } 9828 9829 static bool checkSimdlenSafelenSpecified(Sema &S, 9830 const ArrayRef<OMPClause *> Clauses) { 9831 const OMPSafelenClause *Safelen = nullptr; 9832 const OMPSimdlenClause *Simdlen = nullptr; 9833 9834 for (const OMPClause *Clause : Clauses) { 9835 if (Clause->getClauseKind() == OMPC_safelen) 9836 Safelen = cast<OMPSafelenClause>(Clause); 9837 else if (Clause->getClauseKind() == OMPC_simdlen) 9838 Simdlen = cast<OMPSimdlenClause>(Clause); 9839 if (Safelen && Simdlen) 9840 break; 9841 } 9842 9843 if (Simdlen && Safelen) { 9844 const Expr *SimdlenLength = Simdlen->getSimdlen(); 9845 const Expr *SafelenLength = Safelen->getSafelen(); 9846 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 9847 SimdlenLength->isInstantiationDependent() || 9848 SimdlenLength->containsUnexpandedParameterPack()) 9849 return false; 9850 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 9851 SafelenLength->isInstantiationDependent() || 9852 SafelenLength->containsUnexpandedParameterPack()) 9853 return false; 9854 Expr::EvalResult SimdlenResult, SafelenResult; 9855 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 9856 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 9857 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 9858 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 9859 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 9860 // If both simdlen and safelen clauses are specified, the value of the 9861 // simdlen parameter must be less than or equal to the value of the safelen 9862 // parameter. 9863 if (SimdlenRes > SafelenRes) { 9864 S.Diag(SimdlenLength->getExprLoc(), 9865 diag::err_omp_wrong_simdlen_safelen_values) 9866 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 9867 return true; 9868 } 9869 } 9870 return false; 9871 } 9872 9873 StmtResult 9874 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9875 SourceLocation StartLoc, SourceLocation EndLoc, 9876 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9877 if (!AStmt) 9878 return StmtError(); 9879 9880 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9881 OMPLoopBasedDirective::HelperExprs B; 9882 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9883 // define the nested loops number. 9884 unsigned NestedLoopCount = checkOpenMPLoop( 9885 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9886 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9887 if (NestedLoopCount == 0) 9888 return StmtError(); 9889 9890 assert((CurContext->isDependentContext() || B.builtAll()) && 9891 "omp simd loop exprs were not built"); 9892 9893 if (!CurContext->isDependentContext()) { 9894 // Finalize the clauses that need pre-built expressions for CodeGen. 9895 for (OMPClause *C : Clauses) { 9896 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9897 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9898 B.NumIterations, *this, CurScope, 9899 DSAStack)) 9900 return StmtError(); 9901 } 9902 } 9903 9904 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9905 return StmtError(); 9906 9907 setFunctionHasBranchProtectedScope(); 9908 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9909 Clauses, AStmt, B); 9910 } 9911 9912 StmtResult 9913 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9914 SourceLocation StartLoc, SourceLocation EndLoc, 9915 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9916 if (!AStmt) 9917 return StmtError(); 9918 9919 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9920 OMPLoopBasedDirective::HelperExprs B; 9921 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9922 // define the nested loops number. 9923 unsigned NestedLoopCount = checkOpenMPLoop( 9924 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9925 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9926 if (NestedLoopCount == 0) 9927 return StmtError(); 9928 9929 assert((CurContext->isDependentContext() || B.builtAll()) && 9930 "omp for loop exprs were not built"); 9931 9932 if (!CurContext->isDependentContext()) { 9933 // Finalize the clauses that need pre-built expressions for CodeGen. 9934 for (OMPClause *C : Clauses) { 9935 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9936 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9937 B.NumIterations, *this, CurScope, 9938 DSAStack)) 9939 return StmtError(); 9940 } 9941 } 9942 9943 setFunctionHasBranchProtectedScope(); 9944 return OMPForDirective::Create( 9945 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9946 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9947 } 9948 9949 StmtResult Sema::ActOnOpenMPForSimdDirective( 9950 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9951 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9952 if (!AStmt) 9953 return StmtError(); 9954 9955 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9956 OMPLoopBasedDirective::HelperExprs B; 9957 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9958 // define the nested loops number. 9959 unsigned NestedLoopCount = 9960 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 9961 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9962 VarsWithImplicitDSA, B); 9963 if (NestedLoopCount == 0) 9964 return StmtError(); 9965 9966 assert((CurContext->isDependentContext() || B.builtAll()) && 9967 "omp for simd loop exprs were not built"); 9968 9969 if (!CurContext->isDependentContext()) { 9970 // Finalize the clauses that need pre-built expressions for CodeGen. 9971 for (OMPClause *C : Clauses) { 9972 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9973 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9974 B.NumIterations, *this, CurScope, 9975 DSAStack)) 9976 return StmtError(); 9977 } 9978 } 9979 9980 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9981 return StmtError(); 9982 9983 setFunctionHasBranchProtectedScope(); 9984 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9985 Clauses, AStmt, B); 9986 } 9987 9988 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 9989 Stmt *AStmt, 9990 SourceLocation StartLoc, 9991 SourceLocation EndLoc) { 9992 if (!AStmt) 9993 return StmtError(); 9994 9995 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9996 auto BaseStmt = AStmt; 9997 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9998 BaseStmt = CS->getCapturedStmt(); 9999 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10000 auto S = C->children(); 10001 if (S.begin() == S.end()) 10002 return StmtError(); 10003 // All associated statements must be '#pragma omp section' except for 10004 // the first one. 10005 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 10006 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10007 if (SectionStmt) 10008 Diag(SectionStmt->getBeginLoc(), 10009 diag::err_omp_sections_substmt_not_section); 10010 return StmtError(); 10011 } 10012 cast<OMPSectionDirective>(SectionStmt) 10013 ->setHasCancel(DSAStack->isCancelRegion()); 10014 } 10015 } else { 10016 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 10017 return StmtError(); 10018 } 10019 10020 setFunctionHasBranchProtectedScope(); 10021 10022 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10023 DSAStack->getTaskgroupReductionRef(), 10024 DSAStack->isCancelRegion()); 10025 } 10026 10027 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 10028 SourceLocation StartLoc, 10029 SourceLocation EndLoc) { 10030 if (!AStmt) 10031 return StmtError(); 10032 10033 setFunctionHasBranchProtectedScope(); 10034 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 10035 10036 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 10037 DSAStack->isCancelRegion()); 10038 } 10039 10040 static Expr *getDirectCallExpr(Expr *E) { 10041 E = E->IgnoreParenCasts()->IgnoreImplicit(); 10042 if (auto *CE = dyn_cast<CallExpr>(E)) 10043 if (CE->getDirectCallee()) 10044 return E; 10045 return nullptr; 10046 } 10047 10048 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses, 10049 Stmt *AStmt, 10050 SourceLocation StartLoc, 10051 SourceLocation EndLoc) { 10052 if (!AStmt) 10053 return StmtError(); 10054 10055 Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt(); 10056 10057 // 5.1 OpenMP 10058 // expression-stmt : an expression statement with one of the following forms: 10059 // expression = target-call ( [expression-list] ); 10060 // target-call ( [expression-list] ); 10061 10062 SourceLocation TargetCallLoc; 10063 10064 if (!CurContext->isDependentContext()) { 10065 Expr *TargetCall = nullptr; 10066 10067 auto *E = dyn_cast<Expr>(S); 10068 if (!E) { 10069 Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call); 10070 return StmtError(); 10071 } 10072 10073 E = E->IgnoreParenCasts()->IgnoreImplicit(); 10074 10075 if (auto *BO = dyn_cast<BinaryOperator>(E)) { 10076 if (BO->getOpcode() == BO_Assign) 10077 TargetCall = getDirectCallExpr(BO->getRHS()); 10078 } else { 10079 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E)) 10080 if (COCE->getOperator() == OO_Equal) 10081 TargetCall = getDirectCallExpr(COCE->getArg(1)); 10082 if (!TargetCall) 10083 TargetCall = getDirectCallExpr(E); 10084 } 10085 if (!TargetCall) { 10086 Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call); 10087 return StmtError(); 10088 } 10089 TargetCallLoc = TargetCall->getExprLoc(); 10090 } 10091 10092 setFunctionHasBranchProtectedScope(); 10093 10094 return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10095 TargetCallLoc); 10096 } 10097 10098 StmtResult Sema::ActOnOpenMPGenericLoopDirective( 10099 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10100 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10101 if (!AStmt) 10102 return StmtError(); 10103 10104 // OpenMP 5.1 [2.11.7, loop construct] 10105 // A list item may not appear in a lastprivate clause unless it is the 10106 // loop iteration variable of a loop that is associated with the construct. 10107 for (OMPClause *C : Clauses) { 10108 if (auto *LPC = dyn_cast<OMPLastprivateClause>(C)) { 10109 for (Expr *RefExpr : LPC->varlists()) { 10110 SourceLocation ELoc; 10111 SourceRange ERange; 10112 Expr *SimpleRefExpr = RefExpr; 10113 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10114 if (ValueDecl *D = Res.first) { 10115 auto &&Info = DSAStack->isLoopControlVariable(D); 10116 if (!Info.first) { 10117 Diag(ELoc, diag::err_omp_lastprivate_loop_var_non_loop_iteration); 10118 return StmtError(); 10119 } 10120 } 10121 } 10122 } 10123 } 10124 10125 auto *CS = cast<CapturedStmt>(AStmt); 10126 // 1.2.2 OpenMP Language Terminology 10127 // Structured block - An executable statement with a single entry at the 10128 // top and a single exit at the bottom. 10129 // The point of exit cannot be a branch out of the structured block. 10130 // longjmp() and throw() must not violate the entry/exit criteria. 10131 CS->getCapturedDecl()->setNothrow(); 10132 10133 OMPLoopDirective::HelperExprs B; 10134 // In presence of clause 'collapse', it will define the nested loops number. 10135 unsigned NestedLoopCount = checkOpenMPLoop( 10136 OMPD_loop, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 10137 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 10138 if (NestedLoopCount == 0) 10139 return StmtError(); 10140 10141 assert((CurContext->isDependentContext() || B.builtAll()) && 10142 "omp loop exprs were not built"); 10143 10144 setFunctionHasBranchProtectedScope(); 10145 return OMPGenericLoopDirective::Create(Context, StartLoc, EndLoc, 10146 NestedLoopCount, Clauses, AStmt, B); 10147 } 10148 10149 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 10150 Stmt *AStmt, 10151 SourceLocation StartLoc, 10152 SourceLocation EndLoc) { 10153 if (!AStmt) 10154 return StmtError(); 10155 10156 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10157 10158 setFunctionHasBranchProtectedScope(); 10159 10160 // OpenMP [2.7.3, single Construct, Restrictions] 10161 // The copyprivate clause must not be used with the nowait clause. 10162 const OMPClause *Nowait = nullptr; 10163 const OMPClause *Copyprivate = nullptr; 10164 for (const OMPClause *Clause : Clauses) { 10165 if (Clause->getClauseKind() == OMPC_nowait) 10166 Nowait = Clause; 10167 else if (Clause->getClauseKind() == OMPC_copyprivate) 10168 Copyprivate = Clause; 10169 if (Copyprivate && Nowait) { 10170 Diag(Copyprivate->getBeginLoc(), 10171 diag::err_omp_single_copyprivate_with_nowait); 10172 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 10173 return StmtError(); 10174 } 10175 } 10176 10177 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10178 } 10179 10180 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 10181 SourceLocation StartLoc, 10182 SourceLocation EndLoc) { 10183 if (!AStmt) 10184 return StmtError(); 10185 10186 setFunctionHasBranchProtectedScope(); 10187 10188 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 10189 } 10190 10191 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses, 10192 Stmt *AStmt, 10193 SourceLocation StartLoc, 10194 SourceLocation EndLoc) { 10195 if (!AStmt) 10196 return StmtError(); 10197 10198 setFunctionHasBranchProtectedScope(); 10199 10200 return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10201 } 10202 10203 StmtResult Sema::ActOnOpenMPCriticalDirective( 10204 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 10205 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 10206 if (!AStmt) 10207 return StmtError(); 10208 10209 bool ErrorFound = false; 10210 llvm::APSInt Hint; 10211 SourceLocation HintLoc; 10212 bool DependentHint = false; 10213 for (const OMPClause *C : Clauses) { 10214 if (C->getClauseKind() == OMPC_hint) { 10215 if (!DirName.getName()) { 10216 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 10217 ErrorFound = true; 10218 } 10219 Expr *E = cast<OMPHintClause>(C)->getHint(); 10220 if (E->isTypeDependent() || E->isValueDependent() || 10221 E->isInstantiationDependent()) { 10222 DependentHint = true; 10223 } else { 10224 Hint = E->EvaluateKnownConstInt(Context); 10225 HintLoc = C->getBeginLoc(); 10226 } 10227 } 10228 } 10229 if (ErrorFound) 10230 return StmtError(); 10231 const auto Pair = DSAStack->getCriticalWithHint(DirName); 10232 if (Pair.first && DirName.getName() && !DependentHint) { 10233 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 10234 Diag(StartLoc, diag::err_omp_critical_with_hint); 10235 if (HintLoc.isValid()) 10236 Diag(HintLoc, diag::note_omp_critical_hint_here) 10237 << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false); 10238 else 10239 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 10240 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 10241 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 10242 << 1 10243 << toString(C->getHint()->EvaluateKnownConstInt(Context), 10244 /*Radix=*/10, /*Signed=*/false); 10245 } else { 10246 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 10247 } 10248 } 10249 } 10250 10251 setFunctionHasBranchProtectedScope(); 10252 10253 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 10254 Clauses, AStmt); 10255 if (!Pair.first && DirName.getName() && !DependentHint) 10256 DSAStack->addCriticalWithHint(Dir, Hint); 10257 return Dir; 10258 } 10259 10260 StmtResult Sema::ActOnOpenMPParallelForDirective( 10261 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10262 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10263 if (!AStmt) 10264 return StmtError(); 10265 10266 auto *CS = cast<CapturedStmt>(AStmt); 10267 // 1.2.2 OpenMP Language Terminology 10268 // Structured block - An executable statement with a single entry at the 10269 // top and a single exit at the bottom. 10270 // The point of exit cannot be a branch out of the structured block. 10271 // longjmp() and throw() must not violate the entry/exit criteria. 10272 CS->getCapturedDecl()->setNothrow(); 10273 10274 OMPLoopBasedDirective::HelperExprs B; 10275 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10276 // define the nested loops number. 10277 unsigned NestedLoopCount = 10278 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 10279 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10280 VarsWithImplicitDSA, B); 10281 if (NestedLoopCount == 0) 10282 return StmtError(); 10283 10284 assert((CurContext->isDependentContext() || B.builtAll()) && 10285 "omp parallel for loop exprs were not built"); 10286 10287 if (!CurContext->isDependentContext()) { 10288 // Finalize the clauses that need pre-built expressions for CodeGen. 10289 for (OMPClause *C : Clauses) { 10290 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10291 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10292 B.NumIterations, *this, CurScope, 10293 DSAStack)) 10294 return StmtError(); 10295 } 10296 } 10297 10298 setFunctionHasBranchProtectedScope(); 10299 return OMPParallelForDirective::Create( 10300 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10301 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10302 } 10303 10304 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 10305 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10306 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10307 if (!AStmt) 10308 return StmtError(); 10309 10310 auto *CS = cast<CapturedStmt>(AStmt); 10311 // 1.2.2 OpenMP Language Terminology 10312 // Structured block - An executable statement with a single entry at the 10313 // top and a single exit at the bottom. 10314 // The point of exit cannot be a branch out of the structured block. 10315 // longjmp() and throw() must not violate the entry/exit criteria. 10316 CS->getCapturedDecl()->setNothrow(); 10317 10318 OMPLoopBasedDirective::HelperExprs B; 10319 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10320 // define the nested loops number. 10321 unsigned NestedLoopCount = 10322 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 10323 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10324 VarsWithImplicitDSA, B); 10325 if (NestedLoopCount == 0) 10326 return StmtError(); 10327 10328 if (!CurContext->isDependentContext()) { 10329 // Finalize the clauses that need pre-built expressions for CodeGen. 10330 for (OMPClause *C : Clauses) { 10331 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10332 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10333 B.NumIterations, *this, CurScope, 10334 DSAStack)) 10335 return StmtError(); 10336 } 10337 } 10338 10339 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10340 return StmtError(); 10341 10342 setFunctionHasBranchProtectedScope(); 10343 return OMPParallelForSimdDirective::Create( 10344 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10345 } 10346 10347 StmtResult 10348 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 10349 Stmt *AStmt, SourceLocation StartLoc, 10350 SourceLocation EndLoc) { 10351 if (!AStmt) 10352 return StmtError(); 10353 10354 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10355 auto *CS = cast<CapturedStmt>(AStmt); 10356 // 1.2.2 OpenMP Language Terminology 10357 // Structured block - An executable statement with a single entry at the 10358 // top and a single exit at the bottom. 10359 // The point of exit cannot be a branch out of the structured block. 10360 // longjmp() and throw() must not violate the entry/exit criteria. 10361 CS->getCapturedDecl()->setNothrow(); 10362 10363 setFunctionHasBranchProtectedScope(); 10364 10365 return OMPParallelMasterDirective::Create( 10366 Context, StartLoc, EndLoc, Clauses, AStmt, 10367 DSAStack->getTaskgroupReductionRef()); 10368 } 10369 10370 StmtResult 10371 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 10372 Stmt *AStmt, SourceLocation StartLoc, 10373 SourceLocation EndLoc) { 10374 if (!AStmt) 10375 return StmtError(); 10376 10377 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10378 auto BaseStmt = AStmt; 10379 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 10380 BaseStmt = CS->getCapturedStmt(); 10381 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10382 auto S = C->children(); 10383 if (S.begin() == S.end()) 10384 return StmtError(); 10385 // All associated statements must be '#pragma omp section' except for 10386 // the first one. 10387 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 10388 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10389 if (SectionStmt) 10390 Diag(SectionStmt->getBeginLoc(), 10391 diag::err_omp_parallel_sections_substmt_not_section); 10392 return StmtError(); 10393 } 10394 cast<OMPSectionDirective>(SectionStmt) 10395 ->setHasCancel(DSAStack->isCancelRegion()); 10396 } 10397 } else { 10398 Diag(AStmt->getBeginLoc(), 10399 diag::err_omp_parallel_sections_not_compound_stmt); 10400 return StmtError(); 10401 } 10402 10403 setFunctionHasBranchProtectedScope(); 10404 10405 return OMPParallelSectionsDirective::Create( 10406 Context, StartLoc, EndLoc, Clauses, AStmt, 10407 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10408 } 10409 10410 /// Find and diagnose mutually exclusive clause kinds. 10411 static bool checkMutuallyExclusiveClauses( 10412 Sema &S, ArrayRef<OMPClause *> Clauses, 10413 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) { 10414 const OMPClause *PrevClause = nullptr; 10415 bool ErrorFound = false; 10416 for (const OMPClause *C : Clauses) { 10417 if (llvm::is_contained(MutuallyExclusiveClauses, C->getClauseKind())) { 10418 if (!PrevClause) { 10419 PrevClause = C; 10420 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10421 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10422 << getOpenMPClauseName(C->getClauseKind()) 10423 << getOpenMPClauseName(PrevClause->getClauseKind()); 10424 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10425 << getOpenMPClauseName(PrevClause->getClauseKind()); 10426 ErrorFound = true; 10427 } 10428 } 10429 } 10430 return ErrorFound; 10431 } 10432 10433 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 10434 Stmt *AStmt, SourceLocation StartLoc, 10435 SourceLocation EndLoc) { 10436 if (!AStmt) 10437 return StmtError(); 10438 10439 // OpenMP 5.0, 2.10.1 task Construct 10440 // If a detach clause appears on the directive, then a mergeable clause cannot 10441 // appear on the same directive. 10442 if (checkMutuallyExclusiveClauses(*this, Clauses, 10443 {OMPC_detach, OMPC_mergeable})) 10444 return StmtError(); 10445 10446 auto *CS = cast<CapturedStmt>(AStmt); 10447 // 1.2.2 OpenMP Language Terminology 10448 // Structured block - An executable statement with a single entry at the 10449 // top and a single exit at the bottom. 10450 // The point of exit cannot be a branch out of the structured block. 10451 // longjmp() and throw() must not violate the entry/exit criteria. 10452 CS->getCapturedDecl()->setNothrow(); 10453 10454 setFunctionHasBranchProtectedScope(); 10455 10456 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10457 DSAStack->isCancelRegion()); 10458 } 10459 10460 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 10461 SourceLocation EndLoc) { 10462 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 10463 } 10464 10465 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 10466 SourceLocation EndLoc) { 10467 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 10468 } 10469 10470 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 10471 SourceLocation EndLoc) { 10472 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 10473 } 10474 10475 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 10476 Stmt *AStmt, 10477 SourceLocation StartLoc, 10478 SourceLocation EndLoc) { 10479 if (!AStmt) 10480 return StmtError(); 10481 10482 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10483 10484 setFunctionHasBranchProtectedScope(); 10485 10486 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 10487 AStmt, 10488 DSAStack->getTaskgroupReductionRef()); 10489 } 10490 10491 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 10492 SourceLocation StartLoc, 10493 SourceLocation EndLoc) { 10494 OMPFlushClause *FC = nullptr; 10495 OMPClause *OrderClause = nullptr; 10496 for (OMPClause *C : Clauses) { 10497 if (C->getClauseKind() == OMPC_flush) 10498 FC = cast<OMPFlushClause>(C); 10499 else 10500 OrderClause = C; 10501 } 10502 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10503 SourceLocation MemOrderLoc; 10504 for (const OMPClause *C : Clauses) { 10505 if (C->getClauseKind() == OMPC_acq_rel || 10506 C->getClauseKind() == OMPC_acquire || 10507 C->getClauseKind() == OMPC_release) { 10508 if (MemOrderKind != OMPC_unknown) { 10509 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10510 << getOpenMPDirectiveName(OMPD_flush) << 1 10511 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10512 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10513 << getOpenMPClauseName(MemOrderKind); 10514 } else { 10515 MemOrderKind = C->getClauseKind(); 10516 MemOrderLoc = C->getBeginLoc(); 10517 } 10518 } 10519 } 10520 if (FC && OrderClause) { 10521 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 10522 << getOpenMPClauseName(OrderClause->getClauseKind()); 10523 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 10524 << getOpenMPClauseName(OrderClause->getClauseKind()); 10525 return StmtError(); 10526 } 10527 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 10528 } 10529 10530 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 10531 SourceLocation StartLoc, 10532 SourceLocation EndLoc) { 10533 if (Clauses.empty()) { 10534 Diag(StartLoc, diag::err_omp_depobj_expected); 10535 return StmtError(); 10536 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 10537 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 10538 return StmtError(); 10539 } 10540 // Only depobj expression and another single clause is allowed. 10541 if (Clauses.size() > 2) { 10542 Diag(Clauses[2]->getBeginLoc(), 10543 diag::err_omp_depobj_single_clause_expected); 10544 return StmtError(); 10545 } else if (Clauses.size() < 1) { 10546 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 10547 return StmtError(); 10548 } 10549 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 10550 } 10551 10552 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 10553 SourceLocation StartLoc, 10554 SourceLocation EndLoc) { 10555 // Check that exactly one clause is specified. 10556 if (Clauses.size() != 1) { 10557 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 10558 diag::err_omp_scan_single_clause_expected); 10559 return StmtError(); 10560 } 10561 // Check that scan directive is used in the scopeof the OpenMP loop body. 10562 if (Scope *S = DSAStack->getCurScope()) { 10563 Scope *ParentS = S->getParent(); 10564 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 10565 !ParentS->getBreakParent()->isOpenMPLoopScope()) 10566 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 10567 << getOpenMPDirectiveName(OMPD_scan) << 5); 10568 } 10569 // Check that only one instance of scan directives is used in the same outer 10570 // region. 10571 if (DSAStack->doesParentHasScanDirective()) { 10572 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 10573 Diag(DSAStack->getParentScanDirectiveLoc(), 10574 diag::note_omp_previous_directive) 10575 << "scan"; 10576 return StmtError(); 10577 } 10578 DSAStack->setParentHasScanDirective(StartLoc); 10579 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 10580 } 10581 10582 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 10583 Stmt *AStmt, 10584 SourceLocation StartLoc, 10585 SourceLocation EndLoc) { 10586 const OMPClause *DependFound = nullptr; 10587 const OMPClause *DependSourceClause = nullptr; 10588 const OMPClause *DependSinkClause = nullptr; 10589 bool ErrorFound = false; 10590 const OMPThreadsClause *TC = nullptr; 10591 const OMPSIMDClause *SC = nullptr; 10592 for (const OMPClause *C : Clauses) { 10593 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 10594 DependFound = C; 10595 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 10596 if (DependSourceClause) { 10597 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 10598 << getOpenMPDirectiveName(OMPD_ordered) 10599 << getOpenMPClauseName(OMPC_depend) << 2; 10600 ErrorFound = true; 10601 } else { 10602 DependSourceClause = C; 10603 } 10604 if (DependSinkClause) { 10605 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10606 << 0; 10607 ErrorFound = true; 10608 } 10609 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 10610 if (DependSourceClause) { 10611 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10612 << 1; 10613 ErrorFound = true; 10614 } 10615 DependSinkClause = C; 10616 } 10617 } else if (C->getClauseKind() == OMPC_threads) { 10618 TC = cast<OMPThreadsClause>(C); 10619 } else if (C->getClauseKind() == OMPC_simd) { 10620 SC = cast<OMPSIMDClause>(C); 10621 } 10622 } 10623 if (!ErrorFound && !SC && 10624 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 10625 // OpenMP [2.8.1,simd Construct, Restrictions] 10626 // An ordered construct with the simd clause is the only OpenMP construct 10627 // that can appear in the simd region. 10628 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 10629 << (LangOpts.OpenMP >= 50 ? 1 : 0); 10630 ErrorFound = true; 10631 } else if (DependFound && (TC || SC)) { 10632 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 10633 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 10634 ErrorFound = true; 10635 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 10636 Diag(DependFound->getBeginLoc(), 10637 diag::err_omp_ordered_directive_without_param); 10638 ErrorFound = true; 10639 } else if (TC || Clauses.empty()) { 10640 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 10641 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 10642 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 10643 << (TC != nullptr); 10644 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 10645 ErrorFound = true; 10646 } 10647 } 10648 if ((!AStmt && !DependFound) || ErrorFound) 10649 return StmtError(); 10650 10651 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 10652 // During execution of an iteration of a worksharing-loop or a loop nest 10653 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 10654 // must not execute more than one ordered region corresponding to an ordered 10655 // construct without a depend clause. 10656 if (!DependFound) { 10657 if (DSAStack->doesParentHasOrderedDirective()) { 10658 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 10659 Diag(DSAStack->getParentOrderedDirectiveLoc(), 10660 diag::note_omp_previous_directive) 10661 << "ordered"; 10662 return StmtError(); 10663 } 10664 DSAStack->setParentHasOrderedDirective(StartLoc); 10665 } 10666 10667 if (AStmt) { 10668 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10669 10670 setFunctionHasBranchProtectedScope(); 10671 } 10672 10673 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10674 } 10675 10676 namespace { 10677 /// Helper class for checking expression in 'omp atomic [update]' 10678 /// construct. 10679 class OpenMPAtomicUpdateChecker { 10680 /// Error results for atomic update expressions. 10681 enum ExprAnalysisErrorCode { 10682 /// A statement is not an expression statement. 10683 NotAnExpression, 10684 /// Expression is not builtin binary or unary operation. 10685 NotABinaryOrUnaryExpression, 10686 /// Unary operation is not post-/pre- increment/decrement operation. 10687 NotAnUnaryIncDecExpression, 10688 /// An expression is not of scalar type. 10689 NotAScalarType, 10690 /// A binary operation is not an assignment operation. 10691 NotAnAssignmentOp, 10692 /// RHS part of the binary operation is not a binary expression. 10693 NotABinaryExpression, 10694 /// RHS part is not additive/multiplicative/shift/biwise binary 10695 /// expression. 10696 NotABinaryOperator, 10697 /// RHS binary operation does not have reference to the updated LHS 10698 /// part. 10699 NotAnUpdateExpression, 10700 /// No errors is found. 10701 NoError 10702 }; 10703 /// Reference to Sema. 10704 Sema &SemaRef; 10705 /// A location for note diagnostics (when error is found). 10706 SourceLocation NoteLoc; 10707 /// 'x' lvalue part of the source atomic expression. 10708 Expr *X; 10709 /// 'expr' rvalue part of the source atomic expression. 10710 Expr *E; 10711 /// Helper expression of the form 10712 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10713 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10714 Expr *UpdateExpr; 10715 /// Is 'x' a LHS in a RHS part of full update expression. It is 10716 /// important for non-associative operations. 10717 bool IsXLHSInRHSPart; 10718 BinaryOperatorKind Op; 10719 SourceLocation OpLoc; 10720 /// true if the source expression is a postfix unary operation, false 10721 /// if it is a prefix unary operation. 10722 bool IsPostfixUpdate; 10723 10724 public: 10725 OpenMPAtomicUpdateChecker(Sema &SemaRef) 10726 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 10727 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 10728 /// Check specified statement that it is suitable for 'atomic update' 10729 /// constructs and extract 'x', 'expr' and Operation from the original 10730 /// expression. If DiagId and NoteId == 0, then only check is performed 10731 /// without error notification. 10732 /// \param DiagId Diagnostic which should be emitted if error is found. 10733 /// \param NoteId Diagnostic note for the main error message. 10734 /// \return true if statement is not an update expression, false otherwise. 10735 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 10736 /// Return the 'x' lvalue part of the source atomic expression. 10737 Expr *getX() const { return X; } 10738 /// Return the 'expr' rvalue part of the source atomic expression. 10739 Expr *getExpr() const { return E; } 10740 /// Return the update expression used in calculation of the updated 10741 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10742 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10743 Expr *getUpdateExpr() const { return UpdateExpr; } 10744 /// Return true if 'x' is LHS in RHS part of full update expression, 10745 /// false otherwise. 10746 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 10747 10748 /// true if the source expression is a postfix unary operation, false 10749 /// if it is a prefix unary operation. 10750 bool isPostfixUpdate() const { return IsPostfixUpdate; } 10751 10752 private: 10753 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 10754 unsigned NoteId = 0); 10755 }; 10756 } // namespace 10757 10758 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 10759 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 10760 ExprAnalysisErrorCode ErrorFound = NoError; 10761 SourceLocation ErrorLoc, NoteLoc; 10762 SourceRange ErrorRange, NoteRange; 10763 // Allowed constructs are: 10764 // x = x binop expr; 10765 // x = expr binop x; 10766 if (AtomicBinOp->getOpcode() == BO_Assign) { 10767 X = AtomicBinOp->getLHS(); 10768 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 10769 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 10770 if (AtomicInnerBinOp->isMultiplicativeOp() || 10771 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 10772 AtomicInnerBinOp->isBitwiseOp()) { 10773 Op = AtomicInnerBinOp->getOpcode(); 10774 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 10775 Expr *LHS = AtomicInnerBinOp->getLHS(); 10776 Expr *RHS = AtomicInnerBinOp->getRHS(); 10777 llvm::FoldingSetNodeID XId, LHSId, RHSId; 10778 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 10779 /*Canonical=*/true); 10780 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 10781 /*Canonical=*/true); 10782 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 10783 /*Canonical=*/true); 10784 if (XId == LHSId) { 10785 E = RHS; 10786 IsXLHSInRHSPart = true; 10787 } else if (XId == RHSId) { 10788 E = LHS; 10789 IsXLHSInRHSPart = false; 10790 } else { 10791 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10792 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10793 NoteLoc = X->getExprLoc(); 10794 NoteRange = X->getSourceRange(); 10795 ErrorFound = NotAnUpdateExpression; 10796 } 10797 } else { 10798 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10799 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10800 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 10801 NoteRange = SourceRange(NoteLoc, NoteLoc); 10802 ErrorFound = NotABinaryOperator; 10803 } 10804 } else { 10805 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 10806 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 10807 ErrorFound = NotABinaryExpression; 10808 } 10809 } else { 10810 ErrorLoc = AtomicBinOp->getExprLoc(); 10811 ErrorRange = AtomicBinOp->getSourceRange(); 10812 NoteLoc = AtomicBinOp->getOperatorLoc(); 10813 NoteRange = SourceRange(NoteLoc, NoteLoc); 10814 ErrorFound = NotAnAssignmentOp; 10815 } 10816 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10817 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10818 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10819 return true; 10820 } 10821 if (SemaRef.CurContext->isDependentContext()) 10822 E = X = UpdateExpr = nullptr; 10823 return ErrorFound != NoError; 10824 } 10825 10826 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 10827 unsigned NoteId) { 10828 ExprAnalysisErrorCode ErrorFound = NoError; 10829 SourceLocation ErrorLoc, NoteLoc; 10830 SourceRange ErrorRange, NoteRange; 10831 // Allowed constructs are: 10832 // x++; 10833 // x--; 10834 // ++x; 10835 // --x; 10836 // x binop= expr; 10837 // x = x binop expr; 10838 // x = expr binop x; 10839 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 10840 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 10841 if (AtomicBody->getType()->isScalarType() || 10842 AtomicBody->isInstantiationDependent()) { 10843 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 10844 AtomicBody->IgnoreParenImpCasts())) { 10845 // Check for Compound Assignment Operation 10846 Op = BinaryOperator::getOpForCompoundAssignment( 10847 AtomicCompAssignOp->getOpcode()); 10848 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 10849 E = AtomicCompAssignOp->getRHS(); 10850 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 10851 IsXLHSInRHSPart = true; 10852 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 10853 AtomicBody->IgnoreParenImpCasts())) { 10854 // Check for Binary Operation 10855 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 10856 return true; 10857 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 10858 AtomicBody->IgnoreParenImpCasts())) { 10859 // Check for Unary Operation 10860 if (AtomicUnaryOp->isIncrementDecrementOp()) { 10861 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 10862 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 10863 OpLoc = AtomicUnaryOp->getOperatorLoc(); 10864 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 10865 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 10866 IsXLHSInRHSPart = true; 10867 } else { 10868 ErrorFound = NotAnUnaryIncDecExpression; 10869 ErrorLoc = AtomicUnaryOp->getExprLoc(); 10870 ErrorRange = AtomicUnaryOp->getSourceRange(); 10871 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 10872 NoteRange = SourceRange(NoteLoc, NoteLoc); 10873 } 10874 } else if (!AtomicBody->isInstantiationDependent()) { 10875 ErrorFound = NotABinaryOrUnaryExpression; 10876 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 10877 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 10878 } 10879 } else { 10880 ErrorFound = NotAScalarType; 10881 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 10882 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10883 } 10884 } else { 10885 ErrorFound = NotAnExpression; 10886 NoteLoc = ErrorLoc = S->getBeginLoc(); 10887 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10888 } 10889 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10890 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10891 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10892 return true; 10893 } 10894 if (SemaRef.CurContext->isDependentContext()) 10895 E = X = UpdateExpr = nullptr; 10896 if (ErrorFound == NoError && E && X) { 10897 // Build an update expression of form 'OpaqueValueExpr(x) binop 10898 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 10899 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 10900 auto *OVEX = new (SemaRef.getASTContext()) 10901 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue); 10902 auto *OVEExpr = new (SemaRef.getASTContext()) 10903 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue); 10904 ExprResult Update = 10905 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 10906 IsXLHSInRHSPart ? OVEExpr : OVEX); 10907 if (Update.isInvalid()) 10908 return true; 10909 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 10910 Sema::AA_Casting); 10911 if (Update.isInvalid()) 10912 return true; 10913 UpdateExpr = Update.get(); 10914 } 10915 return ErrorFound != NoError; 10916 } 10917 10918 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 10919 Stmt *AStmt, 10920 SourceLocation StartLoc, 10921 SourceLocation EndLoc) { 10922 // Register location of the first atomic directive. 10923 DSAStack->addAtomicDirectiveLoc(StartLoc); 10924 if (!AStmt) 10925 return StmtError(); 10926 10927 // 1.2.2 OpenMP Language Terminology 10928 // Structured block - An executable statement with a single entry at the 10929 // top and a single exit at the bottom. 10930 // The point of exit cannot be a branch out of the structured block. 10931 // longjmp() and throw() must not violate the entry/exit criteria. 10932 OpenMPClauseKind AtomicKind = OMPC_unknown; 10933 SourceLocation AtomicKindLoc; 10934 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10935 SourceLocation MemOrderLoc; 10936 for (const OMPClause *C : Clauses) { 10937 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 10938 C->getClauseKind() == OMPC_update || 10939 C->getClauseKind() == OMPC_capture) { 10940 if (AtomicKind != OMPC_unknown) { 10941 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 10942 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10943 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 10944 << getOpenMPClauseName(AtomicKind); 10945 } else { 10946 AtomicKind = C->getClauseKind(); 10947 AtomicKindLoc = C->getBeginLoc(); 10948 } 10949 } 10950 if (C->getClauseKind() == OMPC_seq_cst || 10951 C->getClauseKind() == OMPC_acq_rel || 10952 C->getClauseKind() == OMPC_acquire || 10953 C->getClauseKind() == OMPC_release || 10954 C->getClauseKind() == OMPC_relaxed) { 10955 if (MemOrderKind != OMPC_unknown) { 10956 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10957 << getOpenMPDirectiveName(OMPD_atomic) << 0 10958 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10959 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10960 << getOpenMPClauseName(MemOrderKind); 10961 } else { 10962 MemOrderKind = C->getClauseKind(); 10963 MemOrderLoc = C->getBeginLoc(); 10964 } 10965 } 10966 } 10967 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 10968 // If atomic-clause is read then memory-order-clause must not be acq_rel or 10969 // release. 10970 // If atomic-clause is write then memory-order-clause must not be acq_rel or 10971 // acquire. 10972 // If atomic-clause is update or not present then memory-order-clause must not 10973 // be acq_rel or acquire. 10974 if ((AtomicKind == OMPC_read && 10975 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 10976 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 10977 AtomicKind == OMPC_unknown) && 10978 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 10979 SourceLocation Loc = AtomicKindLoc; 10980 if (AtomicKind == OMPC_unknown) 10981 Loc = StartLoc; 10982 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 10983 << getOpenMPClauseName(AtomicKind) 10984 << (AtomicKind == OMPC_unknown ? 1 : 0) 10985 << getOpenMPClauseName(MemOrderKind); 10986 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10987 << getOpenMPClauseName(MemOrderKind); 10988 } 10989 10990 Stmt *Body = AStmt; 10991 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 10992 Body = EWC->getSubExpr(); 10993 10994 Expr *X = nullptr; 10995 Expr *V = nullptr; 10996 Expr *E = nullptr; 10997 Expr *UE = nullptr; 10998 bool IsXLHSInRHSPart = false; 10999 bool IsPostfixUpdate = false; 11000 // OpenMP [2.12.6, atomic Construct] 11001 // In the next expressions: 11002 // * x and v (as applicable) are both l-value expressions with scalar type. 11003 // * During the execution of an atomic region, multiple syntactic 11004 // occurrences of x must designate the same storage location. 11005 // * Neither of v and expr (as applicable) may access the storage location 11006 // designated by x. 11007 // * Neither of x and expr (as applicable) may access the storage location 11008 // designated by v. 11009 // * expr is an expression with scalar type. 11010 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 11011 // * binop, binop=, ++, and -- are not overloaded operators. 11012 // * The expression x binop expr must be numerically equivalent to x binop 11013 // (expr). This requirement is satisfied if the operators in expr have 11014 // precedence greater than binop, or by using parentheses around expr or 11015 // subexpressions of expr. 11016 // * The expression expr binop x must be numerically equivalent to (expr) 11017 // binop x. This requirement is satisfied if the operators in expr have 11018 // precedence equal to or greater than binop, or by using parentheses around 11019 // expr or subexpressions of expr. 11020 // * For forms that allow multiple occurrences of x, the number of times 11021 // that x is evaluated is unspecified. 11022 if (AtomicKind == OMPC_read) { 11023 enum { 11024 NotAnExpression, 11025 NotAnAssignmentOp, 11026 NotAScalarType, 11027 NotAnLValue, 11028 NoError 11029 } ErrorFound = NoError; 11030 SourceLocation ErrorLoc, NoteLoc; 11031 SourceRange ErrorRange, NoteRange; 11032 // If clause is read: 11033 // v = x; 11034 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11035 const auto *AtomicBinOp = 11036 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11037 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11038 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 11039 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 11040 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 11041 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 11042 if (!X->isLValue() || !V->isLValue()) { 11043 const Expr *NotLValueExpr = X->isLValue() ? V : X; 11044 ErrorFound = NotAnLValue; 11045 ErrorLoc = AtomicBinOp->getExprLoc(); 11046 ErrorRange = AtomicBinOp->getSourceRange(); 11047 NoteLoc = NotLValueExpr->getExprLoc(); 11048 NoteRange = NotLValueExpr->getSourceRange(); 11049 } 11050 } else if (!X->isInstantiationDependent() || 11051 !V->isInstantiationDependent()) { 11052 const Expr *NotScalarExpr = 11053 (X->isInstantiationDependent() || X->getType()->isScalarType()) 11054 ? V 11055 : X; 11056 ErrorFound = NotAScalarType; 11057 ErrorLoc = AtomicBinOp->getExprLoc(); 11058 ErrorRange = AtomicBinOp->getSourceRange(); 11059 NoteLoc = NotScalarExpr->getExprLoc(); 11060 NoteRange = NotScalarExpr->getSourceRange(); 11061 } 11062 } else if (!AtomicBody->isInstantiationDependent()) { 11063 ErrorFound = NotAnAssignmentOp; 11064 ErrorLoc = AtomicBody->getExprLoc(); 11065 ErrorRange = AtomicBody->getSourceRange(); 11066 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11067 : AtomicBody->getExprLoc(); 11068 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11069 : AtomicBody->getSourceRange(); 11070 } 11071 } else { 11072 ErrorFound = NotAnExpression; 11073 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11074 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 11075 } 11076 if (ErrorFound != NoError) { 11077 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 11078 << ErrorRange; 11079 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 11080 << NoteRange; 11081 return StmtError(); 11082 } 11083 if (CurContext->isDependentContext()) 11084 V = X = nullptr; 11085 } else if (AtomicKind == OMPC_write) { 11086 enum { 11087 NotAnExpression, 11088 NotAnAssignmentOp, 11089 NotAScalarType, 11090 NotAnLValue, 11091 NoError 11092 } ErrorFound = NoError; 11093 SourceLocation ErrorLoc, NoteLoc; 11094 SourceRange ErrorRange, NoteRange; 11095 // If clause is write: 11096 // x = expr; 11097 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11098 const auto *AtomicBinOp = 11099 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11100 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11101 X = AtomicBinOp->getLHS(); 11102 E = AtomicBinOp->getRHS(); 11103 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 11104 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 11105 if (!X->isLValue()) { 11106 ErrorFound = NotAnLValue; 11107 ErrorLoc = AtomicBinOp->getExprLoc(); 11108 ErrorRange = AtomicBinOp->getSourceRange(); 11109 NoteLoc = X->getExprLoc(); 11110 NoteRange = X->getSourceRange(); 11111 } 11112 } else if (!X->isInstantiationDependent() || 11113 !E->isInstantiationDependent()) { 11114 const Expr *NotScalarExpr = 11115 (X->isInstantiationDependent() || X->getType()->isScalarType()) 11116 ? E 11117 : X; 11118 ErrorFound = NotAScalarType; 11119 ErrorLoc = AtomicBinOp->getExprLoc(); 11120 ErrorRange = AtomicBinOp->getSourceRange(); 11121 NoteLoc = NotScalarExpr->getExprLoc(); 11122 NoteRange = NotScalarExpr->getSourceRange(); 11123 } 11124 } else if (!AtomicBody->isInstantiationDependent()) { 11125 ErrorFound = NotAnAssignmentOp; 11126 ErrorLoc = AtomicBody->getExprLoc(); 11127 ErrorRange = AtomicBody->getSourceRange(); 11128 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11129 : AtomicBody->getExprLoc(); 11130 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11131 : AtomicBody->getSourceRange(); 11132 } 11133 } else { 11134 ErrorFound = NotAnExpression; 11135 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11136 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 11137 } 11138 if (ErrorFound != NoError) { 11139 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 11140 << ErrorRange; 11141 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 11142 << NoteRange; 11143 return StmtError(); 11144 } 11145 if (CurContext->isDependentContext()) 11146 E = X = nullptr; 11147 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 11148 // If clause is update: 11149 // x++; 11150 // x--; 11151 // ++x; 11152 // --x; 11153 // x binop= expr; 11154 // x = x binop expr; 11155 // x = expr binop x; 11156 OpenMPAtomicUpdateChecker Checker(*this); 11157 if (Checker.checkStatement( 11158 Body, (AtomicKind == OMPC_update) 11159 ? diag::err_omp_atomic_update_not_expression_statement 11160 : diag::err_omp_atomic_not_expression_statement, 11161 diag::note_omp_atomic_update)) 11162 return StmtError(); 11163 if (!CurContext->isDependentContext()) { 11164 E = Checker.getExpr(); 11165 X = Checker.getX(); 11166 UE = Checker.getUpdateExpr(); 11167 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11168 } 11169 } else if (AtomicKind == OMPC_capture) { 11170 enum { 11171 NotAnAssignmentOp, 11172 NotACompoundStatement, 11173 NotTwoSubstatements, 11174 NotASpecificExpression, 11175 NoError 11176 } ErrorFound = NoError; 11177 SourceLocation ErrorLoc, NoteLoc; 11178 SourceRange ErrorRange, NoteRange; 11179 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 11180 // If clause is a capture: 11181 // v = x++; 11182 // v = x--; 11183 // v = ++x; 11184 // v = --x; 11185 // v = x binop= expr; 11186 // v = x = x binop expr; 11187 // v = x = expr binop x; 11188 const auto *AtomicBinOp = 11189 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11190 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11191 V = AtomicBinOp->getLHS(); 11192 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 11193 OpenMPAtomicUpdateChecker Checker(*this); 11194 if (Checker.checkStatement( 11195 Body, diag::err_omp_atomic_capture_not_expression_statement, 11196 diag::note_omp_atomic_update)) 11197 return StmtError(); 11198 E = Checker.getExpr(); 11199 X = Checker.getX(); 11200 UE = Checker.getUpdateExpr(); 11201 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11202 IsPostfixUpdate = Checker.isPostfixUpdate(); 11203 } else if (!AtomicBody->isInstantiationDependent()) { 11204 ErrorLoc = AtomicBody->getExprLoc(); 11205 ErrorRange = AtomicBody->getSourceRange(); 11206 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11207 : AtomicBody->getExprLoc(); 11208 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11209 : AtomicBody->getSourceRange(); 11210 ErrorFound = NotAnAssignmentOp; 11211 } 11212 if (ErrorFound != NoError) { 11213 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 11214 << ErrorRange; 11215 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11216 return StmtError(); 11217 } 11218 if (CurContext->isDependentContext()) 11219 UE = V = E = X = nullptr; 11220 } else { 11221 // If clause is a capture: 11222 // { v = x; x = expr; } 11223 // { v = x; x++; } 11224 // { v = x; x--; } 11225 // { v = x; ++x; } 11226 // { v = x; --x; } 11227 // { v = x; x binop= expr; } 11228 // { v = x; x = x binop expr; } 11229 // { v = x; x = expr binop x; } 11230 // { x++; v = x; } 11231 // { x--; v = x; } 11232 // { ++x; v = x; } 11233 // { --x; v = x; } 11234 // { x binop= expr; v = x; } 11235 // { x = x binop expr; v = x; } 11236 // { x = expr binop x; v = x; } 11237 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 11238 // Check that this is { expr1; expr2; } 11239 if (CS->size() == 2) { 11240 Stmt *First = CS->body_front(); 11241 Stmt *Second = CS->body_back(); 11242 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 11243 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 11244 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 11245 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 11246 // Need to find what subexpression is 'v' and what is 'x'. 11247 OpenMPAtomicUpdateChecker Checker(*this); 11248 bool IsUpdateExprFound = !Checker.checkStatement(Second); 11249 BinaryOperator *BinOp = nullptr; 11250 if (IsUpdateExprFound) { 11251 BinOp = dyn_cast<BinaryOperator>(First); 11252 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11253 } 11254 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11255 // { v = x; x++; } 11256 // { v = x; x--; } 11257 // { v = x; ++x; } 11258 // { v = x; --x; } 11259 // { v = x; x binop= expr; } 11260 // { v = x; x = x binop expr; } 11261 // { v = x; x = expr binop x; } 11262 // Check that the first expression has form v = x. 11263 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11264 llvm::FoldingSetNodeID XId, PossibleXId; 11265 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11266 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11267 IsUpdateExprFound = XId == PossibleXId; 11268 if (IsUpdateExprFound) { 11269 V = BinOp->getLHS(); 11270 X = Checker.getX(); 11271 E = Checker.getExpr(); 11272 UE = Checker.getUpdateExpr(); 11273 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11274 IsPostfixUpdate = true; 11275 } 11276 } 11277 if (!IsUpdateExprFound) { 11278 IsUpdateExprFound = !Checker.checkStatement(First); 11279 BinOp = nullptr; 11280 if (IsUpdateExprFound) { 11281 BinOp = dyn_cast<BinaryOperator>(Second); 11282 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11283 } 11284 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11285 // { x++; v = x; } 11286 // { x--; v = x; } 11287 // { ++x; v = x; } 11288 // { --x; v = x; } 11289 // { x binop= expr; v = x; } 11290 // { x = x binop expr; v = x; } 11291 // { x = expr binop x; v = x; } 11292 // Check that the second expression has form v = x. 11293 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11294 llvm::FoldingSetNodeID XId, PossibleXId; 11295 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11296 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11297 IsUpdateExprFound = XId == PossibleXId; 11298 if (IsUpdateExprFound) { 11299 V = BinOp->getLHS(); 11300 X = Checker.getX(); 11301 E = Checker.getExpr(); 11302 UE = Checker.getUpdateExpr(); 11303 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11304 IsPostfixUpdate = false; 11305 } 11306 } 11307 } 11308 if (!IsUpdateExprFound) { 11309 // { v = x; x = expr; } 11310 auto *FirstExpr = dyn_cast<Expr>(First); 11311 auto *SecondExpr = dyn_cast<Expr>(Second); 11312 if (!FirstExpr || !SecondExpr || 11313 !(FirstExpr->isInstantiationDependent() || 11314 SecondExpr->isInstantiationDependent())) { 11315 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 11316 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 11317 ErrorFound = NotAnAssignmentOp; 11318 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 11319 : First->getBeginLoc(); 11320 NoteRange = ErrorRange = FirstBinOp 11321 ? FirstBinOp->getSourceRange() 11322 : SourceRange(ErrorLoc, ErrorLoc); 11323 } else { 11324 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 11325 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 11326 ErrorFound = NotAnAssignmentOp; 11327 NoteLoc = ErrorLoc = SecondBinOp 11328 ? SecondBinOp->getOperatorLoc() 11329 : Second->getBeginLoc(); 11330 NoteRange = ErrorRange = 11331 SecondBinOp ? SecondBinOp->getSourceRange() 11332 : SourceRange(ErrorLoc, ErrorLoc); 11333 } else { 11334 Expr *PossibleXRHSInFirst = 11335 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 11336 Expr *PossibleXLHSInSecond = 11337 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 11338 llvm::FoldingSetNodeID X1Id, X2Id; 11339 PossibleXRHSInFirst->Profile(X1Id, Context, 11340 /*Canonical=*/true); 11341 PossibleXLHSInSecond->Profile(X2Id, Context, 11342 /*Canonical=*/true); 11343 IsUpdateExprFound = X1Id == X2Id; 11344 if (IsUpdateExprFound) { 11345 V = FirstBinOp->getLHS(); 11346 X = SecondBinOp->getLHS(); 11347 E = SecondBinOp->getRHS(); 11348 UE = nullptr; 11349 IsXLHSInRHSPart = false; 11350 IsPostfixUpdate = true; 11351 } else { 11352 ErrorFound = NotASpecificExpression; 11353 ErrorLoc = FirstBinOp->getExprLoc(); 11354 ErrorRange = FirstBinOp->getSourceRange(); 11355 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 11356 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 11357 } 11358 } 11359 } 11360 } 11361 } 11362 } else { 11363 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11364 NoteRange = ErrorRange = 11365 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11366 ErrorFound = NotTwoSubstatements; 11367 } 11368 } else { 11369 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11370 NoteRange = ErrorRange = 11371 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11372 ErrorFound = NotACompoundStatement; 11373 } 11374 if (ErrorFound != NoError) { 11375 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 11376 << ErrorRange; 11377 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11378 return StmtError(); 11379 } 11380 if (CurContext->isDependentContext()) 11381 UE = V = E = X = nullptr; 11382 } 11383 } 11384 11385 setFunctionHasBranchProtectedScope(); 11386 11387 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 11388 X, V, E, UE, IsXLHSInRHSPart, 11389 IsPostfixUpdate); 11390 } 11391 11392 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 11393 Stmt *AStmt, 11394 SourceLocation StartLoc, 11395 SourceLocation EndLoc) { 11396 if (!AStmt) 11397 return StmtError(); 11398 11399 auto *CS = cast<CapturedStmt>(AStmt); 11400 // 1.2.2 OpenMP Language Terminology 11401 // Structured block - An executable statement with a single entry at the 11402 // top and a single exit at the bottom. 11403 // The point of exit cannot be a branch out of the structured block. 11404 // longjmp() and throw() must not violate the entry/exit criteria. 11405 CS->getCapturedDecl()->setNothrow(); 11406 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 11407 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11408 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11409 // 1.2.2 OpenMP Language Terminology 11410 // Structured block - An executable statement with a single entry at the 11411 // top and a single exit at the bottom. 11412 // The point of exit cannot be a branch out of the structured block. 11413 // longjmp() and throw() must not violate the entry/exit criteria. 11414 CS->getCapturedDecl()->setNothrow(); 11415 } 11416 11417 // OpenMP [2.16, Nesting of Regions] 11418 // If specified, a teams construct must be contained within a target 11419 // construct. That target construct must contain no statements or directives 11420 // outside of the teams construct. 11421 if (DSAStack->hasInnerTeamsRegion()) { 11422 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 11423 bool OMPTeamsFound = true; 11424 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 11425 auto I = CS->body_begin(); 11426 while (I != CS->body_end()) { 11427 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 11428 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 11429 OMPTeamsFound) { 11430 11431 OMPTeamsFound = false; 11432 break; 11433 } 11434 ++I; 11435 } 11436 assert(I != CS->body_end() && "Not found statement"); 11437 S = *I; 11438 } else { 11439 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 11440 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 11441 } 11442 if (!OMPTeamsFound) { 11443 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 11444 Diag(DSAStack->getInnerTeamsRegionLoc(), 11445 diag::note_omp_nested_teams_construct_here); 11446 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 11447 << isa<OMPExecutableDirective>(S); 11448 return StmtError(); 11449 } 11450 } 11451 11452 setFunctionHasBranchProtectedScope(); 11453 11454 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11455 } 11456 11457 StmtResult 11458 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 11459 Stmt *AStmt, SourceLocation StartLoc, 11460 SourceLocation EndLoc) { 11461 if (!AStmt) 11462 return StmtError(); 11463 11464 auto *CS = cast<CapturedStmt>(AStmt); 11465 // 1.2.2 OpenMP Language Terminology 11466 // Structured block - An executable statement with a single entry at the 11467 // top and a single exit at the bottom. 11468 // The point of exit cannot be a branch out of the structured block. 11469 // longjmp() and throw() must not violate the entry/exit criteria. 11470 CS->getCapturedDecl()->setNothrow(); 11471 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 11472 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11473 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11474 // 1.2.2 OpenMP Language Terminology 11475 // Structured block - An executable statement with a single entry at the 11476 // top and a single exit at the bottom. 11477 // The point of exit cannot be a branch out of the structured block. 11478 // longjmp() and throw() must not violate the entry/exit criteria. 11479 CS->getCapturedDecl()->setNothrow(); 11480 } 11481 11482 setFunctionHasBranchProtectedScope(); 11483 11484 return OMPTargetParallelDirective::Create( 11485 Context, StartLoc, EndLoc, Clauses, AStmt, 11486 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11487 } 11488 11489 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 11490 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11491 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11492 if (!AStmt) 11493 return StmtError(); 11494 11495 auto *CS = cast<CapturedStmt>(AStmt); 11496 // 1.2.2 OpenMP Language Terminology 11497 // Structured block - An executable statement with a single entry at the 11498 // top and a single exit at the bottom. 11499 // The point of exit cannot be a branch out of the structured block. 11500 // longjmp() and throw() must not violate the entry/exit criteria. 11501 CS->getCapturedDecl()->setNothrow(); 11502 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11503 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11504 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11505 // 1.2.2 OpenMP Language Terminology 11506 // Structured block - An executable statement with a single entry at the 11507 // top and a single exit at the bottom. 11508 // The point of exit cannot be a branch out of the structured block. 11509 // longjmp() and throw() must not violate the entry/exit criteria. 11510 CS->getCapturedDecl()->setNothrow(); 11511 } 11512 11513 OMPLoopBasedDirective::HelperExprs B; 11514 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11515 // define the nested loops number. 11516 unsigned NestedLoopCount = 11517 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 11518 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11519 VarsWithImplicitDSA, B); 11520 if (NestedLoopCount == 0) 11521 return StmtError(); 11522 11523 assert((CurContext->isDependentContext() || B.builtAll()) && 11524 "omp target parallel for loop exprs were not built"); 11525 11526 if (!CurContext->isDependentContext()) { 11527 // Finalize the clauses that need pre-built expressions for CodeGen. 11528 for (OMPClause *C : Clauses) { 11529 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11530 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11531 B.NumIterations, *this, CurScope, 11532 DSAStack)) 11533 return StmtError(); 11534 } 11535 } 11536 11537 setFunctionHasBranchProtectedScope(); 11538 return OMPTargetParallelForDirective::Create( 11539 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11540 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11541 } 11542 11543 /// Check for existence of a map clause in the list of clauses. 11544 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 11545 const OpenMPClauseKind K) { 11546 return llvm::any_of( 11547 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 11548 } 11549 11550 template <typename... Params> 11551 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 11552 const Params... ClauseTypes) { 11553 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 11554 } 11555 11556 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 11557 Stmt *AStmt, 11558 SourceLocation StartLoc, 11559 SourceLocation EndLoc) { 11560 if (!AStmt) 11561 return StmtError(); 11562 11563 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11564 11565 // OpenMP [2.12.2, target data Construct, Restrictions] 11566 // At least one map, use_device_addr or use_device_ptr clause must appear on 11567 // the directive. 11568 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 11569 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 11570 StringRef Expected; 11571 if (LangOpts.OpenMP < 50) 11572 Expected = "'map' or 'use_device_ptr'"; 11573 else 11574 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 11575 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11576 << Expected << getOpenMPDirectiveName(OMPD_target_data); 11577 return StmtError(); 11578 } 11579 11580 setFunctionHasBranchProtectedScope(); 11581 11582 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11583 AStmt); 11584 } 11585 11586 StmtResult 11587 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 11588 SourceLocation StartLoc, 11589 SourceLocation EndLoc, Stmt *AStmt) { 11590 if (!AStmt) 11591 return StmtError(); 11592 11593 auto *CS = cast<CapturedStmt>(AStmt); 11594 // 1.2.2 OpenMP Language Terminology 11595 // Structured block - An executable statement with a single entry at the 11596 // top and a single exit at the bottom. 11597 // The point of exit cannot be a branch out of the structured block. 11598 // longjmp() and throw() must not violate the entry/exit criteria. 11599 CS->getCapturedDecl()->setNothrow(); 11600 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 11601 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11602 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11603 // 1.2.2 OpenMP Language Terminology 11604 // Structured block - An executable statement with a single entry at the 11605 // top and a single exit at the bottom. 11606 // The point of exit cannot be a branch out of the structured block. 11607 // longjmp() and throw() must not violate the entry/exit criteria. 11608 CS->getCapturedDecl()->setNothrow(); 11609 } 11610 11611 // OpenMP [2.10.2, Restrictions, p. 99] 11612 // At least one map clause must appear on the directive. 11613 if (!hasClauses(Clauses, OMPC_map)) { 11614 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11615 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 11616 return StmtError(); 11617 } 11618 11619 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11620 AStmt); 11621 } 11622 11623 StmtResult 11624 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 11625 SourceLocation StartLoc, 11626 SourceLocation EndLoc, Stmt *AStmt) { 11627 if (!AStmt) 11628 return StmtError(); 11629 11630 auto *CS = cast<CapturedStmt>(AStmt); 11631 // 1.2.2 OpenMP Language Terminology 11632 // Structured block - An executable statement with a single entry at the 11633 // top and a single exit at the bottom. 11634 // The point of exit cannot be a branch out of the structured block. 11635 // longjmp() and throw() must not violate the entry/exit criteria. 11636 CS->getCapturedDecl()->setNothrow(); 11637 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 11638 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11639 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11640 // 1.2.2 OpenMP Language Terminology 11641 // Structured block - An executable statement with a single entry at the 11642 // top and a single exit at the bottom. 11643 // The point of exit cannot be a branch out of the structured block. 11644 // longjmp() and throw() must not violate the entry/exit criteria. 11645 CS->getCapturedDecl()->setNothrow(); 11646 } 11647 11648 // OpenMP [2.10.3, Restrictions, p. 102] 11649 // At least one map clause must appear on the directive. 11650 if (!hasClauses(Clauses, OMPC_map)) { 11651 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11652 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 11653 return StmtError(); 11654 } 11655 11656 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11657 AStmt); 11658 } 11659 11660 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 11661 SourceLocation StartLoc, 11662 SourceLocation EndLoc, 11663 Stmt *AStmt) { 11664 if (!AStmt) 11665 return StmtError(); 11666 11667 auto *CS = cast<CapturedStmt>(AStmt); 11668 // 1.2.2 OpenMP Language Terminology 11669 // Structured block - An executable statement with a single entry at the 11670 // top and a single exit at the bottom. 11671 // The point of exit cannot be a branch out of the structured block. 11672 // longjmp() and throw() must not violate the entry/exit criteria. 11673 CS->getCapturedDecl()->setNothrow(); 11674 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 11675 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11676 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11677 // 1.2.2 OpenMP Language Terminology 11678 // Structured block - An executable statement with a single entry at the 11679 // top and a single exit at the bottom. 11680 // The point of exit cannot be a branch out of the structured block. 11681 // longjmp() and throw() must not violate the entry/exit criteria. 11682 CS->getCapturedDecl()->setNothrow(); 11683 } 11684 11685 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 11686 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 11687 return StmtError(); 11688 } 11689 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 11690 AStmt); 11691 } 11692 11693 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 11694 Stmt *AStmt, SourceLocation StartLoc, 11695 SourceLocation EndLoc) { 11696 if (!AStmt) 11697 return StmtError(); 11698 11699 auto *CS = cast<CapturedStmt>(AStmt); 11700 // 1.2.2 OpenMP Language Terminology 11701 // Structured block - An executable statement with a single entry at the 11702 // top and a single exit at the bottom. 11703 // The point of exit cannot be a branch out of the structured block. 11704 // longjmp() and throw() must not violate the entry/exit criteria. 11705 CS->getCapturedDecl()->setNothrow(); 11706 11707 setFunctionHasBranchProtectedScope(); 11708 11709 DSAStack->setParentTeamsRegionLoc(StartLoc); 11710 11711 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11712 } 11713 11714 StmtResult 11715 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 11716 SourceLocation EndLoc, 11717 OpenMPDirectiveKind CancelRegion) { 11718 if (DSAStack->isParentNowaitRegion()) { 11719 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 11720 return StmtError(); 11721 } 11722 if (DSAStack->isParentOrderedRegion()) { 11723 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 11724 return StmtError(); 11725 } 11726 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 11727 CancelRegion); 11728 } 11729 11730 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 11731 SourceLocation StartLoc, 11732 SourceLocation EndLoc, 11733 OpenMPDirectiveKind CancelRegion) { 11734 if (DSAStack->isParentNowaitRegion()) { 11735 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 11736 return StmtError(); 11737 } 11738 if (DSAStack->isParentOrderedRegion()) { 11739 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 11740 return StmtError(); 11741 } 11742 DSAStack->setParentCancelRegion(/*Cancel=*/true); 11743 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 11744 CancelRegion); 11745 } 11746 11747 static bool checkReductionClauseWithNogroup(Sema &S, 11748 ArrayRef<OMPClause *> Clauses) { 11749 const OMPClause *ReductionClause = nullptr; 11750 const OMPClause *NogroupClause = nullptr; 11751 for (const OMPClause *C : Clauses) { 11752 if (C->getClauseKind() == OMPC_reduction) { 11753 ReductionClause = C; 11754 if (NogroupClause) 11755 break; 11756 continue; 11757 } 11758 if (C->getClauseKind() == OMPC_nogroup) { 11759 NogroupClause = C; 11760 if (ReductionClause) 11761 break; 11762 continue; 11763 } 11764 } 11765 if (ReductionClause && NogroupClause) { 11766 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 11767 << SourceRange(NogroupClause->getBeginLoc(), 11768 NogroupClause->getEndLoc()); 11769 return true; 11770 } 11771 return false; 11772 } 11773 11774 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 11775 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11776 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11777 if (!AStmt) 11778 return StmtError(); 11779 11780 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11781 OMPLoopBasedDirective::HelperExprs B; 11782 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11783 // define the nested loops number. 11784 unsigned NestedLoopCount = 11785 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 11786 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11787 VarsWithImplicitDSA, B); 11788 if (NestedLoopCount == 0) 11789 return StmtError(); 11790 11791 assert((CurContext->isDependentContext() || B.builtAll()) && 11792 "omp for loop exprs were not built"); 11793 11794 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11795 // The grainsize clause and num_tasks clause are mutually exclusive and may 11796 // not appear on the same taskloop directive. 11797 if (checkMutuallyExclusiveClauses(*this, Clauses, 11798 {OMPC_grainsize, OMPC_num_tasks})) 11799 return StmtError(); 11800 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11801 // If a reduction clause is present on the taskloop directive, the nogroup 11802 // clause must not be specified. 11803 if (checkReductionClauseWithNogroup(*this, Clauses)) 11804 return StmtError(); 11805 11806 setFunctionHasBranchProtectedScope(); 11807 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11808 NestedLoopCount, Clauses, AStmt, B, 11809 DSAStack->isCancelRegion()); 11810 } 11811 11812 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 11813 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11814 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11815 if (!AStmt) 11816 return StmtError(); 11817 11818 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11819 OMPLoopBasedDirective::HelperExprs B; 11820 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11821 // define the nested loops number. 11822 unsigned NestedLoopCount = 11823 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 11824 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11825 VarsWithImplicitDSA, B); 11826 if (NestedLoopCount == 0) 11827 return StmtError(); 11828 11829 assert((CurContext->isDependentContext() || B.builtAll()) && 11830 "omp for loop exprs were not built"); 11831 11832 if (!CurContext->isDependentContext()) { 11833 // Finalize the clauses that need pre-built expressions for CodeGen. 11834 for (OMPClause *C : Clauses) { 11835 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11836 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11837 B.NumIterations, *this, CurScope, 11838 DSAStack)) 11839 return StmtError(); 11840 } 11841 } 11842 11843 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11844 // The grainsize clause and num_tasks clause are mutually exclusive and may 11845 // not appear on the same taskloop directive. 11846 if (checkMutuallyExclusiveClauses(*this, Clauses, 11847 {OMPC_grainsize, OMPC_num_tasks})) 11848 return StmtError(); 11849 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11850 // If a reduction clause is present on the taskloop directive, the nogroup 11851 // clause must not be specified. 11852 if (checkReductionClauseWithNogroup(*this, Clauses)) 11853 return StmtError(); 11854 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11855 return StmtError(); 11856 11857 setFunctionHasBranchProtectedScope(); 11858 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 11859 NestedLoopCount, Clauses, AStmt, B); 11860 } 11861 11862 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 11863 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11864 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11865 if (!AStmt) 11866 return StmtError(); 11867 11868 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11869 OMPLoopBasedDirective::HelperExprs B; 11870 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11871 // define the nested loops number. 11872 unsigned NestedLoopCount = 11873 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 11874 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11875 VarsWithImplicitDSA, B); 11876 if (NestedLoopCount == 0) 11877 return StmtError(); 11878 11879 assert((CurContext->isDependentContext() || B.builtAll()) && 11880 "omp for loop exprs were not built"); 11881 11882 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11883 // The grainsize clause and num_tasks clause are mutually exclusive and may 11884 // not appear on the same taskloop directive. 11885 if (checkMutuallyExclusiveClauses(*this, Clauses, 11886 {OMPC_grainsize, OMPC_num_tasks})) 11887 return StmtError(); 11888 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11889 // If a reduction clause is present on the taskloop directive, the nogroup 11890 // clause must not be specified. 11891 if (checkReductionClauseWithNogroup(*this, Clauses)) 11892 return StmtError(); 11893 11894 setFunctionHasBranchProtectedScope(); 11895 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11896 NestedLoopCount, Clauses, AStmt, B, 11897 DSAStack->isCancelRegion()); 11898 } 11899 11900 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 11901 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11902 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11903 if (!AStmt) 11904 return StmtError(); 11905 11906 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11907 OMPLoopBasedDirective::HelperExprs B; 11908 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11909 // define the nested loops number. 11910 unsigned NestedLoopCount = 11911 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11912 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11913 VarsWithImplicitDSA, B); 11914 if (NestedLoopCount == 0) 11915 return StmtError(); 11916 11917 assert((CurContext->isDependentContext() || B.builtAll()) && 11918 "omp for loop exprs were not built"); 11919 11920 if (!CurContext->isDependentContext()) { 11921 // Finalize the clauses that need pre-built expressions for CodeGen. 11922 for (OMPClause *C : Clauses) { 11923 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11924 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11925 B.NumIterations, *this, CurScope, 11926 DSAStack)) 11927 return StmtError(); 11928 } 11929 } 11930 11931 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11932 // The grainsize clause and num_tasks clause are mutually exclusive and may 11933 // not appear on the same taskloop directive. 11934 if (checkMutuallyExclusiveClauses(*this, Clauses, 11935 {OMPC_grainsize, OMPC_num_tasks})) 11936 return StmtError(); 11937 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11938 // If a reduction clause is present on the taskloop directive, the nogroup 11939 // clause must not be specified. 11940 if (checkReductionClauseWithNogroup(*this, Clauses)) 11941 return StmtError(); 11942 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11943 return StmtError(); 11944 11945 setFunctionHasBranchProtectedScope(); 11946 return OMPMasterTaskLoopSimdDirective::Create( 11947 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11948 } 11949 11950 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 11951 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11952 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11953 if (!AStmt) 11954 return StmtError(); 11955 11956 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11957 auto *CS = cast<CapturedStmt>(AStmt); 11958 // 1.2.2 OpenMP Language Terminology 11959 // Structured block - An executable statement with a single entry at the 11960 // top and a single exit at the bottom. 11961 // The point of exit cannot be a branch out of the structured block. 11962 // longjmp() and throw() must not violate the entry/exit criteria. 11963 CS->getCapturedDecl()->setNothrow(); 11964 for (int ThisCaptureLevel = 11965 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 11966 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11967 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11968 // 1.2.2 OpenMP Language Terminology 11969 // Structured block - An executable statement with a single entry at the 11970 // top and a single exit at the bottom. 11971 // The point of exit cannot be a branch out of the structured block. 11972 // longjmp() and throw() must not violate the entry/exit criteria. 11973 CS->getCapturedDecl()->setNothrow(); 11974 } 11975 11976 OMPLoopBasedDirective::HelperExprs B; 11977 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11978 // define the nested loops number. 11979 unsigned NestedLoopCount = checkOpenMPLoop( 11980 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 11981 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11982 VarsWithImplicitDSA, B); 11983 if (NestedLoopCount == 0) 11984 return StmtError(); 11985 11986 assert((CurContext->isDependentContext() || B.builtAll()) && 11987 "omp for loop exprs were not built"); 11988 11989 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11990 // The grainsize clause and num_tasks clause are mutually exclusive and may 11991 // not appear on the same taskloop directive. 11992 if (checkMutuallyExclusiveClauses(*this, Clauses, 11993 {OMPC_grainsize, OMPC_num_tasks})) 11994 return StmtError(); 11995 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11996 // If a reduction clause is present on the taskloop directive, the nogroup 11997 // clause must not be specified. 11998 if (checkReductionClauseWithNogroup(*this, Clauses)) 11999 return StmtError(); 12000 12001 setFunctionHasBranchProtectedScope(); 12002 return OMPParallelMasterTaskLoopDirective::Create( 12003 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12004 DSAStack->isCancelRegion()); 12005 } 12006 12007 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 12008 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12009 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12010 if (!AStmt) 12011 return StmtError(); 12012 12013 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 12014 auto *CS = cast<CapturedStmt>(AStmt); 12015 // 1.2.2 OpenMP Language Terminology 12016 // Structured block - An executable statement with a single entry at the 12017 // top and a single exit at the bottom. 12018 // The point of exit cannot be a branch out of the structured block. 12019 // longjmp() and throw() must not violate the entry/exit criteria. 12020 CS->getCapturedDecl()->setNothrow(); 12021 for (int ThisCaptureLevel = 12022 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 12023 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12024 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12025 // 1.2.2 OpenMP Language Terminology 12026 // Structured block - An executable statement with a single entry at the 12027 // top and a single exit at the bottom. 12028 // The point of exit cannot be a branch out of the structured block. 12029 // longjmp() and throw() must not violate the entry/exit criteria. 12030 CS->getCapturedDecl()->setNothrow(); 12031 } 12032 12033 OMPLoopBasedDirective::HelperExprs B; 12034 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12035 // define the nested loops number. 12036 unsigned NestedLoopCount = checkOpenMPLoop( 12037 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 12038 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 12039 VarsWithImplicitDSA, B); 12040 if (NestedLoopCount == 0) 12041 return StmtError(); 12042 12043 assert((CurContext->isDependentContext() || B.builtAll()) && 12044 "omp for loop exprs were not built"); 12045 12046 if (!CurContext->isDependentContext()) { 12047 // Finalize the clauses that need pre-built expressions for CodeGen. 12048 for (OMPClause *C : Clauses) { 12049 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12050 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12051 B.NumIterations, *this, CurScope, 12052 DSAStack)) 12053 return StmtError(); 12054 } 12055 } 12056 12057 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 12058 // The grainsize clause and num_tasks clause are mutually exclusive and may 12059 // not appear on the same taskloop directive. 12060 if (checkMutuallyExclusiveClauses(*this, Clauses, 12061 {OMPC_grainsize, OMPC_num_tasks})) 12062 return StmtError(); 12063 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 12064 // If a reduction clause is present on the taskloop directive, the nogroup 12065 // clause must not be specified. 12066 if (checkReductionClauseWithNogroup(*this, Clauses)) 12067 return StmtError(); 12068 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12069 return StmtError(); 12070 12071 setFunctionHasBranchProtectedScope(); 12072 return OMPParallelMasterTaskLoopSimdDirective::Create( 12073 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12074 } 12075 12076 StmtResult Sema::ActOnOpenMPDistributeDirective( 12077 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12078 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12079 if (!AStmt) 12080 return StmtError(); 12081 12082 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 12083 OMPLoopBasedDirective::HelperExprs B; 12084 // In presence of clause 'collapse' with number of loops, it will 12085 // define the nested loops number. 12086 unsigned NestedLoopCount = 12087 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 12088 nullptr /*ordered not a clause on distribute*/, AStmt, 12089 *this, *DSAStack, VarsWithImplicitDSA, B); 12090 if (NestedLoopCount == 0) 12091 return StmtError(); 12092 12093 assert((CurContext->isDependentContext() || B.builtAll()) && 12094 "omp for loop exprs were not built"); 12095 12096 setFunctionHasBranchProtectedScope(); 12097 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 12098 NestedLoopCount, Clauses, AStmt, B); 12099 } 12100 12101 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 12102 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12103 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12104 if (!AStmt) 12105 return StmtError(); 12106 12107 auto *CS = cast<CapturedStmt>(AStmt); 12108 // 1.2.2 OpenMP Language Terminology 12109 // Structured block - An executable statement with a single entry at the 12110 // top and a single exit at the bottom. 12111 // The point of exit cannot be a branch out of the structured block. 12112 // longjmp() and throw() must not violate the entry/exit criteria. 12113 CS->getCapturedDecl()->setNothrow(); 12114 for (int ThisCaptureLevel = 12115 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 12116 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12117 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12118 // 1.2.2 OpenMP Language Terminology 12119 // Structured block - An executable statement with a single entry at the 12120 // top and a single exit at the bottom. 12121 // The point of exit cannot be a branch out of the structured block. 12122 // longjmp() and throw() must not violate the entry/exit criteria. 12123 CS->getCapturedDecl()->setNothrow(); 12124 } 12125 12126 OMPLoopBasedDirective::HelperExprs B; 12127 // In presence of clause 'collapse' with number of loops, it will 12128 // define the nested loops number. 12129 unsigned NestedLoopCount = checkOpenMPLoop( 12130 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12131 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12132 VarsWithImplicitDSA, B); 12133 if (NestedLoopCount == 0) 12134 return StmtError(); 12135 12136 assert((CurContext->isDependentContext() || B.builtAll()) && 12137 "omp for loop exprs were not built"); 12138 12139 setFunctionHasBranchProtectedScope(); 12140 return OMPDistributeParallelForDirective::Create( 12141 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12142 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12143 } 12144 12145 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 12146 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12147 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12148 if (!AStmt) 12149 return StmtError(); 12150 12151 auto *CS = cast<CapturedStmt>(AStmt); 12152 // 1.2.2 OpenMP Language Terminology 12153 // Structured block - An executable statement with a single entry at the 12154 // top and a single exit at the bottom. 12155 // The point of exit cannot be a branch out of the structured block. 12156 // longjmp() and throw() must not violate the entry/exit criteria. 12157 CS->getCapturedDecl()->setNothrow(); 12158 for (int ThisCaptureLevel = 12159 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 12160 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12161 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12162 // 1.2.2 OpenMP Language Terminology 12163 // Structured block - An executable statement with a single entry at the 12164 // top and a single exit at the bottom. 12165 // The point of exit cannot be a branch out of the structured block. 12166 // longjmp() and throw() must not violate the entry/exit criteria. 12167 CS->getCapturedDecl()->setNothrow(); 12168 } 12169 12170 OMPLoopBasedDirective::HelperExprs B; 12171 // In presence of clause 'collapse' with number of loops, it will 12172 // define the nested loops number. 12173 unsigned NestedLoopCount = checkOpenMPLoop( 12174 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12175 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12176 VarsWithImplicitDSA, B); 12177 if (NestedLoopCount == 0) 12178 return StmtError(); 12179 12180 assert((CurContext->isDependentContext() || B.builtAll()) && 12181 "omp for loop exprs were not built"); 12182 12183 if (!CurContext->isDependentContext()) { 12184 // Finalize the clauses that need pre-built expressions for CodeGen. 12185 for (OMPClause *C : Clauses) { 12186 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12187 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12188 B.NumIterations, *this, CurScope, 12189 DSAStack)) 12190 return StmtError(); 12191 } 12192 } 12193 12194 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12195 return StmtError(); 12196 12197 setFunctionHasBranchProtectedScope(); 12198 return OMPDistributeParallelForSimdDirective::Create( 12199 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12200 } 12201 12202 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 12203 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12204 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12205 if (!AStmt) 12206 return StmtError(); 12207 12208 auto *CS = cast<CapturedStmt>(AStmt); 12209 // 1.2.2 OpenMP Language Terminology 12210 // Structured block - An executable statement with a single entry at the 12211 // top and a single exit at the bottom. 12212 // The point of exit cannot be a branch out of the structured block. 12213 // longjmp() and throw() must not violate the entry/exit criteria. 12214 CS->getCapturedDecl()->setNothrow(); 12215 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 12216 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12217 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12218 // 1.2.2 OpenMP Language Terminology 12219 // Structured block - An executable statement with a single entry at the 12220 // top and a single exit at the bottom. 12221 // The point of exit cannot be a branch out of the structured block. 12222 // longjmp() and throw() must not violate the entry/exit criteria. 12223 CS->getCapturedDecl()->setNothrow(); 12224 } 12225 12226 OMPLoopBasedDirective::HelperExprs B; 12227 // In presence of clause 'collapse' with number of loops, it will 12228 // define the nested loops number. 12229 unsigned NestedLoopCount = 12230 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 12231 nullptr /*ordered not a clause on distribute*/, CS, *this, 12232 *DSAStack, VarsWithImplicitDSA, B); 12233 if (NestedLoopCount == 0) 12234 return StmtError(); 12235 12236 assert((CurContext->isDependentContext() || B.builtAll()) && 12237 "omp for loop exprs were not built"); 12238 12239 if (!CurContext->isDependentContext()) { 12240 // Finalize the clauses that need pre-built expressions for CodeGen. 12241 for (OMPClause *C : Clauses) { 12242 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12243 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12244 B.NumIterations, *this, CurScope, 12245 DSAStack)) 12246 return StmtError(); 12247 } 12248 } 12249 12250 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12251 return StmtError(); 12252 12253 setFunctionHasBranchProtectedScope(); 12254 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 12255 NestedLoopCount, Clauses, AStmt, B); 12256 } 12257 12258 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 12259 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12260 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12261 if (!AStmt) 12262 return StmtError(); 12263 12264 auto *CS = cast<CapturedStmt>(AStmt); 12265 // 1.2.2 OpenMP Language Terminology 12266 // Structured block - An executable statement with a single entry at the 12267 // top and a single exit at the bottom. 12268 // The point of exit cannot be a branch out of the structured block. 12269 // longjmp() and throw() must not violate the entry/exit criteria. 12270 CS->getCapturedDecl()->setNothrow(); 12271 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 12272 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12273 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12274 // 1.2.2 OpenMP Language Terminology 12275 // Structured block - An executable statement with a single entry at the 12276 // top and a single exit at the bottom. 12277 // The point of exit cannot be a branch out of the structured block. 12278 // longjmp() and throw() must not violate the entry/exit criteria. 12279 CS->getCapturedDecl()->setNothrow(); 12280 } 12281 12282 OMPLoopBasedDirective::HelperExprs B; 12283 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12284 // define the nested loops number. 12285 unsigned NestedLoopCount = checkOpenMPLoop( 12286 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 12287 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12288 VarsWithImplicitDSA, B); 12289 if (NestedLoopCount == 0) 12290 return StmtError(); 12291 12292 assert((CurContext->isDependentContext() || B.builtAll()) && 12293 "omp target parallel for simd loop exprs were not built"); 12294 12295 if (!CurContext->isDependentContext()) { 12296 // Finalize the clauses that need pre-built expressions for CodeGen. 12297 for (OMPClause *C : Clauses) { 12298 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12299 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12300 B.NumIterations, *this, CurScope, 12301 DSAStack)) 12302 return StmtError(); 12303 } 12304 } 12305 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12306 return StmtError(); 12307 12308 setFunctionHasBranchProtectedScope(); 12309 return OMPTargetParallelForSimdDirective::Create( 12310 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12311 } 12312 12313 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 12314 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12315 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12316 if (!AStmt) 12317 return StmtError(); 12318 12319 auto *CS = cast<CapturedStmt>(AStmt); 12320 // 1.2.2 OpenMP Language Terminology 12321 // Structured block - An executable statement with a single entry at the 12322 // top and a single exit at the bottom. 12323 // The point of exit cannot be a branch out of the structured block. 12324 // longjmp() and throw() must not violate the entry/exit criteria. 12325 CS->getCapturedDecl()->setNothrow(); 12326 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 12327 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12328 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12329 // 1.2.2 OpenMP Language Terminology 12330 // Structured block - An executable statement with a single entry at the 12331 // top and a single exit at the bottom. 12332 // The point of exit cannot be a branch out of the structured block. 12333 // longjmp() and throw() must not violate the entry/exit criteria. 12334 CS->getCapturedDecl()->setNothrow(); 12335 } 12336 12337 OMPLoopBasedDirective::HelperExprs B; 12338 // In presence of clause 'collapse' with number of loops, it will define the 12339 // nested loops number. 12340 unsigned NestedLoopCount = 12341 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 12342 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12343 VarsWithImplicitDSA, B); 12344 if (NestedLoopCount == 0) 12345 return StmtError(); 12346 12347 assert((CurContext->isDependentContext() || B.builtAll()) && 12348 "omp target simd loop exprs were not built"); 12349 12350 if (!CurContext->isDependentContext()) { 12351 // Finalize the clauses that need pre-built expressions for CodeGen. 12352 for (OMPClause *C : Clauses) { 12353 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12354 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12355 B.NumIterations, *this, CurScope, 12356 DSAStack)) 12357 return StmtError(); 12358 } 12359 } 12360 12361 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12362 return StmtError(); 12363 12364 setFunctionHasBranchProtectedScope(); 12365 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 12366 NestedLoopCount, Clauses, AStmt, B); 12367 } 12368 12369 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 12370 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12371 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12372 if (!AStmt) 12373 return StmtError(); 12374 12375 auto *CS = cast<CapturedStmt>(AStmt); 12376 // 1.2.2 OpenMP Language Terminology 12377 // Structured block - An executable statement with a single entry at the 12378 // top and a single exit at the bottom. 12379 // The point of exit cannot be a branch out of the structured block. 12380 // longjmp() and throw() must not violate the entry/exit criteria. 12381 CS->getCapturedDecl()->setNothrow(); 12382 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 12383 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12384 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12385 // 1.2.2 OpenMP Language Terminology 12386 // Structured block - An executable statement with a single entry at the 12387 // top and a single exit at the bottom. 12388 // The point of exit cannot be a branch out of the structured block. 12389 // longjmp() and throw() must not violate the entry/exit criteria. 12390 CS->getCapturedDecl()->setNothrow(); 12391 } 12392 12393 OMPLoopBasedDirective::HelperExprs B; 12394 // In presence of clause 'collapse' with number of loops, it will 12395 // define the nested loops number. 12396 unsigned NestedLoopCount = 12397 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 12398 nullptr /*ordered not a clause on distribute*/, CS, *this, 12399 *DSAStack, VarsWithImplicitDSA, B); 12400 if (NestedLoopCount == 0) 12401 return StmtError(); 12402 12403 assert((CurContext->isDependentContext() || B.builtAll()) && 12404 "omp teams distribute loop exprs were not built"); 12405 12406 setFunctionHasBranchProtectedScope(); 12407 12408 DSAStack->setParentTeamsRegionLoc(StartLoc); 12409 12410 return OMPTeamsDistributeDirective::Create( 12411 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12412 } 12413 12414 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 12415 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12416 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12417 if (!AStmt) 12418 return StmtError(); 12419 12420 auto *CS = cast<CapturedStmt>(AStmt); 12421 // 1.2.2 OpenMP Language Terminology 12422 // Structured block - An executable statement with a single entry at the 12423 // top and a single exit at the bottom. 12424 // The point of exit cannot be a branch out of the structured block. 12425 // longjmp() and throw() must not violate the entry/exit criteria. 12426 CS->getCapturedDecl()->setNothrow(); 12427 for (int ThisCaptureLevel = 12428 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 12429 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12430 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12431 // 1.2.2 OpenMP Language Terminology 12432 // Structured block - An executable statement with a single entry at the 12433 // top and a single exit at the bottom. 12434 // The point of exit cannot be a branch out of the structured block. 12435 // longjmp() and throw() must not violate the entry/exit criteria. 12436 CS->getCapturedDecl()->setNothrow(); 12437 } 12438 12439 OMPLoopBasedDirective::HelperExprs B; 12440 // In presence of clause 'collapse' with number of loops, it will 12441 // define the nested loops number. 12442 unsigned NestedLoopCount = checkOpenMPLoop( 12443 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12444 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12445 VarsWithImplicitDSA, B); 12446 12447 if (NestedLoopCount == 0) 12448 return StmtError(); 12449 12450 assert((CurContext->isDependentContext() || B.builtAll()) && 12451 "omp teams distribute simd loop exprs were not built"); 12452 12453 if (!CurContext->isDependentContext()) { 12454 // Finalize the clauses that need pre-built expressions for CodeGen. 12455 for (OMPClause *C : Clauses) { 12456 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12457 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12458 B.NumIterations, *this, CurScope, 12459 DSAStack)) 12460 return StmtError(); 12461 } 12462 } 12463 12464 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12465 return StmtError(); 12466 12467 setFunctionHasBranchProtectedScope(); 12468 12469 DSAStack->setParentTeamsRegionLoc(StartLoc); 12470 12471 return OMPTeamsDistributeSimdDirective::Create( 12472 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12473 } 12474 12475 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 12476 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12477 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12478 if (!AStmt) 12479 return StmtError(); 12480 12481 auto *CS = cast<CapturedStmt>(AStmt); 12482 // 1.2.2 OpenMP Language Terminology 12483 // Structured block - An executable statement with a single entry at the 12484 // top and a single exit at the bottom. 12485 // The point of exit cannot be a branch out of the structured block. 12486 // longjmp() and throw() must not violate the entry/exit criteria. 12487 CS->getCapturedDecl()->setNothrow(); 12488 12489 for (int ThisCaptureLevel = 12490 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 12491 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12492 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12493 // 1.2.2 OpenMP Language Terminology 12494 // Structured block - An executable statement with a single entry at the 12495 // top and a single exit at the bottom. 12496 // The point of exit cannot be a branch out of the structured block. 12497 // longjmp() and throw() must not violate the entry/exit criteria. 12498 CS->getCapturedDecl()->setNothrow(); 12499 } 12500 12501 OMPLoopBasedDirective::HelperExprs B; 12502 // In presence of clause 'collapse' with number of loops, it will 12503 // define the nested loops number. 12504 unsigned NestedLoopCount = checkOpenMPLoop( 12505 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12506 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12507 VarsWithImplicitDSA, B); 12508 12509 if (NestedLoopCount == 0) 12510 return StmtError(); 12511 12512 assert((CurContext->isDependentContext() || B.builtAll()) && 12513 "omp for loop exprs were not built"); 12514 12515 if (!CurContext->isDependentContext()) { 12516 // Finalize the clauses that need pre-built expressions for CodeGen. 12517 for (OMPClause *C : Clauses) { 12518 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12519 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12520 B.NumIterations, *this, CurScope, 12521 DSAStack)) 12522 return StmtError(); 12523 } 12524 } 12525 12526 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12527 return StmtError(); 12528 12529 setFunctionHasBranchProtectedScope(); 12530 12531 DSAStack->setParentTeamsRegionLoc(StartLoc); 12532 12533 return OMPTeamsDistributeParallelForSimdDirective::Create( 12534 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12535 } 12536 12537 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 12538 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12539 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12540 if (!AStmt) 12541 return StmtError(); 12542 12543 auto *CS = cast<CapturedStmt>(AStmt); 12544 // 1.2.2 OpenMP Language Terminology 12545 // Structured block - An executable statement with a single entry at the 12546 // top and a single exit at the bottom. 12547 // The point of exit cannot be a branch out of the structured block. 12548 // longjmp() and throw() must not violate the entry/exit criteria. 12549 CS->getCapturedDecl()->setNothrow(); 12550 12551 for (int ThisCaptureLevel = 12552 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 12553 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12554 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12555 // 1.2.2 OpenMP Language Terminology 12556 // Structured block - An executable statement with a single entry at the 12557 // top and a single exit at the bottom. 12558 // The point of exit cannot be a branch out of the structured block. 12559 // longjmp() and throw() must not violate the entry/exit criteria. 12560 CS->getCapturedDecl()->setNothrow(); 12561 } 12562 12563 OMPLoopBasedDirective::HelperExprs B; 12564 // In presence of clause 'collapse' with number of loops, it will 12565 // define the nested loops number. 12566 unsigned NestedLoopCount = checkOpenMPLoop( 12567 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12568 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12569 VarsWithImplicitDSA, B); 12570 12571 if (NestedLoopCount == 0) 12572 return StmtError(); 12573 12574 assert((CurContext->isDependentContext() || B.builtAll()) && 12575 "omp for loop exprs were not built"); 12576 12577 setFunctionHasBranchProtectedScope(); 12578 12579 DSAStack->setParentTeamsRegionLoc(StartLoc); 12580 12581 return OMPTeamsDistributeParallelForDirective::Create( 12582 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12583 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12584 } 12585 12586 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 12587 Stmt *AStmt, 12588 SourceLocation StartLoc, 12589 SourceLocation EndLoc) { 12590 if (!AStmt) 12591 return StmtError(); 12592 12593 auto *CS = cast<CapturedStmt>(AStmt); 12594 // 1.2.2 OpenMP Language Terminology 12595 // Structured block - An executable statement with a single entry at the 12596 // top and a single exit at the bottom. 12597 // The point of exit cannot be a branch out of the structured block. 12598 // longjmp() and throw() must not violate the entry/exit criteria. 12599 CS->getCapturedDecl()->setNothrow(); 12600 12601 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 12602 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12603 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12604 // 1.2.2 OpenMP Language Terminology 12605 // Structured block - An executable statement with a single entry at the 12606 // top and a single exit at the bottom. 12607 // The point of exit cannot be a branch out of the structured block. 12608 // longjmp() and throw() must not violate the entry/exit criteria. 12609 CS->getCapturedDecl()->setNothrow(); 12610 } 12611 setFunctionHasBranchProtectedScope(); 12612 12613 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 12614 AStmt); 12615 } 12616 12617 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 12618 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12619 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12620 if (!AStmt) 12621 return StmtError(); 12622 12623 auto *CS = cast<CapturedStmt>(AStmt); 12624 // 1.2.2 OpenMP Language Terminology 12625 // Structured block - An executable statement with a single entry at the 12626 // top and a single exit at the bottom. 12627 // The point of exit cannot be a branch out of the structured block. 12628 // longjmp() and throw() must not violate the entry/exit criteria. 12629 CS->getCapturedDecl()->setNothrow(); 12630 for (int ThisCaptureLevel = 12631 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 12632 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12633 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12634 // 1.2.2 OpenMP Language Terminology 12635 // Structured block - An executable statement with a single entry at the 12636 // top and a single exit at the bottom. 12637 // The point of exit cannot be a branch out of the structured block. 12638 // longjmp() and throw() must not violate the entry/exit criteria. 12639 CS->getCapturedDecl()->setNothrow(); 12640 } 12641 12642 OMPLoopBasedDirective::HelperExprs B; 12643 // In presence of clause 'collapse' with number of loops, it will 12644 // define the nested loops number. 12645 unsigned NestedLoopCount = checkOpenMPLoop( 12646 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 12647 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12648 VarsWithImplicitDSA, B); 12649 if (NestedLoopCount == 0) 12650 return StmtError(); 12651 12652 assert((CurContext->isDependentContext() || B.builtAll()) && 12653 "omp target teams distribute loop exprs were not built"); 12654 12655 setFunctionHasBranchProtectedScope(); 12656 return OMPTargetTeamsDistributeDirective::Create( 12657 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12658 } 12659 12660 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 12661 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12662 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12663 if (!AStmt) 12664 return StmtError(); 12665 12666 auto *CS = cast<CapturedStmt>(AStmt); 12667 // 1.2.2 OpenMP Language Terminology 12668 // Structured block - An executable statement with a single entry at the 12669 // top and a single exit at the bottom. 12670 // The point of exit cannot be a branch out of the structured block. 12671 // longjmp() and throw() must not violate the entry/exit criteria. 12672 CS->getCapturedDecl()->setNothrow(); 12673 for (int ThisCaptureLevel = 12674 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 12675 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12676 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12677 // 1.2.2 OpenMP Language Terminology 12678 // Structured block - An executable statement with a single entry at the 12679 // top and a single exit at the bottom. 12680 // The point of exit cannot be a branch out of the structured block. 12681 // longjmp() and throw() must not violate the entry/exit criteria. 12682 CS->getCapturedDecl()->setNothrow(); 12683 } 12684 12685 OMPLoopBasedDirective::HelperExprs B; 12686 // In presence of clause 'collapse' with number of loops, it will 12687 // define the nested loops number. 12688 unsigned NestedLoopCount = checkOpenMPLoop( 12689 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12690 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12691 VarsWithImplicitDSA, B); 12692 if (NestedLoopCount == 0) 12693 return StmtError(); 12694 12695 assert((CurContext->isDependentContext() || B.builtAll()) && 12696 "omp target teams distribute parallel for loop exprs were not built"); 12697 12698 if (!CurContext->isDependentContext()) { 12699 // Finalize the clauses that need pre-built expressions for CodeGen. 12700 for (OMPClause *C : Clauses) { 12701 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12702 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12703 B.NumIterations, *this, CurScope, 12704 DSAStack)) 12705 return StmtError(); 12706 } 12707 } 12708 12709 setFunctionHasBranchProtectedScope(); 12710 return OMPTargetTeamsDistributeParallelForDirective::Create( 12711 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12712 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12713 } 12714 12715 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 12716 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12717 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12718 if (!AStmt) 12719 return StmtError(); 12720 12721 auto *CS = cast<CapturedStmt>(AStmt); 12722 // 1.2.2 OpenMP Language Terminology 12723 // Structured block - An executable statement with a single entry at the 12724 // top and a single exit at the bottom. 12725 // The point of exit cannot be a branch out of the structured block. 12726 // longjmp() and throw() must not violate the entry/exit criteria. 12727 CS->getCapturedDecl()->setNothrow(); 12728 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 12729 OMPD_target_teams_distribute_parallel_for_simd); 12730 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12731 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12732 // 1.2.2 OpenMP Language Terminology 12733 // Structured block - An executable statement with a single entry at the 12734 // top and a single exit at the bottom. 12735 // The point of exit cannot be a branch out of the structured block. 12736 // longjmp() and throw() must not violate the entry/exit criteria. 12737 CS->getCapturedDecl()->setNothrow(); 12738 } 12739 12740 OMPLoopBasedDirective::HelperExprs B; 12741 // In presence of clause 'collapse' with number of loops, it will 12742 // define the nested loops number. 12743 unsigned NestedLoopCount = 12744 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 12745 getCollapseNumberExpr(Clauses), 12746 nullptr /*ordered not a clause on distribute*/, CS, *this, 12747 *DSAStack, VarsWithImplicitDSA, B); 12748 if (NestedLoopCount == 0) 12749 return StmtError(); 12750 12751 assert((CurContext->isDependentContext() || B.builtAll()) && 12752 "omp target teams distribute parallel for simd loop exprs were not " 12753 "built"); 12754 12755 if (!CurContext->isDependentContext()) { 12756 // Finalize the clauses that need pre-built expressions for CodeGen. 12757 for (OMPClause *C : Clauses) { 12758 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12759 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12760 B.NumIterations, *this, CurScope, 12761 DSAStack)) 12762 return StmtError(); 12763 } 12764 } 12765 12766 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12767 return StmtError(); 12768 12769 setFunctionHasBranchProtectedScope(); 12770 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 12771 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12772 } 12773 12774 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 12775 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12776 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12777 if (!AStmt) 12778 return StmtError(); 12779 12780 auto *CS = cast<CapturedStmt>(AStmt); 12781 // 1.2.2 OpenMP Language Terminology 12782 // Structured block - An executable statement with a single entry at the 12783 // top and a single exit at the bottom. 12784 // The point of exit cannot be a branch out of the structured block. 12785 // longjmp() and throw() must not violate the entry/exit criteria. 12786 CS->getCapturedDecl()->setNothrow(); 12787 for (int ThisCaptureLevel = 12788 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 12789 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12790 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12791 // 1.2.2 OpenMP Language Terminology 12792 // Structured block - An executable statement with a single entry at the 12793 // top and a single exit at the bottom. 12794 // The point of exit cannot be a branch out of the structured block. 12795 // longjmp() and throw() must not violate the entry/exit criteria. 12796 CS->getCapturedDecl()->setNothrow(); 12797 } 12798 12799 OMPLoopBasedDirective::HelperExprs B; 12800 // In presence of clause 'collapse' with number of loops, it will 12801 // define the nested loops number. 12802 unsigned NestedLoopCount = checkOpenMPLoop( 12803 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12804 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12805 VarsWithImplicitDSA, B); 12806 if (NestedLoopCount == 0) 12807 return StmtError(); 12808 12809 assert((CurContext->isDependentContext() || B.builtAll()) && 12810 "omp target teams distribute simd loop exprs were not built"); 12811 12812 if (!CurContext->isDependentContext()) { 12813 // Finalize the clauses that need pre-built expressions for CodeGen. 12814 for (OMPClause *C : Clauses) { 12815 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12816 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12817 B.NumIterations, *this, CurScope, 12818 DSAStack)) 12819 return StmtError(); 12820 } 12821 } 12822 12823 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12824 return StmtError(); 12825 12826 setFunctionHasBranchProtectedScope(); 12827 return OMPTargetTeamsDistributeSimdDirective::Create( 12828 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12829 } 12830 12831 bool Sema::checkTransformableLoopNest( 12832 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops, 12833 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers, 12834 Stmt *&Body, 12835 SmallVectorImpl<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>> 12836 &OriginalInits) { 12837 OriginalInits.emplace_back(); 12838 bool Result = OMPLoopBasedDirective::doForAllLoops( 12839 AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops, 12840 [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt, 12841 Stmt *CurStmt) { 12842 VarsWithInheritedDSAType TmpDSA; 12843 unsigned SingleNumLoops = 12844 checkOpenMPLoop(Kind, nullptr, nullptr, CurStmt, *this, *DSAStack, 12845 TmpDSA, LoopHelpers[Cnt]); 12846 if (SingleNumLoops == 0) 12847 return true; 12848 assert(SingleNumLoops == 1 && "Expect single loop iteration space"); 12849 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 12850 OriginalInits.back().push_back(For->getInit()); 12851 Body = For->getBody(); 12852 } else { 12853 assert(isa<CXXForRangeStmt>(CurStmt) && 12854 "Expected canonical for or range-based for loops."); 12855 auto *CXXFor = cast<CXXForRangeStmt>(CurStmt); 12856 OriginalInits.back().push_back(CXXFor->getBeginStmt()); 12857 Body = CXXFor->getBody(); 12858 } 12859 OriginalInits.emplace_back(); 12860 return false; 12861 }, 12862 [&OriginalInits](OMPLoopBasedDirective *Transform) { 12863 Stmt *DependentPreInits; 12864 if (auto *Dir = dyn_cast<OMPTileDirective>(Transform)) 12865 DependentPreInits = Dir->getPreInits(); 12866 else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform)) 12867 DependentPreInits = Dir->getPreInits(); 12868 else 12869 llvm_unreachable("Unhandled loop transformation"); 12870 if (!DependentPreInits) 12871 return; 12872 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) 12873 OriginalInits.back().push_back(C); 12874 }); 12875 assert(OriginalInits.back().empty() && "No preinit after innermost loop"); 12876 OriginalInits.pop_back(); 12877 return Result; 12878 } 12879 12880 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses, 12881 Stmt *AStmt, SourceLocation StartLoc, 12882 SourceLocation EndLoc) { 12883 auto SizesClauses = 12884 OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses); 12885 if (SizesClauses.empty()) { 12886 // A missing 'sizes' clause is already reported by the parser. 12887 return StmtError(); 12888 } 12889 const OMPSizesClause *SizesClause = *SizesClauses.begin(); 12890 unsigned NumLoops = SizesClause->getNumSizes(); 12891 12892 // Empty statement should only be possible if there already was an error. 12893 if (!AStmt) 12894 return StmtError(); 12895 12896 // Verify and diagnose loop nest. 12897 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops); 12898 Stmt *Body = nullptr; 12899 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, 4> 12900 OriginalInits; 12901 if (!checkTransformableLoopNest(OMPD_tile, AStmt, NumLoops, LoopHelpers, Body, 12902 OriginalInits)) 12903 return StmtError(); 12904 12905 // Delay tiling to when template is completely instantiated. 12906 if (CurContext->isDependentContext()) 12907 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, 12908 NumLoops, AStmt, nullptr, nullptr); 12909 12910 SmallVector<Decl *, 4> PreInits; 12911 12912 // Create iteration variables for the generated loops. 12913 SmallVector<VarDecl *, 4> FloorIndVars; 12914 SmallVector<VarDecl *, 4> TileIndVars; 12915 FloorIndVars.resize(NumLoops); 12916 TileIndVars.resize(NumLoops); 12917 for (unsigned I = 0; I < NumLoops; ++I) { 12918 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12919 12920 assert(LoopHelper.Counters.size() == 1 && 12921 "Expect single-dimensional loop iteration space"); 12922 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 12923 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString(); 12924 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 12925 QualType CntTy = IterVarRef->getType(); 12926 12927 // Iteration variable for the floor (i.e. outer) loop. 12928 { 12929 std::string FloorCntName = 12930 (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12931 VarDecl *FloorCntDecl = 12932 buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar); 12933 FloorIndVars[I] = FloorCntDecl; 12934 } 12935 12936 // Iteration variable for the tile (i.e. inner) loop. 12937 { 12938 std::string TileCntName = 12939 (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12940 12941 // Reuse the iteration variable created by checkOpenMPLoop. It is also 12942 // used by the expressions to derive the original iteration variable's 12943 // value from the logical iteration number. 12944 auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl()); 12945 TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName)); 12946 TileIndVars[I] = TileCntDecl; 12947 } 12948 for (auto &P : OriginalInits[I]) { 12949 if (auto *D = P.dyn_cast<Decl *>()) 12950 PreInits.push_back(D); 12951 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 12952 PreInits.append(PI->decl_begin(), PI->decl_end()); 12953 } 12954 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 12955 PreInits.append(PI->decl_begin(), PI->decl_end()); 12956 // Gather declarations for the data members used as counters. 12957 for (Expr *CounterRef : LoopHelper.Counters) { 12958 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 12959 if (isa<OMPCapturedExprDecl>(CounterDecl)) 12960 PreInits.push_back(CounterDecl); 12961 } 12962 } 12963 12964 // Once the original iteration values are set, append the innermost body. 12965 Stmt *Inner = Body; 12966 12967 // Create tile loops from the inside to the outside. 12968 for (int I = NumLoops - 1; I >= 0; --I) { 12969 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12970 Expr *NumIterations = LoopHelper.NumIterations; 12971 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12972 QualType CntTy = OrigCntVar->getType(); 12973 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12974 Scope *CurScope = getCurScope(); 12975 12976 // Commonly used variables. 12977 DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy, 12978 OrigCntVar->getExprLoc()); 12979 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12980 OrigCntVar->getExprLoc()); 12981 12982 // For init-statement: auto .tile.iv = .floor.iv 12983 AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(), 12984 /*DirectInit=*/false); 12985 Decl *CounterDecl = TileIndVars[I]; 12986 StmtResult InitStmt = new (Context) 12987 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12988 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12989 if (!InitStmt.isUsable()) 12990 return StmtError(); 12991 12992 // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize, 12993 // NumIterations) 12994 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12995 BO_Add, FloorIV, DimTileSize); 12996 if (!EndOfTile.isUsable()) 12997 return StmtError(); 12998 ExprResult IsPartialTile = 12999 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, 13000 NumIterations, EndOfTile.get()); 13001 if (!IsPartialTile.isUsable()) 13002 return StmtError(); 13003 ExprResult MinTileAndIterSpace = ActOnConditionalOp( 13004 LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(), 13005 IsPartialTile.get(), NumIterations, EndOfTile.get()); 13006 if (!MinTileAndIterSpace.isUsable()) 13007 return StmtError(); 13008 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13009 BO_LT, TileIV, MinTileAndIterSpace.get()); 13010 if (!CondExpr.isUsable()) 13011 return StmtError(); 13012 13013 // For incr-statement: ++.tile.iv 13014 ExprResult IncrStmt = 13015 BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV); 13016 if (!IncrStmt.isUsable()) 13017 return StmtError(); 13018 13019 // Statements to set the original iteration variable's value from the 13020 // logical iteration number. 13021 // Generated for loop is: 13022 // Original_for_init; 13023 // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize, 13024 // NumIterations); ++.tile.iv) { 13025 // Original_Body; 13026 // Original_counter_update; 13027 // } 13028 // FIXME: If the innermost body is an loop itself, inserting these 13029 // statements stops it being recognized as a perfectly nested loop (e.g. 13030 // for applying tiling again). If this is the case, sink the expressions 13031 // further into the inner loop. 13032 SmallVector<Stmt *, 4> BodyParts; 13033 BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 13034 BodyParts.push_back(Inner); 13035 Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(), 13036 Inner->getEndLoc()); 13037 Inner = new (Context) 13038 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 13039 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 13040 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13041 } 13042 13043 // Create floor loops from the inside to the outside. 13044 for (int I = NumLoops - 1; I >= 0; --I) { 13045 auto &LoopHelper = LoopHelpers[I]; 13046 Expr *NumIterations = LoopHelper.NumIterations; 13047 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 13048 QualType CntTy = OrigCntVar->getType(); 13049 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 13050 Scope *CurScope = getCurScope(); 13051 13052 // Commonly used variables. 13053 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 13054 OrigCntVar->getExprLoc()); 13055 13056 // For init-statement: auto .floor.iv = 0 13057 AddInitializerToDecl( 13058 FloorIndVars[I], 13059 ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 13060 /*DirectInit=*/false); 13061 Decl *CounterDecl = FloorIndVars[I]; 13062 StmtResult InitStmt = new (Context) 13063 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 13064 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 13065 if (!InitStmt.isUsable()) 13066 return StmtError(); 13067 13068 // For cond-expression: .floor.iv < NumIterations 13069 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13070 BO_LT, FloorIV, NumIterations); 13071 if (!CondExpr.isUsable()) 13072 return StmtError(); 13073 13074 // For incr-statement: .floor.iv += DimTileSize 13075 ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), 13076 BO_AddAssign, FloorIV, DimTileSize); 13077 if (!IncrStmt.isUsable()) 13078 return StmtError(); 13079 13080 Inner = new (Context) 13081 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 13082 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 13083 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13084 } 13085 13086 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops, 13087 AStmt, Inner, 13088 buildPreInits(Context, PreInits)); 13089 } 13090 13091 StmtResult Sema::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses, 13092 Stmt *AStmt, 13093 SourceLocation StartLoc, 13094 SourceLocation EndLoc) { 13095 // Empty statement should only be possible if there already was an error. 13096 if (!AStmt) 13097 return StmtError(); 13098 13099 if (checkMutuallyExclusiveClauses(*this, Clauses, {OMPC_partial, OMPC_full})) 13100 return StmtError(); 13101 13102 const OMPFullClause *FullClause = 13103 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses); 13104 const OMPPartialClause *PartialClause = 13105 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses); 13106 assert(!(FullClause && PartialClause) && 13107 "mutual exclusivity must have been checked before"); 13108 13109 constexpr unsigned NumLoops = 1; 13110 Stmt *Body = nullptr; 13111 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers( 13112 NumLoops); 13113 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, NumLoops + 1> 13114 OriginalInits; 13115 if (!checkTransformableLoopNest(OMPD_unroll, AStmt, NumLoops, LoopHelpers, 13116 Body, OriginalInits)) 13117 return StmtError(); 13118 13119 unsigned NumGeneratedLoops = PartialClause ? 1 : 0; 13120 13121 // Delay unrolling to when template is completely instantiated. 13122 if (CurContext->isDependentContext()) 13123 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13124 NumGeneratedLoops, nullptr, nullptr); 13125 13126 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front(); 13127 13128 if (FullClause) { 13129 if (!VerifyPositiveIntegerConstantInClause( 13130 LoopHelper.NumIterations, OMPC_full, /*StrictlyPositive=*/false, 13131 /*SuppressExprDigs=*/true) 13132 .isUsable()) { 13133 Diag(AStmt->getBeginLoc(), diag::err_omp_unroll_full_variable_trip_count); 13134 Diag(FullClause->getBeginLoc(), diag::note_omp_directive_here) 13135 << "#pragma omp unroll full"; 13136 return StmtError(); 13137 } 13138 } 13139 13140 // The generated loop may only be passed to other loop-associated directive 13141 // when a partial clause is specified. Without the requirement it is 13142 // sufficient to generate loop unroll metadata at code-generation. 13143 if (NumGeneratedLoops == 0) 13144 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13145 NumGeneratedLoops, nullptr, nullptr); 13146 13147 // Otherwise, we need to provide a de-sugared/transformed AST that can be 13148 // associated with another loop directive. 13149 // 13150 // The canonical loop analysis return by checkTransformableLoopNest assumes 13151 // the following structure to be the same loop without transformations or 13152 // directives applied: \code OriginalInits; LoopHelper.PreInits; 13153 // LoopHelper.Counters; 13154 // for (; IV < LoopHelper.NumIterations; ++IV) { 13155 // LoopHelper.Updates; 13156 // Body; 13157 // } 13158 // \endcode 13159 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits 13160 // and referenced by LoopHelper.IterationVarRef. 13161 // 13162 // The unrolling directive transforms this into the following loop: 13163 // \code 13164 // OriginalInits; \ 13165 // LoopHelper.PreInits; > NewPreInits 13166 // LoopHelper.Counters; / 13167 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) { 13168 // #pragma clang loop unroll_count(Factor) 13169 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV) 13170 // { 13171 // LoopHelper.Updates; 13172 // Body; 13173 // } 13174 // } 13175 // \endcode 13176 // where UIV is a new logical iteration counter. IV must be the same VarDecl 13177 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates 13178 // references it. If the partially unrolled loop is associated with another 13179 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to 13180 // analyze this loop, i.e. the outer loop must fulfill the constraints of an 13181 // OpenMP canonical loop. The inner loop is not an associable canonical loop 13182 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of 13183 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a 13184 // property of the OMPLoopBasedDirective instead of statements in 13185 // CompoundStatement. This is to allow the loop to become a non-outermost loop 13186 // of a canonical loop nest where these PreInits are emitted before the 13187 // outermost directive. 13188 13189 // Determine the PreInit declarations. 13190 SmallVector<Decl *, 4> PreInits; 13191 assert(OriginalInits.size() == 1 && 13192 "Expecting a single-dimensional loop iteration space"); 13193 for (auto &P : OriginalInits[0]) { 13194 if (auto *D = P.dyn_cast<Decl *>()) 13195 PreInits.push_back(D); 13196 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 13197 PreInits.append(PI->decl_begin(), PI->decl_end()); 13198 } 13199 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 13200 PreInits.append(PI->decl_begin(), PI->decl_end()); 13201 // Gather declarations for the data members used as counters. 13202 for (Expr *CounterRef : LoopHelper.Counters) { 13203 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 13204 if (isa<OMPCapturedExprDecl>(CounterDecl)) 13205 PreInits.push_back(CounterDecl); 13206 } 13207 13208 auto *IterationVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 13209 QualType IVTy = IterationVarRef->getType(); 13210 assert(LoopHelper.Counters.size() == 1 && 13211 "Expecting a single-dimensional loop iteration space"); 13212 auto *OrigVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 13213 13214 // Determine the unroll factor. 13215 uint64_t Factor; 13216 SourceLocation FactorLoc; 13217 if (Expr *FactorVal = PartialClause->getFactor()) { 13218 Factor = 13219 FactorVal->getIntegerConstantExpr(Context).getValue().getZExtValue(); 13220 FactorLoc = FactorVal->getExprLoc(); 13221 } else { 13222 // TODO: Use a better profitability model. 13223 Factor = 2; 13224 } 13225 assert(Factor > 0 && "Expected positive unroll factor"); 13226 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() { 13227 return IntegerLiteral::Create( 13228 Context, llvm::APInt(Context.getIntWidth(IVTy), Factor), IVTy, 13229 FactorLoc); 13230 }; 13231 13232 // Iteration variable SourceLocations. 13233 SourceLocation OrigVarLoc = OrigVar->getExprLoc(); 13234 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc(); 13235 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc(); 13236 13237 // Internal variable names. 13238 std::string OrigVarName = OrigVar->getNameInfo().getAsString(); 13239 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str(); 13240 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str(); 13241 std::string InnerTripCountName = 13242 (Twine(".unroll_inner.tripcount.") + OrigVarName).str(); 13243 13244 // Create the iteration variable for the unrolled loop. 13245 VarDecl *OuterIVDecl = 13246 buildVarDecl(*this, {}, IVTy, OuterIVName, nullptr, OrigVar); 13247 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() { 13248 return buildDeclRefExpr(*this, OuterIVDecl, IVTy, OrigVarLoc); 13249 }; 13250 13251 // Iteration variable for the inner loop: Reuse the iteration variable created 13252 // by checkOpenMPLoop. 13253 auto *InnerIVDecl = cast<VarDecl>(IterationVarRef->getDecl()); 13254 InnerIVDecl->setDeclName(&PP.getIdentifierTable().get(InnerIVName)); 13255 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() { 13256 return buildDeclRefExpr(*this, InnerIVDecl, IVTy, OrigVarLoc); 13257 }; 13258 13259 // Make a copy of the NumIterations expression for each use: By the AST 13260 // constraints, every expression object in a DeclContext must be unique. 13261 CaptureVars CopyTransformer(*this); 13262 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * { 13263 return AssertSuccess( 13264 CopyTransformer.TransformExpr(LoopHelper.NumIterations)); 13265 }; 13266 13267 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv 13268 ExprResult LValueConv = DefaultLvalueConversion(MakeOuterRef()); 13269 AddInitializerToDecl(InnerIVDecl, LValueConv.get(), /*DirectInit=*/false); 13270 StmtResult InnerInit = new (Context) 13271 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13272 if (!InnerInit.isUsable()) 13273 return StmtError(); 13274 13275 // Inner For cond-expression: 13276 // \code 13277 // .unroll_inner.iv < .unrolled.iv + Factor && 13278 // .unroll_inner.iv < NumIterations 13279 // \endcode 13280 // This conjunction of two conditions allows ScalarEvolution to derive the 13281 // maximum trip count of the inner loop. 13282 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13283 BO_Add, MakeOuterRef(), MakeFactorExpr()); 13284 if (!EndOfTile.isUsable()) 13285 return StmtError(); 13286 ExprResult InnerCond1 = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13287 BO_LE, MakeInnerRef(), EndOfTile.get()); 13288 if (!InnerCond1.isUsable()) 13289 return StmtError(); 13290 ExprResult InnerCond2 = 13291 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LE, MakeInnerRef(), 13292 MakeNumIterations()); 13293 if (!InnerCond2.isUsable()) 13294 return StmtError(); 13295 ExprResult InnerCond = 13296 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LAnd, 13297 InnerCond1.get(), InnerCond2.get()); 13298 if (!InnerCond.isUsable()) 13299 return StmtError(); 13300 13301 // Inner For incr-statement: ++.unroll_inner.iv 13302 ExprResult InnerIncr = BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), 13303 UO_PreInc, MakeInnerRef()); 13304 if (!InnerIncr.isUsable()) 13305 return StmtError(); 13306 13307 // Inner For statement. 13308 SmallVector<Stmt *> InnerBodyStmts; 13309 InnerBodyStmts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 13310 InnerBodyStmts.push_back(Body); 13311 CompoundStmt *InnerBody = CompoundStmt::Create( 13312 Context, InnerBodyStmts, Body->getBeginLoc(), Body->getEndLoc()); 13313 ForStmt *InnerFor = new (Context) 13314 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr, 13315 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(), 13316 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13317 13318 // Unroll metadata for the inner loop. 13319 // This needs to take into account the remainder portion of the unrolled loop, 13320 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass 13321 // supports multiple loop exits. Instead, unroll using a factor equivalent to 13322 // the maximum trip count, which will also generate a remainder loop. Just 13323 // `unroll(enable)` (which could have been useful if the user has not 13324 // specified a concrete factor; even though the outer loop cannot be 13325 // influenced anymore, would avoid more code bloat than necessary) will refuse 13326 // the loop because "Won't unroll; remainder loop could not be generated when 13327 // assuming runtime trip count". Even if it did work, it must not choose a 13328 // larger unroll factor than the maximum loop length, or it would always just 13329 // execute the remainder loop. 13330 LoopHintAttr *UnrollHintAttr = 13331 LoopHintAttr::CreateImplicit(Context, LoopHintAttr::UnrollCount, 13332 LoopHintAttr::Numeric, MakeFactorExpr()); 13333 AttributedStmt *InnerUnrolled = 13334 AttributedStmt::Create(Context, StartLoc, {UnrollHintAttr}, InnerFor); 13335 13336 // Outer For init-statement: auto .unrolled.iv = 0 13337 AddInitializerToDecl( 13338 OuterIVDecl, ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 13339 /*DirectInit=*/false); 13340 StmtResult OuterInit = new (Context) 13341 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13342 if (!OuterInit.isUsable()) 13343 return StmtError(); 13344 13345 // Outer For cond-expression: .unrolled.iv < NumIterations 13346 ExprResult OuterConde = 13347 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeOuterRef(), 13348 MakeNumIterations()); 13349 if (!OuterConde.isUsable()) 13350 return StmtError(); 13351 13352 // Outer For incr-statement: .unrolled.iv += Factor 13353 ExprResult OuterIncr = 13354 BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign, 13355 MakeOuterRef(), MakeFactorExpr()); 13356 if (!OuterIncr.isUsable()) 13357 return StmtError(); 13358 13359 // Outer For statement. 13360 ForStmt *OuterFor = new (Context) 13361 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr, 13362 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(), 13363 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13364 13365 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13366 NumGeneratedLoops, OuterFor, 13367 buildPreInits(Context, PreInits)); 13368 } 13369 13370 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 13371 SourceLocation StartLoc, 13372 SourceLocation LParenLoc, 13373 SourceLocation EndLoc) { 13374 OMPClause *Res = nullptr; 13375 switch (Kind) { 13376 case OMPC_final: 13377 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 13378 break; 13379 case OMPC_num_threads: 13380 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 13381 break; 13382 case OMPC_safelen: 13383 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 13384 break; 13385 case OMPC_simdlen: 13386 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 13387 break; 13388 case OMPC_allocator: 13389 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 13390 break; 13391 case OMPC_collapse: 13392 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 13393 break; 13394 case OMPC_ordered: 13395 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 13396 break; 13397 case OMPC_num_teams: 13398 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 13399 break; 13400 case OMPC_thread_limit: 13401 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 13402 break; 13403 case OMPC_priority: 13404 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 13405 break; 13406 case OMPC_grainsize: 13407 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 13408 break; 13409 case OMPC_num_tasks: 13410 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 13411 break; 13412 case OMPC_hint: 13413 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 13414 break; 13415 case OMPC_depobj: 13416 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 13417 break; 13418 case OMPC_detach: 13419 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 13420 break; 13421 case OMPC_novariants: 13422 Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc); 13423 break; 13424 case OMPC_nocontext: 13425 Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc); 13426 break; 13427 case OMPC_filter: 13428 Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc); 13429 break; 13430 case OMPC_partial: 13431 Res = ActOnOpenMPPartialClause(Expr, StartLoc, LParenLoc, EndLoc); 13432 break; 13433 case OMPC_align: 13434 Res = ActOnOpenMPAlignClause(Expr, StartLoc, LParenLoc, EndLoc); 13435 break; 13436 case OMPC_device: 13437 case OMPC_if: 13438 case OMPC_default: 13439 case OMPC_proc_bind: 13440 case OMPC_schedule: 13441 case OMPC_private: 13442 case OMPC_firstprivate: 13443 case OMPC_lastprivate: 13444 case OMPC_shared: 13445 case OMPC_reduction: 13446 case OMPC_task_reduction: 13447 case OMPC_in_reduction: 13448 case OMPC_linear: 13449 case OMPC_aligned: 13450 case OMPC_copyin: 13451 case OMPC_copyprivate: 13452 case OMPC_nowait: 13453 case OMPC_untied: 13454 case OMPC_mergeable: 13455 case OMPC_threadprivate: 13456 case OMPC_sizes: 13457 case OMPC_allocate: 13458 case OMPC_flush: 13459 case OMPC_read: 13460 case OMPC_write: 13461 case OMPC_update: 13462 case OMPC_capture: 13463 case OMPC_seq_cst: 13464 case OMPC_acq_rel: 13465 case OMPC_acquire: 13466 case OMPC_release: 13467 case OMPC_relaxed: 13468 case OMPC_depend: 13469 case OMPC_threads: 13470 case OMPC_simd: 13471 case OMPC_map: 13472 case OMPC_nogroup: 13473 case OMPC_dist_schedule: 13474 case OMPC_defaultmap: 13475 case OMPC_unknown: 13476 case OMPC_uniform: 13477 case OMPC_to: 13478 case OMPC_from: 13479 case OMPC_use_device_ptr: 13480 case OMPC_use_device_addr: 13481 case OMPC_is_device_ptr: 13482 case OMPC_unified_address: 13483 case OMPC_unified_shared_memory: 13484 case OMPC_reverse_offload: 13485 case OMPC_dynamic_allocators: 13486 case OMPC_atomic_default_mem_order: 13487 case OMPC_device_type: 13488 case OMPC_match: 13489 case OMPC_nontemporal: 13490 case OMPC_order: 13491 case OMPC_destroy: 13492 case OMPC_inclusive: 13493 case OMPC_exclusive: 13494 case OMPC_uses_allocators: 13495 case OMPC_affinity: 13496 case OMPC_when: 13497 case OMPC_bind: 13498 default: 13499 llvm_unreachable("Clause is not allowed."); 13500 } 13501 return Res; 13502 } 13503 13504 // An OpenMP directive such as 'target parallel' has two captured regions: 13505 // for the 'target' and 'parallel' respectively. This function returns 13506 // the region in which to capture expressions associated with a clause. 13507 // A return value of OMPD_unknown signifies that the expression should not 13508 // be captured. 13509 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 13510 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 13511 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 13512 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13513 switch (CKind) { 13514 case OMPC_if: 13515 switch (DKind) { 13516 case OMPD_target_parallel_for_simd: 13517 if (OpenMPVersion >= 50 && 13518 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13519 CaptureRegion = OMPD_parallel; 13520 break; 13521 } 13522 LLVM_FALLTHROUGH; 13523 case OMPD_target_parallel: 13524 case OMPD_target_parallel_for: 13525 // If this clause applies to the nested 'parallel' region, capture within 13526 // the 'target' region, otherwise do not capture. 13527 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13528 CaptureRegion = OMPD_target; 13529 break; 13530 case OMPD_target_teams_distribute_parallel_for_simd: 13531 if (OpenMPVersion >= 50 && 13532 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13533 CaptureRegion = OMPD_parallel; 13534 break; 13535 } 13536 LLVM_FALLTHROUGH; 13537 case OMPD_target_teams_distribute_parallel_for: 13538 // If this clause applies to the nested 'parallel' region, capture within 13539 // the 'teams' region, otherwise do not capture. 13540 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13541 CaptureRegion = OMPD_teams; 13542 break; 13543 case OMPD_teams_distribute_parallel_for_simd: 13544 if (OpenMPVersion >= 50 && 13545 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13546 CaptureRegion = OMPD_parallel; 13547 break; 13548 } 13549 LLVM_FALLTHROUGH; 13550 case OMPD_teams_distribute_parallel_for: 13551 CaptureRegion = OMPD_teams; 13552 break; 13553 case OMPD_target_update: 13554 case OMPD_target_enter_data: 13555 case OMPD_target_exit_data: 13556 CaptureRegion = OMPD_task; 13557 break; 13558 case OMPD_parallel_master_taskloop: 13559 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 13560 CaptureRegion = OMPD_parallel; 13561 break; 13562 case OMPD_parallel_master_taskloop_simd: 13563 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 13564 NameModifier == OMPD_taskloop) { 13565 CaptureRegion = OMPD_parallel; 13566 break; 13567 } 13568 if (OpenMPVersion <= 45) 13569 break; 13570 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13571 CaptureRegion = OMPD_taskloop; 13572 break; 13573 case OMPD_parallel_for_simd: 13574 if (OpenMPVersion <= 45) 13575 break; 13576 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13577 CaptureRegion = OMPD_parallel; 13578 break; 13579 case OMPD_taskloop_simd: 13580 case OMPD_master_taskloop_simd: 13581 if (OpenMPVersion <= 45) 13582 break; 13583 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13584 CaptureRegion = OMPD_taskloop; 13585 break; 13586 case OMPD_distribute_parallel_for_simd: 13587 if (OpenMPVersion <= 45) 13588 break; 13589 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13590 CaptureRegion = OMPD_parallel; 13591 break; 13592 case OMPD_target_simd: 13593 if (OpenMPVersion >= 50 && 13594 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13595 CaptureRegion = OMPD_target; 13596 break; 13597 case OMPD_teams_distribute_simd: 13598 case OMPD_target_teams_distribute_simd: 13599 if (OpenMPVersion >= 50 && 13600 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13601 CaptureRegion = OMPD_teams; 13602 break; 13603 case OMPD_cancel: 13604 case OMPD_parallel: 13605 case OMPD_parallel_master: 13606 case OMPD_parallel_sections: 13607 case OMPD_parallel_for: 13608 case OMPD_target: 13609 case OMPD_target_teams: 13610 case OMPD_target_teams_distribute: 13611 case OMPD_distribute_parallel_for: 13612 case OMPD_task: 13613 case OMPD_taskloop: 13614 case OMPD_master_taskloop: 13615 case OMPD_target_data: 13616 case OMPD_simd: 13617 case OMPD_for_simd: 13618 case OMPD_distribute_simd: 13619 // Do not capture if-clause expressions. 13620 break; 13621 case OMPD_threadprivate: 13622 case OMPD_allocate: 13623 case OMPD_taskyield: 13624 case OMPD_barrier: 13625 case OMPD_taskwait: 13626 case OMPD_cancellation_point: 13627 case OMPD_flush: 13628 case OMPD_depobj: 13629 case OMPD_scan: 13630 case OMPD_declare_reduction: 13631 case OMPD_declare_mapper: 13632 case OMPD_declare_simd: 13633 case OMPD_declare_variant: 13634 case OMPD_begin_declare_variant: 13635 case OMPD_end_declare_variant: 13636 case OMPD_declare_target: 13637 case OMPD_end_declare_target: 13638 case OMPD_loop: 13639 case OMPD_teams: 13640 case OMPD_tile: 13641 case OMPD_unroll: 13642 case OMPD_for: 13643 case OMPD_sections: 13644 case OMPD_section: 13645 case OMPD_single: 13646 case OMPD_master: 13647 case OMPD_masked: 13648 case OMPD_critical: 13649 case OMPD_taskgroup: 13650 case OMPD_distribute: 13651 case OMPD_ordered: 13652 case OMPD_atomic: 13653 case OMPD_teams_distribute: 13654 case OMPD_requires: 13655 case OMPD_metadirective: 13656 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 13657 case OMPD_unknown: 13658 default: 13659 llvm_unreachable("Unknown OpenMP directive"); 13660 } 13661 break; 13662 case OMPC_num_threads: 13663 switch (DKind) { 13664 case OMPD_target_parallel: 13665 case OMPD_target_parallel_for: 13666 case OMPD_target_parallel_for_simd: 13667 CaptureRegion = OMPD_target; 13668 break; 13669 case OMPD_teams_distribute_parallel_for: 13670 case OMPD_teams_distribute_parallel_for_simd: 13671 case OMPD_target_teams_distribute_parallel_for: 13672 case OMPD_target_teams_distribute_parallel_for_simd: 13673 CaptureRegion = OMPD_teams; 13674 break; 13675 case OMPD_parallel: 13676 case OMPD_parallel_master: 13677 case OMPD_parallel_sections: 13678 case OMPD_parallel_for: 13679 case OMPD_parallel_for_simd: 13680 case OMPD_distribute_parallel_for: 13681 case OMPD_distribute_parallel_for_simd: 13682 case OMPD_parallel_master_taskloop: 13683 case OMPD_parallel_master_taskloop_simd: 13684 // Do not capture num_threads-clause expressions. 13685 break; 13686 case OMPD_target_data: 13687 case OMPD_target_enter_data: 13688 case OMPD_target_exit_data: 13689 case OMPD_target_update: 13690 case OMPD_target: 13691 case OMPD_target_simd: 13692 case OMPD_target_teams: 13693 case OMPD_target_teams_distribute: 13694 case OMPD_target_teams_distribute_simd: 13695 case OMPD_cancel: 13696 case OMPD_task: 13697 case OMPD_taskloop: 13698 case OMPD_taskloop_simd: 13699 case OMPD_master_taskloop: 13700 case OMPD_master_taskloop_simd: 13701 case OMPD_threadprivate: 13702 case OMPD_allocate: 13703 case OMPD_taskyield: 13704 case OMPD_barrier: 13705 case OMPD_taskwait: 13706 case OMPD_cancellation_point: 13707 case OMPD_flush: 13708 case OMPD_depobj: 13709 case OMPD_scan: 13710 case OMPD_declare_reduction: 13711 case OMPD_declare_mapper: 13712 case OMPD_declare_simd: 13713 case OMPD_declare_variant: 13714 case OMPD_begin_declare_variant: 13715 case OMPD_end_declare_variant: 13716 case OMPD_declare_target: 13717 case OMPD_end_declare_target: 13718 case OMPD_loop: 13719 case OMPD_teams: 13720 case OMPD_simd: 13721 case OMPD_tile: 13722 case OMPD_unroll: 13723 case OMPD_for: 13724 case OMPD_for_simd: 13725 case OMPD_sections: 13726 case OMPD_section: 13727 case OMPD_single: 13728 case OMPD_master: 13729 case OMPD_masked: 13730 case OMPD_critical: 13731 case OMPD_taskgroup: 13732 case OMPD_distribute: 13733 case OMPD_ordered: 13734 case OMPD_atomic: 13735 case OMPD_distribute_simd: 13736 case OMPD_teams_distribute: 13737 case OMPD_teams_distribute_simd: 13738 case OMPD_requires: 13739 case OMPD_metadirective: 13740 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 13741 case OMPD_unknown: 13742 default: 13743 llvm_unreachable("Unknown OpenMP directive"); 13744 } 13745 break; 13746 case OMPC_num_teams: 13747 switch (DKind) { 13748 case OMPD_target_teams: 13749 case OMPD_target_teams_distribute: 13750 case OMPD_target_teams_distribute_simd: 13751 case OMPD_target_teams_distribute_parallel_for: 13752 case OMPD_target_teams_distribute_parallel_for_simd: 13753 CaptureRegion = OMPD_target; 13754 break; 13755 case OMPD_teams_distribute_parallel_for: 13756 case OMPD_teams_distribute_parallel_for_simd: 13757 case OMPD_teams: 13758 case OMPD_teams_distribute: 13759 case OMPD_teams_distribute_simd: 13760 // Do not capture num_teams-clause expressions. 13761 break; 13762 case OMPD_distribute_parallel_for: 13763 case OMPD_distribute_parallel_for_simd: 13764 case OMPD_task: 13765 case OMPD_taskloop: 13766 case OMPD_taskloop_simd: 13767 case OMPD_master_taskloop: 13768 case OMPD_master_taskloop_simd: 13769 case OMPD_parallel_master_taskloop: 13770 case OMPD_parallel_master_taskloop_simd: 13771 case OMPD_target_data: 13772 case OMPD_target_enter_data: 13773 case OMPD_target_exit_data: 13774 case OMPD_target_update: 13775 case OMPD_cancel: 13776 case OMPD_parallel: 13777 case OMPD_parallel_master: 13778 case OMPD_parallel_sections: 13779 case OMPD_parallel_for: 13780 case OMPD_parallel_for_simd: 13781 case OMPD_target: 13782 case OMPD_target_simd: 13783 case OMPD_target_parallel: 13784 case OMPD_target_parallel_for: 13785 case OMPD_target_parallel_for_simd: 13786 case OMPD_threadprivate: 13787 case OMPD_allocate: 13788 case OMPD_taskyield: 13789 case OMPD_barrier: 13790 case OMPD_taskwait: 13791 case OMPD_cancellation_point: 13792 case OMPD_flush: 13793 case OMPD_depobj: 13794 case OMPD_scan: 13795 case OMPD_declare_reduction: 13796 case OMPD_declare_mapper: 13797 case OMPD_declare_simd: 13798 case OMPD_declare_variant: 13799 case OMPD_begin_declare_variant: 13800 case OMPD_end_declare_variant: 13801 case OMPD_declare_target: 13802 case OMPD_end_declare_target: 13803 case OMPD_loop: 13804 case OMPD_simd: 13805 case OMPD_tile: 13806 case OMPD_unroll: 13807 case OMPD_for: 13808 case OMPD_for_simd: 13809 case OMPD_sections: 13810 case OMPD_section: 13811 case OMPD_single: 13812 case OMPD_master: 13813 case OMPD_masked: 13814 case OMPD_critical: 13815 case OMPD_taskgroup: 13816 case OMPD_distribute: 13817 case OMPD_ordered: 13818 case OMPD_atomic: 13819 case OMPD_distribute_simd: 13820 case OMPD_requires: 13821 case OMPD_metadirective: 13822 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 13823 case OMPD_unknown: 13824 default: 13825 llvm_unreachable("Unknown OpenMP directive"); 13826 } 13827 break; 13828 case OMPC_thread_limit: 13829 switch (DKind) { 13830 case OMPD_target_teams: 13831 case OMPD_target_teams_distribute: 13832 case OMPD_target_teams_distribute_simd: 13833 case OMPD_target_teams_distribute_parallel_for: 13834 case OMPD_target_teams_distribute_parallel_for_simd: 13835 CaptureRegion = OMPD_target; 13836 break; 13837 case OMPD_teams_distribute_parallel_for: 13838 case OMPD_teams_distribute_parallel_for_simd: 13839 case OMPD_teams: 13840 case OMPD_teams_distribute: 13841 case OMPD_teams_distribute_simd: 13842 // Do not capture thread_limit-clause expressions. 13843 break; 13844 case OMPD_distribute_parallel_for: 13845 case OMPD_distribute_parallel_for_simd: 13846 case OMPD_task: 13847 case OMPD_taskloop: 13848 case OMPD_taskloop_simd: 13849 case OMPD_master_taskloop: 13850 case OMPD_master_taskloop_simd: 13851 case OMPD_parallel_master_taskloop: 13852 case OMPD_parallel_master_taskloop_simd: 13853 case OMPD_target_data: 13854 case OMPD_target_enter_data: 13855 case OMPD_target_exit_data: 13856 case OMPD_target_update: 13857 case OMPD_cancel: 13858 case OMPD_parallel: 13859 case OMPD_parallel_master: 13860 case OMPD_parallel_sections: 13861 case OMPD_parallel_for: 13862 case OMPD_parallel_for_simd: 13863 case OMPD_target: 13864 case OMPD_target_simd: 13865 case OMPD_target_parallel: 13866 case OMPD_target_parallel_for: 13867 case OMPD_target_parallel_for_simd: 13868 case OMPD_threadprivate: 13869 case OMPD_allocate: 13870 case OMPD_taskyield: 13871 case OMPD_barrier: 13872 case OMPD_taskwait: 13873 case OMPD_cancellation_point: 13874 case OMPD_flush: 13875 case OMPD_depobj: 13876 case OMPD_scan: 13877 case OMPD_declare_reduction: 13878 case OMPD_declare_mapper: 13879 case OMPD_declare_simd: 13880 case OMPD_declare_variant: 13881 case OMPD_begin_declare_variant: 13882 case OMPD_end_declare_variant: 13883 case OMPD_declare_target: 13884 case OMPD_end_declare_target: 13885 case OMPD_loop: 13886 case OMPD_simd: 13887 case OMPD_tile: 13888 case OMPD_unroll: 13889 case OMPD_for: 13890 case OMPD_for_simd: 13891 case OMPD_sections: 13892 case OMPD_section: 13893 case OMPD_single: 13894 case OMPD_master: 13895 case OMPD_masked: 13896 case OMPD_critical: 13897 case OMPD_taskgroup: 13898 case OMPD_distribute: 13899 case OMPD_ordered: 13900 case OMPD_atomic: 13901 case OMPD_distribute_simd: 13902 case OMPD_requires: 13903 case OMPD_metadirective: 13904 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 13905 case OMPD_unknown: 13906 default: 13907 llvm_unreachable("Unknown OpenMP directive"); 13908 } 13909 break; 13910 case OMPC_schedule: 13911 switch (DKind) { 13912 case OMPD_parallel_for: 13913 case OMPD_parallel_for_simd: 13914 case OMPD_distribute_parallel_for: 13915 case OMPD_distribute_parallel_for_simd: 13916 case OMPD_teams_distribute_parallel_for: 13917 case OMPD_teams_distribute_parallel_for_simd: 13918 case OMPD_target_parallel_for: 13919 case OMPD_target_parallel_for_simd: 13920 case OMPD_target_teams_distribute_parallel_for: 13921 case OMPD_target_teams_distribute_parallel_for_simd: 13922 CaptureRegion = OMPD_parallel; 13923 break; 13924 case OMPD_for: 13925 case OMPD_for_simd: 13926 // Do not capture schedule-clause expressions. 13927 break; 13928 case OMPD_task: 13929 case OMPD_taskloop: 13930 case OMPD_taskloop_simd: 13931 case OMPD_master_taskloop: 13932 case OMPD_master_taskloop_simd: 13933 case OMPD_parallel_master_taskloop: 13934 case OMPD_parallel_master_taskloop_simd: 13935 case OMPD_target_data: 13936 case OMPD_target_enter_data: 13937 case OMPD_target_exit_data: 13938 case OMPD_target_update: 13939 case OMPD_teams: 13940 case OMPD_teams_distribute: 13941 case OMPD_teams_distribute_simd: 13942 case OMPD_target_teams_distribute: 13943 case OMPD_target_teams_distribute_simd: 13944 case OMPD_target: 13945 case OMPD_target_simd: 13946 case OMPD_target_parallel: 13947 case OMPD_cancel: 13948 case OMPD_parallel: 13949 case OMPD_parallel_master: 13950 case OMPD_parallel_sections: 13951 case OMPD_threadprivate: 13952 case OMPD_allocate: 13953 case OMPD_taskyield: 13954 case OMPD_barrier: 13955 case OMPD_taskwait: 13956 case OMPD_cancellation_point: 13957 case OMPD_flush: 13958 case OMPD_depobj: 13959 case OMPD_scan: 13960 case OMPD_declare_reduction: 13961 case OMPD_declare_mapper: 13962 case OMPD_declare_simd: 13963 case OMPD_declare_variant: 13964 case OMPD_begin_declare_variant: 13965 case OMPD_end_declare_variant: 13966 case OMPD_declare_target: 13967 case OMPD_end_declare_target: 13968 case OMPD_loop: 13969 case OMPD_simd: 13970 case OMPD_tile: 13971 case OMPD_unroll: 13972 case OMPD_sections: 13973 case OMPD_section: 13974 case OMPD_single: 13975 case OMPD_master: 13976 case OMPD_masked: 13977 case OMPD_critical: 13978 case OMPD_taskgroup: 13979 case OMPD_distribute: 13980 case OMPD_ordered: 13981 case OMPD_atomic: 13982 case OMPD_distribute_simd: 13983 case OMPD_target_teams: 13984 case OMPD_requires: 13985 case OMPD_metadirective: 13986 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 13987 case OMPD_unknown: 13988 default: 13989 llvm_unreachable("Unknown OpenMP directive"); 13990 } 13991 break; 13992 case OMPC_dist_schedule: 13993 switch (DKind) { 13994 case OMPD_teams_distribute_parallel_for: 13995 case OMPD_teams_distribute_parallel_for_simd: 13996 case OMPD_teams_distribute: 13997 case OMPD_teams_distribute_simd: 13998 case OMPD_target_teams_distribute_parallel_for: 13999 case OMPD_target_teams_distribute_parallel_for_simd: 14000 case OMPD_target_teams_distribute: 14001 case OMPD_target_teams_distribute_simd: 14002 CaptureRegion = OMPD_teams; 14003 break; 14004 case OMPD_distribute_parallel_for: 14005 case OMPD_distribute_parallel_for_simd: 14006 case OMPD_distribute: 14007 case OMPD_distribute_simd: 14008 // Do not capture dist_schedule-clause expressions. 14009 break; 14010 case OMPD_parallel_for: 14011 case OMPD_parallel_for_simd: 14012 case OMPD_target_parallel_for_simd: 14013 case OMPD_target_parallel_for: 14014 case OMPD_task: 14015 case OMPD_taskloop: 14016 case OMPD_taskloop_simd: 14017 case OMPD_master_taskloop: 14018 case OMPD_master_taskloop_simd: 14019 case OMPD_parallel_master_taskloop: 14020 case OMPD_parallel_master_taskloop_simd: 14021 case OMPD_target_data: 14022 case OMPD_target_enter_data: 14023 case OMPD_target_exit_data: 14024 case OMPD_target_update: 14025 case OMPD_teams: 14026 case OMPD_target: 14027 case OMPD_target_simd: 14028 case OMPD_target_parallel: 14029 case OMPD_cancel: 14030 case OMPD_parallel: 14031 case OMPD_parallel_master: 14032 case OMPD_parallel_sections: 14033 case OMPD_threadprivate: 14034 case OMPD_allocate: 14035 case OMPD_taskyield: 14036 case OMPD_barrier: 14037 case OMPD_taskwait: 14038 case OMPD_cancellation_point: 14039 case OMPD_flush: 14040 case OMPD_depobj: 14041 case OMPD_scan: 14042 case OMPD_declare_reduction: 14043 case OMPD_declare_mapper: 14044 case OMPD_declare_simd: 14045 case OMPD_declare_variant: 14046 case OMPD_begin_declare_variant: 14047 case OMPD_end_declare_variant: 14048 case OMPD_declare_target: 14049 case OMPD_end_declare_target: 14050 case OMPD_loop: 14051 case OMPD_simd: 14052 case OMPD_tile: 14053 case OMPD_unroll: 14054 case OMPD_for: 14055 case OMPD_for_simd: 14056 case OMPD_sections: 14057 case OMPD_section: 14058 case OMPD_single: 14059 case OMPD_master: 14060 case OMPD_masked: 14061 case OMPD_critical: 14062 case OMPD_taskgroup: 14063 case OMPD_ordered: 14064 case OMPD_atomic: 14065 case OMPD_target_teams: 14066 case OMPD_requires: 14067 case OMPD_metadirective: 14068 llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause"); 14069 case OMPD_unknown: 14070 default: 14071 llvm_unreachable("Unknown OpenMP directive"); 14072 } 14073 break; 14074 case OMPC_device: 14075 switch (DKind) { 14076 case OMPD_target_update: 14077 case OMPD_target_enter_data: 14078 case OMPD_target_exit_data: 14079 case OMPD_target: 14080 case OMPD_target_simd: 14081 case OMPD_target_teams: 14082 case OMPD_target_parallel: 14083 case OMPD_target_teams_distribute: 14084 case OMPD_target_teams_distribute_simd: 14085 case OMPD_target_parallel_for: 14086 case OMPD_target_parallel_for_simd: 14087 case OMPD_target_teams_distribute_parallel_for: 14088 case OMPD_target_teams_distribute_parallel_for_simd: 14089 case OMPD_dispatch: 14090 CaptureRegion = OMPD_task; 14091 break; 14092 case OMPD_target_data: 14093 case OMPD_interop: 14094 // Do not capture device-clause expressions. 14095 break; 14096 case OMPD_teams_distribute_parallel_for: 14097 case OMPD_teams_distribute_parallel_for_simd: 14098 case OMPD_teams: 14099 case OMPD_teams_distribute: 14100 case OMPD_teams_distribute_simd: 14101 case OMPD_distribute_parallel_for: 14102 case OMPD_distribute_parallel_for_simd: 14103 case OMPD_task: 14104 case OMPD_taskloop: 14105 case OMPD_taskloop_simd: 14106 case OMPD_master_taskloop: 14107 case OMPD_master_taskloop_simd: 14108 case OMPD_parallel_master_taskloop: 14109 case OMPD_parallel_master_taskloop_simd: 14110 case OMPD_cancel: 14111 case OMPD_parallel: 14112 case OMPD_parallel_master: 14113 case OMPD_parallel_sections: 14114 case OMPD_parallel_for: 14115 case OMPD_parallel_for_simd: 14116 case OMPD_threadprivate: 14117 case OMPD_allocate: 14118 case OMPD_taskyield: 14119 case OMPD_barrier: 14120 case OMPD_taskwait: 14121 case OMPD_cancellation_point: 14122 case OMPD_flush: 14123 case OMPD_depobj: 14124 case OMPD_scan: 14125 case OMPD_declare_reduction: 14126 case OMPD_declare_mapper: 14127 case OMPD_declare_simd: 14128 case OMPD_declare_variant: 14129 case OMPD_begin_declare_variant: 14130 case OMPD_end_declare_variant: 14131 case OMPD_declare_target: 14132 case OMPD_end_declare_target: 14133 case OMPD_loop: 14134 case OMPD_simd: 14135 case OMPD_tile: 14136 case OMPD_unroll: 14137 case OMPD_for: 14138 case OMPD_for_simd: 14139 case OMPD_sections: 14140 case OMPD_section: 14141 case OMPD_single: 14142 case OMPD_master: 14143 case OMPD_masked: 14144 case OMPD_critical: 14145 case OMPD_taskgroup: 14146 case OMPD_distribute: 14147 case OMPD_ordered: 14148 case OMPD_atomic: 14149 case OMPD_distribute_simd: 14150 case OMPD_requires: 14151 case OMPD_metadirective: 14152 llvm_unreachable("Unexpected OpenMP directive with device-clause"); 14153 case OMPD_unknown: 14154 default: 14155 llvm_unreachable("Unknown OpenMP directive"); 14156 } 14157 break; 14158 case OMPC_grainsize: 14159 case OMPC_num_tasks: 14160 case OMPC_final: 14161 case OMPC_priority: 14162 switch (DKind) { 14163 case OMPD_task: 14164 case OMPD_taskloop: 14165 case OMPD_taskloop_simd: 14166 case OMPD_master_taskloop: 14167 case OMPD_master_taskloop_simd: 14168 break; 14169 case OMPD_parallel_master_taskloop: 14170 case OMPD_parallel_master_taskloop_simd: 14171 CaptureRegion = OMPD_parallel; 14172 break; 14173 case OMPD_target_update: 14174 case OMPD_target_enter_data: 14175 case OMPD_target_exit_data: 14176 case OMPD_target: 14177 case OMPD_target_simd: 14178 case OMPD_target_teams: 14179 case OMPD_target_parallel: 14180 case OMPD_target_teams_distribute: 14181 case OMPD_target_teams_distribute_simd: 14182 case OMPD_target_parallel_for: 14183 case OMPD_target_parallel_for_simd: 14184 case OMPD_target_teams_distribute_parallel_for: 14185 case OMPD_target_teams_distribute_parallel_for_simd: 14186 case OMPD_target_data: 14187 case OMPD_teams_distribute_parallel_for: 14188 case OMPD_teams_distribute_parallel_for_simd: 14189 case OMPD_teams: 14190 case OMPD_teams_distribute: 14191 case OMPD_teams_distribute_simd: 14192 case OMPD_distribute_parallel_for: 14193 case OMPD_distribute_parallel_for_simd: 14194 case OMPD_cancel: 14195 case OMPD_parallel: 14196 case OMPD_parallel_master: 14197 case OMPD_parallel_sections: 14198 case OMPD_parallel_for: 14199 case OMPD_parallel_for_simd: 14200 case OMPD_threadprivate: 14201 case OMPD_allocate: 14202 case OMPD_taskyield: 14203 case OMPD_barrier: 14204 case OMPD_taskwait: 14205 case OMPD_cancellation_point: 14206 case OMPD_flush: 14207 case OMPD_depobj: 14208 case OMPD_scan: 14209 case OMPD_declare_reduction: 14210 case OMPD_declare_mapper: 14211 case OMPD_declare_simd: 14212 case OMPD_declare_variant: 14213 case OMPD_begin_declare_variant: 14214 case OMPD_end_declare_variant: 14215 case OMPD_declare_target: 14216 case OMPD_end_declare_target: 14217 case OMPD_loop: 14218 case OMPD_simd: 14219 case OMPD_tile: 14220 case OMPD_unroll: 14221 case OMPD_for: 14222 case OMPD_for_simd: 14223 case OMPD_sections: 14224 case OMPD_section: 14225 case OMPD_single: 14226 case OMPD_master: 14227 case OMPD_masked: 14228 case OMPD_critical: 14229 case OMPD_taskgroup: 14230 case OMPD_distribute: 14231 case OMPD_ordered: 14232 case OMPD_atomic: 14233 case OMPD_distribute_simd: 14234 case OMPD_requires: 14235 case OMPD_metadirective: 14236 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 14237 case OMPD_unknown: 14238 default: 14239 llvm_unreachable("Unknown OpenMP directive"); 14240 } 14241 break; 14242 case OMPC_novariants: 14243 case OMPC_nocontext: 14244 switch (DKind) { 14245 case OMPD_dispatch: 14246 CaptureRegion = OMPD_task; 14247 break; 14248 default: 14249 llvm_unreachable("Unexpected OpenMP directive"); 14250 } 14251 break; 14252 case OMPC_filter: 14253 // Do not capture filter-clause expressions. 14254 break; 14255 case OMPC_when: 14256 if (DKind == OMPD_metadirective) { 14257 CaptureRegion = OMPD_metadirective; 14258 } else if (DKind == OMPD_unknown) { 14259 llvm_unreachable("Unknown OpenMP directive"); 14260 } else { 14261 llvm_unreachable("Unexpected OpenMP directive with when clause"); 14262 } 14263 break; 14264 case OMPC_firstprivate: 14265 case OMPC_lastprivate: 14266 case OMPC_reduction: 14267 case OMPC_task_reduction: 14268 case OMPC_in_reduction: 14269 case OMPC_linear: 14270 case OMPC_default: 14271 case OMPC_proc_bind: 14272 case OMPC_safelen: 14273 case OMPC_simdlen: 14274 case OMPC_sizes: 14275 case OMPC_allocator: 14276 case OMPC_collapse: 14277 case OMPC_private: 14278 case OMPC_shared: 14279 case OMPC_aligned: 14280 case OMPC_copyin: 14281 case OMPC_copyprivate: 14282 case OMPC_ordered: 14283 case OMPC_nowait: 14284 case OMPC_untied: 14285 case OMPC_mergeable: 14286 case OMPC_threadprivate: 14287 case OMPC_allocate: 14288 case OMPC_flush: 14289 case OMPC_depobj: 14290 case OMPC_read: 14291 case OMPC_write: 14292 case OMPC_update: 14293 case OMPC_capture: 14294 case OMPC_seq_cst: 14295 case OMPC_acq_rel: 14296 case OMPC_acquire: 14297 case OMPC_release: 14298 case OMPC_relaxed: 14299 case OMPC_depend: 14300 case OMPC_threads: 14301 case OMPC_simd: 14302 case OMPC_map: 14303 case OMPC_nogroup: 14304 case OMPC_hint: 14305 case OMPC_defaultmap: 14306 case OMPC_unknown: 14307 case OMPC_uniform: 14308 case OMPC_to: 14309 case OMPC_from: 14310 case OMPC_use_device_ptr: 14311 case OMPC_use_device_addr: 14312 case OMPC_is_device_ptr: 14313 case OMPC_unified_address: 14314 case OMPC_unified_shared_memory: 14315 case OMPC_reverse_offload: 14316 case OMPC_dynamic_allocators: 14317 case OMPC_atomic_default_mem_order: 14318 case OMPC_device_type: 14319 case OMPC_match: 14320 case OMPC_nontemporal: 14321 case OMPC_order: 14322 case OMPC_destroy: 14323 case OMPC_detach: 14324 case OMPC_inclusive: 14325 case OMPC_exclusive: 14326 case OMPC_uses_allocators: 14327 case OMPC_affinity: 14328 case OMPC_bind: 14329 default: 14330 llvm_unreachable("Unexpected OpenMP clause."); 14331 } 14332 return CaptureRegion; 14333 } 14334 14335 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 14336 Expr *Condition, SourceLocation StartLoc, 14337 SourceLocation LParenLoc, 14338 SourceLocation NameModifierLoc, 14339 SourceLocation ColonLoc, 14340 SourceLocation EndLoc) { 14341 Expr *ValExpr = Condition; 14342 Stmt *HelperValStmt = nullptr; 14343 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14344 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14345 !Condition->isInstantiationDependent() && 14346 !Condition->containsUnexpandedParameterPack()) { 14347 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14348 if (Val.isInvalid()) 14349 return nullptr; 14350 14351 ValExpr = Val.get(); 14352 14353 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14354 CaptureRegion = getOpenMPCaptureRegionForClause( 14355 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 14356 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14357 ValExpr = MakeFullExpr(ValExpr).get(); 14358 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14359 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14360 HelperValStmt = buildPreInits(Context, Captures); 14361 } 14362 } 14363 14364 return new (Context) 14365 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 14366 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 14367 } 14368 14369 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 14370 SourceLocation StartLoc, 14371 SourceLocation LParenLoc, 14372 SourceLocation EndLoc) { 14373 Expr *ValExpr = Condition; 14374 Stmt *HelperValStmt = nullptr; 14375 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14376 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14377 !Condition->isInstantiationDependent() && 14378 !Condition->containsUnexpandedParameterPack()) { 14379 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14380 if (Val.isInvalid()) 14381 return nullptr; 14382 14383 ValExpr = MakeFullExpr(Val.get()).get(); 14384 14385 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14386 CaptureRegion = 14387 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 14388 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14389 ValExpr = MakeFullExpr(ValExpr).get(); 14390 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14391 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14392 HelperValStmt = buildPreInits(Context, Captures); 14393 } 14394 } 14395 14396 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 14397 StartLoc, LParenLoc, EndLoc); 14398 } 14399 14400 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 14401 Expr *Op) { 14402 if (!Op) 14403 return ExprError(); 14404 14405 class IntConvertDiagnoser : public ICEConvertDiagnoser { 14406 public: 14407 IntConvertDiagnoser() 14408 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 14409 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 14410 QualType T) override { 14411 return S.Diag(Loc, diag::err_omp_not_integral) << T; 14412 } 14413 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 14414 QualType T) override { 14415 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 14416 } 14417 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 14418 QualType T, 14419 QualType ConvTy) override { 14420 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 14421 } 14422 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 14423 QualType ConvTy) override { 14424 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14425 << ConvTy->isEnumeralType() << ConvTy; 14426 } 14427 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 14428 QualType T) override { 14429 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 14430 } 14431 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 14432 QualType ConvTy) override { 14433 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14434 << ConvTy->isEnumeralType() << ConvTy; 14435 } 14436 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 14437 QualType) override { 14438 llvm_unreachable("conversion functions are permitted"); 14439 } 14440 } ConvertDiagnoser; 14441 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 14442 } 14443 14444 static bool 14445 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 14446 bool StrictlyPositive, bool BuildCapture = false, 14447 OpenMPDirectiveKind DKind = OMPD_unknown, 14448 OpenMPDirectiveKind *CaptureRegion = nullptr, 14449 Stmt **HelperValStmt = nullptr) { 14450 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 14451 !ValExpr->isInstantiationDependent()) { 14452 SourceLocation Loc = ValExpr->getExprLoc(); 14453 ExprResult Value = 14454 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 14455 if (Value.isInvalid()) 14456 return false; 14457 14458 ValExpr = Value.get(); 14459 // The expression must evaluate to a non-negative integer value. 14460 if (Optional<llvm::APSInt> Result = 14461 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 14462 if (Result->isSigned() && 14463 !((!StrictlyPositive && Result->isNonNegative()) || 14464 (StrictlyPositive && Result->isStrictlyPositive()))) { 14465 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 14466 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14467 << ValExpr->getSourceRange(); 14468 return false; 14469 } 14470 } 14471 if (!BuildCapture) 14472 return true; 14473 *CaptureRegion = 14474 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 14475 if (*CaptureRegion != OMPD_unknown && 14476 !SemaRef.CurContext->isDependentContext()) { 14477 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 14478 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14479 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 14480 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 14481 } 14482 } 14483 return true; 14484 } 14485 14486 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 14487 SourceLocation StartLoc, 14488 SourceLocation LParenLoc, 14489 SourceLocation EndLoc) { 14490 Expr *ValExpr = NumThreads; 14491 Stmt *HelperValStmt = nullptr; 14492 14493 // OpenMP [2.5, Restrictions] 14494 // The num_threads expression must evaluate to a positive integer value. 14495 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 14496 /*StrictlyPositive=*/true)) 14497 return nullptr; 14498 14499 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14500 OpenMPDirectiveKind CaptureRegion = 14501 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 14502 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14503 ValExpr = MakeFullExpr(ValExpr).get(); 14504 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14505 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14506 HelperValStmt = buildPreInits(Context, Captures); 14507 } 14508 14509 return new (Context) OMPNumThreadsClause( 14510 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14511 } 14512 14513 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 14514 OpenMPClauseKind CKind, 14515 bool StrictlyPositive, 14516 bool SuppressExprDiags) { 14517 if (!E) 14518 return ExprError(); 14519 if (E->isValueDependent() || E->isTypeDependent() || 14520 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 14521 return E; 14522 14523 llvm::APSInt Result; 14524 ExprResult ICE; 14525 if (SuppressExprDiags) { 14526 // Use a custom diagnoser that suppresses 'note' diagnostics about the 14527 // expression. 14528 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser { 14529 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {} 14530 Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, 14531 SourceLocation Loc) override { 14532 llvm_unreachable("Diagnostic suppressed"); 14533 } 14534 } Diagnoser; 14535 ICE = VerifyIntegerConstantExpression(E, &Result, Diagnoser, AllowFold); 14536 } else { 14537 ICE = VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 14538 } 14539 if (ICE.isInvalid()) 14540 return ExprError(); 14541 14542 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 14543 (!StrictlyPositive && !Result.isNonNegative())) { 14544 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 14545 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14546 << E->getSourceRange(); 14547 return ExprError(); 14548 } 14549 if ((CKind == OMPC_aligned || CKind == OMPC_align) && !Result.isPowerOf2()) { 14550 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 14551 << E->getSourceRange(); 14552 return ExprError(); 14553 } 14554 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 14555 DSAStack->setAssociatedLoops(Result.getExtValue()); 14556 else if (CKind == OMPC_ordered) 14557 DSAStack->setAssociatedLoops(Result.getExtValue()); 14558 return ICE; 14559 } 14560 14561 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 14562 SourceLocation LParenLoc, 14563 SourceLocation EndLoc) { 14564 // OpenMP [2.8.1, simd construct, Description] 14565 // The parameter of the safelen clause must be a constant 14566 // positive integer expression. 14567 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 14568 if (Safelen.isInvalid()) 14569 return nullptr; 14570 return new (Context) 14571 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 14572 } 14573 14574 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 14575 SourceLocation LParenLoc, 14576 SourceLocation EndLoc) { 14577 // OpenMP [2.8.1, simd construct, Description] 14578 // The parameter of the simdlen clause must be a constant 14579 // positive integer expression. 14580 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 14581 if (Simdlen.isInvalid()) 14582 return nullptr; 14583 return new (Context) 14584 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 14585 } 14586 14587 /// Tries to find omp_allocator_handle_t type. 14588 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 14589 DSAStackTy *Stack) { 14590 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 14591 if (!OMPAllocatorHandleT.isNull()) 14592 return true; 14593 // Build the predefined allocator expressions. 14594 bool ErrorFound = false; 14595 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 14596 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 14597 StringRef Allocator = 14598 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 14599 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 14600 auto *VD = dyn_cast_or_null<ValueDecl>( 14601 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 14602 if (!VD) { 14603 ErrorFound = true; 14604 break; 14605 } 14606 QualType AllocatorType = 14607 VD->getType().getNonLValueExprType(S.getASTContext()); 14608 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 14609 if (!Res.isUsable()) { 14610 ErrorFound = true; 14611 break; 14612 } 14613 if (OMPAllocatorHandleT.isNull()) 14614 OMPAllocatorHandleT = AllocatorType; 14615 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 14616 ErrorFound = true; 14617 break; 14618 } 14619 Stack->setAllocator(AllocatorKind, Res.get()); 14620 } 14621 if (ErrorFound) { 14622 S.Diag(Loc, diag::err_omp_implied_type_not_found) 14623 << "omp_allocator_handle_t"; 14624 return false; 14625 } 14626 OMPAllocatorHandleT.addConst(); 14627 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 14628 return true; 14629 } 14630 14631 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 14632 SourceLocation LParenLoc, 14633 SourceLocation EndLoc) { 14634 // OpenMP [2.11.3, allocate Directive, Description] 14635 // allocator is an expression of omp_allocator_handle_t type. 14636 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 14637 return nullptr; 14638 14639 ExprResult Allocator = DefaultLvalueConversion(A); 14640 if (Allocator.isInvalid()) 14641 return nullptr; 14642 Allocator = PerformImplicitConversion(Allocator.get(), 14643 DSAStack->getOMPAllocatorHandleT(), 14644 Sema::AA_Initializing, 14645 /*AllowExplicit=*/true); 14646 if (Allocator.isInvalid()) 14647 return nullptr; 14648 return new (Context) 14649 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 14650 } 14651 14652 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 14653 SourceLocation StartLoc, 14654 SourceLocation LParenLoc, 14655 SourceLocation EndLoc) { 14656 // OpenMP [2.7.1, loop construct, Description] 14657 // OpenMP [2.8.1, simd construct, Description] 14658 // OpenMP [2.9.6, distribute construct, Description] 14659 // The parameter of the collapse clause must be a constant 14660 // positive integer expression. 14661 ExprResult NumForLoopsResult = 14662 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 14663 if (NumForLoopsResult.isInvalid()) 14664 return nullptr; 14665 return new (Context) 14666 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 14667 } 14668 14669 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 14670 SourceLocation EndLoc, 14671 SourceLocation LParenLoc, 14672 Expr *NumForLoops) { 14673 // OpenMP [2.7.1, loop construct, Description] 14674 // OpenMP [2.8.1, simd construct, Description] 14675 // OpenMP [2.9.6, distribute construct, Description] 14676 // The parameter of the ordered clause must be a constant 14677 // positive integer expression if any. 14678 if (NumForLoops && LParenLoc.isValid()) { 14679 ExprResult NumForLoopsResult = 14680 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 14681 if (NumForLoopsResult.isInvalid()) 14682 return nullptr; 14683 NumForLoops = NumForLoopsResult.get(); 14684 } else { 14685 NumForLoops = nullptr; 14686 } 14687 auto *Clause = OMPOrderedClause::Create( 14688 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 14689 StartLoc, LParenLoc, EndLoc); 14690 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 14691 return Clause; 14692 } 14693 14694 OMPClause *Sema::ActOnOpenMPSimpleClause( 14695 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 14696 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14697 OMPClause *Res = nullptr; 14698 switch (Kind) { 14699 case OMPC_default: 14700 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 14701 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14702 break; 14703 case OMPC_proc_bind: 14704 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 14705 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14706 break; 14707 case OMPC_atomic_default_mem_order: 14708 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 14709 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 14710 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14711 break; 14712 case OMPC_order: 14713 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 14714 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14715 break; 14716 case OMPC_update: 14717 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 14718 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14719 break; 14720 case OMPC_bind: 14721 Res = ActOnOpenMPBindClause(static_cast<OpenMPBindClauseKind>(Argument), 14722 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14723 break; 14724 case OMPC_if: 14725 case OMPC_final: 14726 case OMPC_num_threads: 14727 case OMPC_safelen: 14728 case OMPC_simdlen: 14729 case OMPC_sizes: 14730 case OMPC_allocator: 14731 case OMPC_collapse: 14732 case OMPC_schedule: 14733 case OMPC_private: 14734 case OMPC_firstprivate: 14735 case OMPC_lastprivate: 14736 case OMPC_shared: 14737 case OMPC_reduction: 14738 case OMPC_task_reduction: 14739 case OMPC_in_reduction: 14740 case OMPC_linear: 14741 case OMPC_aligned: 14742 case OMPC_copyin: 14743 case OMPC_copyprivate: 14744 case OMPC_ordered: 14745 case OMPC_nowait: 14746 case OMPC_untied: 14747 case OMPC_mergeable: 14748 case OMPC_threadprivate: 14749 case OMPC_allocate: 14750 case OMPC_flush: 14751 case OMPC_depobj: 14752 case OMPC_read: 14753 case OMPC_write: 14754 case OMPC_capture: 14755 case OMPC_seq_cst: 14756 case OMPC_acq_rel: 14757 case OMPC_acquire: 14758 case OMPC_release: 14759 case OMPC_relaxed: 14760 case OMPC_depend: 14761 case OMPC_device: 14762 case OMPC_threads: 14763 case OMPC_simd: 14764 case OMPC_map: 14765 case OMPC_num_teams: 14766 case OMPC_thread_limit: 14767 case OMPC_priority: 14768 case OMPC_grainsize: 14769 case OMPC_nogroup: 14770 case OMPC_num_tasks: 14771 case OMPC_hint: 14772 case OMPC_dist_schedule: 14773 case OMPC_defaultmap: 14774 case OMPC_unknown: 14775 case OMPC_uniform: 14776 case OMPC_to: 14777 case OMPC_from: 14778 case OMPC_use_device_ptr: 14779 case OMPC_use_device_addr: 14780 case OMPC_is_device_ptr: 14781 case OMPC_unified_address: 14782 case OMPC_unified_shared_memory: 14783 case OMPC_reverse_offload: 14784 case OMPC_dynamic_allocators: 14785 case OMPC_device_type: 14786 case OMPC_match: 14787 case OMPC_nontemporal: 14788 case OMPC_destroy: 14789 case OMPC_novariants: 14790 case OMPC_nocontext: 14791 case OMPC_detach: 14792 case OMPC_inclusive: 14793 case OMPC_exclusive: 14794 case OMPC_uses_allocators: 14795 case OMPC_affinity: 14796 case OMPC_when: 14797 default: 14798 llvm_unreachable("Clause is not allowed."); 14799 } 14800 return Res; 14801 } 14802 14803 static std::string 14804 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 14805 ArrayRef<unsigned> Exclude = llvm::None) { 14806 SmallString<256> Buffer; 14807 llvm::raw_svector_ostream Out(Buffer); 14808 unsigned Skipped = Exclude.size(); 14809 auto S = Exclude.begin(), E = Exclude.end(); 14810 for (unsigned I = First; I < Last; ++I) { 14811 if (std::find(S, E, I) != E) { 14812 --Skipped; 14813 continue; 14814 } 14815 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 14816 if (I + Skipped + 2 == Last) 14817 Out << " or "; 14818 else if (I + Skipped + 1 != Last) 14819 Out << ", "; 14820 } 14821 return std::string(Out.str()); 14822 } 14823 14824 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 14825 SourceLocation KindKwLoc, 14826 SourceLocation StartLoc, 14827 SourceLocation LParenLoc, 14828 SourceLocation EndLoc) { 14829 if (Kind == OMP_DEFAULT_unknown) { 14830 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14831 << getListOfPossibleValues(OMPC_default, /*First=*/0, 14832 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 14833 << getOpenMPClauseName(OMPC_default); 14834 return nullptr; 14835 } 14836 14837 switch (Kind) { 14838 case OMP_DEFAULT_none: 14839 DSAStack->setDefaultDSANone(KindKwLoc); 14840 break; 14841 case OMP_DEFAULT_shared: 14842 DSAStack->setDefaultDSAShared(KindKwLoc); 14843 break; 14844 case OMP_DEFAULT_firstprivate: 14845 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 14846 break; 14847 default: 14848 llvm_unreachable("DSA unexpected in OpenMP default clause"); 14849 } 14850 14851 return new (Context) 14852 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14853 } 14854 14855 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 14856 SourceLocation KindKwLoc, 14857 SourceLocation StartLoc, 14858 SourceLocation LParenLoc, 14859 SourceLocation EndLoc) { 14860 if (Kind == OMP_PROC_BIND_unknown) { 14861 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14862 << getListOfPossibleValues(OMPC_proc_bind, 14863 /*First=*/unsigned(OMP_PROC_BIND_master), 14864 /*Last=*/ 14865 unsigned(LangOpts.OpenMP > 50 14866 ? OMP_PROC_BIND_primary 14867 : OMP_PROC_BIND_spread) + 14868 1) 14869 << getOpenMPClauseName(OMPC_proc_bind); 14870 return nullptr; 14871 } 14872 if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51) 14873 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14874 << getListOfPossibleValues(OMPC_proc_bind, 14875 /*First=*/unsigned(OMP_PROC_BIND_master), 14876 /*Last=*/ 14877 unsigned(OMP_PROC_BIND_spread) + 1) 14878 << getOpenMPClauseName(OMPC_proc_bind); 14879 return new (Context) 14880 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14881 } 14882 14883 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 14884 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 14885 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14886 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 14887 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14888 << getListOfPossibleValues( 14889 OMPC_atomic_default_mem_order, /*First=*/0, 14890 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 14891 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 14892 return nullptr; 14893 } 14894 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 14895 LParenLoc, EndLoc); 14896 } 14897 14898 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 14899 SourceLocation KindKwLoc, 14900 SourceLocation StartLoc, 14901 SourceLocation LParenLoc, 14902 SourceLocation EndLoc) { 14903 if (Kind == OMPC_ORDER_unknown) { 14904 static_assert(OMPC_ORDER_unknown > 0, 14905 "OMPC_ORDER_unknown not greater than 0"); 14906 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14907 << getListOfPossibleValues(OMPC_order, /*First=*/0, 14908 /*Last=*/OMPC_ORDER_unknown) 14909 << getOpenMPClauseName(OMPC_order); 14910 return nullptr; 14911 } 14912 return new (Context) 14913 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14914 } 14915 14916 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 14917 SourceLocation KindKwLoc, 14918 SourceLocation StartLoc, 14919 SourceLocation LParenLoc, 14920 SourceLocation EndLoc) { 14921 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 14922 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 14923 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 14924 OMPC_DEPEND_depobj}; 14925 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14926 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 14927 /*Last=*/OMPC_DEPEND_unknown, Except) 14928 << getOpenMPClauseName(OMPC_update); 14929 return nullptr; 14930 } 14931 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 14932 EndLoc); 14933 } 14934 14935 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs, 14936 SourceLocation StartLoc, 14937 SourceLocation LParenLoc, 14938 SourceLocation EndLoc) { 14939 for (Expr *SizeExpr : SizeExprs) { 14940 ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause( 14941 SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true); 14942 if (!NumForLoopsResult.isUsable()) 14943 return nullptr; 14944 } 14945 14946 DSAStack->setAssociatedLoops(SizeExprs.size()); 14947 return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14948 SizeExprs); 14949 } 14950 14951 OMPClause *Sema::ActOnOpenMPFullClause(SourceLocation StartLoc, 14952 SourceLocation EndLoc) { 14953 return OMPFullClause::Create(Context, StartLoc, EndLoc); 14954 } 14955 14956 OMPClause *Sema::ActOnOpenMPPartialClause(Expr *FactorExpr, 14957 SourceLocation StartLoc, 14958 SourceLocation LParenLoc, 14959 SourceLocation EndLoc) { 14960 if (FactorExpr) { 14961 // If an argument is specified, it must be a constant (or an unevaluated 14962 // template expression). 14963 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause( 14964 FactorExpr, OMPC_partial, /*StrictlyPositive=*/true); 14965 if (FactorResult.isInvalid()) 14966 return nullptr; 14967 FactorExpr = FactorResult.get(); 14968 } 14969 14970 return OMPPartialClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14971 FactorExpr); 14972 } 14973 14974 OMPClause *Sema::ActOnOpenMPAlignClause(Expr *A, SourceLocation StartLoc, 14975 SourceLocation LParenLoc, 14976 SourceLocation EndLoc) { 14977 ExprResult AlignVal; 14978 AlignVal = VerifyPositiveIntegerConstantInClause(A, OMPC_align); 14979 if (AlignVal.isInvalid()) 14980 return nullptr; 14981 return OMPAlignClause::Create(Context, AlignVal.get(), StartLoc, LParenLoc, 14982 EndLoc); 14983 } 14984 14985 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 14986 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 14987 SourceLocation StartLoc, SourceLocation LParenLoc, 14988 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 14989 SourceLocation EndLoc) { 14990 OMPClause *Res = nullptr; 14991 switch (Kind) { 14992 case OMPC_schedule: 14993 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 14994 assert(Argument.size() == NumberOfElements && 14995 ArgumentLoc.size() == NumberOfElements); 14996 Res = ActOnOpenMPScheduleClause( 14997 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 14998 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 14999 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 15000 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 15001 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 15002 break; 15003 case OMPC_if: 15004 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 15005 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 15006 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 15007 DelimLoc, EndLoc); 15008 break; 15009 case OMPC_dist_schedule: 15010 Res = ActOnOpenMPDistScheduleClause( 15011 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 15012 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 15013 break; 15014 case OMPC_defaultmap: 15015 enum { Modifier, DefaultmapKind }; 15016 Res = ActOnOpenMPDefaultmapClause( 15017 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 15018 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 15019 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 15020 EndLoc); 15021 break; 15022 case OMPC_device: 15023 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 15024 Res = ActOnOpenMPDeviceClause( 15025 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 15026 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 15027 break; 15028 case OMPC_final: 15029 case OMPC_num_threads: 15030 case OMPC_safelen: 15031 case OMPC_simdlen: 15032 case OMPC_sizes: 15033 case OMPC_allocator: 15034 case OMPC_collapse: 15035 case OMPC_default: 15036 case OMPC_proc_bind: 15037 case OMPC_private: 15038 case OMPC_firstprivate: 15039 case OMPC_lastprivate: 15040 case OMPC_shared: 15041 case OMPC_reduction: 15042 case OMPC_task_reduction: 15043 case OMPC_in_reduction: 15044 case OMPC_linear: 15045 case OMPC_aligned: 15046 case OMPC_copyin: 15047 case OMPC_copyprivate: 15048 case OMPC_ordered: 15049 case OMPC_nowait: 15050 case OMPC_untied: 15051 case OMPC_mergeable: 15052 case OMPC_threadprivate: 15053 case OMPC_allocate: 15054 case OMPC_flush: 15055 case OMPC_depobj: 15056 case OMPC_read: 15057 case OMPC_write: 15058 case OMPC_update: 15059 case OMPC_capture: 15060 case OMPC_seq_cst: 15061 case OMPC_acq_rel: 15062 case OMPC_acquire: 15063 case OMPC_release: 15064 case OMPC_relaxed: 15065 case OMPC_depend: 15066 case OMPC_threads: 15067 case OMPC_simd: 15068 case OMPC_map: 15069 case OMPC_num_teams: 15070 case OMPC_thread_limit: 15071 case OMPC_priority: 15072 case OMPC_grainsize: 15073 case OMPC_nogroup: 15074 case OMPC_num_tasks: 15075 case OMPC_hint: 15076 case OMPC_unknown: 15077 case OMPC_uniform: 15078 case OMPC_to: 15079 case OMPC_from: 15080 case OMPC_use_device_ptr: 15081 case OMPC_use_device_addr: 15082 case OMPC_is_device_ptr: 15083 case OMPC_unified_address: 15084 case OMPC_unified_shared_memory: 15085 case OMPC_reverse_offload: 15086 case OMPC_dynamic_allocators: 15087 case OMPC_atomic_default_mem_order: 15088 case OMPC_device_type: 15089 case OMPC_match: 15090 case OMPC_nontemporal: 15091 case OMPC_order: 15092 case OMPC_destroy: 15093 case OMPC_novariants: 15094 case OMPC_nocontext: 15095 case OMPC_detach: 15096 case OMPC_inclusive: 15097 case OMPC_exclusive: 15098 case OMPC_uses_allocators: 15099 case OMPC_affinity: 15100 case OMPC_when: 15101 case OMPC_bind: 15102 default: 15103 llvm_unreachable("Clause is not allowed."); 15104 } 15105 return Res; 15106 } 15107 15108 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 15109 OpenMPScheduleClauseModifier M2, 15110 SourceLocation M1Loc, SourceLocation M2Loc) { 15111 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 15112 SmallVector<unsigned, 2> Excluded; 15113 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 15114 Excluded.push_back(M2); 15115 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 15116 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 15117 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 15118 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 15119 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 15120 << getListOfPossibleValues(OMPC_schedule, 15121 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 15122 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 15123 Excluded) 15124 << getOpenMPClauseName(OMPC_schedule); 15125 return true; 15126 } 15127 return false; 15128 } 15129 15130 OMPClause *Sema::ActOnOpenMPScheduleClause( 15131 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 15132 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 15133 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 15134 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 15135 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 15136 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 15137 return nullptr; 15138 // OpenMP, 2.7.1, Loop Construct, Restrictions 15139 // Either the monotonic modifier or the nonmonotonic modifier can be specified 15140 // but not both. 15141 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 15142 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 15143 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 15144 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 15145 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 15146 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 15147 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 15148 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 15149 return nullptr; 15150 } 15151 if (Kind == OMPC_SCHEDULE_unknown) { 15152 std::string Values; 15153 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 15154 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 15155 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 15156 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 15157 Exclude); 15158 } else { 15159 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 15160 /*Last=*/OMPC_SCHEDULE_unknown); 15161 } 15162 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 15163 << Values << getOpenMPClauseName(OMPC_schedule); 15164 return nullptr; 15165 } 15166 // OpenMP, 2.7.1, Loop Construct, Restrictions 15167 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 15168 // schedule(guided). 15169 // OpenMP 5.0 does not have this restriction. 15170 if (LangOpts.OpenMP < 50 && 15171 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 15172 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 15173 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 15174 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 15175 diag::err_omp_schedule_nonmonotonic_static); 15176 return nullptr; 15177 } 15178 Expr *ValExpr = ChunkSize; 15179 Stmt *HelperValStmt = nullptr; 15180 if (ChunkSize) { 15181 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 15182 !ChunkSize->isInstantiationDependent() && 15183 !ChunkSize->containsUnexpandedParameterPack()) { 15184 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 15185 ExprResult Val = 15186 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 15187 if (Val.isInvalid()) 15188 return nullptr; 15189 15190 ValExpr = Val.get(); 15191 15192 // OpenMP [2.7.1, Restrictions] 15193 // chunk_size must be a loop invariant integer expression with a positive 15194 // value. 15195 if (Optional<llvm::APSInt> Result = 15196 ValExpr->getIntegerConstantExpr(Context)) { 15197 if (Result->isSigned() && !Result->isStrictlyPositive()) { 15198 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 15199 << "schedule" << 1 << ChunkSize->getSourceRange(); 15200 return nullptr; 15201 } 15202 } else if (getOpenMPCaptureRegionForClause( 15203 DSAStack->getCurrentDirective(), OMPC_schedule, 15204 LangOpts.OpenMP) != OMPD_unknown && 15205 !CurContext->isDependentContext()) { 15206 ValExpr = MakeFullExpr(ValExpr).get(); 15207 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15208 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15209 HelperValStmt = buildPreInits(Context, Captures); 15210 } 15211 } 15212 } 15213 15214 return new (Context) 15215 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 15216 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 15217 } 15218 15219 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 15220 SourceLocation StartLoc, 15221 SourceLocation EndLoc) { 15222 OMPClause *Res = nullptr; 15223 switch (Kind) { 15224 case OMPC_ordered: 15225 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 15226 break; 15227 case OMPC_nowait: 15228 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 15229 break; 15230 case OMPC_untied: 15231 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 15232 break; 15233 case OMPC_mergeable: 15234 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 15235 break; 15236 case OMPC_read: 15237 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 15238 break; 15239 case OMPC_write: 15240 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 15241 break; 15242 case OMPC_update: 15243 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 15244 break; 15245 case OMPC_capture: 15246 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 15247 break; 15248 case OMPC_seq_cst: 15249 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 15250 break; 15251 case OMPC_acq_rel: 15252 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 15253 break; 15254 case OMPC_acquire: 15255 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 15256 break; 15257 case OMPC_release: 15258 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 15259 break; 15260 case OMPC_relaxed: 15261 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 15262 break; 15263 case OMPC_threads: 15264 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 15265 break; 15266 case OMPC_simd: 15267 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 15268 break; 15269 case OMPC_nogroup: 15270 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 15271 break; 15272 case OMPC_unified_address: 15273 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 15274 break; 15275 case OMPC_unified_shared_memory: 15276 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15277 break; 15278 case OMPC_reverse_offload: 15279 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 15280 break; 15281 case OMPC_dynamic_allocators: 15282 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 15283 break; 15284 case OMPC_destroy: 15285 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc, 15286 /*LParenLoc=*/SourceLocation(), 15287 /*VarLoc=*/SourceLocation(), EndLoc); 15288 break; 15289 case OMPC_full: 15290 Res = ActOnOpenMPFullClause(StartLoc, EndLoc); 15291 break; 15292 case OMPC_partial: 15293 Res = ActOnOpenMPPartialClause(nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc); 15294 break; 15295 case OMPC_if: 15296 case OMPC_final: 15297 case OMPC_num_threads: 15298 case OMPC_safelen: 15299 case OMPC_simdlen: 15300 case OMPC_sizes: 15301 case OMPC_allocator: 15302 case OMPC_collapse: 15303 case OMPC_schedule: 15304 case OMPC_private: 15305 case OMPC_firstprivate: 15306 case OMPC_lastprivate: 15307 case OMPC_shared: 15308 case OMPC_reduction: 15309 case OMPC_task_reduction: 15310 case OMPC_in_reduction: 15311 case OMPC_linear: 15312 case OMPC_aligned: 15313 case OMPC_copyin: 15314 case OMPC_copyprivate: 15315 case OMPC_default: 15316 case OMPC_proc_bind: 15317 case OMPC_threadprivate: 15318 case OMPC_allocate: 15319 case OMPC_flush: 15320 case OMPC_depobj: 15321 case OMPC_depend: 15322 case OMPC_device: 15323 case OMPC_map: 15324 case OMPC_num_teams: 15325 case OMPC_thread_limit: 15326 case OMPC_priority: 15327 case OMPC_grainsize: 15328 case OMPC_num_tasks: 15329 case OMPC_hint: 15330 case OMPC_dist_schedule: 15331 case OMPC_defaultmap: 15332 case OMPC_unknown: 15333 case OMPC_uniform: 15334 case OMPC_to: 15335 case OMPC_from: 15336 case OMPC_use_device_ptr: 15337 case OMPC_use_device_addr: 15338 case OMPC_is_device_ptr: 15339 case OMPC_atomic_default_mem_order: 15340 case OMPC_device_type: 15341 case OMPC_match: 15342 case OMPC_nontemporal: 15343 case OMPC_order: 15344 case OMPC_novariants: 15345 case OMPC_nocontext: 15346 case OMPC_detach: 15347 case OMPC_inclusive: 15348 case OMPC_exclusive: 15349 case OMPC_uses_allocators: 15350 case OMPC_affinity: 15351 case OMPC_when: 15352 default: 15353 llvm_unreachable("Clause is not allowed."); 15354 } 15355 return Res; 15356 } 15357 15358 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 15359 SourceLocation EndLoc) { 15360 DSAStack->setNowaitRegion(); 15361 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 15362 } 15363 15364 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 15365 SourceLocation EndLoc) { 15366 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 15367 } 15368 15369 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 15370 SourceLocation EndLoc) { 15371 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 15372 } 15373 15374 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 15375 SourceLocation EndLoc) { 15376 return new (Context) OMPReadClause(StartLoc, EndLoc); 15377 } 15378 15379 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 15380 SourceLocation EndLoc) { 15381 return new (Context) OMPWriteClause(StartLoc, EndLoc); 15382 } 15383 15384 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 15385 SourceLocation EndLoc) { 15386 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 15387 } 15388 15389 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 15390 SourceLocation EndLoc) { 15391 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 15392 } 15393 15394 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 15395 SourceLocation EndLoc) { 15396 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 15397 } 15398 15399 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 15400 SourceLocation EndLoc) { 15401 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 15402 } 15403 15404 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 15405 SourceLocation EndLoc) { 15406 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 15407 } 15408 15409 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 15410 SourceLocation EndLoc) { 15411 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 15412 } 15413 15414 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 15415 SourceLocation EndLoc) { 15416 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 15417 } 15418 15419 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 15420 SourceLocation EndLoc) { 15421 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 15422 } 15423 15424 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 15425 SourceLocation EndLoc) { 15426 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 15427 } 15428 15429 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 15430 SourceLocation EndLoc) { 15431 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 15432 } 15433 15434 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 15435 SourceLocation EndLoc) { 15436 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 15437 } 15438 15439 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 15440 SourceLocation EndLoc) { 15441 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15442 } 15443 15444 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 15445 SourceLocation EndLoc) { 15446 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 15447 } 15448 15449 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 15450 SourceLocation EndLoc) { 15451 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 15452 } 15453 15454 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses, 15455 SourceLocation StartLoc, 15456 SourceLocation EndLoc) { 15457 15458 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15459 // At least one action-clause must appear on a directive. 15460 if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) { 15461 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'"; 15462 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 15463 << Expected << getOpenMPDirectiveName(OMPD_interop); 15464 return StmtError(); 15465 } 15466 15467 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15468 // A depend clause can only appear on the directive if a targetsync 15469 // interop-type is present or the interop-var was initialized with 15470 // the targetsync interop-type. 15471 15472 // If there is any 'init' clause diagnose if there is no 'init' clause with 15473 // interop-type of 'targetsync'. Cases involving other directives cannot be 15474 // diagnosed. 15475 const OMPDependClause *DependClause = nullptr; 15476 bool HasInitClause = false; 15477 bool IsTargetSync = false; 15478 for (const OMPClause *C : Clauses) { 15479 if (IsTargetSync) 15480 break; 15481 if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) { 15482 HasInitClause = true; 15483 if (InitClause->getIsTargetSync()) 15484 IsTargetSync = true; 15485 } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) { 15486 DependClause = DC; 15487 } 15488 } 15489 if (DependClause && HasInitClause && !IsTargetSync) { 15490 Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause); 15491 return StmtError(); 15492 } 15493 15494 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15495 // Each interop-var may be specified for at most one action-clause of each 15496 // interop construct. 15497 llvm::SmallPtrSet<const VarDecl *, 4> InteropVars; 15498 for (const OMPClause *C : Clauses) { 15499 OpenMPClauseKind ClauseKind = C->getClauseKind(); 15500 const DeclRefExpr *DRE = nullptr; 15501 SourceLocation VarLoc; 15502 15503 if (ClauseKind == OMPC_init) { 15504 const auto *IC = cast<OMPInitClause>(C); 15505 VarLoc = IC->getVarLoc(); 15506 DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar()); 15507 } else if (ClauseKind == OMPC_use) { 15508 const auto *UC = cast<OMPUseClause>(C); 15509 VarLoc = UC->getVarLoc(); 15510 DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar()); 15511 } else if (ClauseKind == OMPC_destroy) { 15512 const auto *DC = cast<OMPDestroyClause>(C); 15513 VarLoc = DC->getVarLoc(); 15514 DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar()); 15515 } 15516 15517 if (!DRE) 15518 continue; 15519 15520 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 15521 if (!InteropVars.insert(VD->getCanonicalDecl()).second) { 15522 Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD; 15523 return StmtError(); 15524 } 15525 } 15526 } 15527 15528 return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses); 15529 } 15530 15531 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr, 15532 SourceLocation VarLoc, 15533 OpenMPClauseKind Kind) { 15534 if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() || 15535 InteropVarExpr->isInstantiationDependent() || 15536 InteropVarExpr->containsUnexpandedParameterPack()) 15537 return true; 15538 15539 const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr); 15540 if (!DRE || !isa<VarDecl>(DRE->getDecl())) { 15541 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0; 15542 return false; 15543 } 15544 15545 // Interop variable should be of type omp_interop_t. 15546 bool HasError = false; 15547 QualType InteropType; 15548 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"), 15549 VarLoc, Sema::LookupOrdinaryName); 15550 if (SemaRef.LookupName(Result, SemaRef.getCurScope())) { 15551 NamedDecl *ND = Result.getFoundDecl(); 15552 if (const auto *TD = dyn_cast<TypeDecl>(ND)) { 15553 InteropType = QualType(TD->getTypeForDecl(), 0); 15554 } else { 15555 HasError = true; 15556 } 15557 } else { 15558 HasError = true; 15559 } 15560 15561 if (HasError) { 15562 SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found) 15563 << "omp_interop_t"; 15564 return false; 15565 } 15566 15567 QualType VarType = InteropVarExpr->getType().getUnqualifiedType(); 15568 if (!SemaRef.Context.hasSameType(InteropType, VarType)) { 15569 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type); 15570 return false; 15571 } 15572 15573 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15574 // The interop-var passed to init or destroy must be non-const. 15575 if ((Kind == OMPC_init || Kind == OMPC_destroy) && 15576 isConstNotMutableType(SemaRef, InteropVarExpr->getType())) { 15577 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) 15578 << /*non-const*/ 1; 15579 return false; 15580 } 15581 return true; 15582 } 15583 15584 OMPClause * 15585 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs, 15586 bool IsTarget, bool IsTargetSync, 15587 SourceLocation StartLoc, SourceLocation LParenLoc, 15588 SourceLocation VarLoc, SourceLocation EndLoc) { 15589 15590 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init)) 15591 return nullptr; 15592 15593 // Check prefer_type values. These foreign-runtime-id values are either 15594 // string literals or constant integral expressions. 15595 for (const Expr *E : PrefExprs) { 15596 if (E->isValueDependent() || E->isTypeDependent() || 15597 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 15598 continue; 15599 if (E->isIntegerConstantExpr(Context)) 15600 continue; 15601 if (isa<StringLiteral>(E)) 15602 continue; 15603 Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type); 15604 return nullptr; 15605 } 15606 15607 return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget, 15608 IsTargetSync, StartLoc, LParenLoc, VarLoc, 15609 EndLoc); 15610 } 15611 15612 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc, 15613 SourceLocation LParenLoc, 15614 SourceLocation VarLoc, 15615 SourceLocation EndLoc) { 15616 15617 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use)) 15618 return nullptr; 15619 15620 return new (Context) 15621 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15622 } 15623 15624 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar, 15625 SourceLocation StartLoc, 15626 SourceLocation LParenLoc, 15627 SourceLocation VarLoc, 15628 SourceLocation EndLoc) { 15629 if (InteropVar && 15630 !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy)) 15631 return nullptr; 15632 15633 return new (Context) 15634 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15635 } 15636 15637 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition, 15638 SourceLocation StartLoc, 15639 SourceLocation LParenLoc, 15640 SourceLocation EndLoc) { 15641 Expr *ValExpr = Condition; 15642 Stmt *HelperValStmt = nullptr; 15643 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15644 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15645 !Condition->isInstantiationDependent() && 15646 !Condition->containsUnexpandedParameterPack()) { 15647 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15648 if (Val.isInvalid()) 15649 return nullptr; 15650 15651 ValExpr = MakeFullExpr(Val.get()).get(); 15652 15653 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15654 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants, 15655 LangOpts.OpenMP); 15656 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15657 ValExpr = MakeFullExpr(ValExpr).get(); 15658 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15659 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15660 HelperValStmt = buildPreInits(Context, Captures); 15661 } 15662 } 15663 15664 return new (Context) OMPNovariantsClause( 15665 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 15666 } 15667 15668 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition, 15669 SourceLocation StartLoc, 15670 SourceLocation LParenLoc, 15671 SourceLocation EndLoc) { 15672 Expr *ValExpr = Condition; 15673 Stmt *HelperValStmt = nullptr; 15674 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15675 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15676 !Condition->isInstantiationDependent() && 15677 !Condition->containsUnexpandedParameterPack()) { 15678 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15679 if (Val.isInvalid()) 15680 return nullptr; 15681 15682 ValExpr = MakeFullExpr(Val.get()).get(); 15683 15684 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15685 CaptureRegion = 15686 getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP); 15687 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15688 ValExpr = MakeFullExpr(ValExpr).get(); 15689 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15690 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15691 HelperValStmt = buildPreInits(Context, Captures); 15692 } 15693 } 15694 15695 return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion, 15696 StartLoc, LParenLoc, EndLoc); 15697 } 15698 15699 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID, 15700 SourceLocation StartLoc, 15701 SourceLocation LParenLoc, 15702 SourceLocation EndLoc) { 15703 Expr *ValExpr = ThreadID; 15704 Stmt *HelperValStmt = nullptr; 15705 15706 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15707 OpenMPDirectiveKind CaptureRegion = 15708 getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP); 15709 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15710 ValExpr = MakeFullExpr(ValExpr).get(); 15711 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15712 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15713 HelperValStmt = buildPreInits(Context, Captures); 15714 } 15715 15716 return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion, 15717 StartLoc, LParenLoc, EndLoc); 15718 } 15719 15720 OMPClause *Sema::ActOnOpenMPVarListClause( 15721 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 15722 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 15723 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 15724 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 15725 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 15726 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 15727 SourceLocation ExtraModifierLoc, 15728 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 15729 ArrayRef<SourceLocation> MotionModifiersLoc) { 15730 SourceLocation StartLoc = Locs.StartLoc; 15731 SourceLocation LParenLoc = Locs.LParenLoc; 15732 SourceLocation EndLoc = Locs.EndLoc; 15733 OMPClause *Res = nullptr; 15734 switch (Kind) { 15735 case OMPC_private: 15736 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15737 break; 15738 case OMPC_firstprivate: 15739 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15740 break; 15741 case OMPC_lastprivate: 15742 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 15743 "Unexpected lastprivate modifier."); 15744 Res = ActOnOpenMPLastprivateClause( 15745 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 15746 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 15747 break; 15748 case OMPC_shared: 15749 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 15750 break; 15751 case OMPC_reduction: 15752 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 15753 "Unexpected lastprivate modifier."); 15754 Res = ActOnOpenMPReductionClause( 15755 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 15756 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 15757 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 15758 break; 15759 case OMPC_task_reduction: 15760 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15761 EndLoc, ReductionOrMapperIdScopeSpec, 15762 ReductionOrMapperId); 15763 break; 15764 case OMPC_in_reduction: 15765 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15766 EndLoc, ReductionOrMapperIdScopeSpec, 15767 ReductionOrMapperId); 15768 break; 15769 case OMPC_linear: 15770 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 15771 "Unexpected linear modifier."); 15772 Res = ActOnOpenMPLinearClause( 15773 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 15774 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 15775 ColonLoc, EndLoc); 15776 break; 15777 case OMPC_aligned: 15778 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 15779 LParenLoc, ColonLoc, EndLoc); 15780 break; 15781 case OMPC_copyin: 15782 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 15783 break; 15784 case OMPC_copyprivate: 15785 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15786 break; 15787 case OMPC_flush: 15788 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 15789 break; 15790 case OMPC_depend: 15791 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 15792 "Unexpected depend modifier."); 15793 Res = ActOnOpenMPDependClause( 15794 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 15795 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 15796 break; 15797 case OMPC_map: 15798 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 15799 "Unexpected map modifier."); 15800 Res = ActOnOpenMPMapClause( 15801 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 15802 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 15803 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 15804 break; 15805 case OMPC_to: 15806 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 15807 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 15808 ColonLoc, VarList, Locs); 15809 break; 15810 case OMPC_from: 15811 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 15812 ReductionOrMapperIdScopeSpec, 15813 ReductionOrMapperId, ColonLoc, VarList, Locs); 15814 break; 15815 case OMPC_use_device_ptr: 15816 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 15817 break; 15818 case OMPC_use_device_addr: 15819 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 15820 break; 15821 case OMPC_is_device_ptr: 15822 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 15823 break; 15824 case OMPC_allocate: 15825 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 15826 LParenLoc, ColonLoc, EndLoc); 15827 break; 15828 case OMPC_nontemporal: 15829 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 15830 break; 15831 case OMPC_inclusive: 15832 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15833 break; 15834 case OMPC_exclusive: 15835 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15836 break; 15837 case OMPC_affinity: 15838 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 15839 DepModOrTailExpr, VarList); 15840 break; 15841 case OMPC_if: 15842 case OMPC_depobj: 15843 case OMPC_final: 15844 case OMPC_num_threads: 15845 case OMPC_safelen: 15846 case OMPC_simdlen: 15847 case OMPC_sizes: 15848 case OMPC_allocator: 15849 case OMPC_collapse: 15850 case OMPC_default: 15851 case OMPC_proc_bind: 15852 case OMPC_schedule: 15853 case OMPC_ordered: 15854 case OMPC_nowait: 15855 case OMPC_untied: 15856 case OMPC_mergeable: 15857 case OMPC_threadprivate: 15858 case OMPC_read: 15859 case OMPC_write: 15860 case OMPC_update: 15861 case OMPC_capture: 15862 case OMPC_seq_cst: 15863 case OMPC_acq_rel: 15864 case OMPC_acquire: 15865 case OMPC_release: 15866 case OMPC_relaxed: 15867 case OMPC_device: 15868 case OMPC_threads: 15869 case OMPC_simd: 15870 case OMPC_num_teams: 15871 case OMPC_thread_limit: 15872 case OMPC_priority: 15873 case OMPC_grainsize: 15874 case OMPC_nogroup: 15875 case OMPC_num_tasks: 15876 case OMPC_hint: 15877 case OMPC_dist_schedule: 15878 case OMPC_defaultmap: 15879 case OMPC_unknown: 15880 case OMPC_uniform: 15881 case OMPC_unified_address: 15882 case OMPC_unified_shared_memory: 15883 case OMPC_reverse_offload: 15884 case OMPC_dynamic_allocators: 15885 case OMPC_atomic_default_mem_order: 15886 case OMPC_device_type: 15887 case OMPC_match: 15888 case OMPC_order: 15889 case OMPC_destroy: 15890 case OMPC_novariants: 15891 case OMPC_nocontext: 15892 case OMPC_detach: 15893 case OMPC_uses_allocators: 15894 case OMPC_when: 15895 case OMPC_bind: 15896 default: 15897 llvm_unreachable("Clause is not allowed."); 15898 } 15899 return Res; 15900 } 15901 15902 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 15903 ExprObjectKind OK, SourceLocation Loc) { 15904 ExprResult Res = BuildDeclRefExpr( 15905 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 15906 if (!Res.isUsable()) 15907 return ExprError(); 15908 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 15909 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 15910 if (!Res.isUsable()) 15911 return ExprError(); 15912 } 15913 if (VK != VK_LValue && Res.get()->isGLValue()) { 15914 Res = DefaultLvalueConversion(Res.get()); 15915 if (!Res.isUsable()) 15916 return ExprError(); 15917 } 15918 return Res; 15919 } 15920 15921 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 15922 SourceLocation StartLoc, 15923 SourceLocation LParenLoc, 15924 SourceLocation EndLoc) { 15925 SmallVector<Expr *, 8> Vars; 15926 SmallVector<Expr *, 8> PrivateCopies; 15927 for (Expr *RefExpr : VarList) { 15928 assert(RefExpr && "NULL expr in OpenMP private clause."); 15929 SourceLocation ELoc; 15930 SourceRange ERange; 15931 Expr *SimpleRefExpr = RefExpr; 15932 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15933 if (Res.second) { 15934 // It will be analyzed later. 15935 Vars.push_back(RefExpr); 15936 PrivateCopies.push_back(nullptr); 15937 } 15938 ValueDecl *D = Res.first; 15939 if (!D) 15940 continue; 15941 15942 QualType Type = D->getType(); 15943 auto *VD = dyn_cast<VarDecl>(D); 15944 15945 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15946 // A variable that appears in a private clause must not have an incomplete 15947 // type or a reference type. 15948 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 15949 continue; 15950 Type = Type.getNonReferenceType(); 15951 15952 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15953 // A variable that is privatized must not have a const-qualified type 15954 // unless it is of class type with a mutable member. This restriction does 15955 // not apply to the firstprivate clause. 15956 // 15957 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 15958 // A variable that appears in a private clause must not have a 15959 // const-qualified type unless it is of class type with a mutable member. 15960 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 15961 continue; 15962 15963 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15964 // in a Construct] 15965 // Variables with the predetermined data-sharing attributes may not be 15966 // listed in data-sharing attributes clauses, except for the cases 15967 // listed below. For these exceptions only, listing a predetermined 15968 // variable in a data-sharing attribute clause is allowed and overrides 15969 // the variable's predetermined data-sharing attributes. 15970 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15971 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 15972 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15973 << getOpenMPClauseName(OMPC_private); 15974 reportOriginalDsa(*this, DSAStack, D, DVar); 15975 continue; 15976 } 15977 15978 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15979 // Variably modified types are not supported for tasks. 15980 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 15981 isOpenMPTaskingDirective(CurrDir)) { 15982 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15983 << getOpenMPClauseName(OMPC_private) << Type 15984 << getOpenMPDirectiveName(CurrDir); 15985 bool IsDecl = 15986 !VD || 15987 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15988 Diag(D->getLocation(), 15989 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15990 << D; 15991 continue; 15992 } 15993 15994 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 15995 // A list item cannot appear in both a map clause and a data-sharing 15996 // attribute clause on the same construct 15997 // 15998 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 15999 // A list item cannot appear in both a map clause and a data-sharing 16000 // attribute clause on the same construct unless the construct is a 16001 // combined construct. 16002 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 16003 CurrDir == OMPD_target) { 16004 OpenMPClauseKind ConflictKind; 16005 if (DSAStack->checkMappableExprComponentListsForDecl( 16006 VD, /*CurrentRegionOnly=*/true, 16007 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 16008 OpenMPClauseKind WhereFoundClauseKind) -> bool { 16009 ConflictKind = WhereFoundClauseKind; 16010 return true; 16011 })) { 16012 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16013 << getOpenMPClauseName(OMPC_private) 16014 << getOpenMPClauseName(ConflictKind) 16015 << getOpenMPDirectiveName(CurrDir); 16016 reportOriginalDsa(*this, DSAStack, D, DVar); 16017 continue; 16018 } 16019 } 16020 16021 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 16022 // A variable of class type (or array thereof) that appears in a private 16023 // clause requires an accessible, unambiguous default constructor for the 16024 // class type. 16025 // Generate helper private variable and initialize it with the default 16026 // value. The address of the original variable is replaced by the address of 16027 // the new private variable in CodeGen. This new variable is not added to 16028 // IdResolver, so the code in the OpenMP region uses original variable for 16029 // proper diagnostics. 16030 Type = Type.getUnqualifiedType(); 16031 VarDecl *VDPrivate = 16032 buildVarDecl(*this, ELoc, Type, D->getName(), 16033 D->hasAttrs() ? &D->getAttrs() : nullptr, 16034 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16035 ActOnUninitializedDecl(VDPrivate); 16036 if (VDPrivate->isInvalidDecl()) 16037 continue; 16038 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 16039 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 16040 16041 DeclRefExpr *Ref = nullptr; 16042 if (!VD && !CurContext->isDependentContext()) 16043 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16044 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 16045 Vars.push_back((VD || CurContext->isDependentContext()) 16046 ? RefExpr->IgnoreParens() 16047 : Ref); 16048 PrivateCopies.push_back(VDPrivateRefExpr); 16049 } 16050 16051 if (Vars.empty()) 16052 return nullptr; 16053 16054 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 16055 PrivateCopies); 16056 } 16057 16058 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 16059 SourceLocation StartLoc, 16060 SourceLocation LParenLoc, 16061 SourceLocation EndLoc) { 16062 SmallVector<Expr *, 8> Vars; 16063 SmallVector<Expr *, 8> PrivateCopies; 16064 SmallVector<Expr *, 8> Inits; 16065 SmallVector<Decl *, 4> ExprCaptures; 16066 bool IsImplicitClause = 16067 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 16068 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 16069 16070 for (Expr *RefExpr : VarList) { 16071 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 16072 SourceLocation ELoc; 16073 SourceRange ERange; 16074 Expr *SimpleRefExpr = RefExpr; 16075 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16076 if (Res.second) { 16077 // It will be analyzed later. 16078 Vars.push_back(RefExpr); 16079 PrivateCopies.push_back(nullptr); 16080 Inits.push_back(nullptr); 16081 } 16082 ValueDecl *D = Res.first; 16083 if (!D) 16084 continue; 16085 16086 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 16087 QualType Type = D->getType(); 16088 auto *VD = dyn_cast<VarDecl>(D); 16089 16090 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 16091 // A variable that appears in a private clause must not have an incomplete 16092 // type or a reference type. 16093 if (RequireCompleteType(ELoc, Type, 16094 diag::err_omp_firstprivate_incomplete_type)) 16095 continue; 16096 Type = Type.getNonReferenceType(); 16097 16098 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 16099 // A variable of class type (or array thereof) that appears in a private 16100 // clause requires an accessible, unambiguous copy constructor for the 16101 // class type. 16102 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 16103 16104 // If an implicit firstprivate variable found it was checked already. 16105 DSAStackTy::DSAVarData TopDVar; 16106 if (!IsImplicitClause) { 16107 DSAStackTy::DSAVarData DVar = 16108 DSAStack->getTopDSA(D, /*FromParent=*/false); 16109 TopDVar = DVar; 16110 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16111 bool IsConstant = ElemType.isConstant(Context); 16112 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 16113 // A list item that specifies a given variable may not appear in more 16114 // than one clause on the same directive, except that a variable may be 16115 // specified in both firstprivate and lastprivate clauses. 16116 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 16117 // A list item may appear in a firstprivate or lastprivate clause but not 16118 // both. 16119 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 16120 (isOpenMPDistributeDirective(CurrDir) || 16121 DVar.CKind != OMPC_lastprivate) && 16122 DVar.RefExpr) { 16123 Diag(ELoc, diag::err_omp_wrong_dsa) 16124 << getOpenMPClauseName(DVar.CKind) 16125 << getOpenMPClauseName(OMPC_firstprivate); 16126 reportOriginalDsa(*this, DSAStack, D, DVar); 16127 continue; 16128 } 16129 16130 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16131 // in a Construct] 16132 // Variables with the predetermined data-sharing attributes may not be 16133 // listed in data-sharing attributes clauses, except for the cases 16134 // listed below. For these exceptions only, listing a predetermined 16135 // variable in a data-sharing attribute clause is allowed and overrides 16136 // the variable's predetermined data-sharing attributes. 16137 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16138 // in a Construct, C/C++, p.2] 16139 // Variables with const-qualified type having no mutable member may be 16140 // listed in a firstprivate clause, even if they are static data members. 16141 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 16142 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 16143 Diag(ELoc, diag::err_omp_wrong_dsa) 16144 << getOpenMPClauseName(DVar.CKind) 16145 << getOpenMPClauseName(OMPC_firstprivate); 16146 reportOriginalDsa(*this, DSAStack, D, DVar); 16147 continue; 16148 } 16149 16150 // OpenMP [2.9.3.4, Restrictions, p.2] 16151 // A list item that is private within a parallel region must not appear 16152 // in a firstprivate clause on a worksharing construct if any of the 16153 // worksharing regions arising from the worksharing construct ever bind 16154 // to any of the parallel regions arising from the parallel construct. 16155 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 16156 // A list item that is private within a teams region must not appear in a 16157 // firstprivate clause on a distribute construct if any of the distribute 16158 // regions arising from the distribute construct ever bind to any of the 16159 // teams regions arising from the teams construct. 16160 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 16161 // A list item that appears in a reduction clause of a teams construct 16162 // must not appear in a firstprivate clause on a distribute construct if 16163 // any of the distribute regions arising from the distribute construct 16164 // ever bind to any of the teams regions arising from the teams construct. 16165 if ((isOpenMPWorksharingDirective(CurrDir) || 16166 isOpenMPDistributeDirective(CurrDir)) && 16167 !isOpenMPParallelDirective(CurrDir) && 16168 !isOpenMPTeamsDirective(CurrDir)) { 16169 DVar = DSAStack->getImplicitDSA(D, true); 16170 if (DVar.CKind != OMPC_shared && 16171 (isOpenMPParallelDirective(DVar.DKind) || 16172 isOpenMPTeamsDirective(DVar.DKind) || 16173 DVar.DKind == OMPD_unknown)) { 16174 Diag(ELoc, diag::err_omp_required_access) 16175 << getOpenMPClauseName(OMPC_firstprivate) 16176 << getOpenMPClauseName(OMPC_shared); 16177 reportOriginalDsa(*this, DSAStack, D, DVar); 16178 continue; 16179 } 16180 } 16181 // OpenMP [2.9.3.4, Restrictions, p.3] 16182 // A list item that appears in a reduction clause of a parallel construct 16183 // must not appear in a firstprivate clause on a worksharing or task 16184 // construct if any of the worksharing or task regions arising from the 16185 // worksharing or task construct ever bind to any of the parallel regions 16186 // arising from the parallel construct. 16187 // OpenMP [2.9.3.4, Restrictions, p.4] 16188 // A list item that appears in a reduction clause in worksharing 16189 // construct must not appear in a firstprivate clause in a task construct 16190 // encountered during execution of any of the worksharing regions arising 16191 // from the worksharing construct. 16192 if (isOpenMPTaskingDirective(CurrDir)) { 16193 DVar = DSAStack->hasInnermostDSA( 16194 D, 16195 [](OpenMPClauseKind C, bool AppliedToPointee) { 16196 return C == OMPC_reduction && !AppliedToPointee; 16197 }, 16198 [](OpenMPDirectiveKind K) { 16199 return isOpenMPParallelDirective(K) || 16200 isOpenMPWorksharingDirective(K) || 16201 isOpenMPTeamsDirective(K); 16202 }, 16203 /*FromParent=*/true); 16204 if (DVar.CKind == OMPC_reduction && 16205 (isOpenMPParallelDirective(DVar.DKind) || 16206 isOpenMPWorksharingDirective(DVar.DKind) || 16207 isOpenMPTeamsDirective(DVar.DKind))) { 16208 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 16209 << getOpenMPDirectiveName(DVar.DKind); 16210 reportOriginalDsa(*this, DSAStack, D, DVar); 16211 continue; 16212 } 16213 } 16214 16215 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 16216 // A list item cannot appear in both a map clause and a data-sharing 16217 // attribute clause on the same construct 16218 // 16219 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 16220 // A list item cannot appear in both a map clause and a data-sharing 16221 // attribute clause on the same construct unless the construct is a 16222 // combined construct. 16223 if ((LangOpts.OpenMP <= 45 && 16224 isOpenMPTargetExecutionDirective(CurrDir)) || 16225 CurrDir == OMPD_target) { 16226 OpenMPClauseKind ConflictKind; 16227 if (DSAStack->checkMappableExprComponentListsForDecl( 16228 VD, /*CurrentRegionOnly=*/true, 16229 [&ConflictKind]( 16230 OMPClauseMappableExprCommon::MappableExprComponentListRef, 16231 OpenMPClauseKind WhereFoundClauseKind) { 16232 ConflictKind = WhereFoundClauseKind; 16233 return true; 16234 })) { 16235 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16236 << getOpenMPClauseName(OMPC_firstprivate) 16237 << getOpenMPClauseName(ConflictKind) 16238 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16239 reportOriginalDsa(*this, DSAStack, D, DVar); 16240 continue; 16241 } 16242 } 16243 } 16244 16245 // Variably modified types are not supported for tasks. 16246 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 16247 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 16248 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16249 << getOpenMPClauseName(OMPC_firstprivate) << Type 16250 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16251 bool IsDecl = 16252 !VD || 16253 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16254 Diag(D->getLocation(), 16255 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16256 << D; 16257 continue; 16258 } 16259 16260 Type = Type.getUnqualifiedType(); 16261 VarDecl *VDPrivate = 16262 buildVarDecl(*this, ELoc, Type, D->getName(), 16263 D->hasAttrs() ? &D->getAttrs() : nullptr, 16264 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16265 // Generate helper private variable and initialize it with the value of the 16266 // original variable. The address of the original variable is replaced by 16267 // the address of the new private variable in the CodeGen. This new variable 16268 // is not added to IdResolver, so the code in the OpenMP region uses 16269 // original variable for proper diagnostics and variable capturing. 16270 Expr *VDInitRefExpr = nullptr; 16271 // For arrays generate initializer for single element and replace it by the 16272 // original array element in CodeGen. 16273 if (Type->isArrayType()) { 16274 VarDecl *VDInit = 16275 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 16276 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 16277 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 16278 ElemType = ElemType.getUnqualifiedType(); 16279 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 16280 ".firstprivate.temp"); 16281 InitializedEntity Entity = 16282 InitializedEntity::InitializeVariable(VDInitTemp); 16283 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 16284 16285 InitializationSequence InitSeq(*this, Entity, Kind, Init); 16286 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 16287 if (Result.isInvalid()) 16288 VDPrivate->setInvalidDecl(); 16289 else 16290 VDPrivate->setInit(Result.getAs<Expr>()); 16291 // Remove temp variable declaration. 16292 Context.Deallocate(VDInitTemp); 16293 } else { 16294 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 16295 ".firstprivate.temp"); 16296 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 16297 RefExpr->getExprLoc()); 16298 AddInitializerToDecl(VDPrivate, 16299 DefaultLvalueConversion(VDInitRefExpr).get(), 16300 /*DirectInit=*/false); 16301 } 16302 if (VDPrivate->isInvalidDecl()) { 16303 if (IsImplicitClause) { 16304 Diag(RefExpr->getExprLoc(), 16305 diag::note_omp_task_predetermined_firstprivate_here); 16306 } 16307 continue; 16308 } 16309 CurContext->addDecl(VDPrivate); 16310 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 16311 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 16312 RefExpr->getExprLoc()); 16313 DeclRefExpr *Ref = nullptr; 16314 if (!VD && !CurContext->isDependentContext()) { 16315 if (TopDVar.CKind == OMPC_lastprivate) { 16316 Ref = TopDVar.PrivateCopy; 16317 } else { 16318 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16319 if (!isOpenMPCapturedDecl(D)) 16320 ExprCaptures.push_back(Ref->getDecl()); 16321 } 16322 } 16323 if (!IsImplicitClause) 16324 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 16325 Vars.push_back((VD || CurContext->isDependentContext()) 16326 ? RefExpr->IgnoreParens() 16327 : Ref); 16328 PrivateCopies.push_back(VDPrivateRefExpr); 16329 Inits.push_back(VDInitRefExpr); 16330 } 16331 16332 if (Vars.empty()) 16333 return nullptr; 16334 16335 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16336 Vars, PrivateCopies, Inits, 16337 buildPreInits(Context, ExprCaptures)); 16338 } 16339 16340 OMPClause *Sema::ActOnOpenMPLastprivateClause( 16341 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 16342 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 16343 SourceLocation LParenLoc, SourceLocation EndLoc) { 16344 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 16345 assert(ColonLoc.isValid() && "Colon location must be valid."); 16346 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 16347 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 16348 /*Last=*/OMPC_LASTPRIVATE_unknown) 16349 << getOpenMPClauseName(OMPC_lastprivate); 16350 return nullptr; 16351 } 16352 16353 SmallVector<Expr *, 8> Vars; 16354 SmallVector<Expr *, 8> SrcExprs; 16355 SmallVector<Expr *, 8> DstExprs; 16356 SmallVector<Expr *, 8> AssignmentOps; 16357 SmallVector<Decl *, 4> ExprCaptures; 16358 SmallVector<Expr *, 4> ExprPostUpdates; 16359 for (Expr *RefExpr : VarList) { 16360 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16361 SourceLocation ELoc; 16362 SourceRange ERange; 16363 Expr *SimpleRefExpr = RefExpr; 16364 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16365 if (Res.second) { 16366 // It will be analyzed later. 16367 Vars.push_back(RefExpr); 16368 SrcExprs.push_back(nullptr); 16369 DstExprs.push_back(nullptr); 16370 AssignmentOps.push_back(nullptr); 16371 } 16372 ValueDecl *D = Res.first; 16373 if (!D) 16374 continue; 16375 16376 QualType Type = D->getType(); 16377 auto *VD = dyn_cast<VarDecl>(D); 16378 16379 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 16380 // A variable that appears in a lastprivate clause must not have an 16381 // incomplete type or a reference type. 16382 if (RequireCompleteType(ELoc, Type, 16383 diag::err_omp_lastprivate_incomplete_type)) 16384 continue; 16385 Type = Type.getNonReferenceType(); 16386 16387 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 16388 // A variable that is privatized must not have a const-qualified type 16389 // unless it is of class type with a mutable member. This restriction does 16390 // not apply to the firstprivate clause. 16391 // 16392 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 16393 // A variable that appears in a lastprivate clause must not have a 16394 // const-qualified type unless it is of class type with a mutable member. 16395 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 16396 continue; 16397 16398 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 16399 // A list item that appears in a lastprivate clause with the conditional 16400 // modifier must be a scalar variable. 16401 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 16402 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 16403 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16404 VarDecl::DeclarationOnly; 16405 Diag(D->getLocation(), 16406 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16407 << D; 16408 continue; 16409 } 16410 16411 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16412 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 16413 // in a Construct] 16414 // Variables with the predetermined data-sharing attributes may not be 16415 // listed in data-sharing attributes clauses, except for the cases 16416 // listed below. 16417 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 16418 // A list item may appear in a firstprivate or lastprivate clause but not 16419 // both. 16420 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16421 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 16422 (isOpenMPDistributeDirective(CurrDir) || 16423 DVar.CKind != OMPC_firstprivate) && 16424 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 16425 Diag(ELoc, diag::err_omp_wrong_dsa) 16426 << getOpenMPClauseName(DVar.CKind) 16427 << getOpenMPClauseName(OMPC_lastprivate); 16428 reportOriginalDsa(*this, DSAStack, D, DVar); 16429 continue; 16430 } 16431 16432 // OpenMP [2.14.3.5, Restrictions, p.2] 16433 // A list item that is private within a parallel region, or that appears in 16434 // the reduction clause of a parallel construct, must not appear in a 16435 // lastprivate clause on a worksharing construct if any of the corresponding 16436 // worksharing regions ever binds to any of the corresponding parallel 16437 // regions. 16438 DSAStackTy::DSAVarData TopDVar = DVar; 16439 if (isOpenMPWorksharingDirective(CurrDir) && 16440 !isOpenMPParallelDirective(CurrDir) && 16441 !isOpenMPTeamsDirective(CurrDir)) { 16442 DVar = DSAStack->getImplicitDSA(D, true); 16443 if (DVar.CKind != OMPC_shared) { 16444 Diag(ELoc, diag::err_omp_required_access) 16445 << getOpenMPClauseName(OMPC_lastprivate) 16446 << getOpenMPClauseName(OMPC_shared); 16447 reportOriginalDsa(*this, DSAStack, D, DVar); 16448 continue; 16449 } 16450 } 16451 16452 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 16453 // A variable of class type (or array thereof) that appears in a 16454 // lastprivate clause requires an accessible, unambiguous default 16455 // constructor for the class type, unless the list item is also specified 16456 // in a firstprivate clause. 16457 // A variable of class type (or array thereof) that appears in a 16458 // lastprivate clause requires an accessible, unambiguous copy assignment 16459 // operator for the class type. 16460 Type = Context.getBaseElementType(Type).getNonReferenceType(); 16461 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 16462 Type.getUnqualifiedType(), ".lastprivate.src", 16463 D->hasAttrs() ? &D->getAttrs() : nullptr); 16464 DeclRefExpr *PseudoSrcExpr = 16465 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 16466 VarDecl *DstVD = 16467 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 16468 D->hasAttrs() ? &D->getAttrs() : nullptr); 16469 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16470 // For arrays generate assignment operation for single element and replace 16471 // it by the original array element in CodeGen. 16472 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 16473 PseudoDstExpr, PseudoSrcExpr); 16474 if (AssignmentOp.isInvalid()) 16475 continue; 16476 AssignmentOp = 16477 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16478 if (AssignmentOp.isInvalid()) 16479 continue; 16480 16481 DeclRefExpr *Ref = nullptr; 16482 if (!VD && !CurContext->isDependentContext()) { 16483 if (TopDVar.CKind == OMPC_firstprivate) { 16484 Ref = TopDVar.PrivateCopy; 16485 } else { 16486 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16487 if (!isOpenMPCapturedDecl(D)) 16488 ExprCaptures.push_back(Ref->getDecl()); 16489 } 16490 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 16491 (!isOpenMPCapturedDecl(D) && 16492 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 16493 ExprResult RefRes = DefaultLvalueConversion(Ref); 16494 if (!RefRes.isUsable()) 16495 continue; 16496 ExprResult PostUpdateRes = 16497 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 16498 RefRes.get()); 16499 if (!PostUpdateRes.isUsable()) 16500 continue; 16501 ExprPostUpdates.push_back( 16502 IgnoredValueConversions(PostUpdateRes.get()).get()); 16503 } 16504 } 16505 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 16506 Vars.push_back((VD || CurContext->isDependentContext()) 16507 ? RefExpr->IgnoreParens() 16508 : Ref); 16509 SrcExprs.push_back(PseudoSrcExpr); 16510 DstExprs.push_back(PseudoDstExpr); 16511 AssignmentOps.push_back(AssignmentOp.get()); 16512 } 16513 16514 if (Vars.empty()) 16515 return nullptr; 16516 16517 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16518 Vars, SrcExprs, DstExprs, AssignmentOps, 16519 LPKind, LPKindLoc, ColonLoc, 16520 buildPreInits(Context, ExprCaptures), 16521 buildPostUpdate(*this, ExprPostUpdates)); 16522 } 16523 16524 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 16525 SourceLocation StartLoc, 16526 SourceLocation LParenLoc, 16527 SourceLocation EndLoc) { 16528 SmallVector<Expr *, 8> Vars; 16529 for (Expr *RefExpr : VarList) { 16530 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16531 SourceLocation ELoc; 16532 SourceRange ERange; 16533 Expr *SimpleRefExpr = RefExpr; 16534 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16535 if (Res.second) { 16536 // It will be analyzed later. 16537 Vars.push_back(RefExpr); 16538 } 16539 ValueDecl *D = Res.first; 16540 if (!D) 16541 continue; 16542 16543 auto *VD = dyn_cast<VarDecl>(D); 16544 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16545 // in a Construct] 16546 // Variables with the predetermined data-sharing attributes may not be 16547 // listed in data-sharing attributes clauses, except for the cases 16548 // listed below. For these exceptions only, listing a predetermined 16549 // variable in a data-sharing attribute clause is allowed and overrides 16550 // the variable's predetermined data-sharing attributes. 16551 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16552 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 16553 DVar.RefExpr) { 16554 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 16555 << getOpenMPClauseName(OMPC_shared); 16556 reportOriginalDsa(*this, DSAStack, D, DVar); 16557 continue; 16558 } 16559 16560 DeclRefExpr *Ref = nullptr; 16561 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 16562 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16563 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 16564 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 16565 ? RefExpr->IgnoreParens() 16566 : Ref); 16567 } 16568 16569 if (Vars.empty()) 16570 return nullptr; 16571 16572 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 16573 } 16574 16575 namespace { 16576 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 16577 DSAStackTy *Stack; 16578 16579 public: 16580 bool VisitDeclRefExpr(DeclRefExpr *E) { 16581 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 16582 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 16583 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 16584 return false; 16585 if (DVar.CKind != OMPC_unknown) 16586 return true; 16587 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 16588 VD, 16589 [](OpenMPClauseKind C, bool AppliedToPointee) { 16590 return isOpenMPPrivate(C) && !AppliedToPointee; 16591 }, 16592 [](OpenMPDirectiveKind) { return true; }, 16593 /*FromParent=*/true); 16594 return DVarPrivate.CKind != OMPC_unknown; 16595 } 16596 return false; 16597 } 16598 bool VisitStmt(Stmt *S) { 16599 for (Stmt *Child : S->children()) { 16600 if (Child && Visit(Child)) 16601 return true; 16602 } 16603 return false; 16604 } 16605 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 16606 }; 16607 } // namespace 16608 16609 namespace { 16610 // Transform MemberExpression for specified FieldDecl of current class to 16611 // DeclRefExpr to specified OMPCapturedExprDecl. 16612 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 16613 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 16614 ValueDecl *Field = nullptr; 16615 DeclRefExpr *CapturedExpr = nullptr; 16616 16617 public: 16618 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 16619 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 16620 16621 ExprResult TransformMemberExpr(MemberExpr *E) { 16622 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 16623 E->getMemberDecl() == Field) { 16624 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 16625 return CapturedExpr; 16626 } 16627 return BaseTransform::TransformMemberExpr(E); 16628 } 16629 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 16630 }; 16631 } // namespace 16632 16633 template <typename T, typename U> 16634 static T filterLookupForUDReductionAndMapper( 16635 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 16636 for (U &Set : Lookups) { 16637 for (auto *D : Set) { 16638 if (T Res = Gen(cast<ValueDecl>(D))) 16639 return Res; 16640 } 16641 } 16642 return T(); 16643 } 16644 16645 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 16646 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 16647 16648 for (auto RD : D->redecls()) { 16649 // Don't bother with extra checks if we already know this one isn't visible. 16650 if (RD == D) 16651 continue; 16652 16653 auto ND = cast<NamedDecl>(RD); 16654 if (LookupResult::isVisible(SemaRef, ND)) 16655 return ND; 16656 } 16657 16658 return nullptr; 16659 } 16660 16661 static void 16662 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 16663 SourceLocation Loc, QualType Ty, 16664 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 16665 // Find all of the associated namespaces and classes based on the 16666 // arguments we have. 16667 Sema::AssociatedNamespaceSet AssociatedNamespaces; 16668 Sema::AssociatedClassSet AssociatedClasses; 16669 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 16670 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 16671 AssociatedClasses); 16672 16673 // C++ [basic.lookup.argdep]p3: 16674 // Let X be the lookup set produced by unqualified lookup (3.4.1) 16675 // and let Y be the lookup set produced by argument dependent 16676 // lookup (defined as follows). If X contains [...] then Y is 16677 // empty. Otherwise Y is the set of declarations found in the 16678 // namespaces associated with the argument types as described 16679 // below. The set of declarations found by the lookup of the name 16680 // is the union of X and Y. 16681 // 16682 // Here, we compute Y and add its members to the overloaded 16683 // candidate set. 16684 for (auto *NS : AssociatedNamespaces) { 16685 // When considering an associated namespace, the lookup is the 16686 // same as the lookup performed when the associated namespace is 16687 // used as a qualifier (3.4.3.2) except that: 16688 // 16689 // -- Any using-directives in the associated namespace are 16690 // ignored. 16691 // 16692 // -- Any namespace-scope friend functions declared in 16693 // associated classes are visible within their respective 16694 // namespaces even if they are not visible during an ordinary 16695 // lookup (11.4). 16696 DeclContext::lookup_result R = NS->lookup(Id.getName()); 16697 for (auto *D : R) { 16698 auto *Underlying = D; 16699 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16700 Underlying = USD->getTargetDecl(); 16701 16702 if (!isa<OMPDeclareReductionDecl>(Underlying) && 16703 !isa<OMPDeclareMapperDecl>(Underlying)) 16704 continue; 16705 16706 if (!SemaRef.isVisible(D)) { 16707 D = findAcceptableDecl(SemaRef, D); 16708 if (!D) 16709 continue; 16710 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16711 Underlying = USD->getTargetDecl(); 16712 } 16713 Lookups.emplace_back(); 16714 Lookups.back().addDecl(Underlying); 16715 } 16716 } 16717 } 16718 16719 static ExprResult 16720 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 16721 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 16722 const DeclarationNameInfo &ReductionId, QualType Ty, 16723 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 16724 if (ReductionIdScopeSpec.isInvalid()) 16725 return ExprError(); 16726 SmallVector<UnresolvedSet<8>, 4> Lookups; 16727 if (S) { 16728 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16729 Lookup.suppressDiagnostics(); 16730 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 16731 NamedDecl *D = Lookup.getRepresentativeDecl(); 16732 do { 16733 S = S->getParent(); 16734 } while (S && !S->isDeclScope(D)); 16735 if (S) 16736 S = S->getParent(); 16737 Lookups.emplace_back(); 16738 Lookups.back().append(Lookup.begin(), Lookup.end()); 16739 Lookup.clear(); 16740 } 16741 } else if (auto *ULE = 16742 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 16743 Lookups.push_back(UnresolvedSet<8>()); 16744 Decl *PrevD = nullptr; 16745 for (NamedDecl *D : ULE->decls()) { 16746 if (D == PrevD) 16747 Lookups.push_back(UnresolvedSet<8>()); 16748 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 16749 Lookups.back().addDecl(DRD); 16750 PrevD = D; 16751 } 16752 } 16753 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 16754 Ty->isInstantiationDependentType() || 16755 Ty->containsUnexpandedParameterPack() || 16756 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 16757 return !D->isInvalidDecl() && 16758 (D->getType()->isDependentType() || 16759 D->getType()->isInstantiationDependentType() || 16760 D->getType()->containsUnexpandedParameterPack()); 16761 })) { 16762 UnresolvedSet<8> ResSet; 16763 for (const UnresolvedSet<8> &Set : Lookups) { 16764 if (Set.empty()) 16765 continue; 16766 ResSet.append(Set.begin(), Set.end()); 16767 // The last item marks the end of all declarations at the specified scope. 16768 ResSet.addDecl(Set[Set.size() - 1]); 16769 } 16770 return UnresolvedLookupExpr::Create( 16771 SemaRef.Context, /*NamingClass=*/nullptr, 16772 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 16773 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 16774 } 16775 // Lookup inside the classes. 16776 // C++ [over.match.oper]p3: 16777 // For a unary operator @ with an operand of a type whose 16778 // cv-unqualified version is T1, and for a binary operator @ with 16779 // a left operand of a type whose cv-unqualified version is T1 and 16780 // a right operand of a type whose cv-unqualified version is T2, 16781 // three sets of candidate functions, designated member 16782 // candidates, non-member candidates and built-in candidates, are 16783 // constructed as follows: 16784 // -- If T1 is a complete class type or a class currently being 16785 // defined, the set of member candidates is the result of the 16786 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 16787 // the set of member candidates is empty. 16788 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16789 Lookup.suppressDiagnostics(); 16790 if (const auto *TyRec = Ty->getAs<RecordType>()) { 16791 // Complete the type if it can be completed. 16792 // If the type is neither complete nor being defined, bail out now. 16793 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 16794 TyRec->getDecl()->getDefinition()) { 16795 Lookup.clear(); 16796 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 16797 if (Lookup.empty()) { 16798 Lookups.emplace_back(); 16799 Lookups.back().append(Lookup.begin(), Lookup.end()); 16800 } 16801 } 16802 } 16803 // Perform ADL. 16804 if (SemaRef.getLangOpts().CPlusPlus) 16805 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 16806 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16807 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 16808 if (!D->isInvalidDecl() && 16809 SemaRef.Context.hasSameType(D->getType(), Ty)) 16810 return D; 16811 return nullptr; 16812 })) 16813 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 16814 VK_LValue, Loc); 16815 if (SemaRef.getLangOpts().CPlusPlus) { 16816 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16817 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 16818 if (!D->isInvalidDecl() && 16819 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 16820 !Ty.isMoreQualifiedThan(D->getType())) 16821 return D; 16822 return nullptr; 16823 })) { 16824 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 16825 /*DetectVirtual=*/false); 16826 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 16827 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 16828 VD->getType().getUnqualifiedType()))) { 16829 if (SemaRef.CheckBaseClassAccess( 16830 Loc, VD->getType(), Ty, Paths.front(), 16831 /*DiagID=*/0) != Sema::AR_inaccessible) { 16832 SemaRef.BuildBasePathArray(Paths, BasePath); 16833 return SemaRef.BuildDeclRefExpr( 16834 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 16835 } 16836 } 16837 } 16838 } 16839 } 16840 if (ReductionIdScopeSpec.isSet()) { 16841 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 16842 << Ty << Range; 16843 return ExprError(); 16844 } 16845 return ExprEmpty(); 16846 } 16847 16848 namespace { 16849 /// Data for the reduction-based clauses. 16850 struct ReductionData { 16851 /// List of original reduction items. 16852 SmallVector<Expr *, 8> Vars; 16853 /// List of private copies of the reduction items. 16854 SmallVector<Expr *, 8> Privates; 16855 /// LHS expressions for the reduction_op expressions. 16856 SmallVector<Expr *, 8> LHSs; 16857 /// RHS expressions for the reduction_op expressions. 16858 SmallVector<Expr *, 8> RHSs; 16859 /// Reduction operation expression. 16860 SmallVector<Expr *, 8> ReductionOps; 16861 /// inscan copy operation expressions. 16862 SmallVector<Expr *, 8> InscanCopyOps; 16863 /// inscan copy temp array expressions for prefix sums. 16864 SmallVector<Expr *, 8> InscanCopyArrayTemps; 16865 /// inscan copy temp array element expressions for prefix sums. 16866 SmallVector<Expr *, 8> InscanCopyArrayElems; 16867 /// Taskgroup descriptors for the corresponding reduction items in 16868 /// in_reduction clauses. 16869 SmallVector<Expr *, 8> TaskgroupDescriptors; 16870 /// List of captures for clause. 16871 SmallVector<Decl *, 4> ExprCaptures; 16872 /// List of postupdate expressions. 16873 SmallVector<Expr *, 4> ExprPostUpdates; 16874 /// Reduction modifier. 16875 unsigned RedModifier = 0; 16876 ReductionData() = delete; 16877 /// Reserves required memory for the reduction data. 16878 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 16879 Vars.reserve(Size); 16880 Privates.reserve(Size); 16881 LHSs.reserve(Size); 16882 RHSs.reserve(Size); 16883 ReductionOps.reserve(Size); 16884 if (RedModifier == OMPC_REDUCTION_inscan) { 16885 InscanCopyOps.reserve(Size); 16886 InscanCopyArrayTemps.reserve(Size); 16887 InscanCopyArrayElems.reserve(Size); 16888 } 16889 TaskgroupDescriptors.reserve(Size); 16890 ExprCaptures.reserve(Size); 16891 ExprPostUpdates.reserve(Size); 16892 } 16893 /// Stores reduction item and reduction operation only (required for dependent 16894 /// reduction item). 16895 void push(Expr *Item, Expr *ReductionOp) { 16896 Vars.emplace_back(Item); 16897 Privates.emplace_back(nullptr); 16898 LHSs.emplace_back(nullptr); 16899 RHSs.emplace_back(nullptr); 16900 ReductionOps.emplace_back(ReductionOp); 16901 TaskgroupDescriptors.emplace_back(nullptr); 16902 if (RedModifier == OMPC_REDUCTION_inscan) { 16903 InscanCopyOps.push_back(nullptr); 16904 InscanCopyArrayTemps.push_back(nullptr); 16905 InscanCopyArrayElems.push_back(nullptr); 16906 } 16907 } 16908 /// Stores reduction data. 16909 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 16910 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 16911 Expr *CopyArrayElem) { 16912 Vars.emplace_back(Item); 16913 Privates.emplace_back(Private); 16914 LHSs.emplace_back(LHS); 16915 RHSs.emplace_back(RHS); 16916 ReductionOps.emplace_back(ReductionOp); 16917 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 16918 if (RedModifier == OMPC_REDUCTION_inscan) { 16919 InscanCopyOps.push_back(CopyOp); 16920 InscanCopyArrayTemps.push_back(CopyArrayTemp); 16921 InscanCopyArrayElems.push_back(CopyArrayElem); 16922 } else { 16923 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 16924 CopyArrayElem == nullptr && 16925 "Copy operation must be used for inscan reductions only."); 16926 } 16927 } 16928 }; 16929 } // namespace 16930 16931 static bool checkOMPArraySectionConstantForReduction( 16932 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 16933 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 16934 const Expr *Length = OASE->getLength(); 16935 if (Length == nullptr) { 16936 // For array sections of the form [1:] or [:], we would need to analyze 16937 // the lower bound... 16938 if (OASE->getColonLocFirst().isValid()) 16939 return false; 16940 16941 // This is an array subscript which has implicit length 1! 16942 SingleElement = true; 16943 ArraySizes.push_back(llvm::APSInt::get(1)); 16944 } else { 16945 Expr::EvalResult Result; 16946 if (!Length->EvaluateAsInt(Result, Context)) 16947 return false; 16948 16949 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16950 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 16951 ArraySizes.push_back(ConstantLengthValue); 16952 } 16953 16954 // Get the base of this array section and walk up from there. 16955 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 16956 16957 // We require length = 1 for all array sections except the right-most to 16958 // guarantee that the memory region is contiguous and has no holes in it. 16959 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 16960 Length = TempOASE->getLength(); 16961 if (Length == nullptr) { 16962 // For array sections of the form [1:] or [:], we would need to analyze 16963 // the lower bound... 16964 if (OASE->getColonLocFirst().isValid()) 16965 return false; 16966 16967 // This is an array subscript which has implicit length 1! 16968 ArraySizes.push_back(llvm::APSInt::get(1)); 16969 } else { 16970 Expr::EvalResult Result; 16971 if (!Length->EvaluateAsInt(Result, Context)) 16972 return false; 16973 16974 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16975 if (ConstantLengthValue.getSExtValue() != 1) 16976 return false; 16977 16978 ArraySizes.push_back(ConstantLengthValue); 16979 } 16980 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 16981 } 16982 16983 // If we have a single element, we don't need to add the implicit lengths. 16984 if (!SingleElement) { 16985 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 16986 // Has implicit length 1! 16987 ArraySizes.push_back(llvm::APSInt::get(1)); 16988 Base = TempASE->getBase()->IgnoreParenImpCasts(); 16989 } 16990 } 16991 16992 // This array section can be privatized as a single value or as a constant 16993 // sized array. 16994 return true; 16995 } 16996 16997 static BinaryOperatorKind 16998 getRelatedCompoundReductionOp(BinaryOperatorKind BOK) { 16999 if (BOK == BO_Add) 17000 return BO_AddAssign; 17001 if (BOK == BO_Mul) 17002 return BO_MulAssign; 17003 if (BOK == BO_And) 17004 return BO_AndAssign; 17005 if (BOK == BO_Or) 17006 return BO_OrAssign; 17007 if (BOK == BO_Xor) 17008 return BO_XorAssign; 17009 return BOK; 17010 } 17011 17012 static bool actOnOMPReductionKindClause( 17013 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 17014 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17015 SourceLocation ColonLoc, SourceLocation EndLoc, 17016 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17017 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 17018 DeclarationName DN = ReductionId.getName(); 17019 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 17020 BinaryOperatorKind BOK = BO_Comma; 17021 17022 ASTContext &Context = S.Context; 17023 // OpenMP [2.14.3.6, reduction clause] 17024 // C 17025 // reduction-identifier is either an identifier or one of the following 17026 // operators: +, -, *, &, |, ^, && and || 17027 // C++ 17028 // reduction-identifier is either an id-expression or one of the following 17029 // operators: +, -, *, &, |, ^, && and || 17030 switch (OOK) { 17031 case OO_Plus: 17032 case OO_Minus: 17033 BOK = BO_Add; 17034 break; 17035 case OO_Star: 17036 BOK = BO_Mul; 17037 break; 17038 case OO_Amp: 17039 BOK = BO_And; 17040 break; 17041 case OO_Pipe: 17042 BOK = BO_Or; 17043 break; 17044 case OO_Caret: 17045 BOK = BO_Xor; 17046 break; 17047 case OO_AmpAmp: 17048 BOK = BO_LAnd; 17049 break; 17050 case OO_PipePipe: 17051 BOK = BO_LOr; 17052 break; 17053 case OO_New: 17054 case OO_Delete: 17055 case OO_Array_New: 17056 case OO_Array_Delete: 17057 case OO_Slash: 17058 case OO_Percent: 17059 case OO_Tilde: 17060 case OO_Exclaim: 17061 case OO_Equal: 17062 case OO_Less: 17063 case OO_Greater: 17064 case OO_LessEqual: 17065 case OO_GreaterEqual: 17066 case OO_PlusEqual: 17067 case OO_MinusEqual: 17068 case OO_StarEqual: 17069 case OO_SlashEqual: 17070 case OO_PercentEqual: 17071 case OO_CaretEqual: 17072 case OO_AmpEqual: 17073 case OO_PipeEqual: 17074 case OO_LessLess: 17075 case OO_GreaterGreater: 17076 case OO_LessLessEqual: 17077 case OO_GreaterGreaterEqual: 17078 case OO_EqualEqual: 17079 case OO_ExclaimEqual: 17080 case OO_Spaceship: 17081 case OO_PlusPlus: 17082 case OO_MinusMinus: 17083 case OO_Comma: 17084 case OO_ArrowStar: 17085 case OO_Arrow: 17086 case OO_Call: 17087 case OO_Subscript: 17088 case OO_Conditional: 17089 case OO_Coawait: 17090 case NUM_OVERLOADED_OPERATORS: 17091 llvm_unreachable("Unexpected reduction identifier"); 17092 case OO_None: 17093 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 17094 if (II->isStr("max")) 17095 BOK = BO_GT; 17096 else if (II->isStr("min")) 17097 BOK = BO_LT; 17098 } 17099 break; 17100 } 17101 SourceRange ReductionIdRange; 17102 if (ReductionIdScopeSpec.isValid()) 17103 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 17104 else 17105 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 17106 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 17107 17108 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 17109 bool FirstIter = true; 17110 for (Expr *RefExpr : VarList) { 17111 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 17112 // OpenMP [2.1, C/C++] 17113 // A list item is a variable or array section, subject to the restrictions 17114 // specified in Section 2.4 on page 42 and in each of the sections 17115 // describing clauses and directives for which a list appears. 17116 // OpenMP [2.14.3.3, Restrictions, p.1] 17117 // A variable that is part of another variable (as an array or 17118 // structure element) cannot appear in a private clause. 17119 if (!FirstIter && IR != ER) 17120 ++IR; 17121 FirstIter = false; 17122 SourceLocation ELoc; 17123 SourceRange ERange; 17124 Expr *SimpleRefExpr = RefExpr; 17125 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 17126 /*AllowArraySection=*/true); 17127 if (Res.second) { 17128 // Try to find 'declare reduction' corresponding construct before using 17129 // builtin/overloaded operators. 17130 QualType Type = Context.DependentTy; 17131 CXXCastPath BasePath; 17132 ExprResult DeclareReductionRef = buildDeclareReductionRef( 17133 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 17134 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 17135 Expr *ReductionOp = nullptr; 17136 if (S.CurContext->isDependentContext() && 17137 (DeclareReductionRef.isUnset() || 17138 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 17139 ReductionOp = DeclareReductionRef.get(); 17140 // It will be analyzed later. 17141 RD.push(RefExpr, ReductionOp); 17142 } 17143 ValueDecl *D = Res.first; 17144 if (!D) 17145 continue; 17146 17147 Expr *TaskgroupDescriptor = nullptr; 17148 QualType Type; 17149 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 17150 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 17151 if (ASE) { 17152 Type = ASE->getType().getNonReferenceType(); 17153 } else if (OASE) { 17154 QualType BaseType = 17155 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17156 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17157 Type = ATy->getElementType(); 17158 else 17159 Type = BaseType->getPointeeType(); 17160 Type = Type.getNonReferenceType(); 17161 } else { 17162 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 17163 } 17164 auto *VD = dyn_cast<VarDecl>(D); 17165 17166 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 17167 // A variable that appears in a private clause must not have an incomplete 17168 // type or a reference type. 17169 if (S.RequireCompleteType(ELoc, D->getType(), 17170 diag::err_omp_reduction_incomplete_type)) 17171 continue; 17172 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17173 // A list item that appears in a reduction clause must not be 17174 // const-qualified. 17175 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 17176 /*AcceptIfMutable*/ false, ASE || OASE)) 17177 continue; 17178 17179 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 17180 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 17181 // If a list-item is a reference type then it must bind to the same object 17182 // for all threads of the team. 17183 if (!ASE && !OASE) { 17184 if (VD) { 17185 VarDecl *VDDef = VD->getDefinition(); 17186 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 17187 DSARefChecker Check(Stack); 17188 if (Check.Visit(VDDef->getInit())) { 17189 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 17190 << getOpenMPClauseName(ClauseKind) << ERange; 17191 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 17192 continue; 17193 } 17194 } 17195 } 17196 17197 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 17198 // in a Construct] 17199 // Variables with the predetermined data-sharing attributes may not be 17200 // listed in data-sharing attributes clauses, except for the cases 17201 // listed below. For these exceptions only, listing a predetermined 17202 // variable in a data-sharing attribute clause is allowed and overrides 17203 // the variable's predetermined data-sharing attributes. 17204 // OpenMP [2.14.3.6, Restrictions, p.3] 17205 // Any number of reduction clauses can be specified on the directive, 17206 // but a list item can appear only once in the reduction clauses for that 17207 // directive. 17208 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17209 if (DVar.CKind == OMPC_reduction) { 17210 S.Diag(ELoc, diag::err_omp_once_referenced) 17211 << getOpenMPClauseName(ClauseKind); 17212 if (DVar.RefExpr) 17213 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 17214 continue; 17215 } 17216 if (DVar.CKind != OMPC_unknown) { 17217 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17218 << getOpenMPClauseName(DVar.CKind) 17219 << getOpenMPClauseName(OMPC_reduction); 17220 reportOriginalDsa(S, Stack, D, DVar); 17221 continue; 17222 } 17223 17224 // OpenMP [2.14.3.6, Restrictions, p.1] 17225 // A list item that appears in a reduction clause of a worksharing 17226 // construct must be shared in the parallel regions to which any of the 17227 // worksharing regions arising from the worksharing construct bind. 17228 if (isOpenMPWorksharingDirective(CurrDir) && 17229 !isOpenMPParallelDirective(CurrDir) && 17230 !isOpenMPTeamsDirective(CurrDir)) { 17231 DVar = Stack->getImplicitDSA(D, true); 17232 if (DVar.CKind != OMPC_shared) { 17233 S.Diag(ELoc, diag::err_omp_required_access) 17234 << getOpenMPClauseName(OMPC_reduction) 17235 << getOpenMPClauseName(OMPC_shared); 17236 reportOriginalDsa(S, Stack, D, DVar); 17237 continue; 17238 } 17239 } 17240 } else { 17241 // Threadprivates cannot be shared between threads, so dignose if the base 17242 // is a threadprivate variable. 17243 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17244 if (DVar.CKind == OMPC_threadprivate) { 17245 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17246 << getOpenMPClauseName(DVar.CKind) 17247 << getOpenMPClauseName(OMPC_reduction); 17248 reportOriginalDsa(S, Stack, D, DVar); 17249 continue; 17250 } 17251 } 17252 17253 // Try to find 'declare reduction' corresponding construct before using 17254 // builtin/overloaded operators. 17255 CXXCastPath BasePath; 17256 ExprResult DeclareReductionRef = buildDeclareReductionRef( 17257 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 17258 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 17259 if (DeclareReductionRef.isInvalid()) 17260 continue; 17261 if (S.CurContext->isDependentContext() && 17262 (DeclareReductionRef.isUnset() || 17263 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 17264 RD.push(RefExpr, DeclareReductionRef.get()); 17265 continue; 17266 } 17267 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 17268 // Not allowed reduction identifier is found. 17269 S.Diag(ReductionId.getBeginLoc(), 17270 diag::err_omp_unknown_reduction_identifier) 17271 << Type << ReductionIdRange; 17272 continue; 17273 } 17274 17275 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17276 // The type of a list item that appears in a reduction clause must be valid 17277 // for the reduction-identifier. For a max or min reduction in C, the type 17278 // of the list item must be an allowed arithmetic data type: char, int, 17279 // float, double, or _Bool, possibly modified with long, short, signed, or 17280 // unsigned. For a max or min reduction in C++, the type of the list item 17281 // must be an allowed arithmetic data type: char, wchar_t, int, float, 17282 // double, or bool, possibly modified with long, short, signed, or unsigned. 17283 if (DeclareReductionRef.isUnset()) { 17284 if ((BOK == BO_GT || BOK == BO_LT) && 17285 !(Type->isScalarType() || 17286 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 17287 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 17288 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 17289 if (!ASE && !OASE) { 17290 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17291 VarDecl::DeclarationOnly; 17292 S.Diag(D->getLocation(), 17293 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17294 << D; 17295 } 17296 continue; 17297 } 17298 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 17299 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 17300 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 17301 << getOpenMPClauseName(ClauseKind); 17302 if (!ASE && !OASE) { 17303 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17304 VarDecl::DeclarationOnly; 17305 S.Diag(D->getLocation(), 17306 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17307 << D; 17308 } 17309 continue; 17310 } 17311 } 17312 17313 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 17314 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 17315 D->hasAttrs() ? &D->getAttrs() : nullptr); 17316 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 17317 D->hasAttrs() ? &D->getAttrs() : nullptr); 17318 QualType PrivateTy = Type; 17319 17320 // Try if we can determine constant lengths for all array sections and avoid 17321 // the VLA. 17322 bool ConstantLengthOASE = false; 17323 if (OASE) { 17324 bool SingleElement; 17325 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 17326 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 17327 Context, OASE, SingleElement, ArraySizes); 17328 17329 // If we don't have a single element, we must emit a constant array type. 17330 if (ConstantLengthOASE && !SingleElement) { 17331 for (llvm::APSInt &Size : ArraySizes) 17332 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 17333 ArrayType::Normal, 17334 /*IndexTypeQuals=*/0); 17335 } 17336 } 17337 17338 if ((OASE && !ConstantLengthOASE) || 17339 (!OASE && !ASE && 17340 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 17341 if (!Context.getTargetInfo().isVLASupported()) { 17342 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 17343 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17344 S.Diag(ELoc, diag::note_vla_unsupported); 17345 continue; 17346 } else { 17347 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17348 S.targetDiag(ELoc, diag::note_vla_unsupported); 17349 } 17350 } 17351 // For arrays/array sections only: 17352 // Create pseudo array type for private copy. The size for this array will 17353 // be generated during codegen. 17354 // For array subscripts or single variables Private Ty is the same as Type 17355 // (type of the variable or single array element). 17356 PrivateTy = Context.getVariableArrayType( 17357 Type, 17358 new (Context) 17359 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue), 17360 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 17361 } else if (!ASE && !OASE && 17362 Context.getAsArrayType(D->getType().getNonReferenceType())) { 17363 PrivateTy = D->getType().getNonReferenceType(); 17364 } 17365 // Private copy. 17366 VarDecl *PrivateVD = 17367 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17368 D->hasAttrs() ? &D->getAttrs() : nullptr, 17369 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17370 // Add initializer for private variable. 17371 Expr *Init = nullptr; 17372 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 17373 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 17374 if (DeclareReductionRef.isUsable()) { 17375 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 17376 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 17377 if (DRD->getInitializer()) { 17378 Init = DRDRef; 17379 RHSVD->setInit(DRDRef); 17380 RHSVD->setInitStyle(VarDecl::CallInit); 17381 } 17382 } else { 17383 switch (BOK) { 17384 case BO_Add: 17385 case BO_Xor: 17386 case BO_Or: 17387 case BO_LOr: 17388 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 17389 if (Type->isScalarType() || Type->isAnyComplexType()) 17390 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 17391 break; 17392 case BO_Mul: 17393 case BO_LAnd: 17394 if (Type->isScalarType() || Type->isAnyComplexType()) { 17395 // '*' and '&&' reduction ops - initializer is '1'. 17396 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 17397 } 17398 break; 17399 case BO_And: { 17400 // '&' reduction op - initializer is '~0'. 17401 QualType OrigType = Type; 17402 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 17403 Type = ComplexTy->getElementType(); 17404 if (Type->isRealFloatingType()) { 17405 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 17406 Context.getFloatTypeSemantics(Type)); 17407 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17408 Type, ELoc); 17409 } else if (Type->isScalarType()) { 17410 uint64_t Size = Context.getTypeSize(Type); 17411 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 17412 llvm::APInt InitValue = llvm::APInt::getAllOnes(Size); 17413 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17414 } 17415 if (Init && OrigType->isAnyComplexType()) { 17416 // Init = 0xFFFF + 0xFFFFi; 17417 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 17418 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 17419 } 17420 Type = OrigType; 17421 break; 17422 } 17423 case BO_LT: 17424 case BO_GT: { 17425 // 'min' reduction op - initializer is 'Largest representable number in 17426 // the reduction list item type'. 17427 // 'max' reduction op - initializer is 'Least representable number in 17428 // the reduction list item type'. 17429 if (Type->isIntegerType() || Type->isPointerType()) { 17430 bool IsSigned = Type->hasSignedIntegerRepresentation(); 17431 uint64_t Size = Context.getTypeSize(Type); 17432 QualType IntTy = 17433 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 17434 llvm::APInt InitValue = 17435 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 17436 : llvm::APInt::getMinValue(Size) 17437 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 17438 : llvm::APInt::getMaxValue(Size); 17439 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17440 if (Type->isPointerType()) { 17441 // Cast to pointer type. 17442 ExprResult CastExpr = S.BuildCStyleCastExpr( 17443 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 17444 if (CastExpr.isInvalid()) 17445 continue; 17446 Init = CastExpr.get(); 17447 } 17448 } else if (Type->isRealFloatingType()) { 17449 llvm::APFloat InitValue = llvm::APFloat::getLargest( 17450 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 17451 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17452 Type, ELoc); 17453 } 17454 break; 17455 } 17456 case BO_PtrMemD: 17457 case BO_PtrMemI: 17458 case BO_MulAssign: 17459 case BO_Div: 17460 case BO_Rem: 17461 case BO_Sub: 17462 case BO_Shl: 17463 case BO_Shr: 17464 case BO_LE: 17465 case BO_GE: 17466 case BO_EQ: 17467 case BO_NE: 17468 case BO_Cmp: 17469 case BO_AndAssign: 17470 case BO_XorAssign: 17471 case BO_OrAssign: 17472 case BO_Assign: 17473 case BO_AddAssign: 17474 case BO_SubAssign: 17475 case BO_DivAssign: 17476 case BO_RemAssign: 17477 case BO_ShlAssign: 17478 case BO_ShrAssign: 17479 case BO_Comma: 17480 llvm_unreachable("Unexpected reduction operation"); 17481 } 17482 } 17483 if (Init && DeclareReductionRef.isUnset()) { 17484 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 17485 // Store initializer for single element in private copy. Will be used 17486 // during codegen. 17487 PrivateVD->setInit(RHSVD->getInit()); 17488 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17489 } else if (!Init) { 17490 S.ActOnUninitializedDecl(RHSVD); 17491 // Store initializer for single element in private copy. Will be used 17492 // during codegen. 17493 PrivateVD->setInit(RHSVD->getInit()); 17494 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17495 } 17496 if (RHSVD->isInvalidDecl()) 17497 continue; 17498 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 17499 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 17500 << Type << ReductionIdRange; 17501 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17502 VarDecl::DeclarationOnly; 17503 S.Diag(D->getLocation(), 17504 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17505 << D; 17506 continue; 17507 } 17508 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 17509 ExprResult ReductionOp; 17510 if (DeclareReductionRef.isUsable()) { 17511 QualType RedTy = DeclareReductionRef.get()->getType(); 17512 QualType PtrRedTy = Context.getPointerType(RedTy); 17513 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 17514 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 17515 if (!BasePath.empty()) { 17516 LHS = S.DefaultLvalueConversion(LHS.get()); 17517 RHS = S.DefaultLvalueConversion(RHS.get()); 17518 LHS = ImplicitCastExpr::Create( 17519 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 17520 LHS.get()->getValueKind(), FPOptionsOverride()); 17521 RHS = ImplicitCastExpr::Create( 17522 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 17523 RHS.get()->getValueKind(), FPOptionsOverride()); 17524 } 17525 FunctionProtoType::ExtProtoInfo EPI; 17526 QualType Params[] = {PtrRedTy, PtrRedTy}; 17527 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 17528 auto *OVE = new (Context) OpaqueValueExpr( 17529 ELoc, Context.getPointerType(FnTy), VK_PRValue, OK_Ordinary, 17530 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 17531 Expr *Args[] = {LHS.get(), RHS.get()}; 17532 ReductionOp = 17533 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_PRValue, ELoc, 17534 S.CurFPFeatureOverrides()); 17535 } else { 17536 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK); 17537 if (Type->isRecordType() && CombBOK != BOK) { 17538 Sema::TentativeAnalysisScope Trap(S); 17539 ReductionOp = 17540 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17541 CombBOK, LHSDRE, RHSDRE); 17542 } 17543 if (!ReductionOp.isUsable()) { 17544 ReductionOp = 17545 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, 17546 LHSDRE, RHSDRE); 17547 if (ReductionOp.isUsable()) { 17548 if (BOK != BO_LT && BOK != BO_GT) { 17549 ReductionOp = 17550 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17551 BO_Assign, LHSDRE, ReductionOp.get()); 17552 } else { 17553 auto *ConditionalOp = new (Context) 17554 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, 17555 RHSDRE, Type, VK_LValue, OK_Ordinary); 17556 ReductionOp = 17557 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17558 BO_Assign, LHSDRE, ConditionalOp); 17559 } 17560 } 17561 } 17562 if (ReductionOp.isUsable()) 17563 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 17564 /*DiscardedValue*/ false); 17565 if (!ReductionOp.isUsable()) 17566 continue; 17567 } 17568 17569 // Add copy operations for inscan reductions. 17570 // LHS = RHS; 17571 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 17572 if (ClauseKind == OMPC_reduction && 17573 RD.RedModifier == OMPC_REDUCTION_inscan) { 17574 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 17575 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 17576 RHS.get()); 17577 if (!CopyOpRes.isUsable()) 17578 continue; 17579 CopyOpRes = 17580 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 17581 if (!CopyOpRes.isUsable()) 17582 continue; 17583 // For simd directive and simd-based directives in simd mode no need to 17584 // construct temp array, need just a single temp element. 17585 if (Stack->getCurrentDirective() == OMPD_simd || 17586 (S.getLangOpts().OpenMPSimd && 17587 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 17588 VarDecl *TempArrayVD = 17589 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17590 D->hasAttrs() ? &D->getAttrs() : nullptr); 17591 // Add a constructor to the temp decl. 17592 S.ActOnUninitializedDecl(TempArrayVD); 17593 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 17594 } else { 17595 // Build temp array for prefix sum. 17596 auto *Dim = new (S.Context) 17597 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17598 QualType ArrayTy = 17599 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 17600 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 17601 VarDecl *TempArrayVD = 17602 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 17603 D->hasAttrs() ? &D->getAttrs() : nullptr); 17604 // Add a constructor to the temp decl. 17605 S.ActOnUninitializedDecl(TempArrayVD); 17606 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 17607 TempArrayElem = 17608 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 17609 auto *Idx = new (S.Context) 17610 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17611 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 17612 ELoc, Idx, ELoc); 17613 } 17614 } 17615 17616 // OpenMP [2.15.4.6, Restrictions, p.2] 17617 // A list item that appears in an in_reduction clause of a task construct 17618 // must appear in a task_reduction clause of a construct associated with a 17619 // taskgroup region that includes the participating task in its taskgroup 17620 // set. The construct associated with the innermost region that meets this 17621 // condition must specify the same reduction-identifier as the in_reduction 17622 // clause. 17623 if (ClauseKind == OMPC_in_reduction) { 17624 SourceRange ParentSR; 17625 BinaryOperatorKind ParentBOK; 17626 const Expr *ParentReductionOp = nullptr; 17627 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 17628 DSAStackTy::DSAVarData ParentBOKDSA = 17629 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 17630 ParentBOKTD); 17631 DSAStackTy::DSAVarData ParentReductionOpDSA = 17632 Stack->getTopMostTaskgroupReductionData( 17633 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 17634 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 17635 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 17636 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 17637 (DeclareReductionRef.isUsable() && IsParentBOK) || 17638 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 17639 bool EmitError = true; 17640 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 17641 llvm::FoldingSetNodeID RedId, ParentRedId; 17642 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 17643 DeclareReductionRef.get()->Profile(RedId, Context, 17644 /*Canonical=*/true); 17645 EmitError = RedId != ParentRedId; 17646 } 17647 if (EmitError) { 17648 S.Diag(ReductionId.getBeginLoc(), 17649 diag::err_omp_reduction_identifier_mismatch) 17650 << ReductionIdRange << RefExpr->getSourceRange(); 17651 S.Diag(ParentSR.getBegin(), 17652 diag::note_omp_previous_reduction_identifier) 17653 << ParentSR 17654 << (IsParentBOK ? ParentBOKDSA.RefExpr 17655 : ParentReductionOpDSA.RefExpr) 17656 ->getSourceRange(); 17657 continue; 17658 } 17659 } 17660 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 17661 } 17662 17663 DeclRefExpr *Ref = nullptr; 17664 Expr *VarsExpr = RefExpr->IgnoreParens(); 17665 if (!VD && !S.CurContext->isDependentContext()) { 17666 if (ASE || OASE) { 17667 TransformExprToCaptures RebuildToCapture(S, D); 17668 VarsExpr = 17669 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 17670 Ref = RebuildToCapture.getCapturedExpr(); 17671 } else { 17672 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 17673 } 17674 if (!S.isOpenMPCapturedDecl(D)) { 17675 RD.ExprCaptures.emplace_back(Ref->getDecl()); 17676 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17677 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 17678 if (!RefRes.isUsable()) 17679 continue; 17680 ExprResult PostUpdateRes = 17681 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 17682 RefRes.get()); 17683 if (!PostUpdateRes.isUsable()) 17684 continue; 17685 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 17686 Stack->getCurrentDirective() == OMPD_taskgroup) { 17687 S.Diag(RefExpr->getExprLoc(), 17688 diag::err_omp_reduction_non_addressable_expression) 17689 << RefExpr->getSourceRange(); 17690 continue; 17691 } 17692 RD.ExprPostUpdates.emplace_back( 17693 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 17694 } 17695 } 17696 } 17697 // All reduction items are still marked as reduction (to do not increase 17698 // code base size). 17699 unsigned Modifier = RD.RedModifier; 17700 // Consider task_reductions as reductions with task modifier. Required for 17701 // correct analysis of in_reduction clauses. 17702 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 17703 Modifier = OMPC_REDUCTION_task; 17704 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 17705 ASE || OASE); 17706 if (Modifier == OMPC_REDUCTION_task && 17707 (CurrDir == OMPD_taskgroup || 17708 ((isOpenMPParallelDirective(CurrDir) || 17709 isOpenMPWorksharingDirective(CurrDir)) && 17710 !isOpenMPSimdDirective(CurrDir)))) { 17711 if (DeclareReductionRef.isUsable()) 17712 Stack->addTaskgroupReductionData(D, ReductionIdRange, 17713 DeclareReductionRef.get()); 17714 else 17715 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 17716 } 17717 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 17718 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 17719 TempArrayElem.get()); 17720 } 17721 return RD.Vars.empty(); 17722 } 17723 17724 OMPClause *Sema::ActOnOpenMPReductionClause( 17725 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 17726 SourceLocation StartLoc, SourceLocation LParenLoc, 17727 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 17728 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17729 ArrayRef<Expr *> UnresolvedReductions) { 17730 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 17731 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 17732 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 17733 /*Last=*/OMPC_REDUCTION_unknown) 17734 << getOpenMPClauseName(OMPC_reduction); 17735 return nullptr; 17736 } 17737 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 17738 // A reduction clause with the inscan reduction-modifier may only appear on a 17739 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 17740 // construct, a parallel worksharing-loop construct or a parallel 17741 // worksharing-loop SIMD construct. 17742 if (Modifier == OMPC_REDUCTION_inscan && 17743 (DSAStack->getCurrentDirective() != OMPD_for && 17744 DSAStack->getCurrentDirective() != OMPD_for_simd && 17745 DSAStack->getCurrentDirective() != OMPD_simd && 17746 DSAStack->getCurrentDirective() != OMPD_parallel_for && 17747 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 17748 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 17749 return nullptr; 17750 } 17751 17752 ReductionData RD(VarList.size(), Modifier); 17753 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 17754 StartLoc, LParenLoc, ColonLoc, EndLoc, 17755 ReductionIdScopeSpec, ReductionId, 17756 UnresolvedReductions, RD)) 17757 return nullptr; 17758 17759 return OMPReductionClause::Create( 17760 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 17761 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17762 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 17763 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 17764 buildPreInits(Context, RD.ExprCaptures), 17765 buildPostUpdate(*this, RD.ExprPostUpdates)); 17766 } 17767 17768 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 17769 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17770 SourceLocation ColonLoc, SourceLocation EndLoc, 17771 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17772 ArrayRef<Expr *> UnresolvedReductions) { 17773 ReductionData RD(VarList.size()); 17774 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 17775 StartLoc, LParenLoc, ColonLoc, EndLoc, 17776 ReductionIdScopeSpec, ReductionId, 17777 UnresolvedReductions, RD)) 17778 return nullptr; 17779 17780 return OMPTaskReductionClause::Create( 17781 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17782 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17783 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 17784 buildPreInits(Context, RD.ExprCaptures), 17785 buildPostUpdate(*this, RD.ExprPostUpdates)); 17786 } 17787 17788 OMPClause *Sema::ActOnOpenMPInReductionClause( 17789 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17790 SourceLocation ColonLoc, SourceLocation EndLoc, 17791 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17792 ArrayRef<Expr *> UnresolvedReductions) { 17793 ReductionData RD(VarList.size()); 17794 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 17795 StartLoc, LParenLoc, ColonLoc, EndLoc, 17796 ReductionIdScopeSpec, ReductionId, 17797 UnresolvedReductions, RD)) 17798 return nullptr; 17799 17800 return OMPInReductionClause::Create( 17801 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17802 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17803 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 17804 buildPreInits(Context, RD.ExprCaptures), 17805 buildPostUpdate(*this, RD.ExprPostUpdates)); 17806 } 17807 17808 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 17809 SourceLocation LinLoc) { 17810 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 17811 LinKind == OMPC_LINEAR_unknown) { 17812 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 17813 return true; 17814 } 17815 return false; 17816 } 17817 17818 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 17819 OpenMPLinearClauseKind LinKind, QualType Type, 17820 bool IsDeclareSimd) { 17821 const auto *VD = dyn_cast_or_null<VarDecl>(D); 17822 // A variable must not have an incomplete type or a reference type. 17823 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 17824 return true; 17825 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 17826 !Type->isReferenceType()) { 17827 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 17828 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 17829 return true; 17830 } 17831 Type = Type.getNonReferenceType(); 17832 17833 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 17834 // A variable that is privatized must not have a const-qualified type 17835 // unless it is of class type with a mutable member. This restriction does 17836 // not apply to the firstprivate clause, nor to the linear clause on 17837 // declarative directives (like declare simd). 17838 if (!IsDeclareSimd && 17839 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 17840 return true; 17841 17842 // A list item must be of integral or pointer type. 17843 Type = Type.getUnqualifiedType().getCanonicalType(); 17844 const auto *Ty = Type.getTypePtrOrNull(); 17845 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 17846 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 17847 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 17848 if (D) { 17849 bool IsDecl = 17850 !VD || 17851 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17852 Diag(D->getLocation(), 17853 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17854 << D; 17855 } 17856 return true; 17857 } 17858 return false; 17859 } 17860 17861 OMPClause *Sema::ActOnOpenMPLinearClause( 17862 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 17863 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 17864 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17865 SmallVector<Expr *, 8> Vars; 17866 SmallVector<Expr *, 8> Privates; 17867 SmallVector<Expr *, 8> Inits; 17868 SmallVector<Decl *, 4> ExprCaptures; 17869 SmallVector<Expr *, 4> ExprPostUpdates; 17870 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 17871 LinKind = OMPC_LINEAR_val; 17872 for (Expr *RefExpr : VarList) { 17873 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17874 SourceLocation ELoc; 17875 SourceRange ERange; 17876 Expr *SimpleRefExpr = RefExpr; 17877 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17878 if (Res.second) { 17879 // It will be analyzed later. 17880 Vars.push_back(RefExpr); 17881 Privates.push_back(nullptr); 17882 Inits.push_back(nullptr); 17883 } 17884 ValueDecl *D = Res.first; 17885 if (!D) 17886 continue; 17887 17888 QualType Type = D->getType(); 17889 auto *VD = dyn_cast<VarDecl>(D); 17890 17891 // OpenMP [2.14.3.7, linear clause] 17892 // A list-item cannot appear in more than one linear clause. 17893 // A list-item that appears in a linear clause cannot appear in any 17894 // other data-sharing attribute clause. 17895 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 17896 if (DVar.RefExpr) { 17897 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 17898 << getOpenMPClauseName(OMPC_linear); 17899 reportOriginalDsa(*this, DSAStack, D, DVar); 17900 continue; 17901 } 17902 17903 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 17904 continue; 17905 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17906 17907 // Build private copy of original var. 17908 VarDecl *Private = 17909 buildVarDecl(*this, ELoc, Type, D->getName(), 17910 D->hasAttrs() ? &D->getAttrs() : nullptr, 17911 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17912 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 17913 // Build var to save initial value. 17914 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 17915 Expr *InitExpr; 17916 DeclRefExpr *Ref = nullptr; 17917 if (!VD && !CurContext->isDependentContext()) { 17918 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 17919 if (!isOpenMPCapturedDecl(D)) { 17920 ExprCaptures.push_back(Ref->getDecl()); 17921 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17922 ExprResult RefRes = DefaultLvalueConversion(Ref); 17923 if (!RefRes.isUsable()) 17924 continue; 17925 ExprResult PostUpdateRes = 17926 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 17927 SimpleRefExpr, RefRes.get()); 17928 if (!PostUpdateRes.isUsable()) 17929 continue; 17930 ExprPostUpdates.push_back( 17931 IgnoredValueConversions(PostUpdateRes.get()).get()); 17932 } 17933 } 17934 } 17935 if (LinKind == OMPC_LINEAR_uval) 17936 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 17937 else 17938 InitExpr = VD ? SimpleRefExpr : Ref; 17939 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 17940 /*DirectInit=*/false); 17941 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 17942 17943 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 17944 Vars.push_back((VD || CurContext->isDependentContext()) 17945 ? RefExpr->IgnoreParens() 17946 : Ref); 17947 Privates.push_back(PrivateRef); 17948 Inits.push_back(InitRef); 17949 } 17950 17951 if (Vars.empty()) 17952 return nullptr; 17953 17954 Expr *StepExpr = Step; 17955 Expr *CalcStepExpr = nullptr; 17956 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 17957 !Step->isInstantiationDependent() && 17958 !Step->containsUnexpandedParameterPack()) { 17959 SourceLocation StepLoc = Step->getBeginLoc(); 17960 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 17961 if (Val.isInvalid()) 17962 return nullptr; 17963 StepExpr = Val.get(); 17964 17965 // Build var to save the step value. 17966 VarDecl *SaveVar = 17967 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 17968 ExprResult SaveRef = 17969 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 17970 ExprResult CalcStep = 17971 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 17972 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 17973 17974 // Warn about zero linear step (it would be probably better specified as 17975 // making corresponding variables 'const'). 17976 if (Optional<llvm::APSInt> Result = 17977 StepExpr->getIntegerConstantExpr(Context)) { 17978 if (!Result->isNegative() && !Result->isStrictlyPositive()) 17979 Diag(StepLoc, diag::warn_omp_linear_step_zero) 17980 << Vars[0] << (Vars.size() > 1); 17981 } else if (CalcStep.isUsable()) { 17982 // Calculate the step beforehand instead of doing this on each iteration. 17983 // (This is not used if the number of iterations may be kfold-ed). 17984 CalcStepExpr = CalcStep.get(); 17985 } 17986 } 17987 17988 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 17989 ColonLoc, EndLoc, Vars, Privates, Inits, 17990 StepExpr, CalcStepExpr, 17991 buildPreInits(Context, ExprCaptures), 17992 buildPostUpdate(*this, ExprPostUpdates)); 17993 } 17994 17995 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 17996 Expr *NumIterations, Sema &SemaRef, 17997 Scope *S, DSAStackTy *Stack) { 17998 // Walk the vars and build update/final expressions for the CodeGen. 17999 SmallVector<Expr *, 8> Updates; 18000 SmallVector<Expr *, 8> Finals; 18001 SmallVector<Expr *, 8> UsedExprs; 18002 Expr *Step = Clause.getStep(); 18003 Expr *CalcStep = Clause.getCalcStep(); 18004 // OpenMP [2.14.3.7, linear clause] 18005 // If linear-step is not specified it is assumed to be 1. 18006 if (!Step) 18007 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 18008 else if (CalcStep) 18009 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 18010 bool HasErrors = false; 18011 auto CurInit = Clause.inits().begin(); 18012 auto CurPrivate = Clause.privates().begin(); 18013 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 18014 for (Expr *RefExpr : Clause.varlists()) { 18015 SourceLocation ELoc; 18016 SourceRange ERange; 18017 Expr *SimpleRefExpr = RefExpr; 18018 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 18019 ValueDecl *D = Res.first; 18020 if (Res.second || !D) { 18021 Updates.push_back(nullptr); 18022 Finals.push_back(nullptr); 18023 HasErrors = true; 18024 continue; 18025 } 18026 auto &&Info = Stack->isLoopControlVariable(D); 18027 // OpenMP [2.15.11, distribute simd Construct] 18028 // A list item may not appear in a linear clause, unless it is the loop 18029 // iteration variable. 18030 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 18031 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 18032 SemaRef.Diag(ELoc, 18033 diag::err_omp_linear_distribute_var_non_loop_iteration); 18034 Updates.push_back(nullptr); 18035 Finals.push_back(nullptr); 18036 HasErrors = true; 18037 continue; 18038 } 18039 Expr *InitExpr = *CurInit; 18040 18041 // Build privatized reference to the current linear var. 18042 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 18043 Expr *CapturedRef; 18044 if (LinKind == OMPC_LINEAR_uval) 18045 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 18046 else 18047 CapturedRef = 18048 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 18049 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 18050 /*RefersToCapture=*/true); 18051 18052 // Build update: Var = InitExpr + IV * Step 18053 ExprResult Update; 18054 if (!Info.first) 18055 Update = buildCounterUpdate( 18056 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 18057 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 18058 else 18059 Update = *CurPrivate; 18060 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 18061 /*DiscardedValue*/ false); 18062 18063 // Build final: Var = InitExpr + NumIterations * Step 18064 ExprResult Final; 18065 if (!Info.first) 18066 Final = 18067 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 18068 InitExpr, NumIterations, Step, /*Subtract=*/false, 18069 /*IsNonRectangularLB=*/false); 18070 else 18071 Final = *CurPrivate; 18072 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 18073 /*DiscardedValue*/ false); 18074 18075 if (!Update.isUsable() || !Final.isUsable()) { 18076 Updates.push_back(nullptr); 18077 Finals.push_back(nullptr); 18078 UsedExprs.push_back(nullptr); 18079 HasErrors = true; 18080 } else { 18081 Updates.push_back(Update.get()); 18082 Finals.push_back(Final.get()); 18083 if (!Info.first) 18084 UsedExprs.push_back(SimpleRefExpr); 18085 } 18086 ++CurInit; 18087 ++CurPrivate; 18088 } 18089 if (Expr *S = Clause.getStep()) 18090 UsedExprs.push_back(S); 18091 // Fill the remaining part with the nullptr. 18092 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 18093 Clause.setUpdates(Updates); 18094 Clause.setFinals(Finals); 18095 Clause.setUsedExprs(UsedExprs); 18096 return HasErrors; 18097 } 18098 18099 OMPClause *Sema::ActOnOpenMPAlignedClause( 18100 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 18101 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 18102 SmallVector<Expr *, 8> Vars; 18103 for (Expr *RefExpr : VarList) { 18104 assert(RefExpr && "NULL expr in OpenMP linear clause."); 18105 SourceLocation ELoc; 18106 SourceRange ERange; 18107 Expr *SimpleRefExpr = RefExpr; 18108 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18109 if (Res.second) { 18110 // It will be analyzed later. 18111 Vars.push_back(RefExpr); 18112 } 18113 ValueDecl *D = Res.first; 18114 if (!D) 18115 continue; 18116 18117 QualType QType = D->getType(); 18118 auto *VD = dyn_cast<VarDecl>(D); 18119 18120 // OpenMP [2.8.1, simd construct, Restrictions] 18121 // The type of list items appearing in the aligned clause must be 18122 // array, pointer, reference to array, or reference to pointer. 18123 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 18124 const Type *Ty = QType.getTypePtrOrNull(); 18125 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 18126 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 18127 << QType << getLangOpts().CPlusPlus << ERange; 18128 bool IsDecl = 18129 !VD || 18130 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 18131 Diag(D->getLocation(), 18132 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 18133 << D; 18134 continue; 18135 } 18136 18137 // OpenMP [2.8.1, simd construct, Restrictions] 18138 // A list-item cannot appear in more than one aligned clause. 18139 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 18140 Diag(ELoc, diag::err_omp_used_in_clause_twice) 18141 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 18142 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 18143 << getOpenMPClauseName(OMPC_aligned); 18144 continue; 18145 } 18146 18147 DeclRefExpr *Ref = nullptr; 18148 if (!VD && isOpenMPCapturedDecl(D)) 18149 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18150 Vars.push_back(DefaultFunctionArrayConversion( 18151 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 18152 .get()); 18153 } 18154 18155 // OpenMP [2.8.1, simd construct, Description] 18156 // The parameter of the aligned clause, alignment, must be a constant 18157 // positive integer expression. 18158 // If no optional parameter is specified, implementation-defined default 18159 // alignments for SIMD instructions on the target platforms are assumed. 18160 if (Alignment != nullptr) { 18161 ExprResult AlignResult = 18162 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 18163 if (AlignResult.isInvalid()) 18164 return nullptr; 18165 Alignment = AlignResult.get(); 18166 } 18167 if (Vars.empty()) 18168 return nullptr; 18169 18170 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 18171 EndLoc, Vars, Alignment); 18172 } 18173 18174 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 18175 SourceLocation StartLoc, 18176 SourceLocation LParenLoc, 18177 SourceLocation EndLoc) { 18178 SmallVector<Expr *, 8> Vars; 18179 SmallVector<Expr *, 8> SrcExprs; 18180 SmallVector<Expr *, 8> DstExprs; 18181 SmallVector<Expr *, 8> AssignmentOps; 18182 for (Expr *RefExpr : VarList) { 18183 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 18184 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18185 // It will be analyzed later. 18186 Vars.push_back(RefExpr); 18187 SrcExprs.push_back(nullptr); 18188 DstExprs.push_back(nullptr); 18189 AssignmentOps.push_back(nullptr); 18190 continue; 18191 } 18192 18193 SourceLocation ELoc = RefExpr->getExprLoc(); 18194 // OpenMP [2.1, C/C++] 18195 // A list item is a variable name. 18196 // OpenMP [2.14.4.1, Restrictions, p.1] 18197 // A list item that appears in a copyin clause must be threadprivate. 18198 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 18199 if (!DE || !isa<VarDecl>(DE->getDecl())) { 18200 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 18201 << 0 << RefExpr->getSourceRange(); 18202 continue; 18203 } 18204 18205 Decl *D = DE->getDecl(); 18206 auto *VD = cast<VarDecl>(D); 18207 18208 QualType Type = VD->getType(); 18209 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 18210 // It will be analyzed later. 18211 Vars.push_back(DE); 18212 SrcExprs.push_back(nullptr); 18213 DstExprs.push_back(nullptr); 18214 AssignmentOps.push_back(nullptr); 18215 continue; 18216 } 18217 18218 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 18219 // A list item that appears in a copyin clause must be threadprivate. 18220 if (!DSAStack->isThreadPrivate(VD)) { 18221 Diag(ELoc, diag::err_omp_required_access) 18222 << getOpenMPClauseName(OMPC_copyin) 18223 << getOpenMPDirectiveName(OMPD_threadprivate); 18224 continue; 18225 } 18226 18227 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18228 // A variable of class type (or array thereof) that appears in a 18229 // copyin clause requires an accessible, unambiguous copy assignment 18230 // operator for the class type. 18231 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 18232 VarDecl *SrcVD = 18233 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 18234 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18235 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 18236 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 18237 VarDecl *DstVD = 18238 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 18239 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18240 DeclRefExpr *PseudoDstExpr = 18241 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 18242 // For arrays generate assignment operation for single element and replace 18243 // it by the original array element in CodeGen. 18244 ExprResult AssignmentOp = 18245 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 18246 PseudoSrcExpr); 18247 if (AssignmentOp.isInvalid()) 18248 continue; 18249 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 18250 /*DiscardedValue*/ false); 18251 if (AssignmentOp.isInvalid()) 18252 continue; 18253 18254 DSAStack->addDSA(VD, DE, OMPC_copyin); 18255 Vars.push_back(DE); 18256 SrcExprs.push_back(PseudoSrcExpr); 18257 DstExprs.push_back(PseudoDstExpr); 18258 AssignmentOps.push_back(AssignmentOp.get()); 18259 } 18260 18261 if (Vars.empty()) 18262 return nullptr; 18263 18264 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 18265 SrcExprs, DstExprs, AssignmentOps); 18266 } 18267 18268 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 18269 SourceLocation StartLoc, 18270 SourceLocation LParenLoc, 18271 SourceLocation EndLoc) { 18272 SmallVector<Expr *, 8> Vars; 18273 SmallVector<Expr *, 8> SrcExprs; 18274 SmallVector<Expr *, 8> DstExprs; 18275 SmallVector<Expr *, 8> AssignmentOps; 18276 for (Expr *RefExpr : VarList) { 18277 assert(RefExpr && "NULL expr in OpenMP linear clause."); 18278 SourceLocation ELoc; 18279 SourceRange ERange; 18280 Expr *SimpleRefExpr = RefExpr; 18281 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18282 if (Res.second) { 18283 // It will be analyzed later. 18284 Vars.push_back(RefExpr); 18285 SrcExprs.push_back(nullptr); 18286 DstExprs.push_back(nullptr); 18287 AssignmentOps.push_back(nullptr); 18288 } 18289 ValueDecl *D = Res.first; 18290 if (!D) 18291 continue; 18292 18293 QualType Type = D->getType(); 18294 auto *VD = dyn_cast<VarDecl>(D); 18295 18296 // OpenMP [2.14.4.2, Restrictions, p.2] 18297 // A list item that appears in a copyprivate clause may not appear in a 18298 // private or firstprivate clause on the single construct. 18299 if (!VD || !DSAStack->isThreadPrivate(VD)) { 18300 DSAStackTy::DSAVarData DVar = 18301 DSAStack->getTopDSA(D, /*FromParent=*/false); 18302 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 18303 DVar.RefExpr) { 18304 Diag(ELoc, diag::err_omp_wrong_dsa) 18305 << getOpenMPClauseName(DVar.CKind) 18306 << getOpenMPClauseName(OMPC_copyprivate); 18307 reportOriginalDsa(*this, DSAStack, D, DVar); 18308 continue; 18309 } 18310 18311 // OpenMP [2.11.4.2, Restrictions, p.1] 18312 // All list items that appear in a copyprivate clause must be either 18313 // threadprivate or private in the enclosing context. 18314 if (DVar.CKind == OMPC_unknown) { 18315 DVar = DSAStack->getImplicitDSA(D, false); 18316 if (DVar.CKind == OMPC_shared) { 18317 Diag(ELoc, diag::err_omp_required_access) 18318 << getOpenMPClauseName(OMPC_copyprivate) 18319 << "threadprivate or private in the enclosing context"; 18320 reportOriginalDsa(*this, DSAStack, D, DVar); 18321 continue; 18322 } 18323 } 18324 } 18325 18326 // Variably modified types are not supported. 18327 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 18328 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 18329 << getOpenMPClauseName(OMPC_copyprivate) << Type 18330 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18331 bool IsDecl = 18332 !VD || 18333 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 18334 Diag(D->getLocation(), 18335 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 18336 << D; 18337 continue; 18338 } 18339 18340 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18341 // A variable of class type (or array thereof) that appears in a 18342 // copyin clause requires an accessible, unambiguous copy assignment 18343 // operator for the class type. 18344 Type = Context.getBaseElementType(Type.getNonReferenceType()) 18345 .getUnqualifiedType(); 18346 VarDecl *SrcVD = 18347 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 18348 D->hasAttrs() ? &D->getAttrs() : nullptr); 18349 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 18350 VarDecl *DstVD = 18351 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 18352 D->hasAttrs() ? &D->getAttrs() : nullptr); 18353 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 18354 ExprResult AssignmentOp = BuildBinOp( 18355 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 18356 if (AssignmentOp.isInvalid()) 18357 continue; 18358 AssignmentOp = 18359 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 18360 if (AssignmentOp.isInvalid()) 18361 continue; 18362 18363 // No need to mark vars as copyprivate, they are already threadprivate or 18364 // implicitly private. 18365 assert(VD || isOpenMPCapturedDecl(D)); 18366 Vars.push_back( 18367 VD ? RefExpr->IgnoreParens() 18368 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 18369 SrcExprs.push_back(PseudoSrcExpr); 18370 DstExprs.push_back(PseudoDstExpr); 18371 AssignmentOps.push_back(AssignmentOp.get()); 18372 } 18373 18374 if (Vars.empty()) 18375 return nullptr; 18376 18377 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18378 Vars, SrcExprs, DstExprs, AssignmentOps); 18379 } 18380 18381 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 18382 SourceLocation StartLoc, 18383 SourceLocation LParenLoc, 18384 SourceLocation EndLoc) { 18385 if (VarList.empty()) 18386 return nullptr; 18387 18388 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 18389 } 18390 18391 /// Tries to find omp_depend_t. type. 18392 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 18393 bool Diagnose = true) { 18394 QualType OMPDependT = Stack->getOMPDependT(); 18395 if (!OMPDependT.isNull()) 18396 return true; 18397 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 18398 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18399 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18400 if (Diagnose) 18401 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 18402 return false; 18403 } 18404 Stack->setOMPDependT(PT.get()); 18405 return true; 18406 } 18407 18408 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 18409 SourceLocation LParenLoc, 18410 SourceLocation EndLoc) { 18411 if (!Depobj) 18412 return nullptr; 18413 18414 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 18415 18416 // OpenMP 5.0, 2.17.10.1 depobj Construct 18417 // depobj is an lvalue expression of type omp_depend_t. 18418 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 18419 !Depobj->isInstantiationDependent() && 18420 !Depobj->containsUnexpandedParameterPack() && 18421 (OMPDependTFound && 18422 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 18423 /*CompareUnqualified=*/true))) { 18424 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18425 << 0 << Depobj->getType() << Depobj->getSourceRange(); 18426 } 18427 18428 if (!Depobj->isLValue()) { 18429 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18430 << 1 << Depobj->getSourceRange(); 18431 } 18432 18433 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 18434 } 18435 18436 OMPClause * 18437 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 18438 SourceLocation DepLoc, SourceLocation ColonLoc, 18439 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18440 SourceLocation LParenLoc, SourceLocation EndLoc) { 18441 if (DSAStack->getCurrentDirective() == OMPD_ordered && 18442 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 18443 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18444 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 18445 return nullptr; 18446 } 18447 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 18448 DSAStack->getCurrentDirective() == OMPD_depobj) && 18449 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 18450 DepKind == OMPC_DEPEND_sink || 18451 ((LangOpts.OpenMP < 50 || 18452 DSAStack->getCurrentDirective() == OMPD_depobj) && 18453 DepKind == OMPC_DEPEND_depobj))) { 18454 SmallVector<unsigned, 3> Except; 18455 Except.push_back(OMPC_DEPEND_source); 18456 Except.push_back(OMPC_DEPEND_sink); 18457 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 18458 Except.push_back(OMPC_DEPEND_depobj); 18459 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 18460 ? "depend modifier(iterator) or " 18461 : ""; 18462 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18463 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 18464 /*Last=*/OMPC_DEPEND_unknown, 18465 Except) 18466 << getOpenMPClauseName(OMPC_depend); 18467 return nullptr; 18468 } 18469 if (DepModifier && 18470 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 18471 Diag(DepModifier->getExprLoc(), 18472 diag::err_omp_depend_sink_source_with_modifier); 18473 return nullptr; 18474 } 18475 if (DepModifier && 18476 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 18477 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 18478 18479 SmallVector<Expr *, 8> Vars; 18480 DSAStackTy::OperatorOffsetTy OpsOffs; 18481 llvm::APSInt DepCounter(/*BitWidth=*/32); 18482 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 18483 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 18484 if (const Expr *OrderedCountExpr = 18485 DSAStack->getParentOrderedRegionParam().first) { 18486 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 18487 TotalDepCount.setIsUnsigned(/*Val=*/true); 18488 } 18489 } 18490 for (Expr *RefExpr : VarList) { 18491 assert(RefExpr && "NULL expr in OpenMP shared clause."); 18492 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18493 // It will be analyzed later. 18494 Vars.push_back(RefExpr); 18495 continue; 18496 } 18497 18498 SourceLocation ELoc = RefExpr->getExprLoc(); 18499 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 18500 if (DepKind == OMPC_DEPEND_sink) { 18501 if (DSAStack->getParentOrderedRegionParam().first && 18502 DepCounter >= TotalDepCount) { 18503 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 18504 continue; 18505 } 18506 ++DepCounter; 18507 // OpenMP [2.13.9, Summary] 18508 // depend(dependence-type : vec), where dependence-type is: 18509 // 'sink' and where vec is the iteration vector, which has the form: 18510 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 18511 // where n is the value specified by the ordered clause in the loop 18512 // directive, xi denotes the loop iteration variable of the i-th nested 18513 // loop associated with the loop directive, and di is a constant 18514 // non-negative integer. 18515 if (CurContext->isDependentContext()) { 18516 // It will be analyzed later. 18517 Vars.push_back(RefExpr); 18518 continue; 18519 } 18520 SimpleExpr = SimpleExpr->IgnoreImplicit(); 18521 OverloadedOperatorKind OOK = OO_None; 18522 SourceLocation OOLoc; 18523 Expr *LHS = SimpleExpr; 18524 Expr *RHS = nullptr; 18525 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 18526 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 18527 OOLoc = BO->getOperatorLoc(); 18528 LHS = BO->getLHS()->IgnoreParenImpCasts(); 18529 RHS = BO->getRHS()->IgnoreParenImpCasts(); 18530 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 18531 OOK = OCE->getOperator(); 18532 OOLoc = OCE->getOperatorLoc(); 18533 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18534 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 18535 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 18536 OOK = MCE->getMethodDecl() 18537 ->getNameInfo() 18538 .getName() 18539 .getCXXOverloadedOperator(); 18540 OOLoc = MCE->getCallee()->getExprLoc(); 18541 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 18542 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18543 } 18544 SourceLocation ELoc; 18545 SourceRange ERange; 18546 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 18547 if (Res.second) { 18548 // It will be analyzed later. 18549 Vars.push_back(RefExpr); 18550 } 18551 ValueDecl *D = Res.first; 18552 if (!D) 18553 continue; 18554 18555 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 18556 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 18557 continue; 18558 } 18559 if (RHS) { 18560 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 18561 RHS, OMPC_depend, /*StrictlyPositive=*/false); 18562 if (RHSRes.isInvalid()) 18563 continue; 18564 } 18565 if (!CurContext->isDependentContext() && 18566 DSAStack->getParentOrderedRegionParam().first && 18567 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 18568 const ValueDecl *VD = 18569 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 18570 if (VD) 18571 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 18572 << 1 << VD; 18573 else 18574 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 18575 continue; 18576 } 18577 OpsOffs.emplace_back(RHS, OOK); 18578 } else { 18579 bool OMPDependTFound = LangOpts.OpenMP >= 50; 18580 if (OMPDependTFound) 18581 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 18582 DepKind == OMPC_DEPEND_depobj); 18583 if (DepKind == OMPC_DEPEND_depobj) { 18584 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18585 // List items used in depend clauses with the depobj dependence type 18586 // must be expressions of the omp_depend_t type. 18587 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18588 !RefExpr->isInstantiationDependent() && 18589 !RefExpr->containsUnexpandedParameterPack() && 18590 (OMPDependTFound && 18591 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 18592 RefExpr->getType()))) { 18593 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18594 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 18595 continue; 18596 } 18597 if (!RefExpr->isLValue()) { 18598 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18599 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 18600 continue; 18601 } 18602 } else { 18603 // OpenMP 5.0 [2.17.11, Restrictions] 18604 // List items used in depend clauses cannot be zero-length array 18605 // sections. 18606 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 18607 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 18608 if (OASE) { 18609 QualType BaseType = 18610 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 18611 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 18612 ExprTy = ATy->getElementType(); 18613 else 18614 ExprTy = BaseType->getPointeeType(); 18615 ExprTy = ExprTy.getNonReferenceType(); 18616 const Expr *Length = OASE->getLength(); 18617 Expr::EvalResult Result; 18618 if (Length && !Length->isValueDependent() && 18619 Length->EvaluateAsInt(Result, Context) && 18620 Result.Val.getInt().isZero()) { 18621 Diag(ELoc, 18622 diag::err_omp_depend_zero_length_array_section_not_allowed) 18623 << SimpleExpr->getSourceRange(); 18624 continue; 18625 } 18626 } 18627 18628 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18629 // List items used in depend clauses with the in, out, inout or 18630 // mutexinoutset dependence types cannot be expressions of the 18631 // omp_depend_t type. 18632 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18633 !RefExpr->isInstantiationDependent() && 18634 !RefExpr->containsUnexpandedParameterPack() && 18635 (OMPDependTFound && 18636 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 18637 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18638 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 18639 << RefExpr->getSourceRange(); 18640 continue; 18641 } 18642 18643 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 18644 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 18645 (ASE && !ASE->getBase()->isTypeDependent() && 18646 !ASE->getBase() 18647 ->getType() 18648 .getNonReferenceType() 18649 ->isPointerType() && 18650 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 18651 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18652 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18653 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18654 continue; 18655 } 18656 18657 ExprResult Res; 18658 { 18659 Sema::TentativeAnalysisScope Trap(*this); 18660 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 18661 RefExpr->IgnoreParenImpCasts()); 18662 } 18663 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 18664 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 18665 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18666 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18667 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18668 continue; 18669 } 18670 } 18671 } 18672 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 18673 } 18674 18675 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 18676 TotalDepCount > VarList.size() && 18677 DSAStack->getParentOrderedRegionParam().first && 18678 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 18679 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 18680 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 18681 } 18682 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 18683 Vars.empty()) 18684 return nullptr; 18685 18686 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18687 DepModifier, DepKind, DepLoc, ColonLoc, 18688 Vars, TotalDepCount.getZExtValue()); 18689 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 18690 DSAStack->isParentOrderedRegion()) 18691 DSAStack->addDoacrossDependClause(C, OpsOffs); 18692 return C; 18693 } 18694 18695 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 18696 Expr *Device, SourceLocation StartLoc, 18697 SourceLocation LParenLoc, 18698 SourceLocation ModifierLoc, 18699 SourceLocation EndLoc) { 18700 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 18701 "Unexpected device modifier in OpenMP < 50."); 18702 18703 bool ErrorFound = false; 18704 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 18705 std::string Values = 18706 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 18707 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 18708 << Values << getOpenMPClauseName(OMPC_device); 18709 ErrorFound = true; 18710 } 18711 18712 Expr *ValExpr = Device; 18713 Stmt *HelperValStmt = nullptr; 18714 18715 // OpenMP [2.9.1, Restrictions] 18716 // The device expression must evaluate to a non-negative integer value. 18717 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 18718 /*StrictlyPositive=*/false) || 18719 ErrorFound; 18720 if (ErrorFound) 18721 return nullptr; 18722 18723 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18724 OpenMPDirectiveKind CaptureRegion = 18725 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 18726 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18727 ValExpr = MakeFullExpr(ValExpr).get(); 18728 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18729 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18730 HelperValStmt = buildPreInits(Context, Captures); 18731 } 18732 18733 return new (Context) 18734 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 18735 LParenLoc, ModifierLoc, EndLoc); 18736 } 18737 18738 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 18739 DSAStackTy *Stack, QualType QTy, 18740 bool FullCheck = true) { 18741 if (SemaRef.RequireCompleteType(SL, QTy, diag::err_incomplete_type)) 18742 return false; 18743 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 18744 !QTy.isTriviallyCopyableType(SemaRef.Context)) 18745 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 18746 return true; 18747 } 18748 18749 /// Return true if it can be proven that the provided array expression 18750 /// (array section or array subscript) does NOT specify the whole size of the 18751 /// array whose base type is \a BaseQTy. 18752 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 18753 const Expr *E, 18754 QualType BaseQTy) { 18755 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18756 18757 // If this is an array subscript, it refers to the whole size if the size of 18758 // the dimension is constant and equals 1. Also, an array section assumes the 18759 // format of an array subscript if no colon is used. 18760 if (isa<ArraySubscriptExpr>(E) || 18761 (OASE && OASE->getColonLocFirst().isInvalid())) { 18762 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18763 return ATy->getSize().getSExtValue() != 1; 18764 // Size can't be evaluated statically. 18765 return false; 18766 } 18767 18768 assert(OASE && "Expecting array section if not an array subscript."); 18769 const Expr *LowerBound = OASE->getLowerBound(); 18770 const Expr *Length = OASE->getLength(); 18771 18772 // If there is a lower bound that does not evaluates to zero, we are not 18773 // covering the whole dimension. 18774 if (LowerBound) { 18775 Expr::EvalResult Result; 18776 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 18777 return false; // Can't get the integer value as a constant. 18778 18779 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 18780 if (ConstLowerBound.getSExtValue()) 18781 return true; 18782 } 18783 18784 // If we don't have a length we covering the whole dimension. 18785 if (!Length) 18786 return false; 18787 18788 // If the base is a pointer, we don't have a way to get the size of the 18789 // pointee. 18790 if (BaseQTy->isPointerType()) 18791 return false; 18792 18793 // We can only check if the length is the same as the size of the dimension 18794 // if we have a constant array. 18795 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 18796 if (!CATy) 18797 return false; 18798 18799 Expr::EvalResult Result; 18800 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18801 return false; // Can't get the integer value as a constant. 18802 18803 llvm::APSInt ConstLength = Result.Val.getInt(); 18804 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 18805 } 18806 18807 // Return true if it can be proven that the provided array expression (array 18808 // section or array subscript) does NOT specify a single element of the array 18809 // whose base type is \a BaseQTy. 18810 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 18811 const Expr *E, 18812 QualType BaseQTy) { 18813 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18814 18815 // An array subscript always refer to a single element. Also, an array section 18816 // assumes the format of an array subscript if no colon is used. 18817 if (isa<ArraySubscriptExpr>(E) || 18818 (OASE && OASE->getColonLocFirst().isInvalid())) 18819 return false; 18820 18821 assert(OASE && "Expecting array section if not an array subscript."); 18822 const Expr *Length = OASE->getLength(); 18823 18824 // If we don't have a length we have to check if the array has unitary size 18825 // for this dimension. Also, we should always expect a length if the base type 18826 // is pointer. 18827 if (!Length) { 18828 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18829 return ATy->getSize().getSExtValue() != 1; 18830 // We cannot assume anything. 18831 return false; 18832 } 18833 18834 // Check if the length evaluates to 1. 18835 Expr::EvalResult Result; 18836 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18837 return false; // Can't get the integer value as a constant. 18838 18839 llvm::APSInt ConstLength = Result.Val.getInt(); 18840 return ConstLength.getSExtValue() != 1; 18841 } 18842 18843 // The base of elements of list in a map clause have to be either: 18844 // - a reference to variable or field. 18845 // - a member expression. 18846 // - an array expression. 18847 // 18848 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 18849 // reference to 'r'. 18850 // 18851 // If we have: 18852 // 18853 // struct SS { 18854 // Bla S; 18855 // foo() { 18856 // #pragma omp target map (S.Arr[:12]); 18857 // } 18858 // } 18859 // 18860 // We want to retrieve the member expression 'this->S'; 18861 18862 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 18863 // If a list item is an array section, it must specify contiguous storage. 18864 // 18865 // For this restriction it is sufficient that we make sure only references 18866 // to variables or fields and array expressions, and that no array sections 18867 // exist except in the rightmost expression (unless they cover the whole 18868 // dimension of the array). E.g. these would be invalid: 18869 // 18870 // r.ArrS[3:5].Arr[6:7] 18871 // 18872 // r.ArrS[3:5].x 18873 // 18874 // but these would be valid: 18875 // r.ArrS[3].Arr[6:7] 18876 // 18877 // r.ArrS[3].x 18878 namespace { 18879 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 18880 Sema &SemaRef; 18881 OpenMPClauseKind CKind = OMPC_unknown; 18882 OpenMPDirectiveKind DKind = OMPD_unknown; 18883 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 18884 bool IsNonContiguous = false; 18885 bool NoDiagnose = false; 18886 const Expr *RelevantExpr = nullptr; 18887 bool AllowUnitySizeArraySection = true; 18888 bool AllowWholeSizeArraySection = true; 18889 bool AllowAnotherPtr = true; 18890 SourceLocation ELoc; 18891 SourceRange ERange; 18892 18893 void emitErrorMsg() { 18894 // If nothing else worked, this is not a valid map clause expression. 18895 if (SemaRef.getLangOpts().OpenMP < 50) { 18896 SemaRef.Diag(ELoc, 18897 diag::err_omp_expected_named_var_member_or_array_expression) 18898 << ERange; 18899 } else { 18900 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18901 << getOpenMPClauseName(CKind) << ERange; 18902 } 18903 } 18904 18905 public: 18906 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 18907 if (!isa<VarDecl>(DRE->getDecl())) { 18908 emitErrorMsg(); 18909 return false; 18910 } 18911 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18912 RelevantExpr = DRE; 18913 // Record the component. 18914 Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous); 18915 return true; 18916 } 18917 18918 bool VisitMemberExpr(MemberExpr *ME) { 18919 Expr *E = ME; 18920 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 18921 18922 if (isa<CXXThisExpr>(BaseE)) { 18923 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18924 // We found a base expression: this->Val. 18925 RelevantExpr = ME; 18926 } else { 18927 E = BaseE; 18928 } 18929 18930 if (!isa<FieldDecl>(ME->getMemberDecl())) { 18931 if (!NoDiagnose) { 18932 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 18933 << ME->getSourceRange(); 18934 return false; 18935 } 18936 if (RelevantExpr) 18937 return false; 18938 return Visit(E); 18939 } 18940 18941 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 18942 18943 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 18944 // A bit-field cannot appear in a map clause. 18945 // 18946 if (FD->isBitField()) { 18947 if (!NoDiagnose) { 18948 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 18949 << ME->getSourceRange() << getOpenMPClauseName(CKind); 18950 return false; 18951 } 18952 if (RelevantExpr) 18953 return false; 18954 return Visit(E); 18955 } 18956 18957 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18958 // If the type of a list item is a reference to a type T then the type 18959 // will be considered to be T for all purposes of this clause. 18960 QualType CurType = BaseE->getType().getNonReferenceType(); 18961 18962 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 18963 // A list item cannot be a variable that is a member of a structure with 18964 // a union type. 18965 // 18966 if (CurType->isUnionType()) { 18967 if (!NoDiagnose) { 18968 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 18969 << ME->getSourceRange(); 18970 return false; 18971 } 18972 return RelevantExpr || Visit(E); 18973 } 18974 18975 // If we got a member expression, we should not expect any array section 18976 // before that: 18977 // 18978 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 18979 // If a list item is an element of a structure, only the rightmost symbol 18980 // of the variable reference can be an array section. 18981 // 18982 AllowUnitySizeArraySection = false; 18983 AllowWholeSizeArraySection = false; 18984 18985 // Record the component. 18986 Components.emplace_back(ME, FD, IsNonContiguous); 18987 return RelevantExpr || Visit(E); 18988 } 18989 18990 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 18991 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 18992 18993 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 18994 if (!NoDiagnose) { 18995 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18996 << 0 << AE->getSourceRange(); 18997 return false; 18998 } 18999 return RelevantExpr || Visit(E); 19000 } 19001 19002 // If we got an array subscript that express the whole dimension we 19003 // can have any array expressions before. If it only expressing part of 19004 // the dimension, we can only have unitary-size array expressions. 19005 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 19006 E->getType())) 19007 AllowWholeSizeArraySection = false; 19008 19009 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 19010 Expr::EvalResult Result; 19011 if (!AE->getIdx()->isValueDependent() && 19012 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 19013 !Result.Val.getInt().isZero()) { 19014 SemaRef.Diag(AE->getIdx()->getExprLoc(), 19015 diag::err_omp_invalid_map_this_expr); 19016 SemaRef.Diag(AE->getIdx()->getExprLoc(), 19017 diag::note_omp_invalid_subscript_on_this_ptr_map); 19018 } 19019 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19020 RelevantExpr = TE; 19021 } 19022 19023 // Record the component - we don't have any declaration associated. 19024 Components.emplace_back(AE, nullptr, IsNonContiguous); 19025 19026 return RelevantExpr || Visit(E); 19027 } 19028 19029 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 19030 // After OMP 5.0 Array section in reduction clause will be implicitly 19031 // mapped 19032 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) && 19033 "Array sections cannot be implicitly mapped."); 19034 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 19035 QualType CurType = 19036 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 19037 19038 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19039 // If the type of a list item is a reference to a type T then the type 19040 // will be considered to be T for all purposes of this clause. 19041 if (CurType->isReferenceType()) 19042 CurType = CurType->getPointeeType(); 19043 19044 bool IsPointer = CurType->isAnyPointerType(); 19045 19046 if (!IsPointer && !CurType->isArrayType()) { 19047 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 19048 << 0 << OASE->getSourceRange(); 19049 return false; 19050 } 19051 19052 bool NotWhole = 19053 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 19054 bool NotUnity = 19055 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 19056 19057 if (AllowWholeSizeArraySection) { 19058 // Any array section is currently allowed. Allowing a whole size array 19059 // section implies allowing a unity array section as well. 19060 // 19061 // If this array section refers to the whole dimension we can still 19062 // accept other array sections before this one, except if the base is a 19063 // pointer. Otherwise, only unitary sections are accepted. 19064 if (NotWhole || IsPointer) 19065 AllowWholeSizeArraySection = false; 19066 } else if (DKind == OMPD_target_update && 19067 SemaRef.getLangOpts().OpenMP >= 50) { 19068 if (IsPointer && !AllowAnotherPtr) 19069 SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined) 19070 << /*array of unknown bound */ 1; 19071 else 19072 IsNonContiguous = true; 19073 } else if (AllowUnitySizeArraySection && NotUnity) { 19074 // A unity or whole array section is not allowed and that is not 19075 // compatible with the properties of the current array section. 19076 if (NoDiagnose) 19077 return false; 19078 SemaRef.Diag( 19079 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 19080 << OASE->getSourceRange(); 19081 return false; 19082 } 19083 19084 if (IsPointer) 19085 AllowAnotherPtr = false; 19086 19087 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 19088 Expr::EvalResult ResultR; 19089 Expr::EvalResult ResultL; 19090 if (!OASE->getLength()->isValueDependent() && 19091 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 19092 !ResultR.Val.getInt().isOne()) { 19093 SemaRef.Diag(OASE->getLength()->getExprLoc(), 19094 diag::err_omp_invalid_map_this_expr); 19095 SemaRef.Diag(OASE->getLength()->getExprLoc(), 19096 diag::note_omp_invalid_length_on_this_ptr_mapping); 19097 } 19098 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 19099 OASE->getLowerBound()->EvaluateAsInt(ResultL, 19100 SemaRef.getASTContext()) && 19101 !ResultL.Val.getInt().isZero()) { 19102 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 19103 diag::err_omp_invalid_map_this_expr); 19104 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 19105 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 19106 } 19107 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19108 RelevantExpr = TE; 19109 } 19110 19111 // Record the component - we don't have any declaration associated. 19112 Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false); 19113 return RelevantExpr || Visit(E); 19114 } 19115 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 19116 Expr *Base = E->getBase(); 19117 19118 // Record the component - we don't have any declaration associated. 19119 Components.emplace_back(E, nullptr, IsNonContiguous); 19120 19121 return Visit(Base->IgnoreParenImpCasts()); 19122 } 19123 19124 bool VisitUnaryOperator(UnaryOperator *UO) { 19125 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 19126 UO->getOpcode() != UO_Deref) { 19127 emitErrorMsg(); 19128 return false; 19129 } 19130 if (!RelevantExpr) { 19131 // Record the component if haven't found base decl. 19132 Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false); 19133 } 19134 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 19135 } 19136 bool VisitBinaryOperator(BinaryOperator *BO) { 19137 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 19138 emitErrorMsg(); 19139 return false; 19140 } 19141 19142 // Pointer arithmetic is the only thing we expect to happen here so after we 19143 // make sure the binary operator is a pointer type, the we only thing need 19144 // to to is to visit the subtree that has the same type as root (so that we 19145 // know the other subtree is just an offset) 19146 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 19147 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 19148 Components.emplace_back(BO, nullptr, false); 19149 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 19150 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 19151 "Either LHS or RHS have base decl inside"); 19152 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 19153 return RelevantExpr || Visit(LE); 19154 return RelevantExpr || Visit(RE); 19155 } 19156 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 19157 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19158 RelevantExpr = CTE; 19159 Components.emplace_back(CTE, nullptr, IsNonContiguous); 19160 return true; 19161 } 19162 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 19163 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 19164 Components.emplace_back(COCE, nullptr, IsNonContiguous); 19165 return true; 19166 } 19167 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { 19168 Expr *Source = E->getSourceExpr(); 19169 if (!Source) { 19170 emitErrorMsg(); 19171 return false; 19172 } 19173 return Visit(Source); 19174 } 19175 bool VisitStmt(Stmt *) { 19176 emitErrorMsg(); 19177 return false; 19178 } 19179 const Expr *getFoundBase() const { 19180 return RelevantExpr; 19181 } 19182 explicit MapBaseChecker( 19183 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, 19184 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 19185 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 19186 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components), 19187 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 19188 }; 19189 } // namespace 19190 19191 /// Return the expression of the base of the mappable expression or null if it 19192 /// cannot be determined and do all the necessary checks to see if the expression 19193 /// is valid as a standalone mappable expression. In the process, record all the 19194 /// components of the expression. 19195 static const Expr *checkMapClauseExpressionBase( 19196 Sema &SemaRef, Expr *E, 19197 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 19198 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) { 19199 SourceLocation ELoc = E->getExprLoc(); 19200 SourceRange ERange = E->getSourceRange(); 19201 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc, 19202 ERange); 19203 if (Checker.Visit(E->IgnoreParens())) { 19204 // Check if the highest dimension array section has length specified 19205 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() && 19206 (CKind == OMPC_to || CKind == OMPC_from)) { 19207 auto CI = CurComponents.rbegin(); 19208 auto CE = CurComponents.rend(); 19209 for (; CI != CE; ++CI) { 19210 const auto *OASE = 19211 dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression()); 19212 if (!OASE) 19213 continue; 19214 if (OASE && OASE->getLength()) 19215 break; 19216 SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length) 19217 << ERange; 19218 } 19219 } 19220 return Checker.getFoundBase(); 19221 } 19222 return nullptr; 19223 } 19224 19225 // Return true if expression E associated with value VD has conflicts with other 19226 // map information. 19227 static bool checkMapConflicts( 19228 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 19229 bool CurrentRegionOnly, 19230 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 19231 OpenMPClauseKind CKind) { 19232 assert(VD && E); 19233 SourceLocation ELoc = E->getExprLoc(); 19234 SourceRange ERange = E->getSourceRange(); 19235 19236 // In order to easily check the conflicts we need to match each component of 19237 // the expression under test with the components of the expressions that are 19238 // already in the stack. 19239 19240 assert(!CurComponents.empty() && "Map clause expression with no components!"); 19241 assert(CurComponents.back().getAssociatedDeclaration() == VD && 19242 "Map clause expression with unexpected base!"); 19243 19244 // Variables to help detecting enclosing problems in data environment nests. 19245 bool IsEnclosedByDataEnvironmentExpr = false; 19246 const Expr *EnclosingExpr = nullptr; 19247 19248 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 19249 VD, CurrentRegionOnly, 19250 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 19251 ERange, CKind, &EnclosingExpr, 19252 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 19253 StackComponents, 19254 OpenMPClauseKind Kind) { 19255 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50) 19256 return false; 19257 assert(!StackComponents.empty() && 19258 "Map clause expression with no components!"); 19259 assert(StackComponents.back().getAssociatedDeclaration() == VD && 19260 "Map clause expression with unexpected base!"); 19261 (void)VD; 19262 19263 // The whole expression in the stack. 19264 const Expr *RE = StackComponents.front().getAssociatedExpression(); 19265 19266 // Expressions must start from the same base. Here we detect at which 19267 // point both expressions diverge from each other and see if we can 19268 // detect if the memory referred to both expressions is contiguous and 19269 // do not overlap. 19270 auto CI = CurComponents.rbegin(); 19271 auto CE = CurComponents.rend(); 19272 auto SI = StackComponents.rbegin(); 19273 auto SE = StackComponents.rend(); 19274 for (; CI != CE && SI != SE; ++CI, ++SI) { 19275 19276 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 19277 // At most one list item can be an array item derived from a given 19278 // variable in map clauses of the same construct. 19279 if (CurrentRegionOnly && 19280 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 19281 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 19282 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 19283 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 19284 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 19285 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 19286 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 19287 diag::err_omp_multiple_array_items_in_map_clause) 19288 << CI->getAssociatedExpression()->getSourceRange(); 19289 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 19290 diag::note_used_here) 19291 << SI->getAssociatedExpression()->getSourceRange(); 19292 return true; 19293 } 19294 19295 // Do both expressions have the same kind? 19296 if (CI->getAssociatedExpression()->getStmtClass() != 19297 SI->getAssociatedExpression()->getStmtClass()) 19298 break; 19299 19300 // Are we dealing with different variables/fields? 19301 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 19302 break; 19303 } 19304 // Check if the extra components of the expressions in the enclosing 19305 // data environment are redundant for the current base declaration. 19306 // If they are, the maps completely overlap, which is legal. 19307 for (; SI != SE; ++SI) { 19308 QualType Type; 19309 if (const auto *ASE = 19310 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 19311 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 19312 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 19313 SI->getAssociatedExpression())) { 19314 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 19315 Type = 19316 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 19317 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 19318 SI->getAssociatedExpression())) { 19319 Type = OASE->getBase()->getType()->getPointeeType(); 19320 } 19321 if (Type.isNull() || Type->isAnyPointerType() || 19322 checkArrayExpressionDoesNotReferToWholeSize( 19323 SemaRef, SI->getAssociatedExpression(), Type)) 19324 break; 19325 } 19326 19327 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19328 // List items of map clauses in the same construct must not share 19329 // original storage. 19330 // 19331 // If the expressions are exactly the same or one is a subset of the 19332 // other, it means they are sharing storage. 19333 if (CI == CE && SI == SE) { 19334 if (CurrentRegionOnly) { 19335 if (CKind == OMPC_map) { 19336 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19337 } else { 19338 assert(CKind == OMPC_to || CKind == OMPC_from); 19339 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19340 << ERange; 19341 } 19342 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19343 << RE->getSourceRange(); 19344 return true; 19345 } 19346 // If we find the same expression in the enclosing data environment, 19347 // that is legal. 19348 IsEnclosedByDataEnvironmentExpr = true; 19349 return false; 19350 } 19351 19352 QualType DerivedType = 19353 std::prev(CI)->getAssociatedDeclaration()->getType(); 19354 SourceLocation DerivedLoc = 19355 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 19356 19357 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19358 // If the type of a list item is a reference to a type T then the type 19359 // will be considered to be T for all purposes of this clause. 19360 DerivedType = DerivedType.getNonReferenceType(); 19361 19362 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 19363 // A variable for which the type is pointer and an array section 19364 // derived from that variable must not appear as list items of map 19365 // clauses of the same construct. 19366 // 19367 // Also, cover one of the cases in: 19368 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19369 // If any part of the original storage of a list item has corresponding 19370 // storage in the device data environment, all of the original storage 19371 // must have corresponding storage in the device data environment. 19372 // 19373 if (DerivedType->isAnyPointerType()) { 19374 if (CI == CE || SI == SE) { 19375 SemaRef.Diag( 19376 DerivedLoc, 19377 diag::err_omp_pointer_mapped_along_with_derived_section) 19378 << DerivedLoc; 19379 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19380 << RE->getSourceRange(); 19381 return true; 19382 } 19383 if (CI->getAssociatedExpression()->getStmtClass() != 19384 SI->getAssociatedExpression()->getStmtClass() || 19385 CI->getAssociatedDeclaration()->getCanonicalDecl() == 19386 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 19387 assert(CI != CE && SI != SE); 19388 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 19389 << DerivedLoc; 19390 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19391 << RE->getSourceRange(); 19392 return true; 19393 } 19394 } 19395 19396 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19397 // List items of map clauses in the same construct must not share 19398 // original storage. 19399 // 19400 // An expression is a subset of the other. 19401 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 19402 if (CKind == OMPC_map) { 19403 if (CI != CE || SI != SE) { 19404 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 19405 // a pointer. 19406 auto Begin = 19407 CI != CE ? CurComponents.begin() : StackComponents.begin(); 19408 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 19409 auto It = Begin; 19410 while (It != End && !It->getAssociatedDeclaration()) 19411 std::advance(It, 1); 19412 assert(It != End && 19413 "Expected at least one component with the declaration."); 19414 if (It != Begin && It->getAssociatedDeclaration() 19415 ->getType() 19416 .getCanonicalType() 19417 ->isAnyPointerType()) { 19418 IsEnclosedByDataEnvironmentExpr = false; 19419 EnclosingExpr = nullptr; 19420 return false; 19421 } 19422 } 19423 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19424 } else { 19425 assert(CKind == OMPC_to || CKind == OMPC_from); 19426 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19427 << ERange; 19428 } 19429 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19430 << RE->getSourceRange(); 19431 return true; 19432 } 19433 19434 // The current expression uses the same base as other expression in the 19435 // data environment but does not contain it completely. 19436 if (!CurrentRegionOnly && SI != SE) 19437 EnclosingExpr = RE; 19438 19439 // The current expression is a subset of the expression in the data 19440 // environment. 19441 IsEnclosedByDataEnvironmentExpr |= 19442 (!CurrentRegionOnly && CI != CE && SI == SE); 19443 19444 return false; 19445 }); 19446 19447 if (CurrentRegionOnly) 19448 return FoundError; 19449 19450 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19451 // If any part of the original storage of a list item has corresponding 19452 // storage in the device data environment, all of the original storage must 19453 // have corresponding storage in the device data environment. 19454 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 19455 // If a list item is an element of a structure, and a different element of 19456 // the structure has a corresponding list item in the device data environment 19457 // prior to a task encountering the construct associated with the map clause, 19458 // then the list item must also have a corresponding list item in the device 19459 // data environment prior to the task encountering the construct. 19460 // 19461 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 19462 SemaRef.Diag(ELoc, 19463 diag::err_omp_original_storage_is_shared_and_does_not_contain) 19464 << ERange; 19465 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 19466 << EnclosingExpr->getSourceRange(); 19467 return true; 19468 } 19469 19470 return FoundError; 19471 } 19472 19473 // Look up the user-defined mapper given the mapper name and mapped type, and 19474 // build a reference to it. 19475 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 19476 CXXScopeSpec &MapperIdScopeSpec, 19477 const DeclarationNameInfo &MapperId, 19478 QualType Type, 19479 Expr *UnresolvedMapper) { 19480 if (MapperIdScopeSpec.isInvalid()) 19481 return ExprError(); 19482 // Get the actual type for the array type. 19483 if (Type->isArrayType()) { 19484 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 19485 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 19486 } 19487 // Find all user-defined mappers with the given MapperId. 19488 SmallVector<UnresolvedSet<8>, 4> Lookups; 19489 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 19490 Lookup.suppressDiagnostics(); 19491 if (S) { 19492 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 19493 NamedDecl *D = Lookup.getRepresentativeDecl(); 19494 while (S && !S->isDeclScope(D)) 19495 S = S->getParent(); 19496 if (S) 19497 S = S->getParent(); 19498 Lookups.emplace_back(); 19499 Lookups.back().append(Lookup.begin(), Lookup.end()); 19500 Lookup.clear(); 19501 } 19502 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 19503 // Extract the user-defined mappers with the given MapperId. 19504 Lookups.push_back(UnresolvedSet<8>()); 19505 for (NamedDecl *D : ULE->decls()) { 19506 auto *DMD = cast<OMPDeclareMapperDecl>(D); 19507 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 19508 Lookups.back().addDecl(DMD); 19509 } 19510 } 19511 // Defer the lookup for dependent types. The results will be passed through 19512 // UnresolvedMapper on instantiation. 19513 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 19514 Type->isInstantiationDependentType() || 19515 Type->containsUnexpandedParameterPack() || 19516 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 19517 return !D->isInvalidDecl() && 19518 (D->getType()->isDependentType() || 19519 D->getType()->isInstantiationDependentType() || 19520 D->getType()->containsUnexpandedParameterPack()); 19521 })) { 19522 UnresolvedSet<8> URS; 19523 for (const UnresolvedSet<8> &Set : Lookups) { 19524 if (Set.empty()) 19525 continue; 19526 URS.append(Set.begin(), Set.end()); 19527 } 19528 return UnresolvedLookupExpr::Create( 19529 SemaRef.Context, /*NamingClass=*/nullptr, 19530 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 19531 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 19532 } 19533 SourceLocation Loc = MapperId.getLoc(); 19534 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19535 // The type must be of struct, union or class type in C and C++ 19536 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 19537 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 19538 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 19539 return ExprError(); 19540 } 19541 // Perform argument dependent lookup. 19542 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 19543 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 19544 // Return the first user-defined mapper with the desired type. 19545 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19546 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 19547 if (!D->isInvalidDecl() && 19548 SemaRef.Context.hasSameType(D->getType(), Type)) 19549 return D; 19550 return nullptr; 19551 })) 19552 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19553 // Find the first user-defined mapper with a type derived from the desired 19554 // type. 19555 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19556 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 19557 if (!D->isInvalidDecl() && 19558 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 19559 !Type.isMoreQualifiedThan(D->getType())) 19560 return D; 19561 return nullptr; 19562 })) { 19563 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 19564 /*DetectVirtual=*/false); 19565 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 19566 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 19567 VD->getType().getUnqualifiedType()))) { 19568 if (SemaRef.CheckBaseClassAccess( 19569 Loc, VD->getType(), Type, Paths.front(), 19570 /*DiagID=*/0) != Sema::AR_inaccessible) { 19571 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19572 } 19573 } 19574 } 19575 } 19576 // Report error if a mapper is specified, but cannot be found. 19577 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 19578 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 19579 << Type << MapperId.getName(); 19580 return ExprError(); 19581 } 19582 return ExprEmpty(); 19583 } 19584 19585 namespace { 19586 // Utility struct that gathers all the related lists associated with a mappable 19587 // expression. 19588 struct MappableVarListInfo { 19589 // The list of expressions. 19590 ArrayRef<Expr *> VarList; 19591 // The list of processed expressions. 19592 SmallVector<Expr *, 16> ProcessedVarList; 19593 // The mappble components for each expression. 19594 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 19595 // The base declaration of the variable. 19596 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 19597 // The reference to the user-defined mapper associated with every expression. 19598 SmallVector<Expr *, 16> UDMapperList; 19599 19600 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 19601 // We have a list of components and base declarations for each entry in the 19602 // variable list. 19603 VarComponents.reserve(VarList.size()); 19604 VarBaseDeclarations.reserve(VarList.size()); 19605 } 19606 }; 19607 } 19608 19609 // Check the validity of the provided variable list for the provided clause kind 19610 // \a CKind. In the check process the valid expressions, mappable expression 19611 // components, variables, and user-defined mappers are extracted and used to 19612 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 19613 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 19614 // and \a MapperId are expected to be valid if the clause kind is 'map'. 19615 static void checkMappableExpressionList( 19616 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 19617 MappableVarListInfo &MVLI, SourceLocation StartLoc, 19618 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 19619 ArrayRef<Expr *> UnresolvedMappers, 19620 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 19621 ArrayRef<OpenMPMapModifierKind> Modifiers = None, 19622 bool IsMapTypeImplicit = false, bool NoDiagnose = false) { 19623 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 19624 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 19625 "Unexpected clause kind with mappable expressions!"); 19626 19627 // If the identifier of user-defined mapper is not specified, it is "default". 19628 // We do not change the actual name in this clause to distinguish whether a 19629 // mapper is specified explicitly, i.e., it is not explicitly specified when 19630 // MapperId.getName() is empty. 19631 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 19632 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 19633 MapperId.setName(DeclNames.getIdentifier( 19634 &SemaRef.getASTContext().Idents.get("default"))); 19635 MapperId.setLoc(StartLoc); 19636 } 19637 19638 // Iterators to find the current unresolved mapper expression. 19639 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 19640 bool UpdateUMIt = false; 19641 Expr *UnresolvedMapper = nullptr; 19642 19643 bool HasHoldModifier = 19644 llvm::is_contained(Modifiers, OMPC_MAP_MODIFIER_ompx_hold); 19645 19646 // Keep track of the mappable components and base declarations in this clause. 19647 // Each entry in the list is going to have a list of components associated. We 19648 // record each set of the components so that we can build the clause later on. 19649 // In the end we should have the same amount of declarations and component 19650 // lists. 19651 19652 for (Expr *RE : MVLI.VarList) { 19653 assert(RE && "Null expr in omp to/from/map clause"); 19654 SourceLocation ELoc = RE->getExprLoc(); 19655 19656 // Find the current unresolved mapper expression. 19657 if (UpdateUMIt && UMIt != UMEnd) { 19658 UMIt++; 19659 assert( 19660 UMIt != UMEnd && 19661 "Expect the size of UnresolvedMappers to match with that of VarList"); 19662 } 19663 UpdateUMIt = true; 19664 if (UMIt != UMEnd) 19665 UnresolvedMapper = *UMIt; 19666 19667 const Expr *VE = RE->IgnoreParenLValueCasts(); 19668 19669 if (VE->isValueDependent() || VE->isTypeDependent() || 19670 VE->isInstantiationDependent() || 19671 VE->containsUnexpandedParameterPack()) { 19672 // Try to find the associated user-defined mapper. 19673 ExprResult ER = buildUserDefinedMapperRef( 19674 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19675 VE->getType().getCanonicalType(), UnresolvedMapper); 19676 if (ER.isInvalid()) 19677 continue; 19678 MVLI.UDMapperList.push_back(ER.get()); 19679 // We can only analyze this information once the missing information is 19680 // resolved. 19681 MVLI.ProcessedVarList.push_back(RE); 19682 continue; 19683 } 19684 19685 Expr *SimpleExpr = RE->IgnoreParenCasts(); 19686 19687 if (!RE->isLValue()) { 19688 if (SemaRef.getLangOpts().OpenMP < 50) { 19689 SemaRef.Diag( 19690 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 19691 << RE->getSourceRange(); 19692 } else { 19693 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 19694 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 19695 } 19696 continue; 19697 } 19698 19699 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 19700 ValueDecl *CurDeclaration = nullptr; 19701 19702 // Obtain the array or member expression bases if required. Also, fill the 19703 // components array with all the components identified in the process. 19704 const Expr *BE = 19705 checkMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind, 19706 DSAS->getCurrentDirective(), NoDiagnose); 19707 if (!BE) 19708 continue; 19709 19710 assert(!CurComponents.empty() && 19711 "Invalid mappable expression information."); 19712 19713 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 19714 // Add store "this" pointer to class in DSAStackTy for future checking 19715 DSAS->addMappedClassesQualTypes(TE->getType()); 19716 // Try to find the associated user-defined mapper. 19717 ExprResult ER = buildUserDefinedMapperRef( 19718 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19719 VE->getType().getCanonicalType(), UnresolvedMapper); 19720 if (ER.isInvalid()) 19721 continue; 19722 MVLI.UDMapperList.push_back(ER.get()); 19723 // Skip restriction checking for variable or field declarations 19724 MVLI.ProcessedVarList.push_back(RE); 19725 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19726 MVLI.VarComponents.back().append(CurComponents.begin(), 19727 CurComponents.end()); 19728 MVLI.VarBaseDeclarations.push_back(nullptr); 19729 continue; 19730 } 19731 19732 // For the following checks, we rely on the base declaration which is 19733 // expected to be associated with the last component. The declaration is 19734 // expected to be a variable or a field (if 'this' is being mapped). 19735 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 19736 assert(CurDeclaration && "Null decl on map clause."); 19737 assert( 19738 CurDeclaration->isCanonicalDecl() && 19739 "Expecting components to have associated only canonical declarations."); 19740 19741 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 19742 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 19743 19744 assert((VD || FD) && "Only variables or fields are expected here!"); 19745 (void)FD; 19746 19747 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 19748 // threadprivate variables cannot appear in a map clause. 19749 // OpenMP 4.5 [2.10.5, target update Construct] 19750 // threadprivate variables cannot appear in a from clause. 19751 if (VD && DSAS->isThreadPrivate(VD)) { 19752 if (NoDiagnose) 19753 continue; 19754 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19755 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 19756 << getOpenMPClauseName(CKind); 19757 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 19758 continue; 19759 } 19760 19761 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19762 // A list item cannot appear in both a map clause and a data-sharing 19763 // attribute clause on the same construct. 19764 19765 // Check conflicts with other map clause expressions. We check the conflicts 19766 // with the current construct separately from the enclosing data 19767 // environment, because the restrictions are different. We only have to 19768 // check conflicts across regions for the map clauses. 19769 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19770 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 19771 break; 19772 if (CKind == OMPC_map && 19773 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 19774 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19775 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 19776 break; 19777 19778 // OpenMP 4.5 [2.10.5, target update Construct] 19779 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19780 // If the type of a list item is a reference to a type T then the type will 19781 // be considered to be T for all purposes of this clause. 19782 auto I = llvm::find_if( 19783 CurComponents, 19784 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 19785 return MC.getAssociatedDeclaration(); 19786 }); 19787 assert(I != CurComponents.end() && "Null decl on map clause."); 19788 (void)I; 19789 QualType Type; 19790 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 19791 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 19792 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 19793 if (ASE) { 19794 Type = ASE->getType().getNonReferenceType(); 19795 } else if (OASE) { 19796 QualType BaseType = 19797 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 19798 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 19799 Type = ATy->getElementType(); 19800 else 19801 Type = BaseType->getPointeeType(); 19802 Type = Type.getNonReferenceType(); 19803 } else if (OAShE) { 19804 Type = OAShE->getBase()->getType()->getPointeeType(); 19805 } else { 19806 Type = VE->getType(); 19807 } 19808 19809 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 19810 // A list item in a to or from clause must have a mappable type. 19811 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19812 // A list item must have a mappable type. 19813 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 19814 DSAS, Type, /*FullCheck=*/true)) 19815 continue; 19816 19817 if (CKind == OMPC_map) { 19818 // target enter data 19819 // OpenMP [2.10.2, Restrictions, p. 99] 19820 // A map-type must be specified in all map clauses and must be either 19821 // to or alloc. 19822 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 19823 if (DKind == OMPD_target_enter_data && 19824 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 19825 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19826 << (IsMapTypeImplicit ? 1 : 0) 19827 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19828 << getOpenMPDirectiveName(DKind); 19829 continue; 19830 } 19831 19832 // target exit_data 19833 // OpenMP [2.10.3, Restrictions, p. 102] 19834 // A map-type must be specified in all map clauses and must be either 19835 // from, release, or delete. 19836 if (DKind == OMPD_target_exit_data && 19837 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 19838 MapType == OMPC_MAP_delete)) { 19839 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19840 << (IsMapTypeImplicit ? 1 : 0) 19841 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19842 << getOpenMPDirectiveName(DKind); 19843 continue; 19844 } 19845 19846 // The 'ompx_hold' modifier is specifically intended to be used on a 19847 // 'target' or 'target data' directive to prevent data from being unmapped 19848 // during the associated statement. It is not permitted on a 'target 19849 // enter data' or 'target exit data' directive, which have no associated 19850 // statement. 19851 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) && 19852 HasHoldModifier) { 19853 SemaRef.Diag(StartLoc, 19854 diag::err_omp_invalid_map_type_modifier_for_directive) 19855 << getOpenMPSimpleClauseTypeName(OMPC_map, 19856 OMPC_MAP_MODIFIER_ompx_hold) 19857 << getOpenMPDirectiveName(DKind); 19858 continue; 19859 } 19860 19861 // target, target data 19862 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 19863 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 19864 // A map-type in a map clause must be to, from, tofrom or alloc 19865 if ((DKind == OMPD_target_data || 19866 isOpenMPTargetExecutionDirective(DKind)) && 19867 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 19868 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 19869 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19870 << (IsMapTypeImplicit ? 1 : 0) 19871 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19872 << getOpenMPDirectiveName(DKind); 19873 continue; 19874 } 19875 19876 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 19877 // A list item cannot appear in both a map clause and a data-sharing 19878 // attribute clause on the same construct 19879 // 19880 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 19881 // A list item cannot appear in both a map clause and a data-sharing 19882 // attribute clause on the same construct unless the construct is a 19883 // combined construct. 19884 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 19885 isOpenMPTargetExecutionDirective(DKind)) || 19886 DKind == OMPD_target)) { 19887 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19888 if (isOpenMPPrivate(DVar.CKind)) { 19889 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 19890 << getOpenMPClauseName(DVar.CKind) 19891 << getOpenMPClauseName(OMPC_map) 19892 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 19893 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 19894 continue; 19895 } 19896 } 19897 } 19898 19899 // Try to find the associated user-defined mapper. 19900 ExprResult ER = buildUserDefinedMapperRef( 19901 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19902 Type.getCanonicalType(), UnresolvedMapper); 19903 if (ER.isInvalid()) 19904 continue; 19905 MVLI.UDMapperList.push_back(ER.get()); 19906 19907 // Save the current expression. 19908 MVLI.ProcessedVarList.push_back(RE); 19909 19910 // Store the components in the stack so that they can be used to check 19911 // against other clauses later on. 19912 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 19913 /*WhereFoundClauseKind=*/OMPC_map); 19914 19915 // Save the components and declaration to create the clause. For purposes of 19916 // the clause creation, any component list that has has base 'this' uses 19917 // null as base declaration. 19918 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19919 MVLI.VarComponents.back().append(CurComponents.begin(), 19920 CurComponents.end()); 19921 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 19922 : CurDeclaration); 19923 } 19924 } 19925 19926 OMPClause *Sema::ActOnOpenMPMapClause( 19927 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 19928 ArrayRef<SourceLocation> MapTypeModifiersLoc, 19929 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19930 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 19931 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19932 const OMPVarListLocTy &Locs, bool NoDiagnose, 19933 ArrayRef<Expr *> UnresolvedMappers) { 19934 OpenMPMapModifierKind Modifiers[] = { 19935 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19936 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19937 OMPC_MAP_MODIFIER_unknown}; 19938 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 19939 19940 // Process map-type-modifiers, flag errors for duplicate modifiers. 19941 unsigned Count = 0; 19942 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 19943 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 19944 llvm::is_contained(Modifiers, MapTypeModifiers[I])) { 19945 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 19946 continue; 19947 } 19948 assert(Count < NumberOfOMPMapClauseModifiers && 19949 "Modifiers exceed the allowed number of map type modifiers"); 19950 Modifiers[Count] = MapTypeModifiers[I]; 19951 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 19952 ++Count; 19953 } 19954 19955 MappableVarListInfo MVLI(VarList); 19956 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 19957 MapperIdScopeSpec, MapperId, UnresolvedMappers, 19958 MapType, Modifiers, IsMapTypeImplicit, 19959 NoDiagnose); 19960 19961 // We need to produce a map clause even if we don't have variables so that 19962 // other diagnostics related with non-existing map clauses are accurate. 19963 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 19964 MVLI.VarBaseDeclarations, MVLI.VarComponents, 19965 MVLI.UDMapperList, Modifiers, ModifiersLoc, 19966 MapperIdScopeSpec.getWithLocInContext(Context), 19967 MapperId, MapType, IsMapTypeImplicit, MapLoc); 19968 } 19969 19970 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 19971 TypeResult ParsedType) { 19972 assert(ParsedType.isUsable()); 19973 19974 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 19975 if (ReductionType.isNull()) 19976 return QualType(); 19977 19978 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 19979 // A type name in a declare reduction directive cannot be a function type, an 19980 // array type, a reference type, or a type qualified with const, volatile or 19981 // restrict. 19982 if (ReductionType.hasQualifiers()) { 19983 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 19984 return QualType(); 19985 } 19986 19987 if (ReductionType->isFunctionType()) { 19988 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 19989 return QualType(); 19990 } 19991 if (ReductionType->isReferenceType()) { 19992 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 19993 return QualType(); 19994 } 19995 if (ReductionType->isArrayType()) { 19996 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 19997 return QualType(); 19998 } 19999 return ReductionType; 20000 } 20001 20002 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 20003 Scope *S, DeclContext *DC, DeclarationName Name, 20004 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 20005 AccessSpecifier AS, Decl *PrevDeclInScope) { 20006 SmallVector<Decl *, 8> Decls; 20007 Decls.reserve(ReductionTypes.size()); 20008 20009 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 20010 forRedeclarationInCurContext()); 20011 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 20012 // A reduction-identifier may not be re-declared in the current scope for the 20013 // same type or for a type that is compatible according to the base language 20014 // rules. 20015 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 20016 OMPDeclareReductionDecl *PrevDRD = nullptr; 20017 bool InCompoundScope = true; 20018 if (S != nullptr) { 20019 // Find previous declaration with the same name not referenced in other 20020 // declarations. 20021 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 20022 InCompoundScope = 20023 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 20024 LookupName(Lookup, S); 20025 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 20026 /*AllowInlineNamespace=*/false); 20027 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 20028 LookupResult::Filter Filter = Lookup.makeFilter(); 20029 while (Filter.hasNext()) { 20030 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 20031 if (InCompoundScope) { 20032 auto I = UsedAsPrevious.find(PrevDecl); 20033 if (I == UsedAsPrevious.end()) 20034 UsedAsPrevious[PrevDecl] = false; 20035 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 20036 UsedAsPrevious[D] = true; 20037 } 20038 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 20039 PrevDecl->getLocation(); 20040 } 20041 Filter.done(); 20042 if (InCompoundScope) { 20043 for (const auto &PrevData : UsedAsPrevious) { 20044 if (!PrevData.second) { 20045 PrevDRD = PrevData.first; 20046 break; 20047 } 20048 } 20049 } 20050 } else if (PrevDeclInScope != nullptr) { 20051 auto *PrevDRDInScope = PrevDRD = 20052 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 20053 do { 20054 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 20055 PrevDRDInScope->getLocation(); 20056 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 20057 } while (PrevDRDInScope != nullptr); 20058 } 20059 for (const auto &TyData : ReductionTypes) { 20060 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 20061 bool Invalid = false; 20062 if (I != PreviousRedeclTypes.end()) { 20063 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 20064 << TyData.first; 20065 Diag(I->second, diag::note_previous_definition); 20066 Invalid = true; 20067 } 20068 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 20069 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 20070 Name, TyData.first, PrevDRD); 20071 DC->addDecl(DRD); 20072 DRD->setAccess(AS); 20073 Decls.push_back(DRD); 20074 if (Invalid) 20075 DRD->setInvalidDecl(); 20076 else 20077 PrevDRD = DRD; 20078 } 20079 20080 return DeclGroupPtrTy::make( 20081 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 20082 } 20083 20084 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 20085 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20086 20087 // Enter new function scope. 20088 PushFunctionScope(); 20089 setFunctionHasBranchProtectedScope(); 20090 getCurFunction()->setHasOMPDeclareReductionCombiner(); 20091 20092 if (S != nullptr) 20093 PushDeclContext(S, DRD); 20094 else 20095 CurContext = DRD; 20096 20097 PushExpressionEvaluationContext( 20098 ExpressionEvaluationContext::PotentiallyEvaluated); 20099 20100 QualType ReductionType = DRD->getType(); 20101 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 20102 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 20103 // uses semantics of argument handles by value, but it should be passed by 20104 // reference. C lang does not support references, so pass all parameters as 20105 // pointers. 20106 // Create 'T omp_in;' variable. 20107 VarDecl *OmpInParm = 20108 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 20109 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 20110 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 20111 // uses semantics of argument handles by value, but it should be passed by 20112 // reference. C lang does not support references, so pass all parameters as 20113 // pointers. 20114 // Create 'T omp_out;' variable. 20115 VarDecl *OmpOutParm = 20116 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 20117 if (S != nullptr) { 20118 PushOnScopeChains(OmpInParm, S); 20119 PushOnScopeChains(OmpOutParm, S); 20120 } else { 20121 DRD->addDecl(OmpInParm); 20122 DRD->addDecl(OmpOutParm); 20123 } 20124 Expr *InE = 20125 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 20126 Expr *OutE = 20127 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 20128 DRD->setCombinerData(InE, OutE); 20129 } 20130 20131 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 20132 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20133 DiscardCleanupsInEvaluationContext(); 20134 PopExpressionEvaluationContext(); 20135 20136 PopDeclContext(); 20137 PopFunctionScopeInfo(); 20138 20139 if (Combiner != nullptr) 20140 DRD->setCombiner(Combiner); 20141 else 20142 DRD->setInvalidDecl(); 20143 } 20144 20145 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 20146 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20147 20148 // Enter new function scope. 20149 PushFunctionScope(); 20150 setFunctionHasBranchProtectedScope(); 20151 20152 if (S != nullptr) 20153 PushDeclContext(S, DRD); 20154 else 20155 CurContext = DRD; 20156 20157 PushExpressionEvaluationContext( 20158 ExpressionEvaluationContext::PotentiallyEvaluated); 20159 20160 QualType ReductionType = DRD->getType(); 20161 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 20162 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 20163 // uses semantics of argument handles by value, but it should be passed by 20164 // reference. C lang does not support references, so pass all parameters as 20165 // pointers. 20166 // Create 'T omp_priv;' variable. 20167 VarDecl *OmpPrivParm = 20168 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 20169 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 20170 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 20171 // uses semantics of argument handles by value, but it should be passed by 20172 // reference. C lang does not support references, so pass all parameters as 20173 // pointers. 20174 // Create 'T omp_orig;' variable. 20175 VarDecl *OmpOrigParm = 20176 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 20177 if (S != nullptr) { 20178 PushOnScopeChains(OmpPrivParm, S); 20179 PushOnScopeChains(OmpOrigParm, S); 20180 } else { 20181 DRD->addDecl(OmpPrivParm); 20182 DRD->addDecl(OmpOrigParm); 20183 } 20184 Expr *OrigE = 20185 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 20186 Expr *PrivE = 20187 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 20188 DRD->setInitializerData(OrigE, PrivE); 20189 return OmpPrivParm; 20190 } 20191 20192 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 20193 VarDecl *OmpPrivParm) { 20194 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20195 DiscardCleanupsInEvaluationContext(); 20196 PopExpressionEvaluationContext(); 20197 20198 PopDeclContext(); 20199 PopFunctionScopeInfo(); 20200 20201 if (Initializer != nullptr) { 20202 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 20203 } else if (OmpPrivParm->hasInit()) { 20204 DRD->setInitializer(OmpPrivParm->getInit(), 20205 OmpPrivParm->isDirectInit() 20206 ? OMPDeclareReductionDecl::DirectInit 20207 : OMPDeclareReductionDecl::CopyInit); 20208 } else { 20209 DRD->setInvalidDecl(); 20210 } 20211 } 20212 20213 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 20214 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 20215 for (Decl *D : DeclReductions.get()) { 20216 if (IsValid) { 20217 if (S) 20218 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 20219 /*AddToContext=*/false); 20220 } else { 20221 D->setInvalidDecl(); 20222 } 20223 } 20224 return DeclReductions; 20225 } 20226 20227 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 20228 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 20229 QualType T = TInfo->getType(); 20230 if (D.isInvalidType()) 20231 return true; 20232 20233 if (getLangOpts().CPlusPlus) { 20234 // Check that there are no default arguments (C++ only). 20235 CheckExtraCXXDefaultArguments(D); 20236 } 20237 20238 return CreateParsedType(T, TInfo); 20239 } 20240 20241 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 20242 TypeResult ParsedType) { 20243 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 20244 20245 QualType MapperType = GetTypeFromParser(ParsedType.get()); 20246 assert(!MapperType.isNull() && "Expect valid mapper type"); 20247 20248 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20249 // The type must be of struct, union or class type in C and C++ 20250 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 20251 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 20252 return QualType(); 20253 } 20254 return MapperType; 20255 } 20256 20257 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 20258 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 20259 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 20260 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 20261 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 20262 forRedeclarationInCurContext()); 20263 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20264 // A mapper-identifier may not be redeclared in the current scope for the 20265 // same type or for a type that is compatible according to the base language 20266 // rules. 20267 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 20268 OMPDeclareMapperDecl *PrevDMD = nullptr; 20269 bool InCompoundScope = true; 20270 if (S != nullptr) { 20271 // Find previous declaration with the same name not referenced in other 20272 // declarations. 20273 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 20274 InCompoundScope = 20275 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 20276 LookupName(Lookup, S); 20277 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 20278 /*AllowInlineNamespace=*/false); 20279 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 20280 LookupResult::Filter Filter = Lookup.makeFilter(); 20281 while (Filter.hasNext()) { 20282 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 20283 if (InCompoundScope) { 20284 auto I = UsedAsPrevious.find(PrevDecl); 20285 if (I == UsedAsPrevious.end()) 20286 UsedAsPrevious[PrevDecl] = false; 20287 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 20288 UsedAsPrevious[D] = true; 20289 } 20290 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 20291 PrevDecl->getLocation(); 20292 } 20293 Filter.done(); 20294 if (InCompoundScope) { 20295 for (const auto &PrevData : UsedAsPrevious) { 20296 if (!PrevData.second) { 20297 PrevDMD = PrevData.first; 20298 break; 20299 } 20300 } 20301 } 20302 } else if (PrevDeclInScope) { 20303 auto *PrevDMDInScope = PrevDMD = 20304 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 20305 do { 20306 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 20307 PrevDMDInScope->getLocation(); 20308 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 20309 } while (PrevDMDInScope != nullptr); 20310 } 20311 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 20312 bool Invalid = false; 20313 if (I != PreviousRedeclTypes.end()) { 20314 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 20315 << MapperType << Name; 20316 Diag(I->second, diag::note_previous_definition); 20317 Invalid = true; 20318 } 20319 // Build expressions for implicit maps of data members with 'default' 20320 // mappers. 20321 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(), 20322 Clauses.end()); 20323 if (LangOpts.OpenMP >= 50) 20324 processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit); 20325 auto *DMD = 20326 OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN, 20327 ClausesWithImplicit, PrevDMD); 20328 if (S) 20329 PushOnScopeChains(DMD, S); 20330 else 20331 DC->addDecl(DMD); 20332 DMD->setAccess(AS); 20333 if (Invalid) 20334 DMD->setInvalidDecl(); 20335 20336 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 20337 VD->setDeclContext(DMD); 20338 VD->setLexicalDeclContext(DMD); 20339 DMD->addDecl(VD); 20340 DMD->setMapperVarRef(MapperVarRef); 20341 20342 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 20343 } 20344 20345 ExprResult 20346 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 20347 SourceLocation StartLoc, 20348 DeclarationName VN) { 20349 TypeSourceInfo *TInfo = 20350 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 20351 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 20352 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 20353 MapperType, TInfo, SC_None); 20354 if (S) 20355 PushOnScopeChains(VD, S, /*AddToContext=*/false); 20356 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 20357 DSAStack->addDeclareMapperVarRef(E); 20358 return E; 20359 } 20360 20361 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 20362 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20363 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 20364 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) { 20365 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl()) 20366 return true; 20367 if (VD->isUsableInConstantExpressions(Context)) 20368 return true; 20369 return false; 20370 } 20371 return true; 20372 } 20373 20374 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 20375 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20376 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 20377 } 20378 20379 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 20380 SourceLocation StartLoc, 20381 SourceLocation LParenLoc, 20382 SourceLocation EndLoc) { 20383 Expr *ValExpr = NumTeams; 20384 Stmt *HelperValStmt = nullptr; 20385 20386 // OpenMP [teams Constrcut, Restrictions] 20387 // The num_teams expression must evaluate to a positive integer value. 20388 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 20389 /*StrictlyPositive=*/true)) 20390 return nullptr; 20391 20392 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20393 OpenMPDirectiveKind CaptureRegion = 20394 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 20395 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20396 ValExpr = MakeFullExpr(ValExpr).get(); 20397 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20398 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20399 HelperValStmt = buildPreInits(Context, Captures); 20400 } 20401 20402 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 20403 StartLoc, LParenLoc, EndLoc); 20404 } 20405 20406 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 20407 SourceLocation StartLoc, 20408 SourceLocation LParenLoc, 20409 SourceLocation EndLoc) { 20410 Expr *ValExpr = ThreadLimit; 20411 Stmt *HelperValStmt = nullptr; 20412 20413 // OpenMP [teams Constrcut, Restrictions] 20414 // The thread_limit expression must evaluate to a positive integer value. 20415 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 20416 /*StrictlyPositive=*/true)) 20417 return nullptr; 20418 20419 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20420 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 20421 DKind, OMPC_thread_limit, LangOpts.OpenMP); 20422 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20423 ValExpr = MakeFullExpr(ValExpr).get(); 20424 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20425 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20426 HelperValStmt = buildPreInits(Context, Captures); 20427 } 20428 20429 return new (Context) OMPThreadLimitClause( 20430 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 20431 } 20432 20433 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 20434 SourceLocation StartLoc, 20435 SourceLocation LParenLoc, 20436 SourceLocation EndLoc) { 20437 Expr *ValExpr = Priority; 20438 Stmt *HelperValStmt = nullptr; 20439 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20440 20441 // OpenMP [2.9.1, task Constrcut] 20442 // The priority-value is a non-negative numerical scalar expression. 20443 if (!isNonNegativeIntegerValue( 20444 ValExpr, *this, OMPC_priority, 20445 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 20446 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20447 return nullptr; 20448 20449 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 20450 StartLoc, LParenLoc, EndLoc); 20451 } 20452 20453 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 20454 SourceLocation StartLoc, 20455 SourceLocation LParenLoc, 20456 SourceLocation EndLoc) { 20457 Expr *ValExpr = Grainsize; 20458 Stmt *HelperValStmt = nullptr; 20459 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20460 20461 // OpenMP [2.9.2, taskloop Constrcut] 20462 // The parameter of the grainsize clause must be a positive integer 20463 // expression. 20464 if (!isNonNegativeIntegerValue( 20465 ValExpr, *this, OMPC_grainsize, 20466 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20467 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20468 return nullptr; 20469 20470 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 20471 StartLoc, LParenLoc, EndLoc); 20472 } 20473 20474 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 20475 SourceLocation StartLoc, 20476 SourceLocation LParenLoc, 20477 SourceLocation EndLoc) { 20478 Expr *ValExpr = NumTasks; 20479 Stmt *HelperValStmt = nullptr; 20480 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20481 20482 // OpenMP [2.9.2, taskloop Constrcut] 20483 // The parameter of the num_tasks clause must be a positive integer 20484 // expression. 20485 if (!isNonNegativeIntegerValue( 20486 ValExpr, *this, OMPC_num_tasks, 20487 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20488 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20489 return nullptr; 20490 20491 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 20492 StartLoc, LParenLoc, EndLoc); 20493 } 20494 20495 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 20496 SourceLocation LParenLoc, 20497 SourceLocation EndLoc) { 20498 // OpenMP [2.13.2, critical construct, Description] 20499 // ... where hint-expression is an integer constant expression that evaluates 20500 // to a valid lock hint. 20501 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 20502 if (HintExpr.isInvalid()) 20503 return nullptr; 20504 return new (Context) 20505 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 20506 } 20507 20508 /// Tries to find omp_event_handle_t type. 20509 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 20510 DSAStackTy *Stack) { 20511 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 20512 if (!OMPEventHandleT.isNull()) 20513 return true; 20514 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 20515 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 20516 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 20517 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 20518 return false; 20519 } 20520 Stack->setOMPEventHandleT(PT.get()); 20521 return true; 20522 } 20523 20524 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 20525 SourceLocation LParenLoc, 20526 SourceLocation EndLoc) { 20527 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 20528 !Evt->isInstantiationDependent() && 20529 !Evt->containsUnexpandedParameterPack()) { 20530 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 20531 return nullptr; 20532 // OpenMP 5.0, 2.10.1 task Construct. 20533 // event-handle is a variable of the omp_event_handle_t type. 20534 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 20535 if (!Ref) { 20536 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20537 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20538 return nullptr; 20539 } 20540 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 20541 if (!VD) { 20542 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20543 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20544 return nullptr; 20545 } 20546 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 20547 VD->getType()) || 20548 VD->getType().isConstant(Context)) { 20549 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20550 << "omp_event_handle_t" << 1 << VD->getType() 20551 << Evt->getSourceRange(); 20552 return nullptr; 20553 } 20554 // OpenMP 5.0, 2.10.1 task Construct 20555 // [detach clause]... The event-handle will be considered as if it was 20556 // specified on a firstprivate clause. 20557 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 20558 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 20559 DVar.RefExpr) { 20560 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 20561 << getOpenMPClauseName(DVar.CKind) 20562 << getOpenMPClauseName(OMPC_firstprivate); 20563 reportOriginalDsa(*this, DSAStack, VD, DVar); 20564 return nullptr; 20565 } 20566 } 20567 20568 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 20569 } 20570 20571 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 20572 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 20573 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 20574 SourceLocation EndLoc) { 20575 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 20576 std::string Values; 20577 Values += "'"; 20578 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 20579 Values += "'"; 20580 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20581 << Values << getOpenMPClauseName(OMPC_dist_schedule); 20582 return nullptr; 20583 } 20584 Expr *ValExpr = ChunkSize; 20585 Stmt *HelperValStmt = nullptr; 20586 if (ChunkSize) { 20587 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 20588 !ChunkSize->isInstantiationDependent() && 20589 !ChunkSize->containsUnexpandedParameterPack()) { 20590 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 20591 ExprResult Val = 20592 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 20593 if (Val.isInvalid()) 20594 return nullptr; 20595 20596 ValExpr = Val.get(); 20597 20598 // OpenMP [2.7.1, Restrictions] 20599 // chunk_size must be a loop invariant integer expression with a positive 20600 // value. 20601 if (Optional<llvm::APSInt> Result = 20602 ValExpr->getIntegerConstantExpr(Context)) { 20603 if (Result->isSigned() && !Result->isStrictlyPositive()) { 20604 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 20605 << "dist_schedule" << ChunkSize->getSourceRange(); 20606 return nullptr; 20607 } 20608 } else if (getOpenMPCaptureRegionForClause( 20609 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 20610 LangOpts.OpenMP) != OMPD_unknown && 20611 !CurContext->isDependentContext()) { 20612 ValExpr = MakeFullExpr(ValExpr).get(); 20613 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20614 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20615 HelperValStmt = buildPreInits(Context, Captures); 20616 } 20617 } 20618 } 20619 20620 return new (Context) 20621 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 20622 Kind, ValExpr, HelperValStmt); 20623 } 20624 20625 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 20626 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 20627 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 20628 SourceLocation KindLoc, SourceLocation EndLoc) { 20629 if (getLangOpts().OpenMP < 50) { 20630 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 20631 Kind != OMPC_DEFAULTMAP_scalar) { 20632 std::string Value; 20633 SourceLocation Loc; 20634 Value += "'"; 20635 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 20636 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20637 OMPC_DEFAULTMAP_MODIFIER_tofrom); 20638 Loc = MLoc; 20639 } else { 20640 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20641 OMPC_DEFAULTMAP_scalar); 20642 Loc = KindLoc; 20643 } 20644 Value += "'"; 20645 Diag(Loc, diag::err_omp_unexpected_clause_value) 20646 << Value << getOpenMPClauseName(OMPC_defaultmap); 20647 return nullptr; 20648 } 20649 } else { 20650 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 20651 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 20652 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 20653 if (!isDefaultmapKind || !isDefaultmapModifier) { 20654 StringRef KindValue = "'scalar', 'aggregate', 'pointer'"; 20655 if (LangOpts.OpenMP == 50) { 20656 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 20657 "'firstprivate', 'none', 'default'"; 20658 if (!isDefaultmapKind && isDefaultmapModifier) { 20659 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20660 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20661 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20662 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20663 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20664 } else { 20665 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20666 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20667 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20668 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20669 } 20670 } else { 20671 StringRef ModifierValue = 20672 "'alloc', 'from', 'to', 'tofrom', " 20673 "'firstprivate', 'none', 'default', 'present'"; 20674 if (!isDefaultmapKind && isDefaultmapModifier) { 20675 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20676 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20677 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20678 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20679 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20680 } else { 20681 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20682 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20683 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20684 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20685 } 20686 } 20687 return nullptr; 20688 } 20689 20690 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 20691 // At most one defaultmap clause for each category can appear on the 20692 // directive. 20693 if (DSAStack->checkDefaultmapCategory(Kind)) { 20694 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 20695 return nullptr; 20696 } 20697 } 20698 if (Kind == OMPC_DEFAULTMAP_unknown) { 20699 // Variable category is not specified - mark all categories. 20700 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 20701 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 20702 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 20703 } else { 20704 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 20705 } 20706 20707 return new (Context) 20708 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 20709 } 20710 20711 bool Sema::ActOnStartOpenMPDeclareTargetContext( 20712 DeclareTargetContextInfo &DTCI) { 20713 DeclContext *CurLexicalContext = getCurLexicalContext(); 20714 if (!CurLexicalContext->isFileContext() && 20715 !CurLexicalContext->isExternCContext() && 20716 !CurLexicalContext->isExternCXXContext() && 20717 !isa<CXXRecordDecl>(CurLexicalContext) && 20718 !isa<ClassTemplateDecl>(CurLexicalContext) && 20719 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 20720 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 20721 Diag(DTCI.Loc, diag::err_omp_region_not_file_context); 20722 return false; 20723 } 20724 DeclareTargetNesting.push_back(DTCI); 20725 return true; 20726 } 20727 20728 const Sema::DeclareTargetContextInfo 20729 Sema::ActOnOpenMPEndDeclareTargetDirective() { 20730 assert(!DeclareTargetNesting.empty() && 20731 "check isInOpenMPDeclareTargetContext() first!"); 20732 return DeclareTargetNesting.pop_back_val(); 20733 } 20734 20735 void Sema::ActOnFinishedOpenMPDeclareTargetContext( 20736 DeclareTargetContextInfo &DTCI) { 20737 for (auto &It : DTCI.ExplicitlyMapped) 20738 ActOnOpenMPDeclareTargetName(It.first, It.second.Loc, It.second.MT, 20739 DTCI.DT); 20740 } 20741 20742 NamedDecl *Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, 20743 CXXScopeSpec &ScopeSpec, 20744 const DeclarationNameInfo &Id) { 20745 LookupResult Lookup(*this, Id, LookupOrdinaryName); 20746 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 20747 20748 if (Lookup.isAmbiguous()) 20749 return nullptr; 20750 Lookup.suppressDiagnostics(); 20751 20752 if (!Lookup.isSingleResult()) { 20753 VarOrFuncDeclFilterCCC CCC(*this); 20754 if (TypoCorrection Corrected = 20755 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 20756 CTK_ErrorRecovery)) { 20757 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 20758 << Id.getName()); 20759 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 20760 return nullptr; 20761 } 20762 20763 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 20764 return nullptr; 20765 } 20766 20767 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 20768 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 20769 !isa<FunctionTemplateDecl>(ND)) { 20770 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 20771 return nullptr; 20772 } 20773 return ND; 20774 } 20775 20776 void Sema::ActOnOpenMPDeclareTargetName( 20777 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 20778 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 20779 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 20780 isa<FunctionTemplateDecl>(ND)) && 20781 "Expected variable, function or function template."); 20782 20783 // Diagnose marking after use as it may lead to incorrect diagnosis and 20784 // codegen. 20785 if (LangOpts.OpenMP >= 50 && 20786 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 20787 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 20788 20789 // Explicit declare target lists have precedence. 20790 const unsigned Level = -1; 20791 20792 auto *VD = cast<ValueDecl>(ND); 20793 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20794 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20795 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getDevType() != DT && 20796 ActiveAttr.getValue()->getLevel() == Level) { 20797 Diag(Loc, diag::err_omp_device_type_mismatch) 20798 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 20799 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr( 20800 ActiveAttr.getValue()->getDevType()); 20801 return; 20802 } 20803 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getMapType() != MT && 20804 ActiveAttr.getValue()->getLevel() == Level) { 20805 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 20806 return; 20807 } 20808 20809 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() == Level) 20810 return; 20811 20812 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, Level, 20813 SourceRange(Loc, Loc)); 20814 ND->addAttr(A); 20815 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20816 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 20817 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 20818 } 20819 20820 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 20821 Sema &SemaRef, Decl *D) { 20822 if (!D || !isa<VarDecl>(D)) 20823 return; 20824 auto *VD = cast<VarDecl>(D); 20825 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 20826 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20827 if (SemaRef.LangOpts.OpenMP >= 50 && 20828 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 20829 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 20830 VD->hasGlobalStorage()) { 20831 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 20832 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 20833 // If a lambda declaration and definition appears between a 20834 // declare target directive and the matching end declare target 20835 // directive, all variables that are captured by the lambda 20836 // expression must also appear in a to clause. 20837 SemaRef.Diag(VD->getLocation(), 20838 diag::err_omp_lambda_capture_in_declare_target_not_to); 20839 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 20840 << VD << 0 << SR; 20841 return; 20842 } 20843 } 20844 if (MapTy.hasValue()) 20845 return; 20846 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 20847 SemaRef.Diag(SL, diag::note_used_here) << SR; 20848 } 20849 20850 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 20851 Sema &SemaRef, DSAStackTy *Stack, 20852 ValueDecl *VD) { 20853 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 20854 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 20855 /*FullCheck=*/false); 20856 } 20857 20858 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 20859 SourceLocation IdLoc) { 20860 if (!D || D->isInvalidDecl()) 20861 return; 20862 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 20863 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 20864 if (auto *VD = dyn_cast<VarDecl>(D)) { 20865 // Only global variables can be marked as declare target. 20866 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 20867 !VD->isStaticDataMember()) 20868 return; 20869 // 2.10.6: threadprivate variable cannot appear in a declare target 20870 // directive. 20871 if (DSAStack->isThreadPrivate(VD)) { 20872 Diag(SL, diag::err_omp_threadprivate_in_target); 20873 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 20874 return; 20875 } 20876 } 20877 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 20878 D = FTD->getTemplatedDecl(); 20879 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 20880 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 20881 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 20882 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 20883 Diag(IdLoc, diag::err_omp_function_in_link_clause); 20884 Diag(FD->getLocation(), diag::note_defined_here) << FD; 20885 return; 20886 } 20887 } 20888 if (auto *VD = dyn_cast<ValueDecl>(D)) { 20889 // Problem if any with var declared with incomplete type will be reported 20890 // as normal, so no need to check it here. 20891 if ((E || !VD->getType()->isIncompleteType()) && 20892 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 20893 return; 20894 if (!E && isInOpenMPDeclareTargetContext()) { 20895 // Checking declaration inside declare target region. 20896 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 20897 isa<FunctionTemplateDecl>(D)) { 20898 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20899 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20900 unsigned Level = DeclareTargetNesting.size(); 20901 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() >= Level) 20902 return; 20903 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back(); 20904 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20905 Context, OMPDeclareTargetDeclAttr::MT_To, DTCI.DT, Level, 20906 SourceRange(DTCI.Loc, DTCI.Loc)); 20907 D->addAttr(A); 20908 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20909 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 20910 } 20911 return; 20912 } 20913 } 20914 if (!E) 20915 return; 20916 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 20917 } 20918 20919 OMPClause *Sema::ActOnOpenMPToClause( 20920 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20921 ArrayRef<SourceLocation> MotionModifiersLoc, 20922 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20923 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20924 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20925 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20926 OMPC_MOTION_MODIFIER_unknown}; 20927 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20928 20929 // Process motion-modifiers, flag errors for duplicate modifiers. 20930 unsigned Count = 0; 20931 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20932 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20933 llvm::is_contained(Modifiers, MotionModifiers[I])) { 20934 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20935 continue; 20936 } 20937 assert(Count < NumberOfOMPMotionModifiers && 20938 "Modifiers exceed the allowed number of motion modifiers"); 20939 Modifiers[Count] = MotionModifiers[I]; 20940 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20941 ++Count; 20942 } 20943 20944 MappableVarListInfo MVLI(VarList); 20945 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 20946 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20947 if (MVLI.ProcessedVarList.empty()) 20948 return nullptr; 20949 20950 return OMPToClause::Create( 20951 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20952 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20953 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20954 } 20955 20956 OMPClause *Sema::ActOnOpenMPFromClause( 20957 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20958 ArrayRef<SourceLocation> MotionModifiersLoc, 20959 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20960 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20961 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20962 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20963 OMPC_MOTION_MODIFIER_unknown}; 20964 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20965 20966 // Process motion-modifiers, flag errors for duplicate modifiers. 20967 unsigned Count = 0; 20968 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20969 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20970 llvm::is_contained(Modifiers, MotionModifiers[I])) { 20971 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20972 continue; 20973 } 20974 assert(Count < NumberOfOMPMotionModifiers && 20975 "Modifiers exceed the allowed number of motion modifiers"); 20976 Modifiers[Count] = MotionModifiers[I]; 20977 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20978 ++Count; 20979 } 20980 20981 MappableVarListInfo MVLI(VarList); 20982 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 20983 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20984 if (MVLI.ProcessedVarList.empty()) 20985 return nullptr; 20986 20987 return OMPFromClause::Create( 20988 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20989 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20990 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20991 } 20992 20993 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 20994 const OMPVarListLocTy &Locs) { 20995 MappableVarListInfo MVLI(VarList); 20996 SmallVector<Expr *, 8> PrivateCopies; 20997 SmallVector<Expr *, 8> Inits; 20998 20999 for (Expr *RefExpr : VarList) { 21000 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 21001 SourceLocation ELoc; 21002 SourceRange ERange; 21003 Expr *SimpleRefExpr = RefExpr; 21004 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21005 if (Res.second) { 21006 // It will be analyzed later. 21007 MVLI.ProcessedVarList.push_back(RefExpr); 21008 PrivateCopies.push_back(nullptr); 21009 Inits.push_back(nullptr); 21010 } 21011 ValueDecl *D = Res.first; 21012 if (!D) 21013 continue; 21014 21015 QualType Type = D->getType(); 21016 Type = Type.getNonReferenceType().getUnqualifiedType(); 21017 21018 auto *VD = dyn_cast<VarDecl>(D); 21019 21020 // Item should be a pointer or reference to pointer. 21021 if (!Type->isPointerType()) { 21022 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 21023 << 0 << RefExpr->getSourceRange(); 21024 continue; 21025 } 21026 21027 // Build the private variable and the expression that refers to it. 21028 auto VDPrivate = 21029 buildVarDecl(*this, ELoc, Type, D->getName(), 21030 D->hasAttrs() ? &D->getAttrs() : nullptr, 21031 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 21032 if (VDPrivate->isInvalidDecl()) 21033 continue; 21034 21035 CurContext->addDecl(VDPrivate); 21036 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 21037 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 21038 21039 // Add temporary variable to initialize the private copy of the pointer. 21040 VarDecl *VDInit = 21041 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 21042 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 21043 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 21044 AddInitializerToDecl(VDPrivate, 21045 DefaultLvalueConversion(VDInitRefExpr).get(), 21046 /*DirectInit=*/false); 21047 21048 // If required, build a capture to implement the privatization initialized 21049 // with the current list item value. 21050 DeclRefExpr *Ref = nullptr; 21051 if (!VD) 21052 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 21053 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 21054 PrivateCopies.push_back(VDPrivateRefExpr); 21055 Inits.push_back(VDInitRefExpr); 21056 21057 // We need to add a data sharing attribute for this variable to make sure it 21058 // is correctly captured. A variable that shows up in a use_device_ptr has 21059 // similar properties of a first private variable. 21060 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 21061 21062 // Create a mappable component for the list item. List items in this clause 21063 // only need a component. 21064 MVLI.VarBaseDeclarations.push_back(D); 21065 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 21066 MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D, 21067 /*IsNonContiguous=*/false); 21068 } 21069 21070 if (MVLI.ProcessedVarList.empty()) 21071 return nullptr; 21072 21073 return OMPUseDevicePtrClause::Create( 21074 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 21075 MVLI.VarBaseDeclarations, MVLI.VarComponents); 21076 } 21077 21078 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 21079 const OMPVarListLocTy &Locs) { 21080 MappableVarListInfo MVLI(VarList); 21081 21082 for (Expr *RefExpr : VarList) { 21083 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 21084 SourceLocation ELoc; 21085 SourceRange ERange; 21086 Expr *SimpleRefExpr = RefExpr; 21087 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21088 /*AllowArraySection=*/true); 21089 if (Res.second) { 21090 // It will be analyzed later. 21091 MVLI.ProcessedVarList.push_back(RefExpr); 21092 } 21093 ValueDecl *D = Res.first; 21094 if (!D) 21095 continue; 21096 auto *VD = dyn_cast<VarDecl>(D); 21097 21098 // If required, build a capture to implement the privatization initialized 21099 // with the current list item value. 21100 DeclRefExpr *Ref = nullptr; 21101 if (!VD) 21102 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 21103 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 21104 21105 // We need to add a data sharing attribute for this variable to make sure it 21106 // is correctly captured. A variable that shows up in a use_device_addr has 21107 // similar properties of a first private variable. 21108 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 21109 21110 // Create a mappable component for the list item. List items in this clause 21111 // only need a component. 21112 MVLI.VarBaseDeclarations.push_back(D); 21113 MVLI.VarComponents.emplace_back(); 21114 Expr *Component = SimpleRefExpr; 21115 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 21116 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 21117 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 21118 MVLI.VarComponents.back().emplace_back(Component, D, 21119 /*IsNonContiguous=*/false); 21120 } 21121 21122 if (MVLI.ProcessedVarList.empty()) 21123 return nullptr; 21124 21125 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 21126 MVLI.VarBaseDeclarations, 21127 MVLI.VarComponents); 21128 } 21129 21130 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 21131 const OMPVarListLocTy &Locs) { 21132 MappableVarListInfo MVLI(VarList); 21133 for (Expr *RefExpr : VarList) { 21134 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 21135 SourceLocation ELoc; 21136 SourceRange ERange; 21137 Expr *SimpleRefExpr = RefExpr; 21138 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21139 if (Res.second) { 21140 // It will be analyzed later. 21141 MVLI.ProcessedVarList.push_back(RefExpr); 21142 } 21143 ValueDecl *D = Res.first; 21144 if (!D) 21145 continue; 21146 21147 QualType Type = D->getType(); 21148 // item should be a pointer or array or reference to pointer or array 21149 if (!Type.getNonReferenceType()->isPointerType() && 21150 !Type.getNonReferenceType()->isArrayType()) { 21151 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 21152 << 0 << RefExpr->getSourceRange(); 21153 continue; 21154 } 21155 21156 // Check if the declaration in the clause does not show up in any data 21157 // sharing attribute. 21158 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 21159 if (isOpenMPPrivate(DVar.CKind)) { 21160 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 21161 << getOpenMPClauseName(DVar.CKind) 21162 << getOpenMPClauseName(OMPC_is_device_ptr) 21163 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 21164 reportOriginalDsa(*this, DSAStack, D, DVar); 21165 continue; 21166 } 21167 21168 const Expr *ConflictExpr; 21169 if (DSAStack->checkMappableExprComponentListsForDecl( 21170 D, /*CurrentRegionOnly=*/true, 21171 [&ConflictExpr]( 21172 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 21173 OpenMPClauseKind) -> bool { 21174 ConflictExpr = R.front().getAssociatedExpression(); 21175 return true; 21176 })) { 21177 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 21178 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 21179 << ConflictExpr->getSourceRange(); 21180 continue; 21181 } 21182 21183 // Store the components in the stack so that they can be used to check 21184 // against other clauses later on. 21185 OMPClauseMappableExprCommon::MappableComponent MC( 21186 SimpleRefExpr, D, /*IsNonContiguous=*/false); 21187 DSAStack->addMappableExpressionComponents( 21188 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 21189 21190 // Record the expression we've just processed. 21191 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 21192 21193 // Create a mappable component for the list item. List items in this clause 21194 // only need a component. We use a null declaration to signal fields in 21195 // 'this'. 21196 assert((isa<DeclRefExpr>(SimpleRefExpr) || 21197 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 21198 "Unexpected device pointer expression!"); 21199 MVLI.VarBaseDeclarations.push_back( 21200 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 21201 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 21202 MVLI.VarComponents.back().push_back(MC); 21203 } 21204 21205 if (MVLI.ProcessedVarList.empty()) 21206 return nullptr; 21207 21208 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 21209 MVLI.VarBaseDeclarations, 21210 MVLI.VarComponents); 21211 } 21212 21213 OMPClause *Sema::ActOnOpenMPAllocateClause( 21214 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 21215 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 21216 if (Allocator) { 21217 // OpenMP [2.11.4 allocate Clause, Description] 21218 // allocator is an expression of omp_allocator_handle_t type. 21219 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 21220 return nullptr; 21221 21222 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 21223 if (AllocatorRes.isInvalid()) 21224 return nullptr; 21225 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 21226 DSAStack->getOMPAllocatorHandleT(), 21227 Sema::AA_Initializing, 21228 /*AllowExplicit=*/true); 21229 if (AllocatorRes.isInvalid()) 21230 return nullptr; 21231 Allocator = AllocatorRes.get(); 21232 } else { 21233 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 21234 // allocate clauses that appear on a target construct or on constructs in a 21235 // target region must specify an allocator expression unless a requires 21236 // directive with the dynamic_allocators clause is present in the same 21237 // compilation unit. 21238 if (LangOpts.OpenMPIsDevice && 21239 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 21240 targetDiag(StartLoc, diag::err_expected_allocator_expression); 21241 } 21242 // Analyze and build list of variables. 21243 SmallVector<Expr *, 8> Vars; 21244 for (Expr *RefExpr : VarList) { 21245 assert(RefExpr && "NULL expr in OpenMP private clause."); 21246 SourceLocation ELoc; 21247 SourceRange ERange; 21248 Expr *SimpleRefExpr = RefExpr; 21249 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21250 if (Res.second) { 21251 // It will be analyzed later. 21252 Vars.push_back(RefExpr); 21253 } 21254 ValueDecl *D = Res.first; 21255 if (!D) 21256 continue; 21257 21258 auto *VD = dyn_cast<VarDecl>(D); 21259 DeclRefExpr *Ref = nullptr; 21260 if (!VD && !CurContext->isDependentContext()) 21261 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 21262 Vars.push_back((VD || CurContext->isDependentContext()) 21263 ? RefExpr->IgnoreParens() 21264 : Ref); 21265 } 21266 21267 if (Vars.empty()) 21268 return nullptr; 21269 21270 if (Allocator) 21271 DSAStack->addInnerAllocatorExpr(Allocator); 21272 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 21273 ColonLoc, EndLoc, Vars); 21274 } 21275 21276 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 21277 SourceLocation StartLoc, 21278 SourceLocation LParenLoc, 21279 SourceLocation EndLoc) { 21280 SmallVector<Expr *, 8> Vars; 21281 for (Expr *RefExpr : VarList) { 21282 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21283 SourceLocation ELoc; 21284 SourceRange ERange; 21285 Expr *SimpleRefExpr = RefExpr; 21286 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21287 if (Res.second) 21288 // It will be analyzed later. 21289 Vars.push_back(RefExpr); 21290 ValueDecl *D = Res.first; 21291 if (!D) 21292 continue; 21293 21294 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 21295 // A list-item cannot appear in more than one nontemporal clause. 21296 if (const Expr *PrevRef = 21297 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 21298 Diag(ELoc, diag::err_omp_used_in_clause_twice) 21299 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 21300 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 21301 << getOpenMPClauseName(OMPC_nontemporal); 21302 continue; 21303 } 21304 21305 Vars.push_back(RefExpr); 21306 } 21307 21308 if (Vars.empty()) 21309 return nullptr; 21310 21311 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21312 Vars); 21313 } 21314 21315 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 21316 SourceLocation StartLoc, 21317 SourceLocation LParenLoc, 21318 SourceLocation EndLoc) { 21319 SmallVector<Expr *, 8> Vars; 21320 for (Expr *RefExpr : VarList) { 21321 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21322 SourceLocation ELoc; 21323 SourceRange ERange; 21324 Expr *SimpleRefExpr = RefExpr; 21325 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21326 /*AllowArraySection=*/true); 21327 if (Res.second) 21328 // It will be analyzed later. 21329 Vars.push_back(RefExpr); 21330 ValueDecl *D = Res.first; 21331 if (!D) 21332 continue; 21333 21334 const DSAStackTy::DSAVarData DVar = 21335 DSAStack->getTopDSA(D, /*FromParent=*/true); 21336 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21337 // A list item that appears in the inclusive or exclusive clause must appear 21338 // in a reduction clause with the inscan modifier on the enclosing 21339 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21340 if (DVar.CKind != OMPC_reduction || 21341 DVar.Modifier != OMPC_REDUCTION_inscan) 21342 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21343 << RefExpr->getSourceRange(); 21344 21345 if (DSAStack->getParentDirective() != OMPD_unknown) 21346 DSAStack->markDeclAsUsedInScanDirective(D); 21347 Vars.push_back(RefExpr); 21348 } 21349 21350 if (Vars.empty()) 21351 return nullptr; 21352 21353 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21354 } 21355 21356 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 21357 SourceLocation StartLoc, 21358 SourceLocation LParenLoc, 21359 SourceLocation EndLoc) { 21360 SmallVector<Expr *, 8> Vars; 21361 for (Expr *RefExpr : VarList) { 21362 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21363 SourceLocation ELoc; 21364 SourceRange ERange; 21365 Expr *SimpleRefExpr = RefExpr; 21366 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21367 /*AllowArraySection=*/true); 21368 if (Res.second) 21369 // It will be analyzed later. 21370 Vars.push_back(RefExpr); 21371 ValueDecl *D = Res.first; 21372 if (!D) 21373 continue; 21374 21375 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 21376 DSAStackTy::DSAVarData DVar; 21377 if (ParentDirective != OMPD_unknown) 21378 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 21379 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21380 // A list item that appears in the inclusive or exclusive clause must appear 21381 // in a reduction clause with the inscan modifier on the enclosing 21382 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21383 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 21384 DVar.Modifier != OMPC_REDUCTION_inscan) { 21385 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21386 << RefExpr->getSourceRange(); 21387 } else { 21388 DSAStack->markDeclAsUsedInScanDirective(D); 21389 } 21390 Vars.push_back(RefExpr); 21391 } 21392 21393 if (Vars.empty()) 21394 return nullptr; 21395 21396 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21397 } 21398 21399 /// Tries to find omp_alloctrait_t type. 21400 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 21401 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 21402 if (!OMPAlloctraitT.isNull()) 21403 return true; 21404 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 21405 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 21406 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 21407 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 21408 return false; 21409 } 21410 Stack->setOMPAlloctraitT(PT.get()); 21411 return true; 21412 } 21413 21414 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 21415 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 21416 ArrayRef<UsesAllocatorsData> Data) { 21417 // OpenMP [2.12.5, target Construct] 21418 // allocator is an identifier of omp_allocator_handle_t type. 21419 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 21420 return nullptr; 21421 // OpenMP [2.12.5, target Construct] 21422 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 21423 if (llvm::any_of( 21424 Data, 21425 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 21426 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 21427 return nullptr; 21428 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 21429 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 21430 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 21431 StringRef Allocator = 21432 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 21433 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 21434 PredefinedAllocators.insert(LookupSingleName( 21435 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 21436 } 21437 21438 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 21439 for (const UsesAllocatorsData &D : Data) { 21440 Expr *AllocatorExpr = nullptr; 21441 // Check allocator expression. 21442 if (D.Allocator->isTypeDependent()) { 21443 AllocatorExpr = D.Allocator; 21444 } else { 21445 // Traits were specified - need to assign new allocator to the specified 21446 // allocator, so it must be an lvalue. 21447 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 21448 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 21449 bool IsPredefinedAllocator = false; 21450 if (DRE) 21451 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 21452 if (!DRE || 21453 !(Context.hasSameUnqualifiedType( 21454 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 21455 Context.typesAreCompatible(AllocatorExpr->getType(), 21456 DSAStack->getOMPAllocatorHandleT(), 21457 /*CompareUnqualified=*/true)) || 21458 (!IsPredefinedAllocator && 21459 (AllocatorExpr->getType().isConstant(Context) || 21460 !AllocatorExpr->isLValue()))) { 21461 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 21462 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 21463 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 21464 continue; 21465 } 21466 // OpenMP [2.12.5, target Construct] 21467 // Predefined allocators appearing in a uses_allocators clause cannot have 21468 // traits specified. 21469 if (IsPredefinedAllocator && D.AllocatorTraits) { 21470 Diag(D.AllocatorTraits->getExprLoc(), 21471 diag::err_omp_predefined_allocator_with_traits) 21472 << D.AllocatorTraits->getSourceRange(); 21473 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 21474 << cast<NamedDecl>(DRE->getDecl())->getName() 21475 << D.Allocator->getSourceRange(); 21476 continue; 21477 } 21478 // OpenMP [2.12.5, target Construct] 21479 // Non-predefined allocators appearing in a uses_allocators clause must 21480 // have traits specified. 21481 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 21482 Diag(D.Allocator->getExprLoc(), 21483 diag::err_omp_nonpredefined_allocator_without_traits); 21484 continue; 21485 } 21486 // No allocator traits - just convert it to rvalue. 21487 if (!D.AllocatorTraits) 21488 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 21489 DSAStack->addUsesAllocatorsDecl( 21490 DRE->getDecl(), 21491 IsPredefinedAllocator 21492 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 21493 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 21494 } 21495 Expr *AllocatorTraitsExpr = nullptr; 21496 if (D.AllocatorTraits) { 21497 if (D.AllocatorTraits->isTypeDependent()) { 21498 AllocatorTraitsExpr = D.AllocatorTraits; 21499 } else { 21500 // OpenMP [2.12.5, target Construct] 21501 // Arrays that contain allocator traits that appear in a uses_allocators 21502 // clause must be constant arrays, have constant values and be defined 21503 // in the same scope as the construct in which the clause appears. 21504 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 21505 // Check that traits expr is a constant array. 21506 QualType TraitTy; 21507 if (const ArrayType *Ty = 21508 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 21509 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 21510 TraitTy = ConstArrayTy->getElementType(); 21511 if (TraitTy.isNull() || 21512 !(Context.hasSameUnqualifiedType(TraitTy, 21513 DSAStack->getOMPAlloctraitT()) || 21514 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 21515 /*CompareUnqualified=*/true))) { 21516 Diag(D.AllocatorTraits->getExprLoc(), 21517 diag::err_omp_expected_array_alloctraits) 21518 << AllocatorTraitsExpr->getType(); 21519 continue; 21520 } 21521 // Do not map by default allocator traits if it is a standalone 21522 // variable. 21523 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 21524 DSAStack->addUsesAllocatorsDecl( 21525 DRE->getDecl(), 21526 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 21527 } 21528 } 21529 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 21530 NewD.Allocator = AllocatorExpr; 21531 NewD.AllocatorTraits = AllocatorTraitsExpr; 21532 NewD.LParenLoc = D.LParenLoc; 21533 NewD.RParenLoc = D.RParenLoc; 21534 } 21535 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21536 NewData); 21537 } 21538 21539 OMPClause *Sema::ActOnOpenMPAffinityClause( 21540 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 21541 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 21542 SmallVector<Expr *, 8> Vars; 21543 for (Expr *RefExpr : Locators) { 21544 assert(RefExpr && "NULL expr in OpenMP shared clause."); 21545 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 21546 // It will be analyzed later. 21547 Vars.push_back(RefExpr); 21548 continue; 21549 } 21550 21551 SourceLocation ELoc = RefExpr->getExprLoc(); 21552 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 21553 21554 if (!SimpleExpr->isLValue()) { 21555 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21556 << 1 << 0 << RefExpr->getSourceRange(); 21557 continue; 21558 } 21559 21560 ExprResult Res; 21561 { 21562 Sema::TentativeAnalysisScope Trap(*this); 21563 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 21564 } 21565 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 21566 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 21567 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21568 << 1 << 0 << RefExpr->getSourceRange(); 21569 continue; 21570 } 21571 Vars.push_back(SimpleExpr); 21572 } 21573 21574 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 21575 EndLoc, Modifier, Vars); 21576 } 21577 21578 OMPClause *Sema::ActOnOpenMPBindClause(OpenMPBindClauseKind Kind, 21579 SourceLocation KindLoc, 21580 SourceLocation StartLoc, 21581 SourceLocation LParenLoc, 21582 SourceLocation EndLoc) { 21583 if (Kind == OMPC_BIND_unknown) { 21584 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 21585 << getListOfPossibleValues(OMPC_bind, /*First=*/0, 21586 /*Last=*/unsigned(OMPC_BIND_unknown)) 21587 << getOpenMPClauseName(OMPC_bind); 21588 return nullptr; 21589 } 21590 21591 return OMPBindClause::Create(Context, Kind, KindLoc, StartLoc, LParenLoc, 21592 EndLoc); 21593 } 21594