1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/OpenMPClause.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/DiagnosticSema.h" 27 #include "clang/Basic/OpenMPKinds.h" 28 #include "clang/Basic/PartialDiagnostic.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Sema/Initialization.h" 31 #include "clang/Sema/Lookup.h" 32 #include "clang/Sema/Scope.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/SemaInternal.h" 35 #include "llvm/ADT/IndexedMap.h" 36 #include "llvm/ADT/PointerEmbeddedInt.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/ADT/StringExtras.h" 39 #include "llvm/Frontend/OpenMP/OMPConstants.h" 40 #include <set> 41 42 using namespace clang; 43 using namespace llvm::omp; 44 45 //===----------------------------------------------------------------------===// 46 // Stack of data-sharing attributes for variables 47 //===----------------------------------------------------------------------===// 48 49 static const Expr *checkMapClauseExpressionBase( 50 Sema &SemaRef, Expr *E, 51 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 52 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose); 53 54 namespace { 55 /// Default data sharing attributes, which can be applied to directive. 56 enum DefaultDataSharingAttributes { 57 DSA_unspecified = 0, /// Data sharing attribute not specified. 58 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 59 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 60 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 61 }; 62 63 /// Stack for tracking declarations used in OpenMP directives and 64 /// clauses and their data-sharing attributes. 65 class DSAStackTy { 66 public: 67 struct DSAVarData { 68 OpenMPDirectiveKind DKind = OMPD_unknown; 69 OpenMPClauseKind CKind = OMPC_unknown; 70 unsigned Modifier = 0; 71 const Expr *RefExpr = nullptr; 72 DeclRefExpr *PrivateCopy = nullptr; 73 SourceLocation ImplicitDSALoc; 74 bool AppliedToPointee = false; 75 DSAVarData() = default; 76 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 77 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 78 SourceLocation ImplicitDSALoc, unsigned Modifier, 79 bool AppliedToPointee) 80 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 81 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc), 82 AppliedToPointee(AppliedToPointee) {} 83 }; 84 using OperatorOffsetTy = 85 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 86 using DoacrossDependMapTy = 87 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 88 /// Kind of the declaration used in the uses_allocators clauses. 89 enum class UsesAllocatorsDeclKind { 90 /// Predefined allocator 91 PredefinedAllocator, 92 /// User-defined allocator 93 UserDefinedAllocator, 94 /// The declaration that represent allocator trait 95 AllocatorTrait, 96 }; 97 98 private: 99 struct DSAInfo { 100 OpenMPClauseKind Attributes = OMPC_unknown; 101 unsigned Modifier = 0; 102 /// Pointer to a reference expression and a flag which shows that the 103 /// variable is marked as lastprivate(true) or not (false). 104 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 105 DeclRefExpr *PrivateCopy = nullptr; 106 /// true if the attribute is applied to the pointee, not the variable 107 /// itself. 108 bool AppliedToPointee = false; 109 }; 110 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 111 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 112 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 113 using LoopControlVariablesMapTy = 114 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 115 /// Struct that associates a component with the clause kind where they are 116 /// found. 117 struct MappedExprComponentTy { 118 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 119 OpenMPClauseKind Kind = OMPC_unknown; 120 }; 121 using MappedExprComponentsTy = 122 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 123 using CriticalsWithHintsTy = 124 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 125 struct ReductionData { 126 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 127 SourceRange ReductionRange; 128 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 129 ReductionData() = default; 130 void set(BinaryOperatorKind BO, SourceRange RR) { 131 ReductionRange = RR; 132 ReductionOp = BO; 133 } 134 void set(const Expr *RefExpr, SourceRange RR) { 135 ReductionRange = RR; 136 ReductionOp = RefExpr; 137 } 138 }; 139 using DeclReductionMapTy = 140 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 141 struct DefaultmapInfo { 142 OpenMPDefaultmapClauseModifier ImplicitBehavior = 143 OMPC_DEFAULTMAP_MODIFIER_unknown; 144 SourceLocation SLoc; 145 DefaultmapInfo() = default; 146 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 147 : ImplicitBehavior(M), SLoc(Loc) {} 148 }; 149 150 struct SharingMapTy { 151 DeclSAMapTy SharingMap; 152 DeclReductionMapTy ReductionMap; 153 UsedRefMapTy AlignedMap; 154 UsedRefMapTy NontemporalMap; 155 MappedExprComponentsTy MappedExprComponents; 156 LoopControlVariablesMapTy LCVMap; 157 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 158 SourceLocation DefaultAttrLoc; 159 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 160 OpenMPDirectiveKind Directive = OMPD_unknown; 161 DeclarationNameInfo DirectiveName; 162 Scope *CurScope = nullptr; 163 DeclContext *Context = nullptr; 164 SourceLocation ConstructLoc; 165 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 166 /// get the data (loop counters etc.) about enclosing loop-based construct. 167 /// This data is required during codegen. 168 DoacrossDependMapTy DoacrossDepends; 169 /// First argument (Expr *) contains optional argument of the 170 /// 'ordered' clause, the second one is true if the regions has 'ordered' 171 /// clause, false otherwise. 172 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 173 unsigned AssociatedLoops = 1; 174 bool HasMutipleLoops = false; 175 const Decl *PossiblyLoopCounter = nullptr; 176 bool NowaitRegion = false; 177 bool CancelRegion = false; 178 bool LoopStart = false; 179 bool BodyComplete = false; 180 SourceLocation PrevScanLocation; 181 SourceLocation PrevOrderedLocation; 182 SourceLocation InnerTeamsRegionLoc; 183 /// Reference to the taskgroup task_reduction reference expression. 184 Expr *TaskgroupReductionRef = nullptr; 185 llvm::DenseSet<QualType> MappedClassesQualTypes; 186 SmallVector<Expr *, 4> InnerUsedAllocators; 187 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 188 /// List of globals marked as declare target link in this target region 189 /// (isOpenMPTargetExecutionDirective(Directive) == true). 190 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 191 /// List of decls used in inclusive/exclusive clauses of the scan directive. 192 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 193 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 194 UsesAllocatorsDecls; 195 Expr *DeclareMapperVar = nullptr; 196 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 197 Scope *CurScope, SourceLocation Loc) 198 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 199 ConstructLoc(Loc) {} 200 SharingMapTy() = default; 201 }; 202 203 using StackTy = SmallVector<SharingMapTy, 4>; 204 205 /// Stack of used declaration and their data-sharing attributes. 206 DeclSAMapTy Threadprivates; 207 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 208 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 209 /// true, if check for DSA must be from parent directive, false, if 210 /// from current directive. 211 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 212 Sema &SemaRef; 213 bool ForceCapturing = false; 214 /// true if all the variables in the target executable directives must be 215 /// captured by reference. 216 bool ForceCaptureByReferenceInTargetExecutable = false; 217 CriticalsWithHintsTy Criticals; 218 unsigned IgnoredStackElements = 0; 219 220 /// Iterators over the stack iterate in order from innermost to outermost 221 /// directive. 222 using const_iterator = StackTy::const_reverse_iterator; 223 const_iterator begin() const { 224 return Stack.empty() ? const_iterator() 225 : Stack.back().first.rbegin() + IgnoredStackElements; 226 } 227 const_iterator end() const { 228 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 229 } 230 using iterator = StackTy::reverse_iterator; 231 iterator begin() { 232 return Stack.empty() ? iterator() 233 : Stack.back().first.rbegin() + IgnoredStackElements; 234 } 235 iterator end() { 236 return Stack.empty() ? iterator() : Stack.back().first.rend(); 237 } 238 239 // Convenience operations to get at the elements of the stack. 240 241 bool isStackEmpty() const { 242 return Stack.empty() || 243 Stack.back().second != CurrentNonCapturingFunctionScope || 244 Stack.back().first.size() <= IgnoredStackElements; 245 } 246 size_t getStackSize() const { 247 return isStackEmpty() ? 0 248 : Stack.back().first.size() - IgnoredStackElements; 249 } 250 251 SharingMapTy *getTopOfStackOrNull() { 252 size_t Size = getStackSize(); 253 if (Size == 0) 254 return nullptr; 255 return &Stack.back().first[Size - 1]; 256 } 257 const SharingMapTy *getTopOfStackOrNull() const { 258 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 259 } 260 SharingMapTy &getTopOfStack() { 261 assert(!isStackEmpty() && "no current directive"); 262 return *getTopOfStackOrNull(); 263 } 264 const SharingMapTy &getTopOfStack() const { 265 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 266 } 267 268 SharingMapTy *getSecondOnStackOrNull() { 269 size_t Size = getStackSize(); 270 if (Size <= 1) 271 return nullptr; 272 return &Stack.back().first[Size - 2]; 273 } 274 const SharingMapTy *getSecondOnStackOrNull() const { 275 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 276 } 277 278 /// Get the stack element at a certain level (previously returned by 279 /// \c getNestingLevel). 280 /// 281 /// Note that nesting levels count from outermost to innermost, and this is 282 /// the reverse of our iteration order where new inner levels are pushed at 283 /// the front of the stack. 284 SharingMapTy &getStackElemAtLevel(unsigned Level) { 285 assert(Level < getStackSize() && "no such stack element"); 286 return Stack.back().first[Level]; 287 } 288 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 289 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 290 } 291 292 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 293 294 /// Checks if the variable is a local for OpenMP region. 295 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 296 297 /// Vector of previously declared requires directives 298 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 299 /// omp_allocator_handle_t type. 300 QualType OMPAllocatorHandleT; 301 /// omp_depend_t type. 302 QualType OMPDependT; 303 /// omp_event_handle_t type. 304 QualType OMPEventHandleT; 305 /// omp_alloctrait_t type. 306 QualType OMPAlloctraitT; 307 /// Expression for the predefined allocators. 308 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 309 nullptr}; 310 /// Vector of previously encountered target directives 311 SmallVector<SourceLocation, 2> TargetLocations; 312 SourceLocation AtomicLocation; 313 314 public: 315 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 316 317 /// Sets omp_allocator_handle_t type. 318 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 319 /// Gets omp_allocator_handle_t type. 320 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 321 /// Sets omp_alloctrait_t type. 322 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 323 /// Gets omp_alloctrait_t type. 324 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 325 /// Sets the given default allocator. 326 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 327 Expr *Allocator) { 328 OMPPredefinedAllocators[AllocatorKind] = Allocator; 329 } 330 /// Returns the specified default allocator. 331 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 332 return OMPPredefinedAllocators[AllocatorKind]; 333 } 334 /// Sets omp_depend_t type. 335 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 336 /// Gets omp_depend_t type. 337 QualType getOMPDependT() const { return OMPDependT; } 338 339 /// Sets omp_event_handle_t type. 340 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 341 /// Gets omp_event_handle_t type. 342 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 343 344 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 345 OpenMPClauseKind getClauseParsingMode() const { 346 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 347 return ClauseKindMode; 348 } 349 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 350 351 bool isBodyComplete() const { 352 const SharingMapTy *Top = getTopOfStackOrNull(); 353 return Top && Top->BodyComplete; 354 } 355 void setBodyComplete() { 356 getTopOfStack().BodyComplete = true; 357 } 358 359 bool isForceVarCapturing() const { return ForceCapturing; } 360 void setForceVarCapturing(bool V) { ForceCapturing = V; } 361 362 void setForceCaptureByReferenceInTargetExecutable(bool V) { 363 ForceCaptureByReferenceInTargetExecutable = V; 364 } 365 bool isForceCaptureByReferenceInTargetExecutable() const { 366 return ForceCaptureByReferenceInTargetExecutable; 367 } 368 369 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 370 Scope *CurScope, SourceLocation Loc) { 371 assert(!IgnoredStackElements && 372 "cannot change stack while ignoring elements"); 373 if (Stack.empty() || 374 Stack.back().second != CurrentNonCapturingFunctionScope) 375 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 376 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 377 Stack.back().first.back().DefaultAttrLoc = Loc; 378 } 379 380 void pop() { 381 assert(!IgnoredStackElements && 382 "cannot change stack while ignoring elements"); 383 assert(!Stack.back().first.empty() && 384 "Data-sharing attributes stack is empty!"); 385 Stack.back().first.pop_back(); 386 } 387 388 /// RAII object to temporarily leave the scope of a directive when we want to 389 /// logically operate in its parent. 390 class ParentDirectiveScope { 391 DSAStackTy &Self; 392 bool Active; 393 public: 394 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 395 : Self(Self), Active(false) { 396 if (Activate) 397 enable(); 398 } 399 ~ParentDirectiveScope() { disable(); } 400 void disable() { 401 if (Active) { 402 --Self.IgnoredStackElements; 403 Active = false; 404 } 405 } 406 void enable() { 407 if (!Active) { 408 ++Self.IgnoredStackElements; 409 Active = true; 410 } 411 } 412 }; 413 414 /// Marks that we're started loop parsing. 415 void loopInit() { 416 assert(isOpenMPLoopDirective(getCurrentDirective()) && 417 "Expected loop-based directive."); 418 getTopOfStack().LoopStart = true; 419 } 420 /// Start capturing of the variables in the loop context. 421 void loopStart() { 422 assert(isOpenMPLoopDirective(getCurrentDirective()) && 423 "Expected loop-based directive."); 424 getTopOfStack().LoopStart = false; 425 } 426 /// true, if variables are captured, false otherwise. 427 bool isLoopStarted() const { 428 assert(isOpenMPLoopDirective(getCurrentDirective()) && 429 "Expected loop-based directive."); 430 return !getTopOfStack().LoopStart; 431 } 432 /// Marks (or clears) declaration as possibly loop counter. 433 void resetPossibleLoopCounter(const Decl *D = nullptr) { 434 getTopOfStack().PossiblyLoopCounter = 435 D ? D->getCanonicalDecl() : D; 436 } 437 /// Gets the possible loop counter decl. 438 const Decl *getPossiblyLoopCunter() const { 439 return getTopOfStack().PossiblyLoopCounter; 440 } 441 /// Start new OpenMP region stack in new non-capturing function. 442 void pushFunction() { 443 assert(!IgnoredStackElements && 444 "cannot change stack while ignoring elements"); 445 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 446 assert(!isa<CapturingScopeInfo>(CurFnScope)); 447 CurrentNonCapturingFunctionScope = CurFnScope; 448 } 449 /// Pop region stack for non-capturing function. 450 void popFunction(const FunctionScopeInfo *OldFSI) { 451 assert(!IgnoredStackElements && 452 "cannot change stack while ignoring elements"); 453 if (!Stack.empty() && Stack.back().second == OldFSI) { 454 assert(Stack.back().first.empty()); 455 Stack.pop_back(); 456 } 457 CurrentNonCapturingFunctionScope = nullptr; 458 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 459 if (!isa<CapturingScopeInfo>(FSI)) { 460 CurrentNonCapturingFunctionScope = FSI; 461 break; 462 } 463 } 464 } 465 466 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 467 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 468 } 469 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 470 getCriticalWithHint(const DeclarationNameInfo &Name) const { 471 auto I = Criticals.find(Name.getAsString()); 472 if (I != Criticals.end()) 473 return I->second; 474 return std::make_pair(nullptr, llvm::APSInt()); 475 } 476 /// If 'aligned' declaration for given variable \a D was not seen yet, 477 /// add it and return NULL; otherwise return previous occurrence's expression 478 /// for diagnostics. 479 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 480 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 481 /// add it and return NULL; otherwise return previous occurrence's expression 482 /// for diagnostics. 483 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 484 485 /// Register specified variable as loop control variable. 486 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 487 /// Check if the specified variable is a loop control variable for 488 /// current region. 489 /// \return The index of the loop control variable in the list of associated 490 /// for-loops (from outer to inner). 491 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 492 /// Check if the specified variable is a loop control variable for 493 /// parent region. 494 /// \return The index of the loop control variable in the list of associated 495 /// for-loops (from outer to inner). 496 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 497 /// Check if the specified variable is a loop control variable for 498 /// current region. 499 /// \return The index of the loop control variable in the list of associated 500 /// for-loops (from outer to inner). 501 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 502 unsigned Level) const; 503 /// Get the loop control variable for the I-th loop (or nullptr) in 504 /// parent directive. 505 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 506 507 /// Marks the specified decl \p D as used in scan directive. 508 void markDeclAsUsedInScanDirective(ValueDecl *D) { 509 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 510 Stack->UsedInScanDirective.insert(D); 511 } 512 513 /// Checks if the specified declaration was used in the inner scan directive. 514 bool isUsedInScanDirective(ValueDecl *D) const { 515 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 516 return Stack->UsedInScanDirective.count(D) > 0; 517 return false; 518 } 519 520 /// Adds explicit data sharing attribute to the specified declaration. 521 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 522 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0, 523 bool AppliedToPointee = false); 524 525 /// Adds additional information for the reduction items with the reduction id 526 /// represented as an operator. 527 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 528 BinaryOperatorKind BOK); 529 /// Adds additional information for the reduction items with the reduction id 530 /// represented as reduction identifier. 531 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 532 const Expr *ReductionRef); 533 /// Returns the location and reduction operation from the innermost parent 534 /// region for the given \p D. 535 const DSAVarData 536 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 537 BinaryOperatorKind &BOK, 538 Expr *&TaskgroupDescriptor) const; 539 /// Returns the location and reduction operation from the innermost parent 540 /// region for the given \p D. 541 const DSAVarData 542 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 543 const Expr *&ReductionRef, 544 Expr *&TaskgroupDescriptor) const; 545 /// Return reduction reference expression for the current taskgroup or 546 /// parallel/worksharing directives with task reductions. 547 Expr *getTaskgroupReductionRef() const { 548 assert((getTopOfStack().Directive == OMPD_taskgroup || 549 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 550 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 551 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 552 "taskgroup reference expression requested for non taskgroup or " 553 "parallel/worksharing directive."); 554 return getTopOfStack().TaskgroupReductionRef; 555 } 556 /// Checks if the given \p VD declaration is actually a taskgroup reduction 557 /// descriptor variable at the \p Level of OpenMP regions. 558 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 559 return getStackElemAtLevel(Level).TaskgroupReductionRef && 560 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 561 ->getDecl() == VD; 562 } 563 564 /// Returns data sharing attributes from top of the stack for the 565 /// specified declaration. 566 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 567 /// Returns data-sharing attributes for the specified declaration. 568 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 569 /// Returns data-sharing attributes for the specified declaration. 570 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 571 /// Checks if the specified variables has data-sharing attributes which 572 /// match specified \a CPred predicate in any directive which matches \a DPred 573 /// predicate. 574 const DSAVarData 575 hasDSA(ValueDecl *D, 576 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 577 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 578 bool FromParent) const; 579 /// Checks if the specified variables has data-sharing attributes which 580 /// match specified \a CPred predicate in any innermost directive which 581 /// matches \a DPred predicate. 582 const DSAVarData 583 hasInnermostDSA(ValueDecl *D, 584 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 585 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 586 bool FromParent) const; 587 /// Checks if the specified variables has explicit data-sharing 588 /// attributes which match specified \a CPred predicate at the specified 589 /// OpenMP region. 590 bool 591 hasExplicitDSA(const ValueDecl *D, 592 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 593 unsigned Level, bool NotLastprivate = false) const; 594 595 /// Returns true if the directive at level \Level matches in the 596 /// specified \a DPred predicate. 597 bool hasExplicitDirective( 598 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 599 unsigned Level) const; 600 601 /// Finds a directive which matches specified \a DPred predicate. 602 bool hasDirective( 603 const llvm::function_ref<bool( 604 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 605 DPred, 606 bool FromParent) const; 607 608 /// Returns currently analyzed directive. 609 OpenMPDirectiveKind getCurrentDirective() const { 610 const SharingMapTy *Top = getTopOfStackOrNull(); 611 return Top ? Top->Directive : OMPD_unknown; 612 } 613 /// Returns directive kind at specified level. 614 OpenMPDirectiveKind getDirective(unsigned Level) const { 615 assert(!isStackEmpty() && "No directive at specified level."); 616 return getStackElemAtLevel(Level).Directive; 617 } 618 /// Returns the capture region at the specified level. 619 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 620 unsigned OpenMPCaptureLevel) const { 621 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 622 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 623 return CaptureRegions[OpenMPCaptureLevel]; 624 } 625 /// Returns parent directive. 626 OpenMPDirectiveKind getParentDirective() const { 627 const SharingMapTy *Parent = getSecondOnStackOrNull(); 628 return Parent ? Parent->Directive : OMPD_unknown; 629 } 630 631 /// Add requires decl to internal vector 632 void addRequiresDecl(OMPRequiresDecl *RD) { 633 RequiresDecls.push_back(RD); 634 } 635 636 /// Checks if the defined 'requires' directive has specified type of clause. 637 template <typename ClauseType> 638 bool hasRequiresDeclWithClause() const { 639 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 640 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 641 return isa<ClauseType>(C); 642 }); 643 }); 644 } 645 646 /// Checks for a duplicate clause amongst previously declared requires 647 /// directives 648 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 649 bool IsDuplicate = false; 650 for (OMPClause *CNew : ClauseList) { 651 for (const OMPRequiresDecl *D : RequiresDecls) { 652 for (const OMPClause *CPrev : D->clauselists()) { 653 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 654 SemaRef.Diag(CNew->getBeginLoc(), 655 diag::err_omp_requires_clause_redeclaration) 656 << getOpenMPClauseName(CNew->getClauseKind()); 657 SemaRef.Diag(CPrev->getBeginLoc(), 658 diag::note_omp_requires_previous_clause) 659 << getOpenMPClauseName(CPrev->getClauseKind()); 660 IsDuplicate = true; 661 } 662 } 663 } 664 } 665 return IsDuplicate; 666 } 667 668 /// Add location of previously encountered target to internal vector 669 void addTargetDirLocation(SourceLocation LocStart) { 670 TargetLocations.push_back(LocStart); 671 } 672 673 /// Add location for the first encountered atomicc directive. 674 void addAtomicDirectiveLoc(SourceLocation Loc) { 675 if (AtomicLocation.isInvalid()) 676 AtomicLocation = Loc; 677 } 678 679 /// Returns the location of the first encountered atomic directive in the 680 /// module. 681 SourceLocation getAtomicDirectiveLoc() const { 682 return AtomicLocation; 683 } 684 685 // Return previously encountered target region locations. 686 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 687 return TargetLocations; 688 } 689 690 /// Set default data sharing attribute to none. 691 void setDefaultDSANone(SourceLocation Loc) { 692 getTopOfStack().DefaultAttr = DSA_none; 693 getTopOfStack().DefaultAttrLoc = Loc; 694 } 695 /// Set default data sharing attribute to shared. 696 void setDefaultDSAShared(SourceLocation Loc) { 697 getTopOfStack().DefaultAttr = DSA_shared; 698 getTopOfStack().DefaultAttrLoc = Loc; 699 } 700 /// Set default data sharing attribute to firstprivate. 701 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 702 getTopOfStack().DefaultAttr = DSA_firstprivate; 703 getTopOfStack().DefaultAttrLoc = Loc; 704 } 705 /// Set default data mapping attribute to Modifier:Kind 706 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 707 OpenMPDefaultmapClauseKind Kind, 708 SourceLocation Loc) { 709 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 710 DMI.ImplicitBehavior = M; 711 DMI.SLoc = Loc; 712 } 713 /// Check whether the implicit-behavior has been set in defaultmap 714 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 715 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 716 return getTopOfStack() 717 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 718 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 719 getTopOfStack() 720 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 721 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 722 getTopOfStack() 723 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 724 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 725 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 726 OMPC_DEFAULTMAP_MODIFIER_unknown; 727 } 728 729 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 730 return getStackSize() <= Level ? DSA_unspecified 731 : getStackElemAtLevel(Level).DefaultAttr; 732 } 733 DefaultDataSharingAttributes getDefaultDSA() const { 734 return isStackEmpty() ? DSA_unspecified 735 : getTopOfStack().DefaultAttr; 736 } 737 SourceLocation getDefaultDSALocation() const { 738 return isStackEmpty() ? SourceLocation() 739 : getTopOfStack().DefaultAttrLoc; 740 } 741 OpenMPDefaultmapClauseModifier 742 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 743 return isStackEmpty() 744 ? OMPC_DEFAULTMAP_MODIFIER_unknown 745 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 746 } 747 OpenMPDefaultmapClauseModifier 748 getDefaultmapModifierAtLevel(unsigned Level, 749 OpenMPDefaultmapClauseKind Kind) const { 750 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 751 } 752 bool isDefaultmapCapturedByRef(unsigned Level, 753 OpenMPDefaultmapClauseKind Kind) const { 754 OpenMPDefaultmapClauseModifier M = 755 getDefaultmapModifierAtLevel(Level, Kind); 756 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 757 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 758 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 759 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 760 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 761 } 762 return true; 763 } 764 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 765 OpenMPDefaultmapClauseKind Kind) { 766 switch (Kind) { 767 case OMPC_DEFAULTMAP_scalar: 768 case OMPC_DEFAULTMAP_pointer: 769 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 770 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 771 (M == OMPC_DEFAULTMAP_MODIFIER_default); 772 case OMPC_DEFAULTMAP_aggregate: 773 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 774 default: 775 break; 776 } 777 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 778 } 779 bool mustBeFirstprivateAtLevel(unsigned Level, 780 OpenMPDefaultmapClauseKind Kind) const { 781 OpenMPDefaultmapClauseModifier M = 782 getDefaultmapModifierAtLevel(Level, Kind); 783 return mustBeFirstprivateBase(M, Kind); 784 } 785 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 786 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 787 return mustBeFirstprivateBase(M, Kind); 788 } 789 790 /// Checks if the specified variable is a threadprivate. 791 bool isThreadPrivate(VarDecl *D) { 792 const DSAVarData DVar = getTopDSA(D, false); 793 return isOpenMPThreadPrivate(DVar.CKind); 794 } 795 796 /// Marks current region as ordered (it has an 'ordered' clause). 797 void setOrderedRegion(bool IsOrdered, const Expr *Param, 798 OMPOrderedClause *Clause) { 799 if (IsOrdered) 800 getTopOfStack().OrderedRegion.emplace(Param, Clause); 801 else 802 getTopOfStack().OrderedRegion.reset(); 803 } 804 /// Returns true, if region is ordered (has associated 'ordered' clause), 805 /// false - otherwise. 806 bool isOrderedRegion() const { 807 if (const SharingMapTy *Top = getTopOfStackOrNull()) 808 return Top->OrderedRegion.hasValue(); 809 return false; 810 } 811 /// Returns optional parameter for the ordered region. 812 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 813 if (const SharingMapTy *Top = getTopOfStackOrNull()) 814 if (Top->OrderedRegion.hasValue()) 815 return Top->OrderedRegion.getValue(); 816 return std::make_pair(nullptr, nullptr); 817 } 818 /// Returns true, if parent region is ordered (has associated 819 /// 'ordered' clause), false - otherwise. 820 bool isParentOrderedRegion() const { 821 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 822 return Parent->OrderedRegion.hasValue(); 823 return false; 824 } 825 /// Returns optional parameter for the ordered region. 826 std::pair<const Expr *, OMPOrderedClause *> 827 getParentOrderedRegionParam() const { 828 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 829 if (Parent->OrderedRegion.hasValue()) 830 return Parent->OrderedRegion.getValue(); 831 return std::make_pair(nullptr, nullptr); 832 } 833 /// Marks current region as nowait (it has a 'nowait' clause). 834 void setNowaitRegion(bool IsNowait = true) { 835 getTopOfStack().NowaitRegion = IsNowait; 836 } 837 /// Returns true, if parent region is nowait (has associated 838 /// 'nowait' clause), false - otherwise. 839 bool isParentNowaitRegion() const { 840 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 841 return Parent->NowaitRegion; 842 return false; 843 } 844 /// Marks parent region as cancel region. 845 void setParentCancelRegion(bool Cancel = true) { 846 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 847 Parent->CancelRegion |= Cancel; 848 } 849 /// Return true if current region has inner cancel construct. 850 bool isCancelRegion() const { 851 const SharingMapTy *Top = getTopOfStackOrNull(); 852 return Top ? Top->CancelRegion : false; 853 } 854 855 /// Mark that parent region already has scan directive. 856 void setParentHasScanDirective(SourceLocation Loc) { 857 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 858 Parent->PrevScanLocation = Loc; 859 } 860 /// Return true if current region has inner cancel construct. 861 bool doesParentHasScanDirective() const { 862 const SharingMapTy *Top = getSecondOnStackOrNull(); 863 return Top ? Top->PrevScanLocation.isValid() : false; 864 } 865 /// Return true if current region has inner cancel construct. 866 SourceLocation getParentScanDirectiveLoc() const { 867 const SharingMapTy *Top = getSecondOnStackOrNull(); 868 return Top ? Top->PrevScanLocation : SourceLocation(); 869 } 870 /// Mark that parent region already has ordered directive. 871 void setParentHasOrderedDirective(SourceLocation Loc) { 872 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 873 Parent->PrevOrderedLocation = Loc; 874 } 875 /// Return true if current region has inner ordered construct. 876 bool doesParentHasOrderedDirective() const { 877 const SharingMapTy *Top = getSecondOnStackOrNull(); 878 return Top ? Top->PrevOrderedLocation.isValid() : false; 879 } 880 /// Returns the location of the previously specified ordered directive. 881 SourceLocation getParentOrderedDirectiveLoc() const { 882 const SharingMapTy *Top = getSecondOnStackOrNull(); 883 return Top ? Top->PrevOrderedLocation : SourceLocation(); 884 } 885 886 /// Set collapse value for the region. 887 void setAssociatedLoops(unsigned Val) { 888 getTopOfStack().AssociatedLoops = Val; 889 if (Val > 1) 890 getTopOfStack().HasMutipleLoops = true; 891 } 892 /// Return collapse value for region. 893 unsigned getAssociatedLoops() const { 894 const SharingMapTy *Top = getTopOfStackOrNull(); 895 return Top ? Top->AssociatedLoops : 0; 896 } 897 /// Returns true if the construct is associated with multiple loops. 898 bool hasMutipleLoops() const { 899 const SharingMapTy *Top = getTopOfStackOrNull(); 900 return Top ? Top->HasMutipleLoops : false; 901 } 902 903 /// Marks current target region as one with closely nested teams 904 /// region. 905 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 906 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 907 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 908 } 909 /// Returns true, if current region has closely nested teams region. 910 bool hasInnerTeamsRegion() const { 911 return getInnerTeamsRegionLoc().isValid(); 912 } 913 /// Returns location of the nested teams region (if any). 914 SourceLocation getInnerTeamsRegionLoc() const { 915 const SharingMapTy *Top = getTopOfStackOrNull(); 916 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 917 } 918 919 Scope *getCurScope() const { 920 const SharingMapTy *Top = getTopOfStackOrNull(); 921 return Top ? Top->CurScope : nullptr; 922 } 923 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; } 924 SourceLocation getConstructLoc() const { 925 const SharingMapTy *Top = getTopOfStackOrNull(); 926 return Top ? Top->ConstructLoc : SourceLocation(); 927 } 928 929 /// Do the check specified in \a Check to all component lists and return true 930 /// if any issue is found. 931 bool checkMappableExprComponentListsForDecl( 932 const ValueDecl *VD, bool CurrentRegionOnly, 933 const llvm::function_ref< 934 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 935 OpenMPClauseKind)> 936 Check) const { 937 if (isStackEmpty()) 938 return false; 939 auto SI = begin(); 940 auto SE = end(); 941 942 if (SI == SE) 943 return false; 944 945 if (CurrentRegionOnly) 946 SE = std::next(SI); 947 else 948 std::advance(SI, 1); 949 950 for (; SI != SE; ++SI) { 951 auto MI = SI->MappedExprComponents.find(VD); 952 if (MI != SI->MappedExprComponents.end()) 953 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 954 MI->second.Components) 955 if (Check(L, MI->second.Kind)) 956 return true; 957 } 958 return false; 959 } 960 961 /// Do the check specified in \a Check to all component lists at a given level 962 /// and return true if any issue is found. 963 bool checkMappableExprComponentListsForDeclAtLevel( 964 const ValueDecl *VD, unsigned Level, 965 const llvm::function_ref< 966 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 967 OpenMPClauseKind)> 968 Check) const { 969 if (getStackSize() <= Level) 970 return false; 971 972 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 973 auto MI = StackElem.MappedExprComponents.find(VD); 974 if (MI != StackElem.MappedExprComponents.end()) 975 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 976 MI->second.Components) 977 if (Check(L, MI->second.Kind)) 978 return true; 979 return false; 980 } 981 982 /// Create a new mappable expression component list associated with a given 983 /// declaration and initialize it with the provided list of components. 984 void addMappableExpressionComponents( 985 const ValueDecl *VD, 986 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 987 OpenMPClauseKind WhereFoundClauseKind) { 988 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 989 // Create new entry and append the new components there. 990 MEC.Components.resize(MEC.Components.size() + 1); 991 MEC.Components.back().append(Components.begin(), Components.end()); 992 MEC.Kind = WhereFoundClauseKind; 993 } 994 995 unsigned getNestingLevel() const { 996 assert(!isStackEmpty()); 997 return getStackSize() - 1; 998 } 999 void addDoacrossDependClause(OMPDependClause *C, 1000 const OperatorOffsetTy &OpsOffs) { 1001 SharingMapTy *Parent = getSecondOnStackOrNull(); 1002 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 1003 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 1004 } 1005 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 1006 getDoacrossDependClauses() const { 1007 const SharingMapTy &StackElem = getTopOfStack(); 1008 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 1009 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 1010 return llvm::make_range(Ref.begin(), Ref.end()); 1011 } 1012 return llvm::make_range(StackElem.DoacrossDepends.end(), 1013 StackElem.DoacrossDepends.end()); 1014 } 1015 1016 // Store types of classes which have been explicitly mapped 1017 void addMappedClassesQualTypes(QualType QT) { 1018 SharingMapTy &StackElem = getTopOfStack(); 1019 StackElem.MappedClassesQualTypes.insert(QT); 1020 } 1021 1022 // Return set of mapped classes types 1023 bool isClassPreviouslyMapped(QualType QT) const { 1024 const SharingMapTy &StackElem = getTopOfStack(); 1025 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1026 } 1027 1028 /// Adds global declare target to the parent target region. 1029 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1030 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1031 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1032 "Expected declare target link global."); 1033 for (auto &Elem : *this) { 1034 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1035 Elem.DeclareTargetLinkVarDecls.push_back(E); 1036 return; 1037 } 1038 } 1039 } 1040 1041 /// Returns the list of globals with declare target link if current directive 1042 /// is target. 1043 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1044 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1045 "Expected target executable directive."); 1046 return getTopOfStack().DeclareTargetLinkVarDecls; 1047 } 1048 1049 /// Adds list of allocators expressions. 1050 void addInnerAllocatorExpr(Expr *E) { 1051 getTopOfStack().InnerUsedAllocators.push_back(E); 1052 } 1053 /// Return list of used allocators. 1054 ArrayRef<Expr *> getInnerAllocators() const { 1055 return getTopOfStack().InnerUsedAllocators; 1056 } 1057 /// Marks the declaration as implicitly firstprivate nin the task-based 1058 /// regions. 1059 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1060 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1061 } 1062 /// Checks if the decl is implicitly firstprivate in the task-based region. 1063 bool isImplicitTaskFirstprivate(Decl *D) const { 1064 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1065 } 1066 1067 /// Marks decl as used in uses_allocators clause as the allocator. 1068 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1069 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1070 } 1071 /// Checks if specified decl is used in uses allocator clause as the 1072 /// allocator. 1073 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1074 const Decl *D) const { 1075 const SharingMapTy &StackElem = getTopOfStack(); 1076 auto I = StackElem.UsesAllocatorsDecls.find(D); 1077 if (I == StackElem.UsesAllocatorsDecls.end()) 1078 return None; 1079 return I->getSecond(); 1080 } 1081 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1082 const SharingMapTy &StackElem = getTopOfStack(); 1083 auto I = StackElem.UsesAllocatorsDecls.find(D); 1084 if (I == StackElem.UsesAllocatorsDecls.end()) 1085 return None; 1086 return I->getSecond(); 1087 } 1088 1089 void addDeclareMapperVarRef(Expr *Ref) { 1090 SharingMapTy &StackElem = getTopOfStack(); 1091 StackElem.DeclareMapperVar = Ref; 1092 } 1093 const Expr *getDeclareMapperVarRef() const { 1094 const SharingMapTy *Top = getTopOfStackOrNull(); 1095 return Top ? Top->DeclareMapperVar : nullptr; 1096 } 1097 }; 1098 1099 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1100 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1101 } 1102 1103 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1104 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1105 DKind == OMPD_unknown; 1106 } 1107 1108 } // namespace 1109 1110 static const Expr *getExprAsWritten(const Expr *E) { 1111 if (const auto *FE = dyn_cast<FullExpr>(E)) 1112 E = FE->getSubExpr(); 1113 1114 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1115 E = MTE->getSubExpr(); 1116 1117 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1118 E = Binder->getSubExpr(); 1119 1120 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1121 E = ICE->getSubExprAsWritten(); 1122 return E->IgnoreParens(); 1123 } 1124 1125 static Expr *getExprAsWritten(Expr *E) { 1126 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1127 } 1128 1129 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1130 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1131 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1132 D = ME->getMemberDecl(); 1133 const auto *VD = dyn_cast<VarDecl>(D); 1134 const auto *FD = dyn_cast<FieldDecl>(D); 1135 if (VD != nullptr) { 1136 VD = VD->getCanonicalDecl(); 1137 D = VD; 1138 } else { 1139 assert(FD); 1140 FD = FD->getCanonicalDecl(); 1141 D = FD; 1142 } 1143 return D; 1144 } 1145 1146 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1147 return const_cast<ValueDecl *>( 1148 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1149 } 1150 1151 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1152 ValueDecl *D) const { 1153 D = getCanonicalDecl(D); 1154 auto *VD = dyn_cast<VarDecl>(D); 1155 const auto *FD = dyn_cast<FieldDecl>(D); 1156 DSAVarData DVar; 1157 if (Iter == end()) { 1158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1159 // in a region but not in construct] 1160 // File-scope or namespace-scope variables referenced in called routines 1161 // in the region are shared unless they appear in a threadprivate 1162 // directive. 1163 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1164 DVar.CKind = OMPC_shared; 1165 1166 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1167 // in a region but not in construct] 1168 // Variables with static storage duration that are declared in called 1169 // routines in the region are shared. 1170 if (VD && VD->hasGlobalStorage()) 1171 DVar.CKind = OMPC_shared; 1172 1173 // Non-static data members are shared by default. 1174 if (FD) 1175 DVar.CKind = OMPC_shared; 1176 1177 return DVar; 1178 } 1179 1180 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1181 // in a Construct, C/C++, predetermined, p.1] 1182 // Variables with automatic storage duration that are declared in a scope 1183 // inside the construct are private. 1184 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1185 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1186 DVar.CKind = OMPC_private; 1187 return DVar; 1188 } 1189 1190 DVar.DKind = Iter->Directive; 1191 // Explicitly specified attributes and local variables with predetermined 1192 // attributes. 1193 if (Iter->SharingMap.count(D)) { 1194 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1195 DVar.RefExpr = Data.RefExpr.getPointer(); 1196 DVar.PrivateCopy = Data.PrivateCopy; 1197 DVar.CKind = Data.Attributes; 1198 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1199 DVar.Modifier = Data.Modifier; 1200 DVar.AppliedToPointee = Data.AppliedToPointee; 1201 return DVar; 1202 } 1203 1204 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1205 // in a Construct, C/C++, implicitly determined, p.1] 1206 // In a parallel or task construct, the data-sharing attributes of these 1207 // variables are determined by the default clause, if present. 1208 switch (Iter->DefaultAttr) { 1209 case DSA_shared: 1210 DVar.CKind = OMPC_shared; 1211 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1212 return DVar; 1213 case DSA_none: 1214 return DVar; 1215 case DSA_firstprivate: 1216 if (VD->getStorageDuration() == SD_Static && 1217 VD->getDeclContext()->isFileContext()) { 1218 DVar.CKind = OMPC_unknown; 1219 } else { 1220 DVar.CKind = OMPC_firstprivate; 1221 } 1222 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1223 return DVar; 1224 case DSA_unspecified: 1225 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1226 // in a Construct, implicitly determined, p.2] 1227 // In a parallel construct, if no default clause is present, these 1228 // variables are shared. 1229 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1230 if ((isOpenMPParallelDirective(DVar.DKind) && 1231 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1232 isOpenMPTeamsDirective(DVar.DKind)) { 1233 DVar.CKind = OMPC_shared; 1234 return DVar; 1235 } 1236 1237 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1238 // in a Construct, implicitly determined, p.4] 1239 // In a task construct, if no default clause is present, a variable that in 1240 // the enclosing context is determined to be shared by all implicit tasks 1241 // bound to the current team is shared. 1242 if (isOpenMPTaskingDirective(DVar.DKind)) { 1243 DSAVarData DVarTemp; 1244 const_iterator I = Iter, E = end(); 1245 do { 1246 ++I; 1247 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1248 // Referenced in a Construct, implicitly determined, p.6] 1249 // In a task construct, if no default clause is present, a variable 1250 // whose data-sharing attribute is not determined by the rules above is 1251 // firstprivate. 1252 DVarTemp = getDSA(I, D); 1253 if (DVarTemp.CKind != OMPC_shared) { 1254 DVar.RefExpr = nullptr; 1255 DVar.CKind = OMPC_firstprivate; 1256 return DVar; 1257 } 1258 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1259 DVar.CKind = 1260 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1261 return DVar; 1262 } 1263 } 1264 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1265 // in a Construct, implicitly determined, p.3] 1266 // For constructs other than task, if no default clause is present, these 1267 // variables inherit their data-sharing attributes from the enclosing 1268 // context. 1269 return getDSA(++Iter, D); 1270 } 1271 1272 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1273 const Expr *NewDE) { 1274 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1275 D = getCanonicalDecl(D); 1276 SharingMapTy &StackElem = getTopOfStack(); 1277 auto It = StackElem.AlignedMap.find(D); 1278 if (It == StackElem.AlignedMap.end()) { 1279 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1280 StackElem.AlignedMap[D] = NewDE; 1281 return nullptr; 1282 } 1283 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1284 return It->second; 1285 } 1286 1287 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1288 const Expr *NewDE) { 1289 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1290 D = getCanonicalDecl(D); 1291 SharingMapTy &StackElem = getTopOfStack(); 1292 auto It = StackElem.NontemporalMap.find(D); 1293 if (It == StackElem.NontemporalMap.end()) { 1294 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1295 StackElem.NontemporalMap[D] = NewDE; 1296 return nullptr; 1297 } 1298 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1299 return It->second; 1300 } 1301 1302 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1303 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1304 D = getCanonicalDecl(D); 1305 SharingMapTy &StackElem = getTopOfStack(); 1306 StackElem.LCVMap.try_emplace( 1307 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1308 } 1309 1310 const DSAStackTy::LCDeclInfo 1311 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1312 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1313 D = getCanonicalDecl(D); 1314 const SharingMapTy &StackElem = getTopOfStack(); 1315 auto It = StackElem.LCVMap.find(D); 1316 if (It != StackElem.LCVMap.end()) 1317 return It->second; 1318 return {0, nullptr}; 1319 } 1320 1321 const DSAStackTy::LCDeclInfo 1322 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1323 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1324 D = getCanonicalDecl(D); 1325 for (unsigned I = Level + 1; I > 0; --I) { 1326 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1327 auto It = StackElem.LCVMap.find(D); 1328 if (It != StackElem.LCVMap.end()) 1329 return It->second; 1330 } 1331 return {0, nullptr}; 1332 } 1333 1334 const DSAStackTy::LCDeclInfo 1335 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1336 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1337 assert(Parent && "Data-sharing attributes stack is empty"); 1338 D = getCanonicalDecl(D); 1339 auto It = Parent->LCVMap.find(D); 1340 if (It != Parent->LCVMap.end()) 1341 return It->second; 1342 return {0, nullptr}; 1343 } 1344 1345 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1346 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1347 assert(Parent && "Data-sharing attributes stack is empty"); 1348 if (Parent->LCVMap.size() < I) 1349 return nullptr; 1350 for (const auto &Pair : Parent->LCVMap) 1351 if (Pair.second.first == I) 1352 return Pair.first; 1353 return nullptr; 1354 } 1355 1356 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1357 DeclRefExpr *PrivateCopy, unsigned Modifier, 1358 bool AppliedToPointee) { 1359 D = getCanonicalDecl(D); 1360 if (A == OMPC_threadprivate) { 1361 DSAInfo &Data = Threadprivates[D]; 1362 Data.Attributes = A; 1363 Data.RefExpr.setPointer(E); 1364 Data.PrivateCopy = nullptr; 1365 Data.Modifier = Modifier; 1366 } else { 1367 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1368 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1369 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1370 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1371 (isLoopControlVariable(D).first && A == OMPC_private)); 1372 Data.Modifier = Modifier; 1373 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1374 Data.RefExpr.setInt(/*IntVal=*/true); 1375 return; 1376 } 1377 const bool IsLastprivate = 1378 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1379 Data.Attributes = A; 1380 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1381 Data.PrivateCopy = PrivateCopy; 1382 Data.AppliedToPointee = AppliedToPointee; 1383 if (PrivateCopy) { 1384 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1385 Data.Modifier = Modifier; 1386 Data.Attributes = A; 1387 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1388 Data.PrivateCopy = nullptr; 1389 Data.AppliedToPointee = AppliedToPointee; 1390 } 1391 } 1392 } 1393 1394 /// Build a variable declaration for OpenMP loop iteration variable. 1395 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1396 StringRef Name, const AttrVec *Attrs = nullptr, 1397 DeclRefExpr *OrigRef = nullptr) { 1398 DeclContext *DC = SemaRef.CurContext; 1399 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1400 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1401 auto *Decl = 1402 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1403 if (Attrs) { 1404 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1405 I != E; ++I) 1406 Decl->addAttr(*I); 1407 } 1408 Decl->setImplicit(); 1409 if (OrigRef) { 1410 Decl->addAttr( 1411 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1412 } 1413 return Decl; 1414 } 1415 1416 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1417 SourceLocation Loc, 1418 bool RefersToCapture = false) { 1419 D->setReferenced(); 1420 D->markUsed(S.Context); 1421 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1422 SourceLocation(), D, RefersToCapture, Loc, Ty, 1423 VK_LValue); 1424 } 1425 1426 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1427 BinaryOperatorKind BOK) { 1428 D = getCanonicalDecl(D); 1429 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1430 assert( 1431 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1432 "Additional reduction info may be specified only for reduction items."); 1433 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1434 assert(ReductionData.ReductionRange.isInvalid() && 1435 (getTopOfStack().Directive == OMPD_taskgroup || 1436 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1437 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1438 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1439 "Additional reduction info may be specified only once for reduction " 1440 "items."); 1441 ReductionData.set(BOK, SR); 1442 Expr *&TaskgroupReductionRef = 1443 getTopOfStack().TaskgroupReductionRef; 1444 if (!TaskgroupReductionRef) { 1445 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1446 SemaRef.Context.VoidPtrTy, ".task_red."); 1447 TaskgroupReductionRef = 1448 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1449 } 1450 } 1451 1452 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1453 const Expr *ReductionRef) { 1454 D = getCanonicalDecl(D); 1455 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1456 assert( 1457 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1458 "Additional reduction info may be specified only for reduction items."); 1459 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1460 assert(ReductionData.ReductionRange.isInvalid() && 1461 (getTopOfStack().Directive == OMPD_taskgroup || 1462 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1463 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1464 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1465 "Additional reduction info may be specified only once for reduction " 1466 "items."); 1467 ReductionData.set(ReductionRef, SR); 1468 Expr *&TaskgroupReductionRef = 1469 getTopOfStack().TaskgroupReductionRef; 1470 if (!TaskgroupReductionRef) { 1471 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1472 SemaRef.Context.VoidPtrTy, ".task_red."); 1473 TaskgroupReductionRef = 1474 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1475 } 1476 } 1477 1478 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1479 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1480 Expr *&TaskgroupDescriptor) const { 1481 D = getCanonicalDecl(D); 1482 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1483 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1484 const DSAInfo &Data = I->SharingMap.lookup(D); 1485 if (Data.Attributes != OMPC_reduction || 1486 Data.Modifier != OMPC_REDUCTION_task) 1487 continue; 1488 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1489 if (!ReductionData.ReductionOp || 1490 ReductionData.ReductionOp.is<const Expr *>()) 1491 return DSAVarData(); 1492 SR = ReductionData.ReductionRange; 1493 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1494 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1495 "expression for the descriptor is not " 1496 "set."); 1497 TaskgroupDescriptor = I->TaskgroupReductionRef; 1498 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1499 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1500 /*AppliedToPointee=*/false); 1501 } 1502 return DSAVarData(); 1503 } 1504 1505 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1506 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1507 Expr *&TaskgroupDescriptor) const { 1508 D = getCanonicalDecl(D); 1509 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1510 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1511 const DSAInfo &Data = I->SharingMap.lookup(D); 1512 if (Data.Attributes != OMPC_reduction || 1513 Data.Modifier != OMPC_REDUCTION_task) 1514 continue; 1515 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1516 if (!ReductionData.ReductionOp || 1517 !ReductionData.ReductionOp.is<const Expr *>()) 1518 return DSAVarData(); 1519 SR = ReductionData.ReductionRange; 1520 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1521 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1522 "expression for the descriptor is not " 1523 "set."); 1524 TaskgroupDescriptor = I->TaskgroupReductionRef; 1525 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1526 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1527 /*AppliedToPointee=*/false); 1528 } 1529 return DSAVarData(); 1530 } 1531 1532 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1533 D = D->getCanonicalDecl(); 1534 for (const_iterator E = end(); I != E; ++I) { 1535 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1536 isOpenMPTargetExecutionDirective(I->Directive)) { 1537 if (I->CurScope) { 1538 Scope *TopScope = I->CurScope->getParent(); 1539 Scope *CurScope = getCurScope(); 1540 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1541 CurScope = CurScope->getParent(); 1542 return CurScope != TopScope; 1543 } 1544 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent()) 1545 if (I->Context == DC) 1546 return true; 1547 return false; 1548 } 1549 } 1550 return false; 1551 } 1552 1553 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1554 bool AcceptIfMutable = true, 1555 bool *IsClassType = nullptr) { 1556 ASTContext &Context = SemaRef.getASTContext(); 1557 Type = Type.getNonReferenceType().getCanonicalType(); 1558 bool IsConstant = Type.isConstant(Context); 1559 Type = Context.getBaseElementType(Type); 1560 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1561 ? Type->getAsCXXRecordDecl() 1562 : nullptr; 1563 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1564 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1565 RD = CTD->getTemplatedDecl(); 1566 if (IsClassType) 1567 *IsClassType = RD; 1568 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1569 RD->hasDefinition() && RD->hasMutableFields()); 1570 } 1571 1572 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1573 QualType Type, OpenMPClauseKind CKind, 1574 SourceLocation ELoc, 1575 bool AcceptIfMutable = true, 1576 bool ListItemNotVar = false) { 1577 ASTContext &Context = SemaRef.getASTContext(); 1578 bool IsClassType; 1579 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1580 unsigned Diag = ListItemNotVar 1581 ? diag::err_omp_const_list_item 1582 : IsClassType ? diag::err_omp_const_not_mutable_variable 1583 : diag::err_omp_const_variable; 1584 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1585 if (!ListItemNotVar && D) { 1586 const VarDecl *VD = dyn_cast<VarDecl>(D); 1587 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1588 VarDecl::DeclarationOnly; 1589 SemaRef.Diag(D->getLocation(), 1590 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1591 << D; 1592 } 1593 return true; 1594 } 1595 return false; 1596 } 1597 1598 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1599 bool FromParent) { 1600 D = getCanonicalDecl(D); 1601 DSAVarData DVar; 1602 1603 auto *VD = dyn_cast<VarDecl>(D); 1604 auto TI = Threadprivates.find(D); 1605 if (TI != Threadprivates.end()) { 1606 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1607 DVar.CKind = OMPC_threadprivate; 1608 DVar.Modifier = TI->getSecond().Modifier; 1609 return DVar; 1610 } 1611 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1612 DVar.RefExpr = buildDeclRefExpr( 1613 SemaRef, VD, D->getType().getNonReferenceType(), 1614 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1615 DVar.CKind = OMPC_threadprivate; 1616 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1617 return DVar; 1618 } 1619 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1620 // in a Construct, C/C++, predetermined, p.1] 1621 // Variables appearing in threadprivate directives are threadprivate. 1622 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1623 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1624 SemaRef.getLangOpts().OpenMPUseTLS && 1625 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1626 (VD && VD->getStorageClass() == SC_Register && 1627 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1628 DVar.RefExpr = buildDeclRefExpr( 1629 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1630 DVar.CKind = OMPC_threadprivate; 1631 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1632 return DVar; 1633 } 1634 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1635 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1636 !isLoopControlVariable(D).first) { 1637 const_iterator IterTarget = 1638 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1639 return isOpenMPTargetExecutionDirective(Data.Directive); 1640 }); 1641 if (IterTarget != end()) { 1642 const_iterator ParentIterTarget = IterTarget + 1; 1643 for (const_iterator Iter = begin(); 1644 Iter != ParentIterTarget; ++Iter) { 1645 if (isOpenMPLocal(VD, Iter)) { 1646 DVar.RefExpr = 1647 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1648 D->getLocation()); 1649 DVar.CKind = OMPC_threadprivate; 1650 return DVar; 1651 } 1652 } 1653 if (!isClauseParsingMode() || IterTarget != begin()) { 1654 auto DSAIter = IterTarget->SharingMap.find(D); 1655 if (DSAIter != IterTarget->SharingMap.end() && 1656 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1657 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1658 DVar.CKind = OMPC_threadprivate; 1659 return DVar; 1660 } 1661 const_iterator End = end(); 1662 if (!SemaRef.isOpenMPCapturedByRef( 1663 D, std::distance(ParentIterTarget, End), 1664 /*OpenMPCaptureLevel=*/0)) { 1665 DVar.RefExpr = 1666 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1667 IterTarget->ConstructLoc); 1668 DVar.CKind = OMPC_threadprivate; 1669 return DVar; 1670 } 1671 } 1672 } 1673 } 1674 1675 if (isStackEmpty()) 1676 // Not in OpenMP execution region and top scope was already checked. 1677 return DVar; 1678 1679 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1680 // in a Construct, C/C++, predetermined, p.4] 1681 // Static data members are shared. 1682 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1683 // in a Construct, C/C++, predetermined, p.7] 1684 // Variables with static storage duration that are declared in a scope 1685 // inside the construct are shared. 1686 if (VD && VD->isStaticDataMember()) { 1687 // Check for explicitly specified attributes. 1688 const_iterator I = begin(); 1689 const_iterator EndI = end(); 1690 if (FromParent && I != EndI) 1691 ++I; 1692 if (I != EndI) { 1693 auto It = I->SharingMap.find(D); 1694 if (It != I->SharingMap.end()) { 1695 const DSAInfo &Data = It->getSecond(); 1696 DVar.RefExpr = Data.RefExpr.getPointer(); 1697 DVar.PrivateCopy = Data.PrivateCopy; 1698 DVar.CKind = Data.Attributes; 1699 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1700 DVar.DKind = I->Directive; 1701 DVar.Modifier = Data.Modifier; 1702 DVar.AppliedToPointee = Data.AppliedToPointee; 1703 return DVar; 1704 } 1705 } 1706 1707 DVar.CKind = OMPC_shared; 1708 return DVar; 1709 } 1710 1711 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1712 // The predetermined shared attribute for const-qualified types having no 1713 // mutable members was removed after OpenMP 3.1. 1714 if (SemaRef.LangOpts.OpenMP <= 31) { 1715 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1716 // in a Construct, C/C++, predetermined, p.6] 1717 // Variables with const qualified type having no mutable member are 1718 // shared. 1719 if (isConstNotMutableType(SemaRef, D->getType())) { 1720 // Variables with const-qualified type having no mutable member may be 1721 // listed in a firstprivate clause, even if they are static data members. 1722 DSAVarData DVarTemp = hasInnermostDSA( 1723 D, 1724 [](OpenMPClauseKind C, bool) { 1725 return C == OMPC_firstprivate || C == OMPC_shared; 1726 }, 1727 MatchesAlways, FromParent); 1728 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1729 return DVarTemp; 1730 1731 DVar.CKind = OMPC_shared; 1732 return DVar; 1733 } 1734 } 1735 1736 // Explicitly specified attributes and local variables with predetermined 1737 // attributes. 1738 const_iterator I = begin(); 1739 const_iterator EndI = end(); 1740 if (FromParent && I != EndI) 1741 ++I; 1742 if (I == EndI) 1743 return DVar; 1744 auto It = I->SharingMap.find(D); 1745 if (It != I->SharingMap.end()) { 1746 const DSAInfo &Data = It->getSecond(); 1747 DVar.RefExpr = Data.RefExpr.getPointer(); 1748 DVar.PrivateCopy = Data.PrivateCopy; 1749 DVar.CKind = Data.Attributes; 1750 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1751 DVar.DKind = I->Directive; 1752 DVar.Modifier = Data.Modifier; 1753 DVar.AppliedToPointee = Data.AppliedToPointee; 1754 } 1755 1756 return DVar; 1757 } 1758 1759 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1760 bool FromParent) const { 1761 if (isStackEmpty()) { 1762 const_iterator I; 1763 return getDSA(I, D); 1764 } 1765 D = getCanonicalDecl(D); 1766 const_iterator StartI = begin(); 1767 const_iterator EndI = end(); 1768 if (FromParent && StartI != EndI) 1769 ++StartI; 1770 return getDSA(StartI, D); 1771 } 1772 1773 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1774 unsigned Level) const { 1775 if (getStackSize() <= Level) 1776 return DSAVarData(); 1777 D = getCanonicalDecl(D); 1778 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1779 return getDSA(StartI, D); 1780 } 1781 1782 const DSAStackTy::DSAVarData 1783 DSAStackTy::hasDSA(ValueDecl *D, 1784 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1785 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1786 bool FromParent) const { 1787 if (isStackEmpty()) 1788 return {}; 1789 D = getCanonicalDecl(D); 1790 const_iterator I = begin(); 1791 const_iterator EndI = end(); 1792 if (FromParent && I != EndI) 1793 ++I; 1794 for (; I != EndI; ++I) { 1795 if (!DPred(I->Directive) && 1796 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1797 continue; 1798 const_iterator NewI = I; 1799 DSAVarData DVar = getDSA(NewI, D); 1800 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1801 return DVar; 1802 } 1803 return {}; 1804 } 1805 1806 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1807 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1808 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1809 bool FromParent) const { 1810 if (isStackEmpty()) 1811 return {}; 1812 D = getCanonicalDecl(D); 1813 const_iterator StartI = begin(); 1814 const_iterator EndI = end(); 1815 if (FromParent && StartI != EndI) 1816 ++StartI; 1817 if (StartI == EndI || !DPred(StartI->Directive)) 1818 return {}; 1819 const_iterator NewI = StartI; 1820 DSAVarData DVar = getDSA(NewI, D); 1821 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1822 ? DVar 1823 : DSAVarData(); 1824 } 1825 1826 bool DSAStackTy::hasExplicitDSA( 1827 const ValueDecl *D, 1828 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1829 unsigned Level, bool NotLastprivate) const { 1830 if (getStackSize() <= Level) 1831 return false; 1832 D = getCanonicalDecl(D); 1833 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1834 auto I = StackElem.SharingMap.find(D); 1835 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() && 1836 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) && 1837 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1838 return true; 1839 // Check predetermined rules for the loop control variables. 1840 auto LI = StackElem.LCVMap.find(D); 1841 if (LI != StackElem.LCVMap.end()) 1842 return CPred(OMPC_private, /*AppliedToPointee=*/false); 1843 return false; 1844 } 1845 1846 bool DSAStackTy::hasExplicitDirective( 1847 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1848 unsigned Level) const { 1849 if (getStackSize() <= Level) 1850 return false; 1851 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1852 return DPred(StackElem.Directive); 1853 } 1854 1855 bool DSAStackTy::hasDirective( 1856 const llvm::function_ref<bool(OpenMPDirectiveKind, 1857 const DeclarationNameInfo &, SourceLocation)> 1858 DPred, 1859 bool FromParent) const { 1860 // We look only in the enclosing region. 1861 size_t Skip = FromParent ? 2 : 1; 1862 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1863 I != E; ++I) { 1864 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1865 return true; 1866 } 1867 return false; 1868 } 1869 1870 void Sema::InitDataSharingAttributesStack() { 1871 VarDataSharingAttributesStack = new DSAStackTy(*this); 1872 } 1873 1874 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1875 1876 void Sema::pushOpenMPFunctionRegion() { 1877 DSAStack->pushFunction(); 1878 } 1879 1880 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1881 DSAStack->popFunction(OldFSI); 1882 } 1883 1884 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1885 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1886 "Expected OpenMP device compilation."); 1887 return !S.isInOpenMPTargetExecutionDirective(); 1888 } 1889 1890 namespace { 1891 /// Status of the function emission on the host/device. 1892 enum class FunctionEmissionStatus { 1893 Emitted, 1894 Discarded, 1895 Unknown, 1896 }; 1897 } // anonymous namespace 1898 1899 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1900 unsigned DiagID, 1901 FunctionDecl *FD) { 1902 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1903 "Expected OpenMP device compilation."); 1904 1905 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1906 if (FD) { 1907 FunctionEmissionStatus FES = getEmissionStatus(FD); 1908 switch (FES) { 1909 case FunctionEmissionStatus::Emitted: 1910 Kind = SemaDiagnosticBuilder::K_Immediate; 1911 break; 1912 case FunctionEmissionStatus::Unknown: 1913 // TODO: We should always delay diagnostics here in case a target 1914 // region is in a function we do not emit. However, as the 1915 // current diagnostics are associated with the function containing 1916 // the target region and we do not emit that one, we would miss out 1917 // on diagnostics for the target region itself. We need to anchor 1918 // the diagnostics with the new generated function *or* ensure we 1919 // emit diagnostics associated with the surrounding function. 1920 Kind = isOpenMPDeviceDelayedContext(*this) 1921 ? SemaDiagnosticBuilder::K_Deferred 1922 : SemaDiagnosticBuilder::K_Immediate; 1923 break; 1924 case FunctionEmissionStatus::TemplateDiscarded: 1925 case FunctionEmissionStatus::OMPDiscarded: 1926 Kind = SemaDiagnosticBuilder::K_Nop; 1927 break; 1928 case FunctionEmissionStatus::CUDADiscarded: 1929 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1930 break; 1931 } 1932 } 1933 1934 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1935 } 1936 1937 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1938 unsigned DiagID, 1939 FunctionDecl *FD) { 1940 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1941 "Expected OpenMP host compilation."); 1942 1943 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1944 if (FD) { 1945 FunctionEmissionStatus FES = getEmissionStatus(FD); 1946 switch (FES) { 1947 case FunctionEmissionStatus::Emitted: 1948 Kind = SemaDiagnosticBuilder::K_Immediate; 1949 break; 1950 case FunctionEmissionStatus::Unknown: 1951 Kind = SemaDiagnosticBuilder::K_Deferred; 1952 break; 1953 case FunctionEmissionStatus::TemplateDiscarded: 1954 case FunctionEmissionStatus::OMPDiscarded: 1955 case FunctionEmissionStatus::CUDADiscarded: 1956 Kind = SemaDiagnosticBuilder::K_Nop; 1957 break; 1958 } 1959 } 1960 1961 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1962 } 1963 1964 static OpenMPDefaultmapClauseKind 1965 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1966 if (LO.OpenMP <= 45) { 1967 if (VD->getType().getNonReferenceType()->isScalarType()) 1968 return OMPC_DEFAULTMAP_scalar; 1969 return OMPC_DEFAULTMAP_aggregate; 1970 } 1971 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1972 return OMPC_DEFAULTMAP_pointer; 1973 if (VD->getType().getNonReferenceType()->isScalarType()) 1974 return OMPC_DEFAULTMAP_scalar; 1975 return OMPC_DEFAULTMAP_aggregate; 1976 } 1977 1978 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1979 unsigned OpenMPCaptureLevel) const { 1980 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1981 1982 ASTContext &Ctx = getASTContext(); 1983 bool IsByRef = true; 1984 1985 // Find the directive that is associated with the provided scope. 1986 D = cast<ValueDecl>(D->getCanonicalDecl()); 1987 QualType Ty = D->getType(); 1988 1989 bool IsVariableUsedInMapClause = false; 1990 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1991 // This table summarizes how a given variable should be passed to the device 1992 // given its type and the clauses where it appears. This table is based on 1993 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1994 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1995 // 1996 // ========================================================================= 1997 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1998 // | |(tofrom:scalar)| | pvt | | | | 1999 // ========================================================================= 2000 // | scl | | | | - | | bycopy| 2001 // | scl | | - | x | - | - | bycopy| 2002 // | scl | | x | - | - | - | null | 2003 // | scl | x | | | - | | byref | 2004 // | scl | x | - | x | - | - | bycopy| 2005 // | scl | x | x | - | - | - | null | 2006 // | scl | | - | - | - | x | byref | 2007 // | scl | x | - | - | - | x | byref | 2008 // 2009 // | agg | n.a. | | | - | | byref | 2010 // | agg | n.a. | - | x | - | - | byref | 2011 // | agg | n.a. | x | - | - | - | null | 2012 // | agg | n.a. | - | - | - | x | byref | 2013 // | agg | n.a. | - | - | - | x[] | byref | 2014 // 2015 // | ptr | n.a. | | | - | | bycopy| 2016 // | ptr | n.a. | - | x | - | - | bycopy| 2017 // | ptr | n.a. | x | - | - | - | null | 2018 // | ptr | n.a. | - | - | - | x | byref | 2019 // | ptr | n.a. | - | - | - | x[] | bycopy| 2020 // | ptr | n.a. | - | - | x | | bycopy| 2021 // | ptr | n.a. | - | - | x | x | bycopy| 2022 // | ptr | n.a. | - | - | x | x[] | bycopy| 2023 // ========================================================================= 2024 // Legend: 2025 // scl - scalar 2026 // ptr - pointer 2027 // agg - aggregate 2028 // x - applies 2029 // - - invalid in this combination 2030 // [] - mapped with an array section 2031 // byref - should be mapped by reference 2032 // byval - should be mapped by value 2033 // null - initialize a local variable to null on the device 2034 // 2035 // Observations: 2036 // - All scalar declarations that show up in a map clause have to be passed 2037 // by reference, because they may have been mapped in the enclosing data 2038 // environment. 2039 // - If the scalar value does not fit the size of uintptr, it has to be 2040 // passed by reference, regardless the result in the table above. 2041 // - For pointers mapped by value that have either an implicit map or an 2042 // array section, the runtime library may pass the NULL value to the 2043 // device instead of the value passed to it by the compiler. 2044 2045 if (Ty->isReferenceType()) 2046 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2047 2048 // Locate map clauses and see if the variable being captured is referred to 2049 // in any of those clauses. Here we only care about variables, not fields, 2050 // because fields are part of aggregates. 2051 bool IsVariableAssociatedWithSection = false; 2052 2053 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2054 D, Level, 2055 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2056 OMPClauseMappableExprCommon::MappableExprComponentListRef 2057 MapExprComponents, 2058 OpenMPClauseKind WhereFoundClauseKind) { 2059 // Only the map clause information influences how a variable is 2060 // captured. E.g. is_device_ptr does not require changing the default 2061 // behavior. 2062 if (WhereFoundClauseKind != OMPC_map) 2063 return false; 2064 2065 auto EI = MapExprComponents.rbegin(); 2066 auto EE = MapExprComponents.rend(); 2067 2068 assert(EI != EE && "Invalid map expression!"); 2069 2070 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2071 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2072 2073 ++EI; 2074 if (EI == EE) 2075 return false; 2076 2077 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2078 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2079 isa<MemberExpr>(EI->getAssociatedExpression()) || 2080 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2081 IsVariableAssociatedWithSection = true; 2082 // There is nothing more we need to know about this variable. 2083 return true; 2084 } 2085 2086 // Keep looking for more map info. 2087 return false; 2088 }); 2089 2090 if (IsVariableUsedInMapClause) { 2091 // If variable is identified in a map clause it is always captured by 2092 // reference except if it is a pointer that is dereferenced somehow. 2093 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2094 } else { 2095 // By default, all the data that has a scalar type is mapped by copy 2096 // (except for reduction variables). 2097 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2098 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2099 !Ty->isAnyPointerType()) || 2100 !Ty->isScalarType() || 2101 DSAStack->isDefaultmapCapturedByRef( 2102 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2103 DSAStack->hasExplicitDSA( 2104 D, 2105 [](OpenMPClauseKind K, bool AppliedToPointee) { 2106 return K == OMPC_reduction && !AppliedToPointee; 2107 }, 2108 Level); 2109 } 2110 } 2111 2112 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2113 IsByRef = 2114 ((IsVariableUsedInMapClause && 2115 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2116 OMPD_target) || 2117 !(DSAStack->hasExplicitDSA( 2118 D, 2119 [](OpenMPClauseKind K, bool AppliedToPointee) -> bool { 2120 return K == OMPC_firstprivate || 2121 (K == OMPC_reduction && AppliedToPointee); 2122 }, 2123 Level, /*NotLastprivate=*/true) || 2124 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2125 // If the variable is artificial and must be captured by value - try to 2126 // capture by value. 2127 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2128 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2129 // If the variable is implicitly firstprivate and scalar - capture by 2130 // copy 2131 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2132 !DSAStack->hasExplicitDSA( 2133 D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; }, 2134 Level) && 2135 !DSAStack->isLoopControlVariable(D, Level).first); 2136 } 2137 2138 // When passing data by copy, we need to make sure it fits the uintptr size 2139 // and alignment, because the runtime library only deals with uintptr types. 2140 // If it does not fit the uintptr size, we need to pass the data by reference 2141 // instead. 2142 if (!IsByRef && 2143 (Ctx.getTypeSizeInChars(Ty) > 2144 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2145 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2146 IsByRef = true; 2147 } 2148 2149 return IsByRef; 2150 } 2151 2152 unsigned Sema::getOpenMPNestingLevel() const { 2153 assert(getLangOpts().OpenMP); 2154 return DSAStack->getNestingLevel(); 2155 } 2156 2157 bool Sema::isInOpenMPTargetExecutionDirective() const { 2158 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2159 !DSAStack->isClauseParsingMode()) || 2160 DSAStack->hasDirective( 2161 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2162 SourceLocation) -> bool { 2163 return isOpenMPTargetExecutionDirective(K); 2164 }, 2165 false); 2166 } 2167 2168 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2169 unsigned StopAt) { 2170 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2171 D = getCanonicalDecl(D); 2172 2173 auto *VD = dyn_cast<VarDecl>(D); 2174 // Do not capture constexpr variables. 2175 if (VD && VD->isConstexpr()) 2176 return nullptr; 2177 2178 // If we want to determine whether the variable should be captured from the 2179 // perspective of the current capturing scope, and we've already left all the 2180 // capturing scopes of the top directive on the stack, check from the 2181 // perspective of its parent directive (if any) instead. 2182 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2183 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2184 2185 // If we are attempting to capture a global variable in a directive with 2186 // 'target' we return true so that this global is also mapped to the device. 2187 // 2188 if (VD && !VD->hasLocalStorage() && 2189 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2190 if (isInOpenMPDeclareTargetContext()) { 2191 // Try to mark variable as declare target if it is used in capturing 2192 // regions. 2193 if (LangOpts.OpenMP <= 45 && 2194 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2195 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2196 return nullptr; 2197 } 2198 if (isInOpenMPTargetExecutionDirective()) { 2199 // If the declaration is enclosed in a 'declare target' directive, 2200 // then it should not be captured. 2201 // 2202 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2203 return nullptr; 2204 CapturedRegionScopeInfo *CSI = nullptr; 2205 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2206 llvm::reverse(FunctionScopes), 2207 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2208 if (!isa<CapturingScopeInfo>(FSI)) 2209 return nullptr; 2210 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2211 if (RSI->CapRegionKind == CR_OpenMP) { 2212 CSI = RSI; 2213 break; 2214 } 2215 } 2216 assert(CSI && "Failed to find CapturedRegionScopeInfo"); 2217 SmallVector<OpenMPDirectiveKind, 4> Regions; 2218 getOpenMPCaptureRegions(Regions, 2219 DSAStack->getDirective(CSI->OpenMPLevel)); 2220 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2221 return VD; 2222 } 2223 } 2224 2225 if (CheckScopeInfo) { 2226 bool OpenMPFound = false; 2227 for (unsigned I = StopAt + 1; I > 0; --I) { 2228 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2229 if(!isa<CapturingScopeInfo>(FSI)) 2230 return nullptr; 2231 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2232 if (RSI->CapRegionKind == CR_OpenMP) { 2233 OpenMPFound = true; 2234 break; 2235 } 2236 } 2237 if (!OpenMPFound) 2238 return nullptr; 2239 } 2240 2241 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2242 (!DSAStack->isClauseParsingMode() || 2243 DSAStack->getParentDirective() != OMPD_unknown)) { 2244 auto &&Info = DSAStack->isLoopControlVariable(D); 2245 if (Info.first || 2246 (VD && VD->hasLocalStorage() && 2247 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2248 (VD && DSAStack->isForceVarCapturing())) 2249 return VD ? VD : Info.second; 2250 DSAStackTy::DSAVarData DVarTop = 2251 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2252 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) && 2253 (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee)) 2254 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2255 // Threadprivate variables must not be captured. 2256 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2257 return nullptr; 2258 // The variable is not private or it is the variable in the directive with 2259 // default(none) clause and not used in any clause. 2260 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2261 D, 2262 [](OpenMPClauseKind C, bool AppliedToPointee) { 2263 return isOpenMPPrivate(C) && !AppliedToPointee; 2264 }, 2265 [](OpenMPDirectiveKind) { return true; }, 2266 DSAStack->isClauseParsingMode()); 2267 // Global shared must not be captured. 2268 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2269 ((DSAStack->getDefaultDSA() != DSA_none && 2270 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2271 DVarTop.CKind == OMPC_shared)) 2272 return nullptr; 2273 if (DVarPrivate.CKind != OMPC_unknown || 2274 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2275 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2276 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2277 } 2278 return nullptr; 2279 } 2280 2281 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2282 unsigned Level) const { 2283 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2284 } 2285 2286 void Sema::startOpenMPLoop() { 2287 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2288 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2289 DSAStack->loopInit(); 2290 } 2291 2292 void Sema::startOpenMPCXXRangeFor() { 2293 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2294 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2295 DSAStack->resetPossibleLoopCounter(); 2296 DSAStack->loopStart(); 2297 } 2298 } 2299 2300 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2301 unsigned CapLevel) const { 2302 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2303 if (DSAStack->hasExplicitDirective( 2304 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2305 Level)) { 2306 bool IsTriviallyCopyable = 2307 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2308 !D->getType() 2309 .getNonReferenceType() 2310 .getCanonicalType() 2311 ->getAsCXXRecordDecl(); 2312 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2313 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2314 getOpenMPCaptureRegions(CaptureRegions, DKind); 2315 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2316 (IsTriviallyCopyable || 2317 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2318 if (DSAStack->hasExplicitDSA( 2319 D, 2320 [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; }, 2321 Level, /*NotLastprivate=*/true)) 2322 return OMPC_firstprivate; 2323 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2324 if (DVar.CKind != OMPC_shared && 2325 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2326 DSAStack->addImplicitTaskFirstprivate(Level, D); 2327 return OMPC_firstprivate; 2328 } 2329 } 2330 } 2331 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2332 if (DSAStack->getAssociatedLoops() > 0 && 2333 !DSAStack->isLoopStarted()) { 2334 DSAStack->resetPossibleLoopCounter(D); 2335 DSAStack->loopStart(); 2336 return OMPC_private; 2337 } 2338 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2339 DSAStack->isLoopControlVariable(D).first) && 2340 !DSAStack->hasExplicitDSA( 2341 D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; }, 2342 Level) && 2343 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2344 return OMPC_private; 2345 } 2346 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2347 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2348 DSAStack->isForceVarCapturing() && 2349 !DSAStack->hasExplicitDSA( 2350 D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; }, 2351 Level)) 2352 return OMPC_private; 2353 } 2354 // User-defined allocators are private since they must be defined in the 2355 // context of target region. 2356 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2357 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2358 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2359 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2360 return OMPC_private; 2361 return (DSAStack->hasExplicitDSA( 2362 D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; }, 2363 Level) || 2364 (DSAStack->isClauseParsingMode() && 2365 DSAStack->getClauseParsingMode() == OMPC_private) || 2366 // Consider taskgroup reduction descriptor variable a private 2367 // to avoid possible capture in the region. 2368 (DSAStack->hasExplicitDirective( 2369 [](OpenMPDirectiveKind K) { 2370 return K == OMPD_taskgroup || 2371 ((isOpenMPParallelDirective(K) || 2372 isOpenMPWorksharingDirective(K)) && 2373 !isOpenMPSimdDirective(K)); 2374 }, 2375 Level) && 2376 DSAStack->isTaskgroupReductionRef(D, Level))) 2377 ? OMPC_private 2378 : OMPC_unknown; 2379 } 2380 2381 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2382 unsigned Level) { 2383 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2384 D = getCanonicalDecl(D); 2385 OpenMPClauseKind OMPC = OMPC_unknown; 2386 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2387 const unsigned NewLevel = I - 1; 2388 if (DSAStack->hasExplicitDSA( 2389 D, 2390 [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) { 2391 if (isOpenMPPrivate(K) && !AppliedToPointee) { 2392 OMPC = K; 2393 return true; 2394 } 2395 return false; 2396 }, 2397 NewLevel)) 2398 break; 2399 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2400 D, NewLevel, 2401 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2402 OpenMPClauseKind) { return true; })) { 2403 OMPC = OMPC_map; 2404 break; 2405 } 2406 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2407 NewLevel)) { 2408 OMPC = OMPC_map; 2409 if (DSAStack->mustBeFirstprivateAtLevel( 2410 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2411 OMPC = OMPC_firstprivate; 2412 break; 2413 } 2414 } 2415 if (OMPC != OMPC_unknown) 2416 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2417 } 2418 2419 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2420 unsigned CaptureLevel) const { 2421 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2422 // Return true if the current level is no longer enclosed in a target region. 2423 2424 SmallVector<OpenMPDirectiveKind, 4> Regions; 2425 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2426 const auto *VD = dyn_cast<VarDecl>(D); 2427 return VD && !VD->hasLocalStorage() && 2428 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2429 Level) && 2430 Regions[CaptureLevel] != OMPD_task; 2431 } 2432 2433 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2434 unsigned CaptureLevel) const { 2435 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2436 // Return true if the current level is no longer enclosed in a target region. 2437 2438 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2439 if (!VD->hasLocalStorage()) { 2440 if (isInOpenMPTargetExecutionDirective()) 2441 return true; 2442 DSAStackTy::DSAVarData TopDVar = 2443 DSAStack->getTopDSA(D, /*FromParent=*/false); 2444 unsigned NumLevels = 2445 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2446 if (Level == 0) 2447 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2448 do { 2449 --Level; 2450 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2451 if (DVar.CKind != OMPC_shared) 2452 return true; 2453 } while (Level > 0); 2454 } 2455 } 2456 return true; 2457 } 2458 2459 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2460 2461 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2462 OMPTraitInfo &TI) { 2463 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2464 } 2465 2466 void Sema::ActOnOpenMPEndDeclareVariant() { 2467 assert(isInOpenMPDeclareVariantScope() && 2468 "Not in OpenMP declare variant scope!"); 2469 2470 OMPDeclareVariantScopes.pop_back(); 2471 } 2472 2473 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2474 const FunctionDecl *Callee, 2475 SourceLocation Loc) { 2476 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2477 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2478 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2479 // Ignore host functions during device analyzis. 2480 if (LangOpts.OpenMPIsDevice && DevTy && 2481 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) 2482 return; 2483 // Ignore nohost functions during host analyzis. 2484 if (!LangOpts.OpenMPIsDevice && DevTy && 2485 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2486 return; 2487 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2488 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2489 if (LangOpts.OpenMPIsDevice && DevTy && 2490 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2491 // Diagnose host function called during device codegen. 2492 StringRef HostDevTy = 2493 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2494 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2495 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2496 diag::note_omp_marked_device_type_here) 2497 << HostDevTy; 2498 return; 2499 } 2500 if (!LangOpts.OpenMPIsDevice && DevTy && 2501 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2502 // Diagnose nohost function called during host codegen. 2503 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2504 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2505 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2506 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2507 diag::note_omp_marked_device_type_here) 2508 << NoHostDevTy; 2509 } 2510 } 2511 2512 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2513 const DeclarationNameInfo &DirName, 2514 Scope *CurScope, SourceLocation Loc) { 2515 DSAStack->push(DKind, DirName, CurScope, Loc); 2516 PushExpressionEvaluationContext( 2517 ExpressionEvaluationContext::PotentiallyEvaluated); 2518 } 2519 2520 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2521 DSAStack->setClauseParsingMode(K); 2522 } 2523 2524 void Sema::EndOpenMPClause() { 2525 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2526 } 2527 2528 static std::pair<ValueDecl *, bool> 2529 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2530 SourceRange &ERange, bool AllowArraySection = false); 2531 2532 /// Check consistency of the reduction clauses. 2533 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2534 ArrayRef<OMPClause *> Clauses) { 2535 bool InscanFound = false; 2536 SourceLocation InscanLoc; 2537 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2538 // A reduction clause without the inscan reduction-modifier may not appear on 2539 // a construct on which a reduction clause with the inscan reduction-modifier 2540 // appears. 2541 for (OMPClause *C : Clauses) { 2542 if (C->getClauseKind() != OMPC_reduction) 2543 continue; 2544 auto *RC = cast<OMPReductionClause>(C); 2545 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2546 InscanFound = true; 2547 InscanLoc = RC->getModifierLoc(); 2548 continue; 2549 } 2550 if (RC->getModifier() == OMPC_REDUCTION_task) { 2551 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2552 // A reduction clause with the task reduction-modifier may only appear on 2553 // a parallel construct, a worksharing construct or a combined or 2554 // composite construct for which any of the aforementioned constructs is a 2555 // constituent construct and simd or loop are not constituent constructs. 2556 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2557 if (!(isOpenMPParallelDirective(CurDir) || 2558 isOpenMPWorksharingDirective(CurDir)) || 2559 isOpenMPSimdDirective(CurDir)) 2560 S.Diag(RC->getModifierLoc(), 2561 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2562 continue; 2563 } 2564 } 2565 if (InscanFound) { 2566 for (OMPClause *C : Clauses) { 2567 if (C->getClauseKind() != OMPC_reduction) 2568 continue; 2569 auto *RC = cast<OMPReductionClause>(C); 2570 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2571 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2572 ? RC->getBeginLoc() 2573 : RC->getModifierLoc(), 2574 diag::err_omp_inscan_reduction_expected); 2575 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2576 continue; 2577 } 2578 for (Expr *Ref : RC->varlists()) { 2579 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2580 SourceLocation ELoc; 2581 SourceRange ERange; 2582 Expr *SimpleRefExpr = Ref; 2583 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2584 /*AllowArraySection=*/true); 2585 ValueDecl *D = Res.first; 2586 if (!D) 2587 continue; 2588 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2589 S.Diag(Ref->getExprLoc(), 2590 diag::err_omp_reduction_not_inclusive_exclusive) 2591 << Ref->getSourceRange(); 2592 } 2593 } 2594 } 2595 } 2596 } 2597 2598 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2599 ArrayRef<OMPClause *> Clauses); 2600 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2601 bool WithInit); 2602 2603 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2604 const ValueDecl *D, 2605 const DSAStackTy::DSAVarData &DVar, 2606 bool IsLoopIterVar = false); 2607 2608 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2609 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2610 // A variable of class type (or array thereof) that appears in a lastprivate 2611 // clause requires an accessible, unambiguous default constructor for the 2612 // class type, unless the list item is also specified in a firstprivate 2613 // clause. 2614 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2615 for (OMPClause *C : D->clauses()) { 2616 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2617 SmallVector<Expr *, 8> PrivateCopies; 2618 for (Expr *DE : Clause->varlists()) { 2619 if (DE->isValueDependent() || DE->isTypeDependent()) { 2620 PrivateCopies.push_back(nullptr); 2621 continue; 2622 } 2623 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2624 auto *VD = cast<VarDecl>(DRE->getDecl()); 2625 QualType Type = VD->getType().getNonReferenceType(); 2626 const DSAStackTy::DSAVarData DVar = 2627 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2628 if (DVar.CKind == OMPC_lastprivate) { 2629 // Generate helper private variable and initialize it with the 2630 // default value. The address of the original variable is replaced 2631 // by the address of the new private variable in CodeGen. This new 2632 // variable is not added to IdResolver, so the code in the OpenMP 2633 // region uses original variable for proper diagnostics. 2634 VarDecl *VDPrivate = buildVarDecl( 2635 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2636 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2637 ActOnUninitializedDecl(VDPrivate); 2638 if (VDPrivate->isInvalidDecl()) { 2639 PrivateCopies.push_back(nullptr); 2640 continue; 2641 } 2642 PrivateCopies.push_back(buildDeclRefExpr( 2643 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2644 } else { 2645 // The variable is also a firstprivate, so initialization sequence 2646 // for private copy is generated already. 2647 PrivateCopies.push_back(nullptr); 2648 } 2649 } 2650 Clause->setPrivateCopies(PrivateCopies); 2651 continue; 2652 } 2653 // Finalize nontemporal clause by handling private copies, if any. 2654 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2655 SmallVector<Expr *, 8> PrivateRefs; 2656 for (Expr *RefExpr : Clause->varlists()) { 2657 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2658 SourceLocation ELoc; 2659 SourceRange ERange; 2660 Expr *SimpleRefExpr = RefExpr; 2661 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2662 if (Res.second) 2663 // It will be analyzed later. 2664 PrivateRefs.push_back(RefExpr); 2665 ValueDecl *D = Res.first; 2666 if (!D) 2667 continue; 2668 2669 const DSAStackTy::DSAVarData DVar = 2670 DSAStack->getTopDSA(D, /*FromParent=*/false); 2671 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2672 : SimpleRefExpr); 2673 } 2674 Clause->setPrivateRefs(PrivateRefs); 2675 continue; 2676 } 2677 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2678 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2679 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2680 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2681 if (!DRE) 2682 continue; 2683 ValueDecl *VD = DRE->getDecl(); 2684 if (!VD || !isa<VarDecl>(VD)) 2685 continue; 2686 DSAStackTy::DSAVarData DVar = 2687 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2688 // OpenMP [2.12.5, target Construct] 2689 // Memory allocators that appear in a uses_allocators clause cannot 2690 // appear in other data-sharing attribute clauses or data-mapping 2691 // attribute clauses in the same construct. 2692 Expr *MapExpr = nullptr; 2693 if (DVar.RefExpr || 2694 DSAStack->checkMappableExprComponentListsForDecl( 2695 VD, /*CurrentRegionOnly=*/true, 2696 [VD, &MapExpr]( 2697 OMPClauseMappableExprCommon::MappableExprComponentListRef 2698 MapExprComponents, 2699 OpenMPClauseKind C) { 2700 auto MI = MapExprComponents.rbegin(); 2701 auto ME = MapExprComponents.rend(); 2702 if (MI != ME && 2703 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2704 VD->getCanonicalDecl()) { 2705 MapExpr = MI->getAssociatedExpression(); 2706 return true; 2707 } 2708 return false; 2709 })) { 2710 Diag(D.Allocator->getExprLoc(), 2711 diag::err_omp_allocator_used_in_clauses) 2712 << D.Allocator->getSourceRange(); 2713 if (DVar.RefExpr) 2714 reportOriginalDsa(*this, DSAStack, VD, DVar); 2715 else 2716 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2717 << MapExpr->getSourceRange(); 2718 } 2719 } 2720 continue; 2721 } 2722 } 2723 // Check allocate clauses. 2724 if (!CurContext->isDependentContext()) 2725 checkAllocateClauses(*this, DSAStack, D->clauses()); 2726 checkReductionClauses(*this, DSAStack, D->clauses()); 2727 } 2728 2729 DSAStack->pop(); 2730 DiscardCleanupsInEvaluationContext(); 2731 PopExpressionEvaluationContext(); 2732 } 2733 2734 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2735 Expr *NumIterations, Sema &SemaRef, 2736 Scope *S, DSAStackTy *Stack); 2737 2738 namespace { 2739 2740 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2741 private: 2742 Sema &SemaRef; 2743 2744 public: 2745 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2746 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2747 NamedDecl *ND = Candidate.getCorrectionDecl(); 2748 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2749 return VD->hasGlobalStorage() && 2750 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2751 SemaRef.getCurScope()); 2752 } 2753 return false; 2754 } 2755 2756 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2757 return std::make_unique<VarDeclFilterCCC>(*this); 2758 } 2759 2760 }; 2761 2762 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2763 private: 2764 Sema &SemaRef; 2765 2766 public: 2767 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2768 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2769 NamedDecl *ND = Candidate.getCorrectionDecl(); 2770 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2771 isa<FunctionDecl>(ND))) { 2772 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2773 SemaRef.getCurScope()); 2774 } 2775 return false; 2776 } 2777 2778 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2779 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2780 } 2781 }; 2782 2783 } // namespace 2784 2785 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2786 CXXScopeSpec &ScopeSpec, 2787 const DeclarationNameInfo &Id, 2788 OpenMPDirectiveKind Kind) { 2789 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2790 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2791 2792 if (Lookup.isAmbiguous()) 2793 return ExprError(); 2794 2795 VarDecl *VD; 2796 if (!Lookup.isSingleResult()) { 2797 VarDeclFilterCCC CCC(*this); 2798 if (TypoCorrection Corrected = 2799 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2800 CTK_ErrorRecovery)) { 2801 diagnoseTypo(Corrected, 2802 PDiag(Lookup.empty() 2803 ? diag::err_undeclared_var_use_suggest 2804 : diag::err_omp_expected_var_arg_suggest) 2805 << Id.getName()); 2806 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2807 } else { 2808 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2809 : diag::err_omp_expected_var_arg) 2810 << Id.getName(); 2811 return ExprError(); 2812 } 2813 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2814 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2815 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2816 return ExprError(); 2817 } 2818 Lookup.suppressDiagnostics(); 2819 2820 // OpenMP [2.9.2, Syntax, C/C++] 2821 // Variables must be file-scope, namespace-scope, or static block-scope. 2822 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2823 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2824 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2825 bool IsDecl = 2826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2827 Diag(VD->getLocation(), 2828 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2829 << VD; 2830 return ExprError(); 2831 } 2832 2833 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2834 NamedDecl *ND = CanonicalVD; 2835 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2836 // A threadprivate directive for file-scope variables must appear outside 2837 // any definition or declaration. 2838 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2839 !getCurLexicalContext()->isTranslationUnit()) { 2840 Diag(Id.getLoc(), diag::err_omp_var_scope) 2841 << getOpenMPDirectiveName(Kind) << VD; 2842 bool IsDecl = 2843 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2844 Diag(VD->getLocation(), 2845 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2846 << VD; 2847 return ExprError(); 2848 } 2849 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2850 // A threadprivate directive for static class member variables must appear 2851 // in the class definition, in the same scope in which the member 2852 // variables are declared. 2853 if (CanonicalVD->isStaticDataMember() && 2854 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2855 Diag(Id.getLoc(), diag::err_omp_var_scope) 2856 << getOpenMPDirectiveName(Kind) << VD; 2857 bool IsDecl = 2858 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2859 Diag(VD->getLocation(), 2860 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2861 << VD; 2862 return ExprError(); 2863 } 2864 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2865 // A threadprivate directive for namespace-scope variables must appear 2866 // outside any definition or declaration other than the namespace 2867 // definition itself. 2868 if (CanonicalVD->getDeclContext()->isNamespace() && 2869 (!getCurLexicalContext()->isFileContext() || 2870 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2871 Diag(Id.getLoc(), diag::err_omp_var_scope) 2872 << getOpenMPDirectiveName(Kind) << VD; 2873 bool IsDecl = 2874 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2875 Diag(VD->getLocation(), 2876 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2877 << VD; 2878 return ExprError(); 2879 } 2880 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2881 // A threadprivate directive for static block-scope variables must appear 2882 // in the scope of the variable and not in a nested scope. 2883 if (CanonicalVD->isLocalVarDecl() && CurScope && 2884 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2885 Diag(Id.getLoc(), diag::err_omp_var_scope) 2886 << getOpenMPDirectiveName(Kind) << VD; 2887 bool IsDecl = 2888 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2889 Diag(VD->getLocation(), 2890 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2891 << VD; 2892 return ExprError(); 2893 } 2894 2895 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2896 // A threadprivate directive must lexically precede all references to any 2897 // of the variables in its list. 2898 if (Kind == OMPD_threadprivate && VD->isUsed() && 2899 !DSAStack->isThreadPrivate(VD)) { 2900 Diag(Id.getLoc(), diag::err_omp_var_used) 2901 << getOpenMPDirectiveName(Kind) << VD; 2902 return ExprError(); 2903 } 2904 2905 QualType ExprType = VD->getType().getNonReferenceType(); 2906 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2907 SourceLocation(), VD, 2908 /*RefersToEnclosingVariableOrCapture=*/false, 2909 Id.getLoc(), ExprType, VK_LValue); 2910 } 2911 2912 Sema::DeclGroupPtrTy 2913 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2914 ArrayRef<Expr *> VarList) { 2915 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2916 CurContext->addDecl(D); 2917 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2918 } 2919 return nullptr; 2920 } 2921 2922 namespace { 2923 class LocalVarRefChecker final 2924 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2925 Sema &SemaRef; 2926 2927 public: 2928 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2929 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2930 if (VD->hasLocalStorage()) { 2931 SemaRef.Diag(E->getBeginLoc(), 2932 diag::err_omp_local_var_in_threadprivate_init) 2933 << E->getSourceRange(); 2934 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2935 << VD << VD->getSourceRange(); 2936 return true; 2937 } 2938 } 2939 return false; 2940 } 2941 bool VisitStmt(const Stmt *S) { 2942 for (const Stmt *Child : S->children()) { 2943 if (Child && Visit(Child)) 2944 return true; 2945 } 2946 return false; 2947 } 2948 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2949 }; 2950 } // namespace 2951 2952 OMPThreadPrivateDecl * 2953 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2954 SmallVector<Expr *, 8> Vars; 2955 for (Expr *RefExpr : VarList) { 2956 auto *DE = cast<DeclRefExpr>(RefExpr); 2957 auto *VD = cast<VarDecl>(DE->getDecl()); 2958 SourceLocation ILoc = DE->getExprLoc(); 2959 2960 // Mark variable as used. 2961 VD->setReferenced(); 2962 VD->markUsed(Context); 2963 2964 QualType QType = VD->getType(); 2965 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2966 // It will be analyzed later. 2967 Vars.push_back(DE); 2968 continue; 2969 } 2970 2971 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2972 // A threadprivate variable must not have an incomplete type. 2973 if (RequireCompleteType(ILoc, VD->getType(), 2974 diag::err_omp_threadprivate_incomplete_type)) { 2975 continue; 2976 } 2977 2978 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2979 // A threadprivate variable must not have a reference type. 2980 if (VD->getType()->isReferenceType()) { 2981 Diag(ILoc, diag::err_omp_ref_type_arg) 2982 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2983 bool IsDecl = 2984 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2985 Diag(VD->getLocation(), 2986 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2987 << VD; 2988 continue; 2989 } 2990 2991 // Check if this is a TLS variable. If TLS is not being supported, produce 2992 // the corresponding diagnostic. 2993 if ((VD->getTLSKind() != VarDecl::TLS_None && 2994 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2995 getLangOpts().OpenMPUseTLS && 2996 getASTContext().getTargetInfo().isTLSSupported())) || 2997 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2998 !VD->isLocalVarDecl())) { 2999 Diag(ILoc, diag::err_omp_var_thread_local) 3000 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 3001 bool IsDecl = 3002 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3003 Diag(VD->getLocation(), 3004 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3005 << VD; 3006 continue; 3007 } 3008 3009 // Check if initial value of threadprivate variable reference variable with 3010 // local storage (it is not supported by runtime). 3011 if (const Expr *Init = VD->getAnyInitializer()) { 3012 LocalVarRefChecker Checker(*this); 3013 if (Checker.Visit(Init)) 3014 continue; 3015 } 3016 3017 Vars.push_back(RefExpr); 3018 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 3019 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3020 Context, SourceRange(Loc, Loc))); 3021 if (ASTMutationListener *ML = Context.getASTMutationListener()) 3022 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 3023 } 3024 OMPThreadPrivateDecl *D = nullptr; 3025 if (!Vars.empty()) { 3026 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 3027 Vars); 3028 D->setAccess(AS_public); 3029 } 3030 return D; 3031 } 3032 3033 static OMPAllocateDeclAttr::AllocatorTypeTy 3034 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 3035 if (!Allocator) 3036 return OMPAllocateDeclAttr::OMPNullMemAlloc; 3037 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3038 Allocator->isInstantiationDependent() || 3039 Allocator->containsUnexpandedParameterPack()) 3040 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3041 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3042 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3043 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 3044 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 3045 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 3046 llvm::FoldingSetNodeID AEId, DAEId; 3047 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 3048 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 3049 if (AEId == DAEId) { 3050 AllocatorKindRes = AllocatorKind; 3051 break; 3052 } 3053 } 3054 return AllocatorKindRes; 3055 } 3056 3057 static bool checkPreviousOMPAllocateAttribute( 3058 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3059 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3060 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3061 return false; 3062 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3063 Expr *PrevAllocator = A->getAllocator(); 3064 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3065 getAllocatorKind(S, Stack, PrevAllocator); 3066 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3067 if (AllocatorsMatch && 3068 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3069 Allocator && PrevAllocator) { 3070 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3071 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3072 llvm::FoldingSetNodeID AEId, PAEId; 3073 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3074 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3075 AllocatorsMatch = AEId == PAEId; 3076 } 3077 if (!AllocatorsMatch) { 3078 SmallString<256> AllocatorBuffer; 3079 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3080 if (Allocator) 3081 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3082 SmallString<256> PrevAllocatorBuffer; 3083 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3084 if (PrevAllocator) 3085 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3086 S.getPrintingPolicy()); 3087 3088 SourceLocation AllocatorLoc = 3089 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3090 SourceRange AllocatorRange = 3091 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3092 SourceLocation PrevAllocatorLoc = 3093 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3094 SourceRange PrevAllocatorRange = 3095 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3096 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3097 << (Allocator ? 1 : 0) << AllocatorStream.str() 3098 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3099 << AllocatorRange; 3100 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3101 << PrevAllocatorRange; 3102 return true; 3103 } 3104 return false; 3105 } 3106 3107 static void 3108 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3109 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3110 Expr *Allocator, SourceRange SR) { 3111 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3112 return; 3113 if (Allocator && 3114 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3115 Allocator->isInstantiationDependent() || 3116 Allocator->containsUnexpandedParameterPack())) 3117 return; 3118 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3119 Allocator, SR); 3120 VD->addAttr(A); 3121 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3122 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3123 } 3124 3125 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3126 SourceLocation Loc, ArrayRef<Expr *> VarList, 3127 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3128 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3129 Expr *Allocator = nullptr; 3130 if (Clauses.empty()) { 3131 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3132 // allocate directives that appear in a target region must specify an 3133 // allocator clause unless a requires directive with the dynamic_allocators 3134 // clause is present in the same compilation unit. 3135 if (LangOpts.OpenMPIsDevice && 3136 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3137 targetDiag(Loc, diag::err_expected_allocator_clause); 3138 } else { 3139 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3140 } 3141 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3142 getAllocatorKind(*this, DSAStack, Allocator); 3143 SmallVector<Expr *, 8> Vars; 3144 for (Expr *RefExpr : VarList) { 3145 auto *DE = cast<DeclRefExpr>(RefExpr); 3146 auto *VD = cast<VarDecl>(DE->getDecl()); 3147 3148 // Check if this is a TLS variable or global register. 3149 if (VD->getTLSKind() != VarDecl::TLS_None || 3150 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3151 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3152 !VD->isLocalVarDecl())) 3153 continue; 3154 3155 // If the used several times in the allocate directive, the same allocator 3156 // must be used. 3157 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3158 AllocatorKind, Allocator)) 3159 continue; 3160 3161 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3162 // If a list item has a static storage type, the allocator expression in the 3163 // allocator clause must be a constant expression that evaluates to one of 3164 // the predefined memory allocator values. 3165 if (Allocator && VD->hasGlobalStorage()) { 3166 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3167 Diag(Allocator->getExprLoc(), 3168 diag::err_omp_expected_predefined_allocator) 3169 << Allocator->getSourceRange(); 3170 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3171 VarDecl::DeclarationOnly; 3172 Diag(VD->getLocation(), 3173 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3174 << VD; 3175 continue; 3176 } 3177 } 3178 3179 Vars.push_back(RefExpr); 3180 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3181 DE->getSourceRange()); 3182 } 3183 if (Vars.empty()) 3184 return nullptr; 3185 if (!Owner) 3186 Owner = getCurLexicalContext(); 3187 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3188 D->setAccess(AS_public); 3189 Owner->addDecl(D); 3190 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3191 } 3192 3193 Sema::DeclGroupPtrTy 3194 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3195 ArrayRef<OMPClause *> ClauseList) { 3196 OMPRequiresDecl *D = nullptr; 3197 if (!CurContext->isFileContext()) { 3198 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3199 } else { 3200 D = CheckOMPRequiresDecl(Loc, ClauseList); 3201 if (D) { 3202 CurContext->addDecl(D); 3203 DSAStack->addRequiresDecl(D); 3204 } 3205 } 3206 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3207 } 3208 3209 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc, 3210 OpenMPDirectiveKind DKind, 3211 ArrayRef<StringRef> Assumptions, 3212 bool SkippedClauses) { 3213 if (!SkippedClauses && Assumptions.empty()) 3214 Diag(Loc, diag::err_omp_no_clause_for_directive) 3215 << llvm::omp::getAllAssumeClauseOptions() 3216 << llvm::omp::getOpenMPDirectiveName(DKind); 3217 3218 auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc); 3219 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) { 3220 OMPAssumeScoped.push_back(AA); 3221 return; 3222 } 3223 3224 // Global assumes without assumption clauses are ignored. 3225 if (Assumptions.empty()) 3226 return; 3227 3228 assert(DKind == llvm::omp::Directive::OMPD_assumes && 3229 "Unexpected omp assumption directive!"); 3230 OMPAssumeGlobal.push_back(AA); 3231 3232 // The OMPAssumeGlobal scope above will take care of new declarations but 3233 // we also want to apply the assumption to existing ones, e.g., to 3234 // declarations in included headers. To this end, we traverse all existing 3235 // declaration contexts and annotate function declarations here. 3236 SmallVector<DeclContext *, 8> DeclContexts; 3237 auto *Ctx = CurContext; 3238 while (Ctx->getLexicalParent()) 3239 Ctx = Ctx->getLexicalParent(); 3240 DeclContexts.push_back(Ctx); 3241 while (!DeclContexts.empty()) { 3242 DeclContext *DC = DeclContexts.pop_back_val(); 3243 for (auto *SubDC : DC->decls()) { 3244 if (SubDC->isInvalidDecl()) 3245 continue; 3246 if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) { 3247 DeclContexts.push_back(CTD->getTemplatedDecl()); 3248 for (auto *S : CTD->specializations()) 3249 DeclContexts.push_back(S); 3250 continue; 3251 } 3252 if (auto *DC = dyn_cast<DeclContext>(SubDC)) 3253 DeclContexts.push_back(DC); 3254 if (auto *F = dyn_cast<FunctionDecl>(SubDC)) { 3255 F->addAttr(AA); 3256 continue; 3257 } 3258 } 3259 } 3260 } 3261 3262 void Sema::ActOnOpenMPEndAssumesDirective() { 3263 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!"); 3264 OMPAssumeScoped.pop_back(); 3265 } 3266 3267 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3268 ArrayRef<OMPClause *> ClauseList) { 3269 /// For target specific clauses, the requires directive cannot be 3270 /// specified after the handling of any of the target regions in the 3271 /// current compilation unit. 3272 ArrayRef<SourceLocation> TargetLocations = 3273 DSAStack->getEncounteredTargetLocs(); 3274 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3275 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3276 for (const OMPClause *CNew : ClauseList) { 3277 // Check if any of the requires clauses affect target regions. 3278 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3279 isa<OMPUnifiedAddressClause>(CNew) || 3280 isa<OMPReverseOffloadClause>(CNew) || 3281 isa<OMPDynamicAllocatorsClause>(CNew)) { 3282 Diag(Loc, diag::err_omp_directive_before_requires) 3283 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3284 for (SourceLocation TargetLoc : TargetLocations) { 3285 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3286 << "target"; 3287 } 3288 } else if (!AtomicLoc.isInvalid() && 3289 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3290 Diag(Loc, diag::err_omp_directive_before_requires) 3291 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3292 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3293 << "atomic"; 3294 } 3295 } 3296 } 3297 3298 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3299 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3300 ClauseList); 3301 return nullptr; 3302 } 3303 3304 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3305 const ValueDecl *D, 3306 const DSAStackTy::DSAVarData &DVar, 3307 bool IsLoopIterVar) { 3308 if (DVar.RefExpr) { 3309 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3310 << getOpenMPClauseName(DVar.CKind); 3311 return; 3312 } 3313 enum { 3314 PDSA_StaticMemberShared, 3315 PDSA_StaticLocalVarShared, 3316 PDSA_LoopIterVarPrivate, 3317 PDSA_LoopIterVarLinear, 3318 PDSA_LoopIterVarLastprivate, 3319 PDSA_ConstVarShared, 3320 PDSA_GlobalVarShared, 3321 PDSA_TaskVarFirstprivate, 3322 PDSA_LocalVarPrivate, 3323 PDSA_Implicit 3324 } Reason = PDSA_Implicit; 3325 bool ReportHint = false; 3326 auto ReportLoc = D->getLocation(); 3327 auto *VD = dyn_cast<VarDecl>(D); 3328 if (IsLoopIterVar) { 3329 if (DVar.CKind == OMPC_private) 3330 Reason = PDSA_LoopIterVarPrivate; 3331 else if (DVar.CKind == OMPC_lastprivate) 3332 Reason = PDSA_LoopIterVarLastprivate; 3333 else 3334 Reason = PDSA_LoopIterVarLinear; 3335 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3336 DVar.CKind == OMPC_firstprivate) { 3337 Reason = PDSA_TaskVarFirstprivate; 3338 ReportLoc = DVar.ImplicitDSALoc; 3339 } else if (VD && VD->isStaticLocal()) 3340 Reason = PDSA_StaticLocalVarShared; 3341 else if (VD && VD->isStaticDataMember()) 3342 Reason = PDSA_StaticMemberShared; 3343 else if (VD && VD->isFileVarDecl()) 3344 Reason = PDSA_GlobalVarShared; 3345 else if (D->getType().isConstant(SemaRef.getASTContext())) 3346 Reason = PDSA_ConstVarShared; 3347 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3348 ReportHint = true; 3349 Reason = PDSA_LocalVarPrivate; 3350 } 3351 if (Reason != PDSA_Implicit) { 3352 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3353 << Reason << ReportHint 3354 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3355 } else if (DVar.ImplicitDSALoc.isValid()) { 3356 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3357 << getOpenMPClauseName(DVar.CKind); 3358 } 3359 } 3360 3361 static OpenMPMapClauseKind 3362 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3363 bool IsAggregateOrDeclareTarget) { 3364 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3365 switch (M) { 3366 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3367 Kind = OMPC_MAP_alloc; 3368 break; 3369 case OMPC_DEFAULTMAP_MODIFIER_to: 3370 Kind = OMPC_MAP_to; 3371 break; 3372 case OMPC_DEFAULTMAP_MODIFIER_from: 3373 Kind = OMPC_MAP_from; 3374 break; 3375 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3376 Kind = OMPC_MAP_tofrom; 3377 break; 3378 case OMPC_DEFAULTMAP_MODIFIER_present: 3379 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description] 3380 // If implicit-behavior is present, each variable referenced in the 3381 // construct in the category specified by variable-category is treated as if 3382 // it had been listed in a map clause with the map-type of alloc and 3383 // map-type-modifier of present. 3384 Kind = OMPC_MAP_alloc; 3385 break; 3386 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3387 case OMPC_DEFAULTMAP_MODIFIER_last: 3388 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3389 case OMPC_DEFAULTMAP_MODIFIER_none: 3390 case OMPC_DEFAULTMAP_MODIFIER_default: 3391 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3392 // IsAggregateOrDeclareTarget could be true if: 3393 // 1. the implicit behavior for aggregate is tofrom 3394 // 2. it's a declare target link 3395 if (IsAggregateOrDeclareTarget) { 3396 Kind = OMPC_MAP_tofrom; 3397 break; 3398 } 3399 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3400 } 3401 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3402 return Kind; 3403 } 3404 3405 namespace { 3406 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3407 DSAStackTy *Stack; 3408 Sema &SemaRef; 3409 bool ErrorFound = false; 3410 bool TryCaptureCXXThisMembers = false; 3411 CapturedStmt *CS = nullptr; 3412 const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 3413 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3414 llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete]; 3415 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 3416 ImplicitMapModifier[DefaultmapKindNum]; 3417 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3418 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3419 3420 void VisitSubCaptures(OMPExecutableDirective *S) { 3421 // Check implicitly captured variables. 3422 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3423 return; 3424 if (S->getDirectiveKind() == OMPD_atomic || 3425 S->getDirectiveKind() == OMPD_critical || 3426 S->getDirectiveKind() == OMPD_section || 3427 S->getDirectiveKind() == OMPD_master || 3428 S->getDirectiveKind() == OMPD_masked || 3429 isOpenMPLoopTransformationDirective(S->getDirectiveKind())) { 3430 Visit(S->getAssociatedStmt()); 3431 return; 3432 } 3433 visitSubCaptures(S->getInnermostCapturedStmt()); 3434 // Try to capture inner this->member references to generate correct mappings 3435 // and diagnostics. 3436 if (TryCaptureCXXThisMembers || 3437 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3438 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3439 [](const CapturedStmt::Capture &C) { 3440 return C.capturesThis(); 3441 }))) { 3442 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3443 TryCaptureCXXThisMembers = true; 3444 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3445 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3446 } 3447 // In tasks firstprivates are not captured anymore, need to analyze them 3448 // explicitly. 3449 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3450 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3451 for (OMPClause *C : S->clauses()) 3452 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3453 for (Expr *Ref : FC->varlists()) 3454 Visit(Ref); 3455 } 3456 } 3457 } 3458 3459 public: 3460 void VisitDeclRefExpr(DeclRefExpr *E) { 3461 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3462 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3463 E->isInstantiationDependent()) 3464 return; 3465 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3466 // Check the datasharing rules for the expressions in the clauses. 3467 if (!CS) { 3468 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3469 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3470 Visit(CED->getInit()); 3471 return; 3472 } 3473 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3474 // Do not analyze internal variables and do not enclose them into 3475 // implicit clauses. 3476 return; 3477 VD = VD->getCanonicalDecl(); 3478 // Skip internally declared variables. 3479 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3480 !Stack->isImplicitTaskFirstprivate(VD)) 3481 return; 3482 // Skip allocators in uses_allocators clauses. 3483 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3484 return; 3485 3486 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3487 // Check if the variable has explicit DSA set and stop analysis if it so. 3488 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3489 return; 3490 3491 // Skip internally declared static variables. 3492 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3493 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3494 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3495 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3496 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3497 !Stack->isImplicitTaskFirstprivate(VD)) 3498 return; 3499 3500 SourceLocation ELoc = E->getExprLoc(); 3501 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3502 // The default(none) clause requires that each variable that is referenced 3503 // in the construct, and does not have a predetermined data-sharing 3504 // attribute, must have its data-sharing attribute explicitly determined 3505 // by being listed in a data-sharing attribute clause. 3506 if (DVar.CKind == OMPC_unknown && 3507 (Stack->getDefaultDSA() == DSA_none || 3508 Stack->getDefaultDSA() == DSA_firstprivate) && 3509 isImplicitOrExplicitTaskingRegion(DKind) && 3510 VarsWithInheritedDSA.count(VD) == 0) { 3511 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3512 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3513 DSAStackTy::DSAVarData DVar = 3514 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3515 InheritedDSA = DVar.CKind == OMPC_unknown; 3516 } 3517 if (InheritedDSA) 3518 VarsWithInheritedDSA[VD] = E; 3519 return; 3520 } 3521 3522 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3523 // If implicit-behavior is none, each variable referenced in the 3524 // construct that does not have a predetermined data-sharing attribute 3525 // and does not appear in a to or link clause on a declare target 3526 // directive must be listed in a data-mapping attribute clause, a 3527 // data-haring attribute clause (including a data-sharing attribute 3528 // clause on a combined construct where target. is one of the 3529 // constituent constructs), or an is_device_ptr clause. 3530 OpenMPDefaultmapClauseKind ClauseKind = 3531 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3532 if (SemaRef.getLangOpts().OpenMP >= 50) { 3533 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3534 OMPC_DEFAULTMAP_MODIFIER_none; 3535 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3536 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3537 // Only check for data-mapping attribute and is_device_ptr here 3538 // since we have already make sure that the declaration does not 3539 // have a data-sharing attribute above 3540 if (!Stack->checkMappableExprComponentListsForDecl( 3541 VD, /*CurrentRegionOnly=*/true, 3542 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3543 MapExprComponents, 3544 OpenMPClauseKind) { 3545 auto MI = MapExprComponents.rbegin(); 3546 auto ME = MapExprComponents.rend(); 3547 return MI != ME && MI->getAssociatedDeclaration() == VD; 3548 })) { 3549 VarsWithInheritedDSA[VD] = E; 3550 return; 3551 } 3552 } 3553 } 3554 if (SemaRef.getLangOpts().OpenMP > 50) { 3555 bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) == 3556 OMPC_DEFAULTMAP_MODIFIER_present; 3557 if (IsModifierPresent) { 3558 if (llvm::find(ImplicitMapModifier[ClauseKind], 3559 OMPC_MAP_MODIFIER_present) == 3560 std::end(ImplicitMapModifier[ClauseKind])) { 3561 ImplicitMapModifier[ClauseKind].push_back( 3562 OMPC_MAP_MODIFIER_present); 3563 } 3564 } 3565 } 3566 3567 if (isOpenMPTargetExecutionDirective(DKind) && 3568 !Stack->isLoopControlVariable(VD).first) { 3569 if (!Stack->checkMappableExprComponentListsForDecl( 3570 VD, /*CurrentRegionOnly=*/true, 3571 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef 3572 StackComponents, 3573 OpenMPClauseKind) { 3574 if (SemaRef.LangOpts.OpenMP >= 50) 3575 return !StackComponents.empty(); 3576 // Variable is used if it has been marked as an array, array 3577 // section, array shaping or the variable iself. 3578 return StackComponents.size() == 1 || 3579 std::all_of( 3580 std::next(StackComponents.rbegin()), 3581 StackComponents.rend(), 3582 [](const OMPClauseMappableExprCommon:: 3583 MappableComponent &MC) { 3584 return MC.getAssociatedDeclaration() == 3585 nullptr && 3586 (isa<OMPArraySectionExpr>( 3587 MC.getAssociatedExpression()) || 3588 isa<OMPArrayShapingExpr>( 3589 MC.getAssociatedExpression()) || 3590 isa<ArraySubscriptExpr>( 3591 MC.getAssociatedExpression())); 3592 }); 3593 })) { 3594 bool IsFirstprivate = false; 3595 // By default lambdas are captured as firstprivates. 3596 if (const auto *RD = 3597 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3598 IsFirstprivate = RD->isLambda(); 3599 IsFirstprivate = 3600 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3601 if (IsFirstprivate) { 3602 ImplicitFirstprivate.emplace_back(E); 3603 } else { 3604 OpenMPDefaultmapClauseModifier M = 3605 Stack->getDefaultmapModifier(ClauseKind); 3606 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3607 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3608 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3609 } 3610 return; 3611 } 3612 } 3613 3614 // OpenMP [2.9.3.6, Restrictions, p.2] 3615 // A list item that appears in a reduction clause of the innermost 3616 // enclosing worksharing or parallel construct may not be accessed in an 3617 // explicit task. 3618 DVar = Stack->hasInnermostDSA( 3619 VD, 3620 [](OpenMPClauseKind C, bool AppliedToPointee) { 3621 return C == OMPC_reduction && !AppliedToPointee; 3622 }, 3623 [](OpenMPDirectiveKind K) { 3624 return isOpenMPParallelDirective(K) || 3625 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3626 }, 3627 /*FromParent=*/true); 3628 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3629 ErrorFound = true; 3630 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3631 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3632 return; 3633 } 3634 3635 // Define implicit data-sharing attributes for task. 3636 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3637 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3638 (Stack->getDefaultDSA() == DSA_firstprivate && 3639 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3640 !Stack->isLoopControlVariable(VD).first) { 3641 ImplicitFirstprivate.push_back(E); 3642 return; 3643 } 3644 3645 // Store implicitly used globals with declare target link for parent 3646 // target. 3647 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3648 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3649 Stack->addToParentTargetRegionLinkGlobals(E); 3650 return; 3651 } 3652 } 3653 } 3654 void VisitMemberExpr(MemberExpr *E) { 3655 if (E->isTypeDependent() || E->isValueDependent() || 3656 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3657 return; 3658 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3659 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3660 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3661 if (!FD) 3662 return; 3663 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3664 // Check if the variable has explicit DSA set and stop analysis if it 3665 // so. 3666 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3667 return; 3668 3669 if (isOpenMPTargetExecutionDirective(DKind) && 3670 !Stack->isLoopControlVariable(FD).first && 3671 !Stack->checkMappableExprComponentListsForDecl( 3672 FD, /*CurrentRegionOnly=*/true, 3673 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3674 StackComponents, 3675 OpenMPClauseKind) { 3676 return isa<CXXThisExpr>( 3677 cast<MemberExpr>( 3678 StackComponents.back().getAssociatedExpression()) 3679 ->getBase() 3680 ->IgnoreParens()); 3681 })) { 3682 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3683 // A bit-field cannot appear in a map clause. 3684 // 3685 if (FD->isBitField()) 3686 return; 3687 3688 // Check to see if the member expression is referencing a class that 3689 // has already been explicitly mapped 3690 if (Stack->isClassPreviouslyMapped(TE->getType())) 3691 return; 3692 3693 OpenMPDefaultmapClauseModifier Modifier = 3694 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3695 OpenMPDefaultmapClauseKind ClauseKind = 3696 getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD); 3697 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3698 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3699 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3700 return; 3701 } 3702 3703 SourceLocation ELoc = E->getExprLoc(); 3704 // OpenMP [2.9.3.6, Restrictions, p.2] 3705 // A list item that appears in a reduction clause of the innermost 3706 // enclosing worksharing or parallel construct may not be accessed in 3707 // an explicit task. 3708 DVar = Stack->hasInnermostDSA( 3709 FD, 3710 [](OpenMPClauseKind C, bool AppliedToPointee) { 3711 return C == OMPC_reduction && !AppliedToPointee; 3712 }, 3713 [](OpenMPDirectiveKind K) { 3714 return isOpenMPParallelDirective(K) || 3715 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3716 }, 3717 /*FromParent=*/true); 3718 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3719 ErrorFound = true; 3720 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3721 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3722 return; 3723 } 3724 3725 // Define implicit data-sharing attributes for task. 3726 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3727 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3728 !Stack->isLoopControlVariable(FD).first) { 3729 // Check if there is a captured expression for the current field in the 3730 // region. Do not mark it as firstprivate unless there is no captured 3731 // expression. 3732 // TODO: try to make it firstprivate. 3733 if (DVar.CKind != OMPC_unknown) 3734 ImplicitFirstprivate.push_back(E); 3735 } 3736 return; 3737 } 3738 if (isOpenMPTargetExecutionDirective(DKind)) { 3739 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3740 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3741 Stack->getCurrentDirective(), 3742 /*NoDiagnose=*/true)) 3743 return; 3744 const auto *VD = cast<ValueDecl>( 3745 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3746 if (!Stack->checkMappableExprComponentListsForDecl( 3747 VD, /*CurrentRegionOnly=*/true, 3748 [&CurComponents]( 3749 OMPClauseMappableExprCommon::MappableExprComponentListRef 3750 StackComponents, 3751 OpenMPClauseKind) { 3752 auto CCI = CurComponents.rbegin(); 3753 auto CCE = CurComponents.rend(); 3754 for (const auto &SC : llvm::reverse(StackComponents)) { 3755 // Do both expressions have the same kind? 3756 if (CCI->getAssociatedExpression()->getStmtClass() != 3757 SC.getAssociatedExpression()->getStmtClass()) 3758 if (!((isa<OMPArraySectionExpr>( 3759 SC.getAssociatedExpression()) || 3760 isa<OMPArrayShapingExpr>( 3761 SC.getAssociatedExpression())) && 3762 isa<ArraySubscriptExpr>( 3763 CCI->getAssociatedExpression()))) 3764 return false; 3765 3766 const Decl *CCD = CCI->getAssociatedDeclaration(); 3767 const Decl *SCD = SC.getAssociatedDeclaration(); 3768 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3769 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3770 if (SCD != CCD) 3771 return false; 3772 std::advance(CCI, 1); 3773 if (CCI == CCE) 3774 break; 3775 } 3776 return true; 3777 })) { 3778 Visit(E->getBase()); 3779 } 3780 } else if (!TryCaptureCXXThisMembers) { 3781 Visit(E->getBase()); 3782 } 3783 } 3784 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3785 for (OMPClause *C : S->clauses()) { 3786 // Skip analysis of arguments of implicitly defined firstprivate clause 3787 // for task|target directives. 3788 // Skip analysis of arguments of implicitly defined map clause for target 3789 // directives. 3790 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3791 C->isImplicit() && 3792 !isOpenMPTaskingDirective(Stack->getCurrentDirective()))) { 3793 for (Stmt *CC : C->children()) { 3794 if (CC) 3795 Visit(CC); 3796 } 3797 } 3798 } 3799 // Check implicitly captured variables. 3800 VisitSubCaptures(S); 3801 } 3802 3803 void VisitOMPTileDirective(OMPTileDirective *S) { 3804 // #pragma omp tile does not introduce data sharing. 3805 VisitStmt(S); 3806 } 3807 3808 void VisitStmt(Stmt *S) { 3809 for (Stmt *C : S->children()) { 3810 if (C) { 3811 // Check implicitly captured variables in the task-based directives to 3812 // check if they must be firstprivatized. 3813 Visit(C); 3814 } 3815 } 3816 } 3817 3818 void visitSubCaptures(CapturedStmt *S) { 3819 for (const CapturedStmt::Capture &Cap : S->captures()) { 3820 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3821 continue; 3822 VarDecl *VD = Cap.getCapturedVar(); 3823 // Do not try to map the variable if it or its sub-component was mapped 3824 // already. 3825 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3826 Stack->checkMappableExprComponentListsForDecl( 3827 VD, /*CurrentRegionOnly=*/true, 3828 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3829 OpenMPClauseKind) { return true; })) 3830 continue; 3831 DeclRefExpr *DRE = buildDeclRefExpr( 3832 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3833 Cap.getLocation(), /*RefersToCapture=*/true); 3834 Visit(DRE); 3835 } 3836 } 3837 bool isErrorFound() const { return ErrorFound; } 3838 ArrayRef<Expr *> getImplicitFirstprivate() const { 3839 return ImplicitFirstprivate; 3840 } 3841 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK, 3842 OpenMPMapClauseKind MK) const { 3843 return ImplicitMap[DK][MK]; 3844 } 3845 ArrayRef<OpenMPMapModifierKind> 3846 getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const { 3847 return ImplicitMapModifier[Kind]; 3848 } 3849 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3850 return VarsWithInheritedDSA; 3851 } 3852 3853 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3854 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3855 // Process declare target link variables for the target directives. 3856 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3857 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3858 Visit(E); 3859 } 3860 } 3861 }; 3862 } // namespace 3863 3864 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3865 switch (DKind) { 3866 case OMPD_parallel: 3867 case OMPD_parallel_for: 3868 case OMPD_parallel_for_simd: 3869 case OMPD_parallel_sections: 3870 case OMPD_parallel_master: 3871 case OMPD_teams: 3872 case OMPD_teams_distribute: 3873 case OMPD_teams_distribute_simd: { 3874 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3875 QualType KmpInt32PtrTy = 3876 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3877 Sema::CapturedParamNameType Params[] = { 3878 std::make_pair(".global_tid.", KmpInt32PtrTy), 3879 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3880 std::make_pair(StringRef(), QualType()) // __context with shared vars 3881 }; 3882 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3883 Params); 3884 break; 3885 } 3886 case OMPD_target_teams: 3887 case OMPD_target_parallel: 3888 case OMPD_target_parallel_for: 3889 case OMPD_target_parallel_for_simd: 3890 case OMPD_target_teams_distribute: 3891 case OMPD_target_teams_distribute_simd: { 3892 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3893 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3894 QualType KmpInt32PtrTy = 3895 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3896 QualType Args[] = {VoidPtrTy}; 3897 FunctionProtoType::ExtProtoInfo EPI; 3898 EPI.Variadic = true; 3899 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3900 Sema::CapturedParamNameType Params[] = { 3901 std::make_pair(".global_tid.", KmpInt32Ty), 3902 std::make_pair(".part_id.", KmpInt32PtrTy), 3903 std::make_pair(".privates.", VoidPtrTy), 3904 std::make_pair( 3905 ".copy_fn.", 3906 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3907 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3908 std::make_pair(StringRef(), QualType()) // __context with shared vars 3909 }; 3910 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3911 Params, /*OpenMPCaptureLevel=*/0); 3912 // Mark this captured region as inlined, because we don't use outlined 3913 // function directly. 3914 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3915 AlwaysInlineAttr::CreateImplicit( 3916 Context, {}, AttributeCommonInfo::AS_Keyword, 3917 AlwaysInlineAttr::Keyword_forceinline)); 3918 Sema::CapturedParamNameType ParamsTarget[] = { 3919 std::make_pair(StringRef(), QualType()) // __context with shared vars 3920 }; 3921 // Start a captured region for 'target' with no implicit parameters. 3922 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3923 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3924 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3925 std::make_pair(".global_tid.", KmpInt32PtrTy), 3926 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3927 std::make_pair(StringRef(), QualType()) // __context with shared vars 3928 }; 3929 // Start a captured region for 'teams' or 'parallel'. Both regions have 3930 // the same implicit parameters. 3931 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3932 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3933 break; 3934 } 3935 case OMPD_target: 3936 case OMPD_target_simd: { 3937 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3938 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3939 QualType KmpInt32PtrTy = 3940 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3941 QualType Args[] = {VoidPtrTy}; 3942 FunctionProtoType::ExtProtoInfo EPI; 3943 EPI.Variadic = true; 3944 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3945 Sema::CapturedParamNameType Params[] = { 3946 std::make_pair(".global_tid.", KmpInt32Ty), 3947 std::make_pair(".part_id.", KmpInt32PtrTy), 3948 std::make_pair(".privates.", VoidPtrTy), 3949 std::make_pair( 3950 ".copy_fn.", 3951 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3952 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3953 std::make_pair(StringRef(), QualType()) // __context with shared vars 3954 }; 3955 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3956 Params, /*OpenMPCaptureLevel=*/0); 3957 // Mark this captured region as inlined, because we don't use outlined 3958 // function directly. 3959 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3960 AlwaysInlineAttr::CreateImplicit( 3961 Context, {}, AttributeCommonInfo::AS_Keyword, 3962 AlwaysInlineAttr::Keyword_forceinline)); 3963 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3964 std::make_pair(StringRef(), QualType()), 3965 /*OpenMPCaptureLevel=*/1); 3966 break; 3967 } 3968 case OMPD_atomic: 3969 case OMPD_critical: 3970 case OMPD_section: 3971 case OMPD_master: 3972 case OMPD_masked: 3973 case OMPD_tile: 3974 break; 3975 case OMPD_simd: 3976 case OMPD_for: 3977 case OMPD_for_simd: 3978 case OMPD_sections: 3979 case OMPD_single: 3980 case OMPD_taskgroup: 3981 case OMPD_distribute: 3982 case OMPD_distribute_simd: 3983 case OMPD_ordered: 3984 case OMPD_target_data: 3985 case OMPD_dispatch: { 3986 Sema::CapturedParamNameType Params[] = { 3987 std::make_pair(StringRef(), QualType()) // __context with shared vars 3988 }; 3989 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3990 Params); 3991 break; 3992 } 3993 case OMPD_task: { 3994 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3995 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3996 QualType KmpInt32PtrTy = 3997 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3998 QualType Args[] = {VoidPtrTy}; 3999 FunctionProtoType::ExtProtoInfo EPI; 4000 EPI.Variadic = true; 4001 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4002 Sema::CapturedParamNameType Params[] = { 4003 std::make_pair(".global_tid.", KmpInt32Ty), 4004 std::make_pair(".part_id.", KmpInt32PtrTy), 4005 std::make_pair(".privates.", VoidPtrTy), 4006 std::make_pair( 4007 ".copy_fn.", 4008 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4009 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4010 std::make_pair(StringRef(), QualType()) // __context with shared vars 4011 }; 4012 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4013 Params); 4014 // Mark this captured region as inlined, because we don't use outlined 4015 // function directly. 4016 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4017 AlwaysInlineAttr::CreateImplicit( 4018 Context, {}, AttributeCommonInfo::AS_Keyword, 4019 AlwaysInlineAttr::Keyword_forceinline)); 4020 break; 4021 } 4022 case OMPD_taskloop: 4023 case OMPD_taskloop_simd: 4024 case OMPD_master_taskloop: 4025 case OMPD_master_taskloop_simd: { 4026 QualType KmpInt32Ty = 4027 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4028 .withConst(); 4029 QualType KmpUInt64Ty = 4030 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4031 .withConst(); 4032 QualType KmpInt64Ty = 4033 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4034 .withConst(); 4035 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4036 QualType KmpInt32PtrTy = 4037 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4038 QualType Args[] = {VoidPtrTy}; 4039 FunctionProtoType::ExtProtoInfo EPI; 4040 EPI.Variadic = true; 4041 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4042 Sema::CapturedParamNameType Params[] = { 4043 std::make_pair(".global_tid.", KmpInt32Ty), 4044 std::make_pair(".part_id.", KmpInt32PtrTy), 4045 std::make_pair(".privates.", VoidPtrTy), 4046 std::make_pair( 4047 ".copy_fn.", 4048 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4049 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4050 std::make_pair(".lb.", KmpUInt64Ty), 4051 std::make_pair(".ub.", KmpUInt64Ty), 4052 std::make_pair(".st.", KmpInt64Ty), 4053 std::make_pair(".liter.", KmpInt32Ty), 4054 std::make_pair(".reductions.", VoidPtrTy), 4055 std::make_pair(StringRef(), QualType()) // __context with shared vars 4056 }; 4057 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4058 Params); 4059 // Mark this captured region as inlined, because we don't use outlined 4060 // function directly. 4061 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4062 AlwaysInlineAttr::CreateImplicit( 4063 Context, {}, AttributeCommonInfo::AS_Keyword, 4064 AlwaysInlineAttr::Keyword_forceinline)); 4065 break; 4066 } 4067 case OMPD_parallel_master_taskloop: 4068 case OMPD_parallel_master_taskloop_simd: { 4069 QualType KmpInt32Ty = 4070 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4071 .withConst(); 4072 QualType KmpUInt64Ty = 4073 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4074 .withConst(); 4075 QualType KmpInt64Ty = 4076 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4077 .withConst(); 4078 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4079 QualType KmpInt32PtrTy = 4080 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4081 Sema::CapturedParamNameType ParamsParallel[] = { 4082 std::make_pair(".global_tid.", KmpInt32PtrTy), 4083 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4084 std::make_pair(StringRef(), QualType()) // __context with shared vars 4085 }; 4086 // Start a captured region for 'parallel'. 4087 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4088 ParamsParallel, /*OpenMPCaptureLevel=*/0); 4089 QualType Args[] = {VoidPtrTy}; 4090 FunctionProtoType::ExtProtoInfo EPI; 4091 EPI.Variadic = true; 4092 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4093 Sema::CapturedParamNameType Params[] = { 4094 std::make_pair(".global_tid.", KmpInt32Ty), 4095 std::make_pair(".part_id.", KmpInt32PtrTy), 4096 std::make_pair(".privates.", VoidPtrTy), 4097 std::make_pair( 4098 ".copy_fn.", 4099 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4100 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4101 std::make_pair(".lb.", KmpUInt64Ty), 4102 std::make_pair(".ub.", KmpUInt64Ty), 4103 std::make_pair(".st.", KmpInt64Ty), 4104 std::make_pair(".liter.", KmpInt32Ty), 4105 std::make_pair(".reductions.", VoidPtrTy), 4106 std::make_pair(StringRef(), QualType()) // __context with shared vars 4107 }; 4108 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4109 Params, /*OpenMPCaptureLevel=*/1); 4110 // Mark this captured region as inlined, because we don't use outlined 4111 // function directly. 4112 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4113 AlwaysInlineAttr::CreateImplicit( 4114 Context, {}, AttributeCommonInfo::AS_Keyword, 4115 AlwaysInlineAttr::Keyword_forceinline)); 4116 break; 4117 } 4118 case OMPD_distribute_parallel_for_simd: 4119 case OMPD_distribute_parallel_for: { 4120 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4121 QualType KmpInt32PtrTy = 4122 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4123 Sema::CapturedParamNameType Params[] = { 4124 std::make_pair(".global_tid.", KmpInt32PtrTy), 4125 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4126 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4127 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4128 std::make_pair(StringRef(), QualType()) // __context with shared vars 4129 }; 4130 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4131 Params); 4132 break; 4133 } 4134 case OMPD_target_teams_distribute_parallel_for: 4135 case OMPD_target_teams_distribute_parallel_for_simd: { 4136 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4137 QualType KmpInt32PtrTy = 4138 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4139 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4140 4141 QualType Args[] = {VoidPtrTy}; 4142 FunctionProtoType::ExtProtoInfo EPI; 4143 EPI.Variadic = true; 4144 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4145 Sema::CapturedParamNameType Params[] = { 4146 std::make_pair(".global_tid.", KmpInt32Ty), 4147 std::make_pair(".part_id.", KmpInt32PtrTy), 4148 std::make_pair(".privates.", VoidPtrTy), 4149 std::make_pair( 4150 ".copy_fn.", 4151 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4152 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4153 std::make_pair(StringRef(), QualType()) // __context with shared vars 4154 }; 4155 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4156 Params, /*OpenMPCaptureLevel=*/0); 4157 // Mark this captured region as inlined, because we don't use outlined 4158 // function directly. 4159 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4160 AlwaysInlineAttr::CreateImplicit( 4161 Context, {}, AttributeCommonInfo::AS_Keyword, 4162 AlwaysInlineAttr::Keyword_forceinline)); 4163 Sema::CapturedParamNameType ParamsTarget[] = { 4164 std::make_pair(StringRef(), QualType()) // __context with shared vars 4165 }; 4166 // Start a captured region for 'target' with no implicit parameters. 4167 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4168 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4169 4170 Sema::CapturedParamNameType ParamsTeams[] = { 4171 std::make_pair(".global_tid.", KmpInt32PtrTy), 4172 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4173 std::make_pair(StringRef(), QualType()) // __context with shared vars 4174 }; 4175 // Start a captured region for 'target' with no implicit parameters. 4176 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4177 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4178 4179 Sema::CapturedParamNameType ParamsParallel[] = { 4180 std::make_pair(".global_tid.", KmpInt32PtrTy), 4181 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4182 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4183 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4184 std::make_pair(StringRef(), QualType()) // __context with shared vars 4185 }; 4186 // Start a captured region for 'teams' or 'parallel'. Both regions have 4187 // the same implicit parameters. 4188 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4189 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4190 break; 4191 } 4192 4193 case OMPD_teams_distribute_parallel_for: 4194 case OMPD_teams_distribute_parallel_for_simd: { 4195 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4196 QualType KmpInt32PtrTy = 4197 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4198 4199 Sema::CapturedParamNameType ParamsTeams[] = { 4200 std::make_pair(".global_tid.", KmpInt32PtrTy), 4201 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4202 std::make_pair(StringRef(), QualType()) // __context with shared vars 4203 }; 4204 // Start a captured region for 'target' with no implicit parameters. 4205 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4206 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4207 4208 Sema::CapturedParamNameType ParamsParallel[] = { 4209 std::make_pair(".global_tid.", KmpInt32PtrTy), 4210 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4211 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4212 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4213 std::make_pair(StringRef(), QualType()) // __context with shared vars 4214 }; 4215 // Start a captured region for 'teams' or 'parallel'. Both regions have 4216 // the same implicit parameters. 4217 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4218 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4219 break; 4220 } 4221 case OMPD_target_update: 4222 case OMPD_target_enter_data: 4223 case OMPD_target_exit_data: { 4224 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4225 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4226 QualType KmpInt32PtrTy = 4227 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4228 QualType Args[] = {VoidPtrTy}; 4229 FunctionProtoType::ExtProtoInfo EPI; 4230 EPI.Variadic = true; 4231 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4232 Sema::CapturedParamNameType Params[] = { 4233 std::make_pair(".global_tid.", KmpInt32Ty), 4234 std::make_pair(".part_id.", KmpInt32PtrTy), 4235 std::make_pair(".privates.", VoidPtrTy), 4236 std::make_pair( 4237 ".copy_fn.", 4238 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4239 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4240 std::make_pair(StringRef(), QualType()) // __context with shared vars 4241 }; 4242 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4243 Params); 4244 // Mark this captured region as inlined, because we don't use outlined 4245 // function directly. 4246 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4247 AlwaysInlineAttr::CreateImplicit( 4248 Context, {}, AttributeCommonInfo::AS_Keyword, 4249 AlwaysInlineAttr::Keyword_forceinline)); 4250 break; 4251 } 4252 case OMPD_threadprivate: 4253 case OMPD_allocate: 4254 case OMPD_taskyield: 4255 case OMPD_barrier: 4256 case OMPD_taskwait: 4257 case OMPD_cancellation_point: 4258 case OMPD_cancel: 4259 case OMPD_flush: 4260 case OMPD_depobj: 4261 case OMPD_scan: 4262 case OMPD_declare_reduction: 4263 case OMPD_declare_mapper: 4264 case OMPD_declare_simd: 4265 case OMPD_declare_target: 4266 case OMPD_end_declare_target: 4267 case OMPD_requires: 4268 case OMPD_declare_variant: 4269 case OMPD_begin_declare_variant: 4270 case OMPD_end_declare_variant: 4271 llvm_unreachable("OpenMP Directive is not allowed"); 4272 case OMPD_unknown: 4273 default: 4274 llvm_unreachable("Unknown OpenMP directive"); 4275 } 4276 DSAStack->setContext(CurContext); 4277 } 4278 4279 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4280 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4281 } 4282 4283 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4284 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4285 getOpenMPCaptureRegions(CaptureRegions, DKind); 4286 return CaptureRegions.size(); 4287 } 4288 4289 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4290 Expr *CaptureExpr, bool WithInit, 4291 bool AsExpression) { 4292 assert(CaptureExpr); 4293 ASTContext &C = S.getASTContext(); 4294 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4295 QualType Ty = Init->getType(); 4296 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4297 if (S.getLangOpts().CPlusPlus) { 4298 Ty = C.getLValueReferenceType(Ty); 4299 } else { 4300 Ty = C.getPointerType(Ty); 4301 ExprResult Res = 4302 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4303 if (!Res.isUsable()) 4304 return nullptr; 4305 Init = Res.get(); 4306 } 4307 WithInit = true; 4308 } 4309 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4310 CaptureExpr->getBeginLoc()); 4311 if (!WithInit) 4312 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4313 S.CurContext->addHiddenDecl(CED); 4314 Sema::TentativeAnalysisScope Trap(S); 4315 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4316 return CED; 4317 } 4318 4319 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4320 bool WithInit) { 4321 OMPCapturedExprDecl *CD; 4322 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4323 CD = cast<OMPCapturedExprDecl>(VD); 4324 else 4325 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4326 /*AsExpression=*/false); 4327 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4328 CaptureExpr->getExprLoc()); 4329 } 4330 4331 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4332 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4333 if (!Ref) { 4334 OMPCapturedExprDecl *CD = buildCaptureDecl( 4335 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4336 /*WithInit=*/true, /*AsExpression=*/true); 4337 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4338 CaptureExpr->getExprLoc()); 4339 } 4340 ExprResult Res = Ref; 4341 if (!S.getLangOpts().CPlusPlus && 4342 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4343 Ref->getType()->isPointerType()) { 4344 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4345 if (!Res.isUsable()) 4346 return ExprError(); 4347 } 4348 return S.DefaultLvalueConversion(Res.get()); 4349 } 4350 4351 namespace { 4352 // OpenMP directives parsed in this section are represented as a 4353 // CapturedStatement with an associated statement. If a syntax error 4354 // is detected during the parsing of the associated statement, the 4355 // compiler must abort processing and close the CapturedStatement. 4356 // 4357 // Combined directives such as 'target parallel' have more than one 4358 // nested CapturedStatements. This RAII ensures that we unwind out 4359 // of all the nested CapturedStatements when an error is found. 4360 class CaptureRegionUnwinderRAII { 4361 private: 4362 Sema &S; 4363 bool &ErrorFound; 4364 OpenMPDirectiveKind DKind = OMPD_unknown; 4365 4366 public: 4367 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4368 OpenMPDirectiveKind DKind) 4369 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4370 ~CaptureRegionUnwinderRAII() { 4371 if (ErrorFound) { 4372 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4373 while (--ThisCaptureLevel >= 0) 4374 S.ActOnCapturedRegionError(); 4375 } 4376 } 4377 }; 4378 } // namespace 4379 4380 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4381 // Capture variables captured by reference in lambdas for target-based 4382 // directives. 4383 if (!CurContext->isDependentContext() && 4384 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4385 isOpenMPTargetDataManagementDirective( 4386 DSAStack->getCurrentDirective()))) { 4387 QualType Type = V->getType(); 4388 if (const auto *RD = Type.getCanonicalType() 4389 .getNonReferenceType() 4390 ->getAsCXXRecordDecl()) { 4391 bool SavedForceCaptureByReferenceInTargetExecutable = 4392 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4393 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4394 /*V=*/true); 4395 if (RD->isLambda()) { 4396 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4397 FieldDecl *ThisCapture; 4398 RD->getCaptureFields(Captures, ThisCapture); 4399 for (const LambdaCapture &LC : RD->captures()) { 4400 if (LC.getCaptureKind() == LCK_ByRef) { 4401 VarDecl *VD = LC.getCapturedVar(); 4402 DeclContext *VDC = VD->getDeclContext(); 4403 if (!VDC->Encloses(CurContext)) 4404 continue; 4405 MarkVariableReferenced(LC.getLocation(), VD); 4406 } else if (LC.getCaptureKind() == LCK_This) { 4407 QualType ThisTy = getCurrentThisType(); 4408 if (!ThisTy.isNull() && 4409 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4410 CheckCXXThisCapture(LC.getLocation()); 4411 } 4412 } 4413 } 4414 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4415 SavedForceCaptureByReferenceInTargetExecutable); 4416 } 4417 } 4418 } 4419 4420 static bool checkOrderedOrderSpecified(Sema &S, 4421 const ArrayRef<OMPClause *> Clauses) { 4422 const OMPOrderedClause *Ordered = nullptr; 4423 const OMPOrderClause *Order = nullptr; 4424 4425 for (const OMPClause *Clause : Clauses) { 4426 if (Clause->getClauseKind() == OMPC_ordered) 4427 Ordered = cast<OMPOrderedClause>(Clause); 4428 else if (Clause->getClauseKind() == OMPC_order) { 4429 Order = cast<OMPOrderClause>(Clause); 4430 if (Order->getKind() != OMPC_ORDER_concurrent) 4431 Order = nullptr; 4432 } 4433 if (Ordered && Order) 4434 break; 4435 } 4436 4437 if (Ordered && Order) { 4438 S.Diag(Order->getKindKwLoc(), 4439 diag::err_omp_simple_clause_incompatible_with_ordered) 4440 << getOpenMPClauseName(OMPC_order) 4441 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4442 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4443 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4444 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4445 return true; 4446 } 4447 return false; 4448 } 4449 4450 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4451 ArrayRef<OMPClause *> Clauses) { 4452 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4453 DSAStack->getCurrentDirective() == OMPD_critical || 4454 DSAStack->getCurrentDirective() == OMPD_section || 4455 DSAStack->getCurrentDirective() == OMPD_master || 4456 DSAStack->getCurrentDirective() == OMPD_masked) 4457 return S; 4458 4459 bool ErrorFound = false; 4460 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4461 *this, ErrorFound, DSAStack->getCurrentDirective()); 4462 if (!S.isUsable()) { 4463 ErrorFound = true; 4464 return StmtError(); 4465 } 4466 4467 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4468 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4469 OMPOrderedClause *OC = nullptr; 4470 OMPScheduleClause *SC = nullptr; 4471 SmallVector<const OMPLinearClause *, 4> LCs; 4472 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4473 // This is required for proper codegen. 4474 for (OMPClause *Clause : Clauses) { 4475 if (!LangOpts.OpenMPSimd && 4476 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4477 Clause->getClauseKind() == OMPC_in_reduction) { 4478 // Capture taskgroup task_reduction descriptors inside the tasking regions 4479 // with the corresponding in_reduction items. 4480 auto *IRC = cast<OMPInReductionClause>(Clause); 4481 for (Expr *E : IRC->taskgroup_descriptors()) 4482 if (E) 4483 MarkDeclarationsReferencedInExpr(E); 4484 } 4485 if (isOpenMPPrivate(Clause->getClauseKind()) || 4486 Clause->getClauseKind() == OMPC_copyprivate || 4487 (getLangOpts().OpenMPUseTLS && 4488 getASTContext().getTargetInfo().isTLSSupported() && 4489 Clause->getClauseKind() == OMPC_copyin)) { 4490 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4491 // Mark all variables in private list clauses as used in inner region. 4492 for (Stmt *VarRef : Clause->children()) { 4493 if (auto *E = cast_or_null<Expr>(VarRef)) { 4494 MarkDeclarationsReferencedInExpr(E); 4495 } 4496 } 4497 DSAStack->setForceVarCapturing(/*V=*/false); 4498 } else if (isOpenMPLoopTransformationDirective( 4499 DSAStack->getCurrentDirective())) { 4500 assert(CaptureRegions.empty() && 4501 "No captured regions in loop transformation directives."); 4502 } else if (CaptureRegions.size() > 1 || 4503 CaptureRegions.back() != OMPD_unknown) { 4504 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4505 PICs.push_back(C); 4506 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4507 if (Expr *E = C->getPostUpdateExpr()) 4508 MarkDeclarationsReferencedInExpr(E); 4509 } 4510 } 4511 if (Clause->getClauseKind() == OMPC_schedule) 4512 SC = cast<OMPScheduleClause>(Clause); 4513 else if (Clause->getClauseKind() == OMPC_ordered) 4514 OC = cast<OMPOrderedClause>(Clause); 4515 else if (Clause->getClauseKind() == OMPC_linear) 4516 LCs.push_back(cast<OMPLinearClause>(Clause)); 4517 } 4518 // Capture allocator expressions if used. 4519 for (Expr *E : DSAStack->getInnerAllocators()) 4520 MarkDeclarationsReferencedInExpr(E); 4521 // OpenMP, 2.7.1 Loop Construct, Restrictions 4522 // The nonmonotonic modifier cannot be specified if an ordered clause is 4523 // specified. 4524 if (SC && 4525 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4526 SC->getSecondScheduleModifier() == 4527 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4528 OC) { 4529 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4530 ? SC->getFirstScheduleModifierLoc() 4531 : SC->getSecondScheduleModifierLoc(), 4532 diag::err_omp_simple_clause_incompatible_with_ordered) 4533 << getOpenMPClauseName(OMPC_schedule) 4534 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4535 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4536 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4537 ErrorFound = true; 4538 } 4539 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4540 // If an order(concurrent) clause is present, an ordered clause may not appear 4541 // on the same directive. 4542 if (checkOrderedOrderSpecified(*this, Clauses)) 4543 ErrorFound = true; 4544 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4545 for (const OMPLinearClause *C : LCs) { 4546 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4547 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4548 } 4549 ErrorFound = true; 4550 } 4551 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4552 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4553 OC->getNumForLoops()) { 4554 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4555 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4556 ErrorFound = true; 4557 } 4558 if (ErrorFound) { 4559 return StmtError(); 4560 } 4561 StmtResult SR = S; 4562 unsigned CompletedRegions = 0; 4563 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4564 // Mark all variables in private list clauses as used in inner region. 4565 // Required for proper codegen of combined directives. 4566 // TODO: add processing for other clauses. 4567 if (ThisCaptureRegion != OMPD_unknown) { 4568 for (const clang::OMPClauseWithPreInit *C : PICs) { 4569 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4570 // Find the particular capture region for the clause if the 4571 // directive is a combined one with multiple capture regions. 4572 // If the directive is not a combined one, the capture region 4573 // associated with the clause is OMPD_unknown and is generated 4574 // only once. 4575 if (CaptureRegion == ThisCaptureRegion || 4576 CaptureRegion == OMPD_unknown) { 4577 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4578 for (Decl *D : DS->decls()) 4579 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4580 } 4581 } 4582 } 4583 } 4584 if (ThisCaptureRegion == OMPD_target) { 4585 // Capture allocator traits in the target region. They are used implicitly 4586 // and, thus, are not captured by default. 4587 for (OMPClause *C : Clauses) { 4588 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4589 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4590 ++I) { 4591 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4592 if (Expr *E = D.AllocatorTraits) 4593 MarkDeclarationsReferencedInExpr(E); 4594 } 4595 continue; 4596 } 4597 } 4598 } 4599 if (ThisCaptureRegion == OMPD_parallel) { 4600 // Capture temp arrays for inscan reductions. 4601 for (OMPClause *C : Clauses) { 4602 if (auto *RC = dyn_cast<OMPReductionClause>(C)) { 4603 if (RC->getModifier() != OMPC_REDUCTION_inscan) 4604 continue; 4605 for (Expr *E : RC->copy_array_temps()) 4606 MarkDeclarationsReferencedInExpr(E); 4607 } 4608 } 4609 } 4610 if (++CompletedRegions == CaptureRegions.size()) 4611 DSAStack->setBodyComplete(); 4612 SR = ActOnCapturedRegionEnd(SR.get()); 4613 } 4614 return SR; 4615 } 4616 4617 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4618 OpenMPDirectiveKind CancelRegion, 4619 SourceLocation StartLoc) { 4620 // CancelRegion is only needed for cancel and cancellation_point. 4621 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4622 return false; 4623 4624 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4625 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4626 return false; 4627 4628 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4629 << getOpenMPDirectiveName(CancelRegion); 4630 return true; 4631 } 4632 4633 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4634 OpenMPDirectiveKind CurrentRegion, 4635 const DeclarationNameInfo &CurrentName, 4636 OpenMPDirectiveKind CancelRegion, 4637 SourceLocation StartLoc) { 4638 if (Stack->getCurScope()) { 4639 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4640 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4641 bool NestingProhibited = false; 4642 bool CloseNesting = true; 4643 bool OrphanSeen = false; 4644 enum { 4645 NoRecommend, 4646 ShouldBeInParallelRegion, 4647 ShouldBeInOrderedRegion, 4648 ShouldBeInTargetRegion, 4649 ShouldBeInTeamsRegion, 4650 ShouldBeInLoopSimdRegion, 4651 } Recommend = NoRecommend; 4652 if (isOpenMPSimdDirective(ParentRegion) && 4653 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4654 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4655 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4656 CurrentRegion != OMPD_scan))) { 4657 // OpenMP [2.16, Nesting of Regions] 4658 // OpenMP constructs may not be nested inside a simd region. 4659 // OpenMP [2.8.1,simd Construct, Restrictions] 4660 // An ordered construct with the simd clause is the only OpenMP 4661 // construct that can appear in the simd region. 4662 // Allowing a SIMD construct nested in another SIMD construct is an 4663 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4664 // message. 4665 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4666 // The only OpenMP constructs that can be encountered during execution of 4667 // a simd region are the atomic construct, the loop construct, the simd 4668 // construct and the ordered construct with the simd clause. 4669 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4670 ? diag::err_omp_prohibited_region_simd 4671 : diag::warn_omp_nesting_simd) 4672 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4673 return CurrentRegion != OMPD_simd; 4674 } 4675 if (ParentRegion == OMPD_atomic) { 4676 // OpenMP [2.16, Nesting of Regions] 4677 // OpenMP constructs may not be nested inside an atomic region. 4678 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4679 return true; 4680 } 4681 if (CurrentRegion == OMPD_section) { 4682 // OpenMP [2.7.2, sections Construct, Restrictions] 4683 // Orphaned section directives are prohibited. That is, the section 4684 // directives must appear within the sections construct and must not be 4685 // encountered elsewhere in the sections region. 4686 if (ParentRegion != OMPD_sections && 4687 ParentRegion != OMPD_parallel_sections) { 4688 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4689 << (ParentRegion != OMPD_unknown) 4690 << getOpenMPDirectiveName(ParentRegion); 4691 return true; 4692 } 4693 return false; 4694 } 4695 // Allow some constructs (except teams and cancellation constructs) to be 4696 // orphaned (they could be used in functions, called from OpenMP regions 4697 // with the required preconditions). 4698 if (ParentRegion == OMPD_unknown && 4699 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4700 CurrentRegion != OMPD_cancellation_point && 4701 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4702 return false; 4703 if (CurrentRegion == OMPD_cancellation_point || 4704 CurrentRegion == OMPD_cancel) { 4705 // OpenMP [2.16, Nesting of Regions] 4706 // A cancellation point construct for which construct-type-clause is 4707 // taskgroup must be nested inside a task construct. A cancellation 4708 // point construct for which construct-type-clause is not taskgroup must 4709 // be closely nested inside an OpenMP construct that matches the type 4710 // specified in construct-type-clause. 4711 // A cancel construct for which construct-type-clause is taskgroup must be 4712 // nested inside a task construct. A cancel construct for which 4713 // construct-type-clause is not taskgroup must be closely nested inside an 4714 // OpenMP construct that matches the type specified in 4715 // construct-type-clause. 4716 NestingProhibited = 4717 !((CancelRegion == OMPD_parallel && 4718 (ParentRegion == OMPD_parallel || 4719 ParentRegion == OMPD_target_parallel)) || 4720 (CancelRegion == OMPD_for && 4721 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4722 ParentRegion == OMPD_target_parallel_for || 4723 ParentRegion == OMPD_distribute_parallel_for || 4724 ParentRegion == OMPD_teams_distribute_parallel_for || 4725 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4726 (CancelRegion == OMPD_taskgroup && 4727 (ParentRegion == OMPD_task || 4728 (SemaRef.getLangOpts().OpenMP >= 50 && 4729 (ParentRegion == OMPD_taskloop || 4730 ParentRegion == OMPD_master_taskloop || 4731 ParentRegion == OMPD_parallel_master_taskloop)))) || 4732 (CancelRegion == OMPD_sections && 4733 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4734 ParentRegion == OMPD_parallel_sections))); 4735 OrphanSeen = ParentRegion == OMPD_unknown; 4736 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) { 4737 // OpenMP 5.1 [2.22, Nesting of Regions] 4738 // A masked region may not be closely nested inside a worksharing, loop, 4739 // atomic, task, or taskloop region. 4740 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4741 isOpenMPTaskingDirective(ParentRegion); 4742 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4743 // OpenMP [2.16, Nesting of Regions] 4744 // A critical region may not be nested (closely or otherwise) inside a 4745 // critical region with the same name. Note that this restriction is not 4746 // sufficient to prevent deadlock. 4747 SourceLocation PreviousCriticalLoc; 4748 bool DeadLock = Stack->hasDirective( 4749 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4750 const DeclarationNameInfo &DNI, 4751 SourceLocation Loc) { 4752 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4753 PreviousCriticalLoc = Loc; 4754 return true; 4755 } 4756 return false; 4757 }, 4758 false /* skip top directive */); 4759 if (DeadLock) { 4760 SemaRef.Diag(StartLoc, 4761 diag::err_omp_prohibited_region_critical_same_name) 4762 << CurrentName.getName(); 4763 if (PreviousCriticalLoc.isValid()) 4764 SemaRef.Diag(PreviousCriticalLoc, 4765 diag::note_omp_previous_critical_region); 4766 return true; 4767 } 4768 } else if (CurrentRegion == OMPD_barrier) { 4769 // OpenMP 5.1 [2.22, Nesting of Regions] 4770 // A barrier region may not be closely nested inside a worksharing, loop, 4771 // task, taskloop, critical, ordered, atomic, or masked region. 4772 NestingProhibited = 4773 isOpenMPWorksharingDirective(ParentRegion) || 4774 isOpenMPTaskingDirective(ParentRegion) || 4775 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4776 ParentRegion == OMPD_parallel_master || 4777 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4778 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4779 !isOpenMPParallelDirective(CurrentRegion) && 4780 !isOpenMPTeamsDirective(CurrentRegion)) { 4781 // OpenMP 5.1 [2.22, Nesting of Regions] 4782 // A loop region that binds to a parallel region or a worksharing region 4783 // may not be closely nested inside a worksharing, loop, task, taskloop, 4784 // critical, ordered, atomic, or masked region. 4785 NestingProhibited = 4786 isOpenMPWorksharingDirective(ParentRegion) || 4787 isOpenMPTaskingDirective(ParentRegion) || 4788 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4789 ParentRegion == OMPD_parallel_master || 4790 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4791 Recommend = ShouldBeInParallelRegion; 4792 } else if (CurrentRegion == OMPD_ordered) { 4793 // OpenMP [2.16, Nesting of Regions] 4794 // An ordered region may not be closely nested inside a critical, 4795 // atomic, or explicit task region. 4796 // An ordered region must be closely nested inside a loop region (or 4797 // parallel loop region) with an ordered clause. 4798 // OpenMP [2.8.1,simd Construct, Restrictions] 4799 // An ordered construct with the simd clause is the only OpenMP construct 4800 // that can appear in the simd region. 4801 NestingProhibited = ParentRegion == OMPD_critical || 4802 isOpenMPTaskingDirective(ParentRegion) || 4803 !(isOpenMPSimdDirective(ParentRegion) || 4804 Stack->isParentOrderedRegion()); 4805 Recommend = ShouldBeInOrderedRegion; 4806 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4807 // OpenMP [2.16, Nesting of Regions] 4808 // If specified, a teams construct must be contained within a target 4809 // construct. 4810 NestingProhibited = 4811 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4812 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4813 ParentRegion != OMPD_target); 4814 OrphanSeen = ParentRegion == OMPD_unknown; 4815 Recommend = ShouldBeInTargetRegion; 4816 } else if (CurrentRegion == OMPD_scan) { 4817 // OpenMP [2.16, Nesting of Regions] 4818 // If specified, a teams construct must be contained within a target 4819 // construct. 4820 NestingProhibited = 4821 SemaRef.LangOpts.OpenMP < 50 || 4822 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4823 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4824 ParentRegion != OMPD_parallel_for_simd); 4825 OrphanSeen = ParentRegion == OMPD_unknown; 4826 Recommend = ShouldBeInLoopSimdRegion; 4827 } 4828 if (!NestingProhibited && 4829 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4830 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4831 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4832 // OpenMP [2.16, Nesting of Regions] 4833 // distribute, parallel, parallel sections, parallel workshare, and the 4834 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4835 // constructs that can be closely nested in the teams region. 4836 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4837 !isOpenMPDistributeDirective(CurrentRegion); 4838 Recommend = ShouldBeInParallelRegion; 4839 } 4840 if (!NestingProhibited && 4841 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4842 // OpenMP 4.5 [2.17 Nesting of Regions] 4843 // The region associated with the distribute construct must be strictly 4844 // nested inside a teams region 4845 NestingProhibited = 4846 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4847 Recommend = ShouldBeInTeamsRegion; 4848 } 4849 if (!NestingProhibited && 4850 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4851 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4852 // OpenMP 4.5 [2.17 Nesting of Regions] 4853 // If a target, target update, target data, target enter data, or 4854 // target exit data construct is encountered during execution of a 4855 // target region, the behavior is unspecified. 4856 NestingProhibited = Stack->hasDirective( 4857 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4858 SourceLocation) { 4859 if (isOpenMPTargetExecutionDirective(K)) { 4860 OffendingRegion = K; 4861 return true; 4862 } 4863 return false; 4864 }, 4865 false /* don't skip top directive */); 4866 CloseNesting = false; 4867 } 4868 if (NestingProhibited) { 4869 if (OrphanSeen) { 4870 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4871 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4872 } else { 4873 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4874 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4875 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4876 } 4877 return true; 4878 } 4879 } 4880 return false; 4881 } 4882 4883 struct Kind2Unsigned { 4884 using argument_type = OpenMPDirectiveKind; 4885 unsigned operator()(argument_type DK) { return unsigned(DK); } 4886 }; 4887 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4888 ArrayRef<OMPClause *> Clauses, 4889 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4890 bool ErrorFound = false; 4891 unsigned NamedModifiersNumber = 0; 4892 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4893 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4894 SmallVector<SourceLocation, 4> NameModifierLoc; 4895 for (const OMPClause *C : Clauses) { 4896 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4897 // At most one if clause without a directive-name-modifier can appear on 4898 // the directive. 4899 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4900 if (FoundNameModifiers[CurNM]) { 4901 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4902 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4903 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4904 ErrorFound = true; 4905 } else if (CurNM != OMPD_unknown) { 4906 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4907 ++NamedModifiersNumber; 4908 } 4909 FoundNameModifiers[CurNM] = IC; 4910 if (CurNM == OMPD_unknown) 4911 continue; 4912 // Check if the specified name modifier is allowed for the current 4913 // directive. 4914 // At most one if clause with the particular directive-name-modifier can 4915 // appear on the directive. 4916 bool MatchFound = false; 4917 for (auto NM : AllowedNameModifiers) { 4918 if (CurNM == NM) { 4919 MatchFound = true; 4920 break; 4921 } 4922 } 4923 if (!MatchFound) { 4924 S.Diag(IC->getNameModifierLoc(), 4925 diag::err_omp_wrong_if_directive_name_modifier) 4926 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4927 ErrorFound = true; 4928 } 4929 } 4930 } 4931 // If any if clause on the directive includes a directive-name-modifier then 4932 // all if clauses on the directive must include a directive-name-modifier. 4933 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4934 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4935 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4936 diag::err_omp_no_more_if_clause); 4937 } else { 4938 std::string Values; 4939 std::string Sep(", "); 4940 unsigned AllowedCnt = 0; 4941 unsigned TotalAllowedNum = 4942 AllowedNameModifiers.size() - NamedModifiersNumber; 4943 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4944 ++Cnt) { 4945 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4946 if (!FoundNameModifiers[NM]) { 4947 Values += "'"; 4948 Values += getOpenMPDirectiveName(NM); 4949 Values += "'"; 4950 if (AllowedCnt + 2 == TotalAllowedNum) 4951 Values += " or "; 4952 else if (AllowedCnt + 1 != TotalAllowedNum) 4953 Values += Sep; 4954 ++AllowedCnt; 4955 } 4956 } 4957 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 4958 diag::err_omp_unnamed_if_clause) 4959 << (TotalAllowedNum > 1) << Values; 4960 } 4961 for (SourceLocation Loc : NameModifierLoc) { 4962 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 4963 } 4964 ErrorFound = true; 4965 } 4966 return ErrorFound; 4967 } 4968 4969 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 4970 SourceLocation &ELoc, 4971 SourceRange &ERange, 4972 bool AllowArraySection) { 4973 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 4974 RefExpr->containsUnexpandedParameterPack()) 4975 return std::make_pair(nullptr, true); 4976 4977 // OpenMP [3.1, C/C++] 4978 // A list item is a variable name. 4979 // OpenMP [2.9.3.3, Restrictions, p.1] 4980 // A variable that is part of another variable (as an array or 4981 // structure element) cannot appear in a private clause. 4982 RefExpr = RefExpr->IgnoreParens(); 4983 enum { 4984 NoArrayExpr = -1, 4985 ArraySubscript = 0, 4986 OMPArraySection = 1 4987 } IsArrayExpr = NoArrayExpr; 4988 if (AllowArraySection) { 4989 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 4990 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 4991 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4992 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4993 RefExpr = Base; 4994 IsArrayExpr = ArraySubscript; 4995 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 4996 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 4997 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 4998 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 4999 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5000 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5001 RefExpr = Base; 5002 IsArrayExpr = OMPArraySection; 5003 } 5004 } 5005 ELoc = RefExpr->getExprLoc(); 5006 ERange = RefExpr->getSourceRange(); 5007 RefExpr = RefExpr->IgnoreParenImpCasts(); 5008 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 5009 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 5010 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 5011 (S.getCurrentThisType().isNull() || !ME || 5012 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 5013 !isa<FieldDecl>(ME->getMemberDecl()))) { 5014 if (IsArrayExpr != NoArrayExpr) { 5015 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 5016 << ERange; 5017 } else { 5018 S.Diag(ELoc, 5019 AllowArraySection 5020 ? diag::err_omp_expected_var_name_member_expr_or_array_item 5021 : diag::err_omp_expected_var_name_member_expr) 5022 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 5023 } 5024 return std::make_pair(nullptr, false); 5025 } 5026 return std::make_pair( 5027 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 5028 } 5029 5030 namespace { 5031 /// Checks if the allocator is used in uses_allocators clause to be allowed in 5032 /// target regions. 5033 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 5034 DSAStackTy *S = nullptr; 5035 5036 public: 5037 bool VisitDeclRefExpr(const DeclRefExpr *E) { 5038 return S->isUsesAllocatorsDecl(E->getDecl()) 5039 .getValueOr( 5040 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 5041 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 5042 } 5043 bool VisitStmt(const Stmt *S) { 5044 for (const Stmt *Child : S->children()) { 5045 if (Child && Visit(Child)) 5046 return true; 5047 } 5048 return false; 5049 } 5050 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 5051 }; 5052 } // namespace 5053 5054 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 5055 ArrayRef<OMPClause *> Clauses) { 5056 assert(!S.CurContext->isDependentContext() && 5057 "Expected non-dependent context."); 5058 auto AllocateRange = 5059 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 5060 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 5061 DeclToCopy; 5062 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 5063 return isOpenMPPrivate(C->getClauseKind()); 5064 }); 5065 for (OMPClause *Cl : PrivateRange) { 5066 MutableArrayRef<Expr *>::iterator I, It, Et; 5067 if (Cl->getClauseKind() == OMPC_private) { 5068 auto *PC = cast<OMPPrivateClause>(Cl); 5069 I = PC->private_copies().begin(); 5070 It = PC->varlist_begin(); 5071 Et = PC->varlist_end(); 5072 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 5073 auto *PC = cast<OMPFirstprivateClause>(Cl); 5074 I = PC->private_copies().begin(); 5075 It = PC->varlist_begin(); 5076 Et = PC->varlist_end(); 5077 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 5078 auto *PC = cast<OMPLastprivateClause>(Cl); 5079 I = PC->private_copies().begin(); 5080 It = PC->varlist_begin(); 5081 Et = PC->varlist_end(); 5082 } else if (Cl->getClauseKind() == OMPC_linear) { 5083 auto *PC = cast<OMPLinearClause>(Cl); 5084 I = PC->privates().begin(); 5085 It = PC->varlist_begin(); 5086 Et = PC->varlist_end(); 5087 } else if (Cl->getClauseKind() == OMPC_reduction) { 5088 auto *PC = cast<OMPReductionClause>(Cl); 5089 I = PC->privates().begin(); 5090 It = PC->varlist_begin(); 5091 Et = PC->varlist_end(); 5092 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 5093 auto *PC = cast<OMPTaskReductionClause>(Cl); 5094 I = PC->privates().begin(); 5095 It = PC->varlist_begin(); 5096 Et = PC->varlist_end(); 5097 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 5098 auto *PC = cast<OMPInReductionClause>(Cl); 5099 I = PC->privates().begin(); 5100 It = PC->varlist_begin(); 5101 Et = PC->varlist_end(); 5102 } else { 5103 llvm_unreachable("Expected private clause."); 5104 } 5105 for (Expr *E : llvm::make_range(It, Et)) { 5106 if (!*I) { 5107 ++I; 5108 continue; 5109 } 5110 SourceLocation ELoc; 5111 SourceRange ERange; 5112 Expr *SimpleRefExpr = E; 5113 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 5114 /*AllowArraySection=*/true); 5115 DeclToCopy.try_emplace(Res.first, 5116 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 5117 ++I; 5118 } 5119 } 5120 for (OMPClause *C : AllocateRange) { 5121 auto *AC = cast<OMPAllocateClause>(C); 5122 if (S.getLangOpts().OpenMP >= 50 && 5123 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 5124 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 5125 AC->getAllocator()) { 5126 Expr *Allocator = AC->getAllocator(); 5127 // OpenMP, 2.12.5 target Construct 5128 // Memory allocators that do not appear in a uses_allocators clause cannot 5129 // appear as an allocator in an allocate clause or be used in the target 5130 // region unless a requires directive with the dynamic_allocators clause 5131 // is present in the same compilation unit. 5132 AllocatorChecker Checker(Stack); 5133 if (Checker.Visit(Allocator)) 5134 S.Diag(Allocator->getExprLoc(), 5135 diag::err_omp_allocator_not_in_uses_allocators) 5136 << Allocator->getSourceRange(); 5137 } 5138 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5139 getAllocatorKind(S, Stack, AC->getAllocator()); 5140 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5141 // For task, taskloop or target directives, allocation requests to memory 5142 // allocators with the trait access set to thread result in unspecified 5143 // behavior. 5144 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5145 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5146 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5147 S.Diag(AC->getAllocator()->getExprLoc(), 5148 diag::warn_omp_allocate_thread_on_task_target_directive) 5149 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5150 } 5151 for (Expr *E : AC->varlists()) { 5152 SourceLocation ELoc; 5153 SourceRange ERange; 5154 Expr *SimpleRefExpr = E; 5155 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5156 ValueDecl *VD = Res.first; 5157 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5158 if (!isOpenMPPrivate(Data.CKind)) { 5159 S.Diag(E->getExprLoc(), 5160 diag::err_omp_expected_private_copy_for_allocate); 5161 continue; 5162 } 5163 VarDecl *PrivateVD = DeclToCopy[VD]; 5164 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5165 AllocatorKind, AC->getAllocator())) 5166 continue; 5167 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5168 E->getSourceRange()); 5169 } 5170 } 5171 } 5172 5173 namespace { 5174 /// Rewrite statements and expressions for Sema \p Actions CurContext. 5175 /// 5176 /// Used to wrap already parsed statements/expressions into a new CapturedStmt 5177 /// context. DeclRefExpr used inside the new context are changed to refer to the 5178 /// captured variable instead. 5179 class CaptureVars : public TreeTransform<CaptureVars> { 5180 using BaseTransform = TreeTransform<CaptureVars>; 5181 5182 public: 5183 CaptureVars(Sema &Actions) : BaseTransform(Actions) {} 5184 5185 bool AlwaysRebuild() { return true; } 5186 }; 5187 } // namespace 5188 5189 static VarDecl *precomputeExpr(Sema &Actions, 5190 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E, 5191 StringRef Name) { 5192 Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E)); 5193 VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr, 5194 dyn_cast<DeclRefExpr>(E->IgnoreImplicit())); 5195 auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess( 5196 Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {}))); 5197 Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false); 5198 BodyStmts.push_back(NewDeclStmt); 5199 return NewVar; 5200 } 5201 5202 /// Create a closure that computes the number of iterations of a loop. 5203 /// 5204 /// \param Actions The Sema object. 5205 /// \param LogicalTy Type for the logical iteration number. 5206 /// \param Rel Comparison operator of the loop condition. 5207 /// \param StartExpr Value of the loop counter at the first iteration. 5208 /// \param StopExpr Expression the loop counter is compared against in the loop 5209 /// condition. \param StepExpr Amount of increment after each iteration. 5210 /// 5211 /// \return Closure (CapturedStmt) of the distance calculation. 5212 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy, 5213 BinaryOperator::Opcode Rel, 5214 Expr *StartExpr, Expr *StopExpr, 5215 Expr *StepExpr) { 5216 ASTContext &Ctx = Actions.getASTContext(); 5217 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy); 5218 5219 // Captured regions currently don't support return values, we use an 5220 // out-parameter instead. All inputs are implicit captures. 5221 // TODO: Instead of capturing each DeclRefExpr occurring in 5222 // StartExpr/StopExpr/Step, these could also be passed as a value capture. 5223 QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy); 5224 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy}, 5225 {StringRef(), QualType()}}; 5226 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5227 5228 Stmt *Body; 5229 { 5230 Sema::CompoundScopeRAII CompoundScope(Actions); 5231 CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext); 5232 5233 // Get the LValue expression for the result. 5234 ImplicitParamDecl *DistParam = CS->getParam(0); 5235 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr( 5236 DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5237 5238 SmallVector<Stmt *, 4> BodyStmts; 5239 5240 // Capture all referenced variable references. 5241 // TODO: Instead of computing NewStart/NewStop/NewStep inside the 5242 // CapturedStmt, we could compute them before and capture the result, to be 5243 // used jointly with the LoopVar function. 5244 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start"); 5245 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop"); 5246 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step"); 5247 auto BuildVarRef = [&](VarDecl *VD) { 5248 return buildDeclRefExpr(Actions, VD, VD->getType(), {}); 5249 }; 5250 5251 IntegerLiteral *Zero = IntegerLiteral::Create( 5252 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {}); 5253 Expr *Dist; 5254 if (Rel == BO_NE) { 5255 // When using a != comparison, the increment can be +1 or -1. This can be 5256 // dynamic at runtime, so we need to check for the direction. 5257 Expr *IsNegStep = AssertSuccess( 5258 Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero)); 5259 5260 // Positive increment. 5261 Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp( 5262 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5263 ForwardRange = AssertSuccess( 5264 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange)); 5265 Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp( 5266 nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep))); 5267 5268 // Negative increment. 5269 Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp( 5270 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5271 BackwardRange = AssertSuccess( 5272 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange)); 5273 Expr *NegIncAmount = AssertSuccess( 5274 Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep))); 5275 Expr *BackwardDist = AssertSuccess( 5276 Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount)); 5277 5278 // Use the appropriate case. 5279 Dist = AssertSuccess(Actions.ActOnConditionalOp( 5280 {}, {}, IsNegStep, BackwardDist, ForwardDist)); 5281 } else { 5282 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) && 5283 "Expected one of these relational operators"); 5284 5285 // We can derive the direction from any other comparison operator. It is 5286 // non well-formed OpenMP if Step increments/decrements in the other 5287 // directions. Whether at least the first iteration passes the loop 5288 // condition. 5289 Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp( 5290 nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5291 5292 // Compute the range between first and last counter value. 5293 Expr *Range; 5294 if (Rel == BO_GE || Rel == BO_GT) 5295 Range = AssertSuccess(Actions.BuildBinOp( 5296 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5297 else 5298 Range = AssertSuccess(Actions.BuildBinOp( 5299 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5300 5301 // Ensure unsigned range space. 5302 Range = 5303 AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range)); 5304 5305 if (Rel == BO_LE || Rel == BO_GE) { 5306 // Add one to the range if the relational operator is inclusive. 5307 Range = 5308 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_PreInc, Range)); 5309 } 5310 5311 // Divide by the absolute step amount. 5312 Expr *Divisor = BuildVarRef(NewStep); 5313 if (Rel == BO_GE || Rel == BO_GT) 5314 Divisor = 5315 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor)); 5316 Dist = AssertSuccess( 5317 Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor)); 5318 5319 // If there is not at least one iteration, the range contains garbage. Fix 5320 // to zero in this case. 5321 Dist = AssertSuccess( 5322 Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero)); 5323 } 5324 5325 // Assign the result to the out-parameter. 5326 Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp( 5327 Actions.getCurScope(), {}, BO_Assign, DistRef, Dist)); 5328 BodyStmts.push_back(ResultAssign); 5329 5330 Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false)); 5331 } 5332 5333 return cast<CapturedStmt>( 5334 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5335 } 5336 5337 /// Create a closure that computes the loop variable from the logical iteration 5338 /// number. 5339 /// 5340 /// \param Actions The Sema object. 5341 /// \param LoopVarTy Type for the loop variable used for result value. 5342 /// \param LogicalTy Type for the logical iteration number. 5343 /// \param StartExpr Value of the loop counter at the first iteration. 5344 /// \param Step Amount of increment after each iteration. 5345 /// \param Deref Whether the loop variable is a dereference of the loop 5346 /// counter variable. 5347 /// 5348 /// \return Closure (CapturedStmt) of the loop value calculation. 5349 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy, 5350 QualType LogicalTy, 5351 DeclRefExpr *StartExpr, Expr *Step, 5352 bool Deref) { 5353 ASTContext &Ctx = Actions.getASTContext(); 5354 5355 // Pass the result as an out-parameter. Passing as return value would require 5356 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to 5357 // invoke a copy constructor. 5358 QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy); 5359 Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy}, 5360 {"Logical", LogicalTy}, 5361 {StringRef(), QualType()}}; 5362 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5363 5364 // Capture the initial iterator which represents the LoopVar value at the 5365 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update 5366 // it in every iteration, capture it by value before it is modified. 5367 VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl()); 5368 bool Invalid = Actions.tryCaptureVariable(StartVar, {}, 5369 Sema::TryCapture_ExplicitByVal, {}); 5370 (void)Invalid; 5371 assert(!Invalid && "Expecting capture-by-value to work."); 5372 5373 Expr *Body; 5374 { 5375 Sema::CompoundScopeRAII CompoundScope(Actions); 5376 auto *CS = cast<CapturedDecl>(Actions.CurContext); 5377 5378 ImplicitParamDecl *TargetParam = CS->getParam(0); 5379 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr( 5380 TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5381 ImplicitParamDecl *IndvarParam = CS->getParam(1); 5382 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr( 5383 IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5384 5385 // Capture the Start expression. 5386 CaptureVars Recap(Actions); 5387 Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr)); 5388 Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step)); 5389 5390 Expr *Skip = AssertSuccess( 5391 Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef)); 5392 // TODO: Explicitly cast to the iterator's difference_type instead of 5393 // relying on implicit conversion. 5394 Expr *Advanced = 5395 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip)); 5396 5397 if (Deref) { 5398 // For range-based for-loops convert the loop counter value to a concrete 5399 // loop variable value by dereferencing the iterator. 5400 Advanced = 5401 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced)); 5402 } 5403 5404 // Assign the result to the output parameter. 5405 Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {}, 5406 BO_Assign, TargetRef, Advanced)); 5407 } 5408 return cast<CapturedStmt>( 5409 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5410 } 5411 5412 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) { 5413 ASTContext &Ctx = getASTContext(); 5414 5415 // Extract the common elements of ForStmt and CXXForRangeStmt: 5416 // Loop variable, repeat condition, increment 5417 Expr *Cond, *Inc; 5418 VarDecl *LIVDecl, *LUVDecl; 5419 if (auto *For = dyn_cast<ForStmt>(AStmt)) { 5420 Stmt *Init = For->getInit(); 5421 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) { 5422 // For statement declares loop variable. 5423 LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl()); 5424 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) { 5425 // For statement reuses variable. 5426 assert(LCAssign->getOpcode() == BO_Assign && 5427 "init part must be a loop variable assignment"); 5428 auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS()); 5429 LIVDecl = cast<VarDecl>(CounterRef->getDecl()); 5430 } else 5431 llvm_unreachable("Cannot determine loop variable"); 5432 LUVDecl = LIVDecl; 5433 5434 Cond = For->getCond(); 5435 Inc = For->getInc(); 5436 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) { 5437 DeclStmt *BeginStmt = RangeFor->getBeginStmt(); 5438 LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl()); 5439 LUVDecl = RangeFor->getLoopVariable(); 5440 5441 Cond = RangeFor->getCond(); 5442 Inc = RangeFor->getInc(); 5443 } else 5444 llvm_unreachable("unhandled kind of loop"); 5445 5446 QualType CounterTy = LIVDecl->getType(); 5447 QualType LVTy = LUVDecl->getType(); 5448 5449 // Analyze the loop condition. 5450 Expr *LHS, *RHS; 5451 BinaryOperator::Opcode CondRel; 5452 Cond = Cond->IgnoreImplicit(); 5453 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) { 5454 LHS = CondBinExpr->getLHS(); 5455 RHS = CondBinExpr->getRHS(); 5456 CondRel = CondBinExpr->getOpcode(); 5457 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) { 5458 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands"); 5459 LHS = CondCXXOp->getArg(0); 5460 RHS = CondCXXOp->getArg(1); 5461 switch (CondCXXOp->getOperator()) { 5462 case OO_ExclaimEqual: 5463 CondRel = BO_NE; 5464 break; 5465 case OO_Less: 5466 CondRel = BO_LT; 5467 break; 5468 case OO_LessEqual: 5469 CondRel = BO_LE; 5470 break; 5471 case OO_Greater: 5472 CondRel = BO_GT; 5473 break; 5474 case OO_GreaterEqual: 5475 CondRel = BO_GE; 5476 break; 5477 default: 5478 llvm_unreachable("unexpected iterator operator"); 5479 } 5480 } else 5481 llvm_unreachable("unexpected loop condition"); 5482 5483 // Normalize such that the loop counter is on the LHS. 5484 if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) || 5485 cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) { 5486 std::swap(LHS, RHS); 5487 CondRel = BinaryOperator::reverseComparisonOp(CondRel); 5488 } 5489 auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit()); 5490 5491 // Decide the bit width for the logical iteration counter. By default use the 5492 // unsigned ptrdiff_t integer size (for iterators and pointers). 5493 // TODO: For iterators, use iterator::difference_type, 5494 // std::iterator_traits<>::difference_type or decltype(it - end). 5495 QualType LogicalTy = Ctx.getUnsignedPointerDiffType(); 5496 if (CounterTy->isIntegerType()) { 5497 unsigned BitWidth = Ctx.getIntWidth(CounterTy); 5498 LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false); 5499 } 5500 5501 // Analyze the loop increment. 5502 Expr *Step; 5503 if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) { 5504 int Direction; 5505 switch (IncUn->getOpcode()) { 5506 case UO_PreInc: 5507 case UO_PostInc: 5508 Direction = 1; 5509 break; 5510 case UO_PreDec: 5511 case UO_PostDec: 5512 Direction = -1; 5513 break; 5514 default: 5515 llvm_unreachable("unhandled unary increment operator"); 5516 } 5517 Step = IntegerLiteral::Create( 5518 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {}); 5519 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) { 5520 if (IncBin->getOpcode() == BO_AddAssign) { 5521 Step = IncBin->getRHS(); 5522 } else if (IncBin->getOpcode() == BO_SubAssign) { 5523 Step = 5524 AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS())); 5525 } else 5526 llvm_unreachable("unhandled binary increment operator"); 5527 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) { 5528 switch (CondCXXOp->getOperator()) { 5529 case OO_PlusPlus: 5530 Step = IntegerLiteral::Create( 5531 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5532 break; 5533 case OO_MinusMinus: 5534 Step = IntegerLiteral::Create( 5535 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {}); 5536 break; 5537 case OO_PlusEqual: 5538 Step = CondCXXOp->getArg(1); 5539 break; 5540 case OO_MinusEqual: 5541 Step = AssertSuccess( 5542 BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1))); 5543 break; 5544 default: 5545 llvm_unreachable("unhandled overloaded increment operator"); 5546 } 5547 } else 5548 llvm_unreachable("unknown increment expression"); 5549 5550 CapturedStmt *DistanceFunc = 5551 buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step); 5552 CapturedStmt *LoopVarFunc = buildLoopVarFunc( 5553 *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt)); 5554 DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue, 5555 {}, nullptr, nullptr, {}, nullptr); 5556 return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc, 5557 LoopVarFunc, LVRef); 5558 } 5559 5560 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 5561 CXXScopeSpec &MapperIdScopeSpec, 5562 const DeclarationNameInfo &MapperId, 5563 QualType Type, 5564 Expr *UnresolvedMapper); 5565 5566 /// Perform DFS through the structure/class data members trying to find 5567 /// member(s) with user-defined 'default' mapper and generate implicit map 5568 /// clauses for such members with the found 'default' mapper. 5569 static void 5570 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack, 5571 SmallVectorImpl<OMPClause *> &Clauses) { 5572 // Check for the deault mapper for data members. 5573 if (S.getLangOpts().OpenMP < 50) 5574 return; 5575 SmallVector<OMPClause *, 4> ImplicitMaps; 5576 DeclarationNameInfo DefaultMapperId; 5577 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier( 5578 &S.Context.Idents.get("default"))); 5579 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) { 5580 auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]); 5581 if (!C) 5582 continue; 5583 SmallVector<Expr *, 4> SubExprs; 5584 auto *MI = C->mapperlist_begin(); 5585 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End; 5586 ++I, ++MI) { 5587 // Expression is mapped using mapper - skip it. 5588 if (*MI) 5589 continue; 5590 Expr *E = *I; 5591 // Expression is dependent - skip it, build the mapper when it gets 5592 // instantiated. 5593 if (E->isTypeDependent() || E->isValueDependent() || 5594 E->containsUnexpandedParameterPack()) 5595 continue; 5596 // Array section - need to check for the mapping of the array section 5597 // element. 5598 QualType CanonType = E->getType().getCanonicalType(); 5599 if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) { 5600 const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts()); 5601 QualType BaseType = 5602 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 5603 QualType ElemType; 5604 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 5605 ElemType = ATy->getElementType(); 5606 else 5607 ElemType = BaseType->getPointeeType(); 5608 CanonType = ElemType; 5609 } 5610 5611 // DFS over data members in structures/classes. 5612 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types( 5613 1, {CanonType, nullptr}); 5614 llvm::DenseMap<const Type *, Expr *> Visited; 5615 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain( 5616 1, {nullptr, 1}); 5617 while (!Types.empty()) { 5618 QualType BaseType; 5619 FieldDecl *CurFD; 5620 std::tie(BaseType, CurFD) = Types.pop_back_val(); 5621 while (ParentChain.back().second == 0) 5622 ParentChain.pop_back(); 5623 --ParentChain.back().second; 5624 if (BaseType.isNull()) 5625 continue; 5626 // Only structs/classes are allowed to have mappers. 5627 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl(); 5628 if (!RD) 5629 continue; 5630 auto It = Visited.find(BaseType.getTypePtr()); 5631 if (It == Visited.end()) { 5632 // Try to find the associated user-defined mapper. 5633 CXXScopeSpec MapperIdScopeSpec; 5634 ExprResult ER = buildUserDefinedMapperRef( 5635 S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId, 5636 BaseType, /*UnresolvedMapper=*/nullptr); 5637 if (ER.isInvalid()) 5638 continue; 5639 It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first; 5640 } 5641 // Found default mapper. 5642 if (It->second) { 5643 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType, 5644 VK_LValue, OK_Ordinary, E); 5645 OE->setIsUnique(/*V=*/true); 5646 Expr *BaseExpr = OE; 5647 for (const auto &P : ParentChain) { 5648 if (P.first) { 5649 BaseExpr = S.BuildMemberExpr( 5650 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5651 NestedNameSpecifierLoc(), SourceLocation(), P.first, 5652 DeclAccessPair::make(P.first, P.first->getAccess()), 5653 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5654 P.first->getType(), VK_LValue, OK_Ordinary); 5655 BaseExpr = S.DefaultLvalueConversion(BaseExpr).get(); 5656 } 5657 } 5658 if (CurFD) 5659 BaseExpr = S.BuildMemberExpr( 5660 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5661 NestedNameSpecifierLoc(), SourceLocation(), CurFD, 5662 DeclAccessPair::make(CurFD, CurFD->getAccess()), 5663 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5664 CurFD->getType(), VK_LValue, OK_Ordinary); 5665 SubExprs.push_back(BaseExpr); 5666 continue; 5667 } 5668 // Check for the "default" mapper for data memebers. 5669 bool FirstIter = true; 5670 for (FieldDecl *FD : RD->fields()) { 5671 if (!FD) 5672 continue; 5673 QualType FieldTy = FD->getType(); 5674 if (FieldTy.isNull() || 5675 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType())) 5676 continue; 5677 if (FirstIter) { 5678 FirstIter = false; 5679 ParentChain.emplace_back(CurFD, 1); 5680 } else { 5681 ++ParentChain.back().second; 5682 } 5683 Types.emplace_back(FieldTy, FD); 5684 } 5685 } 5686 } 5687 if (SubExprs.empty()) 5688 continue; 5689 CXXScopeSpec MapperIdScopeSpec; 5690 DeclarationNameInfo MapperId; 5691 if (OMPClause *NewClause = S.ActOnOpenMPMapClause( 5692 C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), 5693 MapperIdScopeSpec, MapperId, C->getMapType(), 5694 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5695 SubExprs, OMPVarListLocTy())) 5696 Clauses.push_back(NewClause); 5697 } 5698 } 5699 5700 StmtResult Sema::ActOnOpenMPExecutableDirective( 5701 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5702 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5703 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5704 StmtResult Res = StmtError(); 5705 // First check CancelRegion which is then used in checkNestingOfRegions. 5706 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5707 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5708 StartLoc)) 5709 return StmtError(); 5710 5711 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5712 VarsWithInheritedDSAType VarsWithInheritedDSA; 5713 bool ErrorFound = false; 5714 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5715 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5716 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master && 5717 Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) { 5718 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5719 5720 // Check default data sharing attributes for referenced variables. 5721 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5722 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5723 Stmt *S = AStmt; 5724 while (--ThisCaptureLevel >= 0) 5725 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5726 DSAChecker.Visit(S); 5727 if (!isOpenMPTargetDataManagementDirective(Kind) && 5728 !isOpenMPTaskingDirective(Kind)) { 5729 // Visit subcaptures to generate implicit clauses for captured vars. 5730 auto *CS = cast<CapturedStmt>(AStmt); 5731 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5732 getOpenMPCaptureRegions(CaptureRegions, Kind); 5733 // Ignore outer tasking regions for target directives. 5734 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5735 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5736 DSAChecker.visitSubCaptures(CS); 5737 } 5738 if (DSAChecker.isErrorFound()) 5739 return StmtError(); 5740 // Generate list of implicitly defined firstprivate variables. 5741 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5742 5743 SmallVector<Expr *, 4> ImplicitFirstprivates( 5744 DSAChecker.getImplicitFirstprivate().begin(), 5745 DSAChecker.getImplicitFirstprivate().end()); 5746 const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 5747 SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete]; 5748 SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 5749 ImplicitMapModifiers[DefaultmapKindNum]; 5750 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers> 5751 ImplicitMapModifiersLoc[DefaultmapKindNum]; 5752 // Get the original location of present modifier from Defaultmap clause. 5753 SourceLocation PresentModifierLocs[DefaultmapKindNum]; 5754 for (OMPClause *C : Clauses) { 5755 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C)) 5756 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present) 5757 PresentModifierLocs[DMC->getDefaultmapKind()] = 5758 DMC->getDefaultmapModifierLoc(); 5759 } 5760 for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) { 5761 auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC); 5762 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5763 ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap( 5764 Kind, static_cast<OpenMPMapClauseKind>(I)); 5765 ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end()); 5766 } 5767 ArrayRef<OpenMPMapModifierKind> ImplicitModifier = 5768 DSAChecker.getImplicitMapModifier(Kind); 5769 ImplicitMapModifiers[VC].append(ImplicitModifier.begin(), 5770 ImplicitModifier.end()); 5771 std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]), 5772 ImplicitModifier.size(), PresentModifierLocs[VC]); 5773 } 5774 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5775 for (OMPClause *C : Clauses) { 5776 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5777 for (Expr *E : IRC->taskgroup_descriptors()) 5778 if (E) 5779 ImplicitFirstprivates.emplace_back(E); 5780 } 5781 // OpenMP 5.0, 2.10.1 task Construct 5782 // [detach clause]... The event-handle will be considered as if it was 5783 // specified on a firstprivate clause. 5784 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5785 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5786 } 5787 if (!ImplicitFirstprivates.empty()) { 5788 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5789 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5790 SourceLocation())) { 5791 ClausesWithImplicit.push_back(Implicit); 5792 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5793 ImplicitFirstprivates.size(); 5794 } else { 5795 ErrorFound = true; 5796 } 5797 } 5798 for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) { 5799 int ClauseKindCnt = -1; 5800 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) { 5801 ++ClauseKindCnt; 5802 if (ImplicitMap.empty()) 5803 continue; 5804 CXXScopeSpec MapperIdScopeSpec; 5805 DeclarationNameInfo MapperId; 5806 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5807 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5808 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I], 5809 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true, 5810 SourceLocation(), SourceLocation(), ImplicitMap, 5811 OMPVarListLocTy())) { 5812 ClausesWithImplicit.emplace_back(Implicit); 5813 ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() != 5814 ImplicitMap.size(); 5815 } else { 5816 ErrorFound = true; 5817 } 5818 } 5819 } 5820 // Build expressions for implicit maps of data members with 'default' 5821 // mappers. 5822 if (LangOpts.OpenMP >= 50) 5823 processImplicitMapsWithDefaultMappers(*this, DSAStack, 5824 ClausesWithImplicit); 5825 } 5826 5827 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5828 switch (Kind) { 5829 case OMPD_parallel: 5830 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5831 EndLoc); 5832 AllowedNameModifiers.push_back(OMPD_parallel); 5833 break; 5834 case OMPD_simd: 5835 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5836 VarsWithInheritedDSA); 5837 if (LangOpts.OpenMP >= 50) 5838 AllowedNameModifiers.push_back(OMPD_simd); 5839 break; 5840 case OMPD_tile: 5841 Res = 5842 ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5843 break; 5844 case OMPD_for: 5845 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5846 VarsWithInheritedDSA); 5847 break; 5848 case OMPD_for_simd: 5849 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5850 EndLoc, VarsWithInheritedDSA); 5851 if (LangOpts.OpenMP >= 50) 5852 AllowedNameModifiers.push_back(OMPD_simd); 5853 break; 5854 case OMPD_sections: 5855 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5856 EndLoc); 5857 break; 5858 case OMPD_section: 5859 assert(ClausesWithImplicit.empty() && 5860 "No clauses are allowed for 'omp section' directive"); 5861 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5862 break; 5863 case OMPD_single: 5864 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5865 EndLoc); 5866 break; 5867 case OMPD_master: 5868 assert(ClausesWithImplicit.empty() && 5869 "No clauses are allowed for 'omp master' directive"); 5870 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5871 break; 5872 case OMPD_masked: 5873 Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc, 5874 EndLoc); 5875 break; 5876 case OMPD_critical: 5877 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5878 StartLoc, EndLoc); 5879 break; 5880 case OMPD_parallel_for: 5881 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5882 EndLoc, VarsWithInheritedDSA); 5883 AllowedNameModifiers.push_back(OMPD_parallel); 5884 break; 5885 case OMPD_parallel_for_simd: 5886 Res = ActOnOpenMPParallelForSimdDirective( 5887 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5888 AllowedNameModifiers.push_back(OMPD_parallel); 5889 if (LangOpts.OpenMP >= 50) 5890 AllowedNameModifiers.push_back(OMPD_simd); 5891 break; 5892 case OMPD_parallel_master: 5893 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5894 StartLoc, EndLoc); 5895 AllowedNameModifiers.push_back(OMPD_parallel); 5896 break; 5897 case OMPD_parallel_sections: 5898 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5899 StartLoc, EndLoc); 5900 AllowedNameModifiers.push_back(OMPD_parallel); 5901 break; 5902 case OMPD_task: 5903 Res = 5904 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5905 AllowedNameModifiers.push_back(OMPD_task); 5906 break; 5907 case OMPD_taskyield: 5908 assert(ClausesWithImplicit.empty() && 5909 "No clauses are allowed for 'omp taskyield' directive"); 5910 assert(AStmt == nullptr && 5911 "No associated statement allowed for 'omp taskyield' directive"); 5912 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 5913 break; 5914 case OMPD_barrier: 5915 assert(ClausesWithImplicit.empty() && 5916 "No clauses are allowed for 'omp barrier' directive"); 5917 assert(AStmt == nullptr && 5918 "No associated statement allowed for 'omp barrier' directive"); 5919 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 5920 break; 5921 case OMPD_taskwait: 5922 assert(ClausesWithImplicit.empty() && 5923 "No clauses are allowed for 'omp taskwait' directive"); 5924 assert(AStmt == nullptr && 5925 "No associated statement allowed for 'omp taskwait' directive"); 5926 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 5927 break; 5928 case OMPD_taskgroup: 5929 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 5930 EndLoc); 5931 break; 5932 case OMPD_flush: 5933 assert(AStmt == nullptr && 5934 "No associated statement allowed for 'omp flush' directive"); 5935 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 5936 break; 5937 case OMPD_depobj: 5938 assert(AStmt == nullptr && 5939 "No associated statement allowed for 'omp depobj' directive"); 5940 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 5941 break; 5942 case OMPD_scan: 5943 assert(AStmt == nullptr && 5944 "No associated statement allowed for 'omp scan' directive"); 5945 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 5946 break; 5947 case OMPD_ordered: 5948 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 5949 EndLoc); 5950 break; 5951 case OMPD_atomic: 5952 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 5953 EndLoc); 5954 break; 5955 case OMPD_teams: 5956 Res = 5957 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5958 break; 5959 case OMPD_target: 5960 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 5961 EndLoc); 5962 AllowedNameModifiers.push_back(OMPD_target); 5963 break; 5964 case OMPD_target_parallel: 5965 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 5966 StartLoc, EndLoc); 5967 AllowedNameModifiers.push_back(OMPD_target); 5968 AllowedNameModifiers.push_back(OMPD_parallel); 5969 break; 5970 case OMPD_target_parallel_for: 5971 Res = ActOnOpenMPTargetParallelForDirective( 5972 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5973 AllowedNameModifiers.push_back(OMPD_target); 5974 AllowedNameModifiers.push_back(OMPD_parallel); 5975 break; 5976 case OMPD_cancellation_point: 5977 assert(ClausesWithImplicit.empty() && 5978 "No clauses are allowed for 'omp cancellation point' directive"); 5979 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 5980 "cancellation point' directive"); 5981 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 5982 break; 5983 case OMPD_cancel: 5984 assert(AStmt == nullptr && 5985 "No associated statement allowed for 'omp cancel' directive"); 5986 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 5987 CancelRegion); 5988 AllowedNameModifiers.push_back(OMPD_cancel); 5989 break; 5990 case OMPD_target_data: 5991 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 5992 EndLoc); 5993 AllowedNameModifiers.push_back(OMPD_target_data); 5994 break; 5995 case OMPD_target_enter_data: 5996 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 5997 EndLoc, AStmt); 5998 AllowedNameModifiers.push_back(OMPD_target_enter_data); 5999 break; 6000 case OMPD_target_exit_data: 6001 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 6002 EndLoc, AStmt); 6003 AllowedNameModifiers.push_back(OMPD_target_exit_data); 6004 break; 6005 case OMPD_taskloop: 6006 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6007 EndLoc, VarsWithInheritedDSA); 6008 AllowedNameModifiers.push_back(OMPD_taskloop); 6009 break; 6010 case OMPD_taskloop_simd: 6011 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6012 EndLoc, VarsWithInheritedDSA); 6013 AllowedNameModifiers.push_back(OMPD_taskloop); 6014 if (LangOpts.OpenMP >= 50) 6015 AllowedNameModifiers.push_back(OMPD_simd); 6016 break; 6017 case OMPD_master_taskloop: 6018 Res = ActOnOpenMPMasterTaskLoopDirective( 6019 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6020 AllowedNameModifiers.push_back(OMPD_taskloop); 6021 break; 6022 case OMPD_master_taskloop_simd: 6023 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 6024 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6025 AllowedNameModifiers.push_back(OMPD_taskloop); 6026 if (LangOpts.OpenMP >= 50) 6027 AllowedNameModifiers.push_back(OMPD_simd); 6028 break; 6029 case OMPD_parallel_master_taskloop: 6030 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 6031 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6032 AllowedNameModifiers.push_back(OMPD_taskloop); 6033 AllowedNameModifiers.push_back(OMPD_parallel); 6034 break; 6035 case OMPD_parallel_master_taskloop_simd: 6036 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 6037 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6038 AllowedNameModifiers.push_back(OMPD_taskloop); 6039 AllowedNameModifiers.push_back(OMPD_parallel); 6040 if (LangOpts.OpenMP >= 50) 6041 AllowedNameModifiers.push_back(OMPD_simd); 6042 break; 6043 case OMPD_distribute: 6044 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 6045 EndLoc, VarsWithInheritedDSA); 6046 break; 6047 case OMPD_target_update: 6048 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 6049 EndLoc, AStmt); 6050 AllowedNameModifiers.push_back(OMPD_target_update); 6051 break; 6052 case OMPD_distribute_parallel_for: 6053 Res = ActOnOpenMPDistributeParallelForDirective( 6054 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6055 AllowedNameModifiers.push_back(OMPD_parallel); 6056 break; 6057 case OMPD_distribute_parallel_for_simd: 6058 Res = ActOnOpenMPDistributeParallelForSimdDirective( 6059 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6060 AllowedNameModifiers.push_back(OMPD_parallel); 6061 if (LangOpts.OpenMP >= 50) 6062 AllowedNameModifiers.push_back(OMPD_simd); 6063 break; 6064 case OMPD_distribute_simd: 6065 Res = ActOnOpenMPDistributeSimdDirective( 6066 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6067 if (LangOpts.OpenMP >= 50) 6068 AllowedNameModifiers.push_back(OMPD_simd); 6069 break; 6070 case OMPD_target_parallel_for_simd: 6071 Res = ActOnOpenMPTargetParallelForSimdDirective( 6072 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6073 AllowedNameModifiers.push_back(OMPD_target); 6074 AllowedNameModifiers.push_back(OMPD_parallel); 6075 if (LangOpts.OpenMP >= 50) 6076 AllowedNameModifiers.push_back(OMPD_simd); 6077 break; 6078 case OMPD_target_simd: 6079 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6080 EndLoc, VarsWithInheritedDSA); 6081 AllowedNameModifiers.push_back(OMPD_target); 6082 if (LangOpts.OpenMP >= 50) 6083 AllowedNameModifiers.push_back(OMPD_simd); 6084 break; 6085 case OMPD_teams_distribute: 6086 Res = ActOnOpenMPTeamsDistributeDirective( 6087 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6088 break; 6089 case OMPD_teams_distribute_simd: 6090 Res = ActOnOpenMPTeamsDistributeSimdDirective( 6091 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6092 if (LangOpts.OpenMP >= 50) 6093 AllowedNameModifiers.push_back(OMPD_simd); 6094 break; 6095 case OMPD_teams_distribute_parallel_for_simd: 6096 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6097 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6098 AllowedNameModifiers.push_back(OMPD_parallel); 6099 if (LangOpts.OpenMP >= 50) 6100 AllowedNameModifiers.push_back(OMPD_simd); 6101 break; 6102 case OMPD_teams_distribute_parallel_for: 6103 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 6104 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6105 AllowedNameModifiers.push_back(OMPD_parallel); 6106 break; 6107 case OMPD_target_teams: 6108 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 6109 EndLoc); 6110 AllowedNameModifiers.push_back(OMPD_target); 6111 break; 6112 case OMPD_target_teams_distribute: 6113 Res = ActOnOpenMPTargetTeamsDistributeDirective( 6114 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6115 AllowedNameModifiers.push_back(OMPD_target); 6116 break; 6117 case OMPD_target_teams_distribute_parallel_for: 6118 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6119 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6120 AllowedNameModifiers.push_back(OMPD_target); 6121 AllowedNameModifiers.push_back(OMPD_parallel); 6122 break; 6123 case OMPD_target_teams_distribute_parallel_for_simd: 6124 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6125 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6126 AllowedNameModifiers.push_back(OMPD_target); 6127 AllowedNameModifiers.push_back(OMPD_parallel); 6128 if (LangOpts.OpenMP >= 50) 6129 AllowedNameModifiers.push_back(OMPD_simd); 6130 break; 6131 case OMPD_target_teams_distribute_simd: 6132 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 6133 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6134 AllowedNameModifiers.push_back(OMPD_target); 6135 if (LangOpts.OpenMP >= 50) 6136 AllowedNameModifiers.push_back(OMPD_simd); 6137 break; 6138 case OMPD_interop: 6139 assert(AStmt == nullptr && 6140 "No associated statement allowed for 'omp interop' directive"); 6141 Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc); 6142 break; 6143 case OMPD_dispatch: 6144 Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc, 6145 EndLoc); 6146 break; 6147 case OMPD_declare_target: 6148 case OMPD_end_declare_target: 6149 case OMPD_threadprivate: 6150 case OMPD_allocate: 6151 case OMPD_declare_reduction: 6152 case OMPD_declare_mapper: 6153 case OMPD_declare_simd: 6154 case OMPD_requires: 6155 case OMPD_declare_variant: 6156 case OMPD_begin_declare_variant: 6157 case OMPD_end_declare_variant: 6158 llvm_unreachable("OpenMP Directive is not allowed"); 6159 case OMPD_unknown: 6160 default: 6161 llvm_unreachable("Unknown OpenMP directive"); 6162 } 6163 6164 ErrorFound = Res.isInvalid() || ErrorFound; 6165 6166 // Check variables in the clauses if default(none) or 6167 // default(firstprivate) was specified. 6168 if (DSAStack->getDefaultDSA() == DSA_none || 6169 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6170 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 6171 for (OMPClause *C : Clauses) { 6172 switch (C->getClauseKind()) { 6173 case OMPC_num_threads: 6174 case OMPC_dist_schedule: 6175 // Do not analyse if no parent teams directive. 6176 if (isOpenMPTeamsDirective(Kind)) 6177 break; 6178 continue; 6179 case OMPC_if: 6180 if (isOpenMPTeamsDirective(Kind) && 6181 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 6182 break; 6183 if (isOpenMPParallelDirective(Kind) && 6184 isOpenMPTaskLoopDirective(Kind) && 6185 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 6186 break; 6187 continue; 6188 case OMPC_schedule: 6189 case OMPC_detach: 6190 break; 6191 case OMPC_grainsize: 6192 case OMPC_num_tasks: 6193 case OMPC_final: 6194 case OMPC_priority: 6195 case OMPC_novariants: 6196 case OMPC_nocontext: 6197 // Do not analyze if no parent parallel directive. 6198 if (isOpenMPParallelDirective(Kind)) 6199 break; 6200 continue; 6201 case OMPC_ordered: 6202 case OMPC_device: 6203 case OMPC_num_teams: 6204 case OMPC_thread_limit: 6205 case OMPC_hint: 6206 case OMPC_collapse: 6207 case OMPC_safelen: 6208 case OMPC_simdlen: 6209 case OMPC_sizes: 6210 case OMPC_default: 6211 case OMPC_proc_bind: 6212 case OMPC_private: 6213 case OMPC_firstprivate: 6214 case OMPC_lastprivate: 6215 case OMPC_shared: 6216 case OMPC_reduction: 6217 case OMPC_task_reduction: 6218 case OMPC_in_reduction: 6219 case OMPC_linear: 6220 case OMPC_aligned: 6221 case OMPC_copyin: 6222 case OMPC_copyprivate: 6223 case OMPC_nowait: 6224 case OMPC_untied: 6225 case OMPC_mergeable: 6226 case OMPC_allocate: 6227 case OMPC_read: 6228 case OMPC_write: 6229 case OMPC_update: 6230 case OMPC_capture: 6231 case OMPC_seq_cst: 6232 case OMPC_acq_rel: 6233 case OMPC_acquire: 6234 case OMPC_release: 6235 case OMPC_relaxed: 6236 case OMPC_depend: 6237 case OMPC_threads: 6238 case OMPC_simd: 6239 case OMPC_map: 6240 case OMPC_nogroup: 6241 case OMPC_defaultmap: 6242 case OMPC_to: 6243 case OMPC_from: 6244 case OMPC_use_device_ptr: 6245 case OMPC_use_device_addr: 6246 case OMPC_is_device_ptr: 6247 case OMPC_nontemporal: 6248 case OMPC_order: 6249 case OMPC_destroy: 6250 case OMPC_inclusive: 6251 case OMPC_exclusive: 6252 case OMPC_uses_allocators: 6253 case OMPC_affinity: 6254 continue; 6255 case OMPC_allocator: 6256 case OMPC_flush: 6257 case OMPC_depobj: 6258 case OMPC_threadprivate: 6259 case OMPC_uniform: 6260 case OMPC_unknown: 6261 case OMPC_unified_address: 6262 case OMPC_unified_shared_memory: 6263 case OMPC_reverse_offload: 6264 case OMPC_dynamic_allocators: 6265 case OMPC_atomic_default_mem_order: 6266 case OMPC_device_type: 6267 case OMPC_match: 6268 default: 6269 llvm_unreachable("Unexpected clause"); 6270 } 6271 for (Stmt *CC : C->children()) { 6272 if (CC) 6273 DSAChecker.Visit(CC); 6274 } 6275 } 6276 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 6277 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 6278 } 6279 for (const auto &P : VarsWithInheritedDSA) { 6280 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 6281 continue; 6282 ErrorFound = true; 6283 if (DSAStack->getDefaultDSA() == DSA_none || 6284 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6285 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 6286 << P.first << P.second->getSourceRange(); 6287 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 6288 } else if (getLangOpts().OpenMP >= 50) { 6289 Diag(P.second->getExprLoc(), 6290 diag::err_omp_defaultmap_no_attr_for_variable) 6291 << P.first << P.second->getSourceRange(); 6292 Diag(DSAStack->getDefaultDSALocation(), 6293 diag::note_omp_defaultmap_attr_none); 6294 } 6295 } 6296 6297 if (!AllowedNameModifiers.empty()) 6298 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 6299 ErrorFound; 6300 6301 if (ErrorFound) 6302 return StmtError(); 6303 6304 if (!CurContext->isDependentContext() && 6305 isOpenMPTargetExecutionDirective(Kind) && 6306 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 6307 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 6308 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 6309 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 6310 // Register target to DSA Stack. 6311 DSAStack->addTargetDirLocation(StartLoc); 6312 } 6313 6314 return Res; 6315 } 6316 6317 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 6318 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 6319 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 6320 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 6321 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 6322 assert(Aligneds.size() == Alignments.size()); 6323 assert(Linears.size() == LinModifiers.size()); 6324 assert(Linears.size() == Steps.size()); 6325 if (!DG || DG.get().isNull()) 6326 return DeclGroupPtrTy(); 6327 6328 const int SimdId = 0; 6329 if (!DG.get().isSingleDecl()) { 6330 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6331 << SimdId; 6332 return DG; 6333 } 6334 Decl *ADecl = DG.get().getSingleDecl(); 6335 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6336 ADecl = FTD->getTemplatedDecl(); 6337 6338 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6339 if (!FD) { 6340 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 6341 return DeclGroupPtrTy(); 6342 } 6343 6344 // OpenMP [2.8.2, declare simd construct, Description] 6345 // The parameter of the simdlen clause must be a constant positive integer 6346 // expression. 6347 ExprResult SL; 6348 if (Simdlen) 6349 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 6350 // OpenMP [2.8.2, declare simd construct, Description] 6351 // The special this pointer can be used as if was one of the arguments to the 6352 // function in any of the linear, aligned, or uniform clauses. 6353 // The uniform clause declares one or more arguments to have an invariant 6354 // value for all concurrent invocations of the function in the execution of a 6355 // single SIMD loop. 6356 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 6357 const Expr *UniformedLinearThis = nullptr; 6358 for (const Expr *E : Uniforms) { 6359 E = E->IgnoreParenImpCasts(); 6360 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6361 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 6362 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6363 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6364 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 6365 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 6366 continue; 6367 } 6368 if (isa<CXXThisExpr>(E)) { 6369 UniformedLinearThis = E; 6370 continue; 6371 } 6372 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6373 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6374 } 6375 // OpenMP [2.8.2, declare simd construct, Description] 6376 // The aligned clause declares that the object to which each list item points 6377 // is aligned to the number of bytes expressed in the optional parameter of 6378 // the aligned clause. 6379 // The special this pointer can be used as if was one of the arguments to the 6380 // function in any of the linear, aligned, or uniform clauses. 6381 // The type of list items appearing in the aligned clause must be array, 6382 // pointer, reference to array, or reference to pointer. 6383 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 6384 const Expr *AlignedThis = nullptr; 6385 for (const Expr *E : Aligneds) { 6386 E = E->IgnoreParenImpCasts(); 6387 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6388 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6389 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6390 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6391 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6392 ->getCanonicalDecl() == CanonPVD) { 6393 // OpenMP [2.8.1, simd construct, Restrictions] 6394 // A list-item cannot appear in more than one aligned clause. 6395 if (AlignedArgs.count(CanonPVD) > 0) { 6396 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6397 << 1 << getOpenMPClauseName(OMPC_aligned) 6398 << E->getSourceRange(); 6399 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 6400 diag::note_omp_explicit_dsa) 6401 << getOpenMPClauseName(OMPC_aligned); 6402 continue; 6403 } 6404 AlignedArgs[CanonPVD] = E; 6405 QualType QTy = PVD->getType() 6406 .getNonReferenceType() 6407 .getUnqualifiedType() 6408 .getCanonicalType(); 6409 const Type *Ty = QTy.getTypePtrOrNull(); 6410 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 6411 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 6412 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 6413 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 6414 } 6415 continue; 6416 } 6417 } 6418 if (isa<CXXThisExpr>(E)) { 6419 if (AlignedThis) { 6420 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6421 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 6422 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 6423 << getOpenMPClauseName(OMPC_aligned); 6424 } 6425 AlignedThis = E; 6426 continue; 6427 } 6428 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6429 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6430 } 6431 // The optional parameter of the aligned clause, alignment, must be a constant 6432 // positive integer expression. If no optional parameter is specified, 6433 // implementation-defined default alignments for SIMD instructions on the 6434 // target platforms are assumed. 6435 SmallVector<const Expr *, 4> NewAligns; 6436 for (Expr *E : Alignments) { 6437 ExprResult Align; 6438 if (E) 6439 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 6440 NewAligns.push_back(Align.get()); 6441 } 6442 // OpenMP [2.8.2, declare simd construct, Description] 6443 // The linear clause declares one or more list items to be private to a SIMD 6444 // lane and to have a linear relationship with respect to the iteration space 6445 // of a loop. 6446 // The special this pointer can be used as if was one of the arguments to the 6447 // function in any of the linear, aligned, or uniform clauses. 6448 // When a linear-step expression is specified in a linear clause it must be 6449 // either a constant integer expression or an integer-typed parameter that is 6450 // specified in a uniform clause on the directive. 6451 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 6452 const bool IsUniformedThis = UniformedLinearThis != nullptr; 6453 auto MI = LinModifiers.begin(); 6454 for (const Expr *E : Linears) { 6455 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 6456 ++MI; 6457 E = E->IgnoreParenImpCasts(); 6458 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6459 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6460 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6461 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6462 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6463 ->getCanonicalDecl() == CanonPVD) { 6464 // OpenMP [2.15.3.7, linear Clause, Restrictions] 6465 // A list-item cannot appear in more than one linear clause. 6466 if (LinearArgs.count(CanonPVD) > 0) { 6467 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6468 << getOpenMPClauseName(OMPC_linear) 6469 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 6470 Diag(LinearArgs[CanonPVD]->getExprLoc(), 6471 diag::note_omp_explicit_dsa) 6472 << getOpenMPClauseName(OMPC_linear); 6473 continue; 6474 } 6475 // Each argument can appear in at most one uniform or linear clause. 6476 if (UniformedArgs.count(CanonPVD) > 0) { 6477 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6478 << getOpenMPClauseName(OMPC_linear) 6479 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 6480 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 6481 diag::note_omp_explicit_dsa) 6482 << getOpenMPClauseName(OMPC_uniform); 6483 continue; 6484 } 6485 LinearArgs[CanonPVD] = E; 6486 if (E->isValueDependent() || E->isTypeDependent() || 6487 E->isInstantiationDependent() || 6488 E->containsUnexpandedParameterPack()) 6489 continue; 6490 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 6491 PVD->getOriginalType(), 6492 /*IsDeclareSimd=*/true); 6493 continue; 6494 } 6495 } 6496 if (isa<CXXThisExpr>(E)) { 6497 if (UniformedLinearThis) { 6498 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6499 << getOpenMPClauseName(OMPC_linear) 6500 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 6501 << E->getSourceRange(); 6502 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 6503 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 6504 : OMPC_linear); 6505 continue; 6506 } 6507 UniformedLinearThis = E; 6508 if (E->isValueDependent() || E->isTypeDependent() || 6509 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6510 continue; 6511 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 6512 E->getType(), /*IsDeclareSimd=*/true); 6513 continue; 6514 } 6515 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6516 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6517 } 6518 Expr *Step = nullptr; 6519 Expr *NewStep = nullptr; 6520 SmallVector<Expr *, 4> NewSteps; 6521 for (Expr *E : Steps) { 6522 // Skip the same step expression, it was checked already. 6523 if (Step == E || !E) { 6524 NewSteps.push_back(E ? NewStep : nullptr); 6525 continue; 6526 } 6527 Step = E; 6528 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 6529 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6530 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6531 if (UniformedArgs.count(CanonPVD) == 0) { 6532 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 6533 << Step->getSourceRange(); 6534 } else if (E->isValueDependent() || E->isTypeDependent() || 6535 E->isInstantiationDependent() || 6536 E->containsUnexpandedParameterPack() || 6537 CanonPVD->getType()->hasIntegerRepresentation()) { 6538 NewSteps.push_back(Step); 6539 } else { 6540 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 6541 << Step->getSourceRange(); 6542 } 6543 continue; 6544 } 6545 NewStep = Step; 6546 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 6547 !Step->isInstantiationDependent() && 6548 !Step->containsUnexpandedParameterPack()) { 6549 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 6550 .get(); 6551 if (NewStep) 6552 NewStep = 6553 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 6554 } 6555 NewSteps.push_back(NewStep); 6556 } 6557 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 6558 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 6559 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 6560 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 6561 const_cast<Expr **>(Linears.data()), Linears.size(), 6562 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 6563 NewSteps.data(), NewSteps.size(), SR); 6564 ADecl->addAttr(NewAttr); 6565 return DG; 6566 } 6567 6568 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 6569 QualType NewType) { 6570 assert(NewType->isFunctionProtoType() && 6571 "Expected function type with prototype."); 6572 assert(FD->getType()->isFunctionNoProtoType() && 6573 "Expected function with type with no prototype."); 6574 assert(FDWithProto->getType()->isFunctionProtoType() && 6575 "Expected function with prototype."); 6576 // Synthesize parameters with the same types. 6577 FD->setType(NewType); 6578 SmallVector<ParmVarDecl *, 16> Params; 6579 for (const ParmVarDecl *P : FDWithProto->parameters()) { 6580 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 6581 SourceLocation(), nullptr, P->getType(), 6582 /*TInfo=*/nullptr, SC_None, nullptr); 6583 Param->setScopeInfo(0, Params.size()); 6584 Param->setImplicit(); 6585 Params.push_back(Param); 6586 } 6587 6588 FD->setParams(Params); 6589 } 6590 6591 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) { 6592 if (D->isInvalidDecl()) 6593 return; 6594 FunctionDecl *FD = nullptr; 6595 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6596 FD = UTemplDecl->getTemplatedDecl(); 6597 else 6598 FD = cast<FunctionDecl>(D); 6599 assert(FD && "Expected a function declaration!"); 6600 6601 // If we are intantiating templates we do *not* apply scoped assumptions but 6602 // only global ones. We apply scoped assumption to the template definition 6603 // though. 6604 if (!inTemplateInstantiation()) { 6605 for (AssumptionAttr *AA : OMPAssumeScoped) 6606 FD->addAttr(AA); 6607 } 6608 for (AssumptionAttr *AA : OMPAssumeGlobal) 6609 FD->addAttr(AA); 6610 } 6611 6612 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 6613 : TI(&TI), NameSuffix(TI.getMangledName()) {} 6614 6615 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 6616 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 6617 SmallVectorImpl<FunctionDecl *> &Bases) { 6618 if (!D.getIdentifier()) 6619 return; 6620 6621 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6622 6623 // Template specialization is an extension, check if we do it. 6624 bool IsTemplated = !TemplateParamLists.empty(); 6625 if (IsTemplated & 6626 !DVScope.TI->isExtensionActive( 6627 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 6628 return; 6629 6630 IdentifierInfo *BaseII = D.getIdentifier(); 6631 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 6632 LookupOrdinaryName); 6633 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 6634 6635 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 6636 QualType FType = TInfo->getType(); 6637 6638 bool IsConstexpr = 6639 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr; 6640 bool IsConsteval = 6641 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval; 6642 6643 for (auto *Candidate : Lookup) { 6644 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 6645 FunctionDecl *UDecl = nullptr; 6646 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) 6647 UDecl = cast<FunctionTemplateDecl>(CandidateDecl)->getTemplatedDecl(); 6648 else if (!IsTemplated) 6649 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 6650 if (!UDecl) 6651 continue; 6652 6653 // Don't specialize constexpr/consteval functions with 6654 // non-constexpr/consteval functions. 6655 if (UDecl->isConstexpr() && !IsConstexpr) 6656 continue; 6657 if (UDecl->isConsteval() && !IsConsteval) 6658 continue; 6659 6660 QualType UDeclTy = UDecl->getType(); 6661 if (!UDeclTy->isDependentType()) { 6662 QualType NewType = Context.mergeFunctionTypes( 6663 FType, UDeclTy, /* OfBlockPointer */ false, 6664 /* Unqualified */ false, /* AllowCXX */ true); 6665 if (NewType.isNull()) 6666 continue; 6667 } 6668 6669 // Found a base! 6670 Bases.push_back(UDecl); 6671 } 6672 6673 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 6674 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 6675 // If no base was found we create a declaration that we use as base. 6676 if (Bases.empty() && UseImplicitBase) { 6677 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); 6678 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 6679 BaseD->setImplicit(true); 6680 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 6681 Bases.push_back(BaseTemplD->getTemplatedDecl()); 6682 else 6683 Bases.push_back(cast<FunctionDecl>(BaseD)); 6684 } 6685 6686 std::string MangledName; 6687 MangledName += D.getIdentifier()->getName(); 6688 MangledName += getOpenMPVariantManglingSeparatorStr(); 6689 MangledName += DVScope.NameSuffix; 6690 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 6691 6692 VariantII.setMangledOpenMPVariantName(true); 6693 D.SetIdentifier(&VariantII, D.getBeginLoc()); 6694 } 6695 6696 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 6697 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 6698 // Do not mark function as is used to prevent its emission if this is the 6699 // only place where it is used. 6700 EnterExpressionEvaluationContext Unevaluated( 6701 *this, Sema::ExpressionEvaluationContext::Unevaluated); 6702 6703 FunctionDecl *FD = nullptr; 6704 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6705 FD = UTemplDecl->getTemplatedDecl(); 6706 else 6707 FD = cast<FunctionDecl>(D); 6708 auto *VariantFuncRef = DeclRefExpr::Create( 6709 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 6710 /* RefersToEnclosingVariableOrCapture */ false, 6711 /* NameLoc */ FD->getLocation(), FD->getType(), ExprValueKind::VK_RValue); 6712 6713 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6714 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 6715 Context, VariantFuncRef, DVScope.TI); 6716 for (FunctionDecl *BaseFD : Bases) 6717 BaseFD->addAttr(OMPDeclareVariantA); 6718 } 6719 6720 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 6721 SourceLocation LParenLoc, 6722 MultiExprArg ArgExprs, 6723 SourceLocation RParenLoc, Expr *ExecConfig) { 6724 // The common case is a regular call we do not want to specialize at all. Try 6725 // to make that case fast by bailing early. 6726 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 6727 if (!CE) 6728 return Call; 6729 6730 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 6731 if (!CalleeFnDecl) 6732 return Call; 6733 6734 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 6735 return Call; 6736 6737 ASTContext &Context = getASTContext(); 6738 std::function<void(StringRef)> DiagUnknownTrait = [this, 6739 CE](StringRef ISATrait) { 6740 // TODO Track the selector locations in a way that is accessible here to 6741 // improve the diagnostic location. 6742 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6743 << ISATrait; 6744 }; 6745 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6746 getCurFunctionDecl()); 6747 6748 QualType CalleeFnType = CalleeFnDecl->getType(); 6749 6750 SmallVector<Expr *, 4> Exprs; 6751 SmallVector<VariantMatchInfo, 4> VMIs; 6752 while (CalleeFnDecl) { 6753 for (OMPDeclareVariantAttr *A : 6754 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6755 Expr *VariantRef = A->getVariantFuncRef(); 6756 6757 VariantMatchInfo VMI; 6758 OMPTraitInfo &TI = A->getTraitInfo(); 6759 TI.getAsVariantMatchInfo(Context, VMI); 6760 if (!isVariantApplicableInContext(VMI, OMPCtx, 6761 /* DeviceSetOnly */ false)) 6762 continue; 6763 6764 VMIs.push_back(VMI); 6765 Exprs.push_back(VariantRef); 6766 } 6767 6768 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6769 } 6770 6771 ExprResult NewCall; 6772 do { 6773 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6774 if (BestIdx < 0) 6775 return Call; 6776 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6777 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6778 6779 { 6780 // Try to build a (member) call expression for the current best applicable 6781 // variant expression. We allow this to fail in which case we continue 6782 // with the next best variant expression. The fail case is part of the 6783 // implementation defined behavior in the OpenMP standard when it talks 6784 // about what differences in the function prototypes: "Any differences 6785 // that the specific OpenMP context requires in the prototype of the 6786 // variant from the base function prototype are implementation defined." 6787 // This wording is there to allow the specialized variant to have a 6788 // different type than the base function. This is intended and OK but if 6789 // we cannot create a call the difference is not in the "implementation 6790 // defined range" we allow. 6791 Sema::TentativeAnalysisScope Trap(*this); 6792 6793 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6794 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6795 BestExpr = MemberExpr::CreateImplicit( 6796 Context, MemberCall->getImplicitObjectArgument(), 6797 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6798 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6799 } 6800 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6801 ExecConfig); 6802 if (NewCall.isUsable()) { 6803 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6804 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6805 QualType NewType = Context.mergeFunctionTypes( 6806 CalleeFnType, NewCalleeFnDecl->getType(), 6807 /* OfBlockPointer */ false, 6808 /* Unqualified */ false, /* AllowCXX */ true); 6809 if (!NewType.isNull()) 6810 break; 6811 // Don't use the call if the function type was not compatible. 6812 NewCall = nullptr; 6813 } 6814 } 6815 } 6816 6817 VMIs.erase(VMIs.begin() + BestIdx); 6818 Exprs.erase(Exprs.begin() + BestIdx); 6819 } while (!VMIs.empty()); 6820 6821 if (!NewCall.isUsable()) 6822 return Call; 6823 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6824 } 6825 6826 Optional<std::pair<FunctionDecl *, Expr *>> 6827 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6828 Expr *VariantRef, OMPTraitInfo &TI, 6829 SourceRange SR) { 6830 if (!DG || DG.get().isNull()) 6831 return None; 6832 6833 const int VariantId = 1; 6834 // Must be applied only to single decl. 6835 if (!DG.get().isSingleDecl()) { 6836 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6837 << VariantId << SR; 6838 return None; 6839 } 6840 Decl *ADecl = DG.get().getSingleDecl(); 6841 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6842 ADecl = FTD->getTemplatedDecl(); 6843 6844 // Decl must be a function. 6845 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6846 if (!FD) { 6847 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6848 << VariantId << SR; 6849 return None; 6850 } 6851 6852 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6853 return FD->hasAttrs() && 6854 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6855 FD->hasAttr<TargetAttr>()); 6856 }; 6857 // OpenMP is not compatible with CPU-specific attributes. 6858 if (HasMultiVersionAttributes(FD)) { 6859 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6860 << SR; 6861 return None; 6862 } 6863 6864 // Allow #pragma omp declare variant only if the function is not used. 6865 if (FD->isUsed(false)) 6866 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6867 << FD->getLocation(); 6868 6869 // Check if the function was emitted already. 6870 const FunctionDecl *Definition; 6871 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6872 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6873 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6874 << FD->getLocation(); 6875 6876 // The VariantRef must point to function. 6877 if (!VariantRef) { 6878 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6879 return None; 6880 } 6881 6882 auto ShouldDelayChecks = [](Expr *&E, bool) { 6883 return E && (E->isTypeDependent() || E->isValueDependent() || 6884 E->containsUnexpandedParameterPack() || 6885 E->isInstantiationDependent()); 6886 }; 6887 // Do not check templates, wait until instantiation. 6888 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6889 TI.anyScoreOrCondition(ShouldDelayChecks)) 6890 return std::make_pair(FD, VariantRef); 6891 6892 // Deal with non-constant score and user condition expressions. 6893 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6894 bool IsScore) -> bool { 6895 if (!E || E->isIntegerConstantExpr(Context)) 6896 return false; 6897 6898 if (IsScore) { 6899 // We warn on non-constant scores and pretend they were not present. 6900 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6901 << E; 6902 E = nullptr; 6903 } else { 6904 // We could replace a non-constant user condition with "false" but we 6905 // will soon need to handle these anyway for the dynamic version of 6906 // OpenMP context selectors. 6907 Diag(E->getExprLoc(), 6908 diag::err_omp_declare_variant_user_condition_not_constant) 6909 << E; 6910 } 6911 return true; 6912 }; 6913 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 6914 return None; 6915 6916 // Convert VariantRef expression to the type of the original function to 6917 // resolve possible conflicts. 6918 ExprResult VariantRefCast = VariantRef; 6919 if (LangOpts.CPlusPlus) { 6920 QualType FnPtrType; 6921 auto *Method = dyn_cast<CXXMethodDecl>(FD); 6922 if (Method && !Method->isStatic()) { 6923 const Type *ClassType = 6924 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 6925 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 6926 ExprResult ER; 6927 { 6928 // Build adrr_of unary op to correctly handle type checks for member 6929 // functions. 6930 Sema::TentativeAnalysisScope Trap(*this); 6931 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 6932 VariantRef); 6933 } 6934 if (!ER.isUsable()) { 6935 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6936 << VariantId << VariantRef->getSourceRange(); 6937 return None; 6938 } 6939 VariantRef = ER.get(); 6940 } else { 6941 FnPtrType = Context.getPointerType(FD->getType()); 6942 } 6943 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 6944 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 6945 ImplicitConversionSequence ICS = TryImplicitConversion( 6946 VariantRef, FnPtrType.getUnqualifiedType(), 6947 /*SuppressUserConversions=*/false, AllowedExplicit::None, 6948 /*InOverloadResolution=*/false, 6949 /*CStyle=*/false, 6950 /*AllowObjCWritebackConversion=*/false); 6951 if (ICS.isFailure()) { 6952 Diag(VariantRef->getExprLoc(), 6953 diag::err_omp_declare_variant_incompat_types) 6954 << VariantRef->getType() 6955 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 6956 << VariantRef->getSourceRange(); 6957 return None; 6958 } 6959 VariantRefCast = PerformImplicitConversion( 6960 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 6961 if (!VariantRefCast.isUsable()) 6962 return None; 6963 } 6964 // Drop previously built artificial addr_of unary op for member functions. 6965 if (Method && !Method->isStatic()) { 6966 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 6967 if (auto *UO = dyn_cast<UnaryOperator>( 6968 PossibleAddrOfVariantRef->IgnoreImplicit())) 6969 VariantRefCast = UO->getSubExpr(); 6970 } 6971 } 6972 6973 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 6974 if (!ER.isUsable() || 6975 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 6976 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6977 << VariantId << VariantRef->getSourceRange(); 6978 return None; 6979 } 6980 6981 // The VariantRef must point to function. 6982 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 6983 if (!DRE) { 6984 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6985 << VariantId << VariantRef->getSourceRange(); 6986 return None; 6987 } 6988 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 6989 if (!NewFD) { 6990 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6991 << VariantId << VariantRef->getSourceRange(); 6992 return None; 6993 } 6994 6995 // Check if function types are compatible in C. 6996 if (!LangOpts.CPlusPlus) { 6997 QualType NewType = 6998 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 6999 if (NewType.isNull()) { 7000 Diag(VariantRef->getExprLoc(), 7001 diag::err_omp_declare_variant_incompat_types) 7002 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 7003 return None; 7004 } 7005 if (NewType->isFunctionProtoType()) { 7006 if (FD->getType()->isFunctionNoProtoType()) 7007 setPrototype(*this, FD, NewFD, NewType); 7008 else if (NewFD->getType()->isFunctionNoProtoType()) 7009 setPrototype(*this, NewFD, FD, NewType); 7010 } 7011 } 7012 7013 // Check if variant function is not marked with declare variant directive. 7014 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 7015 Diag(VariantRef->getExprLoc(), 7016 diag::warn_omp_declare_variant_marked_as_declare_variant) 7017 << VariantRef->getSourceRange(); 7018 SourceRange SR = 7019 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 7020 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 7021 return None; 7022 } 7023 7024 enum DoesntSupport { 7025 VirtFuncs = 1, 7026 Constructors = 3, 7027 Destructors = 4, 7028 DeletedFuncs = 5, 7029 DefaultedFuncs = 6, 7030 ConstexprFuncs = 7, 7031 ConstevalFuncs = 8, 7032 }; 7033 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 7034 if (CXXFD->isVirtual()) { 7035 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7036 << VirtFuncs; 7037 return None; 7038 } 7039 7040 if (isa<CXXConstructorDecl>(FD)) { 7041 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7042 << Constructors; 7043 return None; 7044 } 7045 7046 if (isa<CXXDestructorDecl>(FD)) { 7047 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7048 << Destructors; 7049 return None; 7050 } 7051 } 7052 7053 if (FD->isDeleted()) { 7054 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7055 << DeletedFuncs; 7056 return None; 7057 } 7058 7059 if (FD->isDefaulted()) { 7060 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7061 << DefaultedFuncs; 7062 return None; 7063 } 7064 7065 if (FD->isConstexpr()) { 7066 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7067 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 7068 return None; 7069 } 7070 7071 // Check general compatibility. 7072 if (areMultiversionVariantFunctionsCompatible( 7073 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 7074 PartialDiagnosticAt(SourceLocation(), 7075 PartialDiagnostic::NullDiagnostic()), 7076 PartialDiagnosticAt( 7077 VariantRef->getExprLoc(), 7078 PDiag(diag::err_omp_declare_variant_doesnt_support)), 7079 PartialDiagnosticAt(VariantRef->getExprLoc(), 7080 PDiag(diag::err_omp_declare_variant_diff) 7081 << FD->getLocation()), 7082 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 7083 /*CLinkageMayDiffer=*/true)) 7084 return None; 7085 return std::make_pair(FD, cast<Expr>(DRE)); 7086 } 7087 7088 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 7089 Expr *VariantRef, 7090 OMPTraitInfo &TI, 7091 SourceRange SR) { 7092 auto *NewAttr = 7093 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR); 7094 FD->addAttr(NewAttr); 7095 } 7096 7097 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 7098 Stmt *AStmt, 7099 SourceLocation StartLoc, 7100 SourceLocation EndLoc) { 7101 if (!AStmt) 7102 return StmtError(); 7103 7104 auto *CS = cast<CapturedStmt>(AStmt); 7105 // 1.2.2 OpenMP Language Terminology 7106 // Structured block - An executable statement with a single entry at the 7107 // top and a single exit at the bottom. 7108 // The point of exit cannot be a branch out of the structured block. 7109 // longjmp() and throw() must not violate the entry/exit criteria. 7110 CS->getCapturedDecl()->setNothrow(); 7111 7112 setFunctionHasBranchProtectedScope(); 7113 7114 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7115 DSAStack->getTaskgroupReductionRef(), 7116 DSAStack->isCancelRegion()); 7117 } 7118 7119 namespace { 7120 /// Iteration space of a single for loop. 7121 struct LoopIterationSpace final { 7122 /// True if the condition operator is the strict compare operator (<, > or 7123 /// !=). 7124 bool IsStrictCompare = false; 7125 /// Condition of the loop. 7126 Expr *PreCond = nullptr; 7127 /// This expression calculates the number of iterations in the loop. 7128 /// It is always possible to calculate it before starting the loop. 7129 Expr *NumIterations = nullptr; 7130 /// The loop counter variable. 7131 Expr *CounterVar = nullptr; 7132 /// Private loop counter variable. 7133 Expr *PrivateCounterVar = nullptr; 7134 /// This is initializer for the initial value of #CounterVar. 7135 Expr *CounterInit = nullptr; 7136 /// This is step for the #CounterVar used to generate its update: 7137 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 7138 Expr *CounterStep = nullptr; 7139 /// Should step be subtracted? 7140 bool Subtract = false; 7141 /// Source range of the loop init. 7142 SourceRange InitSrcRange; 7143 /// Source range of the loop condition. 7144 SourceRange CondSrcRange; 7145 /// Source range of the loop increment. 7146 SourceRange IncSrcRange; 7147 /// Minimum value that can have the loop control variable. Used to support 7148 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 7149 /// since only such variables can be used in non-loop invariant expressions. 7150 Expr *MinValue = nullptr; 7151 /// Maximum value that can have the loop control variable. Used to support 7152 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 7153 /// since only such variables can be used in non-loop invariant expressions. 7154 Expr *MaxValue = nullptr; 7155 /// true, if the lower bound depends on the outer loop control var. 7156 bool IsNonRectangularLB = false; 7157 /// true, if the upper bound depends on the outer loop control var. 7158 bool IsNonRectangularUB = false; 7159 /// Index of the loop this loop depends on and forms non-rectangular loop 7160 /// nest. 7161 unsigned LoopDependentIdx = 0; 7162 /// Final condition for the non-rectangular loop nest support. It is used to 7163 /// check that the number of iterations for this particular counter must be 7164 /// finished. 7165 Expr *FinalCondition = nullptr; 7166 }; 7167 7168 /// Helper class for checking canonical form of the OpenMP loops and 7169 /// extracting iteration space of each loop in the loop nest, that will be used 7170 /// for IR generation. 7171 class OpenMPIterationSpaceChecker { 7172 /// Reference to Sema. 7173 Sema &SemaRef; 7174 /// Does the loop associated directive support non-rectangular loops? 7175 bool SupportsNonRectangular; 7176 /// Data-sharing stack. 7177 DSAStackTy &Stack; 7178 /// A location for diagnostics (when there is no some better location). 7179 SourceLocation DefaultLoc; 7180 /// A location for diagnostics (when increment is not compatible). 7181 SourceLocation ConditionLoc; 7182 /// A source location for referring to loop init later. 7183 SourceRange InitSrcRange; 7184 /// A source location for referring to condition later. 7185 SourceRange ConditionSrcRange; 7186 /// A source location for referring to increment later. 7187 SourceRange IncrementSrcRange; 7188 /// Loop variable. 7189 ValueDecl *LCDecl = nullptr; 7190 /// Reference to loop variable. 7191 Expr *LCRef = nullptr; 7192 /// Lower bound (initializer for the var). 7193 Expr *LB = nullptr; 7194 /// Upper bound. 7195 Expr *UB = nullptr; 7196 /// Loop step (increment). 7197 Expr *Step = nullptr; 7198 /// This flag is true when condition is one of: 7199 /// Var < UB 7200 /// Var <= UB 7201 /// UB > Var 7202 /// UB >= Var 7203 /// This will have no value when the condition is != 7204 llvm::Optional<bool> TestIsLessOp; 7205 /// This flag is true when condition is strict ( < or > ). 7206 bool TestIsStrictOp = false; 7207 /// This flag is true when step is subtracted on each iteration. 7208 bool SubtractStep = false; 7209 /// The outer loop counter this loop depends on (if any). 7210 const ValueDecl *DepDecl = nullptr; 7211 /// Contains number of loop (starts from 1) on which loop counter init 7212 /// expression of this loop depends on. 7213 Optional<unsigned> InitDependOnLC; 7214 /// Contains number of loop (starts from 1) on which loop counter condition 7215 /// expression of this loop depends on. 7216 Optional<unsigned> CondDependOnLC; 7217 /// Checks if the provide statement depends on the loop counter. 7218 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 7219 /// Original condition required for checking of the exit condition for 7220 /// non-rectangular loop. 7221 Expr *Condition = nullptr; 7222 7223 public: 7224 OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular, 7225 DSAStackTy &Stack, SourceLocation DefaultLoc) 7226 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular), 7227 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 7228 /// Check init-expr for canonical loop form and save loop counter 7229 /// variable - #Var and its initialization value - #LB. 7230 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 7231 /// Check test-expr for canonical form, save upper-bound (#UB), flags 7232 /// for less/greater and for strict/non-strict comparison. 7233 bool checkAndSetCond(Expr *S); 7234 /// Check incr-expr for canonical loop form and return true if it 7235 /// does not conform, otherwise save loop step (#Step). 7236 bool checkAndSetInc(Expr *S); 7237 /// Return the loop counter variable. 7238 ValueDecl *getLoopDecl() const { return LCDecl; } 7239 /// Return the reference expression to loop counter variable. 7240 Expr *getLoopDeclRefExpr() const { return LCRef; } 7241 /// Source range of the loop init. 7242 SourceRange getInitSrcRange() const { return InitSrcRange; } 7243 /// Source range of the loop condition. 7244 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 7245 /// Source range of the loop increment. 7246 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 7247 /// True if the step should be subtracted. 7248 bool shouldSubtractStep() const { return SubtractStep; } 7249 /// True, if the compare operator is strict (<, > or !=). 7250 bool isStrictTestOp() const { return TestIsStrictOp; } 7251 /// Build the expression to calculate the number of iterations. 7252 Expr *buildNumIterations( 7253 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7254 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7255 /// Build the precondition expression for the loops. 7256 Expr * 7257 buildPreCond(Scope *S, Expr *Cond, 7258 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7259 /// Build reference expression to the counter be used for codegen. 7260 DeclRefExpr * 7261 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7262 DSAStackTy &DSA) const; 7263 /// Build reference expression to the private counter be used for 7264 /// codegen. 7265 Expr *buildPrivateCounterVar() const; 7266 /// Build initialization of the counter be used for codegen. 7267 Expr *buildCounterInit() const; 7268 /// Build step of the counter be used for codegen. 7269 Expr *buildCounterStep() const; 7270 /// Build loop data with counter value for depend clauses in ordered 7271 /// directives. 7272 Expr * 7273 buildOrderedLoopData(Scope *S, Expr *Counter, 7274 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7275 SourceLocation Loc, Expr *Inc = nullptr, 7276 OverloadedOperatorKind OOK = OO_Amp); 7277 /// Builds the minimum value for the loop counter. 7278 std::pair<Expr *, Expr *> buildMinMaxValues( 7279 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7280 /// Builds final condition for the non-rectangular loops. 7281 Expr *buildFinalCondition(Scope *S) const; 7282 /// Return true if any expression is dependent. 7283 bool dependent() const; 7284 /// Returns true if the initializer forms non-rectangular loop. 7285 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 7286 /// Returns true if the condition forms non-rectangular loop. 7287 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 7288 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 7289 unsigned getLoopDependentIdx() const { 7290 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 7291 } 7292 7293 private: 7294 /// Check the right-hand side of an assignment in the increment 7295 /// expression. 7296 bool checkAndSetIncRHS(Expr *RHS); 7297 /// Helper to set loop counter variable and its initializer. 7298 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 7299 bool EmitDiags); 7300 /// Helper to set upper bound. 7301 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 7302 SourceRange SR, SourceLocation SL); 7303 /// Helper to set loop increment. 7304 bool setStep(Expr *NewStep, bool Subtract); 7305 }; 7306 7307 bool OpenMPIterationSpaceChecker::dependent() const { 7308 if (!LCDecl) { 7309 assert(!LB && !UB && !Step); 7310 return false; 7311 } 7312 return LCDecl->getType()->isDependentType() || 7313 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 7314 (Step && Step->isValueDependent()); 7315 } 7316 7317 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 7318 Expr *NewLCRefExpr, 7319 Expr *NewLB, bool EmitDiags) { 7320 // State consistency checking to ensure correct usage. 7321 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 7322 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7323 if (!NewLCDecl || !NewLB) 7324 return true; 7325 LCDecl = getCanonicalDecl(NewLCDecl); 7326 LCRef = NewLCRefExpr; 7327 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 7328 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7329 if ((Ctor->isCopyOrMoveConstructor() || 7330 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7331 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7332 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 7333 LB = NewLB; 7334 if (EmitDiags) 7335 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 7336 return false; 7337 } 7338 7339 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 7340 llvm::Optional<bool> LessOp, 7341 bool StrictOp, SourceRange SR, 7342 SourceLocation SL) { 7343 // State consistency checking to ensure correct usage. 7344 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 7345 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7346 if (!NewUB) 7347 return true; 7348 UB = NewUB; 7349 if (LessOp) 7350 TestIsLessOp = LessOp; 7351 TestIsStrictOp = StrictOp; 7352 ConditionSrcRange = SR; 7353 ConditionLoc = SL; 7354 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 7355 return false; 7356 } 7357 7358 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 7359 // State consistency checking to ensure correct usage. 7360 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 7361 if (!NewStep) 7362 return true; 7363 if (!NewStep->isValueDependent()) { 7364 // Check that the step is integer expression. 7365 SourceLocation StepLoc = NewStep->getBeginLoc(); 7366 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 7367 StepLoc, getExprAsWritten(NewStep)); 7368 if (Val.isInvalid()) 7369 return true; 7370 NewStep = Val.get(); 7371 7372 // OpenMP [2.6, Canonical Loop Form, Restrictions] 7373 // If test-expr is of form var relational-op b and relational-op is < or 7374 // <= then incr-expr must cause var to increase on each iteration of the 7375 // loop. If test-expr is of form var relational-op b and relational-op is 7376 // > or >= then incr-expr must cause var to decrease on each iteration of 7377 // the loop. 7378 // If test-expr is of form b relational-op var and relational-op is < or 7379 // <= then incr-expr must cause var to decrease on each iteration of the 7380 // loop. If test-expr is of form b relational-op var and relational-op is 7381 // > or >= then incr-expr must cause var to increase on each iteration of 7382 // the loop. 7383 Optional<llvm::APSInt> Result = 7384 NewStep->getIntegerConstantExpr(SemaRef.Context); 7385 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 7386 bool IsConstNeg = 7387 Result && Result->isSigned() && (Subtract != Result->isNegative()); 7388 bool IsConstPos = 7389 Result && Result->isSigned() && (Subtract == Result->isNegative()); 7390 bool IsConstZero = Result && !Result->getBoolValue(); 7391 7392 // != with increment is treated as <; != with decrement is treated as > 7393 if (!TestIsLessOp.hasValue()) 7394 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 7395 if (UB && (IsConstZero || 7396 (TestIsLessOp.getValue() ? 7397 (IsConstNeg || (IsUnsigned && Subtract)) : 7398 (IsConstPos || (IsUnsigned && !Subtract))))) { 7399 SemaRef.Diag(NewStep->getExprLoc(), 7400 diag::err_omp_loop_incr_not_compatible) 7401 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 7402 SemaRef.Diag(ConditionLoc, 7403 diag::note_omp_loop_cond_requres_compatible_incr) 7404 << TestIsLessOp.getValue() << ConditionSrcRange; 7405 return true; 7406 } 7407 if (TestIsLessOp.getValue() == Subtract) { 7408 NewStep = 7409 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 7410 .get(); 7411 Subtract = !Subtract; 7412 } 7413 } 7414 7415 Step = NewStep; 7416 SubtractStep = Subtract; 7417 return false; 7418 } 7419 7420 namespace { 7421 /// Checker for the non-rectangular loops. Checks if the initializer or 7422 /// condition expression references loop counter variable. 7423 class LoopCounterRefChecker final 7424 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 7425 Sema &SemaRef; 7426 DSAStackTy &Stack; 7427 const ValueDecl *CurLCDecl = nullptr; 7428 const ValueDecl *DepDecl = nullptr; 7429 const ValueDecl *PrevDepDecl = nullptr; 7430 bool IsInitializer = true; 7431 bool SupportsNonRectangular; 7432 unsigned BaseLoopId = 0; 7433 bool checkDecl(const Expr *E, const ValueDecl *VD) { 7434 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 7435 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 7436 << (IsInitializer ? 0 : 1); 7437 return false; 7438 } 7439 const auto &&Data = Stack.isLoopControlVariable(VD); 7440 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 7441 // The type of the loop iterator on which we depend may not have a random 7442 // access iterator type. 7443 if (Data.first && VD->getType()->isRecordType()) { 7444 SmallString<128> Name; 7445 llvm::raw_svector_ostream OS(Name); 7446 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7447 /*Qualified=*/true); 7448 SemaRef.Diag(E->getExprLoc(), 7449 diag::err_omp_wrong_dependency_iterator_type) 7450 << OS.str(); 7451 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 7452 return false; 7453 } 7454 if (Data.first && !SupportsNonRectangular) { 7455 SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency); 7456 return false; 7457 } 7458 if (Data.first && 7459 (DepDecl || (PrevDepDecl && 7460 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 7461 if (!DepDecl && PrevDepDecl) 7462 DepDecl = PrevDepDecl; 7463 SmallString<128> Name; 7464 llvm::raw_svector_ostream OS(Name); 7465 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7466 /*Qualified=*/true); 7467 SemaRef.Diag(E->getExprLoc(), 7468 diag::err_omp_invariant_or_linear_dependency) 7469 << OS.str(); 7470 return false; 7471 } 7472 if (Data.first) { 7473 DepDecl = VD; 7474 BaseLoopId = Data.first; 7475 } 7476 return Data.first; 7477 } 7478 7479 public: 7480 bool VisitDeclRefExpr(const DeclRefExpr *E) { 7481 const ValueDecl *VD = E->getDecl(); 7482 if (isa<VarDecl>(VD)) 7483 return checkDecl(E, VD); 7484 return false; 7485 } 7486 bool VisitMemberExpr(const MemberExpr *E) { 7487 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 7488 const ValueDecl *VD = E->getMemberDecl(); 7489 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 7490 return checkDecl(E, VD); 7491 } 7492 return false; 7493 } 7494 bool VisitStmt(const Stmt *S) { 7495 bool Res = false; 7496 for (const Stmt *Child : S->children()) 7497 Res = (Child && Visit(Child)) || Res; 7498 return Res; 7499 } 7500 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 7501 const ValueDecl *CurLCDecl, bool IsInitializer, 7502 const ValueDecl *PrevDepDecl = nullptr, 7503 bool SupportsNonRectangular = true) 7504 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 7505 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer), 7506 SupportsNonRectangular(SupportsNonRectangular) {} 7507 unsigned getBaseLoopId() const { 7508 assert(CurLCDecl && "Expected loop dependency."); 7509 return BaseLoopId; 7510 } 7511 const ValueDecl *getDepDecl() const { 7512 assert(CurLCDecl && "Expected loop dependency."); 7513 return DepDecl; 7514 } 7515 }; 7516 } // namespace 7517 7518 Optional<unsigned> 7519 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 7520 bool IsInitializer) { 7521 // Check for the non-rectangular loops. 7522 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 7523 DepDecl, SupportsNonRectangular); 7524 if (LoopStmtChecker.Visit(S)) { 7525 DepDecl = LoopStmtChecker.getDepDecl(); 7526 return LoopStmtChecker.getBaseLoopId(); 7527 } 7528 return llvm::None; 7529 } 7530 7531 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 7532 // Check init-expr for canonical loop form and save loop counter 7533 // variable - #Var and its initialization value - #LB. 7534 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 7535 // var = lb 7536 // integer-type var = lb 7537 // random-access-iterator-type var = lb 7538 // pointer-type var = lb 7539 // 7540 if (!S) { 7541 if (EmitDiags) { 7542 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 7543 } 7544 return true; 7545 } 7546 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7547 if (!ExprTemp->cleanupsHaveSideEffects()) 7548 S = ExprTemp->getSubExpr(); 7549 7550 InitSrcRange = S->getSourceRange(); 7551 if (Expr *E = dyn_cast<Expr>(S)) 7552 S = E->IgnoreParens(); 7553 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7554 if (BO->getOpcode() == BO_Assign) { 7555 Expr *LHS = BO->getLHS()->IgnoreParens(); 7556 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7557 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7558 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7559 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7560 EmitDiags); 7561 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 7562 } 7563 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7564 if (ME->isArrow() && 7565 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7566 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7567 EmitDiags); 7568 } 7569 } 7570 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 7571 if (DS->isSingleDecl()) { 7572 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 7573 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 7574 // Accept non-canonical init form here but emit ext. warning. 7575 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 7576 SemaRef.Diag(S->getBeginLoc(), 7577 diag::ext_omp_loop_not_canonical_init) 7578 << S->getSourceRange(); 7579 return setLCDeclAndLB( 7580 Var, 7581 buildDeclRefExpr(SemaRef, Var, 7582 Var->getType().getNonReferenceType(), 7583 DS->getBeginLoc()), 7584 Var->getInit(), EmitDiags); 7585 } 7586 } 7587 } 7588 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7589 if (CE->getOperator() == OO_Equal) { 7590 Expr *LHS = CE->getArg(0); 7591 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7592 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7593 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7594 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7595 EmitDiags); 7596 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 7597 } 7598 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7599 if (ME->isArrow() && 7600 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7601 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7602 EmitDiags); 7603 } 7604 } 7605 } 7606 7607 if (dependent() || SemaRef.CurContext->isDependentContext()) 7608 return false; 7609 if (EmitDiags) { 7610 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 7611 << S->getSourceRange(); 7612 } 7613 return true; 7614 } 7615 7616 /// Ignore parenthesizes, implicit casts, copy constructor and return the 7617 /// variable (which may be the loop variable) if possible. 7618 static const ValueDecl *getInitLCDecl(const Expr *E) { 7619 if (!E) 7620 return nullptr; 7621 E = getExprAsWritten(E); 7622 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 7623 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7624 if ((Ctor->isCopyOrMoveConstructor() || 7625 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7626 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7627 E = CE->getArg(0)->IgnoreParenImpCasts(); 7628 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 7629 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 7630 return getCanonicalDecl(VD); 7631 } 7632 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 7633 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7634 return getCanonicalDecl(ME->getMemberDecl()); 7635 return nullptr; 7636 } 7637 7638 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 7639 // Check test-expr for canonical form, save upper-bound UB, flags for 7640 // less/greater and for strict/non-strict comparison. 7641 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 7642 // var relational-op b 7643 // b relational-op var 7644 // 7645 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 7646 if (!S) { 7647 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 7648 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 7649 return true; 7650 } 7651 Condition = S; 7652 S = getExprAsWritten(S); 7653 SourceLocation CondLoc = S->getBeginLoc(); 7654 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7655 if (BO->isRelationalOp()) { 7656 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7657 return setUB(BO->getRHS(), 7658 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 7659 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 7660 BO->getSourceRange(), BO->getOperatorLoc()); 7661 if (getInitLCDecl(BO->getRHS()) == LCDecl) 7662 return setUB(BO->getLHS(), 7663 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 7664 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 7665 BO->getSourceRange(), BO->getOperatorLoc()); 7666 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE) 7667 return setUB( 7668 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(), 7669 /*LessOp=*/llvm::None, 7670 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc()); 7671 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7672 if (CE->getNumArgs() == 2) { 7673 auto Op = CE->getOperator(); 7674 switch (Op) { 7675 case OO_Greater: 7676 case OO_GreaterEqual: 7677 case OO_Less: 7678 case OO_LessEqual: 7679 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7680 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 7681 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 7682 CE->getOperatorLoc()); 7683 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 7684 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 7685 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 7686 CE->getOperatorLoc()); 7687 break; 7688 case OO_ExclaimEqual: 7689 if (IneqCondIsCanonical) 7690 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1) 7691 : CE->getArg(0), 7692 /*LessOp=*/llvm::None, 7693 /*StrictOp=*/true, CE->getSourceRange(), 7694 CE->getOperatorLoc()); 7695 break; 7696 default: 7697 break; 7698 } 7699 } 7700 } 7701 if (dependent() || SemaRef.CurContext->isDependentContext()) 7702 return false; 7703 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 7704 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 7705 return true; 7706 } 7707 7708 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 7709 // RHS of canonical loop form increment can be: 7710 // var + incr 7711 // incr + var 7712 // var - incr 7713 // 7714 RHS = RHS->IgnoreParenImpCasts(); 7715 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 7716 if (BO->isAdditiveOp()) { 7717 bool IsAdd = BO->getOpcode() == BO_Add; 7718 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7719 return setStep(BO->getRHS(), !IsAdd); 7720 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 7721 return setStep(BO->getLHS(), /*Subtract=*/false); 7722 } 7723 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 7724 bool IsAdd = CE->getOperator() == OO_Plus; 7725 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 7726 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7727 return setStep(CE->getArg(1), !IsAdd); 7728 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 7729 return setStep(CE->getArg(0), /*Subtract=*/false); 7730 } 7731 } 7732 if (dependent() || SemaRef.CurContext->isDependentContext()) 7733 return false; 7734 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7735 << RHS->getSourceRange() << LCDecl; 7736 return true; 7737 } 7738 7739 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 7740 // Check incr-expr for canonical loop form and return true if it 7741 // does not conform. 7742 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 7743 // ++var 7744 // var++ 7745 // --var 7746 // var-- 7747 // var += incr 7748 // var -= incr 7749 // var = var + incr 7750 // var = incr + var 7751 // var = var - incr 7752 // 7753 if (!S) { 7754 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7755 return true; 7756 } 7757 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7758 if (!ExprTemp->cleanupsHaveSideEffects()) 7759 S = ExprTemp->getSubExpr(); 7760 7761 IncrementSrcRange = S->getSourceRange(); 7762 S = S->IgnoreParens(); 7763 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7764 if (UO->isIncrementDecrementOp() && 7765 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7766 return setStep(SemaRef 7767 .ActOnIntegerConstant(UO->getBeginLoc(), 7768 (UO->isDecrementOp() ? -1 : 1)) 7769 .get(), 7770 /*Subtract=*/false); 7771 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7772 switch (BO->getOpcode()) { 7773 case BO_AddAssign: 7774 case BO_SubAssign: 7775 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7776 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 7777 break; 7778 case BO_Assign: 7779 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7780 return checkAndSetIncRHS(BO->getRHS()); 7781 break; 7782 default: 7783 break; 7784 } 7785 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7786 switch (CE->getOperator()) { 7787 case OO_PlusPlus: 7788 case OO_MinusMinus: 7789 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7790 return setStep(SemaRef 7791 .ActOnIntegerConstant( 7792 CE->getBeginLoc(), 7793 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 7794 .get(), 7795 /*Subtract=*/false); 7796 break; 7797 case OO_PlusEqual: 7798 case OO_MinusEqual: 7799 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7800 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 7801 break; 7802 case OO_Equal: 7803 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7804 return checkAndSetIncRHS(CE->getArg(1)); 7805 break; 7806 default: 7807 break; 7808 } 7809 } 7810 if (dependent() || SemaRef.CurContext->isDependentContext()) 7811 return false; 7812 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7813 << S->getSourceRange() << LCDecl; 7814 return true; 7815 } 7816 7817 static ExprResult 7818 tryBuildCapture(Sema &SemaRef, Expr *Capture, 7819 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7820 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 7821 return Capture; 7822 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 7823 return SemaRef.PerformImplicitConversion( 7824 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 7825 /*AllowExplicit=*/true); 7826 auto I = Captures.find(Capture); 7827 if (I != Captures.end()) 7828 return buildCapture(SemaRef, Capture, I->second); 7829 DeclRefExpr *Ref = nullptr; 7830 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 7831 Captures[Capture] = Ref; 7832 return Res; 7833 } 7834 7835 /// Calculate number of iterations, transforming to unsigned, if number of 7836 /// iterations may be larger than the original type. 7837 static Expr * 7838 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 7839 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7840 bool TestIsStrictOp, bool RoundToStep, 7841 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7842 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7843 if (!NewStep.isUsable()) 7844 return nullptr; 7845 llvm::APSInt LRes, SRes; 7846 bool IsLowerConst = false, IsStepConst = false; 7847 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7848 LRes = *Res; 7849 IsLowerConst = true; 7850 } 7851 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7852 SRes = *Res; 7853 IsStepConst = true; 7854 } 7855 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7856 ((!TestIsStrictOp && LRes.isNonNegative()) || 7857 (TestIsStrictOp && LRes.isStrictlyPositive())); 7858 bool NeedToReorganize = false; 7859 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 7860 if (!NoNeedToConvert && IsLowerConst && 7861 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 7862 NoNeedToConvert = true; 7863 if (RoundToStep) { 7864 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 7865 ? LRes.getBitWidth() 7866 : SRes.getBitWidth(); 7867 LRes = LRes.extend(BW + 1); 7868 LRes.setIsSigned(true); 7869 SRes = SRes.extend(BW + 1); 7870 SRes.setIsSigned(true); 7871 LRes -= SRes; 7872 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 7873 LRes = LRes.trunc(BW); 7874 } 7875 if (TestIsStrictOp) { 7876 unsigned BW = LRes.getBitWidth(); 7877 LRes = LRes.extend(BW + 1); 7878 LRes.setIsSigned(true); 7879 ++LRes; 7880 NoNeedToConvert = 7881 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 7882 // truncate to the original bitwidth. 7883 LRes = LRes.trunc(BW); 7884 } 7885 NeedToReorganize = NoNeedToConvert; 7886 } 7887 llvm::APSInt URes; 7888 bool IsUpperConst = false; 7889 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 7890 URes = *Res; 7891 IsUpperConst = true; 7892 } 7893 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 7894 (!RoundToStep || IsStepConst)) { 7895 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 7896 : URes.getBitWidth(); 7897 LRes = LRes.extend(BW + 1); 7898 LRes.setIsSigned(true); 7899 URes = URes.extend(BW + 1); 7900 URes.setIsSigned(true); 7901 URes -= LRes; 7902 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 7903 NeedToReorganize = NoNeedToConvert; 7904 } 7905 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 7906 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 7907 // unsigned. 7908 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 7909 !LCTy->isDependentType() && LCTy->isIntegerType()) { 7910 QualType LowerTy = Lower->getType(); 7911 QualType UpperTy = Upper->getType(); 7912 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 7913 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 7914 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 7915 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 7916 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 7917 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 7918 Upper = 7919 SemaRef 7920 .PerformImplicitConversion( 7921 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7922 CastType, Sema::AA_Converting) 7923 .get(); 7924 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 7925 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 7926 } 7927 } 7928 if (!Lower || !Upper || NewStep.isInvalid()) 7929 return nullptr; 7930 7931 ExprResult Diff; 7932 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 7933 // 1]). 7934 if (NeedToReorganize) { 7935 Diff = Lower; 7936 7937 if (RoundToStep) { 7938 // Lower - Step 7939 Diff = 7940 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 7941 if (!Diff.isUsable()) 7942 return nullptr; 7943 } 7944 7945 // Lower - Step [+ 1] 7946 if (TestIsStrictOp) 7947 Diff = SemaRef.BuildBinOp( 7948 S, DefaultLoc, BO_Add, Diff.get(), 7949 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7950 if (!Diff.isUsable()) 7951 return nullptr; 7952 7953 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7954 if (!Diff.isUsable()) 7955 return nullptr; 7956 7957 // Upper - (Lower - Step [+ 1]). 7958 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 7959 if (!Diff.isUsable()) 7960 return nullptr; 7961 } else { 7962 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 7963 7964 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 7965 // BuildBinOp already emitted error, this one is to point user to upper 7966 // and lower bound, and to tell what is passed to 'operator-'. 7967 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 7968 << Upper->getSourceRange() << Lower->getSourceRange(); 7969 return nullptr; 7970 } 7971 7972 if (!Diff.isUsable()) 7973 return nullptr; 7974 7975 // Upper - Lower [- 1] 7976 if (TestIsStrictOp) 7977 Diff = SemaRef.BuildBinOp( 7978 S, DefaultLoc, BO_Sub, Diff.get(), 7979 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7980 if (!Diff.isUsable()) 7981 return nullptr; 7982 7983 if (RoundToStep) { 7984 // Upper - Lower [- 1] + Step 7985 Diff = 7986 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 7987 if (!Diff.isUsable()) 7988 return nullptr; 7989 } 7990 } 7991 7992 // Parentheses (for dumping/debugging purposes only). 7993 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7994 if (!Diff.isUsable()) 7995 return nullptr; 7996 7997 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 7998 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 7999 if (!Diff.isUsable()) 8000 return nullptr; 8001 8002 return Diff.get(); 8003 } 8004 8005 /// Build the expression to calculate the number of iterations. 8006 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 8007 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 8008 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8009 QualType VarType = LCDecl->getType().getNonReferenceType(); 8010 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8011 !SemaRef.getLangOpts().CPlusPlus) 8012 return nullptr; 8013 Expr *LBVal = LB; 8014 Expr *UBVal = UB; 8015 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 8016 // max(LB(MinVal), LB(MaxVal)) 8017 if (InitDependOnLC) { 8018 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1]; 8019 if (!IS.MinValue || !IS.MaxValue) 8020 return nullptr; 8021 // OuterVar = Min 8022 ExprResult MinValue = 8023 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8024 if (!MinValue.isUsable()) 8025 return nullptr; 8026 8027 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8028 IS.CounterVar, MinValue.get()); 8029 if (!LBMinVal.isUsable()) 8030 return nullptr; 8031 // OuterVar = Min, LBVal 8032 LBMinVal = 8033 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 8034 if (!LBMinVal.isUsable()) 8035 return nullptr; 8036 // (OuterVar = Min, LBVal) 8037 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 8038 if (!LBMinVal.isUsable()) 8039 return nullptr; 8040 8041 // OuterVar = Max 8042 ExprResult MaxValue = 8043 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8044 if (!MaxValue.isUsable()) 8045 return nullptr; 8046 8047 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8048 IS.CounterVar, MaxValue.get()); 8049 if (!LBMaxVal.isUsable()) 8050 return nullptr; 8051 // OuterVar = Max, LBVal 8052 LBMaxVal = 8053 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 8054 if (!LBMaxVal.isUsable()) 8055 return nullptr; 8056 // (OuterVar = Max, LBVal) 8057 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 8058 if (!LBMaxVal.isUsable()) 8059 return nullptr; 8060 8061 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 8062 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 8063 if (!LBMin || !LBMax) 8064 return nullptr; 8065 // LB(MinVal) < LB(MaxVal) 8066 ExprResult MinLessMaxRes = 8067 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 8068 if (!MinLessMaxRes.isUsable()) 8069 return nullptr; 8070 Expr *MinLessMax = 8071 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 8072 if (!MinLessMax) 8073 return nullptr; 8074 if (TestIsLessOp.getValue()) { 8075 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 8076 // LB(MaxVal)) 8077 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8078 MinLessMax, LBMin, LBMax); 8079 if (!MinLB.isUsable()) 8080 return nullptr; 8081 LBVal = MinLB.get(); 8082 } else { 8083 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 8084 // LB(MaxVal)) 8085 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8086 MinLessMax, LBMax, LBMin); 8087 if (!MaxLB.isUsable()) 8088 return nullptr; 8089 LBVal = MaxLB.get(); 8090 } 8091 } 8092 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 8093 // min(UB(MinVal), UB(MaxVal)) 8094 if (CondDependOnLC) { 8095 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1]; 8096 if (!IS.MinValue || !IS.MaxValue) 8097 return nullptr; 8098 // OuterVar = Min 8099 ExprResult MinValue = 8100 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8101 if (!MinValue.isUsable()) 8102 return nullptr; 8103 8104 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8105 IS.CounterVar, MinValue.get()); 8106 if (!UBMinVal.isUsable()) 8107 return nullptr; 8108 // OuterVar = Min, UBVal 8109 UBMinVal = 8110 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 8111 if (!UBMinVal.isUsable()) 8112 return nullptr; 8113 // (OuterVar = Min, UBVal) 8114 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 8115 if (!UBMinVal.isUsable()) 8116 return nullptr; 8117 8118 // OuterVar = Max 8119 ExprResult MaxValue = 8120 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8121 if (!MaxValue.isUsable()) 8122 return nullptr; 8123 8124 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8125 IS.CounterVar, MaxValue.get()); 8126 if (!UBMaxVal.isUsable()) 8127 return nullptr; 8128 // OuterVar = Max, UBVal 8129 UBMaxVal = 8130 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 8131 if (!UBMaxVal.isUsable()) 8132 return nullptr; 8133 // (OuterVar = Max, UBVal) 8134 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 8135 if (!UBMaxVal.isUsable()) 8136 return nullptr; 8137 8138 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 8139 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 8140 if (!UBMin || !UBMax) 8141 return nullptr; 8142 // UB(MinVal) > UB(MaxVal) 8143 ExprResult MinGreaterMaxRes = 8144 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 8145 if (!MinGreaterMaxRes.isUsable()) 8146 return nullptr; 8147 Expr *MinGreaterMax = 8148 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 8149 if (!MinGreaterMax) 8150 return nullptr; 8151 if (TestIsLessOp.getValue()) { 8152 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 8153 // UB(MaxVal)) 8154 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 8155 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 8156 if (!MaxUB.isUsable()) 8157 return nullptr; 8158 UBVal = MaxUB.get(); 8159 } else { 8160 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 8161 // UB(MaxVal)) 8162 ExprResult MinUB = SemaRef.ActOnConditionalOp( 8163 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 8164 if (!MinUB.isUsable()) 8165 return nullptr; 8166 UBVal = MinUB.get(); 8167 } 8168 } 8169 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 8170 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 8171 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8172 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8173 if (!Upper || !Lower) 8174 return nullptr; 8175 8176 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8177 Step, VarType, TestIsStrictOp, 8178 /*RoundToStep=*/true, Captures); 8179 if (!Diff.isUsable()) 8180 return nullptr; 8181 8182 // OpenMP runtime requires 32-bit or 64-bit loop variables. 8183 QualType Type = Diff.get()->getType(); 8184 ASTContext &C = SemaRef.Context; 8185 bool UseVarType = VarType->hasIntegerRepresentation() && 8186 C.getTypeSize(Type) > C.getTypeSize(VarType); 8187 if (!Type->isIntegerType() || UseVarType) { 8188 unsigned NewSize = 8189 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 8190 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 8191 : Type->hasSignedIntegerRepresentation(); 8192 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 8193 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 8194 Diff = SemaRef.PerformImplicitConversion( 8195 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 8196 if (!Diff.isUsable()) 8197 return nullptr; 8198 } 8199 } 8200 if (LimitedType) { 8201 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 8202 if (NewSize != C.getTypeSize(Type)) { 8203 if (NewSize < C.getTypeSize(Type)) { 8204 assert(NewSize == 64 && "incorrect loop var size"); 8205 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 8206 << InitSrcRange << ConditionSrcRange; 8207 } 8208 QualType NewType = C.getIntTypeForBitwidth( 8209 NewSize, Type->hasSignedIntegerRepresentation() || 8210 C.getTypeSize(Type) < NewSize); 8211 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 8212 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 8213 Sema::AA_Converting, true); 8214 if (!Diff.isUsable()) 8215 return nullptr; 8216 } 8217 } 8218 } 8219 8220 return Diff.get(); 8221 } 8222 8223 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 8224 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8225 // Do not build for iterators, they cannot be used in non-rectangular loop 8226 // nests. 8227 if (LCDecl->getType()->isRecordType()) 8228 return std::make_pair(nullptr, nullptr); 8229 // If we subtract, the min is in the condition, otherwise the min is in the 8230 // init value. 8231 Expr *MinExpr = nullptr; 8232 Expr *MaxExpr = nullptr; 8233 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 8234 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 8235 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 8236 : CondDependOnLC.hasValue(); 8237 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 8238 : InitDependOnLC.hasValue(); 8239 Expr *Lower = 8240 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8241 Expr *Upper = 8242 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8243 if (!Upper || !Lower) 8244 return std::make_pair(nullptr, nullptr); 8245 8246 if (TestIsLessOp.getValue()) 8247 MinExpr = Lower; 8248 else 8249 MaxExpr = Upper; 8250 8251 // Build minimum/maximum value based on number of iterations. 8252 QualType VarType = LCDecl->getType().getNonReferenceType(); 8253 8254 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8255 Step, VarType, TestIsStrictOp, 8256 /*RoundToStep=*/false, Captures); 8257 if (!Diff.isUsable()) 8258 return std::make_pair(nullptr, nullptr); 8259 8260 // ((Upper - Lower [- 1]) / Step) * Step 8261 // Parentheses (for dumping/debugging purposes only). 8262 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8263 if (!Diff.isUsable()) 8264 return std::make_pair(nullptr, nullptr); 8265 8266 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8267 if (!NewStep.isUsable()) 8268 return std::make_pair(nullptr, nullptr); 8269 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 8270 if (!Diff.isUsable()) 8271 return std::make_pair(nullptr, nullptr); 8272 8273 // Parentheses (for dumping/debugging purposes only). 8274 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8275 if (!Diff.isUsable()) 8276 return std::make_pair(nullptr, nullptr); 8277 8278 // Convert to the ptrdiff_t, if original type is pointer. 8279 if (VarType->isAnyPointerType() && 8280 !SemaRef.Context.hasSameType( 8281 Diff.get()->getType(), 8282 SemaRef.Context.getUnsignedPointerDiffType())) { 8283 Diff = SemaRef.PerformImplicitConversion( 8284 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 8285 Sema::AA_Converting, /*AllowExplicit=*/true); 8286 } 8287 if (!Diff.isUsable()) 8288 return std::make_pair(nullptr, nullptr); 8289 8290 if (TestIsLessOp.getValue()) { 8291 // MinExpr = Lower; 8292 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 8293 Diff = SemaRef.BuildBinOp( 8294 S, DefaultLoc, BO_Add, 8295 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 8296 Diff.get()); 8297 if (!Diff.isUsable()) 8298 return std::make_pair(nullptr, nullptr); 8299 } else { 8300 // MaxExpr = Upper; 8301 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 8302 Diff = SemaRef.BuildBinOp( 8303 S, DefaultLoc, BO_Sub, 8304 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8305 Diff.get()); 8306 if (!Diff.isUsable()) 8307 return std::make_pair(nullptr, nullptr); 8308 } 8309 8310 // Convert to the original type. 8311 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 8312 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 8313 Sema::AA_Converting, 8314 /*AllowExplicit=*/true); 8315 if (!Diff.isUsable()) 8316 return std::make_pair(nullptr, nullptr); 8317 8318 Sema::TentativeAnalysisScope Trap(SemaRef); 8319 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 8320 if (!Diff.isUsable()) 8321 return std::make_pair(nullptr, nullptr); 8322 8323 if (TestIsLessOp.getValue()) 8324 MaxExpr = Diff.get(); 8325 else 8326 MinExpr = Diff.get(); 8327 8328 return std::make_pair(MinExpr, MaxExpr); 8329 } 8330 8331 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 8332 if (InitDependOnLC || CondDependOnLC) 8333 return Condition; 8334 return nullptr; 8335 } 8336 8337 Expr *OpenMPIterationSpaceChecker::buildPreCond( 8338 Scope *S, Expr *Cond, 8339 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8340 // Do not build a precondition when the condition/initialization is dependent 8341 // to prevent pessimistic early loop exit. 8342 // TODO: this can be improved by calculating min/max values but not sure that 8343 // it will be very effective. 8344 if (CondDependOnLC || InitDependOnLC) 8345 return SemaRef.PerformImplicitConversion( 8346 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 8347 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8348 /*AllowExplicit=*/true).get(); 8349 8350 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 8351 Sema::TentativeAnalysisScope Trap(SemaRef); 8352 8353 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 8354 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 8355 if (!NewLB.isUsable() || !NewUB.isUsable()) 8356 return nullptr; 8357 8358 ExprResult CondExpr = 8359 SemaRef.BuildBinOp(S, DefaultLoc, 8360 TestIsLessOp.getValue() ? 8361 (TestIsStrictOp ? BO_LT : BO_LE) : 8362 (TestIsStrictOp ? BO_GT : BO_GE), 8363 NewLB.get(), NewUB.get()); 8364 if (CondExpr.isUsable()) { 8365 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 8366 SemaRef.Context.BoolTy)) 8367 CondExpr = SemaRef.PerformImplicitConversion( 8368 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8369 /*AllowExplicit=*/true); 8370 } 8371 8372 // Otherwise use original loop condition and evaluate it in runtime. 8373 return CondExpr.isUsable() ? CondExpr.get() : Cond; 8374 } 8375 8376 /// Build reference expression to the counter be used for codegen. 8377 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 8378 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 8379 DSAStackTy &DSA) const { 8380 auto *VD = dyn_cast<VarDecl>(LCDecl); 8381 if (!VD) { 8382 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 8383 DeclRefExpr *Ref = buildDeclRefExpr( 8384 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 8385 const DSAStackTy::DSAVarData Data = 8386 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 8387 // If the loop control decl is explicitly marked as private, do not mark it 8388 // as captured again. 8389 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 8390 Captures.insert(std::make_pair(LCRef, Ref)); 8391 return Ref; 8392 } 8393 return cast<DeclRefExpr>(LCRef); 8394 } 8395 8396 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 8397 if (LCDecl && !LCDecl->isInvalidDecl()) { 8398 QualType Type = LCDecl->getType().getNonReferenceType(); 8399 VarDecl *PrivateVar = buildVarDecl( 8400 SemaRef, DefaultLoc, Type, LCDecl->getName(), 8401 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 8402 isa<VarDecl>(LCDecl) 8403 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 8404 : nullptr); 8405 if (PrivateVar->isInvalidDecl()) 8406 return nullptr; 8407 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 8408 } 8409 return nullptr; 8410 } 8411 8412 /// Build initialization of the counter to be used for codegen. 8413 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 8414 8415 /// Build step of the counter be used for codegen. 8416 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 8417 8418 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 8419 Scope *S, Expr *Counter, 8420 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 8421 Expr *Inc, OverloadedOperatorKind OOK) { 8422 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 8423 if (!Cnt) 8424 return nullptr; 8425 if (Inc) { 8426 assert((OOK == OO_Plus || OOK == OO_Minus) && 8427 "Expected only + or - operations for depend clauses."); 8428 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 8429 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 8430 if (!Cnt) 8431 return nullptr; 8432 } 8433 QualType VarType = LCDecl->getType().getNonReferenceType(); 8434 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8435 !SemaRef.getLangOpts().CPlusPlus) 8436 return nullptr; 8437 // Upper - Lower 8438 Expr *Upper = TestIsLessOp.getValue() 8439 ? Cnt 8440 : tryBuildCapture(SemaRef, LB, Captures).get(); 8441 Expr *Lower = TestIsLessOp.getValue() 8442 ? tryBuildCapture(SemaRef, LB, Captures).get() 8443 : Cnt; 8444 if (!Upper || !Lower) 8445 return nullptr; 8446 8447 ExprResult Diff = calculateNumIters( 8448 SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 8449 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures); 8450 if (!Diff.isUsable()) 8451 return nullptr; 8452 8453 return Diff.get(); 8454 } 8455 } // namespace 8456 8457 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 8458 assert(getLangOpts().OpenMP && "OpenMP is not active."); 8459 assert(Init && "Expected loop in canonical form."); 8460 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 8461 if (AssociatedLoops > 0 && 8462 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 8463 DSAStack->loopStart(); 8464 OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true, 8465 *DSAStack, ForLoc); 8466 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 8467 if (ValueDecl *D = ISC.getLoopDecl()) { 8468 auto *VD = dyn_cast<VarDecl>(D); 8469 DeclRefExpr *PrivateRef = nullptr; 8470 if (!VD) { 8471 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 8472 VD = Private; 8473 } else { 8474 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 8475 /*WithInit=*/false); 8476 VD = cast<VarDecl>(PrivateRef->getDecl()); 8477 } 8478 } 8479 DSAStack->addLoopControlVariable(D, VD); 8480 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 8481 if (LD != D->getCanonicalDecl()) { 8482 DSAStack->resetPossibleLoopCounter(); 8483 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 8484 MarkDeclarationsReferencedInExpr( 8485 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 8486 Var->getType().getNonLValueExprType(Context), 8487 ForLoc, /*RefersToCapture=*/true)); 8488 } 8489 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8490 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 8491 // Referenced in a Construct, C/C++]. The loop iteration variable in the 8492 // associated for-loop of a simd construct with just one associated 8493 // for-loop may be listed in a linear clause with a constant-linear-step 8494 // that is the increment of the associated for-loop. The loop iteration 8495 // variable(s) in the associated for-loop(s) of a for or parallel for 8496 // construct may be listed in a private or lastprivate clause. 8497 DSAStackTy::DSAVarData DVar = 8498 DSAStack->getTopDSA(D, /*FromParent=*/false); 8499 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 8500 // is declared in the loop and it is predetermined as a private. 8501 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 8502 OpenMPClauseKind PredeterminedCKind = 8503 isOpenMPSimdDirective(DKind) 8504 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 8505 : OMPC_private; 8506 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8507 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 8508 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 8509 DVar.CKind != OMPC_private))) || 8510 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 8511 DKind == OMPD_master_taskloop || 8512 DKind == OMPD_parallel_master_taskloop || 8513 isOpenMPDistributeDirective(DKind)) && 8514 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8515 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 8516 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 8517 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 8518 << getOpenMPClauseName(DVar.CKind) 8519 << getOpenMPDirectiveName(DKind) 8520 << getOpenMPClauseName(PredeterminedCKind); 8521 if (DVar.RefExpr == nullptr) 8522 DVar.CKind = PredeterminedCKind; 8523 reportOriginalDsa(*this, DSAStack, D, DVar, 8524 /*IsLoopIterVar=*/true); 8525 } else if (LoopDeclRefExpr) { 8526 // Make the loop iteration variable private (for worksharing 8527 // constructs), linear (for simd directives with the only one 8528 // associated loop) or lastprivate (for simd directives with several 8529 // collapsed or ordered loops). 8530 if (DVar.CKind == OMPC_unknown) 8531 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 8532 PrivateRef); 8533 } 8534 } 8535 } 8536 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 8537 } 8538 } 8539 8540 /// Called on a for stmt to check and extract its iteration space 8541 /// for further processing (such as collapsing). 8542 static bool checkOpenMPIterationSpace( 8543 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 8544 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 8545 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 8546 Expr *OrderedLoopCountExpr, 8547 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8548 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 8549 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8550 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind); 8551 // OpenMP [2.9.1, Canonical Loop Form] 8552 // for (init-expr; test-expr; incr-expr) structured-block 8553 // for (range-decl: range-expr) structured-block 8554 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S)) 8555 S = CanonLoop->getLoopStmt(); 8556 auto *For = dyn_cast_or_null<ForStmt>(S); 8557 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 8558 // Ranged for is supported only in OpenMP 5.0. 8559 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 8560 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 8561 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 8562 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 8563 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 8564 if (TotalNestedLoopCount > 1) { 8565 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 8566 SemaRef.Diag(DSA.getConstructLoc(), 8567 diag::note_omp_collapse_ordered_expr) 8568 << 2 << CollapseLoopCountExpr->getSourceRange() 8569 << OrderedLoopCountExpr->getSourceRange(); 8570 else if (CollapseLoopCountExpr) 8571 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8572 diag::note_omp_collapse_ordered_expr) 8573 << 0 << CollapseLoopCountExpr->getSourceRange(); 8574 else 8575 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8576 diag::note_omp_collapse_ordered_expr) 8577 << 1 << OrderedLoopCountExpr->getSourceRange(); 8578 } 8579 return true; 8580 } 8581 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 8582 "No loop body."); 8583 8584 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA, 8585 For ? For->getForLoc() : CXXFor->getForLoc()); 8586 8587 // Check init. 8588 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 8589 if (ISC.checkAndSetInit(Init)) 8590 return true; 8591 8592 bool HasErrors = false; 8593 8594 // Check loop variable's type. 8595 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 8596 // OpenMP [2.6, Canonical Loop Form] 8597 // Var is one of the following: 8598 // A variable of signed or unsigned integer type. 8599 // For C++, a variable of a random access iterator type. 8600 // For C, a variable of a pointer type. 8601 QualType VarType = LCDecl->getType().getNonReferenceType(); 8602 if (!VarType->isDependentType() && !VarType->isIntegerType() && 8603 !VarType->isPointerType() && 8604 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 8605 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 8606 << SemaRef.getLangOpts().CPlusPlus; 8607 HasErrors = true; 8608 } 8609 8610 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 8611 // a Construct 8612 // The loop iteration variable(s) in the associated for-loop(s) of a for or 8613 // parallel for construct is (are) private. 8614 // The loop iteration variable in the associated for-loop of a simd 8615 // construct with just one associated for-loop is linear with a 8616 // constant-linear-step that is the increment of the associated for-loop. 8617 // Exclude loop var from the list of variables with implicitly defined data 8618 // sharing attributes. 8619 VarsWithImplicitDSA.erase(LCDecl); 8620 8621 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 8622 8623 // Check test-expr. 8624 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 8625 8626 // Check incr-expr. 8627 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 8628 } 8629 8630 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 8631 return HasErrors; 8632 8633 // Build the loop's iteration space representation. 8634 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 8635 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 8636 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 8637 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 8638 (isOpenMPWorksharingDirective(DKind) || 8639 isOpenMPTaskLoopDirective(DKind) || 8640 isOpenMPDistributeDirective(DKind) || 8641 isOpenMPLoopTransformationDirective(DKind)), 8642 Captures); 8643 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 8644 ISC.buildCounterVar(Captures, DSA); 8645 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 8646 ISC.buildPrivateCounterVar(); 8647 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 8648 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 8649 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 8650 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 8651 ISC.getConditionSrcRange(); 8652 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 8653 ISC.getIncrementSrcRange(); 8654 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 8655 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 8656 ISC.isStrictTestOp(); 8657 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 8658 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 8659 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 8660 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 8661 ISC.buildFinalCondition(DSA.getCurScope()); 8662 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 8663 ISC.doesInitDependOnLC(); 8664 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 8665 ISC.doesCondDependOnLC(); 8666 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 8667 ISC.getLoopDependentIdx(); 8668 8669 HasErrors |= 8670 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 8671 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 8672 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 8673 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 8674 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 8675 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 8676 if (!HasErrors && DSA.isOrderedRegion()) { 8677 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 8678 if (CurrentNestedLoopCount < 8679 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 8680 DSA.getOrderedRegionParam().second->setLoopNumIterations( 8681 CurrentNestedLoopCount, 8682 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 8683 DSA.getOrderedRegionParam().second->setLoopCounter( 8684 CurrentNestedLoopCount, 8685 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 8686 } 8687 } 8688 for (auto &Pair : DSA.getDoacrossDependClauses()) { 8689 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 8690 // Erroneous case - clause has some problems. 8691 continue; 8692 } 8693 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 8694 Pair.second.size() <= CurrentNestedLoopCount) { 8695 // Erroneous case - clause has some problems. 8696 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 8697 continue; 8698 } 8699 Expr *CntValue; 8700 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 8701 CntValue = ISC.buildOrderedLoopData( 8702 DSA.getCurScope(), 8703 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8704 Pair.first->getDependencyLoc()); 8705 else 8706 CntValue = ISC.buildOrderedLoopData( 8707 DSA.getCurScope(), 8708 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8709 Pair.first->getDependencyLoc(), 8710 Pair.second[CurrentNestedLoopCount].first, 8711 Pair.second[CurrentNestedLoopCount].second); 8712 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 8713 } 8714 } 8715 8716 return HasErrors; 8717 } 8718 8719 /// Build 'VarRef = Start. 8720 static ExprResult 8721 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8722 ExprResult Start, bool IsNonRectangularLB, 8723 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8724 // Build 'VarRef = Start. 8725 ExprResult NewStart = IsNonRectangularLB 8726 ? Start.get() 8727 : tryBuildCapture(SemaRef, Start.get(), Captures); 8728 if (!NewStart.isUsable()) 8729 return ExprError(); 8730 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 8731 VarRef.get()->getType())) { 8732 NewStart = SemaRef.PerformImplicitConversion( 8733 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 8734 /*AllowExplicit=*/true); 8735 if (!NewStart.isUsable()) 8736 return ExprError(); 8737 } 8738 8739 ExprResult Init = 8740 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8741 return Init; 8742 } 8743 8744 /// Build 'VarRef = Start + Iter * Step'. 8745 static ExprResult buildCounterUpdate( 8746 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8747 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8748 bool IsNonRectangularLB, 8749 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8750 // Add parentheses (for debugging purposes only). 8751 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8752 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8753 !Step.isUsable()) 8754 return ExprError(); 8755 8756 ExprResult NewStep = Step; 8757 if (Captures) 8758 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8759 if (NewStep.isInvalid()) 8760 return ExprError(); 8761 ExprResult Update = 8762 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 8763 if (!Update.isUsable()) 8764 return ExprError(); 8765 8766 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 8767 // 'VarRef = Start (+|-) Iter * Step'. 8768 if (!Start.isUsable()) 8769 return ExprError(); 8770 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 8771 if (!NewStart.isUsable()) 8772 return ExprError(); 8773 if (Captures && !IsNonRectangularLB) 8774 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 8775 if (NewStart.isInvalid()) 8776 return ExprError(); 8777 8778 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 8779 ExprResult SavedUpdate = Update; 8780 ExprResult UpdateVal; 8781 if (VarRef.get()->getType()->isOverloadableType() || 8782 NewStart.get()->getType()->isOverloadableType() || 8783 Update.get()->getType()->isOverloadableType()) { 8784 Sema::TentativeAnalysisScope Trap(SemaRef); 8785 8786 Update = 8787 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8788 if (Update.isUsable()) { 8789 UpdateVal = 8790 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 8791 VarRef.get(), SavedUpdate.get()); 8792 if (UpdateVal.isUsable()) { 8793 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 8794 UpdateVal.get()); 8795 } 8796 } 8797 } 8798 8799 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 8800 if (!Update.isUsable() || !UpdateVal.isUsable()) { 8801 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 8802 NewStart.get(), SavedUpdate.get()); 8803 if (!Update.isUsable()) 8804 return ExprError(); 8805 8806 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 8807 VarRef.get()->getType())) { 8808 Update = SemaRef.PerformImplicitConversion( 8809 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 8810 if (!Update.isUsable()) 8811 return ExprError(); 8812 } 8813 8814 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 8815 } 8816 return Update; 8817 } 8818 8819 /// Convert integer expression \a E to make it have at least \a Bits 8820 /// bits. 8821 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 8822 if (E == nullptr) 8823 return ExprError(); 8824 ASTContext &C = SemaRef.Context; 8825 QualType OldType = E->getType(); 8826 unsigned HasBits = C.getTypeSize(OldType); 8827 if (HasBits >= Bits) 8828 return ExprResult(E); 8829 // OK to convert to signed, because new type has more bits than old. 8830 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 8831 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 8832 true); 8833 } 8834 8835 /// Check if the given expression \a E is a constant integer that fits 8836 /// into \a Bits bits. 8837 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 8838 if (E == nullptr) 8839 return false; 8840 if (Optional<llvm::APSInt> Result = 8841 E->getIntegerConstantExpr(SemaRef.Context)) 8842 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8843 return false; 8844 } 8845 8846 /// Build preinits statement for the given declarations. 8847 static Stmt *buildPreInits(ASTContext &Context, 8848 MutableArrayRef<Decl *> PreInits) { 8849 if (!PreInits.empty()) { 8850 return new (Context) DeclStmt( 8851 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8852 SourceLocation(), SourceLocation()); 8853 } 8854 return nullptr; 8855 } 8856 8857 /// Build preinits statement for the given declarations. 8858 static Stmt * 8859 buildPreInits(ASTContext &Context, 8860 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8861 if (!Captures.empty()) { 8862 SmallVector<Decl *, 16> PreInits; 8863 for (const auto &Pair : Captures) 8864 PreInits.push_back(Pair.second->getDecl()); 8865 return buildPreInits(Context, PreInits); 8866 } 8867 return nullptr; 8868 } 8869 8870 /// Build postupdate expression for the given list of postupdates expressions. 8871 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 8872 Expr *PostUpdate = nullptr; 8873 if (!PostUpdates.empty()) { 8874 for (Expr *E : PostUpdates) { 8875 Expr *ConvE = S.BuildCStyleCastExpr( 8876 E->getExprLoc(), 8877 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 8878 E->getExprLoc(), E) 8879 .get(); 8880 PostUpdate = PostUpdate 8881 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 8882 PostUpdate, ConvE) 8883 .get() 8884 : ConvE; 8885 } 8886 } 8887 return PostUpdate; 8888 } 8889 8890 /// Called on a for stmt to check itself and nested loops (if any). 8891 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 8892 /// number of collapsed loops otherwise. 8893 static unsigned 8894 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 8895 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 8896 DSAStackTy &DSA, 8897 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8898 OMPLoopBasedDirective::HelperExprs &Built) { 8899 unsigned NestedLoopCount = 1; 8900 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) && 8901 !isOpenMPLoopTransformationDirective(DKind); 8902 8903 if (CollapseLoopCountExpr) { 8904 // Found 'collapse' clause - calculate collapse number. 8905 Expr::EvalResult Result; 8906 if (!CollapseLoopCountExpr->isValueDependent() && 8907 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 8908 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 8909 } else { 8910 Built.clear(/*Size=*/1); 8911 return 1; 8912 } 8913 } 8914 unsigned OrderedLoopCount = 1; 8915 if (OrderedLoopCountExpr) { 8916 // Found 'ordered' clause - calculate collapse number. 8917 Expr::EvalResult EVResult; 8918 if (!OrderedLoopCountExpr->isValueDependent() && 8919 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 8920 SemaRef.getASTContext())) { 8921 llvm::APSInt Result = EVResult.Val.getInt(); 8922 if (Result.getLimitedValue() < NestedLoopCount) { 8923 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8924 diag::err_omp_wrong_ordered_loop_count) 8925 << OrderedLoopCountExpr->getSourceRange(); 8926 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8927 diag::note_collapse_loop_count) 8928 << CollapseLoopCountExpr->getSourceRange(); 8929 } 8930 OrderedLoopCount = Result.getLimitedValue(); 8931 } else { 8932 Built.clear(/*Size=*/1); 8933 return 1; 8934 } 8935 } 8936 // This is helper routine for loop directives (e.g., 'for', 'simd', 8937 // 'for simd', etc.). 8938 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8939 unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount); 8940 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops); 8941 if (!OMPLoopBasedDirective::doForAllLoops( 8942 AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)), 8943 SupportsNonPerfectlyNested, NumLoops, 8944 [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount, 8945 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA, 8946 &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) { 8947 if (checkOpenMPIterationSpace( 8948 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8949 NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr, 8950 VarsWithImplicitDSA, IterSpaces, Captures)) 8951 return true; 8952 if (Cnt > 0 && Cnt >= NestedLoopCount && 8953 IterSpaces[Cnt].CounterVar) { 8954 // Handle initialization of captured loop iterator variables. 8955 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 8956 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 8957 Captures[DRE] = DRE; 8958 } 8959 } 8960 return false; 8961 })) 8962 return 0; 8963 8964 Built.clear(/* size */ NestedLoopCount); 8965 8966 if (SemaRef.CurContext->isDependentContext()) 8967 return NestedLoopCount; 8968 8969 // An example of what is generated for the following code: 8970 // 8971 // #pragma omp simd collapse(2) ordered(2) 8972 // for (i = 0; i < NI; ++i) 8973 // for (k = 0; k < NK; ++k) 8974 // for (j = J0; j < NJ; j+=2) { 8975 // <loop body> 8976 // } 8977 // 8978 // We generate the code below. 8979 // Note: the loop body may be outlined in CodeGen. 8980 // Note: some counters may be C++ classes, operator- is used to find number of 8981 // iterations and operator+= to calculate counter value. 8982 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 8983 // or i64 is currently supported). 8984 // 8985 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 8986 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 8987 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 8988 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 8989 // // similar updates for vars in clauses (e.g. 'linear') 8990 // <loop body (using local i and j)> 8991 // } 8992 // i = NI; // assign final values of counters 8993 // j = NJ; 8994 // 8995 8996 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 8997 // the iteration counts of the collapsed for loops. 8998 // Precondition tests if there is at least one iteration (all conditions are 8999 // true). 9000 auto PreCond = ExprResult(IterSpaces[0].PreCond); 9001 Expr *N0 = IterSpaces[0].NumIterations; 9002 ExprResult LastIteration32 = 9003 widenIterationCount(/*Bits=*/32, 9004 SemaRef 9005 .PerformImplicitConversion( 9006 N0->IgnoreImpCasts(), N0->getType(), 9007 Sema::AA_Converting, /*AllowExplicit=*/true) 9008 .get(), 9009 SemaRef); 9010 ExprResult LastIteration64 = widenIterationCount( 9011 /*Bits=*/64, 9012 SemaRef 9013 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 9014 Sema::AA_Converting, 9015 /*AllowExplicit=*/true) 9016 .get(), 9017 SemaRef); 9018 9019 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 9020 return NestedLoopCount; 9021 9022 ASTContext &C = SemaRef.Context; 9023 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 9024 9025 Scope *CurScope = DSA.getCurScope(); 9026 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 9027 if (PreCond.isUsable()) { 9028 PreCond = 9029 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 9030 PreCond.get(), IterSpaces[Cnt].PreCond); 9031 } 9032 Expr *N = IterSpaces[Cnt].NumIterations; 9033 SourceLocation Loc = N->getExprLoc(); 9034 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 9035 if (LastIteration32.isUsable()) 9036 LastIteration32 = SemaRef.BuildBinOp( 9037 CurScope, Loc, BO_Mul, LastIteration32.get(), 9038 SemaRef 9039 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9040 Sema::AA_Converting, 9041 /*AllowExplicit=*/true) 9042 .get()); 9043 if (LastIteration64.isUsable()) 9044 LastIteration64 = SemaRef.BuildBinOp( 9045 CurScope, Loc, BO_Mul, LastIteration64.get(), 9046 SemaRef 9047 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9048 Sema::AA_Converting, 9049 /*AllowExplicit=*/true) 9050 .get()); 9051 } 9052 9053 // Choose either the 32-bit or 64-bit version. 9054 ExprResult LastIteration = LastIteration64; 9055 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 9056 (LastIteration32.isUsable() && 9057 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 9058 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 9059 fitsInto( 9060 /*Bits=*/32, 9061 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 9062 LastIteration64.get(), SemaRef)))) 9063 LastIteration = LastIteration32; 9064 QualType VType = LastIteration.get()->getType(); 9065 QualType RealVType = VType; 9066 QualType StrideVType = VType; 9067 if (isOpenMPTaskLoopDirective(DKind)) { 9068 VType = 9069 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 9070 StrideVType = 9071 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 9072 } 9073 9074 if (!LastIteration.isUsable()) 9075 return 0; 9076 9077 // Save the number of iterations. 9078 ExprResult NumIterations = LastIteration; 9079 { 9080 LastIteration = SemaRef.BuildBinOp( 9081 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 9082 LastIteration.get(), 9083 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9084 if (!LastIteration.isUsable()) 9085 return 0; 9086 } 9087 9088 // Calculate the last iteration number beforehand instead of doing this on 9089 // each iteration. Do not do this if the number of iterations may be kfold-ed. 9090 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 9091 ExprResult CalcLastIteration; 9092 if (!IsConstant) { 9093 ExprResult SaveRef = 9094 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 9095 LastIteration = SaveRef; 9096 9097 // Prepare SaveRef + 1. 9098 NumIterations = SemaRef.BuildBinOp( 9099 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 9100 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9101 if (!NumIterations.isUsable()) 9102 return 0; 9103 } 9104 9105 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 9106 9107 // Build variables passed into runtime, necessary for worksharing directives. 9108 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 9109 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9110 isOpenMPDistributeDirective(DKind) || 9111 isOpenMPLoopTransformationDirective(DKind)) { 9112 // Lower bound variable, initialized with zero. 9113 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 9114 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 9115 SemaRef.AddInitializerToDecl(LBDecl, 9116 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9117 /*DirectInit*/ false); 9118 9119 // Upper bound variable, initialized with last iteration number. 9120 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 9121 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 9122 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 9123 /*DirectInit*/ false); 9124 9125 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 9126 // This will be used to implement clause 'lastprivate'. 9127 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 9128 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 9129 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 9130 SemaRef.AddInitializerToDecl(ILDecl, 9131 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9132 /*DirectInit*/ false); 9133 9134 // Stride variable returned by runtime (we initialize it to 1 by default). 9135 VarDecl *STDecl = 9136 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 9137 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 9138 SemaRef.AddInitializerToDecl(STDecl, 9139 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 9140 /*DirectInit*/ false); 9141 9142 // Build expression: UB = min(UB, LastIteration) 9143 // It is necessary for CodeGen of directives with static scheduling. 9144 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 9145 UB.get(), LastIteration.get()); 9146 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9147 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 9148 LastIteration.get(), UB.get()); 9149 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 9150 CondOp.get()); 9151 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 9152 9153 // If we have a combined directive that combines 'distribute', 'for' or 9154 // 'simd' we need to be able to access the bounds of the schedule of the 9155 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 9156 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 9157 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9158 // Lower bound variable, initialized with zero. 9159 VarDecl *CombLBDecl = 9160 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 9161 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 9162 SemaRef.AddInitializerToDecl( 9163 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9164 /*DirectInit*/ false); 9165 9166 // Upper bound variable, initialized with last iteration number. 9167 VarDecl *CombUBDecl = 9168 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 9169 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 9170 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 9171 /*DirectInit*/ false); 9172 9173 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 9174 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 9175 ExprResult CombCondOp = 9176 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 9177 LastIteration.get(), CombUB.get()); 9178 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 9179 CombCondOp.get()); 9180 CombEUB = 9181 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 9182 9183 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 9184 // We expect to have at least 2 more parameters than the 'parallel' 9185 // directive does - the lower and upper bounds of the previous schedule. 9186 assert(CD->getNumParams() >= 4 && 9187 "Unexpected number of parameters in loop combined directive"); 9188 9189 // Set the proper type for the bounds given what we learned from the 9190 // enclosed loops. 9191 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 9192 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 9193 9194 // Previous lower and upper bounds are obtained from the region 9195 // parameters. 9196 PrevLB = 9197 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 9198 PrevUB = 9199 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 9200 } 9201 } 9202 9203 // Build the iteration variable and its initialization before loop. 9204 ExprResult IV; 9205 ExprResult Init, CombInit; 9206 { 9207 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 9208 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 9209 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 9210 isOpenMPTaskLoopDirective(DKind) || 9211 isOpenMPDistributeDirective(DKind) || 9212 isOpenMPLoopTransformationDirective(DKind)) 9213 ? LB.get() 9214 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9215 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 9216 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 9217 9218 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9219 Expr *CombRHS = 9220 (isOpenMPWorksharingDirective(DKind) || 9221 isOpenMPTaskLoopDirective(DKind) || 9222 isOpenMPDistributeDirective(DKind)) 9223 ? CombLB.get() 9224 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9225 CombInit = 9226 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 9227 CombInit = 9228 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 9229 } 9230 } 9231 9232 bool UseStrictCompare = 9233 RealVType->hasUnsignedIntegerRepresentation() && 9234 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 9235 return LIS.IsStrictCompare; 9236 }); 9237 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 9238 // unsigned IV)) for worksharing loops. 9239 SourceLocation CondLoc = AStmt->getBeginLoc(); 9240 Expr *BoundUB = UB.get(); 9241 if (UseStrictCompare) { 9242 BoundUB = 9243 SemaRef 9244 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 9245 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9246 .get(); 9247 BoundUB = 9248 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 9249 } 9250 ExprResult Cond = 9251 (isOpenMPWorksharingDirective(DKind) || 9252 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || 9253 isOpenMPLoopTransformationDirective(DKind)) 9254 ? SemaRef.BuildBinOp(CurScope, CondLoc, 9255 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 9256 BoundUB) 9257 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9258 NumIterations.get()); 9259 ExprResult CombDistCond; 9260 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9261 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9262 NumIterations.get()); 9263 } 9264 9265 ExprResult CombCond; 9266 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9267 Expr *BoundCombUB = CombUB.get(); 9268 if (UseStrictCompare) { 9269 BoundCombUB = 9270 SemaRef 9271 .BuildBinOp( 9272 CurScope, CondLoc, BO_Add, BoundCombUB, 9273 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9274 .get(); 9275 BoundCombUB = 9276 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 9277 .get(); 9278 } 9279 CombCond = 9280 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9281 IV.get(), BoundCombUB); 9282 } 9283 // Loop increment (IV = IV + 1) 9284 SourceLocation IncLoc = AStmt->getBeginLoc(); 9285 ExprResult Inc = 9286 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 9287 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 9288 if (!Inc.isUsable()) 9289 return 0; 9290 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 9291 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 9292 if (!Inc.isUsable()) 9293 return 0; 9294 9295 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 9296 // Used for directives with static scheduling. 9297 // In combined construct, add combined version that use CombLB and CombUB 9298 // base variables for the update 9299 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 9300 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9301 isOpenMPDistributeDirective(DKind) || 9302 isOpenMPLoopTransformationDirective(DKind)) { 9303 // LB + ST 9304 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 9305 if (!NextLB.isUsable()) 9306 return 0; 9307 // LB = LB + ST 9308 NextLB = 9309 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 9310 NextLB = 9311 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 9312 if (!NextLB.isUsable()) 9313 return 0; 9314 // UB + ST 9315 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 9316 if (!NextUB.isUsable()) 9317 return 0; 9318 // UB = UB + ST 9319 NextUB = 9320 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 9321 NextUB = 9322 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 9323 if (!NextUB.isUsable()) 9324 return 0; 9325 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9326 CombNextLB = 9327 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 9328 if (!NextLB.isUsable()) 9329 return 0; 9330 // LB = LB + ST 9331 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 9332 CombNextLB.get()); 9333 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 9334 /*DiscardedValue*/ false); 9335 if (!CombNextLB.isUsable()) 9336 return 0; 9337 // UB + ST 9338 CombNextUB = 9339 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 9340 if (!CombNextUB.isUsable()) 9341 return 0; 9342 // UB = UB + ST 9343 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 9344 CombNextUB.get()); 9345 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 9346 /*DiscardedValue*/ false); 9347 if (!CombNextUB.isUsable()) 9348 return 0; 9349 } 9350 } 9351 9352 // Create increment expression for distribute loop when combined in a same 9353 // directive with for as IV = IV + ST; ensure upper bound expression based 9354 // on PrevUB instead of NumIterations - used to implement 'for' when found 9355 // in combination with 'distribute', like in 'distribute parallel for' 9356 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 9357 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 9358 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9359 DistCond = SemaRef.BuildBinOp( 9360 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 9361 assert(DistCond.isUsable() && "distribute cond expr was not built"); 9362 9363 DistInc = 9364 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 9365 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9366 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 9367 DistInc.get()); 9368 DistInc = 9369 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 9370 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9371 9372 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 9373 // construct 9374 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 9375 ExprResult IsUBGreater = 9376 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 9377 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9378 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 9379 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 9380 CondOp.get()); 9381 PrevEUB = 9382 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 9383 9384 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 9385 // parallel for is in combination with a distribute directive with 9386 // schedule(static, 1) 9387 Expr *BoundPrevUB = PrevUB.get(); 9388 if (UseStrictCompare) { 9389 BoundPrevUB = 9390 SemaRef 9391 .BuildBinOp( 9392 CurScope, CondLoc, BO_Add, BoundPrevUB, 9393 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9394 .get(); 9395 BoundPrevUB = 9396 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 9397 .get(); 9398 } 9399 ParForInDistCond = 9400 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9401 IV.get(), BoundPrevUB); 9402 } 9403 9404 // Build updates and final values of the loop counters. 9405 bool HasErrors = false; 9406 Built.Counters.resize(NestedLoopCount); 9407 Built.Inits.resize(NestedLoopCount); 9408 Built.Updates.resize(NestedLoopCount); 9409 Built.Finals.resize(NestedLoopCount); 9410 Built.DependentCounters.resize(NestedLoopCount); 9411 Built.DependentInits.resize(NestedLoopCount); 9412 Built.FinalsConditions.resize(NestedLoopCount); 9413 { 9414 // We implement the following algorithm for obtaining the 9415 // original loop iteration variable values based on the 9416 // value of the collapsed loop iteration variable IV. 9417 // 9418 // Let n+1 be the number of collapsed loops in the nest. 9419 // Iteration variables (I0, I1, .... In) 9420 // Iteration counts (N0, N1, ... Nn) 9421 // 9422 // Acc = IV; 9423 // 9424 // To compute Ik for loop k, 0 <= k <= n, generate: 9425 // Prod = N(k+1) * N(k+2) * ... * Nn; 9426 // Ik = Acc / Prod; 9427 // Acc -= Ik * Prod; 9428 // 9429 ExprResult Acc = IV; 9430 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 9431 LoopIterationSpace &IS = IterSpaces[Cnt]; 9432 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 9433 ExprResult Iter; 9434 9435 // Compute prod 9436 ExprResult Prod = 9437 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9438 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 9439 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 9440 IterSpaces[K].NumIterations); 9441 9442 // Iter = Acc / Prod 9443 // If there is at least one more inner loop to avoid 9444 // multiplication by 1. 9445 if (Cnt + 1 < NestedLoopCount) 9446 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 9447 Acc.get(), Prod.get()); 9448 else 9449 Iter = Acc; 9450 if (!Iter.isUsable()) { 9451 HasErrors = true; 9452 break; 9453 } 9454 9455 // Update Acc: 9456 // Acc -= Iter * Prod 9457 // Check if there is at least one more inner loop to avoid 9458 // multiplication by 1. 9459 if (Cnt + 1 < NestedLoopCount) 9460 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 9461 Iter.get(), Prod.get()); 9462 else 9463 Prod = Iter; 9464 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 9465 Acc.get(), Prod.get()); 9466 9467 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 9468 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 9469 DeclRefExpr *CounterVar = buildDeclRefExpr( 9470 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 9471 /*RefersToCapture=*/true); 9472 ExprResult Init = 9473 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 9474 IS.CounterInit, IS.IsNonRectangularLB, Captures); 9475 if (!Init.isUsable()) { 9476 HasErrors = true; 9477 break; 9478 } 9479 ExprResult Update = buildCounterUpdate( 9480 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 9481 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 9482 if (!Update.isUsable()) { 9483 HasErrors = true; 9484 break; 9485 } 9486 9487 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 9488 ExprResult Final = 9489 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 9490 IS.CounterInit, IS.NumIterations, IS.CounterStep, 9491 IS.Subtract, IS.IsNonRectangularLB, &Captures); 9492 if (!Final.isUsable()) { 9493 HasErrors = true; 9494 break; 9495 } 9496 9497 if (!Update.isUsable() || !Final.isUsable()) { 9498 HasErrors = true; 9499 break; 9500 } 9501 // Save results 9502 Built.Counters[Cnt] = IS.CounterVar; 9503 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 9504 Built.Inits[Cnt] = Init.get(); 9505 Built.Updates[Cnt] = Update.get(); 9506 Built.Finals[Cnt] = Final.get(); 9507 Built.DependentCounters[Cnt] = nullptr; 9508 Built.DependentInits[Cnt] = nullptr; 9509 Built.FinalsConditions[Cnt] = nullptr; 9510 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 9511 Built.DependentCounters[Cnt] = 9512 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9513 Built.DependentInits[Cnt] = 9514 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9515 Built.FinalsConditions[Cnt] = IS.FinalCondition; 9516 } 9517 } 9518 } 9519 9520 if (HasErrors) 9521 return 0; 9522 9523 // Save results 9524 Built.IterationVarRef = IV.get(); 9525 Built.LastIteration = LastIteration.get(); 9526 Built.NumIterations = NumIterations.get(); 9527 Built.CalcLastIteration = SemaRef 9528 .ActOnFinishFullExpr(CalcLastIteration.get(), 9529 /*DiscardedValue=*/false) 9530 .get(); 9531 Built.PreCond = PreCond.get(); 9532 Built.PreInits = buildPreInits(C, Captures); 9533 Built.Cond = Cond.get(); 9534 Built.Init = Init.get(); 9535 Built.Inc = Inc.get(); 9536 Built.LB = LB.get(); 9537 Built.UB = UB.get(); 9538 Built.IL = IL.get(); 9539 Built.ST = ST.get(); 9540 Built.EUB = EUB.get(); 9541 Built.NLB = NextLB.get(); 9542 Built.NUB = NextUB.get(); 9543 Built.PrevLB = PrevLB.get(); 9544 Built.PrevUB = PrevUB.get(); 9545 Built.DistInc = DistInc.get(); 9546 Built.PrevEUB = PrevEUB.get(); 9547 Built.DistCombinedFields.LB = CombLB.get(); 9548 Built.DistCombinedFields.UB = CombUB.get(); 9549 Built.DistCombinedFields.EUB = CombEUB.get(); 9550 Built.DistCombinedFields.Init = CombInit.get(); 9551 Built.DistCombinedFields.Cond = CombCond.get(); 9552 Built.DistCombinedFields.NLB = CombNextLB.get(); 9553 Built.DistCombinedFields.NUB = CombNextUB.get(); 9554 Built.DistCombinedFields.DistCond = CombDistCond.get(); 9555 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 9556 9557 return NestedLoopCount; 9558 } 9559 9560 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 9561 auto CollapseClauses = 9562 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 9563 if (CollapseClauses.begin() != CollapseClauses.end()) 9564 return (*CollapseClauses.begin())->getNumForLoops(); 9565 return nullptr; 9566 } 9567 9568 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 9569 auto OrderedClauses = 9570 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 9571 if (OrderedClauses.begin() != OrderedClauses.end()) 9572 return (*OrderedClauses.begin())->getNumForLoops(); 9573 return nullptr; 9574 } 9575 9576 static bool checkSimdlenSafelenSpecified(Sema &S, 9577 const ArrayRef<OMPClause *> Clauses) { 9578 const OMPSafelenClause *Safelen = nullptr; 9579 const OMPSimdlenClause *Simdlen = nullptr; 9580 9581 for (const OMPClause *Clause : Clauses) { 9582 if (Clause->getClauseKind() == OMPC_safelen) 9583 Safelen = cast<OMPSafelenClause>(Clause); 9584 else if (Clause->getClauseKind() == OMPC_simdlen) 9585 Simdlen = cast<OMPSimdlenClause>(Clause); 9586 if (Safelen && Simdlen) 9587 break; 9588 } 9589 9590 if (Simdlen && Safelen) { 9591 const Expr *SimdlenLength = Simdlen->getSimdlen(); 9592 const Expr *SafelenLength = Safelen->getSafelen(); 9593 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 9594 SimdlenLength->isInstantiationDependent() || 9595 SimdlenLength->containsUnexpandedParameterPack()) 9596 return false; 9597 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 9598 SafelenLength->isInstantiationDependent() || 9599 SafelenLength->containsUnexpandedParameterPack()) 9600 return false; 9601 Expr::EvalResult SimdlenResult, SafelenResult; 9602 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 9603 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 9604 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 9605 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 9606 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 9607 // If both simdlen and safelen clauses are specified, the value of the 9608 // simdlen parameter must be less than or equal to the value of the safelen 9609 // parameter. 9610 if (SimdlenRes > SafelenRes) { 9611 S.Diag(SimdlenLength->getExprLoc(), 9612 diag::err_omp_wrong_simdlen_safelen_values) 9613 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 9614 return true; 9615 } 9616 } 9617 return false; 9618 } 9619 9620 StmtResult 9621 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9622 SourceLocation StartLoc, SourceLocation EndLoc, 9623 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9624 if (!AStmt) 9625 return StmtError(); 9626 9627 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9628 OMPLoopBasedDirective::HelperExprs B; 9629 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9630 // define the nested loops number. 9631 unsigned NestedLoopCount = checkOpenMPLoop( 9632 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9633 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9634 if (NestedLoopCount == 0) 9635 return StmtError(); 9636 9637 assert((CurContext->isDependentContext() || B.builtAll()) && 9638 "omp simd loop exprs were not built"); 9639 9640 if (!CurContext->isDependentContext()) { 9641 // Finalize the clauses that need pre-built expressions for CodeGen. 9642 for (OMPClause *C : Clauses) { 9643 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9644 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9645 B.NumIterations, *this, CurScope, 9646 DSAStack)) 9647 return StmtError(); 9648 } 9649 } 9650 9651 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9652 return StmtError(); 9653 9654 setFunctionHasBranchProtectedScope(); 9655 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9656 Clauses, AStmt, B); 9657 } 9658 9659 StmtResult 9660 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9661 SourceLocation StartLoc, SourceLocation EndLoc, 9662 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9663 if (!AStmt) 9664 return StmtError(); 9665 9666 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9667 OMPLoopBasedDirective::HelperExprs B; 9668 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9669 // define the nested loops number. 9670 unsigned NestedLoopCount = checkOpenMPLoop( 9671 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9672 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9673 if (NestedLoopCount == 0) 9674 return StmtError(); 9675 9676 assert((CurContext->isDependentContext() || B.builtAll()) && 9677 "omp for loop exprs were not built"); 9678 9679 if (!CurContext->isDependentContext()) { 9680 // Finalize the clauses that need pre-built expressions for CodeGen. 9681 for (OMPClause *C : Clauses) { 9682 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9683 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9684 B.NumIterations, *this, CurScope, 9685 DSAStack)) 9686 return StmtError(); 9687 } 9688 } 9689 9690 setFunctionHasBranchProtectedScope(); 9691 return OMPForDirective::Create( 9692 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9693 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9694 } 9695 9696 StmtResult Sema::ActOnOpenMPForSimdDirective( 9697 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9698 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9699 if (!AStmt) 9700 return StmtError(); 9701 9702 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9703 OMPLoopBasedDirective::HelperExprs B; 9704 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9705 // define the nested loops number. 9706 unsigned NestedLoopCount = 9707 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 9708 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9709 VarsWithImplicitDSA, B); 9710 if (NestedLoopCount == 0) 9711 return StmtError(); 9712 9713 assert((CurContext->isDependentContext() || B.builtAll()) && 9714 "omp for simd loop exprs were not built"); 9715 9716 if (!CurContext->isDependentContext()) { 9717 // Finalize the clauses that need pre-built expressions for CodeGen. 9718 for (OMPClause *C : Clauses) { 9719 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9720 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9721 B.NumIterations, *this, CurScope, 9722 DSAStack)) 9723 return StmtError(); 9724 } 9725 } 9726 9727 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9728 return StmtError(); 9729 9730 setFunctionHasBranchProtectedScope(); 9731 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9732 Clauses, AStmt, B); 9733 } 9734 9735 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 9736 Stmt *AStmt, 9737 SourceLocation StartLoc, 9738 SourceLocation EndLoc) { 9739 if (!AStmt) 9740 return StmtError(); 9741 9742 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9743 auto BaseStmt = AStmt; 9744 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9745 BaseStmt = CS->getCapturedStmt(); 9746 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9747 auto S = C->children(); 9748 if (S.begin() == S.end()) 9749 return StmtError(); 9750 // All associated statements must be '#pragma omp section' except for 9751 // the first one. 9752 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9753 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9754 if (SectionStmt) 9755 Diag(SectionStmt->getBeginLoc(), 9756 diag::err_omp_sections_substmt_not_section); 9757 return StmtError(); 9758 } 9759 cast<OMPSectionDirective>(SectionStmt) 9760 ->setHasCancel(DSAStack->isCancelRegion()); 9761 } 9762 } else { 9763 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 9764 return StmtError(); 9765 } 9766 9767 setFunctionHasBranchProtectedScope(); 9768 9769 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9770 DSAStack->getTaskgroupReductionRef(), 9771 DSAStack->isCancelRegion()); 9772 } 9773 9774 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 9775 SourceLocation StartLoc, 9776 SourceLocation EndLoc) { 9777 if (!AStmt) 9778 return StmtError(); 9779 9780 setFunctionHasBranchProtectedScope(); 9781 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 9782 9783 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 9784 DSAStack->isCancelRegion()); 9785 } 9786 9787 static Expr *getDirectCallExpr(Expr *E) { 9788 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9789 if (auto *CE = dyn_cast<CallExpr>(E)) 9790 if (CE->getDirectCallee()) 9791 return E; 9792 return nullptr; 9793 } 9794 9795 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses, 9796 Stmt *AStmt, 9797 SourceLocation StartLoc, 9798 SourceLocation EndLoc) { 9799 if (!AStmt) 9800 return StmtError(); 9801 9802 Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt(); 9803 9804 // 5.1 OpenMP 9805 // expression-stmt : an expression statement with one of the following forms: 9806 // expression = target-call ( [expression-list] ); 9807 // target-call ( [expression-list] ); 9808 9809 SourceLocation TargetCallLoc; 9810 9811 if (!CurContext->isDependentContext()) { 9812 Expr *TargetCall = nullptr; 9813 9814 auto *E = dyn_cast<Expr>(S); 9815 if (!E) { 9816 Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9817 return StmtError(); 9818 } 9819 9820 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9821 9822 if (auto *BO = dyn_cast<BinaryOperator>(E)) { 9823 if (BO->getOpcode() == BO_Assign) 9824 TargetCall = getDirectCallExpr(BO->getRHS()); 9825 } else { 9826 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E)) 9827 if (COCE->getOperator() == OO_Equal) 9828 TargetCall = getDirectCallExpr(COCE->getArg(1)); 9829 if (!TargetCall) 9830 TargetCall = getDirectCallExpr(E); 9831 } 9832 if (!TargetCall) { 9833 Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9834 return StmtError(); 9835 } 9836 TargetCallLoc = TargetCall->getExprLoc(); 9837 } 9838 9839 setFunctionHasBranchProtectedScope(); 9840 9841 return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9842 TargetCallLoc); 9843 } 9844 9845 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 9846 Stmt *AStmt, 9847 SourceLocation StartLoc, 9848 SourceLocation EndLoc) { 9849 if (!AStmt) 9850 return StmtError(); 9851 9852 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9853 9854 setFunctionHasBranchProtectedScope(); 9855 9856 // OpenMP [2.7.3, single Construct, Restrictions] 9857 // The copyprivate clause must not be used with the nowait clause. 9858 const OMPClause *Nowait = nullptr; 9859 const OMPClause *Copyprivate = nullptr; 9860 for (const OMPClause *Clause : Clauses) { 9861 if (Clause->getClauseKind() == OMPC_nowait) 9862 Nowait = Clause; 9863 else if (Clause->getClauseKind() == OMPC_copyprivate) 9864 Copyprivate = Clause; 9865 if (Copyprivate && Nowait) { 9866 Diag(Copyprivate->getBeginLoc(), 9867 diag::err_omp_single_copyprivate_with_nowait); 9868 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 9869 return StmtError(); 9870 } 9871 } 9872 9873 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9874 } 9875 9876 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 9877 SourceLocation StartLoc, 9878 SourceLocation EndLoc) { 9879 if (!AStmt) 9880 return StmtError(); 9881 9882 setFunctionHasBranchProtectedScope(); 9883 9884 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 9885 } 9886 9887 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses, 9888 Stmt *AStmt, 9889 SourceLocation StartLoc, 9890 SourceLocation EndLoc) { 9891 if (!AStmt) 9892 return StmtError(); 9893 9894 setFunctionHasBranchProtectedScope(); 9895 9896 return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9897 } 9898 9899 StmtResult Sema::ActOnOpenMPCriticalDirective( 9900 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 9901 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 9902 if (!AStmt) 9903 return StmtError(); 9904 9905 bool ErrorFound = false; 9906 llvm::APSInt Hint; 9907 SourceLocation HintLoc; 9908 bool DependentHint = false; 9909 for (const OMPClause *C : Clauses) { 9910 if (C->getClauseKind() == OMPC_hint) { 9911 if (!DirName.getName()) { 9912 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 9913 ErrorFound = true; 9914 } 9915 Expr *E = cast<OMPHintClause>(C)->getHint(); 9916 if (E->isTypeDependent() || E->isValueDependent() || 9917 E->isInstantiationDependent()) { 9918 DependentHint = true; 9919 } else { 9920 Hint = E->EvaluateKnownConstInt(Context); 9921 HintLoc = C->getBeginLoc(); 9922 } 9923 } 9924 } 9925 if (ErrorFound) 9926 return StmtError(); 9927 const auto Pair = DSAStack->getCriticalWithHint(DirName); 9928 if (Pair.first && DirName.getName() && !DependentHint) { 9929 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 9930 Diag(StartLoc, diag::err_omp_critical_with_hint); 9931 if (HintLoc.isValid()) 9932 Diag(HintLoc, diag::note_omp_critical_hint_here) 9933 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 9934 else 9935 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 9936 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 9937 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 9938 << 1 9939 << C->getHint()->EvaluateKnownConstInt(Context).toString( 9940 /*Radix=*/10, /*Signed=*/false); 9941 } else { 9942 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 9943 } 9944 } 9945 } 9946 9947 setFunctionHasBranchProtectedScope(); 9948 9949 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 9950 Clauses, AStmt); 9951 if (!Pair.first && DirName.getName() && !DependentHint) 9952 DSAStack->addCriticalWithHint(Dir, Hint); 9953 return Dir; 9954 } 9955 9956 StmtResult Sema::ActOnOpenMPParallelForDirective( 9957 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9958 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9959 if (!AStmt) 9960 return StmtError(); 9961 9962 auto *CS = cast<CapturedStmt>(AStmt); 9963 // 1.2.2 OpenMP Language Terminology 9964 // Structured block - An executable statement with a single entry at the 9965 // top and a single exit at the bottom. 9966 // The point of exit cannot be a branch out of the structured block. 9967 // longjmp() and throw() must not violate the entry/exit criteria. 9968 CS->getCapturedDecl()->setNothrow(); 9969 9970 OMPLoopBasedDirective::HelperExprs B; 9971 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9972 // define the nested loops number. 9973 unsigned NestedLoopCount = 9974 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 9975 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9976 VarsWithImplicitDSA, B); 9977 if (NestedLoopCount == 0) 9978 return StmtError(); 9979 9980 assert((CurContext->isDependentContext() || B.builtAll()) && 9981 "omp parallel for loop exprs were not built"); 9982 9983 if (!CurContext->isDependentContext()) { 9984 // Finalize the clauses that need pre-built expressions for CodeGen. 9985 for (OMPClause *C : Clauses) { 9986 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9987 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9988 B.NumIterations, *this, CurScope, 9989 DSAStack)) 9990 return StmtError(); 9991 } 9992 } 9993 9994 setFunctionHasBranchProtectedScope(); 9995 return OMPParallelForDirective::Create( 9996 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9997 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9998 } 9999 10000 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 10001 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10002 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10003 if (!AStmt) 10004 return StmtError(); 10005 10006 auto *CS = cast<CapturedStmt>(AStmt); 10007 // 1.2.2 OpenMP Language Terminology 10008 // Structured block - An executable statement with a single entry at the 10009 // top and a single exit at the bottom. 10010 // The point of exit cannot be a branch out of the structured block. 10011 // longjmp() and throw() must not violate the entry/exit criteria. 10012 CS->getCapturedDecl()->setNothrow(); 10013 10014 OMPLoopBasedDirective::HelperExprs B; 10015 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10016 // define the nested loops number. 10017 unsigned NestedLoopCount = 10018 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 10019 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10020 VarsWithImplicitDSA, B); 10021 if (NestedLoopCount == 0) 10022 return StmtError(); 10023 10024 if (!CurContext->isDependentContext()) { 10025 // Finalize the clauses that need pre-built expressions for CodeGen. 10026 for (OMPClause *C : Clauses) { 10027 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10028 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10029 B.NumIterations, *this, CurScope, 10030 DSAStack)) 10031 return StmtError(); 10032 } 10033 } 10034 10035 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10036 return StmtError(); 10037 10038 setFunctionHasBranchProtectedScope(); 10039 return OMPParallelForSimdDirective::Create( 10040 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10041 } 10042 10043 StmtResult 10044 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 10045 Stmt *AStmt, SourceLocation StartLoc, 10046 SourceLocation EndLoc) { 10047 if (!AStmt) 10048 return StmtError(); 10049 10050 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10051 auto *CS = cast<CapturedStmt>(AStmt); 10052 // 1.2.2 OpenMP Language Terminology 10053 // Structured block - An executable statement with a single entry at the 10054 // top and a single exit at the bottom. 10055 // The point of exit cannot be a branch out of the structured block. 10056 // longjmp() and throw() must not violate the entry/exit criteria. 10057 CS->getCapturedDecl()->setNothrow(); 10058 10059 setFunctionHasBranchProtectedScope(); 10060 10061 return OMPParallelMasterDirective::Create( 10062 Context, StartLoc, EndLoc, Clauses, AStmt, 10063 DSAStack->getTaskgroupReductionRef()); 10064 } 10065 10066 StmtResult 10067 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 10068 Stmt *AStmt, SourceLocation StartLoc, 10069 SourceLocation EndLoc) { 10070 if (!AStmt) 10071 return StmtError(); 10072 10073 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10074 auto BaseStmt = AStmt; 10075 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 10076 BaseStmt = CS->getCapturedStmt(); 10077 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10078 auto S = C->children(); 10079 if (S.begin() == S.end()) 10080 return StmtError(); 10081 // All associated statements must be '#pragma omp section' except for 10082 // the first one. 10083 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 10084 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10085 if (SectionStmt) 10086 Diag(SectionStmt->getBeginLoc(), 10087 diag::err_omp_parallel_sections_substmt_not_section); 10088 return StmtError(); 10089 } 10090 cast<OMPSectionDirective>(SectionStmt) 10091 ->setHasCancel(DSAStack->isCancelRegion()); 10092 } 10093 } else { 10094 Diag(AStmt->getBeginLoc(), 10095 diag::err_omp_parallel_sections_not_compound_stmt); 10096 return StmtError(); 10097 } 10098 10099 setFunctionHasBranchProtectedScope(); 10100 10101 return OMPParallelSectionsDirective::Create( 10102 Context, StartLoc, EndLoc, Clauses, AStmt, 10103 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10104 } 10105 10106 /// detach and mergeable clauses are mutially exclusive, check for it. 10107 static bool checkDetachMergeableClauses(Sema &S, 10108 ArrayRef<OMPClause *> Clauses) { 10109 const OMPClause *PrevClause = nullptr; 10110 bool ErrorFound = false; 10111 for (const OMPClause *C : Clauses) { 10112 if (C->getClauseKind() == OMPC_detach || 10113 C->getClauseKind() == OMPC_mergeable) { 10114 if (!PrevClause) { 10115 PrevClause = C; 10116 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10117 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10118 << getOpenMPClauseName(C->getClauseKind()) 10119 << getOpenMPClauseName(PrevClause->getClauseKind()); 10120 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10121 << getOpenMPClauseName(PrevClause->getClauseKind()); 10122 ErrorFound = true; 10123 } 10124 } 10125 } 10126 return ErrorFound; 10127 } 10128 10129 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 10130 Stmt *AStmt, SourceLocation StartLoc, 10131 SourceLocation EndLoc) { 10132 if (!AStmt) 10133 return StmtError(); 10134 10135 // OpenMP 5.0, 2.10.1 task Construct 10136 // If a detach clause appears on the directive, then a mergeable clause cannot 10137 // appear on the same directive. 10138 if (checkDetachMergeableClauses(*this, Clauses)) 10139 return StmtError(); 10140 10141 auto *CS = cast<CapturedStmt>(AStmt); 10142 // 1.2.2 OpenMP Language Terminology 10143 // Structured block - An executable statement with a single entry at the 10144 // top and a single exit at the bottom. 10145 // The point of exit cannot be a branch out of the structured block. 10146 // longjmp() and throw() must not violate the entry/exit criteria. 10147 CS->getCapturedDecl()->setNothrow(); 10148 10149 setFunctionHasBranchProtectedScope(); 10150 10151 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10152 DSAStack->isCancelRegion()); 10153 } 10154 10155 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 10156 SourceLocation EndLoc) { 10157 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 10158 } 10159 10160 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 10161 SourceLocation EndLoc) { 10162 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 10163 } 10164 10165 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 10166 SourceLocation EndLoc) { 10167 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 10168 } 10169 10170 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 10171 Stmt *AStmt, 10172 SourceLocation StartLoc, 10173 SourceLocation EndLoc) { 10174 if (!AStmt) 10175 return StmtError(); 10176 10177 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10178 10179 setFunctionHasBranchProtectedScope(); 10180 10181 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 10182 AStmt, 10183 DSAStack->getTaskgroupReductionRef()); 10184 } 10185 10186 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 10187 SourceLocation StartLoc, 10188 SourceLocation EndLoc) { 10189 OMPFlushClause *FC = nullptr; 10190 OMPClause *OrderClause = nullptr; 10191 for (OMPClause *C : Clauses) { 10192 if (C->getClauseKind() == OMPC_flush) 10193 FC = cast<OMPFlushClause>(C); 10194 else 10195 OrderClause = C; 10196 } 10197 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10198 SourceLocation MemOrderLoc; 10199 for (const OMPClause *C : Clauses) { 10200 if (C->getClauseKind() == OMPC_acq_rel || 10201 C->getClauseKind() == OMPC_acquire || 10202 C->getClauseKind() == OMPC_release) { 10203 if (MemOrderKind != OMPC_unknown) { 10204 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10205 << getOpenMPDirectiveName(OMPD_flush) << 1 10206 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10207 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10208 << getOpenMPClauseName(MemOrderKind); 10209 } else { 10210 MemOrderKind = C->getClauseKind(); 10211 MemOrderLoc = C->getBeginLoc(); 10212 } 10213 } 10214 } 10215 if (FC && OrderClause) { 10216 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 10217 << getOpenMPClauseName(OrderClause->getClauseKind()); 10218 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 10219 << getOpenMPClauseName(OrderClause->getClauseKind()); 10220 return StmtError(); 10221 } 10222 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 10223 } 10224 10225 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 10226 SourceLocation StartLoc, 10227 SourceLocation EndLoc) { 10228 if (Clauses.empty()) { 10229 Diag(StartLoc, diag::err_omp_depobj_expected); 10230 return StmtError(); 10231 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 10232 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 10233 return StmtError(); 10234 } 10235 // Only depobj expression and another single clause is allowed. 10236 if (Clauses.size() > 2) { 10237 Diag(Clauses[2]->getBeginLoc(), 10238 diag::err_omp_depobj_single_clause_expected); 10239 return StmtError(); 10240 } else if (Clauses.size() < 1) { 10241 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 10242 return StmtError(); 10243 } 10244 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 10245 } 10246 10247 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 10248 SourceLocation StartLoc, 10249 SourceLocation EndLoc) { 10250 // Check that exactly one clause is specified. 10251 if (Clauses.size() != 1) { 10252 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 10253 diag::err_omp_scan_single_clause_expected); 10254 return StmtError(); 10255 } 10256 // Check that scan directive is used in the scopeof the OpenMP loop body. 10257 if (Scope *S = DSAStack->getCurScope()) { 10258 Scope *ParentS = S->getParent(); 10259 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 10260 !ParentS->getBreakParent()->isOpenMPLoopScope()) 10261 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 10262 << getOpenMPDirectiveName(OMPD_scan) << 5); 10263 } 10264 // Check that only one instance of scan directives is used in the same outer 10265 // region. 10266 if (DSAStack->doesParentHasScanDirective()) { 10267 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 10268 Diag(DSAStack->getParentScanDirectiveLoc(), 10269 diag::note_omp_previous_directive) 10270 << "scan"; 10271 return StmtError(); 10272 } 10273 DSAStack->setParentHasScanDirective(StartLoc); 10274 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 10275 } 10276 10277 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 10278 Stmt *AStmt, 10279 SourceLocation StartLoc, 10280 SourceLocation EndLoc) { 10281 const OMPClause *DependFound = nullptr; 10282 const OMPClause *DependSourceClause = nullptr; 10283 const OMPClause *DependSinkClause = nullptr; 10284 bool ErrorFound = false; 10285 const OMPThreadsClause *TC = nullptr; 10286 const OMPSIMDClause *SC = nullptr; 10287 for (const OMPClause *C : Clauses) { 10288 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 10289 DependFound = C; 10290 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 10291 if (DependSourceClause) { 10292 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 10293 << getOpenMPDirectiveName(OMPD_ordered) 10294 << getOpenMPClauseName(OMPC_depend) << 2; 10295 ErrorFound = true; 10296 } else { 10297 DependSourceClause = C; 10298 } 10299 if (DependSinkClause) { 10300 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10301 << 0; 10302 ErrorFound = true; 10303 } 10304 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 10305 if (DependSourceClause) { 10306 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10307 << 1; 10308 ErrorFound = true; 10309 } 10310 DependSinkClause = C; 10311 } 10312 } else if (C->getClauseKind() == OMPC_threads) { 10313 TC = cast<OMPThreadsClause>(C); 10314 } else if (C->getClauseKind() == OMPC_simd) { 10315 SC = cast<OMPSIMDClause>(C); 10316 } 10317 } 10318 if (!ErrorFound && !SC && 10319 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 10320 // OpenMP [2.8.1,simd Construct, Restrictions] 10321 // An ordered construct with the simd clause is the only OpenMP construct 10322 // that can appear in the simd region. 10323 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 10324 << (LangOpts.OpenMP >= 50 ? 1 : 0); 10325 ErrorFound = true; 10326 } else if (DependFound && (TC || SC)) { 10327 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 10328 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 10329 ErrorFound = true; 10330 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 10331 Diag(DependFound->getBeginLoc(), 10332 diag::err_omp_ordered_directive_without_param); 10333 ErrorFound = true; 10334 } else if (TC || Clauses.empty()) { 10335 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 10336 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 10337 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 10338 << (TC != nullptr); 10339 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 10340 ErrorFound = true; 10341 } 10342 } 10343 if ((!AStmt && !DependFound) || ErrorFound) 10344 return StmtError(); 10345 10346 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 10347 // During execution of an iteration of a worksharing-loop or a loop nest 10348 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 10349 // must not execute more than one ordered region corresponding to an ordered 10350 // construct without a depend clause. 10351 if (!DependFound) { 10352 if (DSAStack->doesParentHasOrderedDirective()) { 10353 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 10354 Diag(DSAStack->getParentOrderedDirectiveLoc(), 10355 diag::note_omp_previous_directive) 10356 << "ordered"; 10357 return StmtError(); 10358 } 10359 DSAStack->setParentHasOrderedDirective(StartLoc); 10360 } 10361 10362 if (AStmt) { 10363 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10364 10365 setFunctionHasBranchProtectedScope(); 10366 } 10367 10368 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10369 } 10370 10371 namespace { 10372 /// Helper class for checking expression in 'omp atomic [update]' 10373 /// construct. 10374 class OpenMPAtomicUpdateChecker { 10375 /// Error results for atomic update expressions. 10376 enum ExprAnalysisErrorCode { 10377 /// A statement is not an expression statement. 10378 NotAnExpression, 10379 /// Expression is not builtin binary or unary operation. 10380 NotABinaryOrUnaryExpression, 10381 /// Unary operation is not post-/pre- increment/decrement operation. 10382 NotAnUnaryIncDecExpression, 10383 /// An expression is not of scalar type. 10384 NotAScalarType, 10385 /// A binary operation is not an assignment operation. 10386 NotAnAssignmentOp, 10387 /// RHS part of the binary operation is not a binary expression. 10388 NotABinaryExpression, 10389 /// RHS part is not additive/multiplicative/shift/biwise binary 10390 /// expression. 10391 NotABinaryOperator, 10392 /// RHS binary operation does not have reference to the updated LHS 10393 /// part. 10394 NotAnUpdateExpression, 10395 /// No errors is found. 10396 NoError 10397 }; 10398 /// Reference to Sema. 10399 Sema &SemaRef; 10400 /// A location for note diagnostics (when error is found). 10401 SourceLocation NoteLoc; 10402 /// 'x' lvalue part of the source atomic expression. 10403 Expr *X; 10404 /// 'expr' rvalue part of the source atomic expression. 10405 Expr *E; 10406 /// Helper expression of the form 10407 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10408 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10409 Expr *UpdateExpr; 10410 /// Is 'x' a LHS in a RHS part of full update expression. It is 10411 /// important for non-associative operations. 10412 bool IsXLHSInRHSPart; 10413 BinaryOperatorKind Op; 10414 SourceLocation OpLoc; 10415 /// true if the source expression is a postfix unary operation, false 10416 /// if it is a prefix unary operation. 10417 bool IsPostfixUpdate; 10418 10419 public: 10420 OpenMPAtomicUpdateChecker(Sema &SemaRef) 10421 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 10422 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 10423 /// Check specified statement that it is suitable for 'atomic update' 10424 /// constructs and extract 'x', 'expr' and Operation from the original 10425 /// expression. If DiagId and NoteId == 0, then only check is performed 10426 /// without error notification. 10427 /// \param DiagId Diagnostic which should be emitted if error is found. 10428 /// \param NoteId Diagnostic note for the main error message. 10429 /// \return true if statement is not an update expression, false otherwise. 10430 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 10431 /// Return the 'x' lvalue part of the source atomic expression. 10432 Expr *getX() const { return X; } 10433 /// Return the 'expr' rvalue part of the source atomic expression. 10434 Expr *getExpr() const { return E; } 10435 /// Return the update expression used in calculation of the updated 10436 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10437 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10438 Expr *getUpdateExpr() const { return UpdateExpr; } 10439 /// Return true if 'x' is LHS in RHS part of full update expression, 10440 /// false otherwise. 10441 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 10442 10443 /// true if the source expression is a postfix unary operation, false 10444 /// if it is a prefix unary operation. 10445 bool isPostfixUpdate() const { return IsPostfixUpdate; } 10446 10447 private: 10448 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 10449 unsigned NoteId = 0); 10450 }; 10451 } // namespace 10452 10453 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 10454 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 10455 ExprAnalysisErrorCode ErrorFound = NoError; 10456 SourceLocation ErrorLoc, NoteLoc; 10457 SourceRange ErrorRange, NoteRange; 10458 // Allowed constructs are: 10459 // x = x binop expr; 10460 // x = expr binop x; 10461 if (AtomicBinOp->getOpcode() == BO_Assign) { 10462 X = AtomicBinOp->getLHS(); 10463 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 10464 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 10465 if (AtomicInnerBinOp->isMultiplicativeOp() || 10466 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 10467 AtomicInnerBinOp->isBitwiseOp()) { 10468 Op = AtomicInnerBinOp->getOpcode(); 10469 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 10470 Expr *LHS = AtomicInnerBinOp->getLHS(); 10471 Expr *RHS = AtomicInnerBinOp->getRHS(); 10472 llvm::FoldingSetNodeID XId, LHSId, RHSId; 10473 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 10474 /*Canonical=*/true); 10475 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 10476 /*Canonical=*/true); 10477 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 10478 /*Canonical=*/true); 10479 if (XId == LHSId) { 10480 E = RHS; 10481 IsXLHSInRHSPart = true; 10482 } else if (XId == RHSId) { 10483 E = LHS; 10484 IsXLHSInRHSPart = false; 10485 } else { 10486 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10487 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10488 NoteLoc = X->getExprLoc(); 10489 NoteRange = X->getSourceRange(); 10490 ErrorFound = NotAnUpdateExpression; 10491 } 10492 } else { 10493 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10494 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10495 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 10496 NoteRange = SourceRange(NoteLoc, NoteLoc); 10497 ErrorFound = NotABinaryOperator; 10498 } 10499 } else { 10500 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 10501 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 10502 ErrorFound = NotABinaryExpression; 10503 } 10504 } else { 10505 ErrorLoc = AtomicBinOp->getExprLoc(); 10506 ErrorRange = AtomicBinOp->getSourceRange(); 10507 NoteLoc = AtomicBinOp->getOperatorLoc(); 10508 NoteRange = SourceRange(NoteLoc, NoteLoc); 10509 ErrorFound = NotAnAssignmentOp; 10510 } 10511 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10512 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10513 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10514 return true; 10515 } 10516 if (SemaRef.CurContext->isDependentContext()) 10517 E = X = UpdateExpr = nullptr; 10518 return ErrorFound != NoError; 10519 } 10520 10521 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 10522 unsigned NoteId) { 10523 ExprAnalysisErrorCode ErrorFound = NoError; 10524 SourceLocation ErrorLoc, NoteLoc; 10525 SourceRange ErrorRange, NoteRange; 10526 // Allowed constructs are: 10527 // x++; 10528 // x--; 10529 // ++x; 10530 // --x; 10531 // x binop= expr; 10532 // x = x binop expr; 10533 // x = expr binop x; 10534 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 10535 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 10536 if (AtomicBody->getType()->isScalarType() || 10537 AtomicBody->isInstantiationDependent()) { 10538 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 10539 AtomicBody->IgnoreParenImpCasts())) { 10540 // Check for Compound Assignment Operation 10541 Op = BinaryOperator::getOpForCompoundAssignment( 10542 AtomicCompAssignOp->getOpcode()); 10543 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 10544 E = AtomicCompAssignOp->getRHS(); 10545 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 10546 IsXLHSInRHSPart = true; 10547 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 10548 AtomicBody->IgnoreParenImpCasts())) { 10549 // Check for Binary Operation 10550 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 10551 return true; 10552 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 10553 AtomicBody->IgnoreParenImpCasts())) { 10554 // Check for Unary Operation 10555 if (AtomicUnaryOp->isIncrementDecrementOp()) { 10556 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 10557 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 10558 OpLoc = AtomicUnaryOp->getOperatorLoc(); 10559 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 10560 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 10561 IsXLHSInRHSPart = true; 10562 } else { 10563 ErrorFound = NotAnUnaryIncDecExpression; 10564 ErrorLoc = AtomicUnaryOp->getExprLoc(); 10565 ErrorRange = AtomicUnaryOp->getSourceRange(); 10566 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 10567 NoteRange = SourceRange(NoteLoc, NoteLoc); 10568 } 10569 } else if (!AtomicBody->isInstantiationDependent()) { 10570 ErrorFound = NotABinaryOrUnaryExpression; 10571 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 10572 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 10573 } 10574 } else { 10575 ErrorFound = NotAScalarType; 10576 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 10577 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10578 } 10579 } else { 10580 ErrorFound = NotAnExpression; 10581 NoteLoc = ErrorLoc = S->getBeginLoc(); 10582 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10583 } 10584 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10585 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10586 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10587 return true; 10588 } 10589 if (SemaRef.CurContext->isDependentContext()) 10590 E = X = UpdateExpr = nullptr; 10591 if (ErrorFound == NoError && E && X) { 10592 // Build an update expression of form 'OpaqueValueExpr(x) binop 10593 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 10594 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 10595 auto *OVEX = new (SemaRef.getASTContext()) 10596 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 10597 auto *OVEExpr = new (SemaRef.getASTContext()) 10598 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 10599 ExprResult Update = 10600 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 10601 IsXLHSInRHSPart ? OVEExpr : OVEX); 10602 if (Update.isInvalid()) 10603 return true; 10604 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 10605 Sema::AA_Casting); 10606 if (Update.isInvalid()) 10607 return true; 10608 UpdateExpr = Update.get(); 10609 } 10610 return ErrorFound != NoError; 10611 } 10612 10613 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 10614 Stmt *AStmt, 10615 SourceLocation StartLoc, 10616 SourceLocation EndLoc) { 10617 // Register location of the first atomic directive. 10618 DSAStack->addAtomicDirectiveLoc(StartLoc); 10619 if (!AStmt) 10620 return StmtError(); 10621 10622 // 1.2.2 OpenMP Language Terminology 10623 // Structured block - An executable statement with a single entry at the 10624 // top and a single exit at the bottom. 10625 // The point of exit cannot be a branch out of the structured block. 10626 // longjmp() and throw() must not violate the entry/exit criteria. 10627 OpenMPClauseKind AtomicKind = OMPC_unknown; 10628 SourceLocation AtomicKindLoc; 10629 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10630 SourceLocation MemOrderLoc; 10631 for (const OMPClause *C : Clauses) { 10632 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 10633 C->getClauseKind() == OMPC_update || 10634 C->getClauseKind() == OMPC_capture) { 10635 if (AtomicKind != OMPC_unknown) { 10636 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 10637 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10638 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 10639 << getOpenMPClauseName(AtomicKind); 10640 } else { 10641 AtomicKind = C->getClauseKind(); 10642 AtomicKindLoc = C->getBeginLoc(); 10643 } 10644 } 10645 if (C->getClauseKind() == OMPC_seq_cst || 10646 C->getClauseKind() == OMPC_acq_rel || 10647 C->getClauseKind() == OMPC_acquire || 10648 C->getClauseKind() == OMPC_release || 10649 C->getClauseKind() == OMPC_relaxed) { 10650 if (MemOrderKind != OMPC_unknown) { 10651 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10652 << getOpenMPDirectiveName(OMPD_atomic) << 0 10653 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10654 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10655 << getOpenMPClauseName(MemOrderKind); 10656 } else { 10657 MemOrderKind = C->getClauseKind(); 10658 MemOrderLoc = C->getBeginLoc(); 10659 } 10660 } 10661 } 10662 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 10663 // If atomic-clause is read then memory-order-clause must not be acq_rel or 10664 // release. 10665 // If atomic-clause is write then memory-order-clause must not be acq_rel or 10666 // acquire. 10667 // If atomic-clause is update or not present then memory-order-clause must not 10668 // be acq_rel or acquire. 10669 if ((AtomicKind == OMPC_read && 10670 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 10671 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 10672 AtomicKind == OMPC_unknown) && 10673 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 10674 SourceLocation Loc = AtomicKindLoc; 10675 if (AtomicKind == OMPC_unknown) 10676 Loc = StartLoc; 10677 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 10678 << getOpenMPClauseName(AtomicKind) 10679 << (AtomicKind == OMPC_unknown ? 1 : 0) 10680 << getOpenMPClauseName(MemOrderKind); 10681 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10682 << getOpenMPClauseName(MemOrderKind); 10683 } 10684 10685 Stmt *Body = AStmt; 10686 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 10687 Body = EWC->getSubExpr(); 10688 10689 Expr *X = nullptr; 10690 Expr *V = nullptr; 10691 Expr *E = nullptr; 10692 Expr *UE = nullptr; 10693 bool IsXLHSInRHSPart = false; 10694 bool IsPostfixUpdate = false; 10695 // OpenMP [2.12.6, atomic Construct] 10696 // In the next expressions: 10697 // * x and v (as applicable) are both l-value expressions with scalar type. 10698 // * During the execution of an atomic region, multiple syntactic 10699 // occurrences of x must designate the same storage location. 10700 // * Neither of v and expr (as applicable) may access the storage location 10701 // designated by x. 10702 // * Neither of x and expr (as applicable) may access the storage location 10703 // designated by v. 10704 // * expr is an expression with scalar type. 10705 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 10706 // * binop, binop=, ++, and -- are not overloaded operators. 10707 // * The expression x binop expr must be numerically equivalent to x binop 10708 // (expr). This requirement is satisfied if the operators in expr have 10709 // precedence greater than binop, or by using parentheses around expr or 10710 // subexpressions of expr. 10711 // * The expression expr binop x must be numerically equivalent to (expr) 10712 // binop x. This requirement is satisfied if the operators in expr have 10713 // precedence equal to or greater than binop, or by using parentheses around 10714 // expr or subexpressions of expr. 10715 // * For forms that allow multiple occurrences of x, the number of times 10716 // that x is evaluated is unspecified. 10717 if (AtomicKind == OMPC_read) { 10718 enum { 10719 NotAnExpression, 10720 NotAnAssignmentOp, 10721 NotAScalarType, 10722 NotAnLValue, 10723 NoError 10724 } ErrorFound = NoError; 10725 SourceLocation ErrorLoc, NoteLoc; 10726 SourceRange ErrorRange, NoteRange; 10727 // If clause is read: 10728 // v = x; 10729 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10730 const auto *AtomicBinOp = 10731 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10732 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10733 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10734 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 10735 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10736 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 10737 if (!X->isLValue() || !V->isLValue()) { 10738 const Expr *NotLValueExpr = X->isLValue() ? V : X; 10739 ErrorFound = NotAnLValue; 10740 ErrorLoc = AtomicBinOp->getExprLoc(); 10741 ErrorRange = AtomicBinOp->getSourceRange(); 10742 NoteLoc = NotLValueExpr->getExprLoc(); 10743 NoteRange = NotLValueExpr->getSourceRange(); 10744 } 10745 } else if (!X->isInstantiationDependent() || 10746 !V->isInstantiationDependent()) { 10747 const Expr *NotScalarExpr = 10748 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10749 ? V 10750 : X; 10751 ErrorFound = NotAScalarType; 10752 ErrorLoc = AtomicBinOp->getExprLoc(); 10753 ErrorRange = AtomicBinOp->getSourceRange(); 10754 NoteLoc = NotScalarExpr->getExprLoc(); 10755 NoteRange = NotScalarExpr->getSourceRange(); 10756 } 10757 } else if (!AtomicBody->isInstantiationDependent()) { 10758 ErrorFound = NotAnAssignmentOp; 10759 ErrorLoc = AtomicBody->getExprLoc(); 10760 ErrorRange = AtomicBody->getSourceRange(); 10761 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10762 : AtomicBody->getExprLoc(); 10763 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10764 : AtomicBody->getSourceRange(); 10765 } 10766 } else { 10767 ErrorFound = NotAnExpression; 10768 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10769 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10770 } 10771 if (ErrorFound != NoError) { 10772 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 10773 << ErrorRange; 10774 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10775 << NoteRange; 10776 return StmtError(); 10777 } 10778 if (CurContext->isDependentContext()) 10779 V = X = nullptr; 10780 } else if (AtomicKind == OMPC_write) { 10781 enum { 10782 NotAnExpression, 10783 NotAnAssignmentOp, 10784 NotAScalarType, 10785 NotAnLValue, 10786 NoError 10787 } ErrorFound = NoError; 10788 SourceLocation ErrorLoc, NoteLoc; 10789 SourceRange ErrorRange, NoteRange; 10790 // If clause is write: 10791 // x = expr; 10792 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10793 const auto *AtomicBinOp = 10794 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10795 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10796 X = AtomicBinOp->getLHS(); 10797 E = AtomicBinOp->getRHS(); 10798 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10799 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 10800 if (!X->isLValue()) { 10801 ErrorFound = NotAnLValue; 10802 ErrorLoc = AtomicBinOp->getExprLoc(); 10803 ErrorRange = AtomicBinOp->getSourceRange(); 10804 NoteLoc = X->getExprLoc(); 10805 NoteRange = X->getSourceRange(); 10806 } 10807 } else if (!X->isInstantiationDependent() || 10808 !E->isInstantiationDependent()) { 10809 const Expr *NotScalarExpr = 10810 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10811 ? E 10812 : X; 10813 ErrorFound = NotAScalarType; 10814 ErrorLoc = AtomicBinOp->getExprLoc(); 10815 ErrorRange = AtomicBinOp->getSourceRange(); 10816 NoteLoc = NotScalarExpr->getExprLoc(); 10817 NoteRange = NotScalarExpr->getSourceRange(); 10818 } 10819 } else if (!AtomicBody->isInstantiationDependent()) { 10820 ErrorFound = NotAnAssignmentOp; 10821 ErrorLoc = AtomicBody->getExprLoc(); 10822 ErrorRange = AtomicBody->getSourceRange(); 10823 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10824 : AtomicBody->getExprLoc(); 10825 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10826 : AtomicBody->getSourceRange(); 10827 } 10828 } else { 10829 ErrorFound = NotAnExpression; 10830 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10831 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10832 } 10833 if (ErrorFound != NoError) { 10834 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 10835 << ErrorRange; 10836 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10837 << NoteRange; 10838 return StmtError(); 10839 } 10840 if (CurContext->isDependentContext()) 10841 E = X = nullptr; 10842 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 10843 // If clause is update: 10844 // x++; 10845 // x--; 10846 // ++x; 10847 // --x; 10848 // x binop= expr; 10849 // x = x binop expr; 10850 // x = expr binop x; 10851 OpenMPAtomicUpdateChecker Checker(*this); 10852 if (Checker.checkStatement( 10853 Body, (AtomicKind == OMPC_update) 10854 ? diag::err_omp_atomic_update_not_expression_statement 10855 : diag::err_omp_atomic_not_expression_statement, 10856 diag::note_omp_atomic_update)) 10857 return StmtError(); 10858 if (!CurContext->isDependentContext()) { 10859 E = Checker.getExpr(); 10860 X = Checker.getX(); 10861 UE = Checker.getUpdateExpr(); 10862 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10863 } 10864 } else if (AtomicKind == OMPC_capture) { 10865 enum { 10866 NotAnAssignmentOp, 10867 NotACompoundStatement, 10868 NotTwoSubstatements, 10869 NotASpecificExpression, 10870 NoError 10871 } ErrorFound = NoError; 10872 SourceLocation ErrorLoc, NoteLoc; 10873 SourceRange ErrorRange, NoteRange; 10874 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10875 // If clause is a capture: 10876 // v = x++; 10877 // v = x--; 10878 // v = ++x; 10879 // v = --x; 10880 // v = x binop= expr; 10881 // v = x = x binop expr; 10882 // v = x = expr binop x; 10883 const auto *AtomicBinOp = 10884 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10885 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10886 V = AtomicBinOp->getLHS(); 10887 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10888 OpenMPAtomicUpdateChecker Checker(*this); 10889 if (Checker.checkStatement( 10890 Body, diag::err_omp_atomic_capture_not_expression_statement, 10891 diag::note_omp_atomic_update)) 10892 return StmtError(); 10893 E = Checker.getExpr(); 10894 X = Checker.getX(); 10895 UE = Checker.getUpdateExpr(); 10896 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10897 IsPostfixUpdate = Checker.isPostfixUpdate(); 10898 } else if (!AtomicBody->isInstantiationDependent()) { 10899 ErrorLoc = AtomicBody->getExprLoc(); 10900 ErrorRange = AtomicBody->getSourceRange(); 10901 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10902 : AtomicBody->getExprLoc(); 10903 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10904 : AtomicBody->getSourceRange(); 10905 ErrorFound = NotAnAssignmentOp; 10906 } 10907 if (ErrorFound != NoError) { 10908 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 10909 << ErrorRange; 10910 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10911 return StmtError(); 10912 } 10913 if (CurContext->isDependentContext()) 10914 UE = V = E = X = nullptr; 10915 } else { 10916 // If clause is a capture: 10917 // { v = x; x = expr; } 10918 // { v = x; x++; } 10919 // { v = x; x--; } 10920 // { v = x; ++x; } 10921 // { v = x; --x; } 10922 // { v = x; x binop= expr; } 10923 // { v = x; x = x binop expr; } 10924 // { v = x; x = expr binop x; } 10925 // { x++; v = x; } 10926 // { x--; v = x; } 10927 // { ++x; v = x; } 10928 // { --x; v = x; } 10929 // { x binop= expr; v = x; } 10930 // { x = x binop expr; v = x; } 10931 // { x = expr binop x; v = x; } 10932 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 10933 // Check that this is { expr1; expr2; } 10934 if (CS->size() == 2) { 10935 Stmt *First = CS->body_front(); 10936 Stmt *Second = CS->body_back(); 10937 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 10938 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 10939 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 10940 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 10941 // Need to find what subexpression is 'v' and what is 'x'. 10942 OpenMPAtomicUpdateChecker Checker(*this); 10943 bool IsUpdateExprFound = !Checker.checkStatement(Second); 10944 BinaryOperator *BinOp = nullptr; 10945 if (IsUpdateExprFound) { 10946 BinOp = dyn_cast<BinaryOperator>(First); 10947 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10948 } 10949 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10950 // { v = x; x++; } 10951 // { v = x; x--; } 10952 // { v = x; ++x; } 10953 // { v = x; --x; } 10954 // { v = x; x binop= expr; } 10955 // { v = x; x = x binop expr; } 10956 // { v = x; x = expr binop x; } 10957 // Check that the first expression has form v = x. 10958 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10959 llvm::FoldingSetNodeID XId, PossibleXId; 10960 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10961 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10962 IsUpdateExprFound = XId == PossibleXId; 10963 if (IsUpdateExprFound) { 10964 V = BinOp->getLHS(); 10965 X = Checker.getX(); 10966 E = Checker.getExpr(); 10967 UE = Checker.getUpdateExpr(); 10968 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10969 IsPostfixUpdate = true; 10970 } 10971 } 10972 if (!IsUpdateExprFound) { 10973 IsUpdateExprFound = !Checker.checkStatement(First); 10974 BinOp = nullptr; 10975 if (IsUpdateExprFound) { 10976 BinOp = dyn_cast<BinaryOperator>(Second); 10977 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10978 } 10979 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10980 // { x++; v = x; } 10981 // { x--; v = x; } 10982 // { ++x; v = x; } 10983 // { --x; v = x; } 10984 // { x binop= expr; v = x; } 10985 // { x = x binop expr; v = x; } 10986 // { x = expr binop x; v = x; } 10987 // Check that the second expression has form v = x. 10988 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10989 llvm::FoldingSetNodeID XId, PossibleXId; 10990 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10991 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10992 IsUpdateExprFound = XId == PossibleXId; 10993 if (IsUpdateExprFound) { 10994 V = BinOp->getLHS(); 10995 X = Checker.getX(); 10996 E = Checker.getExpr(); 10997 UE = Checker.getUpdateExpr(); 10998 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10999 IsPostfixUpdate = false; 11000 } 11001 } 11002 } 11003 if (!IsUpdateExprFound) { 11004 // { v = x; x = expr; } 11005 auto *FirstExpr = dyn_cast<Expr>(First); 11006 auto *SecondExpr = dyn_cast<Expr>(Second); 11007 if (!FirstExpr || !SecondExpr || 11008 !(FirstExpr->isInstantiationDependent() || 11009 SecondExpr->isInstantiationDependent())) { 11010 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 11011 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 11012 ErrorFound = NotAnAssignmentOp; 11013 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 11014 : First->getBeginLoc(); 11015 NoteRange = ErrorRange = FirstBinOp 11016 ? FirstBinOp->getSourceRange() 11017 : SourceRange(ErrorLoc, ErrorLoc); 11018 } else { 11019 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 11020 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 11021 ErrorFound = NotAnAssignmentOp; 11022 NoteLoc = ErrorLoc = SecondBinOp 11023 ? SecondBinOp->getOperatorLoc() 11024 : Second->getBeginLoc(); 11025 NoteRange = ErrorRange = 11026 SecondBinOp ? SecondBinOp->getSourceRange() 11027 : SourceRange(ErrorLoc, ErrorLoc); 11028 } else { 11029 Expr *PossibleXRHSInFirst = 11030 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 11031 Expr *PossibleXLHSInSecond = 11032 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 11033 llvm::FoldingSetNodeID X1Id, X2Id; 11034 PossibleXRHSInFirst->Profile(X1Id, Context, 11035 /*Canonical=*/true); 11036 PossibleXLHSInSecond->Profile(X2Id, Context, 11037 /*Canonical=*/true); 11038 IsUpdateExprFound = X1Id == X2Id; 11039 if (IsUpdateExprFound) { 11040 V = FirstBinOp->getLHS(); 11041 X = SecondBinOp->getLHS(); 11042 E = SecondBinOp->getRHS(); 11043 UE = nullptr; 11044 IsXLHSInRHSPart = false; 11045 IsPostfixUpdate = true; 11046 } else { 11047 ErrorFound = NotASpecificExpression; 11048 ErrorLoc = FirstBinOp->getExprLoc(); 11049 ErrorRange = FirstBinOp->getSourceRange(); 11050 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 11051 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 11052 } 11053 } 11054 } 11055 } 11056 } 11057 } else { 11058 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11059 NoteRange = ErrorRange = 11060 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11061 ErrorFound = NotTwoSubstatements; 11062 } 11063 } else { 11064 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11065 NoteRange = ErrorRange = 11066 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11067 ErrorFound = NotACompoundStatement; 11068 } 11069 if (ErrorFound != NoError) { 11070 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 11071 << ErrorRange; 11072 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11073 return StmtError(); 11074 } 11075 if (CurContext->isDependentContext()) 11076 UE = V = E = X = nullptr; 11077 } 11078 } 11079 11080 setFunctionHasBranchProtectedScope(); 11081 11082 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 11083 X, V, E, UE, IsXLHSInRHSPart, 11084 IsPostfixUpdate); 11085 } 11086 11087 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 11088 Stmt *AStmt, 11089 SourceLocation StartLoc, 11090 SourceLocation EndLoc) { 11091 if (!AStmt) 11092 return StmtError(); 11093 11094 auto *CS = cast<CapturedStmt>(AStmt); 11095 // 1.2.2 OpenMP Language Terminology 11096 // Structured block - An executable statement with a single entry at the 11097 // top and a single exit at the bottom. 11098 // The point of exit cannot be a branch out of the structured block. 11099 // longjmp() and throw() must not violate the entry/exit criteria. 11100 CS->getCapturedDecl()->setNothrow(); 11101 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 11102 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11103 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11104 // 1.2.2 OpenMP Language Terminology 11105 // Structured block - An executable statement with a single entry at the 11106 // top and a single exit at the bottom. 11107 // The point of exit cannot be a branch out of the structured block. 11108 // longjmp() and throw() must not violate the entry/exit criteria. 11109 CS->getCapturedDecl()->setNothrow(); 11110 } 11111 11112 // OpenMP [2.16, Nesting of Regions] 11113 // If specified, a teams construct must be contained within a target 11114 // construct. That target construct must contain no statements or directives 11115 // outside of the teams construct. 11116 if (DSAStack->hasInnerTeamsRegion()) { 11117 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 11118 bool OMPTeamsFound = true; 11119 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 11120 auto I = CS->body_begin(); 11121 while (I != CS->body_end()) { 11122 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 11123 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 11124 OMPTeamsFound) { 11125 11126 OMPTeamsFound = false; 11127 break; 11128 } 11129 ++I; 11130 } 11131 assert(I != CS->body_end() && "Not found statement"); 11132 S = *I; 11133 } else { 11134 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 11135 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 11136 } 11137 if (!OMPTeamsFound) { 11138 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 11139 Diag(DSAStack->getInnerTeamsRegionLoc(), 11140 diag::note_omp_nested_teams_construct_here); 11141 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 11142 << isa<OMPExecutableDirective>(S); 11143 return StmtError(); 11144 } 11145 } 11146 11147 setFunctionHasBranchProtectedScope(); 11148 11149 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11150 } 11151 11152 StmtResult 11153 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 11154 Stmt *AStmt, SourceLocation StartLoc, 11155 SourceLocation EndLoc) { 11156 if (!AStmt) 11157 return StmtError(); 11158 11159 auto *CS = cast<CapturedStmt>(AStmt); 11160 // 1.2.2 OpenMP Language Terminology 11161 // Structured block - An executable statement with a single entry at the 11162 // top and a single exit at the bottom. 11163 // The point of exit cannot be a branch out of the structured block. 11164 // longjmp() and throw() must not violate the entry/exit criteria. 11165 CS->getCapturedDecl()->setNothrow(); 11166 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 11167 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11168 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11169 // 1.2.2 OpenMP Language Terminology 11170 // Structured block - An executable statement with a single entry at the 11171 // top and a single exit at the bottom. 11172 // The point of exit cannot be a branch out of the structured block. 11173 // longjmp() and throw() must not violate the entry/exit criteria. 11174 CS->getCapturedDecl()->setNothrow(); 11175 } 11176 11177 setFunctionHasBranchProtectedScope(); 11178 11179 return OMPTargetParallelDirective::Create( 11180 Context, StartLoc, EndLoc, Clauses, AStmt, 11181 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11182 } 11183 11184 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 11185 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11186 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11187 if (!AStmt) 11188 return StmtError(); 11189 11190 auto *CS = cast<CapturedStmt>(AStmt); 11191 // 1.2.2 OpenMP Language Terminology 11192 // Structured block - An executable statement with a single entry at the 11193 // top and a single exit at the bottom. 11194 // The point of exit cannot be a branch out of the structured block. 11195 // longjmp() and throw() must not violate the entry/exit criteria. 11196 CS->getCapturedDecl()->setNothrow(); 11197 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11198 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11199 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11200 // 1.2.2 OpenMP Language Terminology 11201 // Structured block - An executable statement with a single entry at the 11202 // top and a single exit at the bottom. 11203 // The point of exit cannot be a branch out of the structured block. 11204 // longjmp() and throw() must not violate the entry/exit criteria. 11205 CS->getCapturedDecl()->setNothrow(); 11206 } 11207 11208 OMPLoopBasedDirective::HelperExprs B; 11209 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11210 // define the nested loops number. 11211 unsigned NestedLoopCount = 11212 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 11213 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11214 VarsWithImplicitDSA, B); 11215 if (NestedLoopCount == 0) 11216 return StmtError(); 11217 11218 assert((CurContext->isDependentContext() || B.builtAll()) && 11219 "omp target parallel for loop exprs were not built"); 11220 11221 if (!CurContext->isDependentContext()) { 11222 // Finalize the clauses that need pre-built expressions for CodeGen. 11223 for (OMPClause *C : Clauses) { 11224 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11225 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11226 B.NumIterations, *this, CurScope, 11227 DSAStack)) 11228 return StmtError(); 11229 } 11230 } 11231 11232 setFunctionHasBranchProtectedScope(); 11233 return OMPTargetParallelForDirective::Create( 11234 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11235 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11236 } 11237 11238 /// Check for existence of a map clause in the list of clauses. 11239 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 11240 const OpenMPClauseKind K) { 11241 return llvm::any_of( 11242 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 11243 } 11244 11245 template <typename... Params> 11246 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 11247 const Params... ClauseTypes) { 11248 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 11249 } 11250 11251 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 11252 Stmt *AStmt, 11253 SourceLocation StartLoc, 11254 SourceLocation EndLoc) { 11255 if (!AStmt) 11256 return StmtError(); 11257 11258 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11259 11260 // OpenMP [2.12.2, target data Construct, Restrictions] 11261 // At least one map, use_device_addr or use_device_ptr clause must appear on 11262 // the directive. 11263 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 11264 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 11265 StringRef Expected; 11266 if (LangOpts.OpenMP < 50) 11267 Expected = "'map' or 'use_device_ptr'"; 11268 else 11269 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 11270 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11271 << Expected << getOpenMPDirectiveName(OMPD_target_data); 11272 return StmtError(); 11273 } 11274 11275 setFunctionHasBranchProtectedScope(); 11276 11277 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11278 AStmt); 11279 } 11280 11281 StmtResult 11282 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 11283 SourceLocation StartLoc, 11284 SourceLocation EndLoc, Stmt *AStmt) { 11285 if (!AStmt) 11286 return StmtError(); 11287 11288 auto *CS = cast<CapturedStmt>(AStmt); 11289 // 1.2.2 OpenMP Language Terminology 11290 // Structured block - An executable statement with a single entry at the 11291 // top and a single exit at the bottom. 11292 // The point of exit cannot be a branch out of the structured block. 11293 // longjmp() and throw() must not violate the entry/exit criteria. 11294 CS->getCapturedDecl()->setNothrow(); 11295 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 11296 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11297 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11298 // 1.2.2 OpenMP Language Terminology 11299 // Structured block - An executable statement with a single entry at the 11300 // top and a single exit at the bottom. 11301 // The point of exit cannot be a branch out of the structured block. 11302 // longjmp() and throw() must not violate the entry/exit criteria. 11303 CS->getCapturedDecl()->setNothrow(); 11304 } 11305 11306 // OpenMP [2.10.2, Restrictions, p. 99] 11307 // At least one map clause must appear on the directive. 11308 if (!hasClauses(Clauses, OMPC_map)) { 11309 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11310 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 11311 return StmtError(); 11312 } 11313 11314 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11315 AStmt); 11316 } 11317 11318 StmtResult 11319 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 11320 SourceLocation StartLoc, 11321 SourceLocation EndLoc, Stmt *AStmt) { 11322 if (!AStmt) 11323 return StmtError(); 11324 11325 auto *CS = cast<CapturedStmt>(AStmt); 11326 // 1.2.2 OpenMP Language Terminology 11327 // Structured block - An executable statement with a single entry at the 11328 // top and a single exit at the bottom. 11329 // The point of exit cannot be a branch out of the structured block. 11330 // longjmp() and throw() must not violate the entry/exit criteria. 11331 CS->getCapturedDecl()->setNothrow(); 11332 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 11333 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11334 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11335 // 1.2.2 OpenMP Language Terminology 11336 // Structured block - An executable statement with a single entry at the 11337 // top and a single exit at the bottom. 11338 // The point of exit cannot be a branch out of the structured block. 11339 // longjmp() and throw() must not violate the entry/exit criteria. 11340 CS->getCapturedDecl()->setNothrow(); 11341 } 11342 11343 // OpenMP [2.10.3, Restrictions, p. 102] 11344 // At least one map clause must appear on the directive. 11345 if (!hasClauses(Clauses, OMPC_map)) { 11346 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11347 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 11348 return StmtError(); 11349 } 11350 11351 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11352 AStmt); 11353 } 11354 11355 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 11356 SourceLocation StartLoc, 11357 SourceLocation EndLoc, 11358 Stmt *AStmt) { 11359 if (!AStmt) 11360 return StmtError(); 11361 11362 auto *CS = cast<CapturedStmt>(AStmt); 11363 // 1.2.2 OpenMP Language Terminology 11364 // Structured block - An executable statement with a single entry at the 11365 // top and a single exit at the bottom. 11366 // The point of exit cannot be a branch out of the structured block. 11367 // longjmp() and throw() must not violate the entry/exit criteria. 11368 CS->getCapturedDecl()->setNothrow(); 11369 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 11370 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11371 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11372 // 1.2.2 OpenMP Language Terminology 11373 // Structured block - An executable statement with a single entry at the 11374 // top and a single exit at the bottom. 11375 // The point of exit cannot be a branch out of the structured block. 11376 // longjmp() and throw() must not violate the entry/exit criteria. 11377 CS->getCapturedDecl()->setNothrow(); 11378 } 11379 11380 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 11381 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 11382 return StmtError(); 11383 } 11384 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 11385 AStmt); 11386 } 11387 11388 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 11389 Stmt *AStmt, SourceLocation StartLoc, 11390 SourceLocation EndLoc) { 11391 if (!AStmt) 11392 return StmtError(); 11393 11394 auto *CS = cast<CapturedStmt>(AStmt); 11395 // 1.2.2 OpenMP Language Terminology 11396 // Structured block - An executable statement with a single entry at the 11397 // top and a single exit at the bottom. 11398 // The point of exit cannot be a branch out of the structured block. 11399 // longjmp() and throw() must not violate the entry/exit criteria. 11400 CS->getCapturedDecl()->setNothrow(); 11401 11402 setFunctionHasBranchProtectedScope(); 11403 11404 DSAStack->setParentTeamsRegionLoc(StartLoc); 11405 11406 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11407 } 11408 11409 StmtResult 11410 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 11411 SourceLocation EndLoc, 11412 OpenMPDirectiveKind CancelRegion) { 11413 if (DSAStack->isParentNowaitRegion()) { 11414 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 11415 return StmtError(); 11416 } 11417 if (DSAStack->isParentOrderedRegion()) { 11418 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 11419 return StmtError(); 11420 } 11421 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 11422 CancelRegion); 11423 } 11424 11425 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 11426 SourceLocation StartLoc, 11427 SourceLocation EndLoc, 11428 OpenMPDirectiveKind CancelRegion) { 11429 if (DSAStack->isParentNowaitRegion()) { 11430 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 11431 return StmtError(); 11432 } 11433 if (DSAStack->isParentOrderedRegion()) { 11434 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 11435 return StmtError(); 11436 } 11437 DSAStack->setParentCancelRegion(/*Cancel=*/true); 11438 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 11439 CancelRegion); 11440 } 11441 11442 static bool checkGrainsizeNumTasksClauses(Sema &S, 11443 ArrayRef<OMPClause *> Clauses) { 11444 const OMPClause *PrevClause = nullptr; 11445 bool ErrorFound = false; 11446 for (const OMPClause *C : Clauses) { 11447 if (C->getClauseKind() == OMPC_grainsize || 11448 C->getClauseKind() == OMPC_num_tasks) { 11449 if (!PrevClause) 11450 PrevClause = C; 11451 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 11452 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 11453 << getOpenMPClauseName(C->getClauseKind()) 11454 << getOpenMPClauseName(PrevClause->getClauseKind()); 11455 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 11456 << getOpenMPClauseName(PrevClause->getClauseKind()); 11457 ErrorFound = true; 11458 } 11459 } 11460 } 11461 return ErrorFound; 11462 } 11463 11464 static bool checkReductionClauseWithNogroup(Sema &S, 11465 ArrayRef<OMPClause *> Clauses) { 11466 const OMPClause *ReductionClause = nullptr; 11467 const OMPClause *NogroupClause = nullptr; 11468 for (const OMPClause *C : Clauses) { 11469 if (C->getClauseKind() == OMPC_reduction) { 11470 ReductionClause = C; 11471 if (NogroupClause) 11472 break; 11473 continue; 11474 } 11475 if (C->getClauseKind() == OMPC_nogroup) { 11476 NogroupClause = C; 11477 if (ReductionClause) 11478 break; 11479 continue; 11480 } 11481 } 11482 if (ReductionClause && NogroupClause) { 11483 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 11484 << SourceRange(NogroupClause->getBeginLoc(), 11485 NogroupClause->getEndLoc()); 11486 return true; 11487 } 11488 return false; 11489 } 11490 11491 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 11492 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11493 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11494 if (!AStmt) 11495 return StmtError(); 11496 11497 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11498 OMPLoopBasedDirective::HelperExprs B; 11499 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11500 // define the nested loops number. 11501 unsigned NestedLoopCount = 11502 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 11503 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11504 VarsWithImplicitDSA, B); 11505 if (NestedLoopCount == 0) 11506 return StmtError(); 11507 11508 assert((CurContext->isDependentContext() || B.builtAll()) && 11509 "omp for loop exprs were not built"); 11510 11511 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11512 // The grainsize clause and num_tasks clause are mutually exclusive and may 11513 // not appear on the same taskloop directive. 11514 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11515 return StmtError(); 11516 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11517 // If a reduction clause is present on the taskloop directive, the nogroup 11518 // clause must not be specified. 11519 if (checkReductionClauseWithNogroup(*this, Clauses)) 11520 return StmtError(); 11521 11522 setFunctionHasBranchProtectedScope(); 11523 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11524 NestedLoopCount, Clauses, AStmt, B, 11525 DSAStack->isCancelRegion()); 11526 } 11527 11528 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 11529 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11530 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11531 if (!AStmt) 11532 return StmtError(); 11533 11534 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11535 OMPLoopBasedDirective::HelperExprs B; 11536 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11537 // define the nested loops number. 11538 unsigned NestedLoopCount = 11539 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 11540 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11541 VarsWithImplicitDSA, B); 11542 if (NestedLoopCount == 0) 11543 return StmtError(); 11544 11545 assert((CurContext->isDependentContext() || B.builtAll()) && 11546 "omp for loop exprs were not built"); 11547 11548 if (!CurContext->isDependentContext()) { 11549 // Finalize the clauses that need pre-built expressions for CodeGen. 11550 for (OMPClause *C : Clauses) { 11551 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11552 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11553 B.NumIterations, *this, CurScope, 11554 DSAStack)) 11555 return StmtError(); 11556 } 11557 } 11558 11559 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11560 // The grainsize clause and num_tasks clause are mutually exclusive and may 11561 // not appear on the same taskloop directive. 11562 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11563 return StmtError(); 11564 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11565 // If a reduction clause is present on the taskloop directive, the nogroup 11566 // clause must not be specified. 11567 if (checkReductionClauseWithNogroup(*this, Clauses)) 11568 return StmtError(); 11569 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11570 return StmtError(); 11571 11572 setFunctionHasBranchProtectedScope(); 11573 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 11574 NestedLoopCount, Clauses, AStmt, B); 11575 } 11576 11577 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 11578 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11579 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11580 if (!AStmt) 11581 return StmtError(); 11582 11583 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11584 OMPLoopBasedDirective::HelperExprs B; 11585 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11586 // define the nested loops number. 11587 unsigned NestedLoopCount = 11588 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 11589 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11590 VarsWithImplicitDSA, B); 11591 if (NestedLoopCount == 0) 11592 return StmtError(); 11593 11594 assert((CurContext->isDependentContext() || B.builtAll()) && 11595 "omp for loop exprs were not built"); 11596 11597 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11598 // The grainsize clause and num_tasks clause are mutually exclusive and may 11599 // not appear on the same taskloop directive. 11600 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11601 return StmtError(); 11602 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11603 // If a reduction clause is present on the taskloop directive, the nogroup 11604 // clause must not be specified. 11605 if (checkReductionClauseWithNogroup(*this, Clauses)) 11606 return StmtError(); 11607 11608 setFunctionHasBranchProtectedScope(); 11609 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11610 NestedLoopCount, Clauses, AStmt, B, 11611 DSAStack->isCancelRegion()); 11612 } 11613 11614 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 11615 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11616 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11617 if (!AStmt) 11618 return StmtError(); 11619 11620 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11621 OMPLoopBasedDirective::HelperExprs B; 11622 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11623 // define the nested loops number. 11624 unsigned NestedLoopCount = 11625 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11626 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11627 VarsWithImplicitDSA, B); 11628 if (NestedLoopCount == 0) 11629 return StmtError(); 11630 11631 assert((CurContext->isDependentContext() || B.builtAll()) && 11632 "omp for loop exprs were not built"); 11633 11634 if (!CurContext->isDependentContext()) { 11635 // Finalize the clauses that need pre-built expressions for CodeGen. 11636 for (OMPClause *C : Clauses) { 11637 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11638 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11639 B.NumIterations, *this, CurScope, 11640 DSAStack)) 11641 return StmtError(); 11642 } 11643 } 11644 11645 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11646 // The grainsize clause and num_tasks clause are mutually exclusive and may 11647 // not appear on the same taskloop directive. 11648 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11649 return StmtError(); 11650 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11651 // If a reduction clause is present on the taskloop directive, the nogroup 11652 // clause must not be specified. 11653 if (checkReductionClauseWithNogroup(*this, Clauses)) 11654 return StmtError(); 11655 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11656 return StmtError(); 11657 11658 setFunctionHasBranchProtectedScope(); 11659 return OMPMasterTaskLoopSimdDirective::Create( 11660 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11661 } 11662 11663 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 11664 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11665 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11666 if (!AStmt) 11667 return StmtError(); 11668 11669 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11670 auto *CS = cast<CapturedStmt>(AStmt); 11671 // 1.2.2 OpenMP Language Terminology 11672 // Structured block - An executable statement with a single entry at the 11673 // top and a single exit at the bottom. 11674 // The point of exit cannot be a branch out of the structured block. 11675 // longjmp() and throw() must not violate the entry/exit criteria. 11676 CS->getCapturedDecl()->setNothrow(); 11677 for (int ThisCaptureLevel = 11678 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 11679 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11680 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11681 // 1.2.2 OpenMP Language Terminology 11682 // Structured block - An executable statement with a single entry at the 11683 // top and a single exit at the bottom. 11684 // The point of exit cannot be a branch out of the structured block. 11685 // longjmp() and throw() must not violate the entry/exit criteria. 11686 CS->getCapturedDecl()->setNothrow(); 11687 } 11688 11689 OMPLoopBasedDirective::HelperExprs B; 11690 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11691 // define the nested loops number. 11692 unsigned NestedLoopCount = checkOpenMPLoop( 11693 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 11694 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11695 VarsWithImplicitDSA, B); 11696 if (NestedLoopCount == 0) 11697 return StmtError(); 11698 11699 assert((CurContext->isDependentContext() || B.builtAll()) && 11700 "omp for loop exprs were not built"); 11701 11702 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11703 // The grainsize clause and num_tasks clause are mutually exclusive and may 11704 // not appear on the same taskloop directive. 11705 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11706 return StmtError(); 11707 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11708 // If a reduction clause is present on the taskloop directive, the nogroup 11709 // clause must not be specified. 11710 if (checkReductionClauseWithNogroup(*this, Clauses)) 11711 return StmtError(); 11712 11713 setFunctionHasBranchProtectedScope(); 11714 return OMPParallelMasterTaskLoopDirective::Create( 11715 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11716 DSAStack->isCancelRegion()); 11717 } 11718 11719 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 11720 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11721 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11722 if (!AStmt) 11723 return StmtError(); 11724 11725 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11726 auto *CS = cast<CapturedStmt>(AStmt); 11727 // 1.2.2 OpenMP Language Terminology 11728 // Structured block - An executable statement with a single entry at the 11729 // top and a single exit at the bottom. 11730 // The point of exit cannot be a branch out of the structured block. 11731 // longjmp() and throw() must not violate the entry/exit criteria. 11732 CS->getCapturedDecl()->setNothrow(); 11733 for (int ThisCaptureLevel = 11734 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 11735 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11736 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11737 // 1.2.2 OpenMP Language Terminology 11738 // Structured block - An executable statement with a single entry at the 11739 // top and a single exit at the bottom. 11740 // The point of exit cannot be a branch out of the structured block. 11741 // longjmp() and throw() must not violate the entry/exit criteria. 11742 CS->getCapturedDecl()->setNothrow(); 11743 } 11744 11745 OMPLoopBasedDirective::HelperExprs B; 11746 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11747 // define the nested loops number. 11748 unsigned NestedLoopCount = checkOpenMPLoop( 11749 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11750 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11751 VarsWithImplicitDSA, B); 11752 if (NestedLoopCount == 0) 11753 return StmtError(); 11754 11755 assert((CurContext->isDependentContext() || B.builtAll()) && 11756 "omp for loop exprs were not built"); 11757 11758 if (!CurContext->isDependentContext()) { 11759 // Finalize the clauses that need pre-built expressions for CodeGen. 11760 for (OMPClause *C : Clauses) { 11761 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11762 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11763 B.NumIterations, *this, CurScope, 11764 DSAStack)) 11765 return StmtError(); 11766 } 11767 } 11768 11769 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11770 // The grainsize clause and num_tasks clause are mutually exclusive and may 11771 // not appear on the same taskloop directive. 11772 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 11773 return StmtError(); 11774 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11775 // If a reduction clause is present on the taskloop directive, the nogroup 11776 // clause must not be specified. 11777 if (checkReductionClauseWithNogroup(*this, Clauses)) 11778 return StmtError(); 11779 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11780 return StmtError(); 11781 11782 setFunctionHasBranchProtectedScope(); 11783 return OMPParallelMasterTaskLoopSimdDirective::Create( 11784 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11785 } 11786 11787 StmtResult Sema::ActOnOpenMPDistributeDirective( 11788 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11789 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11790 if (!AStmt) 11791 return StmtError(); 11792 11793 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11794 OMPLoopBasedDirective::HelperExprs B; 11795 // In presence of clause 'collapse' with number of loops, it will 11796 // define the nested loops number. 11797 unsigned NestedLoopCount = 11798 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 11799 nullptr /*ordered not a clause on distribute*/, AStmt, 11800 *this, *DSAStack, VarsWithImplicitDSA, B); 11801 if (NestedLoopCount == 0) 11802 return StmtError(); 11803 11804 assert((CurContext->isDependentContext() || B.builtAll()) && 11805 "omp for loop exprs were not built"); 11806 11807 setFunctionHasBranchProtectedScope(); 11808 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 11809 NestedLoopCount, Clauses, AStmt, B); 11810 } 11811 11812 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 11813 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11814 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11815 if (!AStmt) 11816 return StmtError(); 11817 11818 auto *CS = cast<CapturedStmt>(AStmt); 11819 // 1.2.2 OpenMP Language Terminology 11820 // Structured block - An executable statement with a single entry at the 11821 // top and a single exit at the bottom. 11822 // The point of exit cannot be a branch out of the structured block. 11823 // longjmp() and throw() must not violate the entry/exit criteria. 11824 CS->getCapturedDecl()->setNothrow(); 11825 for (int ThisCaptureLevel = 11826 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 11827 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11828 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11829 // 1.2.2 OpenMP Language Terminology 11830 // Structured block - An executable statement with a single entry at the 11831 // top and a single exit at the bottom. 11832 // The point of exit cannot be a branch out of the structured block. 11833 // longjmp() and throw() must not violate the entry/exit criteria. 11834 CS->getCapturedDecl()->setNothrow(); 11835 } 11836 11837 OMPLoopBasedDirective::HelperExprs B; 11838 // In presence of clause 'collapse' with number of loops, it will 11839 // define the nested loops number. 11840 unsigned NestedLoopCount = checkOpenMPLoop( 11841 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11842 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11843 VarsWithImplicitDSA, B); 11844 if (NestedLoopCount == 0) 11845 return StmtError(); 11846 11847 assert((CurContext->isDependentContext() || B.builtAll()) && 11848 "omp for loop exprs were not built"); 11849 11850 setFunctionHasBranchProtectedScope(); 11851 return OMPDistributeParallelForDirective::Create( 11852 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11853 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11854 } 11855 11856 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 11857 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11858 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11859 if (!AStmt) 11860 return StmtError(); 11861 11862 auto *CS = cast<CapturedStmt>(AStmt); 11863 // 1.2.2 OpenMP Language Terminology 11864 // Structured block - An executable statement with a single entry at the 11865 // top and a single exit at the bottom. 11866 // The point of exit cannot be a branch out of the structured block. 11867 // longjmp() and throw() must not violate the entry/exit criteria. 11868 CS->getCapturedDecl()->setNothrow(); 11869 for (int ThisCaptureLevel = 11870 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 11871 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11872 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11873 // 1.2.2 OpenMP Language Terminology 11874 // Structured block - An executable statement with a single entry at the 11875 // top and a single exit at the bottom. 11876 // The point of exit cannot be a branch out of the structured block. 11877 // longjmp() and throw() must not violate the entry/exit criteria. 11878 CS->getCapturedDecl()->setNothrow(); 11879 } 11880 11881 OMPLoopBasedDirective::HelperExprs B; 11882 // In presence of clause 'collapse' with number of loops, it will 11883 // define the nested loops number. 11884 unsigned NestedLoopCount = checkOpenMPLoop( 11885 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11886 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11887 VarsWithImplicitDSA, B); 11888 if (NestedLoopCount == 0) 11889 return StmtError(); 11890 11891 assert((CurContext->isDependentContext() || B.builtAll()) && 11892 "omp for loop exprs were not built"); 11893 11894 if (!CurContext->isDependentContext()) { 11895 // Finalize the clauses that need pre-built expressions for CodeGen. 11896 for (OMPClause *C : Clauses) { 11897 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11898 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11899 B.NumIterations, *this, CurScope, 11900 DSAStack)) 11901 return StmtError(); 11902 } 11903 } 11904 11905 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11906 return StmtError(); 11907 11908 setFunctionHasBranchProtectedScope(); 11909 return OMPDistributeParallelForSimdDirective::Create( 11910 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11911 } 11912 11913 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 11914 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11915 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11916 if (!AStmt) 11917 return StmtError(); 11918 11919 auto *CS = cast<CapturedStmt>(AStmt); 11920 // 1.2.2 OpenMP Language Terminology 11921 // Structured block - An executable statement with a single entry at the 11922 // top and a single exit at the bottom. 11923 // The point of exit cannot be a branch out of the structured block. 11924 // longjmp() and throw() must not violate the entry/exit criteria. 11925 CS->getCapturedDecl()->setNothrow(); 11926 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 11927 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11928 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11929 // 1.2.2 OpenMP Language Terminology 11930 // Structured block - An executable statement with a single entry at the 11931 // top and a single exit at the bottom. 11932 // The point of exit cannot be a branch out of the structured block. 11933 // longjmp() and throw() must not violate the entry/exit criteria. 11934 CS->getCapturedDecl()->setNothrow(); 11935 } 11936 11937 OMPLoopBasedDirective::HelperExprs B; 11938 // In presence of clause 'collapse' with number of loops, it will 11939 // define the nested loops number. 11940 unsigned NestedLoopCount = 11941 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 11942 nullptr /*ordered not a clause on distribute*/, CS, *this, 11943 *DSAStack, VarsWithImplicitDSA, B); 11944 if (NestedLoopCount == 0) 11945 return StmtError(); 11946 11947 assert((CurContext->isDependentContext() || B.builtAll()) && 11948 "omp for loop exprs were not built"); 11949 11950 if (!CurContext->isDependentContext()) { 11951 // Finalize the clauses that need pre-built expressions for CodeGen. 11952 for (OMPClause *C : Clauses) { 11953 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11954 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11955 B.NumIterations, *this, CurScope, 11956 DSAStack)) 11957 return StmtError(); 11958 } 11959 } 11960 11961 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11962 return StmtError(); 11963 11964 setFunctionHasBranchProtectedScope(); 11965 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 11966 NestedLoopCount, Clauses, AStmt, B); 11967 } 11968 11969 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 11970 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11971 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11972 if (!AStmt) 11973 return StmtError(); 11974 11975 auto *CS = cast<CapturedStmt>(AStmt); 11976 // 1.2.2 OpenMP Language Terminology 11977 // Structured block - An executable statement with a single entry at the 11978 // top and a single exit at the bottom. 11979 // The point of exit cannot be a branch out of the structured block. 11980 // longjmp() and throw() must not violate the entry/exit criteria. 11981 CS->getCapturedDecl()->setNothrow(); 11982 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11983 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11984 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11985 // 1.2.2 OpenMP Language Terminology 11986 // Structured block - An executable statement with a single entry at the 11987 // top and a single exit at the bottom. 11988 // The point of exit cannot be a branch out of the structured block. 11989 // longjmp() and throw() must not violate the entry/exit criteria. 11990 CS->getCapturedDecl()->setNothrow(); 11991 } 11992 11993 OMPLoopBasedDirective::HelperExprs B; 11994 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11995 // define the nested loops number. 11996 unsigned NestedLoopCount = checkOpenMPLoop( 11997 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 11998 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11999 VarsWithImplicitDSA, B); 12000 if (NestedLoopCount == 0) 12001 return StmtError(); 12002 12003 assert((CurContext->isDependentContext() || B.builtAll()) && 12004 "omp target parallel for simd loop exprs were not built"); 12005 12006 if (!CurContext->isDependentContext()) { 12007 // Finalize the clauses that need pre-built expressions for CodeGen. 12008 for (OMPClause *C : Clauses) { 12009 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12010 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12011 B.NumIterations, *this, CurScope, 12012 DSAStack)) 12013 return StmtError(); 12014 } 12015 } 12016 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12017 return StmtError(); 12018 12019 setFunctionHasBranchProtectedScope(); 12020 return OMPTargetParallelForSimdDirective::Create( 12021 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12022 } 12023 12024 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 12025 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12026 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12027 if (!AStmt) 12028 return StmtError(); 12029 12030 auto *CS = cast<CapturedStmt>(AStmt); 12031 // 1.2.2 OpenMP Language Terminology 12032 // Structured block - An executable statement with a single entry at the 12033 // top and a single exit at the bottom. 12034 // The point of exit cannot be a branch out of the structured block. 12035 // longjmp() and throw() must not violate the entry/exit criteria. 12036 CS->getCapturedDecl()->setNothrow(); 12037 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 12038 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12039 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12040 // 1.2.2 OpenMP Language Terminology 12041 // Structured block - An executable statement with a single entry at the 12042 // top and a single exit at the bottom. 12043 // The point of exit cannot be a branch out of the structured block. 12044 // longjmp() and throw() must not violate the entry/exit criteria. 12045 CS->getCapturedDecl()->setNothrow(); 12046 } 12047 12048 OMPLoopBasedDirective::HelperExprs B; 12049 // In presence of clause 'collapse' with number of loops, it will define the 12050 // nested loops number. 12051 unsigned NestedLoopCount = 12052 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 12053 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12054 VarsWithImplicitDSA, B); 12055 if (NestedLoopCount == 0) 12056 return StmtError(); 12057 12058 assert((CurContext->isDependentContext() || B.builtAll()) && 12059 "omp target simd loop exprs were not built"); 12060 12061 if (!CurContext->isDependentContext()) { 12062 // Finalize the clauses that need pre-built expressions for CodeGen. 12063 for (OMPClause *C : Clauses) { 12064 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12065 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12066 B.NumIterations, *this, CurScope, 12067 DSAStack)) 12068 return StmtError(); 12069 } 12070 } 12071 12072 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12073 return StmtError(); 12074 12075 setFunctionHasBranchProtectedScope(); 12076 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 12077 NestedLoopCount, Clauses, AStmt, B); 12078 } 12079 12080 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 12081 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12082 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12083 if (!AStmt) 12084 return StmtError(); 12085 12086 auto *CS = cast<CapturedStmt>(AStmt); 12087 // 1.2.2 OpenMP Language Terminology 12088 // Structured block - An executable statement with a single entry at the 12089 // top and a single exit at the bottom. 12090 // The point of exit cannot be a branch out of the structured block. 12091 // longjmp() and throw() must not violate the entry/exit criteria. 12092 CS->getCapturedDecl()->setNothrow(); 12093 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 12094 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12095 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12096 // 1.2.2 OpenMP Language Terminology 12097 // Structured block - An executable statement with a single entry at the 12098 // top and a single exit at the bottom. 12099 // The point of exit cannot be a branch out of the structured block. 12100 // longjmp() and throw() must not violate the entry/exit criteria. 12101 CS->getCapturedDecl()->setNothrow(); 12102 } 12103 12104 OMPLoopBasedDirective::HelperExprs B; 12105 // In presence of clause 'collapse' with number of loops, it will 12106 // define the nested loops number. 12107 unsigned NestedLoopCount = 12108 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 12109 nullptr /*ordered not a clause on distribute*/, CS, *this, 12110 *DSAStack, VarsWithImplicitDSA, B); 12111 if (NestedLoopCount == 0) 12112 return StmtError(); 12113 12114 assert((CurContext->isDependentContext() || B.builtAll()) && 12115 "omp teams distribute loop exprs were not built"); 12116 12117 setFunctionHasBranchProtectedScope(); 12118 12119 DSAStack->setParentTeamsRegionLoc(StartLoc); 12120 12121 return OMPTeamsDistributeDirective::Create( 12122 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12123 } 12124 12125 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 12126 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12127 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12128 if (!AStmt) 12129 return StmtError(); 12130 12131 auto *CS = cast<CapturedStmt>(AStmt); 12132 // 1.2.2 OpenMP Language Terminology 12133 // Structured block - An executable statement with a single entry at the 12134 // top and a single exit at the bottom. 12135 // The point of exit cannot be a branch out of the structured block. 12136 // longjmp() and throw() must not violate the entry/exit criteria. 12137 CS->getCapturedDecl()->setNothrow(); 12138 for (int ThisCaptureLevel = 12139 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 12140 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12141 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12142 // 1.2.2 OpenMP Language Terminology 12143 // Structured block - An executable statement with a single entry at the 12144 // top and a single exit at the bottom. 12145 // The point of exit cannot be a branch out of the structured block. 12146 // longjmp() and throw() must not violate the entry/exit criteria. 12147 CS->getCapturedDecl()->setNothrow(); 12148 } 12149 12150 OMPLoopBasedDirective::HelperExprs B; 12151 // In presence of clause 'collapse' with number of loops, it will 12152 // define the nested loops number. 12153 unsigned NestedLoopCount = checkOpenMPLoop( 12154 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12155 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12156 VarsWithImplicitDSA, B); 12157 12158 if (NestedLoopCount == 0) 12159 return StmtError(); 12160 12161 assert((CurContext->isDependentContext() || B.builtAll()) && 12162 "omp teams distribute simd loop exprs were not built"); 12163 12164 if (!CurContext->isDependentContext()) { 12165 // Finalize the clauses that need pre-built expressions for CodeGen. 12166 for (OMPClause *C : Clauses) { 12167 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12168 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12169 B.NumIterations, *this, CurScope, 12170 DSAStack)) 12171 return StmtError(); 12172 } 12173 } 12174 12175 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12176 return StmtError(); 12177 12178 setFunctionHasBranchProtectedScope(); 12179 12180 DSAStack->setParentTeamsRegionLoc(StartLoc); 12181 12182 return OMPTeamsDistributeSimdDirective::Create( 12183 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12184 } 12185 12186 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 12187 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12188 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12189 if (!AStmt) 12190 return StmtError(); 12191 12192 auto *CS = cast<CapturedStmt>(AStmt); 12193 // 1.2.2 OpenMP Language Terminology 12194 // Structured block - An executable statement with a single entry at the 12195 // top and a single exit at the bottom. 12196 // The point of exit cannot be a branch out of the structured block. 12197 // longjmp() and throw() must not violate the entry/exit criteria. 12198 CS->getCapturedDecl()->setNothrow(); 12199 12200 for (int ThisCaptureLevel = 12201 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 12202 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12203 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12204 // 1.2.2 OpenMP Language Terminology 12205 // Structured block - An executable statement with a single entry at the 12206 // top and a single exit at the bottom. 12207 // The point of exit cannot be a branch out of the structured block. 12208 // longjmp() and throw() must not violate the entry/exit criteria. 12209 CS->getCapturedDecl()->setNothrow(); 12210 } 12211 12212 OMPLoopBasedDirective::HelperExprs B; 12213 // In presence of clause 'collapse' with number of loops, it will 12214 // define the nested loops number. 12215 unsigned NestedLoopCount = checkOpenMPLoop( 12216 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12217 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12218 VarsWithImplicitDSA, B); 12219 12220 if (NestedLoopCount == 0) 12221 return StmtError(); 12222 12223 assert((CurContext->isDependentContext() || B.builtAll()) && 12224 "omp for loop exprs were not built"); 12225 12226 if (!CurContext->isDependentContext()) { 12227 // Finalize the clauses that need pre-built expressions for CodeGen. 12228 for (OMPClause *C : Clauses) { 12229 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12230 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12231 B.NumIterations, *this, CurScope, 12232 DSAStack)) 12233 return StmtError(); 12234 } 12235 } 12236 12237 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12238 return StmtError(); 12239 12240 setFunctionHasBranchProtectedScope(); 12241 12242 DSAStack->setParentTeamsRegionLoc(StartLoc); 12243 12244 return OMPTeamsDistributeParallelForSimdDirective::Create( 12245 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12246 } 12247 12248 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 12249 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12250 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12251 if (!AStmt) 12252 return StmtError(); 12253 12254 auto *CS = cast<CapturedStmt>(AStmt); 12255 // 1.2.2 OpenMP Language Terminology 12256 // Structured block - An executable statement with a single entry at the 12257 // top and a single exit at the bottom. 12258 // The point of exit cannot be a branch out of the structured block. 12259 // longjmp() and throw() must not violate the entry/exit criteria. 12260 CS->getCapturedDecl()->setNothrow(); 12261 12262 for (int ThisCaptureLevel = 12263 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 12264 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12265 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12266 // 1.2.2 OpenMP Language Terminology 12267 // Structured block - An executable statement with a single entry at the 12268 // top and a single exit at the bottom. 12269 // The point of exit cannot be a branch out of the structured block. 12270 // longjmp() and throw() must not violate the entry/exit criteria. 12271 CS->getCapturedDecl()->setNothrow(); 12272 } 12273 12274 OMPLoopBasedDirective::HelperExprs B; 12275 // In presence of clause 'collapse' with number of loops, it will 12276 // define the nested loops number. 12277 unsigned NestedLoopCount = checkOpenMPLoop( 12278 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12279 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12280 VarsWithImplicitDSA, B); 12281 12282 if (NestedLoopCount == 0) 12283 return StmtError(); 12284 12285 assert((CurContext->isDependentContext() || B.builtAll()) && 12286 "omp for loop exprs were not built"); 12287 12288 setFunctionHasBranchProtectedScope(); 12289 12290 DSAStack->setParentTeamsRegionLoc(StartLoc); 12291 12292 return OMPTeamsDistributeParallelForDirective::Create( 12293 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12294 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12295 } 12296 12297 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 12298 Stmt *AStmt, 12299 SourceLocation StartLoc, 12300 SourceLocation EndLoc) { 12301 if (!AStmt) 12302 return StmtError(); 12303 12304 auto *CS = cast<CapturedStmt>(AStmt); 12305 // 1.2.2 OpenMP Language Terminology 12306 // Structured block - An executable statement with a single entry at the 12307 // top and a single exit at the bottom. 12308 // The point of exit cannot be a branch out of the structured block. 12309 // longjmp() and throw() must not violate the entry/exit criteria. 12310 CS->getCapturedDecl()->setNothrow(); 12311 12312 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 12313 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12314 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12315 // 1.2.2 OpenMP Language Terminology 12316 // Structured block - An executable statement with a single entry at the 12317 // top and a single exit at the bottom. 12318 // The point of exit cannot be a branch out of the structured block. 12319 // longjmp() and throw() must not violate the entry/exit criteria. 12320 CS->getCapturedDecl()->setNothrow(); 12321 } 12322 setFunctionHasBranchProtectedScope(); 12323 12324 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 12325 AStmt); 12326 } 12327 12328 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 12329 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12330 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12331 if (!AStmt) 12332 return StmtError(); 12333 12334 auto *CS = cast<CapturedStmt>(AStmt); 12335 // 1.2.2 OpenMP Language Terminology 12336 // Structured block - An executable statement with a single entry at the 12337 // top and a single exit at the bottom. 12338 // The point of exit cannot be a branch out of the structured block. 12339 // longjmp() and throw() must not violate the entry/exit criteria. 12340 CS->getCapturedDecl()->setNothrow(); 12341 for (int ThisCaptureLevel = 12342 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 12343 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12344 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12345 // 1.2.2 OpenMP Language Terminology 12346 // Structured block - An executable statement with a single entry at the 12347 // top and a single exit at the bottom. 12348 // The point of exit cannot be a branch out of the structured block. 12349 // longjmp() and throw() must not violate the entry/exit criteria. 12350 CS->getCapturedDecl()->setNothrow(); 12351 } 12352 12353 OMPLoopBasedDirective::HelperExprs B; 12354 // In presence of clause 'collapse' with number of loops, it will 12355 // define the nested loops number. 12356 unsigned NestedLoopCount = checkOpenMPLoop( 12357 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 12358 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12359 VarsWithImplicitDSA, B); 12360 if (NestedLoopCount == 0) 12361 return StmtError(); 12362 12363 assert((CurContext->isDependentContext() || B.builtAll()) && 12364 "omp target teams distribute loop exprs were not built"); 12365 12366 setFunctionHasBranchProtectedScope(); 12367 return OMPTargetTeamsDistributeDirective::Create( 12368 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12369 } 12370 12371 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 12372 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12373 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12374 if (!AStmt) 12375 return StmtError(); 12376 12377 auto *CS = cast<CapturedStmt>(AStmt); 12378 // 1.2.2 OpenMP Language Terminology 12379 // Structured block - An executable statement with a single entry at the 12380 // top and a single exit at the bottom. 12381 // The point of exit cannot be a branch out of the structured block. 12382 // longjmp() and throw() must not violate the entry/exit criteria. 12383 CS->getCapturedDecl()->setNothrow(); 12384 for (int ThisCaptureLevel = 12385 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 12386 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12387 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12388 // 1.2.2 OpenMP Language Terminology 12389 // Structured block - An executable statement with a single entry at the 12390 // top and a single exit at the bottom. 12391 // The point of exit cannot be a branch out of the structured block. 12392 // longjmp() and throw() must not violate the entry/exit criteria. 12393 CS->getCapturedDecl()->setNothrow(); 12394 } 12395 12396 OMPLoopBasedDirective::HelperExprs B; 12397 // In presence of clause 'collapse' with number of loops, it will 12398 // define the nested loops number. 12399 unsigned NestedLoopCount = checkOpenMPLoop( 12400 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12401 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12402 VarsWithImplicitDSA, B); 12403 if (NestedLoopCount == 0) 12404 return StmtError(); 12405 12406 assert((CurContext->isDependentContext() || B.builtAll()) && 12407 "omp target teams distribute parallel for loop exprs were not built"); 12408 12409 if (!CurContext->isDependentContext()) { 12410 // Finalize the clauses that need pre-built expressions for CodeGen. 12411 for (OMPClause *C : Clauses) { 12412 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12413 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12414 B.NumIterations, *this, CurScope, 12415 DSAStack)) 12416 return StmtError(); 12417 } 12418 } 12419 12420 setFunctionHasBranchProtectedScope(); 12421 return OMPTargetTeamsDistributeParallelForDirective::Create( 12422 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12423 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12424 } 12425 12426 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 12427 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12428 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12429 if (!AStmt) 12430 return StmtError(); 12431 12432 auto *CS = cast<CapturedStmt>(AStmt); 12433 // 1.2.2 OpenMP Language Terminology 12434 // Structured block - An executable statement with a single entry at the 12435 // top and a single exit at the bottom. 12436 // The point of exit cannot be a branch out of the structured block. 12437 // longjmp() and throw() must not violate the entry/exit criteria. 12438 CS->getCapturedDecl()->setNothrow(); 12439 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 12440 OMPD_target_teams_distribute_parallel_for_simd); 12441 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12442 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12443 // 1.2.2 OpenMP Language Terminology 12444 // Structured block - An executable statement with a single entry at the 12445 // top and a single exit at the bottom. 12446 // The point of exit cannot be a branch out of the structured block. 12447 // longjmp() and throw() must not violate the entry/exit criteria. 12448 CS->getCapturedDecl()->setNothrow(); 12449 } 12450 12451 OMPLoopBasedDirective::HelperExprs B; 12452 // In presence of clause 'collapse' with number of loops, it will 12453 // define the nested loops number. 12454 unsigned NestedLoopCount = 12455 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 12456 getCollapseNumberExpr(Clauses), 12457 nullptr /*ordered not a clause on distribute*/, CS, *this, 12458 *DSAStack, VarsWithImplicitDSA, B); 12459 if (NestedLoopCount == 0) 12460 return StmtError(); 12461 12462 assert((CurContext->isDependentContext() || B.builtAll()) && 12463 "omp target teams distribute parallel for simd loop exprs were not " 12464 "built"); 12465 12466 if (!CurContext->isDependentContext()) { 12467 // Finalize the clauses that need pre-built expressions for CodeGen. 12468 for (OMPClause *C : Clauses) { 12469 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12470 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12471 B.NumIterations, *this, CurScope, 12472 DSAStack)) 12473 return StmtError(); 12474 } 12475 } 12476 12477 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12478 return StmtError(); 12479 12480 setFunctionHasBranchProtectedScope(); 12481 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 12482 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12483 } 12484 12485 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 12486 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12487 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12488 if (!AStmt) 12489 return StmtError(); 12490 12491 auto *CS = cast<CapturedStmt>(AStmt); 12492 // 1.2.2 OpenMP Language Terminology 12493 // Structured block - An executable statement with a single entry at the 12494 // top and a single exit at the bottom. 12495 // The point of exit cannot be a branch out of the structured block. 12496 // longjmp() and throw() must not violate the entry/exit criteria. 12497 CS->getCapturedDecl()->setNothrow(); 12498 for (int ThisCaptureLevel = 12499 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 12500 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12501 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12502 // 1.2.2 OpenMP Language Terminology 12503 // Structured block - An executable statement with a single entry at the 12504 // top and a single exit at the bottom. 12505 // The point of exit cannot be a branch out of the structured block. 12506 // longjmp() and throw() must not violate the entry/exit criteria. 12507 CS->getCapturedDecl()->setNothrow(); 12508 } 12509 12510 OMPLoopBasedDirective::HelperExprs B; 12511 // In presence of clause 'collapse' with number of loops, it will 12512 // define the nested loops number. 12513 unsigned NestedLoopCount = checkOpenMPLoop( 12514 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12515 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12516 VarsWithImplicitDSA, B); 12517 if (NestedLoopCount == 0) 12518 return StmtError(); 12519 12520 assert((CurContext->isDependentContext() || B.builtAll()) && 12521 "omp target teams distribute simd loop exprs were not built"); 12522 12523 if (!CurContext->isDependentContext()) { 12524 // Finalize the clauses that need pre-built expressions for CodeGen. 12525 for (OMPClause *C : Clauses) { 12526 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12527 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12528 B.NumIterations, *this, CurScope, 12529 DSAStack)) 12530 return StmtError(); 12531 } 12532 } 12533 12534 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12535 return StmtError(); 12536 12537 setFunctionHasBranchProtectedScope(); 12538 return OMPTargetTeamsDistributeSimdDirective::Create( 12539 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12540 } 12541 12542 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses, 12543 Stmt *AStmt, SourceLocation StartLoc, 12544 SourceLocation EndLoc) { 12545 auto SizesClauses = 12546 OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses); 12547 if (SizesClauses.empty()) { 12548 // A missing 'sizes' clause is already reported by the parser. 12549 return StmtError(); 12550 } 12551 const OMPSizesClause *SizesClause = *SizesClauses.begin(); 12552 unsigned NumLoops = SizesClause->getNumSizes(); 12553 12554 // Empty statement should only be possible if there already was an error. 12555 if (!AStmt) 12556 return StmtError(); 12557 12558 // Verify and diagnose loop nest. 12559 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops); 12560 Stmt *Body = nullptr; 12561 SmallVector<Stmt *, 4> OriginalInits; 12562 if (!OMPLoopBasedDirective::doForAllLoops( 12563 AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, 12564 NumLoops, 12565 [this, &LoopHelpers, &Body, &OriginalInits](unsigned Cnt, 12566 Stmt *CurStmt) { 12567 VarsWithInheritedDSAType TmpDSA; 12568 unsigned SingleNumLoops = 12569 checkOpenMPLoop(OMPD_tile, nullptr, nullptr, CurStmt, *this, 12570 *DSAStack, TmpDSA, LoopHelpers[Cnt]); 12571 if (SingleNumLoops == 0) 12572 return true; 12573 assert(SingleNumLoops == 1 && "Expect single loop iteration space"); 12574 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 12575 OriginalInits.push_back(For->getInit()); 12576 Body = For->getBody(); 12577 } else { 12578 assert(isa<CXXForRangeStmt>(CurStmt) && 12579 "Expected canonical for or range-based for loops."); 12580 auto *CXXFor = cast<CXXForRangeStmt>(CurStmt); 12581 OriginalInits.push_back(CXXFor->getBeginStmt()); 12582 Body = CXXFor->getBody(); 12583 } 12584 return false; 12585 })) 12586 return StmtError(); 12587 12588 // Delay tiling to when template is completely instantiated. 12589 if (CurContext->isDependentContext()) 12590 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, 12591 NumLoops, AStmt, nullptr, nullptr); 12592 12593 // Collection of generated variable declaration. 12594 SmallVector<Decl *, 4> PreInits; 12595 12596 // Create iteration variables for the generated loops. 12597 SmallVector<VarDecl *, 4> FloorIndVars; 12598 SmallVector<VarDecl *, 4> TileIndVars; 12599 FloorIndVars.resize(NumLoops); 12600 TileIndVars.resize(NumLoops); 12601 for (unsigned I = 0; I < NumLoops; ++I) { 12602 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12603 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 12604 PreInits.append(PI->decl_begin(), PI->decl_end()); 12605 assert(LoopHelper.Counters.size() == 1 && 12606 "Expect single-dimensional loop iteration space"); 12607 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 12608 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString(); 12609 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 12610 QualType CntTy = IterVarRef->getType(); 12611 12612 // Iteration variable for the floor (i.e. outer) loop. 12613 { 12614 std::string FloorCntName = 12615 (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12616 VarDecl *FloorCntDecl = 12617 buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar); 12618 FloorIndVars[I] = FloorCntDecl; 12619 } 12620 12621 // Iteration variable for the tile (i.e. inner) loop. 12622 { 12623 std::string TileCntName = 12624 (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12625 12626 // Reuse the iteration variable created by checkOpenMPLoop. It is also 12627 // used by the expressions to derive the original iteration variable's 12628 // value from the logical iteration number. 12629 auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl()); 12630 TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName)); 12631 TileIndVars[I] = TileCntDecl; 12632 } 12633 if (auto *PI = dyn_cast_or_null<DeclStmt>(OriginalInits[I])) 12634 PreInits.append(PI->decl_begin(), PI->decl_end()); 12635 // Gather declarations for the data members used as counters. 12636 for (Expr *CounterRef : LoopHelper.Counters) { 12637 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 12638 if (isa<OMPCapturedExprDecl>(CounterDecl)) 12639 PreInits.push_back(CounterDecl); 12640 } 12641 } 12642 12643 // Once the original iteration values are set, append the innermost body. 12644 Stmt *Inner = Body; 12645 12646 // Create tile loops from the inside to the outside. 12647 for (int I = NumLoops - 1; I >= 0; --I) { 12648 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12649 Expr *NumIterations = LoopHelper.NumIterations; 12650 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12651 QualType CntTy = OrigCntVar->getType(); 12652 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12653 Scope *CurScope = getCurScope(); 12654 12655 // Commonly used variables. 12656 DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy, 12657 OrigCntVar->getExprLoc()); 12658 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12659 OrigCntVar->getExprLoc()); 12660 12661 // For init-statement: auto .tile.iv = .floor.iv 12662 AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(), 12663 /*DirectInit=*/false); 12664 Decl *CounterDecl = TileIndVars[I]; 12665 StmtResult InitStmt = new (Context) 12666 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12667 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12668 if (!InitStmt.isUsable()) 12669 return StmtError(); 12670 12671 // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize, 12672 // NumIterations) 12673 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12674 BO_Add, FloorIV, DimTileSize); 12675 if (!EndOfTile.isUsable()) 12676 return StmtError(); 12677 ExprResult IsPartialTile = 12678 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, 12679 NumIterations, EndOfTile.get()); 12680 if (!IsPartialTile.isUsable()) 12681 return StmtError(); 12682 ExprResult MinTileAndIterSpace = ActOnConditionalOp( 12683 LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(), 12684 IsPartialTile.get(), NumIterations, EndOfTile.get()); 12685 if (!MinTileAndIterSpace.isUsable()) 12686 return StmtError(); 12687 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12688 BO_LT, TileIV, MinTileAndIterSpace.get()); 12689 if (!CondExpr.isUsable()) 12690 return StmtError(); 12691 12692 // For incr-statement: ++.tile.iv 12693 ExprResult IncrStmt = 12694 BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV); 12695 if (!IncrStmt.isUsable()) 12696 return StmtError(); 12697 12698 // Statements to set the original iteration variable's value from the 12699 // logical iteration number. 12700 // Generated for loop is: 12701 // Original_for_init; 12702 // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize, 12703 // NumIterations); ++.tile.iv) { 12704 // Original_Body; 12705 // Original_counter_update; 12706 // } 12707 // FIXME: If the innermost body is an loop itself, inserting these 12708 // statements stops it being recognized as a perfectly nested loop (e.g. 12709 // for applying tiling again). If this is the case, sink the expressions 12710 // further into the inner loop. 12711 SmallVector<Stmt *, 4> BodyParts; 12712 BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 12713 BodyParts.push_back(Inner); 12714 Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(), 12715 Inner->getEndLoc()); 12716 Inner = new (Context) 12717 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12718 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12719 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12720 } 12721 12722 // Create floor loops from the inside to the outside. 12723 for (int I = NumLoops - 1; I >= 0; --I) { 12724 auto &LoopHelper = LoopHelpers[I]; 12725 Expr *NumIterations = LoopHelper.NumIterations; 12726 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12727 QualType CntTy = OrigCntVar->getType(); 12728 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12729 Scope *CurScope = getCurScope(); 12730 12731 // Commonly used variables. 12732 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12733 OrigCntVar->getExprLoc()); 12734 12735 // For init-statement: auto .floor.iv = 0 12736 AddInitializerToDecl( 12737 FloorIndVars[I], 12738 ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 12739 /*DirectInit=*/false); 12740 Decl *CounterDecl = FloorIndVars[I]; 12741 StmtResult InitStmt = new (Context) 12742 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12743 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12744 if (!InitStmt.isUsable()) 12745 return StmtError(); 12746 12747 // For cond-expression: .floor.iv < NumIterations 12748 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12749 BO_LT, FloorIV, NumIterations); 12750 if (!CondExpr.isUsable()) 12751 return StmtError(); 12752 12753 // For incr-statement: .floor.iv += DimTileSize 12754 ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), 12755 BO_AddAssign, FloorIV, DimTileSize); 12756 if (!IncrStmt.isUsable()) 12757 return StmtError(); 12758 12759 Inner = new (Context) 12760 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12761 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12762 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12763 } 12764 12765 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops, 12766 AStmt, Inner, 12767 buildPreInits(Context, PreInits)); 12768 } 12769 12770 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 12771 SourceLocation StartLoc, 12772 SourceLocation LParenLoc, 12773 SourceLocation EndLoc) { 12774 OMPClause *Res = nullptr; 12775 switch (Kind) { 12776 case OMPC_final: 12777 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 12778 break; 12779 case OMPC_num_threads: 12780 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 12781 break; 12782 case OMPC_safelen: 12783 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 12784 break; 12785 case OMPC_simdlen: 12786 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 12787 break; 12788 case OMPC_allocator: 12789 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 12790 break; 12791 case OMPC_collapse: 12792 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 12793 break; 12794 case OMPC_ordered: 12795 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 12796 break; 12797 case OMPC_num_teams: 12798 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 12799 break; 12800 case OMPC_thread_limit: 12801 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 12802 break; 12803 case OMPC_priority: 12804 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 12805 break; 12806 case OMPC_grainsize: 12807 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 12808 break; 12809 case OMPC_num_tasks: 12810 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 12811 break; 12812 case OMPC_hint: 12813 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 12814 break; 12815 case OMPC_depobj: 12816 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 12817 break; 12818 case OMPC_detach: 12819 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 12820 break; 12821 case OMPC_novariants: 12822 Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc); 12823 break; 12824 case OMPC_nocontext: 12825 Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc); 12826 break; 12827 case OMPC_filter: 12828 Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc); 12829 break; 12830 case OMPC_device: 12831 case OMPC_if: 12832 case OMPC_default: 12833 case OMPC_proc_bind: 12834 case OMPC_schedule: 12835 case OMPC_private: 12836 case OMPC_firstprivate: 12837 case OMPC_lastprivate: 12838 case OMPC_shared: 12839 case OMPC_reduction: 12840 case OMPC_task_reduction: 12841 case OMPC_in_reduction: 12842 case OMPC_linear: 12843 case OMPC_aligned: 12844 case OMPC_copyin: 12845 case OMPC_copyprivate: 12846 case OMPC_nowait: 12847 case OMPC_untied: 12848 case OMPC_mergeable: 12849 case OMPC_threadprivate: 12850 case OMPC_sizes: 12851 case OMPC_allocate: 12852 case OMPC_flush: 12853 case OMPC_read: 12854 case OMPC_write: 12855 case OMPC_update: 12856 case OMPC_capture: 12857 case OMPC_seq_cst: 12858 case OMPC_acq_rel: 12859 case OMPC_acquire: 12860 case OMPC_release: 12861 case OMPC_relaxed: 12862 case OMPC_depend: 12863 case OMPC_threads: 12864 case OMPC_simd: 12865 case OMPC_map: 12866 case OMPC_nogroup: 12867 case OMPC_dist_schedule: 12868 case OMPC_defaultmap: 12869 case OMPC_unknown: 12870 case OMPC_uniform: 12871 case OMPC_to: 12872 case OMPC_from: 12873 case OMPC_use_device_ptr: 12874 case OMPC_use_device_addr: 12875 case OMPC_is_device_ptr: 12876 case OMPC_unified_address: 12877 case OMPC_unified_shared_memory: 12878 case OMPC_reverse_offload: 12879 case OMPC_dynamic_allocators: 12880 case OMPC_atomic_default_mem_order: 12881 case OMPC_device_type: 12882 case OMPC_match: 12883 case OMPC_nontemporal: 12884 case OMPC_order: 12885 case OMPC_destroy: 12886 case OMPC_inclusive: 12887 case OMPC_exclusive: 12888 case OMPC_uses_allocators: 12889 case OMPC_affinity: 12890 default: 12891 llvm_unreachable("Clause is not allowed."); 12892 } 12893 return Res; 12894 } 12895 12896 // An OpenMP directive such as 'target parallel' has two captured regions: 12897 // for the 'target' and 'parallel' respectively. This function returns 12898 // the region in which to capture expressions associated with a clause. 12899 // A return value of OMPD_unknown signifies that the expression should not 12900 // be captured. 12901 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 12902 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 12903 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 12904 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12905 switch (CKind) { 12906 case OMPC_if: 12907 switch (DKind) { 12908 case OMPD_target_parallel_for_simd: 12909 if (OpenMPVersion >= 50 && 12910 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 12911 CaptureRegion = OMPD_parallel; 12912 break; 12913 } 12914 LLVM_FALLTHROUGH; 12915 case OMPD_target_parallel: 12916 case OMPD_target_parallel_for: 12917 // If this clause applies to the nested 'parallel' region, capture within 12918 // the 'target' region, otherwise do not capture. 12919 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 12920 CaptureRegion = OMPD_target; 12921 break; 12922 case OMPD_target_teams_distribute_parallel_for_simd: 12923 if (OpenMPVersion >= 50 && 12924 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 12925 CaptureRegion = OMPD_parallel; 12926 break; 12927 } 12928 LLVM_FALLTHROUGH; 12929 case OMPD_target_teams_distribute_parallel_for: 12930 // If this clause applies to the nested 'parallel' region, capture within 12931 // the 'teams' region, otherwise do not capture. 12932 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 12933 CaptureRegion = OMPD_teams; 12934 break; 12935 case OMPD_teams_distribute_parallel_for_simd: 12936 if (OpenMPVersion >= 50 && 12937 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 12938 CaptureRegion = OMPD_parallel; 12939 break; 12940 } 12941 LLVM_FALLTHROUGH; 12942 case OMPD_teams_distribute_parallel_for: 12943 CaptureRegion = OMPD_teams; 12944 break; 12945 case OMPD_target_update: 12946 case OMPD_target_enter_data: 12947 case OMPD_target_exit_data: 12948 CaptureRegion = OMPD_task; 12949 break; 12950 case OMPD_parallel_master_taskloop: 12951 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 12952 CaptureRegion = OMPD_parallel; 12953 break; 12954 case OMPD_parallel_master_taskloop_simd: 12955 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 12956 NameModifier == OMPD_taskloop) { 12957 CaptureRegion = OMPD_parallel; 12958 break; 12959 } 12960 if (OpenMPVersion <= 45) 12961 break; 12962 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12963 CaptureRegion = OMPD_taskloop; 12964 break; 12965 case OMPD_parallel_for_simd: 12966 if (OpenMPVersion <= 45) 12967 break; 12968 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12969 CaptureRegion = OMPD_parallel; 12970 break; 12971 case OMPD_taskloop_simd: 12972 case OMPD_master_taskloop_simd: 12973 if (OpenMPVersion <= 45) 12974 break; 12975 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12976 CaptureRegion = OMPD_taskloop; 12977 break; 12978 case OMPD_distribute_parallel_for_simd: 12979 if (OpenMPVersion <= 45) 12980 break; 12981 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 12982 CaptureRegion = OMPD_parallel; 12983 break; 12984 case OMPD_target_simd: 12985 if (OpenMPVersion >= 50 && 12986 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 12987 CaptureRegion = OMPD_target; 12988 break; 12989 case OMPD_teams_distribute_simd: 12990 case OMPD_target_teams_distribute_simd: 12991 if (OpenMPVersion >= 50 && 12992 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 12993 CaptureRegion = OMPD_teams; 12994 break; 12995 case OMPD_cancel: 12996 case OMPD_parallel: 12997 case OMPD_parallel_master: 12998 case OMPD_parallel_sections: 12999 case OMPD_parallel_for: 13000 case OMPD_target: 13001 case OMPD_target_teams: 13002 case OMPD_target_teams_distribute: 13003 case OMPD_distribute_parallel_for: 13004 case OMPD_task: 13005 case OMPD_taskloop: 13006 case OMPD_master_taskloop: 13007 case OMPD_target_data: 13008 case OMPD_simd: 13009 case OMPD_for_simd: 13010 case OMPD_distribute_simd: 13011 // Do not capture if-clause expressions. 13012 break; 13013 case OMPD_threadprivate: 13014 case OMPD_allocate: 13015 case OMPD_taskyield: 13016 case OMPD_barrier: 13017 case OMPD_taskwait: 13018 case OMPD_cancellation_point: 13019 case OMPD_flush: 13020 case OMPD_depobj: 13021 case OMPD_scan: 13022 case OMPD_declare_reduction: 13023 case OMPD_declare_mapper: 13024 case OMPD_declare_simd: 13025 case OMPD_declare_variant: 13026 case OMPD_begin_declare_variant: 13027 case OMPD_end_declare_variant: 13028 case OMPD_declare_target: 13029 case OMPD_end_declare_target: 13030 case OMPD_teams: 13031 case OMPD_tile: 13032 case OMPD_for: 13033 case OMPD_sections: 13034 case OMPD_section: 13035 case OMPD_single: 13036 case OMPD_master: 13037 case OMPD_masked: 13038 case OMPD_critical: 13039 case OMPD_taskgroup: 13040 case OMPD_distribute: 13041 case OMPD_ordered: 13042 case OMPD_atomic: 13043 case OMPD_teams_distribute: 13044 case OMPD_requires: 13045 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 13046 case OMPD_unknown: 13047 default: 13048 llvm_unreachable("Unknown OpenMP directive"); 13049 } 13050 break; 13051 case OMPC_num_threads: 13052 switch (DKind) { 13053 case OMPD_target_parallel: 13054 case OMPD_target_parallel_for: 13055 case OMPD_target_parallel_for_simd: 13056 CaptureRegion = OMPD_target; 13057 break; 13058 case OMPD_teams_distribute_parallel_for: 13059 case OMPD_teams_distribute_parallel_for_simd: 13060 case OMPD_target_teams_distribute_parallel_for: 13061 case OMPD_target_teams_distribute_parallel_for_simd: 13062 CaptureRegion = OMPD_teams; 13063 break; 13064 case OMPD_parallel: 13065 case OMPD_parallel_master: 13066 case OMPD_parallel_sections: 13067 case OMPD_parallel_for: 13068 case OMPD_parallel_for_simd: 13069 case OMPD_distribute_parallel_for: 13070 case OMPD_distribute_parallel_for_simd: 13071 case OMPD_parallel_master_taskloop: 13072 case OMPD_parallel_master_taskloop_simd: 13073 // Do not capture num_threads-clause expressions. 13074 break; 13075 case OMPD_target_data: 13076 case OMPD_target_enter_data: 13077 case OMPD_target_exit_data: 13078 case OMPD_target_update: 13079 case OMPD_target: 13080 case OMPD_target_simd: 13081 case OMPD_target_teams: 13082 case OMPD_target_teams_distribute: 13083 case OMPD_target_teams_distribute_simd: 13084 case OMPD_cancel: 13085 case OMPD_task: 13086 case OMPD_taskloop: 13087 case OMPD_taskloop_simd: 13088 case OMPD_master_taskloop: 13089 case OMPD_master_taskloop_simd: 13090 case OMPD_threadprivate: 13091 case OMPD_allocate: 13092 case OMPD_taskyield: 13093 case OMPD_barrier: 13094 case OMPD_taskwait: 13095 case OMPD_cancellation_point: 13096 case OMPD_flush: 13097 case OMPD_depobj: 13098 case OMPD_scan: 13099 case OMPD_declare_reduction: 13100 case OMPD_declare_mapper: 13101 case OMPD_declare_simd: 13102 case OMPD_declare_variant: 13103 case OMPD_begin_declare_variant: 13104 case OMPD_end_declare_variant: 13105 case OMPD_declare_target: 13106 case OMPD_end_declare_target: 13107 case OMPD_teams: 13108 case OMPD_simd: 13109 case OMPD_tile: 13110 case OMPD_for: 13111 case OMPD_for_simd: 13112 case OMPD_sections: 13113 case OMPD_section: 13114 case OMPD_single: 13115 case OMPD_master: 13116 case OMPD_masked: 13117 case OMPD_critical: 13118 case OMPD_taskgroup: 13119 case OMPD_distribute: 13120 case OMPD_ordered: 13121 case OMPD_atomic: 13122 case OMPD_distribute_simd: 13123 case OMPD_teams_distribute: 13124 case OMPD_teams_distribute_simd: 13125 case OMPD_requires: 13126 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 13127 case OMPD_unknown: 13128 default: 13129 llvm_unreachable("Unknown OpenMP directive"); 13130 } 13131 break; 13132 case OMPC_num_teams: 13133 switch (DKind) { 13134 case OMPD_target_teams: 13135 case OMPD_target_teams_distribute: 13136 case OMPD_target_teams_distribute_simd: 13137 case OMPD_target_teams_distribute_parallel_for: 13138 case OMPD_target_teams_distribute_parallel_for_simd: 13139 CaptureRegion = OMPD_target; 13140 break; 13141 case OMPD_teams_distribute_parallel_for: 13142 case OMPD_teams_distribute_parallel_for_simd: 13143 case OMPD_teams: 13144 case OMPD_teams_distribute: 13145 case OMPD_teams_distribute_simd: 13146 // Do not capture num_teams-clause expressions. 13147 break; 13148 case OMPD_distribute_parallel_for: 13149 case OMPD_distribute_parallel_for_simd: 13150 case OMPD_task: 13151 case OMPD_taskloop: 13152 case OMPD_taskloop_simd: 13153 case OMPD_master_taskloop: 13154 case OMPD_master_taskloop_simd: 13155 case OMPD_parallel_master_taskloop: 13156 case OMPD_parallel_master_taskloop_simd: 13157 case OMPD_target_data: 13158 case OMPD_target_enter_data: 13159 case OMPD_target_exit_data: 13160 case OMPD_target_update: 13161 case OMPD_cancel: 13162 case OMPD_parallel: 13163 case OMPD_parallel_master: 13164 case OMPD_parallel_sections: 13165 case OMPD_parallel_for: 13166 case OMPD_parallel_for_simd: 13167 case OMPD_target: 13168 case OMPD_target_simd: 13169 case OMPD_target_parallel: 13170 case OMPD_target_parallel_for: 13171 case OMPD_target_parallel_for_simd: 13172 case OMPD_threadprivate: 13173 case OMPD_allocate: 13174 case OMPD_taskyield: 13175 case OMPD_barrier: 13176 case OMPD_taskwait: 13177 case OMPD_cancellation_point: 13178 case OMPD_flush: 13179 case OMPD_depobj: 13180 case OMPD_scan: 13181 case OMPD_declare_reduction: 13182 case OMPD_declare_mapper: 13183 case OMPD_declare_simd: 13184 case OMPD_declare_variant: 13185 case OMPD_begin_declare_variant: 13186 case OMPD_end_declare_variant: 13187 case OMPD_declare_target: 13188 case OMPD_end_declare_target: 13189 case OMPD_simd: 13190 case OMPD_tile: 13191 case OMPD_for: 13192 case OMPD_for_simd: 13193 case OMPD_sections: 13194 case OMPD_section: 13195 case OMPD_single: 13196 case OMPD_master: 13197 case OMPD_masked: 13198 case OMPD_critical: 13199 case OMPD_taskgroup: 13200 case OMPD_distribute: 13201 case OMPD_ordered: 13202 case OMPD_atomic: 13203 case OMPD_distribute_simd: 13204 case OMPD_requires: 13205 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 13206 case OMPD_unknown: 13207 default: 13208 llvm_unreachable("Unknown OpenMP directive"); 13209 } 13210 break; 13211 case OMPC_thread_limit: 13212 switch (DKind) { 13213 case OMPD_target_teams: 13214 case OMPD_target_teams_distribute: 13215 case OMPD_target_teams_distribute_simd: 13216 case OMPD_target_teams_distribute_parallel_for: 13217 case OMPD_target_teams_distribute_parallel_for_simd: 13218 CaptureRegion = OMPD_target; 13219 break; 13220 case OMPD_teams_distribute_parallel_for: 13221 case OMPD_teams_distribute_parallel_for_simd: 13222 case OMPD_teams: 13223 case OMPD_teams_distribute: 13224 case OMPD_teams_distribute_simd: 13225 // Do not capture thread_limit-clause expressions. 13226 break; 13227 case OMPD_distribute_parallel_for: 13228 case OMPD_distribute_parallel_for_simd: 13229 case OMPD_task: 13230 case OMPD_taskloop: 13231 case OMPD_taskloop_simd: 13232 case OMPD_master_taskloop: 13233 case OMPD_master_taskloop_simd: 13234 case OMPD_parallel_master_taskloop: 13235 case OMPD_parallel_master_taskloop_simd: 13236 case OMPD_target_data: 13237 case OMPD_target_enter_data: 13238 case OMPD_target_exit_data: 13239 case OMPD_target_update: 13240 case OMPD_cancel: 13241 case OMPD_parallel: 13242 case OMPD_parallel_master: 13243 case OMPD_parallel_sections: 13244 case OMPD_parallel_for: 13245 case OMPD_parallel_for_simd: 13246 case OMPD_target: 13247 case OMPD_target_simd: 13248 case OMPD_target_parallel: 13249 case OMPD_target_parallel_for: 13250 case OMPD_target_parallel_for_simd: 13251 case OMPD_threadprivate: 13252 case OMPD_allocate: 13253 case OMPD_taskyield: 13254 case OMPD_barrier: 13255 case OMPD_taskwait: 13256 case OMPD_cancellation_point: 13257 case OMPD_flush: 13258 case OMPD_depobj: 13259 case OMPD_scan: 13260 case OMPD_declare_reduction: 13261 case OMPD_declare_mapper: 13262 case OMPD_declare_simd: 13263 case OMPD_declare_variant: 13264 case OMPD_begin_declare_variant: 13265 case OMPD_end_declare_variant: 13266 case OMPD_declare_target: 13267 case OMPD_end_declare_target: 13268 case OMPD_simd: 13269 case OMPD_tile: 13270 case OMPD_for: 13271 case OMPD_for_simd: 13272 case OMPD_sections: 13273 case OMPD_section: 13274 case OMPD_single: 13275 case OMPD_master: 13276 case OMPD_masked: 13277 case OMPD_critical: 13278 case OMPD_taskgroup: 13279 case OMPD_distribute: 13280 case OMPD_ordered: 13281 case OMPD_atomic: 13282 case OMPD_distribute_simd: 13283 case OMPD_requires: 13284 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 13285 case OMPD_unknown: 13286 default: 13287 llvm_unreachable("Unknown OpenMP directive"); 13288 } 13289 break; 13290 case OMPC_schedule: 13291 switch (DKind) { 13292 case OMPD_parallel_for: 13293 case OMPD_parallel_for_simd: 13294 case OMPD_distribute_parallel_for: 13295 case OMPD_distribute_parallel_for_simd: 13296 case OMPD_teams_distribute_parallel_for: 13297 case OMPD_teams_distribute_parallel_for_simd: 13298 case OMPD_target_parallel_for: 13299 case OMPD_target_parallel_for_simd: 13300 case OMPD_target_teams_distribute_parallel_for: 13301 case OMPD_target_teams_distribute_parallel_for_simd: 13302 CaptureRegion = OMPD_parallel; 13303 break; 13304 case OMPD_for: 13305 case OMPD_for_simd: 13306 // Do not capture schedule-clause expressions. 13307 break; 13308 case OMPD_task: 13309 case OMPD_taskloop: 13310 case OMPD_taskloop_simd: 13311 case OMPD_master_taskloop: 13312 case OMPD_master_taskloop_simd: 13313 case OMPD_parallel_master_taskloop: 13314 case OMPD_parallel_master_taskloop_simd: 13315 case OMPD_target_data: 13316 case OMPD_target_enter_data: 13317 case OMPD_target_exit_data: 13318 case OMPD_target_update: 13319 case OMPD_teams: 13320 case OMPD_teams_distribute: 13321 case OMPD_teams_distribute_simd: 13322 case OMPD_target_teams_distribute: 13323 case OMPD_target_teams_distribute_simd: 13324 case OMPD_target: 13325 case OMPD_target_simd: 13326 case OMPD_target_parallel: 13327 case OMPD_cancel: 13328 case OMPD_parallel: 13329 case OMPD_parallel_master: 13330 case OMPD_parallel_sections: 13331 case OMPD_threadprivate: 13332 case OMPD_allocate: 13333 case OMPD_taskyield: 13334 case OMPD_barrier: 13335 case OMPD_taskwait: 13336 case OMPD_cancellation_point: 13337 case OMPD_flush: 13338 case OMPD_depobj: 13339 case OMPD_scan: 13340 case OMPD_declare_reduction: 13341 case OMPD_declare_mapper: 13342 case OMPD_declare_simd: 13343 case OMPD_declare_variant: 13344 case OMPD_begin_declare_variant: 13345 case OMPD_end_declare_variant: 13346 case OMPD_declare_target: 13347 case OMPD_end_declare_target: 13348 case OMPD_simd: 13349 case OMPD_tile: 13350 case OMPD_sections: 13351 case OMPD_section: 13352 case OMPD_single: 13353 case OMPD_master: 13354 case OMPD_masked: 13355 case OMPD_critical: 13356 case OMPD_taskgroup: 13357 case OMPD_distribute: 13358 case OMPD_ordered: 13359 case OMPD_atomic: 13360 case OMPD_distribute_simd: 13361 case OMPD_target_teams: 13362 case OMPD_requires: 13363 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 13364 case OMPD_unknown: 13365 default: 13366 llvm_unreachable("Unknown OpenMP directive"); 13367 } 13368 break; 13369 case OMPC_dist_schedule: 13370 switch (DKind) { 13371 case OMPD_teams_distribute_parallel_for: 13372 case OMPD_teams_distribute_parallel_for_simd: 13373 case OMPD_teams_distribute: 13374 case OMPD_teams_distribute_simd: 13375 case OMPD_target_teams_distribute_parallel_for: 13376 case OMPD_target_teams_distribute_parallel_for_simd: 13377 case OMPD_target_teams_distribute: 13378 case OMPD_target_teams_distribute_simd: 13379 CaptureRegion = OMPD_teams; 13380 break; 13381 case OMPD_distribute_parallel_for: 13382 case OMPD_distribute_parallel_for_simd: 13383 case OMPD_distribute: 13384 case OMPD_distribute_simd: 13385 // Do not capture dist_schedule-clause expressions. 13386 break; 13387 case OMPD_parallel_for: 13388 case OMPD_parallel_for_simd: 13389 case OMPD_target_parallel_for_simd: 13390 case OMPD_target_parallel_for: 13391 case OMPD_task: 13392 case OMPD_taskloop: 13393 case OMPD_taskloop_simd: 13394 case OMPD_master_taskloop: 13395 case OMPD_master_taskloop_simd: 13396 case OMPD_parallel_master_taskloop: 13397 case OMPD_parallel_master_taskloop_simd: 13398 case OMPD_target_data: 13399 case OMPD_target_enter_data: 13400 case OMPD_target_exit_data: 13401 case OMPD_target_update: 13402 case OMPD_teams: 13403 case OMPD_target: 13404 case OMPD_target_simd: 13405 case OMPD_target_parallel: 13406 case OMPD_cancel: 13407 case OMPD_parallel: 13408 case OMPD_parallel_master: 13409 case OMPD_parallel_sections: 13410 case OMPD_threadprivate: 13411 case OMPD_allocate: 13412 case OMPD_taskyield: 13413 case OMPD_barrier: 13414 case OMPD_taskwait: 13415 case OMPD_cancellation_point: 13416 case OMPD_flush: 13417 case OMPD_depobj: 13418 case OMPD_scan: 13419 case OMPD_declare_reduction: 13420 case OMPD_declare_mapper: 13421 case OMPD_declare_simd: 13422 case OMPD_declare_variant: 13423 case OMPD_begin_declare_variant: 13424 case OMPD_end_declare_variant: 13425 case OMPD_declare_target: 13426 case OMPD_end_declare_target: 13427 case OMPD_simd: 13428 case OMPD_tile: 13429 case OMPD_for: 13430 case OMPD_for_simd: 13431 case OMPD_sections: 13432 case OMPD_section: 13433 case OMPD_single: 13434 case OMPD_master: 13435 case OMPD_masked: 13436 case OMPD_critical: 13437 case OMPD_taskgroup: 13438 case OMPD_ordered: 13439 case OMPD_atomic: 13440 case OMPD_target_teams: 13441 case OMPD_requires: 13442 llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause"); 13443 case OMPD_unknown: 13444 default: 13445 llvm_unreachable("Unknown OpenMP directive"); 13446 } 13447 break; 13448 case OMPC_device: 13449 switch (DKind) { 13450 case OMPD_target_update: 13451 case OMPD_target_enter_data: 13452 case OMPD_target_exit_data: 13453 case OMPD_target: 13454 case OMPD_target_simd: 13455 case OMPD_target_teams: 13456 case OMPD_target_parallel: 13457 case OMPD_target_teams_distribute: 13458 case OMPD_target_teams_distribute_simd: 13459 case OMPD_target_parallel_for: 13460 case OMPD_target_parallel_for_simd: 13461 case OMPD_target_teams_distribute_parallel_for: 13462 case OMPD_target_teams_distribute_parallel_for_simd: 13463 case OMPD_dispatch: 13464 CaptureRegion = OMPD_task; 13465 break; 13466 case OMPD_target_data: 13467 case OMPD_interop: 13468 // Do not capture device-clause expressions. 13469 break; 13470 case OMPD_teams_distribute_parallel_for: 13471 case OMPD_teams_distribute_parallel_for_simd: 13472 case OMPD_teams: 13473 case OMPD_teams_distribute: 13474 case OMPD_teams_distribute_simd: 13475 case OMPD_distribute_parallel_for: 13476 case OMPD_distribute_parallel_for_simd: 13477 case OMPD_task: 13478 case OMPD_taskloop: 13479 case OMPD_taskloop_simd: 13480 case OMPD_master_taskloop: 13481 case OMPD_master_taskloop_simd: 13482 case OMPD_parallel_master_taskloop: 13483 case OMPD_parallel_master_taskloop_simd: 13484 case OMPD_cancel: 13485 case OMPD_parallel: 13486 case OMPD_parallel_master: 13487 case OMPD_parallel_sections: 13488 case OMPD_parallel_for: 13489 case OMPD_parallel_for_simd: 13490 case OMPD_threadprivate: 13491 case OMPD_allocate: 13492 case OMPD_taskyield: 13493 case OMPD_barrier: 13494 case OMPD_taskwait: 13495 case OMPD_cancellation_point: 13496 case OMPD_flush: 13497 case OMPD_depobj: 13498 case OMPD_scan: 13499 case OMPD_declare_reduction: 13500 case OMPD_declare_mapper: 13501 case OMPD_declare_simd: 13502 case OMPD_declare_variant: 13503 case OMPD_begin_declare_variant: 13504 case OMPD_end_declare_variant: 13505 case OMPD_declare_target: 13506 case OMPD_end_declare_target: 13507 case OMPD_simd: 13508 case OMPD_tile: 13509 case OMPD_for: 13510 case OMPD_for_simd: 13511 case OMPD_sections: 13512 case OMPD_section: 13513 case OMPD_single: 13514 case OMPD_master: 13515 case OMPD_masked: 13516 case OMPD_critical: 13517 case OMPD_taskgroup: 13518 case OMPD_distribute: 13519 case OMPD_ordered: 13520 case OMPD_atomic: 13521 case OMPD_distribute_simd: 13522 case OMPD_requires: 13523 llvm_unreachable("Unexpected OpenMP directive with device-clause"); 13524 case OMPD_unknown: 13525 default: 13526 llvm_unreachable("Unknown OpenMP directive"); 13527 } 13528 break; 13529 case OMPC_grainsize: 13530 case OMPC_num_tasks: 13531 case OMPC_final: 13532 case OMPC_priority: 13533 switch (DKind) { 13534 case OMPD_task: 13535 case OMPD_taskloop: 13536 case OMPD_taskloop_simd: 13537 case OMPD_master_taskloop: 13538 case OMPD_master_taskloop_simd: 13539 break; 13540 case OMPD_parallel_master_taskloop: 13541 case OMPD_parallel_master_taskloop_simd: 13542 CaptureRegion = OMPD_parallel; 13543 break; 13544 case OMPD_target_update: 13545 case OMPD_target_enter_data: 13546 case OMPD_target_exit_data: 13547 case OMPD_target: 13548 case OMPD_target_simd: 13549 case OMPD_target_teams: 13550 case OMPD_target_parallel: 13551 case OMPD_target_teams_distribute: 13552 case OMPD_target_teams_distribute_simd: 13553 case OMPD_target_parallel_for: 13554 case OMPD_target_parallel_for_simd: 13555 case OMPD_target_teams_distribute_parallel_for: 13556 case OMPD_target_teams_distribute_parallel_for_simd: 13557 case OMPD_target_data: 13558 case OMPD_teams_distribute_parallel_for: 13559 case OMPD_teams_distribute_parallel_for_simd: 13560 case OMPD_teams: 13561 case OMPD_teams_distribute: 13562 case OMPD_teams_distribute_simd: 13563 case OMPD_distribute_parallel_for: 13564 case OMPD_distribute_parallel_for_simd: 13565 case OMPD_cancel: 13566 case OMPD_parallel: 13567 case OMPD_parallel_master: 13568 case OMPD_parallel_sections: 13569 case OMPD_parallel_for: 13570 case OMPD_parallel_for_simd: 13571 case OMPD_threadprivate: 13572 case OMPD_allocate: 13573 case OMPD_taskyield: 13574 case OMPD_barrier: 13575 case OMPD_taskwait: 13576 case OMPD_cancellation_point: 13577 case OMPD_flush: 13578 case OMPD_depobj: 13579 case OMPD_scan: 13580 case OMPD_declare_reduction: 13581 case OMPD_declare_mapper: 13582 case OMPD_declare_simd: 13583 case OMPD_declare_variant: 13584 case OMPD_begin_declare_variant: 13585 case OMPD_end_declare_variant: 13586 case OMPD_declare_target: 13587 case OMPD_end_declare_target: 13588 case OMPD_simd: 13589 case OMPD_tile: 13590 case OMPD_for: 13591 case OMPD_for_simd: 13592 case OMPD_sections: 13593 case OMPD_section: 13594 case OMPD_single: 13595 case OMPD_master: 13596 case OMPD_masked: 13597 case OMPD_critical: 13598 case OMPD_taskgroup: 13599 case OMPD_distribute: 13600 case OMPD_ordered: 13601 case OMPD_atomic: 13602 case OMPD_distribute_simd: 13603 case OMPD_requires: 13604 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 13605 case OMPD_unknown: 13606 default: 13607 llvm_unreachable("Unknown OpenMP directive"); 13608 } 13609 break; 13610 case OMPC_novariants: 13611 case OMPC_nocontext: 13612 switch (DKind) { 13613 case OMPD_dispatch: 13614 CaptureRegion = OMPD_task; 13615 break; 13616 default: 13617 llvm_unreachable("Unexpected OpenMP directive"); 13618 } 13619 break; 13620 case OMPC_filter: 13621 // Do not capture filter-clause expressions. 13622 break; 13623 case OMPC_firstprivate: 13624 case OMPC_lastprivate: 13625 case OMPC_reduction: 13626 case OMPC_task_reduction: 13627 case OMPC_in_reduction: 13628 case OMPC_linear: 13629 case OMPC_default: 13630 case OMPC_proc_bind: 13631 case OMPC_safelen: 13632 case OMPC_simdlen: 13633 case OMPC_sizes: 13634 case OMPC_allocator: 13635 case OMPC_collapse: 13636 case OMPC_private: 13637 case OMPC_shared: 13638 case OMPC_aligned: 13639 case OMPC_copyin: 13640 case OMPC_copyprivate: 13641 case OMPC_ordered: 13642 case OMPC_nowait: 13643 case OMPC_untied: 13644 case OMPC_mergeable: 13645 case OMPC_threadprivate: 13646 case OMPC_allocate: 13647 case OMPC_flush: 13648 case OMPC_depobj: 13649 case OMPC_read: 13650 case OMPC_write: 13651 case OMPC_update: 13652 case OMPC_capture: 13653 case OMPC_seq_cst: 13654 case OMPC_acq_rel: 13655 case OMPC_acquire: 13656 case OMPC_release: 13657 case OMPC_relaxed: 13658 case OMPC_depend: 13659 case OMPC_threads: 13660 case OMPC_simd: 13661 case OMPC_map: 13662 case OMPC_nogroup: 13663 case OMPC_hint: 13664 case OMPC_defaultmap: 13665 case OMPC_unknown: 13666 case OMPC_uniform: 13667 case OMPC_to: 13668 case OMPC_from: 13669 case OMPC_use_device_ptr: 13670 case OMPC_use_device_addr: 13671 case OMPC_is_device_ptr: 13672 case OMPC_unified_address: 13673 case OMPC_unified_shared_memory: 13674 case OMPC_reverse_offload: 13675 case OMPC_dynamic_allocators: 13676 case OMPC_atomic_default_mem_order: 13677 case OMPC_device_type: 13678 case OMPC_match: 13679 case OMPC_nontemporal: 13680 case OMPC_order: 13681 case OMPC_destroy: 13682 case OMPC_detach: 13683 case OMPC_inclusive: 13684 case OMPC_exclusive: 13685 case OMPC_uses_allocators: 13686 case OMPC_affinity: 13687 default: 13688 llvm_unreachable("Unexpected OpenMP clause."); 13689 } 13690 return CaptureRegion; 13691 } 13692 13693 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 13694 Expr *Condition, SourceLocation StartLoc, 13695 SourceLocation LParenLoc, 13696 SourceLocation NameModifierLoc, 13697 SourceLocation ColonLoc, 13698 SourceLocation EndLoc) { 13699 Expr *ValExpr = Condition; 13700 Stmt *HelperValStmt = nullptr; 13701 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13702 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 13703 !Condition->isInstantiationDependent() && 13704 !Condition->containsUnexpandedParameterPack()) { 13705 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 13706 if (Val.isInvalid()) 13707 return nullptr; 13708 13709 ValExpr = Val.get(); 13710 13711 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13712 CaptureRegion = getOpenMPCaptureRegionForClause( 13713 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 13714 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13715 ValExpr = MakeFullExpr(ValExpr).get(); 13716 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13717 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13718 HelperValStmt = buildPreInits(Context, Captures); 13719 } 13720 } 13721 13722 return new (Context) 13723 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 13724 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 13725 } 13726 13727 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 13728 SourceLocation StartLoc, 13729 SourceLocation LParenLoc, 13730 SourceLocation EndLoc) { 13731 Expr *ValExpr = Condition; 13732 Stmt *HelperValStmt = nullptr; 13733 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13734 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 13735 !Condition->isInstantiationDependent() && 13736 !Condition->containsUnexpandedParameterPack()) { 13737 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 13738 if (Val.isInvalid()) 13739 return nullptr; 13740 13741 ValExpr = MakeFullExpr(Val.get()).get(); 13742 13743 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13744 CaptureRegion = 13745 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 13746 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13747 ValExpr = MakeFullExpr(ValExpr).get(); 13748 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13749 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13750 HelperValStmt = buildPreInits(Context, Captures); 13751 } 13752 } 13753 13754 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 13755 StartLoc, LParenLoc, EndLoc); 13756 } 13757 13758 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 13759 Expr *Op) { 13760 if (!Op) 13761 return ExprError(); 13762 13763 class IntConvertDiagnoser : public ICEConvertDiagnoser { 13764 public: 13765 IntConvertDiagnoser() 13766 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 13767 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 13768 QualType T) override { 13769 return S.Diag(Loc, diag::err_omp_not_integral) << T; 13770 } 13771 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 13772 QualType T) override { 13773 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 13774 } 13775 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 13776 QualType T, 13777 QualType ConvTy) override { 13778 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 13779 } 13780 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 13781 QualType ConvTy) override { 13782 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 13783 << ConvTy->isEnumeralType() << ConvTy; 13784 } 13785 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 13786 QualType T) override { 13787 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 13788 } 13789 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 13790 QualType ConvTy) override { 13791 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 13792 << ConvTy->isEnumeralType() << ConvTy; 13793 } 13794 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 13795 QualType) override { 13796 llvm_unreachable("conversion functions are permitted"); 13797 } 13798 } ConvertDiagnoser; 13799 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 13800 } 13801 13802 static bool 13803 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 13804 bool StrictlyPositive, bool BuildCapture = false, 13805 OpenMPDirectiveKind DKind = OMPD_unknown, 13806 OpenMPDirectiveKind *CaptureRegion = nullptr, 13807 Stmt **HelperValStmt = nullptr) { 13808 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 13809 !ValExpr->isInstantiationDependent()) { 13810 SourceLocation Loc = ValExpr->getExprLoc(); 13811 ExprResult Value = 13812 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 13813 if (Value.isInvalid()) 13814 return false; 13815 13816 ValExpr = Value.get(); 13817 // The expression must evaluate to a non-negative integer value. 13818 if (Optional<llvm::APSInt> Result = 13819 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 13820 if (Result->isSigned() && 13821 !((!StrictlyPositive && Result->isNonNegative()) || 13822 (StrictlyPositive && Result->isStrictlyPositive()))) { 13823 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 13824 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 13825 << ValExpr->getSourceRange(); 13826 return false; 13827 } 13828 } 13829 if (!BuildCapture) 13830 return true; 13831 *CaptureRegion = 13832 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 13833 if (*CaptureRegion != OMPD_unknown && 13834 !SemaRef.CurContext->isDependentContext()) { 13835 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 13836 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13837 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 13838 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 13839 } 13840 } 13841 return true; 13842 } 13843 13844 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 13845 SourceLocation StartLoc, 13846 SourceLocation LParenLoc, 13847 SourceLocation EndLoc) { 13848 Expr *ValExpr = NumThreads; 13849 Stmt *HelperValStmt = nullptr; 13850 13851 // OpenMP [2.5, Restrictions] 13852 // The num_threads expression must evaluate to a positive integer value. 13853 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 13854 /*StrictlyPositive=*/true)) 13855 return nullptr; 13856 13857 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13858 OpenMPDirectiveKind CaptureRegion = 13859 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 13860 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13861 ValExpr = MakeFullExpr(ValExpr).get(); 13862 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13863 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13864 HelperValStmt = buildPreInits(Context, Captures); 13865 } 13866 13867 return new (Context) OMPNumThreadsClause( 13868 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 13869 } 13870 13871 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 13872 OpenMPClauseKind CKind, 13873 bool StrictlyPositive) { 13874 if (!E) 13875 return ExprError(); 13876 if (E->isValueDependent() || E->isTypeDependent() || 13877 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 13878 return E; 13879 llvm::APSInt Result; 13880 ExprResult ICE = 13881 VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 13882 if (ICE.isInvalid()) 13883 return ExprError(); 13884 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 13885 (!StrictlyPositive && !Result.isNonNegative())) { 13886 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 13887 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 13888 << E->getSourceRange(); 13889 return ExprError(); 13890 } 13891 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 13892 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 13893 << E->getSourceRange(); 13894 return ExprError(); 13895 } 13896 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 13897 DSAStack->setAssociatedLoops(Result.getExtValue()); 13898 else if (CKind == OMPC_ordered) 13899 DSAStack->setAssociatedLoops(Result.getExtValue()); 13900 return ICE; 13901 } 13902 13903 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 13904 SourceLocation LParenLoc, 13905 SourceLocation EndLoc) { 13906 // OpenMP [2.8.1, simd construct, Description] 13907 // The parameter of the safelen clause must be a constant 13908 // positive integer expression. 13909 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 13910 if (Safelen.isInvalid()) 13911 return nullptr; 13912 return new (Context) 13913 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 13914 } 13915 13916 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 13917 SourceLocation LParenLoc, 13918 SourceLocation EndLoc) { 13919 // OpenMP [2.8.1, simd construct, Description] 13920 // The parameter of the simdlen clause must be a constant 13921 // positive integer expression. 13922 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 13923 if (Simdlen.isInvalid()) 13924 return nullptr; 13925 return new (Context) 13926 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 13927 } 13928 13929 /// Tries to find omp_allocator_handle_t type. 13930 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 13931 DSAStackTy *Stack) { 13932 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 13933 if (!OMPAllocatorHandleT.isNull()) 13934 return true; 13935 // Build the predefined allocator expressions. 13936 bool ErrorFound = false; 13937 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 13938 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 13939 StringRef Allocator = 13940 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 13941 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 13942 auto *VD = dyn_cast_or_null<ValueDecl>( 13943 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 13944 if (!VD) { 13945 ErrorFound = true; 13946 break; 13947 } 13948 QualType AllocatorType = 13949 VD->getType().getNonLValueExprType(S.getASTContext()); 13950 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 13951 if (!Res.isUsable()) { 13952 ErrorFound = true; 13953 break; 13954 } 13955 if (OMPAllocatorHandleT.isNull()) 13956 OMPAllocatorHandleT = AllocatorType; 13957 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 13958 ErrorFound = true; 13959 break; 13960 } 13961 Stack->setAllocator(AllocatorKind, Res.get()); 13962 } 13963 if (ErrorFound) { 13964 S.Diag(Loc, diag::err_omp_implied_type_not_found) 13965 << "omp_allocator_handle_t"; 13966 return false; 13967 } 13968 OMPAllocatorHandleT.addConst(); 13969 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 13970 return true; 13971 } 13972 13973 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 13974 SourceLocation LParenLoc, 13975 SourceLocation EndLoc) { 13976 // OpenMP [2.11.3, allocate Directive, Description] 13977 // allocator is an expression of omp_allocator_handle_t type. 13978 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 13979 return nullptr; 13980 13981 ExprResult Allocator = DefaultLvalueConversion(A); 13982 if (Allocator.isInvalid()) 13983 return nullptr; 13984 Allocator = PerformImplicitConversion(Allocator.get(), 13985 DSAStack->getOMPAllocatorHandleT(), 13986 Sema::AA_Initializing, 13987 /*AllowExplicit=*/true); 13988 if (Allocator.isInvalid()) 13989 return nullptr; 13990 return new (Context) 13991 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 13992 } 13993 13994 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 13995 SourceLocation StartLoc, 13996 SourceLocation LParenLoc, 13997 SourceLocation EndLoc) { 13998 // OpenMP [2.7.1, loop construct, Description] 13999 // OpenMP [2.8.1, simd construct, Description] 14000 // OpenMP [2.9.6, distribute construct, Description] 14001 // The parameter of the collapse clause must be a constant 14002 // positive integer expression. 14003 ExprResult NumForLoopsResult = 14004 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 14005 if (NumForLoopsResult.isInvalid()) 14006 return nullptr; 14007 return new (Context) 14008 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 14009 } 14010 14011 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 14012 SourceLocation EndLoc, 14013 SourceLocation LParenLoc, 14014 Expr *NumForLoops) { 14015 // OpenMP [2.7.1, loop construct, Description] 14016 // OpenMP [2.8.1, simd construct, Description] 14017 // OpenMP [2.9.6, distribute construct, Description] 14018 // The parameter of the ordered clause must be a constant 14019 // positive integer expression if any. 14020 if (NumForLoops && LParenLoc.isValid()) { 14021 ExprResult NumForLoopsResult = 14022 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 14023 if (NumForLoopsResult.isInvalid()) 14024 return nullptr; 14025 NumForLoops = NumForLoopsResult.get(); 14026 } else { 14027 NumForLoops = nullptr; 14028 } 14029 auto *Clause = OMPOrderedClause::Create( 14030 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 14031 StartLoc, LParenLoc, EndLoc); 14032 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 14033 return Clause; 14034 } 14035 14036 OMPClause *Sema::ActOnOpenMPSimpleClause( 14037 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 14038 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14039 OMPClause *Res = nullptr; 14040 switch (Kind) { 14041 case OMPC_default: 14042 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 14043 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14044 break; 14045 case OMPC_proc_bind: 14046 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 14047 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14048 break; 14049 case OMPC_atomic_default_mem_order: 14050 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 14051 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 14052 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14053 break; 14054 case OMPC_order: 14055 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 14056 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14057 break; 14058 case OMPC_update: 14059 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 14060 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14061 break; 14062 case OMPC_if: 14063 case OMPC_final: 14064 case OMPC_num_threads: 14065 case OMPC_safelen: 14066 case OMPC_simdlen: 14067 case OMPC_sizes: 14068 case OMPC_allocator: 14069 case OMPC_collapse: 14070 case OMPC_schedule: 14071 case OMPC_private: 14072 case OMPC_firstprivate: 14073 case OMPC_lastprivate: 14074 case OMPC_shared: 14075 case OMPC_reduction: 14076 case OMPC_task_reduction: 14077 case OMPC_in_reduction: 14078 case OMPC_linear: 14079 case OMPC_aligned: 14080 case OMPC_copyin: 14081 case OMPC_copyprivate: 14082 case OMPC_ordered: 14083 case OMPC_nowait: 14084 case OMPC_untied: 14085 case OMPC_mergeable: 14086 case OMPC_threadprivate: 14087 case OMPC_allocate: 14088 case OMPC_flush: 14089 case OMPC_depobj: 14090 case OMPC_read: 14091 case OMPC_write: 14092 case OMPC_capture: 14093 case OMPC_seq_cst: 14094 case OMPC_acq_rel: 14095 case OMPC_acquire: 14096 case OMPC_release: 14097 case OMPC_relaxed: 14098 case OMPC_depend: 14099 case OMPC_device: 14100 case OMPC_threads: 14101 case OMPC_simd: 14102 case OMPC_map: 14103 case OMPC_num_teams: 14104 case OMPC_thread_limit: 14105 case OMPC_priority: 14106 case OMPC_grainsize: 14107 case OMPC_nogroup: 14108 case OMPC_num_tasks: 14109 case OMPC_hint: 14110 case OMPC_dist_schedule: 14111 case OMPC_defaultmap: 14112 case OMPC_unknown: 14113 case OMPC_uniform: 14114 case OMPC_to: 14115 case OMPC_from: 14116 case OMPC_use_device_ptr: 14117 case OMPC_use_device_addr: 14118 case OMPC_is_device_ptr: 14119 case OMPC_unified_address: 14120 case OMPC_unified_shared_memory: 14121 case OMPC_reverse_offload: 14122 case OMPC_dynamic_allocators: 14123 case OMPC_device_type: 14124 case OMPC_match: 14125 case OMPC_nontemporal: 14126 case OMPC_destroy: 14127 case OMPC_novariants: 14128 case OMPC_nocontext: 14129 case OMPC_detach: 14130 case OMPC_inclusive: 14131 case OMPC_exclusive: 14132 case OMPC_uses_allocators: 14133 case OMPC_affinity: 14134 default: 14135 llvm_unreachable("Clause is not allowed."); 14136 } 14137 return Res; 14138 } 14139 14140 static std::string 14141 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 14142 ArrayRef<unsigned> Exclude = llvm::None) { 14143 SmallString<256> Buffer; 14144 llvm::raw_svector_ostream Out(Buffer); 14145 unsigned Skipped = Exclude.size(); 14146 auto S = Exclude.begin(), E = Exclude.end(); 14147 for (unsigned I = First; I < Last; ++I) { 14148 if (std::find(S, E, I) != E) { 14149 --Skipped; 14150 continue; 14151 } 14152 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 14153 if (I + Skipped + 2 == Last) 14154 Out << " or "; 14155 else if (I + Skipped + 1 != Last) 14156 Out << ", "; 14157 } 14158 return std::string(Out.str()); 14159 } 14160 14161 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 14162 SourceLocation KindKwLoc, 14163 SourceLocation StartLoc, 14164 SourceLocation LParenLoc, 14165 SourceLocation EndLoc) { 14166 if (Kind == OMP_DEFAULT_unknown) { 14167 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14168 << getListOfPossibleValues(OMPC_default, /*First=*/0, 14169 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 14170 << getOpenMPClauseName(OMPC_default); 14171 return nullptr; 14172 } 14173 14174 switch (Kind) { 14175 case OMP_DEFAULT_none: 14176 DSAStack->setDefaultDSANone(KindKwLoc); 14177 break; 14178 case OMP_DEFAULT_shared: 14179 DSAStack->setDefaultDSAShared(KindKwLoc); 14180 break; 14181 case OMP_DEFAULT_firstprivate: 14182 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 14183 break; 14184 default: 14185 llvm_unreachable("DSA unexpected in OpenMP default clause"); 14186 } 14187 14188 return new (Context) 14189 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14190 } 14191 14192 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 14193 SourceLocation KindKwLoc, 14194 SourceLocation StartLoc, 14195 SourceLocation LParenLoc, 14196 SourceLocation EndLoc) { 14197 if (Kind == OMP_PROC_BIND_unknown) { 14198 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14199 << getListOfPossibleValues(OMPC_proc_bind, 14200 /*First=*/unsigned(OMP_PROC_BIND_master), 14201 /*Last=*/ 14202 unsigned(LangOpts.OpenMP > 50 14203 ? OMP_PROC_BIND_primary 14204 : OMP_PROC_BIND_spread) + 14205 1) 14206 << getOpenMPClauseName(OMPC_proc_bind); 14207 return nullptr; 14208 } 14209 if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51) 14210 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14211 << getListOfPossibleValues(OMPC_proc_bind, 14212 /*First=*/unsigned(OMP_PROC_BIND_master), 14213 /*Last=*/ 14214 unsigned(OMP_PROC_BIND_spread) + 1) 14215 << getOpenMPClauseName(OMPC_proc_bind); 14216 return new (Context) 14217 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14218 } 14219 14220 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 14221 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 14222 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14223 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 14224 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14225 << getListOfPossibleValues( 14226 OMPC_atomic_default_mem_order, /*First=*/0, 14227 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 14228 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 14229 return nullptr; 14230 } 14231 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 14232 LParenLoc, EndLoc); 14233 } 14234 14235 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 14236 SourceLocation KindKwLoc, 14237 SourceLocation StartLoc, 14238 SourceLocation LParenLoc, 14239 SourceLocation EndLoc) { 14240 if (Kind == OMPC_ORDER_unknown) { 14241 static_assert(OMPC_ORDER_unknown > 0, 14242 "OMPC_ORDER_unknown not greater than 0"); 14243 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14244 << getListOfPossibleValues(OMPC_order, /*First=*/0, 14245 /*Last=*/OMPC_ORDER_unknown) 14246 << getOpenMPClauseName(OMPC_order); 14247 return nullptr; 14248 } 14249 return new (Context) 14250 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14251 } 14252 14253 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 14254 SourceLocation KindKwLoc, 14255 SourceLocation StartLoc, 14256 SourceLocation LParenLoc, 14257 SourceLocation EndLoc) { 14258 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 14259 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 14260 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 14261 OMPC_DEPEND_depobj}; 14262 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14263 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 14264 /*Last=*/OMPC_DEPEND_unknown, Except) 14265 << getOpenMPClauseName(OMPC_update); 14266 return nullptr; 14267 } 14268 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 14269 EndLoc); 14270 } 14271 14272 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs, 14273 SourceLocation StartLoc, 14274 SourceLocation LParenLoc, 14275 SourceLocation EndLoc) { 14276 for (Expr *SizeExpr : SizeExprs) { 14277 ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause( 14278 SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true); 14279 if (!NumForLoopsResult.isUsable()) 14280 return nullptr; 14281 } 14282 14283 DSAStack->setAssociatedLoops(SizeExprs.size()); 14284 return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14285 SizeExprs); 14286 } 14287 14288 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 14289 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 14290 SourceLocation StartLoc, SourceLocation LParenLoc, 14291 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 14292 SourceLocation EndLoc) { 14293 OMPClause *Res = nullptr; 14294 switch (Kind) { 14295 case OMPC_schedule: 14296 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 14297 assert(Argument.size() == NumberOfElements && 14298 ArgumentLoc.size() == NumberOfElements); 14299 Res = ActOnOpenMPScheduleClause( 14300 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 14301 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 14302 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 14303 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 14304 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 14305 break; 14306 case OMPC_if: 14307 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14308 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 14309 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 14310 DelimLoc, EndLoc); 14311 break; 14312 case OMPC_dist_schedule: 14313 Res = ActOnOpenMPDistScheduleClause( 14314 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 14315 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 14316 break; 14317 case OMPC_defaultmap: 14318 enum { Modifier, DefaultmapKind }; 14319 Res = ActOnOpenMPDefaultmapClause( 14320 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 14321 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 14322 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 14323 EndLoc); 14324 break; 14325 case OMPC_device: 14326 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14327 Res = ActOnOpenMPDeviceClause( 14328 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 14329 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 14330 break; 14331 case OMPC_final: 14332 case OMPC_num_threads: 14333 case OMPC_safelen: 14334 case OMPC_simdlen: 14335 case OMPC_sizes: 14336 case OMPC_allocator: 14337 case OMPC_collapse: 14338 case OMPC_default: 14339 case OMPC_proc_bind: 14340 case OMPC_private: 14341 case OMPC_firstprivate: 14342 case OMPC_lastprivate: 14343 case OMPC_shared: 14344 case OMPC_reduction: 14345 case OMPC_task_reduction: 14346 case OMPC_in_reduction: 14347 case OMPC_linear: 14348 case OMPC_aligned: 14349 case OMPC_copyin: 14350 case OMPC_copyprivate: 14351 case OMPC_ordered: 14352 case OMPC_nowait: 14353 case OMPC_untied: 14354 case OMPC_mergeable: 14355 case OMPC_threadprivate: 14356 case OMPC_allocate: 14357 case OMPC_flush: 14358 case OMPC_depobj: 14359 case OMPC_read: 14360 case OMPC_write: 14361 case OMPC_update: 14362 case OMPC_capture: 14363 case OMPC_seq_cst: 14364 case OMPC_acq_rel: 14365 case OMPC_acquire: 14366 case OMPC_release: 14367 case OMPC_relaxed: 14368 case OMPC_depend: 14369 case OMPC_threads: 14370 case OMPC_simd: 14371 case OMPC_map: 14372 case OMPC_num_teams: 14373 case OMPC_thread_limit: 14374 case OMPC_priority: 14375 case OMPC_grainsize: 14376 case OMPC_nogroup: 14377 case OMPC_num_tasks: 14378 case OMPC_hint: 14379 case OMPC_unknown: 14380 case OMPC_uniform: 14381 case OMPC_to: 14382 case OMPC_from: 14383 case OMPC_use_device_ptr: 14384 case OMPC_use_device_addr: 14385 case OMPC_is_device_ptr: 14386 case OMPC_unified_address: 14387 case OMPC_unified_shared_memory: 14388 case OMPC_reverse_offload: 14389 case OMPC_dynamic_allocators: 14390 case OMPC_atomic_default_mem_order: 14391 case OMPC_device_type: 14392 case OMPC_match: 14393 case OMPC_nontemporal: 14394 case OMPC_order: 14395 case OMPC_destroy: 14396 case OMPC_novariants: 14397 case OMPC_nocontext: 14398 case OMPC_detach: 14399 case OMPC_inclusive: 14400 case OMPC_exclusive: 14401 case OMPC_uses_allocators: 14402 case OMPC_affinity: 14403 default: 14404 llvm_unreachable("Clause is not allowed."); 14405 } 14406 return Res; 14407 } 14408 14409 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 14410 OpenMPScheduleClauseModifier M2, 14411 SourceLocation M1Loc, SourceLocation M2Loc) { 14412 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 14413 SmallVector<unsigned, 2> Excluded; 14414 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 14415 Excluded.push_back(M2); 14416 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 14417 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 14418 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 14419 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 14420 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 14421 << getListOfPossibleValues(OMPC_schedule, 14422 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 14423 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14424 Excluded) 14425 << getOpenMPClauseName(OMPC_schedule); 14426 return true; 14427 } 14428 return false; 14429 } 14430 14431 OMPClause *Sema::ActOnOpenMPScheduleClause( 14432 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 14433 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14434 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 14435 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 14436 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 14437 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 14438 return nullptr; 14439 // OpenMP, 2.7.1, Loop Construct, Restrictions 14440 // Either the monotonic modifier or the nonmonotonic modifier can be specified 14441 // but not both. 14442 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 14443 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 14444 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 14445 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 14446 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 14447 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 14448 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 14449 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 14450 return nullptr; 14451 } 14452 if (Kind == OMPC_SCHEDULE_unknown) { 14453 std::string Values; 14454 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 14455 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 14456 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14457 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14458 Exclude); 14459 } else { 14460 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14461 /*Last=*/OMPC_SCHEDULE_unknown); 14462 } 14463 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14464 << Values << getOpenMPClauseName(OMPC_schedule); 14465 return nullptr; 14466 } 14467 // OpenMP, 2.7.1, Loop Construct, Restrictions 14468 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 14469 // schedule(guided). 14470 // OpenMP 5.0 does not have this restriction. 14471 if (LangOpts.OpenMP < 50 && 14472 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 14473 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 14474 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 14475 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 14476 diag::err_omp_schedule_nonmonotonic_static); 14477 return nullptr; 14478 } 14479 Expr *ValExpr = ChunkSize; 14480 Stmt *HelperValStmt = nullptr; 14481 if (ChunkSize) { 14482 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 14483 !ChunkSize->isInstantiationDependent() && 14484 !ChunkSize->containsUnexpandedParameterPack()) { 14485 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 14486 ExprResult Val = 14487 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 14488 if (Val.isInvalid()) 14489 return nullptr; 14490 14491 ValExpr = Val.get(); 14492 14493 // OpenMP [2.7.1, Restrictions] 14494 // chunk_size must be a loop invariant integer expression with a positive 14495 // value. 14496 if (Optional<llvm::APSInt> Result = 14497 ValExpr->getIntegerConstantExpr(Context)) { 14498 if (Result->isSigned() && !Result->isStrictlyPositive()) { 14499 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 14500 << "schedule" << 1 << ChunkSize->getSourceRange(); 14501 return nullptr; 14502 } 14503 } else if (getOpenMPCaptureRegionForClause( 14504 DSAStack->getCurrentDirective(), OMPC_schedule, 14505 LangOpts.OpenMP) != OMPD_unknown && 14506 !CurContext->isDependentContext()) { 14507 ValExpr = MakeFullExpr(ValExpr).get(); 14508 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14509 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14510 HelperValStmt = buildPreInits(Context, Captures); 14511 } 14512 } 14513 } 14514 14515 return new (Context) 14516 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 14517 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 14518 } 14519 14520 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 14521 SourceLocation StartLoc, 14522 SourceLocation EndLoc) { 14523 OMPClause *Res = nullptr; 14524 switch (Kind) { 14525 case OMPC_ordered: 14526 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 14527 break; 14528 case OMPC_nowait: 14529 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 14530 break; 14531 case OMPC_untied: 14532 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 14533 break; 14534 case OMPC_mergeable: 14535 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 14536 break; 14537 case OMPC_read: 14538 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 14539 break; 14540 case OMPC_write: 14541 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 14542 break; 14543 case OMPC_update: 14544 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 14545 break; 14546 case OMPC_capture: 14547 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 14548 break; 14549 case OMPC_seq_cst: 14550 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 14551 break; 14552 case OMPC_acq_rel: 14553 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 14554 break; 14555 case OMPC_acquire: 14556 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 14557 break; 14558 case OMPC_release: 14559 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 14560 break; 14561 case OMPC_relaxed: 14562 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 14563 break; 14564 case OMPC_threads: 14565 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 14566 break; 14567 case OMPC_simd: 14568 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 14569 break; 14570 case OMPC_nogroup: 14571 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 14572 break; 14573 case OMPC_unified_address: 14574 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 14575 break; 14576 case OMPC_unified_shared_memory: 14577 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 14578 break; 14579 case OMPC_reverse_offload: 14580 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 14581 break; 14582 case OMPC_dynamic_allocators: 14583 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 14584 break; 14585 case OMPC_destroy: 14586 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc, 14587 /*LParenLoc=*/SourceLocation(), 14588 /*VarLoc=*/SourceLocation(), EndLoc); 14589 break; 14590 case OMPC_if: 14591 case OMPC_final: 14592 case OMPC_num_threads: 14593 case OMPC_safelen: 14594 case OMPC_simdlen: 14595 case OMPC_sizes: 14596 case OMPC_allocator: 14597 case OMPC_collapse: 14598 case OMPC_schedule: 14599 case OMPC_private: 14600 case OMPC_firstprivate: 14601 case OMPC_lastprivate: 14602 case OMPC_shared: 14603 case OMPC_reduction: 14604 case OMPC_task_reduction: 14605 case OMPC_in_reduction: 14606 case OMPC_linear: 14607 case OMPC_aligned: 14608 case OMPC_copyin: 14609 case OMPC_copyprivate: 14610 case OMPC_default: 14611 case OMPC_proc_bind: 14612 case OMPC_threadprivate: 14613 case OMPC_allocate: 14614 case OMPC_flush: 14615 case OMPC_depobj: 14616 case OMPC_depend: 14617 case OMPC_device: 14618 case OMPC_map: 14619 case OMPC_num_teams: 14620 case OMPC_thread_limit: 14621 case OMPC_priority: 14622 case OMPC_grainsize: 14623 case OMPC_num_tasks: 14624 case OMPC_hint: 14625 case OMPC_dist_schedule: 14626 case OMPC_defaultmap: 14627 case OMPC_unknown: 14628 case OMPC_uniform: 14629 case OMPC_to: 14630 case OMPC_from: 14631 case OMPC_use_device_ptr: 14632 case OMPC_use_device_addr: 14633 case OMPC_is_device_ptr: 14634 case OMPC_atomic_default_mem_order: 14635 case OMPC_device_type: 14636 case OMPC_match: 14637 case OMPC_nontemporal: 14638 case OMPC_order: 14639 case OMPC_novariants: 14640 case OMPC_nocontext: 14641 case OMPC_detach: 14642 case OMPC_inclusive: 14643 case OMPC_exclusive: 14644 case OMPC_uses_allocators: 14645 case OMPC_affinity: 14646 default: 14647 llvm_unreachable("Clause is not allowed."); 14648 } 14649 return Res; 14650 } 14651 14652 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 14653 SourceLocation EndLoc) { 14654 DSAStack->setNowaitRegion(); 14655 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 14656 } 14657 14658 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 14659 SourceLocation EndLoc) { 14660 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 14661 } 14662 14663 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 14664 SourceLocation EndLoc) { 14665 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 14666 } 14667 14668 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 14669 SourceLocation EndLoc) { 14670 return new (Context) OMPReadClause(StartLoc, EndLoc); 14671 } 14672 14673 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 14674 SourceLocation EndLoc) { 14675 return new (Context) OMPWriteClause(StartLoc, EndLoc); 14676 } 14677 14678 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 14679 SourceLocation EndLoc) { 14680 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 14681 } 14682 14683 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 14684 SourceLocation EndLoc) { 14685 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 14686 } 14687 14688 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 14689 SourceLocation EndLoc) { 14690 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 14691 } 14692 14693 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 14694 SourceLocation EndLoc) { 14695 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 14696 } 14697 14698 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 14699 SourceLocation EndLoc) { 14700 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 14701 } 14702 14703 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 14704 SourceLocation EndLoc) { 14705 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 14706 } 14707 14708 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 14709 SourceLocation EndLoc) { 14710 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 14711 } 14712 14713 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 14714 SourceLocation EndLoc) { 14715 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 14716 } 14717 14718 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 14719 SourceLocation EndLoc) { 14720 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 14721 } 14722 14723 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 14724 SourceLocation EndLoc) { 14725 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 14726 } 14727 14728 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 14729 SourceLocation EndLoc) { 14730 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 14731 } 14732 14733 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 14734 SourceLocation EndLoc) { 14735 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 14736 } 14737 14738 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 14739 SourceLocation EndLoc) { 14740 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 14741 } 14742 14743 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 14744 SourceLocation EndLoc) { 14745 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 14746 } 14747 14748 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses, 14749 SourceLocation StartLoc, 14750 SourceLocation EndLoc) { 14751 14752 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 14753 // At least one action-clause must appear on a directive. 14754 if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) { 14755 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'"; 14756 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 14757 << Expected << getOpenMPDirectiveName(OMPD_interop); 14758 return StmtError(); 14759 } 14760 14761 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 14762 // A depend clause can only appear on the directive if a targetsync 14763 // interop-type is present or the interop-var was initialized with 14764 // the targetsync interop-type. 14765 14766 // If there is any 'init' clause diagnose if there is no 'init' clause with 14767 // interop-type of 'targetsync'. Cases involving other directives cannot be 14768 // diagnosed. 14769 const OMPDependClause *DependClause = nullptr; 14770 bool HasInitClause = false; 14771 bool IsTargetSync = false; 14772 for (const OMPClause *C : Clauses) { 14773 if (IsTargetSync) 14774 break; 14775 if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) { 14776 HasInitClause = true; 14777 if (InitClause->getIsTargetSync()) 14778 IsTargetSync = true; 14779 } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) { 14780 DependClause = DC; 14781 } 14782 } 14783 if (DependClause && HasInitClause && !IsTargetSync) { 14784 Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause); 14785 return StmtError(); 14786 } 14787 14788 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 14789 // Each interop-var may be specified for at most one action-clause of each 14790 // interop construct. 14791 llvm::SmallPtrSet<const VarDecl *, 4> InteropVars; 14792 for (const OMPClause *C : Clauses) { 14793 OpenMPClauseKind ClauseKind = C->getClauseKind(); 14794 const DeclRefExpr *DRE = nullptr; 14795 SourceLocation VarLoc; 14796 14797 if (ClauseKind == OMPC_init) { 14798 const auto *IC = cast<OMPInitClause>(C); 14799 VarLoc = IC->getVarLoc(); 14800 DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar()); 14801 } else if (ClauseKind == OMPC_use) { 14802 const auto *UC = cast<OMPUseClause>(C); 14803 VarLoc = UC->getVarLoc(); 14804 DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar()); 14805 } else if (ClauseKind == OMPC_destroy) { 14806 const auto *DC = cast<OMPDestroyClause>(C); 14807 VarLoc = DC->getVarLoc(); 14808 DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar()); 14809 } 14810 14811 if (!DRE) 14812 continue; 14813 14814 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 14815 if (!InteropVars.insert(VD->getCanonicalDecl()).second) { 14816 Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD; 14817 return StmtError(); 14818 } 14819 } 14820 } 14821 14822 return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses); 14823 } 14824 14825 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr, 14826 SourceLocation VarLoc, 14827 OpenMPClauseKind Kind) { 14828 if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() || 14829 InteropVarExpr->isInstantiationDependent() || 14830 InteropVarExpr->containsUnexpandedParameterPack()) 14831 return true; 14832 14833 const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr); 14834 if (!DRE || !isa<VarDecl>(DRE->getDecl())) { 14835 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0; 14836 return false; 14837 } 14838 14839 // Interop variable should be of type omp_interop_t. 14840 bool HasError = false; 14841 QualType InteropType; 14842 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"), 14843 VarLoc, Sema::LookupOrdinaryName); 14844 if (SemaRef.LookupName(Result, SemaRef.getCurScope())) { 14845 NamedDecl *ND = Result.getFoundDecl(); 14846 if (const auto *TD = dyn_cast<TypeDecl>(ND)) { 14847 InteropType = QualType(TD->getTypeForDecl(), 0); 14848 } else { 14849 HasError = true; 14850 } 14851 } else { 14852 HasError = true; 14853 } 14854 14855 if (HasError) { 14856 SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found) 14857 << "omp_interop_t"; 14858 return false; 14859 } 14860 14861 QualType VarType = InteropVarExpr->getType().getUnqualifiedType(); 14862 if (!SemaRef.Context.hasSameType(InteropType, VarType)) { 14863 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type); 14864 return false; 14865 } 14866 14867 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 14868 // The interop-var passed to init or destroy must be non-const. 14869 if ((Kind == OMPC_init || Kind == OMPC_destroy) && 14870 isConstNotMutableType(SemaRef, InteropVarExpr->getType())) { 14871 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) 14872 << /*non-const*/ 1; 14873 return false; 14874 } 14875 return true; 14876 } 14877 14878 OMPClause * 14879 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs, 14880 bool IsTarget, bool IsTargetSync, 14881 SourceLocation StartLoc, SourceLocation LParenLoc, 14882 SourceLocation VarLoc, SourceLocation EndLoc) { 14883 14884 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init)) 14885 return nullptr; 14886 14887 // Check prefer_type values. These foreign-runtime-id values are either 14888 // string literals or constant integral expressions. 14889 for (const Expr *E : PrefExprs) { 14890 if (E->isValueDependent() || E->isTypeDependent() || 14891 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 14892 continue; 14893 if (E->isIntegerConstantExpr(Context)) 14894 continue; 14895 if (isa<StringLiteral>(E)) 14896 continue; 14897 Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type); 14898 return nullptr; 14899 } 14900 14901 return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget, 14902 IsTargetSync, StartLoc, LParenLoc, VarLoc, 14903 EndLoc); 14904 } 14905 14906 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc, 14907 SourceLocation LParenLoc, 14908 SourceLocation VarLoc, 14909 SourceLocation EndLoc) { 14910 14911 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use)) 14912 return nullptr; 14913 14914 return new (Context) 14915 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 14916 } 14917 14918 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar, 14919 SourceLocation StartLoc, 14920 SourceLocation LParenLoc, 14921 SourceLocation VarLoc, 14922 SourceLocation EndLoc) { 14923 if (InteropVar && 14924 !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy)) 14925 return nullptr; 14926 14927 return new (Context) 14928 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 14929 } 14930 14931 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition, 14932 SourceLocation StartLoc, 14933 SourceLocation LParenLoc, 14934 SourceLocation EndLoc) { 14935 Expr *ValExpr = Condition; 14936 Stmt *HelperValStmt = nullptr; 14937 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14938 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14939 !Condition->isInstantiationDependent() && 14940 !Condition->containsUnexpandedParameterPack()) { 14941 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14942 if (Val.isInvalid()) 14943 return nullptr; 14944 14945 ValExpr = MakeFullExpr(Val.get()).get(); 14946 14947 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14948 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants, 14949 LangOpts.OpenMP); 14950 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14951 ValExpr = MakeFullExpr(ValExpr).get(); 14952 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14953 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14954 HelperValStmt = buildPreInits(Context, Captures); 14955 } 14956 } 14957 14958 return new (Context) OMPNovariantsClause( 14959 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14960 } 14961 14962 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition, 14963 SourceLocation StartLoc, 14964 SourceLocation LParenLoc, 14965 SourceLocation EndLoc) { 14966 Expr *ValExpr = Condition; 14967 Stmt *HelperValStmt = nullptr; 14968 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14969 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14970 !Condition->isInstantiationDependent() && 14971 !Condition->containsUnexpandedParameterPack()) { 14972 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14973 if (Val.isInvalid()) 14974 return nullptr; 14975 14976 ValExpr = MakeFullExpr(Val.get()).get(); 14977 14978 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14979 CaptureRegion = 14980 getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP); 14981 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14982 ValExpr = MakeFullExpr(ValExpr).get(); 14983 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14984 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14985 HelperValStmt = buildPreInits(Context, Captures); 14986 } 14987 } 14988 14989 return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion, 14990 StartLoc, LParenLoc, EndLoc); 14991 } 14992 14993 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID, 14994 SourceLocation StartLoc, 14995 SourceLocation LParenLoc, 14996 SourceLocation EndLoc) { 14997 Expr *ValExpr = ThreadID; 14998 Stmt *HelperValStmt = nullptr; 14999 15000 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15001 OpenMPDirectiveKind CaptureRegion = 15002 getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP); 15003 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15004 ValExpr = MakeFullExpr(ValExpr).get(); 15005 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15006 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15007 HelperValStmt = buildPreInits(Context, Captures); 15008 } 15009 15010 return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion, 15011 StartLoc, LParenLoc, EndLoc); 15012 } 15013 15014 OMPClause *Sema::ActOnOpenMPVarListClause( 15015 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 15016 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 15017 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 15018 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 15019 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 15020 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 15021 SourceLocation ExtraModifierLoc, 15022 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 15023 ArrayRef<SourceLocation> MotionModifiersLoc) { 15024 SourceLocation StartLoc = Locs.StartLoc; 15025 SourceLocation LParenLoc = Locs.LParenLoc; 15026 SourceLocation EndLoc = Locs.EndLoc; 15027 OMPClause *Res = nullptr; 15028 switch (Kind) { 15029 case OMPC_private: 15030 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15031 break; 15032 case OMPC_firstprivate: 15033 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15034 break; 15035 case OMPC_lastprivate: 15036 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 15037 "Unexpected lastprivate modifier."); 15038 Res = ActOnOpenMPLastprivateClause( 15039 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 15040 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 15041 break; 15042 case OMPC_shared: 15043 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 15044 break; 15045 case OMPC_reduction: 15046 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 15047 "Unexpected lastprivate modifier."); 15048 Res = ActOnOpenMPReductionClause( 15049 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 15050 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 15051 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 15052 break; 15053 case OMPC_task_reduction: 15054 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15055 EndLoc, ReductionOrMapperIdScopeSpec, 15056 ReductionOrMapperId); 15057 break; 15058 case OMPC_in_reduction: 15059 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15060 EndLoc, ReductionOrMapperIdScopeSpec, 15061 ReductionOrMapperId); 15062 break; 15063 case OMPC_linear: 15064 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 15065 "Unexpected linear modifier."); 15066 Res = ActOnOpenMPLinearClause( 15067 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 15068 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 15069 ColonLoc, EndLoc); 15070 break; 15071 case OMPC_aligned: 15072 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 15073 LParenLoc, ColonLoc, EndLoc); 15074 break; 15075 case OMPC_copyin: 15076 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 15077 break; 15078 case OMPC_copyprivate: 15079 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15080 break; 15081 case OMPC_flush: 15082 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 15083 break; 15084 case OMPC_depend: 15085 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 15086 "Unexpected depend modifier."); 15087 Res = ActOnOpenMPDependClause( 15088 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 15089 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 15090 break; 15091 case OMPC_map: 15092 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 15093 "Unexpected map modifier."); 15094 Res = ActOnOpenMPMapClause( 15095 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 15096 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 15097 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 15098 break; 15099 case OMPC_to: 15100 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 15101 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 15102 ColonLoc, VarList, Locs); 15103 break; 15104 case OMPC_from: 15105 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 15106 ReductionOrMapperIdScopeSpec, 15107 ReductionOrMapperId, ColonLoc, VarList, Locs); 15108 break; 15109 case OMPC_use_device_ptr: 15110 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 15111 break; 15112 case OMPC_use_device_addr: 15113 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 15114 break; 15115 case OMPC_is_device_ptr: 15116 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 15117 break; 15118 case OMPC_allocate: 15119 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 15120 LParenLoc, ColonLoc, EndLoc); 15121 break; 15122 case OMPC_nontemporal: 15123 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 15124 break; 15125 case OMPC_inclusive: 15126 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15127 break; 15128 case OMPC_exclusive: 15129 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15130 break; 15131 case OMPC_affinity: 15132 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 15133 DepModOrTailExpr, VarList); 15134 break; 15135 case OMPC_if: 15136 case OMPC_depobj: 15137 case OMPC_final: 15138 case OMPC_num_threads: 15139 case OMPC_safelen: 15140 case OMPC_simdlen: 15141 case OMPC_sizes: 15142 case OMPC_allocator: 15143 case OMPC_collapse: 15144 case OMPC_default: 15145 case OMPC_proc_bind: 15146 case OMPC_schedule: 15147 case OMPC_ordered: 15148 case OMPC_nowait: 15149 case OMPC_untied: 15150 case OMPC_mergeable: 15151 case OMPC_threadprivate: 15152 case OMPC_read: 15153 case OMPC_write: 15154 case OMPC_update: 15155 case OMPC_capture: 15156 case OMPC_seq_cst: 15157 case OMPC_acq_rel: 15158 case OMPC_acquire: 15159 case OMPC_release: 15160 case OMPC_relaxed: 15161 case OMPC_device: 15162 case OMPC_threads: 15163 case OMPC_simd: 15164 case OMPC_num_teams: 15165 case OMPC_thread_limit: 15166 case OMPC_priority: 15167 case OMPC_grainsize: 15168 case OMPC_nogroup: 15169 case OMPC_num_tasks: 15170 case OMPC_hint: 15171 case OMPC_dist_schedule: 15172 case OMPC_defaultmap: 15173 case OMPC_unknown: 15174 case OMPC_uniform: 15175 case OMPC_unified_address: 15176 case OMPC_unified_shared_memory: 15177 case OMPC_reverse_offload: 15178 case OMPC_dynamic_allocators: 15179 case OMPC_atomic_default_mem_order: 15180 case OMPC_device_type: 15181 case OMPC_match: 15182 case OMPC_order: 15183 case OMPC_destroy: 15184 case OMPC_novariants: 15185 case OMPC_nocontext: 15186 case OMPC_detach: 15187 case OMPC_uses_allocators: 15188 default: 15189 llvm_unreachable("Clause is not allowed."); 15190 } 15191 return Res; 15192 } 15193 15194 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 15195 ExprObjectKind OK, SourceLocation Loc) { 15196 ExprResult Res = BuildDeclRefExpr( 15197 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 15198 if (!Res.isUsable()) 15199 return ExprError(); 15200 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 15201 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 15202 if (!Res.isUsable()) 15203 return ExprError(); 15204 } 15205 if (VK != VK_LValue && Res.get()->isGLValue()) { 15206 Res = DefaultLvalueConversion(Res.get()); 15207 if (!Res.isUsable()) 15208 return ExprError(); 15209 } 15210 return Res; 15211 } 15212 15213 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 15214 SourceLocation StartLoc, 15215 SourceLocation LParenLoc, 15216 SourceLocation EndLoc) { 15217 SmallVector<Expr *, 8> Vars; 15218 SmallVector<Expr *, 8> PrivateCopies; 15219 for (Expr *RefExpr : VarList) { 15220 assert(RefExpr && "NULL expr in OpenMP private clause."); 15221 SourceLocation ELoc; 15222 SourceRange ERange; 15223 Expr *SimpleRefExpr = RefExpr; 15224 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15225 if (Res.second) { 15226 // It will be analyzed later. 15227 Vars.push_back(RefExpr); 15228 PrivateCopies.push_back(nullptr); 15229 } 15230 ValueDecl *D = Res.first; 15231 if (!D) 15232 continue; 15233 15234 QualType Type = D->getType(); 15235 auto *VD = dyn_cast<VarDecl>(D); 15236 15237 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15238 // A variable that appears in a private clause must not have an incomplete 15239 // type or a reference type. 15240 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 15241 continue; 15242 Type = Type.getNonReferenceType(); 15243 15244 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15245 // A variable that is privatized must not have a const-qualified type 15246 // unless it is of class type with a mutable member. This restriction does 15247 // not apply to the firstprivate clause. 15248 // 15249 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 15250 // A variable that appears in a private clause must not have a 15251 // const-qualified type unless it is of class type with a mutable member. 15252 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 15253 continue; 15254 15255 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15256 // in a Construct] 15257 // Variables with the predetermined data-sharing attributes may not be 15258 // listed in data-sharing attributes clauses, except for the cases 15259 // listed below. For these exceptions only, listing a predetermined 15260 // variable in a data-sharing attribute clause is allowed and overrides 15261 // the variable's predetermined data-sharing attributes. 15262 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15263 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 15264 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15265 << getOpenMPClauseName(OMPC_private); 15266 reportOriginalDsa(*this, DSAStack, D, DVar); 15267 continue; 15268 } 15269 15270 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15271 // Variably modified types are not supported for tasks. 15272 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 15273 isOpenMPTaskingDirective(CurrDir)) { 15274 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15275 << getOpenMPClauseName(OMPC_private) << Type 15276 << getOpenMPDirectiveName(CurrDir); 15277 bool IsDecl = 15278 !VD || 15279 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15280 Diag(D->getLocation(), 15281 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15282 << D; 15283 continue; 15284 } 15285 15286 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 15287 // A list item cannot appear in both a map clause and a data-sharing 15288 // attribute clause on the same construct 15289 // 15290 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 15291 // A list item cannot appear in both a map clause and a data-sharing 15292 // attribute clause on the same construct unless the construct is a 15293 // combined construct. 15294 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 15295 CurrDir == OMPD_target) { 15296 OpenMPClauseKind ConflictKind; 15297 if (DSAStack->checkMappableExprComponentListsForDecl( 15298 VD, /*CurrentRegionOnly=*/true, 15299 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 15300 OpenMPClauseKind WhereFoundClauseKind) -> bool { 15301 ConflictKind = WhereFoundClauseKind; 15302 return true; 15303 })) { 15304 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 15305 << getOpenMPClauseName(OMPC_private) 15306 << getOpenMPClauseName(ConflictKind) 15307 << getOpenMPDirectiveName(CurrDir); 15308 reportOriginalDsa(*this, DSAStack, D, DVar); 15309 continue; 15310 } 15311 } 15312 15313 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 15314 // A variable of class type (or array thereof) that appears in a private 15315 // clause requires an accessible, unambiguous default constructor for the 15316 // class type. 15317 // Generate helper private variable and initialize it with the default 15318 // value. The address of the original variable is replaced by the address of 15319 // the new private variable in CodeGen. This new variable is not added to 15320 // IdResolver, so the code in the OpenMP region uses original variable for 15321 // proper diagnostics. 15322 Type = Type.getUnqualifiedType(); 15323 VarDecl *VDPrivate = 15324 buildVarDecl(*this, ELoc, Type, D->getName(), 15325 D->hasAttrs() ? &D->getAttrs() : nullptr, 15326 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15327 ActOnUninitializedDecl(VDPrivate); 15328 if (VDPrivate->isInvalidDecl()) 15329 continue; 15330 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 15331 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 15332 15333 DeclRefExpr *Ref = nullptr; 15334 if (!VD && !CurContext->isDependentContext()) 15335 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15336 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 15337 Vars.push_back((VD || CurContext->isDependentContext()) 15338 ? RefExpr->IgnoreParens() 15339 : Ref); 15340 PrivateCopies.push_back(VDPrivateRefExpr); 15341 } 15342 15343 if (Vars.empty()) 15344 return nullptr; 15345 15346 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 15347 PrivateCopies); 15348 } 15349 15350 namespace { 15351 class DiagsUninitializedSeveretyRAII { 15352 private: 15353 DiagnosticsEngine &Diags; 15354 SourceLocation SavedLoc; 15355 bool IsIgnored = false; 15356 15357 public: 15358 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 15359 bool IsIgnored) 15360 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 15361 if (!IsIgnored) { 15362 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 15363 /*Map*/ diag::Severity::Ignored, Loc); 15364 } 15365 } 15366 ~DiagsUninitializedSeveretyRAII() { 15367 if (!IsIgnored) 15368 Diags.popMappings(SavedLoc); 15369 } 15370 }; 15371 } 15372 15373 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 15374 SourceLocation StartLoc, 15375 SourceLocation LParenLoc, 15376 SourceLocation EndLoc) { 15377 SmallVector<Expr *, 8> Vars; 15378 SmallVector<Expr *, 8> PrivateCopies; 15379 SmallVector<Expr *, 8> Inits; 15380 SmallVector<Decl *, 4> ExprCaptures; 15381 bool IsImplicitClause = 15382 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 15383 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 15384 15385 for (Expr *RefExpr : VarList) { 15386 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 15387 SourceLocation ELoc; 15388 SourceRange ERange; 15389 Expr *SimpleRefExpr = RefExpr; 15390 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15391 if (Res.second) { 15392 // It will be analyzed later. 15393 Vars.push_back(RefExpr); 15394 PrivateCopies.push_back(nullptr); 15395 Inits.push_back(nullptr); 15396 } 15397 ValueDecl *D = Res.first; 15398 if (!D) 15399 continue; 15400 15401 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 15402 QualType Type = D->getType(); 15403 auto *VD = dyn_cast<VarDecl>(D); 15404 15405 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15406 // A variable that appears in a private clause must not have an incomplete 15407 // type or a reference type. 15408 if (RequireCompleteType(ELoc, Type, 15409 diag::err_omp_firstprivate_incomplete_type)) 15410 continue; 15411 Type = Type.getNonReferenceType(); 15412 15413 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 15414 // A variable of class type (or array thereof) that appears in a private 15415 // clause requires an accessible, unambiguous copy constructor for the 15416 // class type. 15417 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 15418 15419 // If an implicit firstprivate variable found it was checked already. 15420 DSAStackTy::DSAVarData TopDVar; 15421 if (!IsImplicitClause) { 15422 DSAStackTy::DSAVarData DVar = 15423 DSAStack->getTopDSA(D, /*FromParent=*/false); 15424 TopDVar = DVar; 15425 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15426 bool IsConstant = ElemType.isConstant(Context); 15427 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 15428 // A list item that specifies a given variable may not appear in more 15429 // than one clause on the same directive, except that a variable may be 15430 // specified in both firstprivate and lastprivate clauses. 15431 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 15432 // A list item may appear in a firstprivate or lastprivate clause but not 15433 // both. 15434 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 15435 (isOpenMPDistributeDirective(CurrDir) || 15436 DVar.CKind != OMPC_lastprivate) && 15437 DVar.RefExpr) { 15438 Diag(ELoc, diag::err_omp_wrong_dsa) 15439 << getOpenMPClauseName(DVar.CKind) 15440 << getOpenMPClauseName(OMPC_firstprivate); 15441 reportOriginalDsa(*this, DSAStack, D, DVar); 15442 continue; 15443 } 15444 15445 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15446 // in a Construct] 15447 // Variables with the predetermined data-sharing attributes may not be 15448 // listed in data-sharing attributes clauses, except for the cases 15449 // listed below. For these exceptions only, listing a predetermined 15450 // variable in a data-sharing attribute clause is allowed and overrides 15451 // the variable's predetermined data-sharing attributes. 15452 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15453 // in a Construct, C/C++, p.2] 15454 // Variables with const-qualified type having no mutable member may be 15455 // listed in a firstprivate clause, even if they are static data members. 15456 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 15457 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 15458 Diag(ELoc, diag::err_omp_wrong_dsa) 15459 << getOpenMPClauseName(DVar.CKind) 15460 << getOpenMPClauseName(OMPC_firstprivate); 15461 reportOriginalDsa(*this, DSAStack, D, DVar); 15462 continue; 15463 } 15464 15465 // OpenMP [2.9.3.4, Restrictions, p.2] 15466 // A list item that is private within a parallel region must not appear 15467 // in a firstprivate clause on a worksharing construct if any of the 15468 // worksharing regions arising from the worksharing construct ever bind 15469 // to any of the parallel regions arising from the parallel construct. 15470 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15471 // A list item that is private within a teams region must not appear in a 15472 // firstprivate clause on a distribute construct if any of the distribute 15473 // regions arising from the distribute construct ever bind to any of the 15474 // teams regions arising from the teams construct. 15475 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15476 // A list item that appears in a reduction clause of a teams construct 15477 // must not appear in a firstprivate clause on a distribute construct if 15478 // any of the distribute regions arising from the distribute construct 15479 // ever bind to any of the teams regions arising from the teams construct. 15480 if ((isOpenMPWorksharingDirective(CurrDir) || 15481 isOpenMPDistributeDirective(CurrDir)) && 15482 !isOpenMPParallelDirective(CurrDir) && 15483 !isOpenMPTeamsDirective(CurrDir)) { 15484 DVar = DSAStack->getImplicitDSA(D, true); 15485 if (DVar.CKind != OMPC_shared && 15486 (isOpenMPParallelDirective(DVar.DKind) || 15487 isOpenMPTeamsDirective(DVar.DKind) || 15488 DVar.DKind == OMPD_unknown)) { 15489 Diag(ELoc, diag::err_omp_required_access) 15490 << getOpenMPClauseName(OMPC_firstprivate) 15491 << getOpenMPClauseName(OMPC_shared); 15492 reportOriginalDsa(*this, DSAStack, D, DVar); 15493 continue; 15494 } 15495 } 15496 // OpenMP [2.9.3.4, Restrictions, p.3] 15497 // A list item that appears in a reduction clause of a parallel construct 15498 // must not appear in a firstprivate clause on a worksharing or task 15499 // construct if any of the worksharing or task regions arising from the 15500 // worksharing or task construct ever bind to any of the parallel regions 15501 // arising from the parallel construct. 15502 // OpenMP [2.9.3.4, Restrictions, p.4] 15503 // A list item that appears in a reduction clause in worksharing 15504 // construct must not appear in a firstprivate clause in a task construct 15505 // encountered during execution of any of the worksharing regions arising 15506 // from the worksharing construct. 15507 if (isOpenMPTaskingDirective(CurrDir)) { 15508 DVar = DSAStack->hasInnermostDSA( 15509 D, 15510 [](OpenMPClauseKind C, bool AppliedToPointee) { 15511 return C == OMPC_reduction && !AppliedToPointee; 15512 }, 15513 [](OpenMPDirectiveKind K) { 15514 return isOpenMPParallelDirective(K) || 15515 isOpenMPWorksharingDirective(K) || 15516 isOpenMPTeamsDirective(K); 15517 }, 15518 /*FromParent=*/true); 15519 if (DVar.CKind == OMPC_reduction && 15520 (isOpenMPParallelDirective(DVar.DKind) || 15521 isOpenMPWorksharingDirective(DVar.DKind) || 15522 isOpenMPTeamsDirective(DVar.DKind))) { 15523 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 15524 << getOpenMPDirectiveName(DVar.DKind); 15525 reportOriginalDsa(*this, DSAStack, D, DVar); 15526 continue; 15527 } 15528 } 15529 15530 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 15531 // A list item cannot appear in both a map clause and a data-sharing 15532 // attribute clause on the same construct 15533 // 15534 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 15535 // A list item cannot appear in both a map clause and a data-sharing 15536 // attribute clause on the same construct unless the construct is a 15537 // combined construct. 15538 if ((LangOpts.OpenMP <= 45 && 15539 isOpenMPTargetExecutionDirective(CurrDir)) || 15540 CurrDir == OMPD_target) { 15541 OpenMPClauseKind ConflictKind; 15542 if (DSAStack->checkMappableExprComponentListsForDecl( 15543 VD, /*CurrentRegionOnly=*/true, 15544 [&ConflictKind]( 15545 OMPClauseMappableExprCommon::MappableExprComponentListRef, 15546 OpenMPClauseKind WhereFoundClauseKind) { 15547 ConflictKind = WhereFoundClauseKind; 15548 return true; 15549 })) { 15550 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 15551 << getOpenMPClauseName(OMPC_firstprivate) 15552 << getOpenMPClauseName(ConflictKind) 15553 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 15554 reportOriginalDsa(*this, DSAStack, D, DVar); 15555 continue; 15556 } 15557 } 15558 } 15559 15560 // Variably modified types are not supported for tasks. 15561 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 15562 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 15563 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15564 << getOpenMPClauseName(OMPC_firstprivate) << Type 15565 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 15566 bool IsDecl = 15567 !VD || 15568 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15569 Diag(D->getLocation(), 15570 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15571 << D; 15572 continue; 15573 } 15574 15575 Type = Type.getUnqualifiedType(); 15576 VarDecl *VDPrivate = 15577 buildVarDecl(*this, ELoc, Type, D->getName(), 15578 D->hasAttrs() ? &D->getAttrs() : nullptr, 15579 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15580 // Generate helper private variable and initialize it with the value of the 15581 // original variable. The address of the original variable is replaced by 15582 // the address of the new private variable in the CodeGen. This new variable 15583 // is not added to IdResolver, so the code in the OpenMP region uses 15584 // original variable for proper diagnostics and variable capturing. 15585 Expr *VDInitRefExpr = nullptr; 15586 // For arrays generate initializer for single element and replace it by the 15587 // original array element in CodeGen. 15588 if (Type->isArrayType()) { 15589 VarDecl *VDInit = 15590 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 15591 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 15592 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 15593 ElemType = ElemType.getUnqualifiedType(); 15594 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 15595 ".firstprivate.temp"); 15596 InitializedEntity Entity = 15597 InitializedEntity::InitializeVariable(VDInitTemp); 15598 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 15599 15600 InitializationSequence InitSeq(*this, Entity, Kind, Init); 15601 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 15602 if (Result.isInvalid()) 15603 VDPrivate->setInvalidDecl(); 15604 else 15605 VDPrivate->setInit(Result.getAs<Expr>()); 15606 // Remove temp variable declaration. 15607 Context.Deallocate(VDInitTemp); 15608 } else { 15609 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 15610 ".firstprivate.temp"); 15611 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 15612 RefExpr->getExprLoc()); 15613 AddInitializerToDecl(VDPrivate, 15614 DefaultLvalueConversion(VDInitRefExpr).get(), 15615 /*DirectInit=*/false); 15616 } 15617 if (VDPrivate->isInvalidDecl()) { 15618 if (IsImplicitClause) { 15619 Diag(RefExpr->getExprLoc(), 15620 diag::note_omp_task_predetermined_firstprivate_here); 15621 } 15622 continue; 15623 } 15624 CurContext->addDecl(VDPrivate); 15625 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 15626 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 15627 RefExpr->getExprLoc()); 15628 DeclRefExpr *Ref = nullptr; 15629 if (!VD && !CurContext->isDependentContext()) { 15630 if (TopDVar.CKind == OMPC_lastprivate) { 15631 Ref = TopDVar.PrivateCopy; 15632 } else { 15633 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15634 if (!isOpenMPCapturedDecl(D)) 15635 ExprCaptures.push_back(Ref->getDecl()); 15636 } 15637 } 15638 if (!IsImplicitClause) 15639 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 15640 Vars.push_back((VD || CurContext->isDependentContext()) 15641 ? RefExpr->IgnoreParens() 15642 : Ref); 15643 PrivateCopies.push_back(VDPrivateRefExpr); 15644 Inits.push_back(VDInitRefExpr); 15645 } 15646 15647 if (Vars.empty()) 15648 return nullptr; 15649 15650 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 15651 Vars, PrivateCopies, Inits, 15652 buildPreInits(Context, ExprCaptures)); 15653 } 15654 15655 OMPClause *Sema::ActOnOpenMPLastprivateClause( 15656 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 15657 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 15658 SourceLocation LParenLoc, SourceLocation EndLoc) { 15659 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 15660 assert(ColonLoc.isValid() && "Colon location must be valid."); 15661 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 15662 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 15663 /*Last=*/OMPC_LASTPRIVATE_unknown) 15664 << getOpenMPClauseName(OMPC_lastprivate); 15665 return nullptr; 15666 } 15667 15668 SmallVector<Expr *, 8> Vars; 15669 SmallVector<Expr *, 8> SrcExprs; 15670 SmallVector<Expr *, 8> DstExprs; 15671 SmallVector<Expr *, 8> AssignmentOps; 15672 SmallVector<Decl *, 4> ExprCaptures; 15673 SmallVector<Expr *, 4> ExprPostUpdates; 15674 for (Expr *RefExpr : VarList) { 15675 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 15676 SourceLocation ELoc; 15677 SourceRange ERange; 15678 Expr *SimpleRefExpr = RefExpr; 15679 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15680 if (Res.second) { 15681 // It will be analyzed later. 15682 Vars.push_back(RefExpr); 15683 SrcExprs.push_back(nullptr); 15684 DstExprs.push_back(nullptr); 15685 AssignmentOps.push_back(nullptr); 15686 } 15687 ValueDecl *D = Res.first; 15688 if (!D) 15689 continue; 15690 15691 QualType Type = D->getType(); 15692 auto *VD = dyn_cast<VarDecl>(D); 15693 15694 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 15695 // A variable that appears in a lastprivate clause must not have an 15696 // incomplete type or a reference type. 15697 if (RequireCompleteType(ELoc, Type, 15698 diag::err_omp_lastprivate_incomplete_type)) 15699 continue; 15700 Type = Type.getNonReferenceType(); 15701 15702 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15703 // A variable that is privatized must not have a const-qualified type 15704 // unless it is of class type with a mutable member. This restriction does 15705 // not apply to the firstprivate clause. 15706 // 15707 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 15708 // A variable that appears in a lastprivate clause must not have a 15709 // const-qualified type unless it is of class type with a mutable member. 15710 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 15711 continue; 15712 15713 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 15714 // A list item that appears in a lastprivate clause with the conditional 15715 // modifier must be a scalar variable. 15716 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 15717 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 15718 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15719 VarDecl::DeclarationOnly; 15720 Diag(D->getLocation(), 15721 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15722 << D; 15723 continue; 15724 } 15725 15726 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15727 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 15728 // in a Construct] 15729 // Variables with the predetermined data-sharing attributes may not be 15730 // listed in data-sharing attributes clauses, except for the cases 15731 // listed below. 15732 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 15733 // A list item may appear in a firstprivate or lastprivate clause but not 15734 // both. 15735 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15736 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 15737 (isOpenMPDistributeDirective(CurrDir) || 15738 DVar.CKind != OMPC_firstprivate) && 15739 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 15740 Diag(ELoc, diag::err_omp_wrong_dsa) 15741 << getOpenMPClauseName(DVar.CKind) 15742 << getOpenMPClauseName(OMPC_lastprivate); 15743 reportOriginalDsa(*this, DSAStack, D, DVar); 15744 continue; 15745 } 15746 15747 // OpenMP [2.14.3.5, Restrictions, p.2] 15748 // A list item that is private within a parallel region, or that appears in 15749 // the reduction clause of a parallel construct, must not appear in a 15750 // lastprivate clause on a worksharing construct if any of the corresponding 15751 // worksharing regions ever binds to any of the corresponding parallel 15752 // regions. 15753 DSAStackTy::DSAVarData TopDVar = DVar; 15754 if (isOpenMPWorksharingDirective(CurrDir) && 15755 !isOpenMPParallelDirective(CurrDir) && 15756 !isOpenMPTeamsDirective(CurrDir)) { 15757 DVar = DSAStack->getImplicitDSA(D, true); 15758 if (DVar.CKind != OMPC_shared) { 15759 Diag(ELoc, diag::err_omp_required_access) 15760 << getOpenMPClauseName(OMPC_lastprivate) 15761 << getOpenMPClauseName(OMPC_shared); 15762 reportOriginalDsa(*this, DSAStack, D, DVar); 15763 continue; 15764 } 15765 } 15766 15767 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 15768 // A variable of class type (or array thereof) that appears in a 15769 // lastprivate clause requires an accessible, unambiguous default 15770 // constructor for the class type, unless the list item is also specified 15771 // in a firstprivate clause. 15772 // A variable of class type (or array thereof) that appears in a 15773 // lastprivate clause requires an accessible, unambiguous copy assignment 15774 // operator for the class type. 15775 Type = Context.getBaseElementType(Type).getNonReferenceType(); 15776 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 15777 Type.getUnqualifiedType(), ".lastprivate.src", 15778 D->hasAttrs() ? &D->getAttrs() : nullptr); 15779 DeclRefExpr *PseudoSrcExpr = 15780 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 15781 VarDecl *DstVD = 15782 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 15783 D->hasAttrs() ? &D->getAttrs() : nullptr); 15784 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 15785 // For arrays generate assignment operation for single element and replace 15786 // it by the original array element in CodeGen. 15787 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 15788 PseudoDstExpr, PseudoSrcExpr); 15789 if (AssignmentOp.isInvalid()) 15790 continue; 15791 AssignmentOp = 15792 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 15793 if (AssignmentOp.isInvalid()) 15794 continue; 15795 15796 DeclRefExpr *Ref = nullptr; 15797 if (!VD && !CurContext->isDependentContext()) { 15798 if (TopDVar.CKind == OMPC_firstprivate) { 15799 Ref = TopDVar.PrivateCopy; 15800 } else { 15801 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15802 if (!isOpenMPCapturedDecl(D)) 15803 ExprCaptures.push_back(Ref->getDecl()); 15804 } 15805 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 15806 (!isOpenMPCapturedDecl(D) && 15807 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 15808 ExprResult RefRes = DefaultLvalueConversion(Ref); 15809 if (!RefRes.isUsable()) 15810 continue; 15811 ExprResult PostUpdateRes = 15812 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 15813 RefRes.get()); 15814 if (!PostUpdateRes.isUsable()) 15815 continue; 15816 ExprPostUpdates.push_back( 15817 IgnoredValueConversions(PostUpdateRes.get()).get()); 15818 } 15819 } 15820 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 15821 Vars.push_back((VD || CurContext->isDependentContext()) 15822 ? RefExpr->IgnoreParens() 15823 : Ref); 15824 SrcExprs.push_back(PseudoSrcExpr); 15825 DstExprs.push_back(PseudoDstExpr); 15826 AssignmentOps.push_back(AssignmentOp.get()); 15827 } 15828 15829 if (Vars.empty()) 15830 return nullptr; 15831 15832 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 15833 Vars, SrcExprs, DstExprs, AssignmentOps, 15834 LPKind, LPKindLoc, ColonLoc, 15835 buildPreInits(Context, ExprCaptures), 15836 buildPostUpdate(*this, ExprPostUpdates)); 15837 } 15838 15839 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 15840 SourceLocation StartLoc, 15841 SourceLocation LParenLoc, 15842 SourceLocation EndLoc) { 15843 SmallVector<Expr *, 8> Vars; 15844 for (Expr *RefExpr : VarList) { 15845 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 15846 SourceLocation ELoc; 15847 SourceRange ERange; 15848 Expr *SimpleRefExpr = RefExpr; 15849 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15850 if (Res.second) { 15851 // It will be analyzed later. 15852 Vars.push_back(RefExpr); 15853 } 15854 ValueDecl *D = Res.first; 15855 if (!D) 15856 continue; 15857 15858 auto *VD = dyn_cast<VarDecl>(D); 15859 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15860 // in a Construct] 15861 // Variables with the predetermined data-sharing attributes may not be 15862 // listed in data-sharing attributes clauses, except for the cases 15863 // listed below. For these exceptions only, listing a predetermined 15864 // variable in a data-sharing attribute clause is allowed and overrides 15865 // the variable's predetermined data-sharing attributes. 15866 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15867 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 15868 DVar.RefExpr) { 15869 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15870 << getOpenMPClauseName(OMPC_shared); 15871 reportOriginalDsa(*this, DSAStack, D, DVar); 15872 continue; 15873 } 15874 15875 DeclRefExpr *Ref = nullptr; 15876 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 15877 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15878 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 15879 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 15880 ? RefExpr->IgnoreParens() 15881 : Ref); 15882 } 15883 15884 if (Vars.empty()) 15885 return nullptr; 15886 15887 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 15888 } 15889 15890 namespace { 15891 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 15892 DSAStackTy *Stack; 15893 15894 public: 15895 bool VisitDeclRefExpr(DeclRefExpr *E) { 15896 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 15897 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 15898 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 15899 return false; 15900 if (DVar.CKind != OMPC_unknown) 15901 return true; 15902 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 15903 VD, 15904 [](OpenMPClauseKind C, bool AppliedToPointee) { 15905 return isOpenMPPrivate(C) && !AppliedToPointee; 15906 }, 15907 [](OpenMPDirectiveKind) { return true; }, 15908 /*FromParent=*/true); 15909 return DVarPrivate.CKind != OMPC_unknown; 15910 } 15911 return false; 15912 } 15913 bool VisitStmt(Stmt *S) { 15914 for (Stmt *Child : S->children()) { 15915 if (Child && Visit(Child)) 15916 return true; 15917 } 15918 return false; 15919 } 15920 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 15921 }; 15922 } // namespace 15923 15924 namespace { 15925 // Transform MemberExpression for specified FieldDecl of current class to 15926 // DeclRefExpr to specified OMPCapturedExprDecl. 15927 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 15928 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 15929 ValueDecl *Field = nullptr; 15930 DeclRefExpr *CapturedExpr = nullptr; 15931 15932 public: 15933 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 15934 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 15935 15936 ExprResult TransformMemberExpr(MemberExpr *E) { 15937 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 15938 E->getMemberDecl() == Field) { 15939 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 15940 return CapturedExpr; 15941 } 15942 return BaseTransform::TransformMemberExpr(E); 15943 } 15944 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 15945 }; 15946 } // namespace 15947 15948 template <typename T, typename U> 15949 static T filterLookupForUDReductionAndMapper( 15950 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 15951 for (U &Set : Lookups) { 15952 for (auto *D : Set) { 15953 if (T Res = Gen(cast<ValueDecl>(D))) 15954 return Res; 15955 } 15956 } 15957 return T(); 15958 } 15959 15960 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 15961 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 15962 15963 for (auto RD : D->redecls()) { 15964 // Don't bother with extra checks if we already know this one isn't visible. 15965 if (RD == D) 15966 continue; 15967 15968 auto ND = cast<NamedDecl>(RD); 15969 if (LookupResult::isVisible(SemaRef, ND)) 15970 return ND; 15971 } 15972 15973 return nullptr; 15974 } 15975 15976 static void 15977 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 15978 SourceLocation Loc, QualType Ty, 15979 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 15980 // Find all of the associated namespaces and classes based on the 15981 // arguments we have. 15982 Sema::AssociatedNamespaceSet AssociatedNamespaces; 15983 Sema::AssociatedClassSet AssociatedClasses; 15984 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 15985 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 15986 AssociatedClasses); 15987 15988 // C++ [basic.lookup.argdep]p3: 15989 // Let X be the lookup set produced by unqualified lookup (3.4.1) 15990 // and let Y be the lookup set produced by argument dependent 15991 // lookup (defined as follows). If X contains [...] then Y is 15992 // empty. Otherwise Y is the set of declarations found in the 15993 // namespaces associated with the argument types as described 15994 // below. The set of declarations found by the lookup of the name 15995 // is the union of X and Y. 15996 // 15997 // Here, we compute Y and add its members to the overloaded 15998 // candidate set. 15999 for (auto *NS : AssociatedNamespaces) { 16000 // When considering an associated namespace, the lookup is the 16001 // same as the lookup performed when the associated namespace is 16002 // used as a qualifier (3.4.3.2) except that: 16003 // 16004 // -- Any using-directives in the associated namespace are 16005 // ignored. 16006 // 16007 // -- Any namespace-scope friend functions declared in 16008 // associated classes are visible within their respective 16009 // namespaces even if they are not visible during an ordinary 16010 // lookup (11.4). 16011 DeclContext::lookup_result R = NS->lookup(Id.getName()); 16012 for (auto *D : R) { 16013 auto *Underlying = D; 16014 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16015 Underlying = USD->getTargetDecl(); 16016 16017 if (!isa<OMPDeclareReductionDecl>(Underlying) && 16018 !isa<OMPDeclareMapperDecl>(Underlying)) 16019 continue; 16020 16021 if (!SemaRef.isVisible(D)) { 16022 D = findAcceptableDecl(SemaRef, D); 16023 if (!D) 16024 continue; 16025 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16026 Underlying = USD->getTargetDecl(); 16027 } 16028 Lookups.emplace_back(); 16029 Lookups.back().addDecl(Underlying); 16030 } 16031 } 16032 } 16033 16034 static ExprResult 16035 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 16036 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 16037 const DeclarationNameInfo &ReductionId, QualType Ty, 16038 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 16039 if (ReductionIdScopeSpec.isInvalid()) 16040 return ExprError(); 16041 SmallVector<UnresolvedSet<8>, 4> Lookups; 16042 if (S) { 16043 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16044 Lookup.suppressDiagnostics(); 16045 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 16046 NamedDecl *D = Lookup.getRepresentativeDecl(); 16047 do { 16048 S = S->getParent(); 16049 } while (S && !S->isDeclScope(D)); 16050 if (S) 16051 S = S->getParent(); 16052 Lookups.emplace_back(); 16053 Lookups.back().append(Lookup.begin(), Lookup.end()); 16054 Lookup.clear(); 16055 } 16056 } else if (auto *ULE = 16057 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 16058 Lookups.push_back(UnresolvedSet<8>()); 16059 Decl *PrevD = nullptr; 16060 for (NamedDecl *D : ULE->decls()) { 16061 if (D == PrevD) 16062 Lookups.push_back(UnresolvedSet<8>()); 16063 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 16064 Lookups.back().addDecl(DRD); 16065 PrevD = D; 16066 } 16067 } 16068 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 16069 Ty->isInstantiationDependentType() || 16070 Ty->containsUnexpandedParameterPack() || 16071 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 16072 return !D->isInvalidDecl() && 16073 (D->getType()->isDependentType() || 16074 D->getType()->isInstantiationDependentType() || 16075 D->getType()->containsUnexpandedParameterPack()); 16076 })) { 16077 UnresolvedSet<8> ResSet; 16078 for (const UnresolvedSet<8> &Set : Lookups) { 16079 if (Set.empty()) 16080 continue; 16081 ResSet.append(Set.begin(), Set.end()); 16082 // The last item marks the end of all declarations at the specified scope. 16083 ResSet.addDecl(Set[Set.size() - 1]); 16084 } 16085 return UnresolvedLookupExpr::Create( 16086 SemaRef.Context, /*NamingClass=*/nullptr, 16087 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 16088 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 16089 } 16090 // Lookup inside the classes. 16091 // C++ [over.match.oper]p3: 16092 // For a unary operator @ with an operand of a type whose 16093 // cv-unqualified version is T1, and for a binary operator @ with 16094 // a left operand of a type whose cv-unqualified version is T1 and 16095 // a right operand of a type whose cv-unqualified version is T2, 16096 // three sets of candidate functions, designated member 16097 // candidates, non-member candidates and built-in candidates, are 16098 // constructed as follows: 16099 // -- If T1 is a complete class type or a class currently being 16100 // defined, the set of member candidates is the result of the 16101 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 16102 // the set of member candidates is empty. 16103 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16104 Lookup.suppressDiagnostics(); 16105 if (const auto *TyRec = Ty->getAs<RecordType>()) { 16106 // Complete the type if it can be completed. 16107 // If the type is neither complete nor being defined, bail out now. 16108 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 16109 TyRec->getDecl()->getDefinition()) { 16110 Lookup.clear(); 16111 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 16112 if (Lookup.empty()) { 16113 Lookups.emplace_back(); 16114 Lookups.back().append(Lookup.begin(), Lookup.end()); 16115 } 16116 } 16117 } 16118 // Perform ADL. 16119 if (SemaRef.getLangOpts().CPlusPlus) 16120 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 16121 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16122 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 16123 if (!D->isInvalidDecl() && 16124 SemaRef.Context.hasSameType(D->getType(), Ty)) 16125 return D; 16126 return nullptr; 16127 })) 16128 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 16129 VK_LValue, Loc); 16130 if (SemaRef.getLangOpts().CPlusPlus) { 16131 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16132 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 16133 if (!D->isInvalidDecl() && 16134 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 16135 !Ty.isMoreQualifiedThan(D->getType())) 16136 return D; 16137 return nullptr; 16138 })) { 16139 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 16140 /*DetectVirtual=*/false); 16141 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 16142 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 16143 VD->getType().getUnqualifiedType()))) { 16144 if (SemaRef.CheckBaseClassAccess( 16145 Loc, VD->getType(), Ty, Paths.front(), 16146 /*DiagID=*/0) != Sema::AR_inaccessible) { 16147 SemaRef.BuildBasePathArray(Paths, BasePath); 16148 return SemaRef.BuildDeclRefExpr( 16149 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 16150 } 16151 } 16152 } 16153 } 16154 } 16155 if (ReductionIdScopeSpec.isSet()) { 16156 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 16157 << Ty << Range; 16158 return ExprError(); 16159 } 16160 return ExprEmpty(); 16161 } 16162 16163 namespace { 16164 /// Data for the reduction-based clauses. 16165 struct ReductionData { 16166 /// List of original reduction items. 16167 SmallVector<Expr *, 8> Vars; 16168 /// List of private copies of the reduction items. 16169 SmallVector<Expr *, 8> Privates; 16170 /// LHS expressions for the reduction_op expressions. 16171 SmallVector<Expr *, 8> LHSs; 16172 /// RHS expressions for the reduction_op expressions. 16173 SmallVector<Expr *, 8> RHSs; 16174 /// Reduction operation expression. 16175 SmallVector<Expr *, 8> ReductionOps; 16176 /// inscan copy operation expressions. 16177 SmallVector<Expr *, 8> InscanCopyOps; 16178 /// inscan copy temp array expressions for prefix sums. 16179 SmallVector<Expr *, 8> InscanCopyArrayTemps; 16180 /// inscan copy temp array element expressions for prefix sums. 16181 SmallVector<Expr *, 8> InscanCopyArrayElems; 16182 /// Taskgroup descriptors for the corresponding reduction items in 16183 /// in_reduction clauses. 16184 SmallVector<Expr *, 8> TaskgroupDescriptors; 16185 /// List of captures for clause. 16186 SmallVector<Decl *, 4> ExprCaptures; 16187 /// List of postupdate expressions. 16188 SmallVector<Expr *, 4> ExprPostUpdates; 16189 /// Reduction modifier. 16190 unsigned RedModifier = 0; 16191 ReductionData() = delete; 16192 /// Reserves required memory for the reduction data. 16193 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 16194 Vars.reserve(Size); 16195 Privates.reserve(Size); 16196 LHSs.reserve(Size); 16197 RHSs.reserve(Size); 16198 ReductionOps.reserve(Size); 16199 if (RedModifier == OMPC_REDUCTION_inscan) { 16200 InscanCopyOps.reserve(Size); 16201 InscanCopyArrayTemps.reserve(Size); 16202 InscanCopyArrayElems.reserve(Size); 16203 } 16204 TaskgroupDescriptors.reserve(Size); 16205 ExprCaptures.reserve(Size); 16206 ExprPostUpdates.reserve(Size); 16207 } 16208 /// Stores reduction item and reduction operation only (required for dependent 16209 /// reduction item). 16210 void push(Expr *Item, Expr *ReductionOp) { 16211 Vars.emplace_back(Item); 16212 Privates.emplace_back(nullptr); 16213 LHSs.emplace_back(nullptr); 16214 RHSs.emplace_back(nullptr); 16215 ReductionOps.emplace_back(ReductionOp); 16216 TaskgroupDescriptors.emplace_back(nullptr); 16217 if (RedModifier == OMPC_REDUCTION_inscan) { 16218 InscanCopyOps.push_back(nullptr); 16219 InscanCopyArrayTemps.push_back(nullptr); 16220 InscanCopyArrayElems.push_back(nullptr); 16221 } 16222 } 16223 /// Stores reduction data. 16224 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 16225 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 16226 Expr *CopyArrayElem) { 16227 Vars.emplace_back(Item); 16228 Privates.emplace_back(Private); 16229 LHSs.emplace_back(LHS); 16230 RHSs.emplace_back(RHS); 16231 ReductionOps.emplace_back(ReductionOp); 16232 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 16233 if (RedModifier == OMPC_REDUCTION_inscan) { 16234 InscanCopyOps.push_back(CopyOp); 16235 InscanCopyArrayTemps.push_back(CopyArrayTemp); 16236 InscanCopyArrayElems.push_back(CopyArrayElem); 16237 } else { 16238 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 16239 CopyArrayElem == nullptr && 16240 "Copy operation must be used for inscan reductions only."); 16241 } 16242 } 16243 }; 16244 } // namespace 16245 16246 static bool checkOMPArraySectionConstantForReduction( 16247 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 16248 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 16249 const Expr *Length = OASE->getLength(); 16250 if (Length == nullptr) { 16251 // For array sections of the form [1:] or [:], we would need to analyze 16252 // the lower bound... 16253 if (OASE->getColonLocFirst().isValid()) 16254 return false; 16255 16256 // This is an array subscript which has implicit length 1! 16257 SingleElement = true; 16258 ArraySizes.push_back(llvm::APSInt::get(1)); 16259 } else { 16260 Expr::EvalResult Result; 16261 if (!Length->EvaluateAsInt(Result, Context)) 16262 return false; 16263 16264 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16265 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 16266 ArraySizes.push_back(ConstantLengthValue); 16267 } 16268 16269 // Get the base of this array section and walk up from there. 16270 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 16271 16272 // We require length = 1 for all array sections except the right-most to 16273 // guarantee that the memory region is contiguous and has no holes in it. 16274 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 16275 Length = TempOASE->getLength(); 16276 if (Length == nullptr) { 16277 // For array sections of the form [1:] or [:], we would need to analyze 16278 // the lower bound... 16279 if (OASE->getColonLocFirst().isValid()) 16280 return false; 16281 16282 // This is an array subscript which has implicit length 1! 16283 ArraySizes.push_back(llvm::APSInt::get(1)); 16284 } else { 16285 Expr::EvalResult Result; 16286 if (!Length->EvaluateAsInt(Result, Context)) 16287 return false; 16288 16289 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16290 if (ConstantLengthValue.getSExtValue() != 1) 16291 return false; 16292 16293 ArraySizes.push_back(ConstantLengthValue); 16294 } 16295 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 16296 } 16297 16298 // If we have a single element, we don't need to add the implicit lengths. 16299 if (!SingleElement) { 16300 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 16301 // Has implicit length 1! 16302 ArraySizes.push_back(llvm::APSInt::get(1)); 16303 Base = TempASE->getBase()->IgnoreParenImpCasts(); 16304 } 16305 } 16306 16307 // This array section can be privatized as a single value or as a constant 16308 // sized array. 16309 return true; 16310 } 16311 16312 static bool actOnOMPReductionKindClause( 16313 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 16314 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 16315 SourceLocation ColonLoc, SourceLocation EndLoc, 16316 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 16317 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 16318 DeclarationName DN = ReductionId.getName(); 16319 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 16320 BinaryOperatorKind BOK = BO_Comma; 16321 16322 ASTContext &Context = S.Context; 16323 // OpenMP [2.14.3.6, reduction clause] 16324 // C 16325 // reduction-identifier is either an identifier or one of the following 16326 // operators: +, -, *, &, |, ^, && and || 16327 // C++ 16328 // reduction-identifier is either an id-expression or one of the following 16329 // operators: +, -, *, &, |, ^, && and || 16330 switch (OOK) { 16331 case OO_Plus: 16332 case OO_Minus: 16333 BOK = BO_Add; 16334 break; 16335 case OO_Star: 16336 BOK = BO_Mul; 16337 break; 16338 case OO_Amp: 16339 BOK = BO_And; 16340 break; 16341 case OO_Pipe: 16342 BOK = BO_Or; 16343 break; 16344 case OO_Caret: 16345 BOK = BO_Xor; 16346 break; 16347 case OO_AmpAmp: 16348 BOK = BO_LAnd; 16349 break; 16350 case OO_PipePipe: 16351 BOK = BO_LOr; 16352 break; 16353 case OO_New: 16354 case OO_Delete: 16355 case OO_Array_New: 16356 case OO_Array_Delete: 16357 case OO_Slash: 16358 case OO_Percent: 16359 case OO_Tilde: 16360 case OO_Exclaim: 16361 case OO_Equal: 16362 case OO_Less: 16363 case OO_Greater: 16364 case OO_LessEqual: 16365 case OO_GreaterEqual: 16366 case OO_PlusEqual: 16367 case OO_MinusEqual: 16368 case OO_StarEqual: 16369 case OO_SlashEqual: 16370 case OO_PercentEqual: 16371 case OO_CaretEqual: 16372 case OO_AmpEqual: 16373 case OO_PipeEqual: 16374 case OO_LessLess: 16375 case OO_GreaterGreater: 16376 case OO_LessLessEqual: 16377 case OO_GreaterGreaterEqual: 16378 case OO_EqualEqual: 16379 case OO_ExclaimEqual: 16380 case OO_Spaceship: 16381 case OO_PlusPlus: 16382 case OO_MinusMinus: 16383 case OO_Comma: 16384 case OO_ArrowStar: 16385 case OO_Arrow: 16386 case OO_Call: 16387 case OO_Subscript: 16388 case OO_Conditional: 16389 case OO_Coawait: 16390 case NUM_OVERLOADED_OPERATORS: 16391 llvm_unreachable("Unexpected reduction identifier"); 16392 case OO_None: 16393 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 16394 if (II->isStr("max")) 16395 BOK = BO_GT; 16396 else if (II->isStr("min")) 16397 BOK = BO_LT; 16398 } 16399 break; 16400 } 16401 SourceRange ReductionIdRange; 16402 if (ReductionIdScopeSpec.isValid()) 16403 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 16404 else 16405 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 16406 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 16407 16408 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 16409 bool FirstIter = true; 16410 for (Expr *RefExpr : VarList) { 16411 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 16412 // OpenMP [2.1, C/C++] 16413 // A list item is a variable or array section, subject to the restrictions 16414 // specified in Section 2.4 on page 42 and in each of the sections 16415 // describing clauses and directives for which a list appears. 16416 // OpenMP [2.14.3.3, Restrictions, p.1] 16417 // A variable that is part of another variable (as an array or 16418 // structure element) cannot appear in a private clause. 16419 if (!FirstIter && IR != ER) 16420 ++IR; 16421 FirstIter = false; 16422 SourceLocation ELoc; 16423 SourceRange ERange; 16424 Expr *SimpleRefExpr = RefExpr; 16425 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 16426 /*AllowArraySection=*/true); 16427 if (Res.second) { 16428 // Try to find 'declare reduction' corresponding construct before using 16429 // builtin/overloaded operators. 16430 QualType Type = Context.DependentTy; 16431 CXXCastPath BasePath; 16432 ExprResult DeclareReductionRef = buildDeclareReductionRef( 16433 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 16434 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 16435 Expr *ReductionOp = nullptr; 16436 if (S.CurContext->isDependentContext() && 16437 (DeclareReductionRef.isUnset() || 16438 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 16439 ReductionOp = DeclareReductionRef.get(); 16440 // It will be analyzed later. 16441 RD.push(RefExpr, ReductionOp); 16442 } 16443 ValueDecl *D = Res.first; 16444 if (!D) 16445 continue; 16446 16447 Expr *TaskgroupDescriptor = nullptr; 16448 QualType Type; 16449 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 16450 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 16451 if (ASE) { 16452 Type = ASE->getType().getNonReferenceType(); 16453 } else if (OASE) { 16454 QualType BaseType = 16455 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16456 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16457 Type = ATy->getElementType(); 16458 else 16459 Type = BaseType->getPointeeType(); 16460 Type = Type.getNonReferenceType(); 16461 } else { 16462 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 16463 } 16464 auto *VD = dyn_cast<VarDecl>(D); 16465 16466 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 16467 // A variable that appears in a private clause must not have an incomplete 16468 // type or a reference type. 16469 if (S.RequireCompleteType(ELoc, D->getType(), 16470 diag::err_omp_reduction_incomplete_type)) 16471 continue; 16472 // OpenMP [2.14.3.6, reduction clause, Restrictions] 16473 // A list item that appears in a reduction clause must not be 16474 // const-qualified. 16475 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 16476 /*AcceptIfMutable*/ false, ASE || OASE)) 16477 continue; 16478 16479 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 16480 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 16481 // If a list-item is a reference type then it must bind to the same object 16482 // for all threads of the team. 16483 if (!ASE && !OASE) { 16484 if (VD) { 16485 VarDecl *VDDef = VD->getDefinition(); 16486 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 16487 DSARefChecker Check(Stack); 16488 if (Check.Visit(VDDef->getInit())) { 16489 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 16490 << getOpenMPClauseName(ClauseKind) << ERange; 16491 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 16492 continue; 16493 } 16494 } 16495 } 16496 16497 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 16498 // in a Construct] 16499 // Variables with the predetermined data-sharing attributes may not be 16500 // listed in data-sharing attributes clauses, except for the cases 16501 // listed below. For these exceptions only, listing a predetermined 16502 // variable in a data-sharing attribute clause is allowed and overrides 16503 // the variable's predetermined data-sharing attributes. 16504 // OpenMP [2.14.3.6, Restrictions, p.3] 16505 // Any number of reduction clauses can be specified on the directive, 16506 // but a list item can appear only once in the reduction clauses for that 16507 // directive. 16508 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 16509 if (DVar.CKind == OMPC_reduction) { 16510 S.Diag(ELoc, diag::err_omp_once_referenced) 16511 << getOpenMPClauseName(ClauseKind); 16512 if (DVar.RefExpr) 16513 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 16514 continue; 16515 } 16516 if (DVar.CKind != OMPC_unknown) { 16517 S.Diag(ELoc, diag::err_omp_wrong_dsa) 16518 << getOpenMPClauseName(DVar.CKind) 16519 << getOpenMPClauseName(OMPC_reduction); 16520 reportOriginalDsa(S, Stack, D, DVar); 16521 continue; 16522 } 16523 16524 // OpenMP [2.14.3.6, Restrictions, p.1] 16525 // A list item that appears in a reduction clause of a worksharing 16526 // construct must be shared in the parallel regions to which any of the 16527 // worksharing regions arising from the worksharing construct bind. 16528 if (isOpenMPWorksharingDirective(CurrDir) && 16529 !isOpenMPParallelDirective(CurrDir) && 16530 !isOpenMPTeamsDirective(CurrDir)) { 16531 DVar = Stack->getImplicitDSA(D, true); 16532 if (DVar.CKind != OMPC_shared) { 16533 S.Diag(ELoc, diag::err_omp_required_access) 16534 << getOpenMPClauseName(OMPC_reduction) 16535 << getOpenMPClauseName(OMPC_shared); 16536 reportOriginalDsa(S, Stack, D, DVar); 16537 continue; 16538 } 16539 } 16540 } else { 16541 // Threadprivates cannot be shared between threads, so dignose if the base 16542 // is a threadprivate variable. 16543 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 16544 if (DVar.CKind == OMPC_threadprivate) { 16545 S.Diag(ELoc, diag::err_omp_wrong_dsa) 16546 << getOpenMPClauseName(DVar.CKind) 16547 << getOpenMPClauseName(OMPC_reduction); 16548 reportOriginalDsa(S, Stack, D, DVar); 16549 continue; 16550 } 16551 } 16552 16553 // Try to find 'declare reduction' corresponding construct before using 16554 // builtin/overloaded operators. 16555 CXXCastPath BasePath; 16556 ExprResult DeclareReductionRef = buildDeclareReductionRef( 16557 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 16558 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 16559 if (DeclareReductionRef.isInvalid()) 16560 continue; 16561 if (S.CurContext->isDependentContext() && 16562 (DeclareReductionRef.isUnset() || 16563 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 16564 RD.push(RefExpr, DeclareReductionRef.get()); 16565 continue; 16566 } 16567 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 16568 // Not allowed reduction identifier is found. 16569 S.Diag(ReductionId.getBeginLoc(), 16570 diag::err_omp_unknown_reduction_identifier) 16571 << Type << ReductionIdRange; 16572 continue; 16573 } 16574 16575 // OpenMP [2.14.3.6, reduction clause, Restrictions] 16576 // The type of a list item that appears in a reduction clause must be valid 16577 // for the reduction-identifier. For a max or min reduction in C, the type 16578 // of the list item must be an allowed arithmetic data type: char, int, 16579 // float, double, or _Bool, possibly modified with long, short, signed, or 16580 // unsigned. For a max or min reduction in C++, the type of the list item 16581 // must be an allowed arithmetic data type: char, wchar_t, int, float, 16582 // double, or bool, possibly modified with long, short, signed, or unsigned. 16583 if (DeclareReductionRef.isUnset()) { 16584 if ((BOK == BO_GT || BOK == BO_LT) && 16585 !(Type->isScalarType() || 16586 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 16587 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 16588 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 16589 if (!ASE && !OASE) { 16590 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16591 VarDecl::DeclarationOnly; 16592 S.Diag(D->getLocation(), 16593 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16594 << D; 16595 } 16596 continue; 16597 } 16598 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 16599 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 16600 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 16601 << getOpenMPClauseName(ClauseKind); 16602 if (!ASE && !OASE) { 16603 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16604 VarDecl::DeclarationOnly; 16605 S.Diag(D->getLocation(), 16606 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16607 << D; 16608 } 16609 continue; 16610 } 16611 } 16612 16613 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 16614 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 16615 D->hasAttrs() ? &D->getAttrs() : nullptr); 16616 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 16617 D->hasAttrs() ? &D->getAttrs() : nullptr); 16618 QualType PrivateTy = Type; 16619 16620 // Try if we can determine constant lengths for all array sections and avoid 16621 // the VLA. 16622 bool ConstantLengthOASE = false; 16623 if (OASE) { 16624 bool SingleElement; 16625 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 16626 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 16627 Context, OASE, SingleElement, ArraySizes); 16628 16629 // If we don't have a single element, we must emit a constant array type. 16630 if (ConstantLengthOASE && !SingleElement) { 16631 for (llvm::APSInt &Size : ArraySizes) 16632 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 16633 ArrayType::Normal, 16634 /*IndexTypeQuals=*/0); 16635 } 16636 } 16637 16638 if ((OASE && !ConstantLengthOASE) || 16639 (!OASE && !ASE && 16640 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 16641 if (!Context.getTargetInfo().isVLASupported()) { 16642 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 16643 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 16644 S.Diag(ELoc, diag::note_vla_unsupported); 16645 continue; 16646 } else { 16647 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 16648 S.targetDiag(ELoc, diag::note_vla_unsupported); 16649 } 16650 } 16651 // For arrays/array sections only: 16652 // Create pseudo array type for private copy. The size for this array will 16653 // be generated during codegen. 16654 // For array subscripts or single variables Private Ty is the same as Type 16655 // (type of the variable or single array element). 16656 PrivateTy = Context.getVariableArrayType( 16657 Type, 16658 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 16659 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 16660 } else if (!ASE && !OASE && 16661 Context.getAsArrayType(D->getType().getNonReferenceType())) { 16662 PrivateTy = D->getType().getNonReferenceType(); 16663 } 16664 // Private copy. 16665 VarDecl *PrivateVD = 16666 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 16667 D->hasAttrs() ? &D->getAttrs() : nullptr, 16668 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16669 // Add initializer for private variable. 16670 Expr *Init = nullptr; 16671 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 16672 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 16673 if (DeclareReductionRef.isUsable()) { 16674 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 16675 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 16676 if (DRD->getInitializer()) { 16677 S.ActOnUninitializedDecl(PrivateVD); 16678 Init = DRDRef; 16679 RHSVD->setInit(DRDRef); 16680 RHSVD->setInitStyle(VarDecl::CallInit); 16681 } 16682 } else { 16683 switch (BOK) { 16684 case BO_Add: 16685 case BO_Xor: 16686 case BO_Or: 16687 case BO_LOr: 16688 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 16689 if (Type->isScalarType() || Type->isAnyComplexType()) 16690 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 16691 break; 16692 case BO_Mul: 16693 case BO_LAnd: 16694 if (Type->isScalarType() || Type->isAnyComplexType()) { 16695 // '*' and '&&' reduction ops - initializer is '1'. 16696 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 16697 } 16698 break; 16699 case BO_And: { 16700 // '&' reduction op - initializer is '~0'. 16701 QualType OrigType = Type; 16702 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 16703 Type = ComplexTy->getElementType(); 16704 if (Type->isRealFloatingType()) { 16705 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 16706 Context.getFloatTypeSemantics(Type), 16707 Context.getTypeSize(Type)); 16708 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 16709 Type, ELoc); 16710 } else if (Type->isScalarType()) { 16711 uint64_t Size = Context.getTypeSize(Type); 16712 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 16713 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 16714 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 16715 } 16716 if (Init && OrigType->isAnyComplexType()) { 16717 // Init = 0xFFFF + 0xFFFFi; 16718 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 16719 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 16720 } 16721 Type = OrigType; 16722 break; 16723 } 16724 case BO_LT: 16725 case BO_GT: { 16726 // 'min' reduction op - initializer is 'Largest representable number in 16727 // the reduction list item type'. 16728 // 'max' reduction op - initializer is 'Least representable number in 16729 // the reduction list item type'. 16730 if (Type->isIntegerType() || Type->isPointerType()) { 16731 bool IsSigned = Type->hasSignedIntegerRepresentation(); 16732 uint64_t Size = Context.getTypeSize(Type); 16733 QualType IntTy = 16734 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 16735 llvm::APInt InitValue = 16736 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 16737 : llvm::APInt::getMinValue(Size) 16738 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 16739 : llvm::APInt::getMaxValue(Size); 16740 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 16741 if (Type->isPointerType()) { 16742 // Cast to pointer type. 16743 ExprResult CastExpr = S.BuildCStyleCastExpr( 16744 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 16745 if (CastExpr.isInvalid()) 16746 continue; 16747 Init = CastExpr.get(); 16748 } 16749 } else if (Type->isRealFloatingType()) { 16750 llvm::APFloat InitValue = llvm::APFloat::getLargest( 16751 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 16752 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 16753 Type, ELoc); 16754 } 16755 break; 16756 } 16757 case BO_PtrMemD: 16758 case BO_PtrMemI: 16759 case BO_MulAssign: 16760 case BO_Div: 16761 case BO_Rem: 16762 case BO_Sub: 16763 case BO_Shl: 16764 case BO_Shr: 16765 case BO_LE: 16766 case BO_GE: 16767 case BO_EQ: 16768 case BO_NE: 16769 case BO_Cmp: 16770 case BO_AndAssign: 16771 case BO_XorAssign: 16772 case BO_OrAssign: 16773 case BO_Assign: 16774 case BO_AddAssign: 16775 case BO_SubAssign: 16776 case BO_DivAssign: 16777 case BO_RemAssign: 16778 case BO_ShlAssign: 16779 case BO_ShrAssign: 16780 case BO_Comma: 16781 llvm_unreachable("Unexpected reduction operation"); 16782 } 16783 } 16784 if (Init && DeclareReductionRef.isUnset()) { 16785 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 16786 // Store initializer for single element in private copy. Will be used 16787 // during codegen. 16788 PrivateVD->setInit(RHSVD->getInit()); 16789 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 16790 } else if (!Init) { 16791 S.ActOnUninitializedDecl(RHSVD); 16792 // Store initializer for single element in private copy. Will be used 16793 // during codegen. 16794 PrivateVD->setInit(RHSVD->getInit()); 16795 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 16796 } 16797 if (RHSVD->isInvalidDecl()) 16798 continue; 16799 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 16800 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 16801 << Type << ReductionIdRange; 16802 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16803 VarDecl::DeclarationOnly; 16804 S.Diag(D->getLocation(), 16805 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16806 << D; 16807 continue; 16808 } 16809 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 16810 ExprResult ReductionOp; 16811 if (DeclareReductionRef.isUsable()) { 16812 QualType RedTy = DeclareReductionRef.get()->getType(); 16813 QualType PtrRedTy = Context.getPointerType(RedTy); 16814 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 16815 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 16816 if (!BasePath.empty()) { 16817 LHS = S.DefaultLvalueConversion(LHS.get()); 16818 RHS = S.DefaultLvalueConversion(RHS.get()); 16819 LHS = ImplicitCastExpr::Create( 16820 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 16821 LHS.get()->getValueKind(), FPOptionsOverride()); 16822 RHS = ImplicitCastExpr::Create( 16823 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 16824 RHS.get()->getValueKind(), FPOptionsOverride()); 16825 } 16826 FunctionProtoType::ExtProtoInfo EPI; 16827 QualType Params[] = {PtrRedTy, PtrRedTy}; 16828 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 16829 auto *OVE = new (Context) OpaqueValueExpr( 16830 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 16831 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 16832 Expr *Args[] = {LHS.get(), RHS.get()}; 16833 ReductionOp = 16834 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc, 16835 S.CurFPFeatureOverrides()); 16836 } else { 16837 ReductionOp = S.BuildBinOp( 16838 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 16839 if (ReductionOp.isUsable()) { 16840 if (BOK != BO_LT && BOK != BO_GT) { 16841 ReductionOp = 16842 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 16843 BO_Assign, LHSDRE, ReductionOp.get()); 16844 } else { 16845 auto *ConditionalOp = new (Context) 16846 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 16847 Type, VK_LValue, OK_Ordinary); 16848 ReductionOp = 16849 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 16850 BO_Assign, LHSDRE, ConditionalOp); 16851 } 16852 if (ReductionOp.isUsable()) 16853 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 16854 /*DiscardedValue*/ false); 16855 } 16856 if (!ReductionOp.isUsable()) 16857 continue; 16858 } 16859 16860 // Add copy operations for inscan reductions. 16861 // LHS = RHS; 16862 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 16863 if (ClauseKind == OMPC_reduction && 16864 RD.RedModifier == OMPC_REDUCTION_inscan) { 16865 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 16866 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 16867 RHS.get()); 16868 if (!CopyOpRes.isUsable()) 16869 continue; 16870 CopyOpRes = 16871 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 16872 if (!CopyOpRes.isUsable()) 16873 continue; 16874 // For simd directive and simd-based directives in simd mode no need to 16875 // construct temp array, need just a single temp element. 16876 if (Stack->getCurrentDirective() == OMPD_simd || 16877 (S.getLangOpts().OpenMPSimd && 16878 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 16879 VarDecl *TempArrayVD = 16880 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 16881 D->hasAttrs() ? &D->getAttrs() : nullptr); 16882 // Add a constructor to the temp decl. 16883 S.ActOnUninitializedDecl(TempArrayVD); 16884 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 16885 } else { 16886 // Build temp array for prefix sum. 16887 auto *Dim = new (S.Context) 16888 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 16889 QualType ArrayTy = 16890 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 16891 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 16892 VarDecl *TempArrayVD = 16893 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 16894 D->hasAttrs() ? &D->getAttrs() : nullptr); 16895 // Add a constructor to the temp decl. 16896 S.ActOnUninitializedDecl(TempArrayVD); 16897 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 16898 TempArrayElem = 16899 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 16900 auto *Idx = new (S.Context) 16901 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 16902 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 16903 ELoc, Idx, ELoc); 16904 } 16905 } 16906 16907 // OpenMP [2.15.4.6, Restrictions, p.2] 16908 // A list item that appears in an in_reduction clause of a task construct 16909 // must appear in a task_reduction clause of a construct associated with a 16910 // taskgroup region that includes the participating task in its taskgroup 16911 // set. The construct associated with the innermost region that meets this 16912 // condition must specify the same reduction-identifier as the in_reduction 16913 // clause. 16914 if (ClauseKind == OMPC_in_reduction) { 16915 SourceRange ParentSR; 16916 BinaryOperatorKind ParentBOK; 16917 const Expr *ParentReductionOp = nullptr; 16918 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 16919 DSAStackTy::DSAVarData ParentBOKDSA = 16920 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 16921 ParentBOKTD); 16922 DSAStackTy::DSAVarData ParentReductionOpDSA = 16923 Stack->getTopMostTaskgroupReductionData( 16924 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 16925 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 16926 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 16927 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 16928 (DeclareReductionRef.isUsable() && IsParentBOK) || 16929 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 16930 bool EmitError = true; 16931 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 16932 llvm::FoldingSetNodeID RedId, ParentRedId; 16933 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 16934 DeclareReductionRef.get()->Profile(RedId, Context, 16935 /*Canonical=*/true); 16936 EmitError = RedId != ParentRedId; 16937 } 16938 if (EmitError) { 16939 S.Diag(ReductionId.getBeginLoc(), 16940 diag::err_omp_reduction_identifier_mismatch) 16941 << ReductionIdRange << RefExpr->getSourceRange(); 16942 S.Diag(ParentSR.getBegin(), 16943 diag::note_omp_previous_reduction_identifier) 16944 << ParentSR 16945 << (IsParentBOK ? ParentBOKDSA.RefExpr 16946 : ParentReductionOpDSA.RefExpr) 16947 ->getSourceRange(); 16948 continue; 16949 } 16950 } 16951 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 16952 } 16953 16954 DeclRefExpr *Ref = nullptr; 16955 Expr *VarsExpr = RefExpr->IgnoreParens(); 16956 if (!VD && !S.CurContext->isDependentContext()) { 16957 if (ASE || OASE) { 16958 TransformExprToCaptures RebuildToCapture(S, D); 16959 VarsExpr = 16960 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 16961 Ref = RebuildToCapture.getCapturedExpr(); 16962 } else { 16963 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 16964 } 16965 if (!S.isOpenMPCapturedDecl(D)) { 16966 RD.ExprCaptures.emplace_back(Ref->getDecl()); 16967 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 16968 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 16969 if (!RefRes.isUsable()) 16970 continue; 16971 ExprResult PostUpdateRes = 16972 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 16973 RefRes.get()); 16974 if (!PostUpdateRes.isUsable()) 16975 continue; 16976 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 16977 Stack->getCurrentDirective() == OMPD_taskgroup) { 16978 S.Diag(RefExpr->getExprLoc(), 16979 diag::err_omp_reduction_non_addressable_expression) 16980 << RefExpr->getSourceRange(); 16981 continue; 16982 } 16983 RD.ExprPostUpdates.emplace_back( 16984 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 16985 } 16986 } 16987 } 16988 // All reduction items are still marked as reduction (to do not increase 16989 // code base size). 16990 unsigned Modifier = RD.RedModifier; 16991 // Consider task_reductions as reductions with task modifier. Required for 16992 // correct analysis of in_reduction clauses. 16993 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 16994 Modifier = OMPC_REDUCTION_task; 16995 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 16996 ASE || OASE); 16997 if (Modifier == OMPC_REDUCTION_task && 16998 (CurrDir == OMPD_taskgroup || 16999 ((isOpenMPParallelDirective(CurrDir) || 17000 isOpenMPWorksharingDirective(CurrDir)) && 17001 !isOpenMPSimdDirective(CurrDir)))) { 17002 if (DeclareReductionRef.isUsable()) 17003 Stack->addTaskgroupReductionData(D, ReductionIdRange, 17004 DeclareReductionRef.get()); 17005 else 17006 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 17007 } 17008 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 17009 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 17010 TempArrayElem.get()); 17011 } 17012 return RD.Vars.empty(); 17013 } 17014 17015 OMPClause *Sema::ActOnOpenMPReductionClause( 17016 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 17017 SourceLocation StartLoc, SourceLocation LParenLoc, 17018 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 17019 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17020 ArrayRef<Expr *> UnresolvedReductions) { 17021 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 17022 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 17023 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 17024 /*Last=*/OMPC_REDUCTION_unknown) 17025 << getOpenMPClauseName(OMPC_reduction); 17026 return nullptr; 17027 } 17028 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 17029 // A reduction clause with the inscan reduction-modifier may only appear on a 17030 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 17031 // construct, a parallel worksharing-loop construct or a parallel 17032 // worksharing-loop SIMD construct. 17033 if (Modifier == OMPC_REDUCTION_inscan && 17034 (DSAStack->getCurrentDirective() != OMPD_for && 17035 DSAStack->getCurrentDirective() != OMPD_for_simd && 17036 DSAStack->getCurrentDirective() != OMPD_simd && 17037 DSAStack->getCurrentDirective() != OMPD_parallel_for && 17038 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 17039 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 17040 return nullptr; 17041 } 17042 17043 ReductionData RD(VarList.size(), Modifier); 17044 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 17045 StartLoc, LParenLoc, ColonLoc, EndLoc, 17046 ReductionIdScopeSpec, ReductionId, 17047 UnresolvedReductions, RD)) 17048 return nullptr; 17049 17050 return OMPReductionClause::Create( 17051 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 17052 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17053 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 17054 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 17055 buildPreInits(Context, RD.ExprCaptures), 17056 buildPostUpdate(*this, RD.ExprPostUpdates)); 17057 } 17058 17059 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 17060 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17061 SourceLocation ColonLoc, SourceLocation EndLoc, 17062 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17063 ArrayRef<Expr *> UnresolvedReductions) { 17064 ReductionData RD(VarList.size()); 17065 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 17066 StartLoc, LParenLoc, ColonLoc, EndLoc, 17067 ReductionIdScopeSpec, ReductionId, 17068 UnresolvedReductions, RD)) 17069 return nullptr; 17070 17071 return OMPTaskReductionClause::Create( 17072 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17073 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17074 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 17075 buildPreInits(Context, RD.ExprCaptures), 17076 buildPostUpdate(*this, RD.ExprPostUpdates)); 17077 } 17078 17079 OMPClause *Sema::ActOnOpenMPInReductionClause( 17080 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17081 SourceLocation ColonLoc, SourceLocation EndLoc, 17082 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17083 ArrayRef<Expr *> UnresolvedReductions) { 17084 ReductionData RD(VarList.size()); 17085 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 17086 StartLoc, LParenLoc, ColonLoc, EndLoc, 17087 ReductionIdScopeSpec, ReductionId, 17088 UnresolvedReductions, RD)) 17089 return nullptr; 17090 17091 return OMPInReductionClause::Create( 17092 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17093 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17094 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 17095 buildPreInits(Context, RD.ExprCaptures), 17096 buildPostUpdate(*this, RD.ExprPostUpdates)); 17097 } 17098 17099 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 17100 SourceLocation LinLoc) { 17101 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 17102 LinKind == OMPC_LINEAR_unknown) { 17103 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 17104 return true; 17105 } 17106 return false; 17107 } 17108 17109 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 17110 OpenMPLinearClauseKind LinKind, QualType Type, 17111 bool IsDeclareSimd) { 17112 const auto *VD = dyn_cast_or_null<VarDecl>(D); 17113 // A variable must not have an incomplete type or a reference type. 17114 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 17115 return true; 17116 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 17117 !Type->isReferenceType()) { 17118 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 17119 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 17120 return true; 17121 } 17122 Type = Type.getNonReferenceType(); 17123 17124 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 17125 // A variable that is privatized must not have a const-qualified type 17126 // unless it is of class type with a mutable member. This restriction does 17127 // not apply to the firstprivate clause, nor to the linear clause on 17128 // declarative directives (like declare simd). 17129 if (!IsDeclareSimd && 17130 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 17131 return true; 17132 17133 // A list item must be of integral or pointer type. 17134 Type = Type.getUnqualifiedType().getCanonicalType(); 17135 const auto *Ty = Type.getTypePtrOrNull(); 17136 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 17137 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 17138 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 17139 if (D) { 17140 bool IsDecl = 17141 !VD || 17142 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17143 Diag(D->getLocation(), 17144 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17145 << D; 17146 } 17147 return true; 17148 } 17149 return false; 17150 } 17151 17152 OMPClause *Sema::ActOnOpenMPLinearClause( 17153 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 17154 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 17155 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17156 SmallVector<Expr *, 8> Vars; 17157 SmallVector<Expr *, 8> Privates; 17158 SmallVector<Expr *, 8> Inits; 17159 SmallVector<Decl *, 4> ExprCaptures; 17160 SmallVector<Expr *, 4> ExprPostUpdates; 17161 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 17162 LinKind = OMPC_LINEAR_val; 17163 for (Expr *RefExpr : VarList) { 17164 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17165 SourceLocation ELoc; 17166 SourceRange ERange; 17167 Expr *SimpleRefExpr = RefExpr; 17168 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17169 if (Res.second) { 17170 // It will be analyzed later. 17171 Vars.push_back(RefExpr); 17172 Privates.push_back(nullptr); 17173 Inits.push_back(nullptr); 17174 } 17175 ValueDecl *D = Res.first; 17176 if (!D) 17177 continue; 17178 17179 QualType Type = D->getType(); 17180 auto *VD = dyn_cast<VarDecl>(D); 17181 17182 // OpenMP [2.14.3.7, linear clause] 17183 // A list-item cannot appear in more than one linear clause. 17184 // A list-item that appears in a linear clause cannot appear in any 17185 // other data-sharing attribute clause. 17186 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 17187 if (DVar.RefExpr) { 17188 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 17189 << getOpenMPClauseName(OMPC_linear); 17190 reportOriginalDsa(*this, DSAStack, D, DVar); 17191 continue; 17192 } 17193 17194 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 17195 continue; 17196 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17197 17198 // Build private copy of original var. 17199 VarDecl *Private = 17200 buildVarDecl(*this, ELoc, Type, D->getName(), 17201 D->hasAttrs() ? &D->getAttrs() : nullptr, 17202 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17203 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 17204 // Build var to save initial value. 17205 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 17206 Expr *InitExpr; 17207 DeclRefExpr *Ref = nullptr; 17208 if (!VD && !CurContext->isDependentContext()) { 17209 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 17210 if (!isOpenMPCapturedDecl(D)) { 17211 ExprCaptures.push_back(Ref->getDecl()); 17212 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17213 ExprResult RefRes = DefaultLvalueConversion(Ref); 17214 if (!RefRes.isUsable()) 17215 continue; 17216 ExprResult PostUpdateRes = 17217 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 17218 SimpleRefExpr, RefRes.get()); 17219 if (!PostUpdateRes.isUsable()) 17220 continue; 17221 ExprPostUpdates.push_back( 17222 IgnoredValueConversions(PostUpdateRes.get()).get()); 17223 } 17224 } 17225 } 17226 if (LinKind == OMPC_LINEAR_uval) 17227 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 17228 else 17229 InitExpr = VD ? SimpleRefExpr : Ref; 17230 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 17231 /*DirectInit=*/false); 17232 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 17233 17234 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 17235 Vars.push_back((VD || CurContext->isDependentContext()) 17236 ? RefExpr->IgnoreParens() 17237 : Ref); 17238 Privates.push_back(PrivateRef); 17239 Inits.push_back(InitRef); 17240 } 17241 17242 if (Vars.empty()) 17243 return nullptr; 17244 17245 Expr *StepExpr = Step; 17246 Expr *CalcStepExpr = nullptr; 17247 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 17248 !Step->isInstantiationDependent() && 17249 !Step->containsUnexpandedParameterPack()) { 17250 SourceLocation StepLoc = Step->getBeginLoc(); 17251 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 17252 if (Val.isInvalid()) 17253 return nullptr; 17254 StepExpr = Val.get(); 17255 17256 // Build var to save the step value. 17257 VarDecl *SaveVar = 17258 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 17259 ExprResult SaveRef = 17260 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 17261 ExprResult CalcStep = 17262 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 17263 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 17264 17265 // Warn about zero linear step (it would be probably better specified as 17266 // making corresponding variables 'const'). 17267 if (Optional<llvm::APSInt> Result = 17268 StepExpr->getIntegerConstantExpr(Context)) { 17269 if (!Result->isNegative() && !Result->isStrictlyPositive()) 17270 Diag(StepLoc, diag::warn_omp_linear_step_zero) 17271 << Vars[0] << (Vars.size() > 1); 17272 } else if (CalcStep.isUsable()) { 17273 // Calculate the step beforehand instead of doing this on each iteration. 17274 // (This is not used if the number of iterations may be kfold-ed). 17275 CalcStepExpr = CalcStep.get(); 17276 } 17277 } 17278 17279 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 17280 ColonLoc, EndLoc, Vars, Privates, Inits, 17281 StepExpr, CalcStepExpr, 17282 buildPreInits(Context, ExprCaptures), 17283 buildPostUpdate(*this, ExprPostUpdates)); 17284 } 17285 17286 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 17287 Expr *NumIterations, Sema &SemaRef, 17288 Scope *S, DSAStackTy *Stack) { 17289 // Walk the vars and build update/final expressions for the CodeGen. 17290 SmallVector<Expr *, 8> Updates; 17291 SmallVector<Expr *, 8> Finals; 17292 SmallVector<Expr *, 8> UsedExprs; 17293 Expr *Step = Clause.getStep(); 17294 Expr *CalcStep = Clause.getCalcStep(); 17295 // OpenMP [2.14.3.7, linear clause] 17296 // If linear-step is not specified it is assumed to be 1. 17297 if (!Step) 17298 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 17299 else if (CalcStep) 17300 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 17301 bool HasErrors = false; 17302 auto CurInit = Clause.inits().begin(); 17303 auto CurPrivate = Clause.privates().begin(); 17304 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 17305 for (Expr *RefExpr : Clause.varlists()) { 17306 SourceLocation ELoc; 17307 SourceRange ERange; 17308 Expr *SimpleRefExpr = RefExpr; 17309 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 17310 ValueDecl *D = Res.first; 17311 if (Res.second || !D) { 17312 Updates.push_back(nullptr); 17313 Finals.push_back(nullptr); 17314 HasErrors = true; 17315 continue; 17316 } 17317 auto &&Info = Stack->isLoopControlVariable(D); 17318 // OpenMP [2.15.11, distribute simd Construct] 17319 // A list item may not appear in a linear clause, unless it is the loop 17320 // iteration variable. 17321 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 17322 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 17323 SemaRef.Diag(ELoc, 17324 diag::err_omp_linear_distribute_var_non_loop_iteration); 17325 Updates.push_back(nullptr); 17326 Finals.push_back(nullptr); 17327 HasErrors = true; 17328 continue; 17329 } 17330 Expr *InitExpr = *CurInit; 17331 17332 // Build privatized reference to the current linear var. 17333 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 17334 Expr *CapturedRef; 17335 if (LinKind == OMPC_LINEAR_uval) 17336 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 17337 else 17338 CapturedRef = 17339 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 17340 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 17341 /*RefersToCapture=*/true); 17342 17343 // Build update: Var = InitExpr + IV * Step 17344 ExprResult Update; 17345 if (!Info.first) 17346 Update = buildCounterUpdate( 17347 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 17348 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 17349 else 17350 Update = *CurPrivate; 17351 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 17352 /*DiscardedValue*/ false); 17353 17354 // Build final: Var = InitExpr + NumIterations * Step 17355 ExprResult Final; 17356 if (!Info.first) 17357 Final = 17358 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 17359 InitExpr, NumIterations, Step, /*Subtract=*/false, 17360 /*IsNonRectangularLB=*/false); 17361 else 17362 Final = *CurPrivate; 17363 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 17364 /*DiscardedValue*/ false); 17365 17366 if (!Update.isUsable() || !Final.isUsable()) { 17367 Updates.push_back(nullptr); 17368 Finals.push_back(nullptr); 17369 UsedExprs.push_back(nullptr); 17370 HasErrors = true; 17371 } else { 17372 Updates.push_back(Update.get()); 17373 Finals.push_back(Final.get()); 17374 if (!Info.first) 17375 UsedExprs.push_back(SimpleRefExpr); 17376 } 17377 ++CurInit; 17378 ++CurPrivate; 17379 } 17380 if (Expr *S = Clause.getStep()) 17381 UsedExprs.push_back(S); 17382 // Fill the remaining part with the nullptr. 17383 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 17384 Clause.setUpdates(Updates); 17385 Clause.setFinals(Finals); 17386 Clause.setUsedExprs(UsedExprs); 17387 return HasErrors; 17388 } 17389 17390 OMPClause *Sema::ActOnOpenMPAlignedClause( 17391 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 17392 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17393 SmallVector<Expr *, 8> Vars; 17394 for (Expr *RefExpr : VarList) { 17395 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17396 SourceLocation ELoc; 17397 SourceRange ERange; 17398 Expr *SimpleRefExpr = RefExpr; 17399 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17400 if (Res.second) { 17401 // It will be analyzed later. 17402 Vars.push_back(RefExpr); 17403 } 17404 ValueDecl *D = Res.first; 17405 if (!D) 17406 continue; 17407 17408 QualType QType = D->getType(); 17409 auto *VD = dyn_cast<VarDecl>(D); 17410 17411 // OpenMP [2.8.1, simd construct, Restrictions] 17412 // The type of list items appearing in the aligned clause must be 17413 // array, pointer, reference to array, or reference to pointer. 17414 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17415 const Type *Ty = QType.getTypePtrOrNull(); 17416 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 17417 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 17418 << QType << getLangOpts().CPlusPlus << ERange; 17419 bool IsDecl = 17420 !VD || 17421 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17422 Diag(D->getLocation(), 17423 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17424 << D; 17425 continue; 17426 } 17427 17428 // OpenMP [2.8.1, simd construct, Restrictions] 17429 // A list-item cannot appear in more than one aligned clause. 17430 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 17431 Diag(ELoc, diag::err_omp_used_in_clause_twice) 17432 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 17433 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 17434 << getOpenMPClauseName(OMPC_aligned); 17435 continue; 17436 } 17437 17438 DeclRefExpr *Ref = nullptr; 17439 if (!VD && isOpenMPCapturedDecl(D)) 17440 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 17441 Vars.push_back(DefaultFunctionArrayConversion( 17442 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 17443 .get()); 17444 } 17445 17446 // OpenMP [2.8.1, simd construct, Description] 17447 // The parameter of the aligned clause, alignment, must be a constant 17448 // positive integer expression. 17449 // If no optional parameter is specified, implementation-defined default 17450 // alignments for SIMD instructions on the target platforms are assumed. 17451 if (Alignment != nullptr) { 17452 ExprResult AlignResult = 17453 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 17454 if (AlignResult.isInvalid()) 17455 return nullptr; 17456 Alignment = AlignResult.get(); 17457 } 17458 if (Vars.empty()) 17459 return nullptr; 17460 17461 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 17462 EndLoc, Vars, Alignment); 17463 } 17464 17465 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 17466 SourceLocation StartLoc, 17467 SourceLocation LParenLoc, 17468 SourceLocation EndLoc) { 17469 SmallVector<Expr *, 8> Vars; 17470 SmallVector<Expr *, 8> SrcExprs; 17471 SmallVector<Expr *, 8> DstExprs; 17472 SmallVector<Expr *, 8> AssignmentOps; 17473 for (Expr *RefExpr : VarList) { 17474 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 17475 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 17476 // It will be analyzed later. 17477 Vars.push_back(RefExpr); 17478 SrcExprs.push_back(nullptr); 17479 DstExprs.push_back(nullptr); 17480 AssignmentOps.push_back(nullptr); 17481 continue; 17482 } 17483 17484 SourceLocation ELoc = RefExpr->getExprLoc(); 17485 // OpenMP [2.1, C/C++] 17486 // A list item is a variable name. 17487 // OpenMP [2.14.4.1, Restrictions, p.1] 17488 // A list item that appears in a copyin clause must be threadprivate. 17489 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 17490 if (!DE || !isa<VarDecl>(DE->getDecl())) { 17491 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 17492 << 0 << RefExpr->getSourceRange(); 17493 continue; 17494 } 17495 17496 Decl *D = DE->getDecl(); 17497 auto *VD = cast<VarDecl>(D); 17498 17499 QualType Type = VD->getType(); 17500 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 17501 // It will be analyzed later. 17502 Vars.push_back(DE); 17503 SrcExprs.push_back(nullptr); 17504 DstExprs.push_back(nullptr); 17505 AssignmentOps.push_back(nullptr); 17506 continue; 17507 } 17508 17509 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 17510 // A list item that appears in a copyin clause must be threadprivate. 17511 if (!DSAStack->isThreadPrivate(VD)) { 17512 Diag(ELoc, diag::err_omp_required_access) 17513 << getOpenMPClauseName(OMPC_copyin) 17514 << getOpenMPDirectiveName(OMPD_threadprivate); 17515 continue; 17516 } 17517 17518 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 17519 // A variable of class type (or array thereof) that appears in a 17520 // copyin clause requires an accessible, unambiguous copy assignment 17521 // operator for the class type. 17522 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 17523 VarDecl *SrcVD = 17524 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 17525 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 17526 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 17527 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 17528 VarDecl *DstVD = 17529 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 17530 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 17531 DeclRefExpr *PseudoDstExpr = 17532 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 17533 // For arrays generate assignment operation for single element and replace 17534 // it by the original array element in CodeGen. 17535 ExprResult AssignmentOp = 17536 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 17537 PseudoSrcExpr); 17538 if (AssignmentOp.isInvalid()) 17539 continue; 17540 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 17541 /*DiscardedValue*/ false); 17542 if (AssignmentOp.isInvalid()) 17543 continue; 17544 17545 DSAStack->addDSA(VD, DE, OMPC_copyin); 17546 Vars.push_back(DE); 17547 SrcExprs.push_back(PseudoSrcExpr); 17548 DstExprs.push_back(PseudoDstExpr); 17549 AssignmentOps.push_back(AssignmentOp.get()); 17550 } 17551 17552 if (Vars.empty()) 17553 return nullptr; 17554 17555 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 17556 SrcExprs, DstExprs, AssignmentOps); 17557 } 17558 17559 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 17560 SourceLocation StartLoc, 17561 SourceLocation LParenLoc, 17562 SourceLocation EndLoc) { 17563 SmallVector<Expr *, 8> Vars; 17564 SmallVector<Expr *, 8> SrcExprs; 17565 SmallVector<Expr *, 8> DstExprs; 17566 SmallVector<Expr *, 8> AssignmentOps; 17567 for (Expr *RefExpr : VarList) { 17568 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17569 SourceLocation ELoc; 17570 SourceRange ERange; 17571 Expr *SimpleRefExpr = RefExpr; 17572 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17573 if (Res.second) { 17574 // It will be analyzed later. 17575 Vars.push_back(RefExpr); 17576 SrcExprs.push_back(nullptr); 17577 DstExprs.push_back(nullptr); 17578 AssignmentOps.push_back(nullptr); 17579 } 17580 ValueDecl *D = Res.first; 17581 if (!D) 17582 continue; 17583 17584 QualType Type = D->getType(); 17585 auto *VD = dyn_cast<VarDecl>(D); 17586 17587 // OpenMP [2.14.4.2, Restrictions, p.2] 17588 // A list item that appears in a copyprivate clause may not appear in a 17589 // private or firstprivate clause on the single construct. 17590 if (!VD || !DSAStack->isThreadPrivate(VD)) { 17591 DSAStackTy::DSAVarData DVar = 17592 DSAStack->getTopDSA(D, /*FromParent=*/false); 17593 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 17594 DVar.RefExpr) { 17595 Diag(ELoc, diag::err_omp_wrong_dsa) 17596 << getOpenMPClauseName(DVar.CKind) 17597 << getOpenMPClauseName(OMPC_copyprivate); 17598 reportOriginalDsa(*this, DSAStack, D, DVar); 17599 continue; 17600 } 17601 17602 // OpenMP [2.11.4.2, Restrictions, p.1] 17603 // All list items that appear in a copyprivate clause must be either 17604 // threadprivate or private in the enclosing context. 17605 if (DVar.CKind == OMPC_unknown) { 17606 DVar = DSAStack->getImplicitDSA(D, false); 17607 if (DVar.CKind == OMPC_shared) { 17608 Diag(ELoc, diag::err_omp_required_access) 17609 << getOpenMPClauseName(OMPC_copyprivate) 17610 << "threadprivate or private in the enclosing context"; 17611 reportOriginalDsa(*this, DSAStack, D, DVar); 17612 continue; 17613 } 17614 } 17615 } 17616 17617 // Variably modified types are not supported. 17618 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 17619 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 17620 << getOpenMPClauseName(OMPC_copyprivate) << Type 17621 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 17622 bool IsDecl = 17623 !VD || 17624 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17625 Diag(D->getLocation(), 17626 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17627 << D; 17628 continue; 17629 } 17630 17631 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 17632 // A variable of class type (or array thereof) that appears in a 17633 // copyin clause requires an accessible, unambiguous copy assignment 17634 // operator for the class type. 17635 Type = Context.getBaseElementType(Type.getNonReferenceType()) 17636 .getUnqualifiedType(); 17637 VarDecl *SrcVD = 17638 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 17639 D->hasAttrs() ? &D->getAttrs() : nullptr); 17640 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 17641 VarDecl *DstVD = 17642 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 17643 D->hasAttrs() ? &D->getAttrs() : nullptr); 17644 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 17645 ExprResult AssignmentOp = BuildBinOp( 17646 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 17647 if (AssignmentOp.isInvalid()) 17648 continue; 17649 AssignmentOp = 17650 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 17651 if (AssignmentOp.isInvalid()) 17652 continue; 17653 17654 // No need to mark vars as copyprivate, they are already threadprivate or 17655 // implicitly private. 17656 assert(VD || isOpenMPCapturedDecl(D)); 17657 Vars.push_back( 17658 VD ? RefExpr->IgnoreParens() 17659 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 17660 SrcExprs.push_back(PseudoSrcExpr); 17661 DstExprs.push_back(PseudoDstExpr); 17662 AssignmentOps.push_back(AssignmentOp.get()); 17663 } 17664 17665 if (Vars.empty()) 17666 return nullptr; 17667 17668 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 17669 Vars, SrcExprs, DstExprs, AssignmentOps); 17670 } 17671 17672 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 17673 SourceLocation StartLoc, 17674 SourceLocation LParenLoc, 17675 SourceLocation EndLoc) { 17676 if (VarList.empty()) 17677 return nullptr; 17678 17679 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 17680 } 17681 17682 /// Tries to find omp_depend_t. type. 17683 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 17684 bool Diagnose = true) { 17685 QualType OMPDependT = Stack->getOMPDependT(); 17686 if (!OMPDependT.isNull()) 17687 return true; 17688 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 17689 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 17690 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 17691 if (Diagnose) 17692 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 17693 return false; 17694 } 17695 Stack->setOMPDependT(PT.get()); 17696 return true; 17697 } 17698 17699 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 17700 SourceLocation LParenLoc, 17701 SourceLocation EndLoc) { 17702 if (!Depobj) 17703 return nullptr; 17704 17705 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 17706 17707 // OpenMP 5.0, 2.17.10.1 depobj Construct 17708 // depobj is an lvalue expression of type omp_depend_t. 17709 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 17710 !Depobj->isInstantiationDependent() && 17711 !Depobj->containsUnexpandedParameterPack() && 17712 (OMPDependTFound && 17713 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 17714 /*CompareUnqualified=*/true))) { 17715 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 17716 << 0 << Depobj->getType() << Depobj->getSourceRange(); 17717 } 17718 17719 if (!Depobj->isLValue()) { 17720 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 17721 << 1 << Depobj->getSourceRange(); 17722 } 17723 17724 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 17725 } 17726 17727 OMPClause * 17728 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 17729 SourceLocation DepLoc, SourceLocation ColonLoc, 17730 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 17731 SourceLocation LParenLoc, SourceLocation EndLoc) { 17732 if (DSAStack->getCurrentDirective() == OMPD_ordered && 17733 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 17734 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 17735 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 17736 return nullptr; 17737 } 17738 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 17739 DSAStack->getCurrentDirective() == OMPD_depobj) && 17740 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 17741 DepKind == OMPC_DEPEND_sink || 17742 ((LangOpts.OpenMP < 50 || 17743 DSAStack->getCurrentDirective() == OMPD_depobj) && 17744 DepKind == OMPC_DEPEND_depobj))) { 17745 SmallVector<unsigned, 3> Except; 17746 Except.push_back(OMPC_DEPEND_source); 17747 Except.push_back(OMPC_DEPEND_sink); 17748 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 17749 Except.push_back(OMPC_DEPEND_depobj); 17750 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 17751 ? "depend modifier(iterator) or " 17752 : ""; 17753 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 17754 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 17755 /*Last=*/OMPC_DEPEND_unknown, 17756 Except) 17757 << getOpenMPClauseName(OMPC_depend); 17758 return nullptr; 17759 } 17760 if (DepModifier && 17761 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 17762 Diag(DepModifier->getExprLoc(), 17763 diag::err_omp_depend_sink_source_with_modifier); 17764 return nullptr; 17765 } 17766 if (DepModifier && 17767 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 17768 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 17769 17770 SmallVector<Expr *, 8> Vars; 17771 DSAStackTy::OperatorOffsetTy OpsOffs; 17772 llvm::APSInt DepCounter(/*BitWidth=*/32); 17773 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 17774 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 17775 if (const Expr *OrderedCountExpr = 17776 DSAStack->getParentOrderedRegionParam().first) { 17777 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 17778 TotalDepCount.setIsUnsigned(/*Val=*/true); 17779 } 17780 } 17781 for (Expr *RefExpr : VarList) { 17782 assert(RefExpr && "NULL expr in OpenMP shared clause."); 17783 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 17784 // It will be analyzed later. 17785 Vars.push_back(RefExpr); 17786 continue; 17787 } 17788 17789 SourceLocation ELoc = RefExpr->getExprLoc(); 17790 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 17791 if (DepKind == OMPC_DEPEND_sink) { 17792 if (DSAStack->getParentOrderedRegionParam().first && 17793 DepCounter >= TotalDepCount) { 17794 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 17795 continue; 17796 } 17797 ++DepCounter; 17798 // OpenMP [2.13.9, Summary] 17799 // depend(dependence-type : vec), where dependence-type is: 17800 // 'sink' and where vec is the iteration vector, which has the form: 17801 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 17802 // where n is the value specified by the ordered clause in the loop 17803 // directive, xi denotes the loop iteration variable of the i-th nested 17804 // loop associated with the loop directive, and di is a constant 17805 // non-negative integer. 17806 if (CurContext->isDependentContext()) { 17807 // It will be analyzed later. 17808 Vars.push_back(RefExpr); 17809 continue; 17810 } 17811 SimpleExpr = SimpleExpr->IgnoreImplicit(); 17812 OverloadedOperatorKind OOK = OO_None; 17813 SourceLocation OOLoc; 17814 Expr *LHS = SimpleExpr; 17815 Expr *RHS = nullptr; 17816 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 17817 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 17818 OOLoc = BO->getOperatorLoc(); 17819 LHS = BO->getLHS()->IgnoreParenImpCasts(); 17820 RHS = BO->getRHS()->IgnoreParenImpCasts(); 17821 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 17822 OOK = OCE->getOperator(); 17823 OOLoc = OCE->getOperatorLoc(); 17824 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 17825 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 17826 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 17827 OOK = MCE->getMethodDecl() 17828 ->getNameInfo() 17829 .getName() 17830 .getCXXOverloadedOperator(); 17831 OOLoc = MCE->getCallee()->getExprLoc(); 17832 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 17833 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 17834 } 17835 SourceLocation ELoc; 17836 SourceRange ERange; 17837 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 17838 if (Res.second) { 17839 // It will be analyzed later. 17840 Vars.push_back(RefExpr); 17841 } 17842 ValueDecl *D = Res.first; 17843 if (!D) 17844 continue; 17845 17846 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 17847 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 17848 continue; 17849 } 17850 if (RHS) { 17851 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 17852 RHS, OMPC_depend, /*StrictlyPositive=*/false); 17853 if (RHSRes.isInvalid()) 17854 continue; 17855 } 17856 if (!CurContext->isDependentContext() && 17857 DSAStack->getParentOrderedRegionParam().first && 17858 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 17859 const ValueDecl *VD = 17860 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 17861 if (VD) 17862 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 17863 << 1 << VD; 17864 else 17865 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 17866 continue; 17867 } 17868 OpsOffs.emplace_back(RHS, OOK); 17869 } else { 17870 bool OMPDependTFound = LangOpts.OpenMP >= 50; 17871 if (OMPDependTFound) 17872 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 17873 DepKind == OMPC_DEPEND_depobj); 17874 if (DepKind == OMPC_DEPEND_depobj) { 17875 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 17876 // List items used in depend clauses with the depobj dependence type 17877 // must be expressions of the omp_depend_t type. 17878 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 17879 !RefExpr->isInstantiationDependent() && 17880 !RefExpr->containsUnexpandedParameterPack() && 17881 (OMPDependTFound && 17882 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 17883 RefExpr->getType()))) { 17884 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 17885 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 17886 continue; 17887 } 17888 if (!RefExpr->isLValue()) { 17889 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 17890 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 17891 continue; 17892 } 17893 } else { 17894 // OpenMP 5.0 [2.17.11, Restrictions] 17895 // List items used in depend clauses cannot be zero-length array 17896 // sections. 17897 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 17898 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 17899 if (OASE) { 17900 QualType BaseType = 17901 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17902 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17903 ExprTy = ATy->getElementType(); 17904 else 17905 ExprTy = BaseType->getPointeeType(); 17906 ExprTy = ExprTy.getNonReferenceType(); 17907 const Expr *Length = OASE->getLength(); 17908 Expr::EvalResult Result; 17909 if (Length && !Length->isValueDependent() && 17910 Length->EvaluateAsInt(Result, Context) && 17911 Result.Val.getInt().isNullValue()) { 17912 Diag(ELoc, 17913 diag::err_omp_depend_zero_length_array_section_not_allowed) 17914 << SimpleExpr->getSourceRange(); 17915 continue; 17916 } 17917 } 17918 17919 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 17920 // List items used in depend clauses with the in, out, inout or 17921 // mutexinoutset dependence types cannot be expressions of the 17922 // omp_depend_t type. 17923 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 17924 !RefExpr->isInstantiationDependent() && 17925 !RefExpr->containsUnexpandedParameterPack() && 17926 (OMPDependTFound && 17927 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 17928 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 17929 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 17930 << RefExpr->getSourceRange(); 17931 continue; 17932 } 17933 17934 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 17935 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 17936 (ASE && !ASE->getBase()->isTypeDependent() && 17937 !ASE->getBase() 17938 ->getType() 17939 .getNonReferenceType() 17940 ->isPointerType() && 17941 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 17942 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 17943 << (LangOpts.OpenMP >= 50 ? 1 : 0) 17944 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 17945 continue; 17946 } 17947 17948 ExprResult Res; 17949 { 17950 Sema::TentativeAnalysisScope Trap(*this); 17951 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 17952 RefExpr->IgnoreParenImpCasts()); 17953 } 17954 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 17955 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 17956 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 17957 << (LangOpts.OpenMP >= 50 ? 1 : 0) 17958 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 17959 continue; 17960 } 17961 } 17962 } 17963 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 17964 } 17965 17966 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 17967 TotalDepCount > VarList.size() && 17968 DSAStack->getParentOrderedRegionParam().first && 17969 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 17970 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 17971 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 17972 } 17973 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 17974 Vars.empty()) 17975 return nullptr; 17976 17977 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 17978 DepModifier, DepKind, DepLoc, ColonLoc, 17979 Vars, TotalDepCount.getZExtValue()); 17980 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 17981 DSAStack->isParentOrderedRegion()) 17982 DSAStack->addDoacrossDependClause(C, OpsOffs); 17983 return C; 17984 } 17985 17986 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 17987 Expr *Device, SourceLocation StartLoc, 17988 SourceLocation LParenLoc, 17989 SourceLocation ModifierLoc, 17990 SourceLocation EndLoc) { 17991 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 17992 "Unexpected device modifier in OpenMP < 50."); 17993 17994 bool ErrorFound = false; 17995 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 17996 std::string Values = 17997 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 17998 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 17999 << Values << getOpenMPClauseName(OMPC_device); 18000 ErrorFound = true; 18001 } 18002 18003 Expr *ValExpr = Device; 18004 Stmt *HelperValStmt = nullptr; 18005 18006 // OpenMP [2.9.1, Restrictions] 18007 // The device expression must evaluate to a non-negative integer value. 18008 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 18009 /*StrictlyPositive=*/false) || 18010 ErrorFound; 18011 if (ErrorFound) 18012 return nullptr; 18013 18014 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18015 OpenMPDirectiveKind CaptureRegion = 18016 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 18017 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18018 ValExpr = MakeFullExpr(ValExpr).get(); 18019 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18020 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18021 HelperValStmt = buildPreInits(Context, Captures); 18022 } 18023 18024 return new (Context) 18025 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 18026 LParenLoc, ModifierLoc, EndLoc); 18027 } 18028 18029 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 18030 DSAStackTy *Stack, QualType QTy, 18031 bool FullCheck = true) { 18032 NamedDecl *ND; 18033 if (QTy->isIncompleteType(&ND)) { 18034 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 18035 return false; 18036 } 18037 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 18038 !QTy.isTriviallyCopyableType(SemaRef.Context)) 18039 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 18040 return true; 18041 } 18042 18043 /// Return true if it can be proven that the provided array expression 18044 /// (array section or array subscript) does NOT specify the whole size of the 18045 /// array whose base type is \a BaseQTy. 18046 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 18047 const Expr *E, 18048 QualType BaseQTy) { 18049 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18050 18051 // If this is an array subscript, it refers to the whole size if the size of 18052 // the dimension is constant and equals 1. Also, an array section assumes the 18053 // format of an array subscript if no colon is used. 18054 if (isa<ArraySubscriptExpr>(E) || 18055 (OASE && OASE->getColonLocFirst().isInvalid())) { 18056 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18057 return ATy->getSize().getSExtValue() != 1; 18058 // Size can't be evaluated statically. 18059 return false; 18060 } 18061 18062 assert(OASE && "Expecting array section if not an array subscript."); 18063 const Expr *LowerBound = OASE->getLowerBound(); 18064 const Expr *Length = OASE->getLength(); 18065 18066 // If there is a lower bound that does not evaluates to zero, we are not 18067 // covering the whole dimension. 18068 if (LowerBound) { 18069 Expr::EvalResult Result; 18070 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 18071 return false; // Can't get the integer value as a constant. 18072 18073 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 18074 if (ConstLowerBound.getSExtValue()) 18075 return true; 18076 } 18077 18078 // If we don't have a length we covering the whole dimension. 18079 if (!Length) 18080 return false; 18081 18082 // If the base is a pointer, we don't have a way to get the size of the 18083 // pointee. 18084 if (BaseQTy->isPointerType()) 18085 return false; 18086 18087 // We can only check if the length is the same as the size of the dimension 18088 // if we have a constant array. 18089 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 18090 if (!CATy) 18091 return false; 18092 18093 Expr::EvalResult Result; 18094 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18095 return false; // Can't get the integer value as a constant. 18096 18097 llvm::APSInt ConstLength = Result.Val.getInt(); 18098 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 18099 } 18100 18101 // Return true if it can be proven that the provided array expression (array 18102 // section or array subscript) does NOT specify a single element of the array 18103 // whose base type is \a BaseQTy. 18104 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 18105 const Expr *E, 18106 QualType BaseQTy) { 18107 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18108 18109 // An array subscript always refer to a single element. Also, an array section 18110 // assumes the format of an array subscript if no colon is used. 18111 if (isa<ArraySubscriptExpr>(E) || 18112 (OASE && OASE->getColonLocFirst().isInvalid())) 18113 return false; 18114 18115 assert(OASE && "Expecting array section if not an array subscript."); 18116 const Expr *Length = OASE->getLength(); 18117 18118 // If we don't have a length we have to check if the array has unitary size 18119 // for this dimension. Also, we should always expect a length if the base type 18120 // is pointer. 18121 if (!Length) { 18122 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18123 return ATy->getSize().getSExtValue() != 1; 18124 // We cannot assume anything. 18125 return false; 18126 } 18127 18128 // Check if the length evaluates to 1. 18129 Expr::EvalResult Result; 18130 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18131 return false; // Can't get the integer value as a constant. 18132 18133 llvm::APSInt ConstLength = Result.Val.getInt(); 18134 return ConstLength.getSExtValue() != 1; 18135 } 18136 18137 // The base of elements of list in a map clause have to be either: 18138 // - a reference to variable or field. 18139 // - a member expression. 18140 // - an array expression. 18141 // 18142 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 18143 // reference to 'r'. 18144 // 18145 // If we have: 18146 // 18147 // struct SS { 18148 // Bla S; 18149 // foo() { 18150 // #pragma omp target map (S.Arr[:12]); 18151 // } 18152 // } 18153 // 18154 // We want to retrieve the member expression 'this->S'; 18155 18156 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 18157 // If a list item is an array section, it must specify contiguous storage. 18158 // 18159 // For this restriction it is sufficient that we make sure only references 18160 // to variables or fields and array expressions, and that no array sections 18161 // exist except in the rightmost expression (unless they cover the whole 18162 // dimension of the array). E.g. these would be invalid: 18163 // 18164 // r.ArrS[3:5].Arr[6:7] 18165 // 18166 // r.ArrS[3:5].x 18167 // 18168 // but these would be valid: 18169 // r.ArrS[3].Arr[6:7] 18170 // 18171 // r.ArrS[3].x 18172 namespace { 18173 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 18174 Sema &SemaRef; 18175 OpenMPClauseKind CKind = OMPC_unknown; 18176 OpenMPDirectiveKind DKind = OMPD_unknown; 18177 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 18178 bool IsNonContiguous = false; 18179 bool NoDiagnose = false; 18180 const Expr *RelevantExpr = nullptr; 18181 bool AllowUnitySizeArraySection = true; 18182 bool AllowWholeSizeArraySection = true; 18183 bool AllowAnotherPtr = true; 18184 SourceLocation ELoc; 18185 SourceRange ERange; 18186 18187 void emitErrorMsg() { 18188 // If nothing else worked, this is not a valid map clause expression. 18189 if (SemaRef.getLangOpts().OpenMP < 50) { 18190 SemaRef.Diag(ELoc, 18191 diag::err_omp_expected_named_var_member_or_array_expression) 18192 << ERange; 18193 } else { 18194 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18195 << getOpenMPClauseName(CKind) << ERange; 18196 } 18197 } 18198 18199 public: 18200 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 18201 if (!isa<VarDecl>(DRE->getDecl())) { 18202 emitErrorMsg(); 18203 return false; 18204 } 18205 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18206 RelevantExpr = DRE; 18207 // Record the component. 18208 Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous); 18209 return true; 18210 } 18211 18212 bool VisitMemberExpr(MemberExpr *ME) { 18213 Expr *E = ME; 18214 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 18215 18216 if (isa<CXXThisExpr>(BaseE)) { 18217 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18218 // We found a base expression: this->Val. 18219 RelevantExpr = ME; 18220 } else { 18221 E = BaseE; 18222 } 18223 18224 if (!isa<FieldDecl>(ME->getMemberDecl())) { 18225 if (!NoDiagnose) { 18226 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 18227 << ME->getSourceRange(); 18228 return false; 18229 } 18230 if (RelevantExpr) 18231 return false; 18232 return Visit(E); 18233 } 18234 18235 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 18236 18237 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 18238 // A bit-field cannot appear in a map clause. 18239 // 18240 if (FD->isBitField()) { 18241 if (!NoDiagnose) { 18242 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 18243 << ME->getSourceRange() << getOpenMPClauseName(CKind); 18244 return false; 18245 } 18246 if (RelevantExpr) 18247 return false; 18248 return Visit(E); 18249 } 18250 18251 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18252 // If the type of a list item is a reference to a type T then the type 18253 // will be considered to be T for all purposes of this clause. 18254 QualType CurType = BaseE->getType().getNonReferenceType(); 18255 18256 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 18257 // A list item cannot be a variable that is a member of a structure with 18258 // a union type. 18259 // 18260 if (CurType->isUnionType()) { 18261 if (!NoDiagnose) { 18262 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 18263 << ME->getSourceRange(); 18264 return false; 18265 } 18266 return RelevantExpr || Visit(E); 18267 } 18268 18269 // If we got a member expression, we should not expect any array section 18270 // before that: 18271 // 18272 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 18273 // If a list item is an element of a structure, only the rightmost symbol 18274 // of the variable reference can be an array section. 18275 // 18276 AllowUnitySizeArraySection = false; 18277 AllowWholeSizeArraySection = false; 18278 18279 // Record the component. 18280 Components.emplace_back(ME, FD, IsNonContiguous); 18281 return RelevantExpr || Visit(E); 18282 } 18283 18284 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 18285 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 18286 18287 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 18288 if (!NoDiagnose) { 18289 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18290 << 0 << AE->getSourceRange(); 18291 return false; 18292 } 18293 return RelevantExpr || Visit(E); 18294 } 18295 18296 // If we got an array subscript that express the whole dimension we 18297 // can have any array expressions before. If it only expressing part of 18298 // the dimension, we can only have unitary-size array expressions. 18299 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 18300 E->getType())) 18301 AllowWholeSizeArraySection = false; 18302 18303 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 18304 Expr::EvalResult Result; 18305 if (!AE->getIdx()->isValueDependent() && 18306 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 18307 !Result.Val.getInt().isNullValue()) { 18308 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18309 diag::err_omp_invalid_map_this_expr); 18310 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18311 diag::note_omp_invalid_subscript_on_this_ptr_map); 18312 } 18313 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18314 RelevantExpr = TE; 18315 } 18316 18317 // Record the component - we don't have any declaration associated. 18318 Components.emplace_back(AE, nullptr, IsNonContiguous); 18319 18320 return RelevantExpr || Visit(E); 18321 } 18322 18323 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 18324 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 18325 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 18326 QualType CurType = 18327 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 18328 18329 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18330 // If the type of a list item is a reference to a type T then the type 18331 // will be considered to be T for all purposes of this clause. 18332 if (CurType->isReferenceType()) 18333 CurType = CurType->getPointeeType(); 18334 18335 bool IsPointer = CurType->isAnyPointerType(); 18336 18337 if (!IsPointer && !CurType->isArrayType()) { 18338 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18339 << 0 << OASE->getSourceRange(); 18340 return false; 18341 } 18342 18343 bool NotWhole = 18344 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 18345 bool NotUnity = 18346 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 18347 18348 if (AllowWholeSizeArraySection) { 18349 // Any array section is currently allowed. Allowing a whole size array 18350 // section implies allowing a unity array section as well. 18351 // 18352 // If this array section refers to the whole dimension we can still 18353 // accept other array sections before this one, except if the base is a 18354 // pointer. Otherwise, only unitary sections are accepted. 18355 if (NotWhole || IsPointer) 18356 AllowWholeSizeArraySection = false; 18357 } else if (DKind == OMPD_target_update && 18358 SemaRef.getLangOpts().OpenMP >= 50) { 18359 if (IsPointer && !AllowAnotherPtr) 18360 SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined) 18361 << /*array of unknown bound */ 1; 18362 else 18363 IsNonContiguous = true; 18364 } else if (AllowUnitySizeArraySection && NotUnity) { 18365 // A unity or whole array section is not allowed and that is not 18366 // compatible with the properties of the current array section. 18367 SemaRef.Diag( 18368 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 18369 << OASE->getSourceRange(); 18370 return false; 18371 } 18372 18373 if (IsPointer) 18374 AllowAnotherPtr = false; 18375 18376 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 18377 Expr::EvalResult ResultR; 18378 Expr::EvalResult ResultL; 18379 if (!OASE->getLength()->isValueDependent() && 18380 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 18381 !ResultR.Val.getInt().isOneValue()) { 18382 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18383 diag::err_omp_invalid_map_this_expr); 18384 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18385 diag::note_omp_invalid_length_on_this_ptr_mapping); 18386 } 18387 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 18388 OASE->getLowerBound()->EvaluateAsInt(ResultL, 18389 SemaRef.getASTContext()) && 18390 !ResultL.Val.getInt().isNullValue()) { 18391 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18392 diag::err_omp_invalid_map_this_expr); 18393 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18394 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 18395 } 18396 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18397 RelevantExpr = TE; 18398 } 18399 18400 // Record the component - we don't have any declaration associated. 18401 Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false); 18402 return RelevantExpr || Visit(E); 18403 } 18404 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 18405 Expr *Base = E->getBase(); 18406 18407 // Record the component - we don't have any declaration associated. 18408 Components.emplace_back(E, nullptr, IsNonContiguous); 18409 18410 return Visit(Base->IgnoreParenImpCasts()); 18411 } 18412 18413 bool VisitUnaryOperator(UnaryOperator *UO) { 18414 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 18415 UO->getOpcode() != UO_Deref) { 18416 emitErrorMsg(); 18417 return false; 18418 } 18419 if (!RelevantExpr) { 18420 // Record the component if haven't found base decl. 18421 Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false); 18422 } 18423 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 18424 } 18425 bool VisitBinaryOperator(BinaryOperator *BO) { 18426 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 18427 emitErrorMsg(); 18428 return false; 18429 } 18430 18431 // Pointer arithmetic is the only thing we expect to happen here so after we 18432 // make sure the binary operator is a pointer type, the we only thing need 18433 // to to is to visit the subtree that has the same type as root (so that we 18434 // know the other subtree is just an offset) 18435 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 18436 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 18437 Components.emplace_back(BO, nullptr, false); 18438 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 18439 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 18440 "Either LHS or RHS have base decl inside"); 18441 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 18442 return RelevantExpr || Visit(LE); 18443 return RelevantExpr || Visit(RE); 18444 } 18445 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 18446 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18447 RelevantExpr = CTE; 18448 Components.emplace_back(CTE, nullptr, IsNonContiguous); 18449 return true; 18450 } 18451 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 18452 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18453 Components.emplace_back(COCE, nullptr, IsNonContiguous); 18454 return true; 18455 } 18456 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { 18457 Expr *Source = E->getSourceExpr(); 18458 if (!Source) { 18459 emitErrorMsg(); 18460 return false; 18461 } 18462 return Visit(Source); 18463 } 18464 bool VisitStmt(Stmt *) { 18465 emitErrorMsg(); 18466 return false; 18467 } 18468 const Expr *getFoundBase() const { 18469 return RelevantExpr; 18470 } 18471 explicit MapBaseChecker( 18472 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, 18473 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 18474 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 18475 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components), 18476 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 18477 }; 18478 } // namespace 18479 18480 /// Return the expression of the base of the mappable expression or null if it 18481 /// cannot be determined and do all the necessary checks to see if the expression 18482 /// is valid as a standalone mappable expression. In the process, record all the 18483 /// components of the expression. 18484 static const Expr *checkMapClauseExpressionBase( 18485 Sema &SemaRef, Expr *E, 18486 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 18487 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) { 18488 SourceLocation ELoc = E->getExprLoc(); 18489 SourceRange ERange = E->getSourceRange(); 18490 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc, 18491 ERange); 18492 if (Checker.Visit(E->IgnoreParens())) { 18493 // Check if the highest dimension array section has length specified 18494 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() && 18495 (CKind == OMPC_to || CKind == OMPC_from)) { 18496 auto CI = CurComponents.rbegin(); 18497 auto CE = CurComponents.rend(); 18498 for (; CI != CE; ++CI) { 18499 const auto *OASE = 18500 dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression()); 18501 if (!OASE) 18502 continue; 18503 if (OASE && OASE->getLength()) 18504 break; 18505 SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length) 18506 << ERange; 18507 } 18508 } 18509 return Checker.getFoundBase(); 18510 } 18511 return nullptr; 18512 } 18513 18514 // Return true if expression E associated with value VD has conflicts with other 18515 // map information. 18516 static bool checkMapConflicts( 18517 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 18518 bool CurrentRegionOnly, 18519 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 18520 OpenMPClauseKind CKind) { 18521 assert(VD && E); 18522 SourceLocation ELoc = E->getExprLoc(); 18523 SourceRange ERange = E->getSourceRange(); 18524 18525 // In order to easily check the conflicts we need to match each component of 18526 // the expression under test with the components of the expressions that are 18527 // already in the stack. 18528 18529 assert(!CurComponents.empty() && "Map clause expression with no components!"); 18530 assert(CurComponents.back().getAssociatedDeclaration() == VD && 18531 "Map clause expression with unexpected base!"); 18532 18533 // Variables to help detecting enclosing problems in data environment nests. 18534 bool IsEnclosedByDataEnvironmentExpr = false; 18535 const Expr *EnclosingExpr = nullptr; 18536 18537 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 18538 VD, CurrentRegionOnly, 18539 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 18540 ERange, CKind, &EnclosingExpr, 18541 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 18542 StackComponents, 18543 OpenMPClauseKind Kind) { 18544 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50) 18545 return false; 18546 assert(!StackComponents.empty() && 18547 "Map clause expression with no components!"); 18548 assert(StackComponents.back().getAssociatedDeclaration() == VD && 18549 "Map clause expression with unexpected base!"); 18550 (void)VD; 18551 18552 // The whole expression in the stack. 18553 const Expr *RE = StackComponents.front().getAssociatedExpression(); 18554 18555 // Expressions must start from the same base. Here we detect at which 18556 // point both expressions diverge from each other and see if we can 18557 // detect if the memory referred to both expressions is contiguous and 18558 // do not overlap. 18559 auto CI = CurComponents.rbegin(); 18560 auto CE = CurComponents.rend(); 18561 auto SI = StackComponents.rbegin(); 18562 auto SE = StackComponents.rend(); 18563 for (; CI != CE && SI != SE; ++CI, ++SI) { 18564 18565 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 18566 // At most one list item can be an array item derived from a given 18567 // variable in map clauses of the same construct. 18568 if (CurrentRegionOnly && 18569 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 18570 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 18571 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 18572 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 18573 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 18574 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 18575 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 18576 diag::err_omp_multiple_array_items_in_map_clause) 18577 << CI->getAssociatedExpression()->getSourceRange(); 18578 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 18579 diag::note_used_here) 18580 << SI->getAssociatedExpression()->getSourceRange(); 18581 return true; 18582 } 18583 18584 // Do both expressions have the same kind? 18585 if (CI->getAssociatedExpression()->getStmtClass() != 18586 SI->getAssociatedExpression()->getStmtClass()) 18587 break; 18588 18589 // Are we dealing with different variables/fields? 18590 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 18591 break; 18592 } 18593 // Check if the extra components of the expressions in the enclosing 18594 // data environment are redundant for the current base declaration. 18595 // If they are, the maps completely overlap, which is legal. 18596 for (; SI != SE; ++SI) { 18597 QualType Type; 18598 if (const auto *ASE = 18599 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 18600 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 18601 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 18602 SI->getAssociatedExpression())) { 18603 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 18604 Type = 18605 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 18606 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 18607 SI->getAssociatedExpression())) { 18608 Type = OASE->getBase()->getType()->getPointeeType(); 18609 } 18610 if (Type.isNull() || Type->isAnyPointerType() || 18611 checkArrayExpressionDoesNotReferToWholeSize( 18612 SemaRef, SI->getAssociatedExpression(), Type)) 18613 break; 18614 } 18615 18616 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 18617 // List items of map clauses in the same construct must not share 18618 // original storage. 18619 // 18620 // If the expressions are exactly the same or one is a subset of the 18621 // other, it means they are sharing storage. 18622 if (CI == CE && SI == SE) { 18623 if (CurrentRegionOnly) { 18624 if (CKind == OMPC_map) { 18625 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 18626 } else { 18627 assert(CKind == OMPC_to || CKind == OMPC_from); 18628 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 18629 << ERange; 18630 } 18631 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18632 << RE->getSourceRange(); 18633 return true; 18634 } 18635 // If we find the same expression in the enclosing data environment, 18636 // that is legal. 18637 IsEnclosedByDataEnvironmentExpr = true; 18638 return false; 18639 } 18640 18641 QualType DerivedType = 18642 std::prev(CI)->getAssociatedDeclaration()->getType(); 18643 SourceLocation DerivedLoc = 18644 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 18645 18646 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18647 // If the type of a list item is a reference to a type T then the type 18648 // will be considered to be T for all purposes of this clause. 18649 DerivedType = DerivedType.getNonReferenceType(); 18650 18651 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 18652 // A variable for which the type is pointer and an array section 18653 // derived from that variable must not appear as list items of map 18654 // clauses of the same construct. 18655 // 18656 // Also, cover one of the cases in: 18657 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 18658 // If any part of the original storage of a list item has corresponding 18659 // storage in the device data environment, all of the original storage 18660 // must have corresponding storage in the device data environment. 18661 // 18662 if (DerivedType->isAnyPointerType()) { 18663 if (CI == CE || SI == SE) { 18664 SemaRef.Diag( 18665 DerivedLoc, 18666 diag::err_omp_pointer_mapped_along_with_derived_section) 18667 << DerivedLoc; 18668 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18669 << RE->getSourceRange(); 18670 return true; 18671 } 18672 if (CI->getAssociatedExpression()->getStmtClass() != 18673 SI->getAssociatedExpression()->getStmtClass() || 18674 CI->getAssociatedDeclaration()->getCanonicalDecl() == 18675 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 18676 assert(CI != CE && SI != SE); 18677 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 18678 << DerivedLoc; 18679 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18680 << RE->getSourceRange(); 18681 return true; 18682 } 18683 } 18684 18685 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 18686 // List items of map clauses in the same construct must not share 18687 // original storage. 18688 // 18689 // An expression is a subset of the other. 18690 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 18691 if (CKind == OMPC_map) { 18692 if (CI != CE || SI != SE) { 18693 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 18694 // a pointer. 18695 auto Begin = 18696 CI != CE ? CurComponents.begin() : StackComponents.begin(); 18697 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 18698 auto It = Begin; 18699 while (It != End && !It->getAssociatedDeclaration()) 18700 std::advance(It, 1); 18701 assert(It != End && 18702 "Expected at least one component with the declaration."); 18703 if (It != Begin && It->getAssociatedDeclaration() 18704 ->getType() 18705 .getCanonicalType() 18706 ->isAnyPointerType()) { 18707 IsEnclosedByDataEnvironmentExpr = false; 18708 EnclosingExpr = nullptr; 18709 return false; 18710 } 18711 } 18712 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 18713 } else { 18714 assert(CKind == OMPC_to || CKind == OMPC_from); 18715 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 18716 << ERange; 18717 } 18718 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 18719 << RE->getSourceRange(); 18720 return true; 18721 } 18722 18723 // The current expression uses the same base as other expression in the 18724 // data environment but does not contain it completely. 18725 if (!CurrentRegionOnly && SI != SE) 18726 EnclosingExpr = RE; 18727 18728 // The current expression is a subset of the expression in the data 18729 // environment. 18730 IsEnclosedByDataEnvironmentExpr |= 18731 (!CurrentRegionOnly && CI != CE && SI == SE); 18732 18733 return false; 18734 }); 18735 18736 if (CurrentRegionOnly) 18737 return FoundError; 18738 18739 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 18740 // If any part of the original storage of a list item has corresponding 18741 // storage in the device data environment, all of the original storage must 18742 // have corresponding storage in the device data environment. 18743 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 18744 // If a list item is an element of a structure, and a different element of 18745 // the structure has a corresponding list item in the device data environment 18746 // prior to a task encountering the construct associated with the map clause, 18747 // then the list item must also have a corresponding list item in the device 18748 // data environment prior to the task encountering the construct. 18749 // 18750 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 18751 SemaRef.Diag(ELoc, 18752 diag::err_omp_original_storage_is_shared_and_does_not_contain) 18753 << ERange; 18754 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 18755 << EnclosingExpr->getSourceRange(); 18756 return true; 18757 } 18758 18759 return FoundError; 18760 } 18761 18762 // Look up the user-defined mapper given the mapper name and mapped type, and 18763 // build a reference to it. 18764 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 18765 CXXScopeSpec &MapperIdScopeSpec, 18766 const DeclarationNameInfo &MapperId, 18767 QualType Type, 18768 Expr *UnresolvedMapper) { 18769 if (MapperIdScopeSpec.isInvalid()) 18770 return ExprError(); 18771 // Get the actual type for the array type. 18772 if (Type->isArrayType()) { 18773 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 18774 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 18775 } 18776 // Find all user-defined mappers with the given MapperId. 18777 SmallVector<UnresolvedSet<8>, 4> Lookups; 18778 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 18779 Lookup.suppressDiagnostics(); 18780 if (S) { 18781 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 18782 NamedDecl *D = Lookup.getRepresentativeDecl(); 18783 while (S && !S->isDeclScope(D)) 18784 S = S->getParent(); 18785 if (S) 18786 S = S->getParent(); 18787 Lookups.emplace_back(); 18788 Lookups.back().append(Lookup.begin(), Lookup.end()); 18789 Lookup.clear(); 18790 } 18791 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 18792 // Extract the user-defined mappers with the given MapperId. 18793 Lookups.push_back(UnresolvedSet<8>()); 18794 for (NamedDecl *D : ULE->decls()) { 18795 auto *DMD = cast<OMPDeclareMapperDecl>(D); 18796 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 18797 Lookups.back().addDecl(DMD); 18798 } 18799 } 18800 // Defer the lookup for dependent types. The results will be passed through 18801 // UnresolvedMapper on instantiation. 18802 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 18803 Type->isInstantiationDependentType() || 18804 Type->containsUnexpandedParameterPack() || 18805 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 18806 return !D->isInvalidDecl() && 18807 (D->getType()->isDependentType() || 18808 D->getType()->isInstantiationDependentType() || 18809 D->getType()->containsUnexpandedParameterPack()); 18810 })) { 18811 UnresolvedSet<8> URS; 18812 for (const UnresolvedSet<8> &Set : Lookups) { 18813 if (Set.empty()) 18814 continue; 18815 URS.append(Set.begin(), Set.end()); 18816 } 18817 return UnresolvedLookupExpr::Create( 18818 SemaRef.Context, /*NamingClass=*/nullptr, 18819 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 18820 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 18821 } 18822 SourceLocation Loc = MapperId.getLoc(); 18823 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 18824 // The type must be of struct, union or class type in C and C++ 18825 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 18826 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 18827 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 18828 return ExprError(); 18829 } 18830 // Perform argument dependent lookup. 18831 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 18832 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 18833 // Return the first user-defined mapper with the desired type. 18834 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 18835 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 18836 if (!D->isInvalidDecl() && 18837 SemaRef.Context.hasSameType(D->getType(), Type)) 18838 return D; 18839 return nullptr; 18840 })) 18841 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 18842 // Find the first user-defined mapper with a type derived from the desired 18843 // type. 18844 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 18845 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 18846 if (!D->isInvalidDecl() && 18847 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 18848 !Type.isMoreQualifiedThan(D->getType())) 18849 return D; 18850 return nullptr; 18851 })) { 18852 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 18853 /*DetectVirtual=*/false); 18854 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 18855 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 18856 VD->getType().getUnqualifiedType()))) { 18857 if (SemaRef.CheckBaseClassAccess( 18858 Loc, VD->getType(), Type, Paths.front(), 18859 /*DiagID=*/0) != Sema::AR_inaccessible) { 18860 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 18861 } 18862 } 18863 } 18864 } 18865 // Report error if a mapper is specified, but cannot be found. 18866 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 18867 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 18868 << Type << MapperId.getName(); 18869 return ExprError(); 18870 } 18871 return ExprEmpty(); 18872 } 18873 18874 namespace { 18875 // Utility struct that gathers all the related lists associated with a mappable 18876 // expression. 18877 struct MappableVarListInfo { 18878 // The list of expressions. 18879 ArrayRef<Expr *> VarList; 18880 // The list of processed expressions. 18881 SmallVector<Expr *, 16> ProcessedVarList; 18882 // The mappble components for each expression. 18883 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 18884 // The base declaration of the variable. 18885 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 18886 // The reference to the user-defined mapper associated with every expression. 18887 SmallVector<Expr *, 16> UDMapperList; 18888 18889 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 18890 // We have a list of components and base declarations for each entry in the 18891 // variable list. 18892 VarComponents.reserve(VarList.size()); 18893 VarBaseDeclarations.reserve(VarList.size()); 18894 } 18895 }; 18896 } 18897 18898 // Check the validity of the provided variable list for the provided clause kind 18899 // \a CKind. In the check process the valid expressions, mappable expression 18900 // components, variables, and user-defined mappers are extracted and used to 18901 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 18902 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 18903 // and \a MapperId are expected to be valid if the clause kind is 'map'. 18904 static void checkMappableExpressionList( 18905 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 18906 MappableVarListInfo &MVLI, SourceLocation StartLoc, 18907 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 18908 ArrayRef<Expr *> UnresolvedMappers, 18909 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 18910 bool IsMapTypeImplicit = false) { 18911 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 18912 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 18913 "Unexpected clause kind with mappable expressions!"); 18914 18915 // If the identifier of user-defined mapper is not specified, it is "default". 18916 // We do not change the actual name in this clause to distinguish whether a 18917 // mapper is specified explicitly, i.e., it is not explicitly specified when 18918 // MapperId.getName() is empty. 18919 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 18920 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 18921 MapperId.setName(DeclNames.getIdentifier( 18922 &SemaRef.getASTContext().Idents.get("default"))); 18923 MapperId.setLoc(StartLoc); 18924 } 18925 18926 // Iterators to find the current unresolved mapper expression. 18927 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 18928 bool UpdateUMIt = false; 18929 Expr *UnresolvedMapper = nullptr; 18930 18931 // Keep track of the mappable components and base declarations in this clause. 18932 // Each entry in the list is going to have a list of components associated. We 18933 // record each set of the components so that we can build the clause later on. 18934 // In the end we should have the same amount of declarations and component 18935 // lists. 18936 18937 for (Expr *RE : MVLI.VarList) { 18938 assert(RE && "Null expr in omp to/from/map clause"); 18939 SourceLocation ELoc = RE->getExprLoc(); 18940 18941 // Find the current unresolved mapper expression. 18942 if (UpdateUMIt && UMIt != UMEnd) { 18943 UMIt++; 18944 assert( 18945 UMIt != UMEnd && 18946 "Expect the size of UnresolvedMappers to match with that of VarList"); 18947 } 18948 UpdateUMIt = true; 18949 if (UMIt != UMEnd) 18950 UnresolvedMapper = *UMIt; 18951 18952 const Expr *VE = RE->IgnoreParenLValueCasts(); 18953 18954 if (VE->isValueDependent() || VE->isTypeDependent() || 18955 VE->isInstantiationDependent() || 18956 VE->containsUnexpandedParameterPack()) { 18957 // Try to find the associated user-defined mapper. 18958 ExprResult ER = buildUserDefinedMapperRef( 18959 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 18960 VE->getType().getCanonicalType(), UnresolvedMapper); 18961 if (ER.isInvalid()) 18962 continue; 18963 MVLI.UDMapperList.push_back(ER.get()); 18964 // We can only analyze this information once the missing information is 18965 // resolved. 18966 MVLI.ProcessedVarList.push_back(RE); 18967 continue; 18968 } 18969 18970 Expr *SimpleExpr = RE->IgnoreParenCasts(); 18971 18972 if (!RE->isLValue()) { 18973 if (SemaRef.getLangOpts().OpenMP < 50) { 18974 SemaRef.Diag( 18975 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 18976 << RE->getSourceRange(); 18977 } else { 18978 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18979 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 18980 } 18981 continue; 18982 } 18983 18984 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 18985 ValueDecl *CurDeclaration = nullptr; 18986 18987 // Obtain the array or member expression bases if required. Also, fill the 18988 // components array with all the components identified in the process. 18989 const Expr *BE = checkMapClauseExpressionBase( 18990 SemaRef, SimpleExpr, CurComponents, CKind, DSAS->getCurrentDirective(), 18991 /*NoDiagnose=*/false); 18992 if (!BE) 18993 continue; 18994 18995 assert(!CurComponents.empty() && 18996 "Invalid mappable expression information."); 18997 18998 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 18999 // Add store "this" pointer to class in DSAStackTy for future checking 19000 DSAS->addMappedClassesQualTypes(TE->getType()); 19001 // Try to find the associated user-defined mapper. 19002 ExprResult ER = buildUserDefinedMapperRef( 19003 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19004 VE->getType().getCanonicalType(), UnresolvedMapper); 19005 if (ER.isInvalid()) 19006 continue; 19007 MVLI.UDMapperList.push_back(ER.get()); 19008 // Skip restriction checking for variable or field declarations 19009 MVLI.ProcessedVarList.push_back(RE); 19010 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19011 MVLI.VarComponents.back().append(CurComponents.begin(), 19012 CurComponents.end()); 19013 MVLI.VarBaseDeclarations.push_back(nullptr); 19014 continue; 19015 } 19016 19017 // For the following checks, we rely on the base declaration which is 19018 // expected to be associated with the last component. The declaration is 19019 // expected to be a variable or a field (if 'this' is being mapped). 19020 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 19021 assert(CurDeclaration && "Null decl on map clause."); 19022 assert( 19023 CurDeclaration->isCanonicalDecl() && 19024 "Expecting components to have associated only canonical declarations."); 19025 19026 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 19027 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 19028 19029 assert((VD || FD) && "Only variables or fields are expected here!"); 19030 (void)FD; 19031 19032 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 19033 // threadprivate variables cannot appear in a map clause. 19034 // OpenMP 4.5 [2.10.5, target update Construct] 19035 // threadprivate variables cannot appear in a from clause. 19036 if (VD && DSAS->isThreadPrivate(VD)) { 19037 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19038 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 19039 << getOpenMPClauseName(CKind); 19040 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 19041 continue; 19042 } 19043 19044 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19045 // A list item cannot appear in both a map clause and a data-sharing 19046 // attribute clause on the same construct. 19047 19048 // Check conflicts with other map clause expressions. We check the conflicts 19049 // with the current construct separately from the enclosing data 19050 // environment, because the restrictions are different. We only have to 19051 // check conflicts across regions for the map clauses. 19052 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19053 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 19054 break; 19055 if (CKind == OMPC_map && 19056 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 19057 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19058 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 19059 break; 19060 19061 // OpenMP 4.5 [2.10.5, target update Construct] 19062 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19063 // If the type of a list item is a reference to a type T then the type will 19064 // be considered to be T for all purposes of this clause. 19065 auto I = llvm::find_if( 19066 CurComponents, 19067 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 19068 return MC.getAssociatedDeclaration(); 19069 }); 19070 assert(I != CurComponents.end() && "Null decl on map clause."); 19071 (void)I; 19072 QualType Type; 19073 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 19074 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 19075 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 19076 if (ASE) { 19077 Type = ASE->getType().getNonReferenceType(); 19078 } else if (OASE) { 19079 QualType BaseType = 19080 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 19081 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 19082 Type = ATy->getElementType(); 19083 else 19084 Type = BaseType->getPointeeType(); 19085 Type = Type.getNonReferenceType(); 19086 } else if (OAShE) { 19087 Type = OAShE->getBase()->getType()->getPointeeType(); 19088 } else { 19089 Type = VE->getType(); 19090 } 19091 19092 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 19093 // A list item in a to or from clause must have a mappable type. 19094 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19095 // A list item must have a mappable type. 19096 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 19097 DSAS, Type)) 19098 continue; 19099 19100 if (CKind == OMPC_map) { 19101 // target enter data 19102 // OpenMP [2.10.2, Restrictions, p. 99] 19103 // A map-type must be specified in all map clauses and must be either 19104 // to or alloc. 19105 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 19106 if (DKind == OMPD_target_enter_data && 19107 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 19108 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19109 << (IsMapTypeImplicit ? 1 : 0) 19110 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19111 << getOpenMPDirectiveName(DKind); 19112 continue; 19113 } 19114 19115 // target exit_data 19116 // OpenMP [2.10.3, Restrictions, p. 102] 19117 // A map-type must be specified in all map clauses and must be either 19118 // from, release, or delete. 19119 if (DKind == OMPD_target_exit_data && 19120 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 19121 MapType == OMPC_MAP_delete)) { 19122 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19123 << (IsMapTypeImplicit ? 1 : 0) 19124 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19125 << getOpenMPDirectiveName(DKind); 19126 continue; 19127 } 19128 19129 // target, target data 19130 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 19131 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 19132 // A map-type in a map clause must be to, from, tofrom or alloc 19133 if ((DKind == OMPD_target_data || 19134 isOpenMPTargetExecutionDirective(DKind)) && 19135 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 19136 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 19137 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19138 << (IsMapTypeImplicit ? 1 : 0) 19139 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19140 << getOpenMPDirectiveName(DKind); 19141 continue; 19142 } 19143 19144 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 19145 // A list item cannot appear in both a map clause and a data-sharing 19146 // attribute clause on the same construct 19147 // 19148 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 19149 // A list item cannot appear in both a map clause and a data-sharing 19150 // attribute clause on the same construct unless the construct is a 19151 // combined construct. 19152 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 19153 isOpenMPTargetExecutionDirective(DKind)) || 19154 DKind == OMPD_target)) { 19155 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19156 if (isOpenMPPrivate(DVar.CKind)) { 19157 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 19158 << getOpenMPClauseName(DVar.CKind) 19159 << getOpenMPClauseName(OMPC_map) 19160 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 19161 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 19162 continue; 19163 } 19164 } 19165 } 19166 19167 // Try to find the associated user-defined mapper. 19168 ExprResult ER = buildUserDefinedMapperRef( 19169 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19170 Type.getCanonicalType(), UnresolvedMapper); 19171 if (ER.isInvalid()) 19172 continue; 19173 MVLI.UDMapperList.push_back(ER.get()); 19174 19175 // Save the current expression. 19176 MVLI.ProcessedVarList.push_back(RE); 19177 19178 // Store the components in the stack so that they can be used to check 19179 // against other clauses later on. 19180 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 19181 /*WhereFoundClauseKind=*/OMPC_map); 19182 19183 // Save the components and declaration to create the clause. For purposes of 19184 // the clause creation, any component list that has has base 'this' uses 19185 // null as base declaration. 19186 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19187 MVLI.VarComponents.back().append(CurComponents.begin(), 19188 CurComponents.end()); 19189 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 19190 : CurDeclaration); 19191 } 19192 } 19193 19194 OMPClause *Sema::ActOnOpenMPMapClause( 19195 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 19196 ArrayRef<SourceLocation> MapTypeModifiersLoc, 19197 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19198 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 19199 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19200 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 19201 OpenMPMapModifierKind Modifiers[] = { 19202 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19203 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown}; 19204 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 19205 19206 // Process map-type-modifiers, flag errors for duplicate modifiers. 19207 unsigned Count = 0; 19208 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 19209 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 19210 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 19211 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 19212 continue; 19213 } 19214 assert(Count < NumberOfOMPMapClauseModifiers && 19215 "Modifiers exceed the allowed number of map type modifiers"); 19216 Modifiers[Count] = MapTypeModifiers[I]; 19217 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 19218 ++Count; 19219 } 19220 19221 MappableVarListInfo MVLI(VarList); 19222 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 19223 MapperIdScopeSpec, MapperId, UnresolvedMappers, 19224 MapType, IsMapTypeImplicit); 19225 19226 // We need to produce a map clause even if we don't have variables so that 19227 // other diagnostics related with non-existing map clauses are accurate. 19228 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 19229 MVLI.VarBaseDeclarations, MVLI.VarComponents, 19230 MVLI.UDMapperList, Modifiers, ModifiersLoc, 19231 MapperIdScopeSpec.getWithLocInContext(Context), 19232 MapperId, MapType, IsMapTypeImplicit, MapLoc); 19233 } 19234 19235 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 19236 TypeResult ParsedType) { 19237 assert(ParsedType.isUsable()); 19238 19239 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 19240 if (ReductionType.isNull()) 19241 return QualType(); 19242 19243 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 19244 // A type name in a declare reduction directive cannot be a function type, an 19245 // array type, a reference type, or a type qualified with const, volatile or 19246 // restrict. 19247 if (ReductionType.hasQualifiers()) { 19248 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 19249 return QualType(); 19250 } 19251 19252 if (ReductionType->isFunctionType()) { 19253 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 19254 return QualType(); 19255 } 19256 if (ReductionType->isReferenceType()) { 19257 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 19258 return QualType(); 19259 } 19260 if (ReductionType->isArrayType()) { 19261 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 19262 return QualType(); 19263 } 19264 return ReductionType; 19265 } 19266 19267 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 19268 Scope *S, DeclContext *DC, DeclarationName Name, 19269 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 19270 AccessSpecifier AS, Decl *PrevDeclInScope) { 19271 SmallVector<Decl *, 8> Decls; 19272 Decls.reserve(ReductionTypes.size()); 19273 19274 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 19275 forRedeclarationInCurContext()); 19276 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 19277 // A reduction-identifier may not be re-declared in the current scope for the 19278 // same type or for a type that is compatible according to the base language 19279 // rules. 19280 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 19281 OMPDeclareReductionDecl *PrevDRD = nullptr; 19282 bool InCompoundScope = true; 19283 if (S != nullptr) { 19284 // Find previous declaration with the same name not referenced in other 19285 // declarations. 19286 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 19287 InCompoundScope = 19288 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 19289 LookupName(Lookup, S); 19290 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 19291 /*AllowInlineNamespace=*/false); 19292 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 19293 LookupResult::Filter Filter = Lookup.makeFilter(); 19294 while (Filter.hasNext()) { 19295 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 19296 if (InCompoundScope) { 19297 auto I = UsedAsPrevious.find(PrevDecl); 19298 if (I == UsedAsPrevious.end()) 19299 UsedAsPrevious[PrevDecl] = false; 19300 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 19301 UsedAsPrevious[D] = true; 19302 } 19303 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 19304 PrevDecl->getLocation(); 19305 } 19306 Filter.done(); 19307 if (InCompoundScope) { 19308 for (const auto &PrevData : UsedAsPrevious) { 19309 if (!PrevData.second) { 19310 PrevDRD = PrevData.first; 19311 break; 19312 } 19313 } 19314 } 19315 } else if (PrevDeclInScope != nullptr) { 19316 auto *PrevDRDInScope = PrevDRD = 19317 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 19318 do { 19319 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 19320 PrevDRDInScope->getLocation(); 19321 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 19322 } while (PrevDRDInScope != nullptr); 19323 } 19324 for (const auto &TyData : ReductionTypes) { 19325 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 19326 bool Invalid = false; 19327 if (I != PreviousRedeclTypes.end()) { 19328 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 19329 << TyData.first; 19330 Diag(I->second, diag::note_previous_definition); 19331 Invalid = true; 19332 } 19333 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 19334 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 19335 Name, TyData.first, PrevDRD); 19336 DC->addDecl(DRD); 19337 DRD->setAccess(AS); 19338 Decls.push_back(DRD); 19339 if (Invalid) 19340 DRD->setInvalidDecl(); 19341 else 19342 PrevDRD = DRD; 19343 } 19344 19345 return DeclGroupPtrTy::make( 19346 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 19347 } 19348 19349 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 19350 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19351 19352 // Enter new function scope. 19353 PushFunctionScope(); 19354 setFunctionHasBranchProtectedScope(); 19355 getCurFunction()->setHasOMPDeclareReductionCombiner(); 19356 19357 if (S != nullptr) 19358 PushDeclContext(S, DRD); 19359 else 19360 CurContext = DRD; 19361 19362 PushExpressionEvaluationContext( 19363 ExpressionEvaluationContext::PotentiallyEvaluated); 19364 19365 QualType ReductionType = DRD->getType(); 19366 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 19367 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 19368 // uses semantics of argument handles by value, but it should be passed by 19369 // reference. C lang does not support references, so pass all parameters as 19370 // pointers. 19371 // Create 'T omp_in;' variable. 19372 VarDecl *OmpInParm = 19373 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 19374 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 19375 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 19376 // uses semantics of argument handles by value, but it should be passed by 19377 // reference. C lang does not support references, so pass all parameters as 19378 // pointers. 19379 // Create 'T omp_out;' variable. 19380 VarDecl *OmpOutParm = 19381 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 19382 if (S != nullptr) { 19383 PushOnScopeChains(OmpInParm, S); 19384 PushOnScopeChains(OmpOutParm, S); 19385 } else { 19386 DRD->addDecl(OmpInParm); 19387 DRD->addDecl(OmpOutParm); 19388 } 19389 Expr *InE = 19390 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 19391 Expr *OutE = 19392 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 19393 DRD->setCombinerData(InE, OutE); 19394 } 19395 19396 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 19397 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19398 DiscardCleanupsInEvaluationContext(); 19399 PopExpressionEvaluationContext(); 19400 19401 PopDeclContext(); 19402 PopFunctionScopeInfo(); 19403 19404 if (Combiner != nullptr) 19405 DRD->setCombiner(Combiner); 19406 else 19407 DRD->setInvalidDecl(); 19408 } 19409 19410 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 19411 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19412 19413 // Enter new function scope. 19414 PushFunctionScope(); 19415 setFunctionHasBranchProtectedScope(); 19416 19417 if (S != nullptr) 19418 PushDeclContext(S, DRD); 19419 else 19420 CurContext = DRD; 19421 19422 PushExpressionEvaluationContext( 19423 ExpressionEvaluationContext::PotentiallyEvaluated); 19424 19425 QualType ReductionType = DRD->getType(); 19426 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 19427 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 19428 // uses semantics of argument handles by value, but it should be passed by 19429 // reference. C lang does not support references, so pass all parameters as 19430 // pointers. 19431 // Create 'T omp_priv;' variable. 19432 VarDecl *OmpPrivParm = 19433 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 19434 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 19435 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 19436 // uses semantics of argument handles by value, but it should be passed by 19437 // reference. C lang does not support references, so pass all parameters as 19438 // pointers. 19439 // Create 'T omp_orig;' variable. 19440 VarDecl *OmpOrigParm = 19441 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 19442 if (S != nullptr) { 19443 PushOnScopeChains(OmpPrivParm, S); 19444 PushOnScopeChains(OmpOrigParm, S); 19445 } else { 19446 DRD->addDecl(OmpPrivParm); 19447 DRD->addDecl(OmpOrigParm); 19448 } 19449 Expr *OrigE = 19450 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 19451 Expr *PrivE = 19452 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 19453 DRD->setInitializerData(OrigE, PrivE); 19454 return OmpPrivParm; 19455 } 19456 19457 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 19458 VarDecl *OmpPrivParm) { 19459 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19460 DiscardCleanupsInEvaluationContext(); 19461 PopExpressionEvaluationContext(); 19462 19463 PopDeclContext(); 19464 PopFunctionScopeInfo(); 19465 19466 if (Initializer != nullptr) { 19467 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 19468 } else if (OmpPrivParm->hasInit()) { 19469 DRD->setInitializer(OmpPrivParm->getInit(), 19470 OmpPrivParm->isDirectInit() 19471 ? OMPDeclareReductionDecl::DirectInit 19472 : OMPDeclareReductionDecl::CopyInit); 19473 } else { 19474 DRD->setInvalidDecl(); 19475 } 19476 } 19477 19478 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 19479 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 19480 for (Decl *D : DeclReductions.get()) { 19481 if (IsValid) { 19482 if (S) 19483 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 19484 /*AddToContext=*/false); 19485 } else { 19486 D->setInvalidDecl(); 19487 } 19488 } 19489 return DeclReductions; 19490 } 19491 19492 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 19493 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 19494 QualType T = TInfo->getType(); 19495 if (D.isInvalidType()) 19496 return true; 19497 19498 if (getLangOpts().CPlusPlus) { 19499 // Check that there are no default arguments (C++ only). 19500 CheckExtraCXXDefaultArguments(D); 19501 } 19502 19503 return CreateParsedType(T, TInfo); 19504 } 19505 19506 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 19507 TypeResult ParsedType) { 19508 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 19509 19510 QualType MapperType = GetTypeFromParser(ParsedType.get()); 19511 assert(!MapperType.isNull() && "Expect valid mapper type"); 19512 19513 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19514 // The type must be of struct, union or class type in C and C++ 19515 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 19516 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 19517 return QualType(); 19518 } 19519 return MapperType; 19520 } 19521 19522 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 19523 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 19524 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 19525 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 19526 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 19527 forRedeclarationInCurContext()); 19528 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19529 // A mapper-identifier may not be redeclared in the current scope for the 19530 // same type or for a type that is compatible according to the base language 19531 // rules. 19532 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 19533 OMPDeclareMapperDecl *PrevDMD = nullptr; 19534 bool InCompoundScope = true; 19535 if (S != nullptr) { 19536 // Find previous declaration with the same name not referenced in other 19537 // declarations. 19538 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 19539 InCompoundScope = 19540 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 19541 LookupName(Lookup, S); 19542 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 19543 /*AllowInlineNamespace=*/false); 19544 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 19545 LookupResult::Filter Filter = Lookup.makeFilter(); 19546 while (Filter.hasNext()) { 19547 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 19548 if (InCompoundScope) { 19549 auto I = UsedAsPrevious.find(PrevDecl); 19550 if (I == UsedAsPrevious.end()) 19551 UsedAsPrevious[PrevDecl] = false; 19552 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 19553 UsedAsPrevious[D] = true; 19554 } 19555 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 19556 PrevDecl->getLocation(); 19557 } 19558 Filter.done(); 19559 if (InCompoundScope) { 19560 for (const auto &PrevData : UsedAsPrevious) { 19561 if (!PrevData.second) { 19562 PrevDMD = PrevData.first; 19563 break; 19564 } 19565 } 19566 } 19567 } else if (PrevDeclInScope) { 19568 auto *PrevDMDInScope = PrevDMD = 19569 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 19570 do { 19571 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 19572 PrevDMDInScope->getLocation(); 19573 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 19574 } while (PrevDMDInScope != nullptr); 19575 } 19576 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 19577 bool Invalid = false; 19578 if (I != PreviousRedeclTypes.end()) { 19579 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 19580 << MapperType << Name; 19581 Diag(I->second, diag::note_previous_definition); 19582 Invalid = true; 19583 } 19584 // Build expressions for implicit maps of data members with 'default' 19585 // mappers. 19586 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(), 19587 Clauses.end()); 19588 if (LangOpts.OpenMP >= 50) 19589 processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit); 19590 auto *DMD = 19591 OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN, 19592 ClausesWithImplicit, PrevDMD); 19593 if (S) 19594 PushOnScopeChains(DMD, S); 19595 else 19596 DC->addDecl(DMD); 19597 DMD->setAccess(AS); 19598 if (Invalid) 19599 DMD->setInvalidDecl(); 19600 19601 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 19602 VD->setDeclContext(DMD); 19603 VD->setLexicalDeclContext(DMD); 19604 DMD->addDecl(VD); 19605 DMD->setMapperVarRef(MapperVarRef); 19606 19607 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 19608 } 19609 19610 ExprResult 19611 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 19612 SourceLocation StartLoc, 19613 DeclarationName VN) { 19614 TypeSourceInfo *TInfo = 19615 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 19616 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 19617 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 19618 MapperType, TInfo, SC_None); 19619 if (S) 19620 PushOnScopeChains(VD, S, /*AddToContext=*/false); 19621 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 19622 DSAStack->addDeclareMapperVarRef(E); 19623 return E; 19624 } 19625 19626 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 19627 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 19628 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 19629 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) 19630 return VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl(); 19631 return true; 19632 } 19633 19634 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 19635 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 19636 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 19637 } 19638 19639 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 19640 SourceLocation StartLoc, 19641 SourceLocation LParenLoc, 19642 SourceLocation EndLoc) { 19643 Expr *ValExpr = NumTeams; 19644 Stmt *HelperValStmt = nullptr; 19645 19646 // OpenMP [teams Constrcut, Restrictions] 19647 // The num_teams expression must evaluate to a positive integer value. 19648 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 19649 /*StrictlyPositive=*/true)) 19650 return nullptr; 19651 19652 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 19653 OpenMPDirectiveKind CaptureRegion = 19654 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 19655 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 19656 ValExpr = MakeFullExpr(ValExpr).get(); 19657 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 19658 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 19659 HelperValStmt = buildPreInits(Context, Captures); 19660 } 19661 19662 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 19663 StartLoc, LParenLoc, EndLoc); 19664 } 19665 19666 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 19667 SourceLocation StartLoc, 19668 SourceLocation LParenLoc, 19669 SourceLocation EndLoc) { 19670 Expr *ValExpr = ThreadLimit; 19671 Stmt *HelperValStmt = nullptr; 19672 19673 // OpenMP [teams Constrcut, Restrictions] 19674 // The thread_limit expression must evaluate to a positive integer value. 19675 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 19676 /*StrictlyPositive=*/true)) 19677 return nullptr; 19678 19679 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 19680 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 19681 DKind, OMPC_thread_limit, LangOpts.OpenMP); 19682 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 19683 ValExpr = MakeFullExpr(ValExpr).get(); 19684 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 19685 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 19686 HelperValStmt = buildPreInits(Context, Captures); 19687 } 19688 19689 return new (Context) OMPThreadLimitClause( 19690 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 19691 } 19692 19693 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 19694 SourceLocation StartLoc, 19695 SourceLocation LParenLoc, 19696 SourceLocation EndLoc) { 19697 Expr *ValExpr = Priority; 19698 Stmt *HelperValStmt = nullptr; 19699 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 19700 19701 // OpenMP [2.9.1, task Constrcut] 19702 // The priority-value is a non-negative numerical scalar expression. 19703 if (!isNonNegativeIntegerValue( 19704 ValExpr, *this, OMPC_priority, 19705 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 19706 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 19707 return nullptr; 19708 19709 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 19710 StartLoc, LParenLoc, EndLoc); 19711 } 19712 19713 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 19714 SourceLocation StartLoc, 19715 SourceLocation LParenLoc, 19716 SourceLocation EndLoc) { 19717 Expr *ValExpr = Grainsize; 19718 Stmt *HelperValStmt = nullptr; 19719 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 19720 19721 // OpenMP [2.9.2, taskloop Constrcut] 19722 // The parameter of the grainsize clause must be a positive integer 19723 // expression. 19724 if (!isNonNegativeIntegerValue( 19725 ValExpr, *this, OMPC_grainsize, 19726 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 19727 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 19728 return nullptr; 19729 19730 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 19731 StartLoc, LParenLoc, EndLoc); 19732 } 19733 19734 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 19735 SourceLocation StartLoc, 19736 SourceLocation LParenLoc, 19737 SourceLocation EndLoc) { 19738 Expr *ValExpr = NumTasks; 19739 Stmt *HelperValStmt = nullptr; 19740 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 19741 19742 // OpenMP [2.9.2, taskloop Constrcut] 19743 // The parameter of the num_tasks clause must be a positive integer 19744 // expression. 19745 if (!isNonNegativeIntegerValue( 19746 ValExpr, *this, OMPC_num_tasks, 19747 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 19748 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 19749 return nullptr; 19750 19751 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 19752 StartLoc, LParenLoc, EndLoc); 19753 } 19754 19755 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 19756 SourceLocation LParenLoc, 19757 SourceLocation EndLoc) { 19758 // OpenMP [2.13.2, critical construct, Description] 19759 // ... where hint-expression is an integer constant expression that evaluates 19760 // to a valid lock hint. 19761 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 19762 if (HintExpr.isInvalid()) 19763 return nullptr; 19764 return new (Context) 19765 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 19766 } 19767 19768 /// Tries to find omp_event_handle_t type. 19769 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 19770 DSAStackTy *Stack) { 19771 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 19772 if (!OMPEventHandleT.isNull()) 19773 return true; 19774 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 19775 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 19776 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 19777 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 19778 return false; 19779 } 19780 Stack->setOMPEventHandleT(PT.get()); 19781 return true; 19782 } 19783 19784 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 19785 SourceLocation LParenLoc, 19786 SourceLocation EndLoc) { 19787 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 19788 !Evt->isInstantiationDependent() && 19789 !Evt->containsUnexpandedParameterPack()) { 19790 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 19791 return nullptr; 19792 // OpenMP 5.0, 2.10.1 task Construct. 19793 // event-handle is a variable of the omp_event_handle_t type. 19794 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 19795 if (!Ref) { 19796 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 19797 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 19798 return nullptr; 19799 } 19800 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 19801 if (!VD) { 19802 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 19803 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 19804 return nullptr; 19805 } 19806 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 19807 VD->getType()) || 19808 VD->getType().isConstant(Context)) { 19809 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 19810 << "omp_event_handle_t" << 1 << VD->getType() 19811 << Evt->getSourceRange(); 19812 return nullptr; 19813 } 19814 // OpenMP 5.0, 2.10.1 task Construct 19815 // [detach clause]... The event-handle will be considered as if it was 19816 // specified on a firstprivate clause. 19817 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 19818 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 19819 DVar.RefExpr) { 19820 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 19821 << getOpenMPClauseName(DVar.CKind) 19822 << getOpenMPClauseName(OMPC_firstprivate); 19823 reportOriginalDsa(*this, DSAStack, VD, DVar); 19824 return nullptr; 19825 } 19826 } 19827 19828 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 19829 } 19830 19831 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 19832 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 19833 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 19834 SourceLocation EndLoc) { 19835 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 19836 std::string Values; 19837 Values += "'"; 19838 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 19839 Values += "'"; 19840 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19841 << Values << getOpenMPClauseName(OMPC_dist_schedule); 19842 return nullptr; 19843 } 19844 Expr *ValExpr = ChunkSize; 19845 Stmt *HelperValStmt = nullptr; 19846 if (ChunkSize) { 19847 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 19848 !ChunkSize->isInstantiationDependent() && 19849 !ChunkSize->containsUnexpandedParameterPack()) { 19850 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 19851 ExprResult Val = 19852 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 19853 if (Val.isInvalid()) 19854 return nullptr; 19855 19856 ValExpr = Val.get(); 19857 19858 // OpenMP [2.7.1, Restrictions] 19859 // chunk_size must be a loop invariant integer expression with a positive 19860 // value. 19861 if (Optional<llvm::APSInt> Result = 19862 ValExpr->getIntegerConstantExpr(Context)) { 19863 if (Result->isSigned() && !Result->isStrictlyPositive()) { 19864 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 19865 << "dist_schedule" << ChunkSize->getSourceRange(); 19866 return nullptr; 19867 } 19868 } else if (getOpenMPCaptureRegionForClause( 19869 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 19870 LangOpts.OpenMP) != OMPD_unknown && 19871 !CurContext->isDependentContext()) { 19872 ValExpr = MakeFullExpr(ValExpr).get(); 19873 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 19874 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 19875 HelperValStmt = buildPreInits(Context, Captures); 19876 } 19877 } 19878 } 19879 19880 return new (Context) 19881 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 19882 Kind, ValExpr, HelperValStmt); 19883 } 19884 19885 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 19886 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 19887 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 19888 SourceLocation KindLoc, SourceLocation EndLoc) { 19889 if (getLangOpts().OpenMP < 50) { 19890 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 19891 Kind != OMPC_DEFAULTMAP_scalar) { 19892 std::string Value; 19893 SourceLocation Loc; 19894 Value += "'"; 19895 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 19896 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 19897 OMPC_DEFAULTMAP_MODIFIER_tofrom); 19898 Loc = MLoc; 19899 } else { 19900 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 19901 OMPC_DEFAULTMAP_scalar); 19902 Loc = KindLoc; 19903 } 19904 Value += "'"; 19905 Diag(Loc, diag::err_omp_unexpected_clause_value) 19906 << Value << getOpenMPClauseName(OMPC_defaultmap); 19907 return nullptr; 19908 } 19909 } else { 19910 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 19911 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 19912 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 19913 if (!isDefaultmapKind || !isDefaultmapModifier) { 19914 StringRef KindValue = "'scalar', 'aggregate', 'pointer'"; 19915 if (LangOpts.OpenMP == 50) { 19916 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 19917 "'firstprivate', 'none', 'default'"; 19918 if (!isDefaultmapKind && isDefaultmapModifier) { 19919 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19920 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19921 } else if (isDefaultmapKind && !isDefaultmapModifier) { 19922 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19923 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19924 } else { 19925 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19926 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19927 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19928 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19929 } 19930 } else { 19931 StringRef ModifierValue = 19932 "'alloc', 'from', 'to', 'tofrom', " 19933 "'firstprivate', 'none', 'default', 'present'"; 19934 if (!isDefaultmapKind && isDefaultmapModifier) { 19935 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19936 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19937 } else if (isDefaultmapKind && !isDefaultmapModifier) { 19938 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19939 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19940 } else { 19941 Diag(MLoc, diag::err_omp_unexpected_clause_value) 19942 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 19943 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 19944 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 19945 } 19946 } 19947 return nullptr; 19948 } 19949 19950 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 19951 // At most one defaultmap clause for each category can appear on the 19952 // directive. 19953 if (DSAStack->checkDefaultmapCategory(Kind)) { 19954 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 19955 return nullptr; 19956 } 19957 } 19958 if (Kind == OMPC_DEFAULTMAP_unknown) { 19959 // Variable category is not specified - mark all categories. 19960 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 19961 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 19962 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 19963 } else { 19964 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 19965 } 19966 19967 return new (Context) 19968 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 19969 } 19970 19971 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 19972 DeclContext *CurLexicalContext = getCurLexicalContext(); 19973 if (!CurLexicalContext->isFileContext() && 19974 !CurLexicalContext->isExternCContext() && 19975 !CurLexicalContext->isExternCXXContext() && 19976 !isa<CXXRecordDecl>(CurLexicalContext) && 19977 !isa<ClassTemplateDecl>(CurLexicalContext) && 19978 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 19979 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 19980 Diag(Loc, diag::err_omp_region_not_file_context); 19981 return false; 19982 } 19983 DeclareTargetNesting.push_back(Loc); 19984 return true; 19985 } 19986 19987 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 19988 assert(!DeclareTargetNesting.empty() && 19989 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 19990 DeclareTargetNesting.pop_back(); 19991 } 19992 19993 NamedDecl * 19994 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 19995 const DeclarationNameInfo &Id, 19996 NamedDeclSetType &SameDirectiveDecls) { 19997 LookupResult Lookup(*this, Id, LookupOrdinaryName); 19998 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 19999 20000 if (Lookup.isAmbiguous()) 20001 return nullptr; 20002 Lookup.suppressDiagnostics(); 20003 20004 if (!Lookup.isSingleResult()) { 20005 VarOrFuncDeclFilterCCC CCC(*this); 20006 if (TypoCorrection Corrected = 20007 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 20008 CTK_ErrorRecovery)) { 20009 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 20010 << Id.getName()); 20011 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 20012 return nullptr; 20013 } 20014 20015 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 20016 return nullptr; 20017 } 20018 20019 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 20020 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 20021 !isa<FunctionTemplateDecl>(ND)) { 20022 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 20023 return nullptr; 20024 } 20025 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 20026 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 20027 return ND; 20028 } 20029 20030 void Sema::ActOnOpenMPDeclareTargetName( 20031 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 20032 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 20033 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 20034 isa<FunctionTemplateDecl>(ND)) && 20035 "Expected variable, function or function template."); 20036 20037 // Diagnose marking after use as it may lead to incorrect diagnosis and 20038 // codegen. 20039 if (LangOpts.OpenMP >= 50 && 20040 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 20041 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 20042 20043 auto *VD = cast<ValueDecl>(ND); 20044 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 20045 OMPDeclareTargetDeclAttr::getDeviceType(VD); 20046 Optional<SourceLocation> AttrLoc = OMPDeclareTargetDeclAttr::getLocation(VD); 20047 if (DevTy.hasValue() && *DevTy != DT && 20048 (DeclareTargetNesting.empty() || 20049 *AttrLoc != DeclareTargetNesting.back())) { 20050 Diag(Loc, diag::err_omp_device_type_mismatch) 20051 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 20052 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy); 20053 return; 20054 } 20055 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 20056 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20057 if (!Res || (!DeclareTargetNesting.empty() && 20058 *AttrLoc == DeclareTargetNesting.back())) { 20059 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20060 Context, MT, DT, DeclareTargetNesting.size() + 1, 20061 SourceRange(Loc, Loc)); 20062 ND->addAttr(A); 20063 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20064 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 20065 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 20066 } else if (*Res != MT) { 20067 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 20068 } 20069 } 20070 20071 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 20072 Sema &SemaRef, Decl *D) { 20073 if (!D || !isa<VarDecl>(D)) 20074 return; 20075 auto *VD = cast<VarDecl>(D); 20076 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 20077 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20078 if (SemaRef.LangOpts.OpenMP >= 50 && 20079 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 20080 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 20081 VD->hasGlobalStorage()) { 20082 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 20083 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20084 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 20085 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 20086 // If a lambda declaration and definition appears between a 20087 // declare target directive and the matching end declare target 20088 // directive, all variables that are captured by the lambda 20089 // expression must also appear in a to clause. 20090 SemaRef.Diag(VD->getLocation(), 20091 diag::err_omp_lambda_capture_in_declare_target_not_to); 20092 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 20093 << VD << 0 << SR; 20094 return; 20095 } 20096 } 20097 if (MapTy.hasValue()) 20098 return; 20099 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 20100 SemaRef.Diag(SL, diag::note_used_here) << SR; 20101 } 20102 20103 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 20104 Sema &SemaRef, DSAStackTy *Stack, 20105 ValueDecl *VD) { 20106 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 20107 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 20108 /*FullCheck=*/false); 20109 } 20110 20111 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 20112 SourceLocation IdLoc) { 20113 if (!D || D->isInvalidDecl()) 20114 return; 20115 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 20116 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 20117 if (auto *VD = dyn_cast<VarDecl>(D)) { 20118 // Only global variables can be marked as declare target. 20119 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 20120 !VD->isStaticDataMember()) 20121 return; 20122 // 2.10.6: threadprivate variable cannot appear in a declare target 20123 // directive. 20124 if (DSAStack->isThreadPrivate(VD)) { 20125 Diag(SL, diag::err_omp_threadprivate_in_target); 20126 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 20127 return; 20128 } 20129 } 20130 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 20131 D = FTD->getTemplatedDecl(); 20132 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 20133 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 20134 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 20135 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 20136 Diag(IdLoc, diag::err_omp_function_in_link_clause); 20137 Diag(FD->getLocation(), diag::note_defined_here) << FD; 20138 return; 20139 } 20140 } 20141 if (auto *VD = dyn_cast<ValueDecl>(D)) { 20142 // Problem if any with var declared with incomplete type will be reported 20143 // as normal, so no need to check it here. 20144 if ((E || !VD->getType()->isIncompleteType()) && 20145 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 20146 return; 20147 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 20148 // Checking declaration inside declare target region. 20149 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 20150 isa<FunctionTemplateDecl>(D)) { 20151 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20152 Context, OMPDeclareTargetDeclAttr::MT_To, 20153 OMPDeclareTargetDeclAttr::DT_Any, DeclareTargetNesting.size(), 20154 SourceRange(DeclareTargetNesting.back(), 20155 DeclareTargetNesting.back())); 20156 D->addAttr(A); 20157 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20158 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 20159 } 20160 return; 20161 } 20162 } 20163 if (!E) 20164 return; 20165 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 20166 } 20167 20168 OMPClause *Sema::ActOnOpenMPToClause( 20169 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20170 ArrayRef<SourceLocation> MotionModifiersLoc, 20171 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20172 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20173 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20174 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20175 OMPC_MOTION_MODIFIER_unknown}; 20176 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20177 20178 // Process motion-modifiers, flag errors for duplicate modifiers. 20179 unsigned Count = 0; 20180 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20181 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20182 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 20183 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20184 continue; 20185 } 20186 assert(Count < NumberOfOMPMotionModifiers && 20187 "Modifiers exceed the allowed number of motion modifiers"); 20188 Modifiers[Count] = MotionModifiers[I]; 20189 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20190 ++Count; 20191 } 20192 20193 MappableVarListInfo MVLI(VarList); 20194 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 20195 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20196 if (MVLI.ProcessedVarList.empty()) 20197 return nullptr; 20198 20199 return OMPToClause::Create( 20200 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20201 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20202 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20203 } 20204 20205 OMPClause *Sema::ActOnOpenMPFromClause( 20206 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20207 ArrayRef<SourceLocation> MotionModifiersLoc, 20208 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20209 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20210 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20211 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20212 OMPC_MOTION_MODIFIER_unknown}; 20213 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20214 20215 // Process motion-modifiers, flag errors for duplicate modifiers. 20216 unsigned Count = 0; 20217 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20218 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20219 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 20220 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20221 continue; 20222 } 20223 assert(Count < NumberOfOMPMotionModifiers && 20224 "Modifiers exceed the allowed number of motion modifiers"); 20225 Modifiers[Count] = MotionModifiers[I]; 20226 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20227 ++Count; 20228 } 20229 20230 MappableVarListInfo MVLI(VarList); 20231 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 20232 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20233 if (MVLI.ProcessedVarList.empty()) 20234 return nullptr; 20235 20236 return OMPFromClause::Create( 20237 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20238 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20239 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20240 } 20241 20242 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 20243 const OMPVarListLocTy &Locs) { 20244 MappableVarListInfo MVLI(VarList); 20245 SmallVector<Expr *, 8> PrivateCopies; 20246 SmallVector<Expr *, 8> Inits; 20247 20248 for (Expr *RefExpr : VarList) { 20249 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 20250 SourceLocation ELoc; 20251 SourceRange ERange; 20252 Expr *SimpleRefExpr = RefExpr; 20253 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20254 if (Res.second) { 20255 // It will be analyzed later. 20256 MVLI.ProcessedVarList.push_back(RefExpr); 20257 PrivateCopies.push_back(nullptr); 20258 Inits.push_back(nullptr); 20259 } 20260 ValueDecl *D = Res.first; 20261 if (!D) 20262 continue; 20263 20264 QualType Type = D->getType(); 20265 Type = Type.getNonReferenceType().getUnqualifiedType(); 20266 20267 auto *VD = dyn_cast<VarDecl>(D); 20268 20269 // Item should be a pointer or reference to pointer. 20270 if (!Type->isPointerType()) { 20271 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 20272 << 0 << RefExpr->getSourceRange(); 20273 continue; 20274 } 20275 20276 // Build the private variable and the expression that refers to it. 20277 auto VDPrivate = 20278 buildVarDecl(*this, ELoc, Type, D->getName(), 20279 D->hasAttrs() ? &D->getAttrs() : nullptr, 20280 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 20281 if (VDPrivate->isInvalidDecl()) 20282 continue; 20283 20284 CurContext->addDecl(VDPrivate); 20285 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 20286 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 20287 20288 // Add temporary variable to initialize the private copy of the pointer. 20289 VarDecl *VDInit = 20290 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 20291 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 20292 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 20293 AddInitializerToDecl(VDPrivate, 20294 DefaultLvalueConversion(VDInitRefExpr).get(), 20295 /*DirectInit=*/false); 20296 20297 // If required, build a capture to implement the privatization initialized 20298 // with the current list item value. 20299 DeclRefExpr *Ref = nullptr; 20300 if (!VD) 20301 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20302 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20303 PrivateCopies.push_back(VDPrivateRefExpr); 20304 Inits.push_back(VDInitRefExpr); 20305 20306 // We need to add a data sharing attribute for this variable to make sure it 20307 // is correctly captured. A variable that shows up in a use_device_ptr has 20308 // similar properties of a first private variable. 20309 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20310 20311 // Create a mappable component for the list item. List items in this clause 20312 // only need a component. 20313 MVLI.VarBaseDeclarations.push_back(D); 20314 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 20315 MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D, 20316 /*IsNonContiguous=*/false); 20317 } 20318 20319 if (MVLI.ProcessedVarList.empty()) 20320 return nullptr; 20321 20322 return OMPUseDevicePtrClause::Create( 20323 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 20324 MVLI.VarBaseDeclarations, MVLI.VarComponents); 20325 } 20326 20327 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 20328 const OMPVarListLocTy &Locs) { 20329 MappableVarListInfo MVLI(VarList); 20330 20331 for (Expr *RefExpr : VarList) { 20332 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 20333 SourceLocation ELoc; 20334 SourceRange ERange; 20335 Expr *SimpleRefExpr = RefExpr; 20336 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20337 /*AllowArraySection=*/true); 20338 if (Res.second) { 20339 // It will be analyzed later. 20340 MVLI.ProcessedVarList.push_back(RefExpr); 20341 } 20342 ValueDecl *D = Res.first; 20343 if (!D) 20344 continue; 20345 auto *VD = dyn_cast<VarDecl>(D); 20346 20347 // If required, build a capture to implement the privatization initialized 20348 // with the current list item value. 20349 DeclRefExpr *Ref = nullptr; 20350 if (!VD) 20351 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20352 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20353 20354 // We need to add a data sharing attribute for this variable to make sure it 20355 // is correctly captured. A variable that shows up in a use_device_addr has 20356 // similar properties of a first private variable. 20357 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20358 20359 // Create a mappable component for the list item. List items in this clause 20360 // only need a component. 20361 MVLI.VarBaseDeclarations.push_back(D); 20362 MVLI.VarComponents.emplace_back(); 20363 Expr *Component = SimpleRefExpr; 20364 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 20365 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 20366 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 20367 MVLI.VarComponents.back().emplace_back(Component, D, 20368 /*IsNonContiguous=*/false); 20369 } 20370 20371 if (MVLI.ProcessedVarList.empty()) 20372 return nullptr; 20373 20374 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 20375 MVLI.VarBaseDeclarations, 20376 MVLI.VarComponents); 20377 } 20378 20379 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 20380 const OMPVarListLocTy &Locs) { 20381 MappableVarListInfo MVLI(VarList); 20382 for (Expr *RefExpr : VarList) { 20383 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 20384 SourceLocation ELoc; 20385 SourceRange ERange; 20386 Expr *SimpleRefExpr = RefExpr; 20387 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20388 if (Res.second) { 20389 // It will be analyzed later. 20390 MVLI.ProcessedVarList.push_back(RefExpr); 20391 } 20392 ValueDecl *D = Res.first; 20393 if (!D) 20394 continue; 20395 20396 QualType Type = D->getType(); 20397 // item should be a pointer or array or reference to pointer or array 20398 if (!Type.getNonReferenceType()->isPointerType() && 20399 !Type.getNonReferenceType()->isArrayType()) { 20400 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 20401 << 0 << RefExpr->getSourceRange(); 20402 continue; 20403 } 20404 20405 // Check if the declaration in the clause does not show up in any data 20406 // sharing attribute. 20407 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 20408 if (isOpenMPPrivate(DVar.CKind)) { 20409 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 20410 << getOpenMPClauseName(DVar.CKind) 20411 << getOpenMPClauseName(OMPC_is_device_ptr) 20412 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 20413 reportOriginalDsa(*this, DSAStack, D, DVar); 20414 continue; 20415 } 20416 20417 const Expr *ConflictExpr; 20418 if (DSAStack->checkMappableExprComponentListsForDecl( 20419 D, /*CurrentRegionOnly=*/true, 20420 [&ConflictExpr]( 20421 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 20422 OpenMPClauseKind) -> bool { 20423 ConflictExpr = R.front().getAssociatedExpression(); 20424 return true; 20425 })) { 20426 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 20427 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 20428 << ConflictExpr->getSourceRange(); 20429 continue; 20430 } 20431 20432 // Store the components in the stack so that they can be used to check 20433 // against other clauses later on. 20434 OMPClauseMappableExprCommon::MappableComponent MC( 20435 SimpleRefExpr, D, /*IsNonContiguous=*/false); 20436 DSAStack->addMappableExpressionComponents( 20437 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 20438 20439 // Record the expression we've just processed. 20440 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 20441 20442 // Create a mappable component for the list item. List items in this clause 20443 // only need a component. We use a null declaration to signal fields in 20444 // 'this'. 20445 assert((isa<DeclRefExpr>(SimpleRefExpr) || 20446 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 20447 "Unexpected device pointer expression!"); 20448 MVLI.VarBaseDeclarations.push_back( 20449 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 20450 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 20451 MVLI.VarComponents.back().push_back(MC); 20452 } 20453 20454 if (MVLI.ProcessedVarList.empty()) 20455 return nullptr; 20456 20457 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 20458 MVLI.VarBaseDeclarations, 20459 MVLI.VarComponents); 20460 } 20461 20462 OMPClause *Sema::ActOnOpenMPAllocateClause( 20463 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 20464 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 20465 if (Allocator) { 20466 // OpenMP [2.11.4 allocate Clause, Description] 20467 // allocator is an expression of omp_allocator_handle_t type. 20468 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 20469 return nullptr; 20470 20471 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 20472 if (AllocatorRes.isInvalid()) 20473 return nullptr; 20474 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 20475 DSAStack->getOMPAllocatorHandleT(), 20476 Sema::AA_Initializing, 20477 /*AllowExplicit=*/true); 20478 if (AllocatorRes.isInvalid()) 20479 return nullptr; 20480 Allocator = AllocatorRes.get(); 20481 } else { 20482 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 20483 // allocate clauses that appear on a target construct or on constructs in a 20484 // target region must specify an allocator expression unless a requires 20485 // directive with the dynamic_allocators clause is present in the same 20486 // compilation unit. 20487 if (LangOpts.OpenMPIsDevice && 20488 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 20489 targetDiag(StartLoc, diag::err_expected_allocator_expression); 20490 } 20491 // Analyze and build list of variables. 20492 SmallVector<Expr *, 8> Vars; 20493 for (Expr *RefExpr : VarList) { 20494 assert(RefExpr && "NULL expr in OpenMP private clause."); 20495 SourceLocation ELoc; 20496 SourceRange ERange; 20497 Expr *SimpleRefExpr = RefExpr; 20498 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20499 if (Res.second) { 20500 // It will be analyzed later. 20501 Vars.push_back(RefExpr); 20502 } 20503 ValueDecl *D = Res.first; 20504 if (!D) 20505 continue; 20506 20507 auto *VD = dyn_cast<VarDecl>(D); 20508 DeclRefExpr *Ref = nullptr; 20509 if (!VD && !CurContext->isDependentContext()) 20510 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 20511 Vars.push_back((VD || CurContext->isDependentContext()) 20512 ? RefExpr->IgnoreParens() 20513 : Ref); 20514 } 20515 20516 if (Vars.empty()) 20517 return nullptr; 20518 20519 if (Allocator) 20520 DSAStack->addInnerAllocatorExpr(Allocator); 20521 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 20522 ColonLoc, EndLoc, Vars); 20523 } 20524 20525 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 20526 SourceLocation StartLoc, 20527 SourceLocation LParenLoc, 20528 SourceLocation EndLoc) { 20529 SmallVector<Expr *, 8> Vars; 20530 for (Expr *RefExpr : VarList) { 20531 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 20532 SourceLocation ELoc; 20533 SourceRange ERange; 20534 Expr *SimpleRefExpr = RefExpr; 20535 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20536 if (Res.second) 20537 // It will be analyzed later. 20538 Vars.push_back(RefExpr); 20539 ValueDecl *D = Res.first; 20540 if (!D) 20541 continue; 20542 20543 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 20544 // A list-item cannot appear in more than one nontemporal clause. 20545 if (const Expr *PrevRef = 20546 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 20547 Diag(ELoc, diag::err_omp_used_in_clause_twice) 20548 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 20549 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 20550 << getOpenMPClauseName(OMPC_nontemporal); 20551 continue; 20552 } 20553 20554 Vars.push_back(RefExpr); 20555 } 20556 20557 if (Vars.empty()) 20558 return nullptr; 20559 20560 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 20561 Vars); 20562 } 20563 20564 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 20565 SourceLocation StartLoc, 20566 SourceLocation LParenLoc, 20567 SourceLocation EndLoc) { 20568 SmallVector<Expr *, 8> Vars; 20569 for (Expr *RefExpr : VarList) { 20570 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 20571 SourceLocation ELoc; 20572 SourceRange ERange; 20573 Expr *SimpleRefExpr = RefExpr; 20574 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20575 /*AllowArraySection=*/true); 20576 if (Res.second) 20577 // It will be analyzed later. 20578 Vars.push_back(RefExpr); 20579 ValueDecl *D = Res.first; 20580 if (!D) 20581 continue; 20582 20583 const DSAStackTy::DSAVarData DVar = 20584 DSAStack->getTopDSA(D, /*FromParent=*/true); 20585 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 20586 // A list item that appears in the inclusive or exclusive clause must appear 20587 // in a reduction clause with the inscan modifier on the enclosing 20588 // worksharing-loop, worksharing-loop SIMD, or simd construct. 20589 if (DVar.CKind != OMPC_reduction || 20590 DVar.Modifier != OMPC_REDUCTION_inscan) 20591 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 20592 << RefExpr->getSourceRange(); 20593 20594 if (DSAStack->getParentDirective() != OMPD_unknown) 20595 DSAStack->markDeclAsUsedInScanDirective(D); 20596 Vars.push_back(RefExpr); 20597 } 20598 20599 if (Vars.empty()) 20600 return nullptr; 20601 20602 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 20603 } 20604 20605 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 20606 SourceLocation StartLoc, 20607 SourceLocation LParenLoc, 20608 SourceLocation EndLoc) { 20609 SmallVector<Expr *, 8> Vars; 20610 for (Expr *RefExpr : VarList) { 20611 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 20612 SourceLocation ELoc; 20613 SourceRange ERange; 20614 Expr *SimpleRefExpr = RefExpr; 20615 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20616 /*AllowArraySection=*/true); 20617 if (Res.second) 20618 // It will be analyzed later. 20619 Vars.push_back(RefExpr); 20620 ValueDecl *D = Res.first; 20621 if (!D) 20622 continue; 20623 20624 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 20625 DSAStackTy::DSAVarData DVar; 20626 if (ParentDirective != OMPD_unknown) 20627 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 20628 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 20629 // A list item that appears in the inclusive or exclusive clause must appear 20630 // in a reduction clause with the inscan modifier on the enclosing 20631 // worksharing-loop, worksharing-loop SIMD, or simd construct. 20632 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 20633 DVar.Modifier != OMPC_REDUCTION_inscan) { 20634 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 20635 << RefExpr->getSourceRange(); 20636 } else { 20637 DSAStack->markDeclAsUsedInScanDirective(D); 20638 } 20639 Vars.push_back(RefExpr); 20640 } 20641 20642 if (Vars.empty()) 20643 return nullptr; 20644 20645 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 20646 } 20647 20648 /// Tries to find omp_alloctrait_t type. 20649 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 20650 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 20651 if (!OMPAlloctraitT.isNull()) 20652 return true; 20653 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 20654 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 20655 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 20656 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 20657 return false; 20658 } 20659 Stack->setOMPAlloctraitT(PT.get()); 20660 return true; 20661 } 20662 20663 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 20664 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 20665 ArrayRef<UsesAllocatorsData> Data) { 20666 // OpenMP [2.12.5, target Construct] 20667 // allocator is an identifier of omp_allocator_handle_t type. 20668 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 20669 return nullptr; 20670 // OpenMP [2.12.5, target Construct] 20671 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 20672 if (llvm::any_of( 20673 Data, 20674 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 20675 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 20676 return nullptr; 20677 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 20678 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 20679 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 20680 StringRef Allocator = 20681 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 20682 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 20683 PredefinedAllocators.insert(LookupSingleName( 20684 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 20685 } 20686 20687 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 20688 for (const UsesAllocatorsData &D : Data) { 20689 Expr *AllocatorExpr = nullptr; 20690 // Check allocator expression. 20691 if (D.Allocator->isTypeDependent()) { 20692 AllocatorExpr = D.Allocator; 20693 } else { 20694 // Traits were specified - need to assign new allocator to the specified 20695 // allocator, so it must be an lvalue. 20696 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 20697 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 20698 bool IsPredefinedAllocator = false; 20699 if (DRE) 20700 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 20701 if (!DRE || 20702 !(Context.hasSameUnqualifiedType( 20703 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 20704 Context.typesAreCompatible(AllocatorExpr->getType(), 20705 DSAStack->getOMPAllocatorHandleT(), 20706 /*CompareUnqualified=*/true)) || 20707 (!IsPredefinedAllocator && 20708 (AllocatorExpr->getType().isConstant(Context) || 20709 !AllocatorExpr->isLValue()))) { 20710 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 20711 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 20712 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 20713 continue; 20714 } 20715 // OpenMP [2.12.5, target Construct] 20716 // Predefined allocators appearing in a uses_allocators clause cannot have 20717 // traits specified. 20718 if (IsPredefinedAllocator && D.AllocatorTraits) { 20719 Diag(D.AllocatorTraits->getExprLoc(), 20720 diag::err_omp_predefined_allocator_with_traits) 20721 << D.AllocatorTraits->getSourceRange(); 20722 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 20723 << cast<NamedDecl>(DRE->getDecl())->getName() 20724 << D.Allocator->getSourceRange(); 20725 continue; 20726 } 20727 // OpenMP [2.12.5, target Construct] 20728 // Non-predefined allocators appearing in a uses_allocators clause must 20729 // have traits specified. 20730 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 20731 Diag(D.Allocator->getExprLoc(), 20732 diag::err_omp_nonpredefined_allocator_without_traits); 20733 continue; 20734 } 20735 // No allocator traits - just convert it to rvalue. 20736 if (!D.AllocatorTraits) 20737 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 20738 DSAStack->addUsesAllocatorsDecl( 20739 DRE->getDecl(), 20740 IsPredefinedAllocator 20741 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 20742 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 20743 } 20744 Expr *AllocatorTraitsExpr = nullptr; 20745 if (D.AllocatorTraits) { 20746 if (D.AllocatorTraits->isTypeDependent()) { 20747 AllocatorTraitsExpr = D.AllocatorTraits; 20748 } else { 20749 // OpenMP [2.12.5, target Construct] 20750 // Arrays that contain allocator traits that appear in a uses_allocators 20751 // clause must be constant arrays, have constant values and be defined 20752 // in the same scope as the construct in which the clause appears. 20753 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 20754 // Check that traits expr is a constant array. 20755 QualType TraitTy; 20756 if (const ArrayType *Ty = 20757 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 20758 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 20759 TraitTy = ConstArrayTy->getElementType(); 20760 if (TraitTy.isNull() || 20761 !(Context.hasSameUnqualifiedType(TraitTy, 20762 DSAStack->getOMPAlloctraitT()) || 20763 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 20764 /*CompareUnqualified=*/true))) { 20765 Diag(D.AllocatorTraits->getExprLoc(), 20766 diag::err_omp_expected_array_alloctraits) 20767 << AllocatorTraitsExpr->getType(); 20768 continue; 20769 } 20770 // Do not map by default allocator traits if it is a standalone 20771 // variable. 20772 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 20773 DSAStack->addUsesAllocatorsDecl( 20774 DRE->getDecl(), 20775 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 20776 } 20777 } 20778 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 20779 NewD.Allocator = AllocatorExpr; 20780 NewD.AllocatorTraits = AllocatorTraitsExpr; 20781 NewD.LParenLoc = D.LParenLoc; 20782 NewD.RParenLoc = D.RParenLoc; 20783 } 20784 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 20785 NewData); 20786 } 20787 20788 OMPClause *Sema::ActOnOpenMPAffinityClause( 20789 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 20790 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 20791 SmallVector<Expr *, 8> Vars; 20792 for (Expr *RefExpr : Locators) { 20793 assert(RefExpr && "NULL expr in OpenMP shared clause."); 20794 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 20795 // It will be analyzed later. 20796 Vars.push_back(RefExpr); 20797 continue; 20798 } 20799 20800 SourceLocation ELoc = RefExpr->getExprLoc(); 20801 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 20802 20803 if (!SimpleExpr->isLValue()) { 20804 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 20805 << 1 << 0 << RefExpr->getSourceRange(); 20806 continue; 20807 } 20808 20809 ExprResult Res; 20810 { 20811 Sema::TentativeAnalysisScope Trap(*this); 20812 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 20813 } 20814 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 20815 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 20816 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 20817 << 1 << 0 << RefExpr->getSourceRange(); 20818 continue; 20819 } 20820 Vars.push_back(SimpleExpr); 20821 } 20822 20823 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 20824 EndLoc, Modifier, Vars); 20825 } 20826