1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/OpenMPClause.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/DiagnosticSema.h" 27 #include "clang/Basic/OpenMPKinds.h" 28 #include "clang/Basic/PartialDiagnostic.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Sema/Initialization.h" 31 #include "clang/Sema/Lookup.h" 32 #include "clang/Sema/Scope.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/SemaInternal.h" 35 #include "llvm/ADT/IndexedMap.h" 36 #include "llvm/ADT/PointerEmbeddedInt.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/ADT/StringExtras.h" 39 #include "llvm/Frontend/OpenMP/OMPConstants.h" 40 #include <set> 41 42 using namespace clang; 43 using namespace llvm::omp; 44 45 //===----------------------------------------------------------------------===// 46 // Stack of data-sharing attributes for variables 47 //===----------------------------------------------------------------------===// 48 49 static const Expr *checkMapClauseExpressionBase( 50 Sema &SemaRef, Expr *E, 51 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 52 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose); 53 54 namespace { 55 /// Default data sharing attributes, which can be applied to directive. 56 enum DefaultDataSharingAttributes { 57 DSA_unspecified = 0, /// Data sharing attribute not specified. 58 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 59 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 60 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 61 }; 62 63 /// Stack for tracking declarations used in OpenMP directives and 64 /// clauses and their data-sharing attributes. 65 class DSAStackTy { 66 public: 67 struct DSAVarData { 68 OpenMPDirectiveKind DKind = OMPD_unknown; 69 OpenMPClauseKind CKind = OMPC_unknown; 70 unsigned Modifier = 0; 71 const Expr *RefExpr = nullptr; 72 DeclRefExpr *PrivateCopy = nullptr; 73 SourceLocation ImplicitDSALoc; 74 bool AppliedToPointee = false; 75 DSAVarData() = default; 76 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 77 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 78 SourceLocation ImplicitDSALoc, unsigned Modifier, 79 bool AppliedToPointee) 80 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 81 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc), 82 AppliedToPointee(AppliedToPointee) {} 83 }; 84 using OperatorOffsetTy = 85 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 86 using DoacrossDependMapTy = 87 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 88 /// Kind of the declaration used in the uses_allocators clauses. 89 enum class UsesAllocatorsDeclKind { 90 /// Predefined allocator 91 PredefinedAllocator, 92 /// User-defined allocator 93 UserDefinedAllocator, 94 /// The declaration that represent allocator trait 95 AllocatorTrait, 96 }; 97 98 private: 99 struct DSAInfo { 100 OpenMPClauseKind Attributes = OMPC_unknown; 101 unsigned Modifier = 0; 102 /// Pointer to a reference expression and a flag which shows that the 103 /// variable is marked as lastprivate(true) or not (false). 104 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 105 DeclRefExpr *PrivateCopy = nullptr; 106 /// true if the attribute is applied to the pointee, not the variable 107 /// itself. 108 bool AppliedToPointee = false; 109 }; 110 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 111 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 112 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 113 using LoopControlVariablesMapTy = 114 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 115 /// Struct that associates a component with the clause kind where they are 116 /// found. 117 struct MappedExprComponentTy { 118 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 119 OpenMPClauseKind Kind = OMPC_unknown; 120 }; 121 using MappedExprComponentsTy = 122 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 123 using CriticalsWithHintsTy = 124 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 125 struct ReductionData { 126 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 127 SourceRange ReductionRange; 128 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 129 ReductionData() = default; 130 void set(BinaryOperatorKind BO, SourceRange RR) { 131 ReductionRange = RR; 132 ReductionOp = BO; 133 } 134 void set(const Expr *RefExpr, SourceRange RR) { 135 ReductionRange = RR; 136 ReductionOp = RefExpr; 137 } 138 }; 139 using DeclReductionMapTy = 140 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 141 struct DefaultmapInfo { 142 OpenMPDefaultmapClauseModifier ImplicitBehavior = 143 OMPC_DEFAULTMAP_MODIFIER_unknown; 144 SourceLocation SLoc; 145 DefaultmapInfo() = default; 146 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 147 : ImplicitBehavior(M), SLoc(Loc) {} 148 }; 149 150 struct SharingMapTy { 151 DeclSAMapTy SharingMap; 152 DeclReductionMapTy ReductionMap; 153 UsedRefMapTy AlignedMap; 154 UsedRefMapTy NontemporalMap; 155 MappedExprComponentsTy MappedExprComponents; 156 LoopControlVariablesMapTy LCVMap; 157 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 158 SourceLocation DefaultAttrLoc; 159 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 160 OpenMPDirectiveKind Directive = OMPD_unknown; 161 DeclarationNameInfo DirectiveName; 162 Scope *CurScope = nullptr; 163 DeclContext *Context = nullptr; 164 SourceLocation ConstructLoc; 165 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 166 /// get the data (loop counters etc.) about enclosing loop-based construct. 167 /// This data is required during codegen. 168 DoacrossDependMapTy DoacrossDepends; 169 /// First argument (Expr *) contains optional argument of the 170 /// 'ordered' clause, the second one is true if the regions has 'ordered' 171 /// clause, false otherwise. 172 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 173 unsigned AssociatedLoops = 1; 174 bool HasMutipleLoops = false; 175 const Decl *PossiblyLoopCounter = nullptr; 176 bool NowaitRegion = false; 177 bool CancelRegion = false; 178 bool LoopStart = false; 179 bool BodyComplete = false; 180 SourceLocation PrevScanLocation; 181 SourceLocation PrevOrderedLocation; 182 SourceLocation InnerTeamsRegionLoc; 183 /// Reference to the taskgroup task_reduction reference expression. 184 Expr *TaskgroupReductionRef = nullptr; 185 llvm::DenseSet<QualType> MappedClassesQualTypes; 186 SmallVector<Expr *, 4> InnerUsedAllocators; 187 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 188 /// List of globals marked as declare target link in this target region 189 /// (isOpenMPTargetExecutionDirective(Directive) == true). 190 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 191 /// List of decls used in inclusive/exclusive clauses of the scan directive. 192 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 193 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 194 UsesAllocatorsDecls; 195 Expr *DeclareMapperVar = nullptr; 196 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 197 Scope *CurScope, SourceLocation Loc) 198 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 199 ConstructLoc(Loc) {} 200 SharingMapTy() = default; 201 }; 202 203 using StackTy = SmallVector<SharingMapTy, 4>; 204 205 /// Stack of used declaration and their data-sharing attributes. 206 DeclSAMapTy Threadprivates; 207 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 208 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 209 /// true, if check for DSA must be from parent directive, false, if 210 /// from current directive. 211 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 212 Sema &SemaRef; 213 bool ForceCapturing = false; 214 /// true if all the variables in the target executable directives must be 215 /// captured by reference. 216 bool ForceCaptureByReferenceInTargetExecutable = false; 217 CriticalsWithHintsTy Criticals; 218 unsigned IgnoredStackElements = 0; 219 220 /// Iterators over the stack iterate in order from innermost to outermost 221 /// directive. 222 using const_iterator = StackTy::const_reverse_iterator; 223 const_iterator begin() const { 224 return Stack.empty() ? const_iterator() 225 : Stack.back().first.rbegin() + IgnoredStackElements; 226 } 227 const_iterator end() const { 228 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 229 } 230 using iterator = StackTy::reverse_iterator; 231 iterator begin() { 232 return Stack.empty() ? iterator() 233 : Stack.back().first.rbegin() + IgnoredStackElements; 234 } 235 iterator end() { 236 return Stack.empty() ? iterator() : Stack.back().first.rend(); 237 } 238 239 // Convenience operations to get at the elements of the stack. 240 241 bool isStackEmpty() const { 242 return Stack.empty() || 243 Stack.back().second != CurrentNonCapturingFunctionScope || 244 Stack.back().first.size() <= IgnoredStackElements; 245 } 246 size_t getStackSize() const { 247 return isStackEmpty() ? 0 248 : Stack.back().first.size() - IgnoredStackElements; 249 } 250 251 SharingMapTy *getTopOfStackOrNull() { 252 size_t Size = getStackSize(); 253 if (Size == 0) 254 return nullptr; 255 return &Stack.back().first[Size - 1]; 256 } 257 const SharingMapTy *getTopOfStackOrNull() const { 258 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 259 } 260 SharingMapTy &getTopOfStack() { 261 assert(!isStackEmpty() && "no current directive"); 262 return *getTopOfStackOrNull(); 263 } 264 const SharingMapTy &getTopOfStack() const { 265 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 266 } 267 268 SharingMapTy *getSecondOnStackOrNull() { 269 size_t Size = getStackSize(); 270 if (Size <= 1) 271 return nullptr; 272 return &Stack.back().first[Size - 2]; 273 } 274 const SharingMapTy *getSecondOnStackOrNull() const { 275 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 276 } 277 278 /// Get the stack element at a certain level (previously returned by 279 /// \c getNestingLevel). 280 /// 281 /// Note that nesting levels count from outermost to innermost, and this is 282 /// the reverse of our iteration order where new inner levels are pushed at 283 /// the front of the stack. 284 SharingMapTy &getStackElemAtLevel(unsigned Level) { 285 assert(Level < getStackSize() && "no such stack element"); 286 return Stack.back().first[Level]; 287 } 288 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 289 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 290 } 291 292 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 293 294 /// Checks if the variable is a local for OpenMP region. 295 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 296 297 /// Vector of previously declared requires directives 298 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 299 /// omp_allocator_handle_t type. 300 QualType OMPAllocatorHandleT; 301 /// omp_depend_t type. 302 QualType OMPDependT; 303 /// omp_event_handle_t type. 304 QualType OMPEventHandleT; 305 /// omp_alloctrait_t type. 306 QualType OMPAlloctraitT; 307 /// Expression for the predefined allocators. 308 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 309 nullptr}; 310 /// Vector of previously encountered target directives 311 SmallVector<SourceLocation, 2> TargetLocations; 312 SourceLocation AtomicLocation; 313 /// Vector of declare variant construct traits. 314 SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits; 315 316 public: 317 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 318 319 /// Sets omp_allocator_handle_t type. 320 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 321 /// Gets omp_allocator_handle_t type. 322 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 323 /// Sets omp_alloctrait_t type. 324 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 325 /// Gets omp_alloctrait_t type. 326 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 327 /// Sets the given default allocator. 328 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 329 Expr *Allocator) { 330 OMPPredefinedAllocators[AllocatorKind] = Allocator; 331 } 332 /// Returns the specified default allocator. 333 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 334 return OMPPredefinedAllocators[AllocatorKind]; 335 } 336 /// Sets omp_depend_t type. 337 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 338 /// Gets omp_depend_t type. 339 QualType getOMPDependT() const { return OMPDependT; } 340 341 /// Sets omp_event_handle_t type. 342 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 343 /// Gets omp_event_handle_t type. 344 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 345 346 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 347 OpenMPClauseKind getClauseParsingMode() const { 348 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 349 return ClauseKindMode; 350 } 351 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 352 353 bool isBodyComplete() const { 354 const SharingMapTy *Top = getTopOfStackOrNull(); 355 return Top && Top->BodyComplete; 356 } 357 void setBodyComplete() { 358 getTopOfStack().BodyComplete = true; 359 } 360 361 bool isForceVarCapturing() const { return ForceCapturing; } 362 void setForceVarCapturing(bool V) { ForceCapturing = V; } 363 364 void setForceCaptureByReferenceInTargetExecutable(bool V) { 365 ForceCaptureByReferenceInTargetExecutable = V; 366 } 367 bool isForceCaptureByReferenceInTargetExecutable() const { 368 return ForceCaptureByReferenceInTargetExecutable; 369 } 370 371 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 372 Scope *CurScope, SourceLocation Loc) { 373 assert(!IgnoredStackElements && 374 "cannot change stack while ignoring elements"); 375 if (Stack.empty() || 376 Stack.back().second != CurrentNonCapturingFunctionScope) 377 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 378 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 379 Stack.back().first.back().DefaultAttrLoc = Loc; 380 } 381 382 void pop() { 383 assert(!IgnoredStackElements && 384 "cannot change stack while ignoring elements"); 385 assert(!Stack.back().first.empty() && 386 "Data-sharing attributes stack is empty!"); 387 Stack.back().first.pop_back(); 388 } 389 390 /// RAII object to temporarily leave the scope of a directive when we want to 391 /// logically operate in its parent. 392 class ParentDirectiveScope { 393 DSAStackTy &Self; 394 bool Active; 395 public: 396 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 397 : Self(Self), Active(false) { 398 if (Activate) 399 enable(); 400 } 401 ~ParentDirectiveScope() { disable(); } 402 void disable() { 403 if (Active) { 404 --Self.IgnoredStackElements; 405 Active = false; 406 } 407 } 408 void enable() { 409 if (!Active) { 410 ++Self.IgnoredStackElements; 411 Active = true; 412 } 413 } 414 }; 415 416 /// Marks that we're started loop parsing. 417 void loopInit() { 418 assert(isOpenMPLoopDirective(getCurrentDirective()) && 419 "Expected loop-based directive."); 420 getTopOfStack().LoopStart = true; 421 } 422 /// Start capturing of the variables in the loop context. 423 void loopStart() { 424 assert(isOpenMPLoopDirective(getCurrentDirective()) && 425 "Expected loop-based directive."); 426 getTopOfStack().LoopStart = false; 427 } 428 /// true, if variables are captured, false otherwise. 429 bool isLoopStarted() const { 430 assert(isOpenMPLoopDirective(getCurrentDirective()) && 431 "Expected loop-based directive."); 432 return !getTopOfStack().LoopStart; 433 } 434 /// Marks (or clears) declaration as possibly loop counter. 435 void resetPossibleLoopCounter(const Decl *D = nullptr) { 436 getTopOfStack().PossiblyLoopCounter = 437 D ? D->getCanonicalDecl() : D; 438 } 439 /// Gets the possible loop counter decl. 440 const Decl *getPossiblyLoopCunter() const { 441 return getTopOfStack().PossiblyLoopCounter; 442 } 443 /// Start new OpenMP region stack in new non-capturing function. 444 void pushFunction() { 445 assert(!IgnoredStackElements && 446 "cannot change stack while ignoring elements"); 447 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 448 assert(!isa<CapturingScopeInfo>(CurFnScope)); 449 CurrentNonCapturingFunctionScope = CurFnScope; 450 } 451 /// Pop region stack for non-capturing function. 452 void popFunction(const FunctionScopeInfo *OldFSI) { 453 assert(!IgnoredStackElements && 454 "cannot change stack while ignoring elements"); 455 if (!Stack.empty() && Stack.back().second == OldFSI) { 456 assert(Stack.back().first.empty()); 457 Stack.pop_back(); 458 } 459 CurrentNonCapturingFunctionScope = nullptr; 460 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 461 if (!isa<CapturingScopeInfo>(FSI)) { 462 CurrentNonCapturingFunctionScope = FSI; 463 break; 464 } 465 } 466 } 467 468 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 469 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 470 } 471 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 472 getCriticalWithHint(const DeclarationNameInfo &Name) const { 473 auto I = Criticals.find(Name.getAsString()); 474 if (I != Criticals.end()) 475 return I->second; 476 return std::make_pair(nullptr, llvm::APSInt()); 477 } 478 /// If 'aligned' declaration for given variable \a D was not seen yet, 479 /// add it and return NULL; otherwise return previous occurrence's expression 480 /// for diagnostics. 481 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 482 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 483 /// add it and return NULL; otherwise return previous occurrence's expression 484 /// for diagnostics. 485 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 486 487 /// Register specified variable as loop control variable. 488 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 489 /// Check if the specified variable is a loop control variable for 490 /// current region. 491 /// \return The index of the loop control variable in the list of associated 492 /// for-loops (from outer to inner). 493 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 494 /// Check if the specified variable is a loop control variable for 495 /// parent region. 496 /// \return The index of the loop control variable in the list of associated 497 /// for-loops (from outer to inner). 498 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 499 /// Check if the specified variable is a loop control variable for 500 /// current region. 501 /// \return The index of the loop control variable in the list of associated 502 /// for-loops (from outer to inner). 503 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 504 unsigned Level) const; 505 /// Get the loop control variable for the I-th loop (or nullptr) in 506 /// parent directive. 507 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 508 509 /// Marks the specified decl \p D as used in scan directive. 510 void markDeclAsUsedInScanDirective(ValueDecl *D) { 511 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 512 Stack->UsedInScanDirective.insert(D); 513 } 514 515 /// Checks if the specified declaration was used in the inner scan directive. 516 bool isUsedInScanDirective(ValueDecl *D) const { 517 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 518 return Stack->UsedInScanDirective.count(D) > 0; 519 return false; 520 } 521 522 /// Adds explicit data sharing attribute to the specified declaration. 523 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 524 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0, 525 bool AppliedToPointee = false); 526 527 /// Adds additional information for the reduction items with the reduction id 528 /// represented as an operator. 529 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 530 BinaryOperatorKind BOK); 531 /// Adds additional information for the reduction items with the reduction id 532 /// represented as reduction identifier. 533 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 534 const Expr *ReductionRef); 535 /// Returns the location and reduction operation from the innermost parent 536 /// region for the given \p D. 537 const DSAVarData 538 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 539 BinaryOperatorKind &BOK, 540 Expr *&TaskgroupDescriptor) const; 541 /// Returns the location and reduction operation from the innermost parent 542 /// region for the given \p D. 543 const DSAVarData 544 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 545 const Expr *&ReductionRef, 546 Expr *&TaskgroupDescriptor) const; 547 /// Return reduction reference expression for the current taskgroup or 548 /// parallel/worksharing directives with task reductions. 549 Expr *getTaskgroupReductionRef() const { 550 assert((getTopOfStack().Directive == OMPD_taskgroup || 551 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 552 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 553 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 554 "taskgroup reference expression requested for non taskgroup or " 555 "parallel/worksharing directive."); 556 return getTopOfStack().TaskgroupReductionRef; 557 } 558 /// Checks if the given \p VD declaration is actually a taskgroup reduction 559 /// descriptor variable at the \p Level of OpenMP regions. 560 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 561 return getStackElemAtLevel(Level).TaskgroupReductionRef && 562 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 563 ->getDecl() == VD; 564 } 565 566 /// Returns data sharing attributes from top of the stack for the 567 /// specified declaration. 568 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 569 /// Returns data-sharing attributes for the specified declaration. 570 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 571 /// Returns data-sharing attributes for the specified declaration. 572 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 573 /// Checks if the specified variables has data-sharing attributes which 574 /// match specified \a CPred predicate in any directive which matches \a DPred 575 /// predicate. 576 const DSAVarData 577 hasDSA(ValueDecl *D, 578 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 579 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 580 bool FromParent) const; 581 /// Checks if the specified variables has data-sharing attributes which 582 /// match specified \a CPred predicate in any innermost directive which 583 /// matches \a DPred predicate. 584 const DSAVarData 585 hasInnermostDSA(ValueDecl *D, 586 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 587 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 588 bool FromParent) const; 589 /// Checks if the specified variables has explicit data-sharing 590 /// attributes which match specified \a CPred predicate at the specified 591 /// OpenMP region. 592 bool 593 hasExplicitDSA(const ValueDecl *D, 594 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 595 unsigned Level, bool NotLastprivate = false) const; 596 597 /// Returns true if the directive at level \Level matches in the 598 /// specified \a DPred predicate. 599 bool hasExplicitDirective( 600 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 601 unsigned Level) const; 602 603 /// Finds a directive which matches specified \a DPred predicate. 604 bool hasDirective( 605 const llvm::function_ref<bool( 606 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 607 DPred, 608 bool FromParent) const; 609 610 /// Returns currently analyzed directive. 611 OpenMPDirectiveKind getCurrentDirective() const { 612 const SharingMapTy *Top = getTopOfStackOrNull(); 613 return Top ? Top->Directive : OMPD_unknown; 614 } 615 /// Returns directive kind at specified level. 616 OpenMPDirectiveKind getDirective(unsigned Level) const { 617 assert(!isStackEmpty() && "No directive at specified level."); 618 return getStackElemAtLevel(Level).Directive; 619 } 620 /// Returns the capture region at the specified level. 621 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 622 unsigned OpenMPCaptureLevel) const { 623 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 624 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 625 return CaptureRegions[OpenMPCaptureLevel]; 626 } 627 /// Returns parent directive. 628 OpenMPDirectiveKind getParentDirective() const { 629 const SharingMapTy *Parent = getSecondOnStackOrNull(); 630 return Parent ? Parent->Directive : OMPD_unknown; 631 } 632 633 /// Add requires decl to internal vector 634 void addRequiresDecl(OMPRequiresDecl *RD) { 635 RequiresDecls.push_back(RD); 636 } 637 638 /// Checks if the defined 'requires' directive has specified type of clause. 639 template <typename ClauseType> 640 bool hasRequiresDeclWithClause() const { 641 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 642 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 643 return isa<ClauseType>(C); 644 }); 645 }); 646 } 647 648 /// Checks for a duplicate clause amongst previously declared requires 649 /// directives 650 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 651 bool IsDuplicate = false; 652 for (OMPClause *CNew : ClauseList) { 653 for (const OMPRequiresDecl *D : RequiresDecls) { 654 for (const OMPClause *CPrev : D->clauselists()) { 655 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 656 SemaRef.Diag(CNew->getBeginLoc(), 657 diag::err_omp_requires_clause_redeclaration) 658 << getOpenMPClauseName(CNew->getClauseKind()); 659 SemaRef.Diag(CPrev->getBeginLoc(), 660 diag::note_omp_requires_previous_clause) 661 << getOpenMPClauseName(CPrev->getClauseKind()); 662 IsDuplicate = true; 663 } 664 } 665 } 666 } 667 return IsDuplicate; 668 } 669 670 /// Add location of previously encountered target to internal vector 671 void addTargetDirLocation(SourceLocation LocStart) { 672 TargetLocations.push_back(LocStart); 673 } 674 675 /// Add location for the first encountered atomicc directive. 676 void addAtomicDirectiveLoc(SourceLocation Loc) { 677 if (AtomicLocation.isInvalid()) 678 AtomicLocation = Loc; 679 } 680 681 /// Returns the location of the first encountered atomic directive in the 682 /// module. 683 SourceLocation getAtomicDirectiveLoc() const { 684 return AtomicLocation; 685 } 686 687 // Return previously encountered target region locations. 688 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 689 return TargetLocations; 690 } 691 692 /// Set default data sharing attribute to none. 693 void setDefaultDSANone(SourceLocation Loc) { 694 getTopOfStack().DefaultAttr = DSA_none; 695 getTopOfStack().DefaultAttrLoc = Loc; 696 } 697 /// Set default data sharing attribute to shared. 698 void setDefaultDSAShared(SourceLocation Loc) { 699 getTopOfStack().DefaultAttr = DSA_shared; 700 getTopOfStack().DefaultAttrLoc = Loc; 701 } 702 /// Set default data sharing attribute to firstprivate. 703 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 704 getTopOfStack().DefaultAttr = DSA_firstprivate; 705 getTopOfStack().DefaultAttrLoc = Loc; 706 } 707 /// Set default data mapping attribute to Modifier:Kind 708 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 709 OpenMPDefaultmapClauseKind Kind, 710 SourceLocation Loc) { 711 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 712 DMI.ImplicitBehavior = M; 713 DMI.SLoc = Loc; 714 } 715 /// Check whether the implicit-behavior has been set in defaultmap 716 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 717 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 718 return getTopOfStack() 719 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 720 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 721 getTopOfStack() 722 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 723 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 724 getTopOfStack() 725 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 726 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 727 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 728 OMPC_DEFAULTMAP_MODIFIER_unknown; 729 } 730 731 ArrayRef<llvm::omp::TraitProperty> getConstructTraits() { 732 return ConstructTraits; 733 } 734 void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits, 735 bool ScopeEntry) { 736 if (ScopeEntry) 737 ConstructTraits.append(Traits.begin(), Traits.end()); 738 else 739 for (llvm::omp::TraitProperty Trait : llvm::reverse(Traits)) { 740 llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val(); 741 assert(Top == Trait && "Something left a trait on the stack!"); 742 (void)Trait; 743 (void)Top; 744 } 745 } 746 747 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 748 return getStackSize() <= Level ? DSA_unspecified 749 : getStackElemAtLevel(Level).DefaultAttr; 750 } 751 DefaultDataSharingAttributes getDefaultDSA() const { 752 return isStackEmpty() ? DSA_unspecified 753 : getTopOfStack().DefaultAttr; 754 } 755 SourceLocation getDefaultDSALocation() const { 756 return isStackEmpty() ? SourceLocation() 757 : getTopOfStack().DefaultAttrLoc; 758 } 759 OpenMPDefaultmapClauseModifier 760 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 761 return isStackEmpty() 762 ? OMPC_DEFAULTMAP_MODIFIER_unknown 763 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 764 } 765 OpenMPDefaultmapClauseModifier 766 getDefaultmapModifierAtLevel(unsigned Level, 767 OpenMPDefaultmapClauseKind Kind) const { 768 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 769 } 770 bool isDefaultmapCapturedByRef(unsigned Level, 771 OpenMPDefaultmapClauseKind Kind) const { 772 OpenMPDefaultmapClauseModifier M = 773 getDefaultmapModifierAtLevel(Level, Kind); 774 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 775 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 776 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 777 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 778 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 779 } 780 return true; 781 } 782 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 783 OpenMPDefaultmapClauseKind Kind) { 784 switch (Kind) { 785 case OMPC_DEFAULTMAP_scalar: 786 case OMPC_DEFAULTMAP_pointer: 787 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 788 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 789 (M == OMPC_DEFAULTMAP_MODIFIER_default); 790 case OMPC_DEFAULTMAP_aggregate: 791 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 792 default: 793 break; 794 } 795 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 796 } 797 bool mustBeFirstprivateAtLevel(unsigned Level, 798 OpenMPDefaultmapClauseKind Kind) const { 799 OpenMPDefaultmapClauseModifier M = 800 getDefaultmapModifierAtLevel(Level, Kind); 801 return mustBeFirstprivateBase(M, Kind); 802 } 803 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 804 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 805 return mustBeFirstprivateBase(M, Kind); 806 } 807 808 /// Checks if the specified variable is a threadprivate. 809 bool isThreadPrivate(VarDecl *D) { 810 const DSAVarData DVar = getTopDSA(D, false); 811 return isOpenMPThreadPrivate(DVar.CKind); 812 } 813 814 /// Marks current region as ordered (it has an 'ordered' clause). 815 void setOrderedRegion(bool IsOrdered, const Expr *Param, 816 OMPOrderedClause *Clause) { 817 if (IsOrdered) 818 getTopOfStack().OrderedRegion.emplace(Param, Clause); 819 else 820 getTopOfStack().OrderedRegion.reset(); 821 } 822 /// Returns true, if region is ordered (has associated 'ordered' clause), 823 /// false - otherwise. 824 bool isOrderedRegion() const { 825 if (const SharingMapTy *Top = getTopOfStackOrNull()) 826 return Top->OrderedRegion.hasValue(); 827 return false; 828 } 829 /// Returns optional parameter for the ordered region. 830 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 831 if (const SharingMapTy *Top = getTopOfStackOrNull()) 832 if (Top->OrderedRegion.hasValue()) 833 return Top->OrderedRegion.getValue(); 834 return std::make_pair(nullptr, nullptr); 835 } 836 /// Returns true, if parent region is ordered (has associated 837 /// 'ordered' clause), false - otherwise. 838 bool isParentOrderedRegion() const { 839 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 840 return Parent->OrderedRegion.hasValue(); 841 return false; 842 } 843 /// Returns optional parameter for the ordered region. 844 std::pair<const Expr *, OMPOrderedClause *> 845 getParentOrderedRegionParam() const { 846 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 847 if (Parent->OrderedRegion.hasValue()) 848 return Parent->OrderedRegion.getValue(); 849 return std::make_pair(nullptr, nullptr); 850 } 851 /// Marks current region as nowait (it has a 'nowait' clause). 852 void setNowaitRegion(bool IsNowait = true) { 853 getTopOfStack().NowaitRegion = IsNowait; 854 } 855 /// Returns true, if parent region is nowait (has associated 856 /// 'nowait' clause), false - otherwise. 857 bool isParentNowaitRegion() const { 858 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 859 return Parent->NowaitRegion; 860 return false; 861 } 862 /// Marks parent region as cancel region. 863 void setParentCancelRegion(bool Cancel = true) { 864 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 865 Parent->CancelRegion |= Cancel; 866 } 867 /// Return true if current region has inner cancel construct. 868 bool isCancelRegion() const { 869 const SharingMapTy *Top = getTopOfStackOrNull(); 870 return Top ? Top->CancelRegion : false; 871 } 872 873 /// Mark that parent region already has scan directive. 874 void setParentHasScanDirective(SourceLocation Loc) { 875 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 876 Parent->PrevScanLocation = Loc; 877 } 878 /// Return true if current region has inner cancel construct. 879 bool doesParentHasScanDirective() const { 880 const SharingMapTy *Top = getSecondOnStackOrNull(); 881 return Top ? Top->PrevScanLocation.isValid() : false; 882 } 883 /// Return true if current region has inner cancel construct. 884 SourceLocation getParentScanDirectiveLoc() const { 885 const SharingMapTy *Top = getSecondOnStackOrNull(); 886 return Top ? Top->PrevScanLocation : SourceLocation(); 887 } 888 /// Mark that parent region already has ordered directive. 889 void setParentHasOrderedDirective(SourceLocation Loc) { 890 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 891 Parent->PrevOrderedLocation = Loc; 892 } 893 /// Return true if current region has inner ordered construct. 894 bool doesParentHasOrderedDirective() const { 895 const SharingMapTy *Top = getSecondOnStackOrNull(); 896 return Top ? Top->PrevOrderedLocation.isValid() : false; 897 } 898 /// Returns the location of the previously specified ordered directive. 899 SourceLocation getParentOrderedDirectiveLoc() const { 900 const SharingMapTy *Top = getSecondOnStackOrNull(); 901 return Top ? Top->PrevOrderedLocation : SourceLocation(); 902 } 903 904 /// Set collapse value for the region. 905 void setAssociatedLoops(unsigned Val) { 906 getTopOfStack().AssociatedLoops = Val; 907 if (Val > 1) 908 getTopOfStack().HasMutipleLoops = true; 909 } 910 /// Return collapse value for region. 911 unsigned getAssociatedLoops() const { 912 const SharingMapTy *Top = getTopOfStackOrNull(); 913 return Top ? Top->AssociatedLoops : 0; 914 } 915 /// Returns true if the construct is associated with multiple loops. 916 bool hasMutipleLoops() const { 917 const SharingMapTy *Top = getTopOfStackOrNull(); 918 return Top ? Top->HasMutipleLoops : false; 919 } 920 921 /// Marks current target region as one with closely nested teams 922 /// region. 923 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 924 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 925 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 926 } 927 /// Returns true, if current region has closely nested teams region. 928 bool hasInnerTeamsRegion() const { 929 return getInnerTeamsRegionLoc().isValid(); 930 } 931 /// Returns location of the nested teams region (if any). 932 SourceLocation getInnerTeamsRegionLoc() const { 933 const SharingMapTy *Top = getTopOfStackOrNull(); 934 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 935 } 936 937 Scope *getCurScope() const { 938 const SharingMapTy *Top = getTopOfStackOrNull(); 939 return Top ? Top->CurScope : nullptr; 940 } 941 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; } 942 SourceLocation getConstructLoc() const { 943 const SharingMapTy *Top = getTopOfStackOrNull(); 944 return Top ? Top->ConstructLoc : SourceLocation(); 945 } 946 947 /// Do the check specified in \a Check to all component lists and return true 948 /// if any issue is found. 949 bool checkMappableExprComponentListsForDecl( 950 const ValueDecl *VD, bool CurrentRegionOnly, 951 const llvm::function_ref< 952 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 953 OpenMPClauseKind)> 954 Check) const { 955 if (isStackEmpty()) 956 return false; 957 auto SI = begin(); 958 auto SE = end(); 959 960 if (SI == SE) 961 return false; 962 963 if (CurrentRegionOnly) 964 SE = std::next(SI); 965 else 966 std::advance(SI, 1); 967 968 for (; SI != SE; ++SI) { 969 auto MI = SI->MappedExprComponents.find(VD); 970 if (MI != SI->MappedExprComponents.end()) 971 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 972 MI->second.Components) 973 if (Check(L, MI->second.Kind)) 974 return true; 975 } 976 return false; 977 } 978 979 /// Do the check specified in \a Check to all component lists at a given level 980 /// and return true if any issue is found. 981 bool checkMappableExprComponentListsForDeclAtLevel( 982 const ValueDecl *VD, unsigned Level, 983 const llvm::function_ref< 984 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 985 OpenMPClauseKind)> 986 Check) const { 987 if (getStackSize() <= Level) 988 return false; 989 990 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 991 auto MI = StackElem.MappedExprComponents.find(VD); 992 if (MI != StackElem.MappedExprComponents.end()) 993 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 994 MI->second.Components) 995 if (Check(L, MI->second.Kind)) 996 return true; 997 return false; 998 } 999 1000 /// Create a new mappable expression component list associated with a given 1001 /// declaration and initialize it with the provided list of components. 1002 void addMappableExpressionComponents( 1003 const ValueDecl *VD, 1004 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 1005 OpenMPClauseKind WhereFoundClauseKind) { 1006 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 1007 // Create new entry and append the new components there. 1008 MEC.Components.resize(MEC.Components.size() + 1); 1009 MEC.Components.back().append(Components.begin(), Components.end()); 1010 MEC.Kind = WhereFoundClauseKind; 1011 } 1012 1013 unsigned getNestingLevel() const { 1014 assert(!isStackEmpty()); 1015 return getStackSize() - 1; 1016 } 1017 void addDoacrossDependClause(OMPDependClause *C, 1018 const OperatorOffsetTy &OpsOffs) { 1019 SharingMapTy *Parent = getSecondOnStackOrNull(); 1020 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 1021 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 1022 } 1023 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 1024 getDoacrossDependClauses() const { 1025 const SharingMapTy &StackElem = getTopOfStack(); 1026 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 1027 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 1028 return llvm::make_range(Ref.begin(), Ref.end()); 1029 } 1030 return llvm::make_range(StackElem.DoacrossDepends.end(), 1031 StackElem.DoacrossDepends.end()); 1032 } 1033 1034 // Store types of classes which have been explicitly mapped 1035 void addMappedClassesQualTypes(QualType QT) { 1036 SharingMapTy &StackElem = getTopOfStack(); 1037 StackElem.MappedClassesQualTypes.insert(QT); 1038 } 1039 1040 // Return set of mapped classes types 1041 bool isClassPreviouslyMapped(QualType QT) const { 1042 const SharingMapTy &StackElem = getTopOfStack(); 1043 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1044 } 1045 1046 /// Adds global declare target to the parent target region. 1047 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1048 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1049 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1050 "Expected declare target link global."); 1051 for (auto &Elem : *this) { 1052 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1053 Elem.DeclareTargetLinkVarDecls.push_back(E); 1054 return; 1055 } 1056 } 1057 } 1058 1059 /// Returns the list of globals with declare target link if current directive 1060 /// is target. 1061 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1062 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1063 "Expected target executable directive."); 1064 return getTopOfStack().DeclareTargetLinkVarDecls; 1065 } 1066 1067 /// Adds list of allocators expressions. 1068 void addInnerAllocatorExpr(Expr *E) { 1069 getTopOfStack().InnerUsedAllocators.push_back(E); 1070 } 1071 /// Return list of used allocators. 1072 ArrayRef<Expr *> getInnerAllocators() const { 1073 return getTopOfStack().InnerUsedAllocators; 1074 } 1075 /// Marks the declaration as implicitly firstprivate nin the task-based 1076 /// regions. 1077 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1078 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1079 } 1080 /// Checks if the decl is implicitly firstprivate in the task-based region. 1081 bool isImplicitTaskFirstprivate(Decl *D) const { 1082 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1083 } 1084 1085 /// Marks decl as used in uses_allocators clause as the allocator. 1086 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1087 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1088 } 1089 /// Checks if specified decl is used in uses allocator clause as the 1090 /// allocator. 1091 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1092 const Decl *D) const { 1093 const SharingMapTy &StackElem = getTopOfStack(); 1094 auto I = StackElem.UsesAllocatorsDecls.find(D); 1095 if (I == StackElem.UsesAllocatorsDecls.end()) 1096 return None; 1097 return I->getSecond(); 1098 } 1099 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1100 const SharingMapTy &StackElem = getTopOfStack(); 1101 auto I = StackElem.UsesAllocatorsDecls.find(D); 1102 if (I == StackElem.UsesAllocatorsDecls.end()) 1103 return None; 1104 return I->getSecond(); 1105 } 1106 1107 void addDeclareMapperVarRef(Expr *Ref) { 1108 SharingMapTy &StackElem = getTopOfStack(); 1109 StackElem.DeclareMapperVar = Ref; 1110 } 1111 const Expr *getDeclareMapperVarRef() const { 1112 const SharingMapTy *Top = getTopOfStackOrNull(); 1113 return Top ? Top->DeclareMapperVar : nullptr; 1114 } 1115 }; 1116 1117 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1118 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1119 } 1120 1121 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1122 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1123 DKind == OMPD_unknown; 1124 } 1125 1126 } // namespace 1127 1128 static const Expr *getExprAsWritten(const Expr *E) { 1129 if (const auto *FE = dyn_cast<FullExpr>(E)) 1130 E = FE->getSubExpr(); 1131 1132 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1133 E = MTE->getSubExpr(); 1134 1135 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1136 E = Binder->getSubExpr(); 1137 1138 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1139 E = ICE->getSubExprAsWritten(); 1140 return E->IgnoreParens(); 1141 } 1142 1143 static Expr *getExprAsWritten(Expr *E) { 1144 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1145 } 1146 1147 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1148 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1149 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1150 D = ME->getMemberDecl(); 1151 const auto *VD = dyn_cast<VarDecl>(D); 1152 const auto *FD = dyn_cast<FieldDecl>(D); 1153 if (VD != nullptr) { 1154 VD = VD->getCanonicalDecl(); 1155 D = VD; 1156 } else { 1157 assert(FD); 1158 FD = FD->getCanonicalDecl(); 1159 D = FD; 1160 } 1161 return D; 1162 } 1163 1164 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1165 return const_cast<ValueDecl *>( 1166 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1167 } 1168 1169 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1170 ValueDecl *D) const { 1171 D = getCanonicalDecl(D); 1172 auto *VD = dyn_cast<VarDecl>(D); 1173 const auto *FD = dyn_cast<FieldDecl>(D); 1174 DSAVarData DVar; 1175 if (Iter == end()) { 1176 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1177 // in a region but not in construct] 1178 // File-scope or namespace-scope variables referenced in called routines 1179 // in the region are shared unless they appear in a threadprivate 1180 // directive. 1181 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1182 DVar.CKind = OMPC_shared; 1183 1184 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1185 // in a region but not in construct] 1186 // Variables with static storage duration that are declared in called 1187 // routines in the region are shared. 1188 if (VD && VD->hasGlobalStorage()) 1189 DVar.CKind = OMPC_shared; 1190 1191 // Non-static data members are shared by default. 1192 if (FD) 1193 DVar.CKind = OMPC_shared; 1194 1195 return DVar; 1196 } 1197 1198 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1199 // in a Construct, C/C++, predetermined, p.1] 1200 // Variables with automatic storage duration that are declared in a scope 1201 // inside the construct are private. 1202 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1203 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1204 DVar.CKind = OMPC_private; 1205 return DVar; 1206 } 1207 1208 DVar.DKind = Iter->Directive; 1209 // Explicitly specified attributes and local variables with predetermined 1210 // attributes. 1211 if (Iter->SharingMap.count(D)) { 1212 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1213 DVar.RefExpr = Data.RefExpr.getPointer(); 1214 DVar.PrivateCopy = Data.PrivateCopy; 1215 DVar.CKind = Data.Attributes; 1216 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1217 DVar.Modifier = Data.Modifier; 1218 DVar.AppliedToPointee = Data.AppliedToPointee; 1219 return DVar; 1220 } 1221 1222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1223 // in a Construct, C/C++, implicitly determined, p.1] 1224 // In a parallel or task construct, the data-sharing attributes of these 1225 // variables are determined by the default clause, if present. 1226 switch (Iter->DefaultAttr) { 1227 case DSA_shared: 1228 DVar.CKind = OMPC_shared; 1229 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1230 return DVar; 1231 case DSA_none: 1232 return DVar; 1233 case DSA_firstprivate: 1234 if (VD->getStorageDuration() == SD_Static && 1235 VD->getDeclContext()->isFileContext()) { 1236 DVar.CKind = OMPC_unknown; 1237 } else { 1238 DVar.CKind = OMPC_firstprivate; 1239 } 1240 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1241 return DVar; 1242 case DSA_unspecified: 1243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1244 // in a Construct, implicitly determined, p.2] 1245 // In a parallel construct, if no default clause is present, these 1246 // variables are shared. 1247 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1248 if ((isOpenMPParallelDirective(DVar.DKind) && 1249 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1250 isOpenMPTeamsDirective(DVar.DKind)) { 1251 DVar.CKind = OMPC_shared; 1252 return DVar; 1253 } 1254 1255 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1256 // in a Construct, implicitly determined, p.4] 1257 // In a task construct, if no default clause is present, a variable that in 1258 // the enclosing context is determined to be shared by all implicit tasks 1259 // bound to the current team is shared. 1260 if (isOpenMPTaskingDirective(DVar.DKind)) { 1261 DSAVarData DVarTemp; 1262 const_iterator I = Iter, E = end(); 1263 do { 1264 ++I; 1265 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1266 // Referenced in a Construct, implicitly determined, p.6] 1267 // In a task construct, if no default clause is present, a variable 1268 // whose data-sharing attribute is not determined by the rules above is 1269 // firstprivate. 1270 DVarTemp = getDSA(I, D); 1271 if (DVarTemp.CKind != OMPC_shared) { 1272 DVar.RefExpr = nullptr; 1273 DVar.CKind = OMPC_firstprivate; 1274 return DVar; 1275 } 1276 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1277 DVar.CKind = 1278 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1279 return DVar; 1280 } 1281 } 1282 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1283 // in a Construct, implicitly determined, p.3] 1284 // For constructs other than task, if no default clause is present, these 1285 // variables inherit their data-sharing attributes from the enclosing 1286 // context. 1287 return getDSA(++Iter, D); 1288 } 1289 1290 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1291 const Expr *NewDE) { 1292 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1293 D = getCanonicalDecl(D); 1294 SharingMapTy &StackElem = getTopOfStack(); 1295 auto It = StackElem.AlignedMap.find(D); 1296 if (It == StackElem.AlignedMap.end()) { 1297 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1298 StackElem.AlignedMap[D] = NewDE; 1299 return nullptr; 1300 } 1301 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1302 return It->second; 1303 } 1304 1305 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1306 const Expr *NewDE) { 1307 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1308 D = getCanonicalDecl(D); 1309 SharingMapTy &StackElem = getTopOfStack(); 1310 auto It = StackElem.NontemporalMap.find(D); 1311 if (It == StackElem.NontemporalMap.end()) { 1312 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1313 StackElem.NontemporalMap[D] = NewDE; 1314 return nullptr; 1315 } 1316 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1317 return It->second; 1318 } 1319 1320 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1321 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1322 D = getCanonicalDecl(D); 1323 SharingMapTy &StackElem = getTopOfStack(); 1324 StackElem.LCVMap.try_emplace( 1325 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1326 } 1327 1328 const DSAStackTy::LCDeclInfo 1329 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1330 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1331 D = getCanonicalDecl(D); 1332 const SharingMapTy &StackElem = getTopOfStack(); 1333 auto It = StackElem.LCVMap.find(D); 1334 if (It != StackElem.LCVMap.end()) 1335 return It->second; 1336 return {0, nullptr}; 1337 } 1338 1339 const DSAStackTy::LCDeclInfo 1340 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1341 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1342 D = getCanonicalDecl(D); 1343 for (unsigned I = Level + 1; I > 0; --I) { 1344 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1345 auto It = StackElem.LCVMap.find(D); 1346 if (It != StackElem.LCVMap.end()) 1347 return It->second; 1348 } 1349 return {0, nullptr}; 1350 } 1351 1352 const DSAStackTy::LCDeclInfo 1353 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1354 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1355 assert(Parent && "Data-sharing attributes stack is empty"); 1356 D = getCanonicalDecl(D); 1357 auto It = Parent->LCVMap.find(D); 1358 if (It != Parent->LCVMap.end()) 1359 return It->second; 1360 return {0, nullptr}; 1361 } 1362 1363 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1364 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1365 assert(Parent && "Data-sharing attributes stack is empty"); 1366 if (Parent->LCVMap.size() < I) 1367 return nullptr; 1368 for (const auto &Pair : Parent->LCVMap) 1369 if (Pair.second.first == I) 1370 return Pair.first; 1371 return nullptr; 1372 } 1373 1374 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1375 DeclRefExpr *PrivateCopy, unsigned Modifier, 1376 bool AppliedToPointee) { 1377 D = getCanonicalDecl(D); 1378 if (A == OMPC_threadprivate) { 1379 DSAInfo &Data = Threadprivates[D]; 1380 Data.Attributes = A; 1381 Data.RefExpr.setPointer(E); 1382 Data.PrivateCopy = nullptr; 1383 Data.Modifier = Modifier; 1384 } else { 1385 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1386 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1387 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1388 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1389 (isLoopControlVariable(D).first && A == OMPC_private)); 1390 Data.Modifier = Modifier; 1391 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1392 Data.RefExpr.setInt(/*IntVal=*/true); 1393 return; 1394 } 1395 const bool IsLastprivate = 1396 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1397 Data.Attributes = A; 1398 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1399 Data.PrivateCopy = PrivateCopy; 1400 Data.AppliedToPointee = AppliedToPointee; 1401 if (PrivateCopy) { 1402 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1403 Data.Modifier = Modifier; 1404 Data.Attributes = A; 1405 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1406 Data.PrivateCopy = nullptr; 1407 Data.AppliedToPointee = AppliedToPointee; 1408 } 1409 } 1410 } 1411 1412 /// Build a variable declaration for OpenMP loop iteration variable. 1413 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1414 StringRef Name, const AttrVec *Attrs = nullptr, 1415 DeclRefExpr *OrigRef = nullptr) { 1416 DeclContext *DC = SemaRef.CurContext; 1417 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1418 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1419 auto *Decl = 1420 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1421 if (Attrs) { 1422 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1423 I != E; ++I) 1424 Decl->addAttr(*I); 1425 } 1426 Decl->setImplicit(); 1427 if (OrigRef) { 1428 Decl->addAttr( 1429 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1430 } 1431 return Decl; 1432 } 1433 1434 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1435 SourceLocation Loc, 1436 bool RefersToCapture = false) { 1437 D->setReferenced(); 1438 D->markUsed(S.Context); 1439 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1440 SourceLocation(), D, RefersToCapture, Loc, Ty, 1441 VK_LValue); 1442 } 1443 1444 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1445 BinaryOperatorKind BOK) { 1446 D = getCanonicalDecl(D); 1447 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1448 assert( 1449 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1450 "Additional reduction info may be specified only for reduction items."); 1451 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1452 assert(ReductionData.ReductionRange.isInvalid() && 1453 (getTopOfStack().Directive == OMPD_taskgroup || 1454 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1455 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1456 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1457 "Additional reduction info may be specified only once for reduction " 1458 "items."); 1459 ReductionData.set(BOK, SR); 1460 Expr *&TaskgroupReductionRef = 1461 getTopOfStack().TaskgroupReductionRef; 1462 if (!TaskgroupReductionRef) { 1463 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1464 SemaRef.Context.VoidPtrTy, ".task_red."); 1465 TaskgroupReductionRef = 1466 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1467 } 1468 } 1469 1470 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1471 const Expr *ReductionRef) { 1472 D = getCanonicalDecl(D); 1473 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1474 assert( 1475 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1476 "Additional reduction info may be specified only for reduction items."); 1477 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1478 assert(ReductionData.ReductionRange.isInvalid() && 1479 (getTopOfStack().Directive == OMPD_taskgroup || 1480 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1481 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1482 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1483 "Additional reduction info may be specified only once for reduction " 1484 "items."); 1485 ReductionData.set(ReductionRef, SR); 1486 Expr *&TaskgroupReductionRef = 1487 getTopOfStack().TaskgroupReductionRef; 1488 if (!TaskgroupReductionRef) { 1489 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1490 SemaRef.Context.VoidPtrTy, ".task_red."); 1491 TaskgroupReductionRef = 1492 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1493 } 1494 } 1495 1496 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1497 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1498 Expr *&TaskgroupDescriptor) const { 1499 D = getCanonicalDecl(D); 1500 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1501 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1502 const DSAInfo &Data = I->SharingMap.lookup(D); 1503 if (Data.Attributes != OMPC_reduction || 1504 Data.Modifier != OMPC_REDUCTION_task) 1505 continue; 1506 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1507 if (!ReductionData.ReductionOp || 1508 ReductionData.ReductionOp.is<const Expr *>()) 1509 return DSAVarData(); 1510 SR = ReductionData.ReductionRange; 1511 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1512 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1513 "expression for the descriptor is not " 1514 "set."); 1515 TaskgroupDescriptor = I->TaskgroupReductionRef; 1516 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1517 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1518 /*AppliedToPointee=*/false); 1519 } 1520 return DSAVarData(); 1521 } 1522 1523 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1524 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1525 Expr *&TaskgroupDescriptor) const { 1526 D = getCanonicalDecl(D); 1527 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1528 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1529 const DSAInfo &Data = I->SharingMap.lookup(D); 1530 if (Data.Attributes != OMPC_reduction || 1531 Data.Modifier != OMPC_REDUCTION_task) 1532 continue; 1533 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1534 if (!ReductionData.ReductionOp || 1535 !ReductionData.ReductionOp.is<const Expr *>()) 1536 return DSAVarData(); 1537 SR = ReductionData.ReductionRange; 1538 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1539 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1540 "expression for the descriptor is not " 1541 "set."); 1542 TaskgroupDescriptor = I->TaskgroupReductionRef; 1543 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1544 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task, 1545 /*AppliedToPointee=*/false); 1546 } 1547 return DSAVarData(); 1548 } 1549 1550 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1551 D = D->getCanonicalDecl(); 1552 for (const_iterator E = end(); I != E; ++I) { 1553 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1554 isOpenMPTargetExecutionDirective(I->Directive)) { 1555 if (I->CurScope) { 1556 Scope *TopScope = I->CurScope->getParent(); 1557 Scope *CurScope = getCurScope(); 1558 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1559 CurScope = CurScope->getParent(); 1560 return CurScope != TopScope; 1561 } 1562 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent()) 1563 if (I->Context == DC) 1564 return true; 1565 return false; 1566 } 1567 } 1568 return false; 1569 } 1570 1571 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1572 bool AcceptIfMutable = true, 1573 bool *IsClassType = nullptr) { 1574 ASTContext &Context = SemaRef.getASTContext(); 1575 Type = Type.getNonReferenceType().getCanonicalType(); 1576 bool IsConstant = Type.isConstant(Context); 1577 Type = Context.getBaseElementType(Type); 1578 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1579 ? Type->getAsCXXRecordDecl() 1580 : nullptr; 1581 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1582 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1583 RD = CTD->getTemplatedDecl(); 1584 if (IsClassType) 1585 *IsClassType = RD; 1586 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1587 RD->hasDefinition() && RD->hasMutableFields()); 1588 } 1589 1590 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1591 QualType Type, OpenMPClauseKind CKind, 1592 SourceLocation ELoc, 1593 bool AcceptIfMutable = true, 1594 bool ListItemNotVar = false) { 1595 ASTContext &Context = SemaRef.getASTContext(); 1596 bool IsClassType; 1597 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1598 unsigned Diag = ListItemNotVar 1599 ? diag::err_omp_const_list_item 1600 : IsClassType ? diag::err_omp_const_not_mutable_variable 1601 : diag::err_omp_const_variable; 1602 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1603 if (!ListItemNotVar && D) { 1604 const VarDecl *VD = dyn_cast<VarDecl>(D); 1605 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1606 VarDecl::DeclarationOnly; 1607 SemaRef.Diag(D->getLocation(), 1608 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1609 << D; 1610 } 1611 return true; 1612 } 1613 return false; 1614 } 1615 1616 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1617 bool FromParent) { 1618 D = getCanonicalDecl(D); 1619 DSAVarData DVar; 1620 1621 auto *VD = dyn_cast<VarDecl>(D); 1622 auto TI = Threadprivates.find(D); 1623 if (TI != Threadprivates.end()) { 1624 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1625 DVar.CKind = OMPC_threadprivate; 1626 DVar.Modifier = TI->getSecond().Modifier; 1627 return DVar; 1628 } 1629 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1630 DVar.RefExpr = buildDeclRefExpr( 1631 SemaRef, VD, D->getType().getNonReferenceType(), 1632 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1633 DVar.CKind = OMPC_threadprivate; 1634 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1635 return DVar; 1636 } 1637 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1638 // in a Construct, C/C++, predetermined, p.1] 1639 // Variables appearing in threadprivate directives are threadprivate. 1640 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1641 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1642 SemaRef.getLangOpts().OpenMPUseTLS && 1643 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1644 (VD && VD->getStorageClass() == SC_Register && 1645 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1646 DVar.RefExpr = buildDeclRefExpr( 1647 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1648 DVar.CKind = OMPC_threadprivate; 1649 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1650 return DVar; 1651 } 1652 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1653 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1654 !isLoopControlVariable(D).first) { 1655 const_iterator IterTarget = 1656 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1657 return isOpenMPTargetExecutionDirective(Data.Directive); 1658 }); 1659 if (IterTarget != end()) { 1660 const_iterator ParentIterTarget = IterTarget + 1; 1661 for (const_iterator Iter = begin(); 1662 Iter != ParentIterTarget; ++Iter) { 1663 if (isOpenMPLocal(VD, Iter)) { 1664 DVar.RefExpr = 1665 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1666 D->getLocation()); 1667 DVar.CKind = OMPC_threadprivate; 1668 return DVar; 1669 } 1670 } 1671 if (!isClauseParsingMode() || IterTarget != begin()) { 1672 auto DSAIter = IterTarget->SharingMap.find(D); 1673 if (DSAIter != IterTarget->SharingMap.end() && 1674 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1675 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1676 DVar.CKind = OMPC_threadprivate; 1677 return DVar; 1678 } 1679 const_iterator End = end(); 1680 if (!SemaRef.isOpenMPCapturedByRef( 1681 D, std::distance(ParentIterTarget, End), 1682 /*OpenMPCaptureLevel=*/0)) { 1683 DVar.RefExpr = 1684 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1685 IterTarget->ConstructLoc); 1686 DVar.CKind = OMPC_threadprivate; 1687 return DVar; 1688 } 1689 } 1690 } 1691 } 1692 1693 if (isStackEmpty()) 1694 // Not in OpenMP execution region and top scope was already checked. 1695 return DVar; 1696 1697 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1698 // in a Construct, C/C++, predetermined, p.4] 1699 // Static data members are shared. 1700 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1701 // in a Construct, C/C++, predetermined, p.7] 1702 // Variables with static storage duration that are declared in a scope 1703 // inside the construct are shared. 1704 if (VD && VD->isStaticDataMember()) { 1705 // Check for explicitly specified attributes. 1706 const_iterator I = begin(); 1707 const_iterator EndI = end(); 1708 if (FromParent && I != EndI) 1709 ++I; 1710 if (I != EndI) { 1711 auto It = I->SharingMap.find(D); 1712 if (It != I->SharingMap.end()) { 1713 const DSAInfo &Data = It->getSecond(); 1714 DVar.RefExpr = Data.RefExpr.getPointer(); 1715 DVar.PrivateCopy = Data.PrivateCopy; 1716 DVar.CKind = Data.Attributes; 1717 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1718 DVar.DKind = I->Directive; 1719 DVar.Modifier = Data.Modifier; 1720 DVar.AppliedToPointee = Data.AppliedToPointee; 1721 return DVar; 1722 } 1723 } 1724 1725 DVar.CKind = OMPC_shared; 1726 return DVar; 1727 } 1728 1729 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1730 // The predetermined shared attribute for const-qualified types having no 1731 // mutable members was removed after OpenMP 3.1. 1732 if (SemaRef.LangOpts.OpenMP <= 31) { 1733 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1734 // in a Construct, C/C++, predetermined, p.6] 1735 // Variables with const qualified type having no mutable member are 1736 // shared. 1737 if (isConstNotMutableType(SemaRef, D->getType())) { 1738 // Variables with const-qualified type having no mutable member may be 1739 // listed in a firstprivate clause, even if they are static data members. 1740 DSAVarData DVarTemp = hasInnermostDSA( 1741 D, 1742 [](OpenMPClauseKind C, bool) { 1743 return C == OMPC_firstprivate || C == OMPC_shared; 1744 }, 1745 MatchesAlways, FromParent); 1746 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1747 return DVarTemp; 1748 1749 DVar.CKind = OMPC_shared; 1750 return DVar; 1751 } 1752 } 1753 1754 // Explicitly specified attributes and local variables with predetermined 1755 // attributes. 1756 const_iterator I = begin(); 1757 const_iterator EndI = end(); 1758 if (FromParent && I != EndI) 1759 ++I; 1760 if (I == EndI) 1761 return DVar; 1762 auto It = I->SharingMap.find(D); 1763 if (It != I->SharingMap.end()) { 1764 const DSAInfo &Data = It->getSecond(); 1765 DVar.RefExpr = Data.RefExpr.getPointer(); 1766 DVar.PrivateCopy = Data.PrivateCopy; 1767 DVar.CKind = Data.Attributes; 1768 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1769 DVar.DKind = I->Directive; 1770 DVar.Modifier = Data.Modifier; 1771 DVar.AppliedToPointee = Data.AppliedToPointee; 1772 } 1773 1774 return DVar; 1775 } 1776 1777 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1778 bool FromParent) const { 1779 if (isStackEmpty()) { 1780 const_iterator I; 1781 return getDSA(I, D); 1782 } 1783 D = getCanonicalDecl(D); 1784 const_iterator StartI = begin(); 1785 const_iterator EndI = end(); 1786 if (FromParent && StartI != EndI) 1787 ++StartI; 1788 return getDSA(StartI, D); 1789 } 1790 1791 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1792 unsigned Level) const { 1793 if (getStackSize() <= Level) 1794 return DSAVarData(); 1795 D = getCanonicalDecl(D); 1796 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1797 return getDSA(StartI, D); 1798 } 1799 1800 const DSAStackTy::DSAVarData 1801 DSAStackTy::hasDSA(ValueDecl *D, 1802 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1803 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1804 bool FromParent) const { 1805 if (isStackEmpty()) 1806 return {}; 1807 D = getCanonicalDecl(D); 1808 const_iterator I = begin(); 1809 const_iterator EndI = end(); 1810 if (FromParent && I != EndI) 1811 ++I; 1812 for (; I != EndI; ++I) { 1813 if (!DPred(I->Directive) && 1814 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1815 continue; 1816 const_iterator NewI = I; 1817 DSAVarData DVar = getDSA(NewI, D); 1818 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1819 return DVar; 1820 } 1821 return {}; 1822 } 1823 1824 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1825 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1826 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1827 bool FromParent) const { 1828 if (isStackEmpty()) 1829 return {}; 1830 D = getCanonicalDecl(D); 1831 const_iterator StartI = begin(); 1832 const_iterator EndI = end(); 1833 if (FromParent && StartI != EndI) 1834 ++StartI; 1835 if (StartI == EndI || !DPred(StartI->Directive)) 1836 return {}; 1837 const_iterator NewI = StartI; 1838 DSAVarData DVar = getDSA(NewI, D); 1839 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee)) 1840 ? DVar 1841 : DSAVarData(); 1842 } 1843 1844 bool DSAStackTy::hasExplicitDSA( 1845 const ValueDecl *D, 1846 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred, 1847 unsigned Level, bool NotLastprivate) const { 1848 if (getStackSize() <= Level) 1849 return false; 1850 D = getCanonicalDecl(D); 1851 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1852 auto I = StackElem.SharingMap.find(D); 1853 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() && 1854 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) && 1855 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1856 return true; 1857 // Check predetermined rules for the loop control variables. 1858 auto LI = StackElem.LCVMap.find(D); 1859 if (LI != StackElem.LCVMap.end()) 1860 return CPred(OMPC_private, /*AppliedToPointee=*/false); 1861 return false; 1862 } 1863 1864 bool DSAStackTy::hasExplicitDirective( 1865 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1866 unsigned Level) const { 1867 if (getStackSize() <= Level) 1868 return false; 1869 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1870 return DPred(StackElem.Directive); 1871 } 1872 1873 bool DSAStackTy::hasDirective( 1874 const llvm::function_ref<bool(OpenMPDirectiveKind, 1875 const DeclarationNameInfo &, SourceLocation)> 1876 DPred, 1877 bool FromParent) const { 1878 // We look only in the enclosing region. 1879 size_t Skip = FromParent ? 2 : 1; 1880 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1881 I != E; ++I) { 1882 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1883 return true; 1884 } 1885 return false; 1886 } 1887 1888 void Sema::InitDataSharingAttributesStack() { 1889 VarDataSharingAttributesStack = new DSAStackTy(*this); 1890 } 1891 1892 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1893 1894 void Sema::pushOpenMPFunctionRegion() { 1895 DSAStack->pushFunction(); 1896 } 1897 1898 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1899 DSAStack->popFunction(OldFSI); 1900 } 1901 1902 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1903 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1904 "Expected OpenMP device compilation."); 1905 return !S.isInOpenMPTargetExecutionDirective(); 1906 } 1907 1908 namespace { 1909 /// Status of the function emission on the host/device. 1910 enum class FunctionEmissionStatus { 1911 Emitted, 1912 Discarded, 1913 Unknown, 1914 }; 1915 } // anonymous namespace 1916 1917 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1918 unsigned DiagID, 1919 FunctionDecl *FD) { 1920 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1921 "Expected OpenMP device compilation."); 1922 1923 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1924 if (FD) { 1925 FunctionEmissionStatus FES = getEmissionStatus(FD); 1926 switch (FES) { 1927 case FunctionEmissionStatus::Emitted: 1928 Kind = SemaDiagnosticBuilder::K_Immediate; 1929 break; 1930 case FunctionEmissionStatus::Unknown: 1931 // TODO: We should always delay diagnostics here in case a target 1932 // region is in a function we do not emit. However, as the 1933 // current diagnostics are associated with the function containing 1934 // the target region and we do not emit that one, we would miss out 1935 // on diagnostics for the target region itself. We need to anchor 1936 // the diagnostics with the new generated function *or* ensure we 1937 // emit diagnostics associated with the surrounding function. 1938 Kind = isOpenMPDeviceDelayedContext(*this) 1939 ? SemaDiagnosticBuilder::K_Deferred 1940 : SemaDiagnosticBuilder::K_Immediate; 1941 break; 1942 case FunctionEmissionStatus::TemplateDiscarded: 1943 case FunctionEmissionStatus::OMPDiscarded: 1944 Kind = SemaDiagnosticBuilder::K_Nop; 1945 break; 1946 case FunctionEmissionStatus::CUDADiscarded: 1947 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1948 break; 1949 } 1950 } 1951 1952 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1953 } 1954 1955 Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1956 unsigned DiagID, 1957 FunctionDecl *FD) { 1958 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1959 "Expected OpenMP host compilation."); 1960 1961 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop; 1962 if (FD) { 1963 FunctionEmissionStatus FES = getEmissionStatus(FD); 1964 switch (FES) { 1965 case FunctionEmissionStatus::Emitted: 1966 Kind = SemaDiagnosticBuilder::K_Immediate; 1967 break; 1968 case FunctionEmissionStatus::Unknown: 1969 Kind = SemaDiagnosticBuilder::K_Deferred; 1970 break; 1971 case FunctionEmissionStatus::TemplateDiscarded: 1972 case FunctionEmissionStatus::OMPDiscarded: 1973 case FunctionEmissionStatus::CUDADiscarded: 1974 Kind = SemaDiagnosticBuilder::K_Nop; 1975 break; 1976 } 1977 } 1978 1979 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this); 1980 } 1981 1982 static OpenMPDefaultmapClauseKind 1983 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1984 if (LO.OpenMP <= 45) { 1985 if (VD->getType().getNonReferenceType()->isScalarType()) 1986 return OMPC_DEFAULTMAP_scalar; 1987 return OMPC_DEFAULTMAP_aggregate; 1988 } 1989 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1990 return OMPC_DEFAULTMAP_pointer; 1991 if (VD->getType().getNonReferenceType()->isScalarType()) 1992 return OMPC_DEFAULTMAP_scalar; 1993 return OMPC_DEFAULTMAP_aggregate; 1994 } 1995 1996 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1997 unsigned OpenMPCaptureLevel) const { 1998 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1999 2000 ASTContext &Ctx = getASTContext(); 2001 bool IsByRef = true; 2002 2003 // Find the directive that is associated with the provided scope. 2004 D = cast<ValueDecl>(D->getCanonicalDecl()); 2005 QualType Ty = D->getType(); 2006 2007 bool IsVariableUsedInMapClause = false; 2008 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 2009 // This table summarizes how a given variable should be passed to the device 2010 // given its type and the clauses where it appears. This table is based on 2011 // the description in OpenMP 4.5 [2.10.4, target Construct] and 2012 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 2013 // 2014 // ========================================================================= 2015 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 2016 // | |(tofrom:scalar)| | pvt | | | | 2017 // ========================================================================= 2018 // | scl | | | | - | | bycopy| 2019 // | scl | | - | x | - | - | bycopy| 2020 // | scl | | x | - | - | - | null | 2021 // | scl | x | | | - | | byref | 2022 // | scl | x | - | x | - | - | bycopy| 2023 // | scl | x | x | - | - | - | null | 2024 // | scl | | - | - | - | x | byref | 2025 // | scl | x | - | - | - | x | byref | 2026 // 2027 // | agg | n.a. | | | - | | byref | 2028 // | agg | n.a. | - | x | - | - | byref | 2029 // | agg | n.a. | x | - | - | - | null | 2030 // | agg | n.a. | - | - | - | x | byref | 2031 // | agg | n.a. | - | - | - | x[] | byref | 2032 // 2033 // | ptr | n.a. | | | - | | bycopy| 2034 // | ptr | n.a. | - | x | - | - | bycopy| 2035 // | ptr | n.a. | x | - | - | - | null | 2036 // | ptr | n.a. | - | - | - | x | byref | 2037 // | ptr | n.a. | - | - | - | x[] | bycopy| 2038 // | ptr | n.a. | - | - | x | | bycopy| 2039 // | ptr | n.a. | - | - | x | x | bycopy| 2040 // | ptr | n.a. | - | - | x | x[] | bycopy| 2041 // ========================================================================= 2042 // Legend: 2043 // scl - scalar 2044 // ptr - pointer 2045 // agg - aggregate 2046 // x - applies 2047 // - - invalid in this combination 2048 // [] - mapped with an array section 2049 // byref - should be mapped by reference 2050 // byval - should be mapped by value 2051 // null - initialize a local variable to null on the device 2052 // 2053 // Observations: 2054 // - All scalar declarations that show up in a map clause have to be passed 2055 // by reference, because they may have been mapped in the enclosing data 2056 // environment. 2057 // - If the scalar value does not fit the size of uintptr, it has to be 2058 // passed by reference, regardless the result in the table above. 2059 // - For pointers mapped by value that have either an implicit map or an 2060 // array section, the runtime library may pass the NULL value to the 2061 // device instead of the value passed to it by the compiler. 2062 2063 if (Ty->isReferenceType()) 2064 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 2065 2066 // Locate map clauses and see if the variable being captured is referred to 2067 // in any of those clauses. Here we only care about variables, not fields, 2068 // because fields are part of aggregates. 2069 bool IsVariableAssociatedWithSection = false; 2070 2071 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2072 D, Level, 2073 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2074 OMPClauseMappableExprCommon::MappableExprComponentListRef 2075 MapExprComponents, 2076 OpenMPClauseKind WhereFoundClauseKind) { 2077 // Only the map clause information influences how a variable is 2078 // captured. E.g. is_device_ptr does not require changing the default 2079 // behavior. 2080 if (WhereFoundClauseKind != OMPC_map) 2081 return false; 2082 2083 auto EI = MapExprComponents.rbegin(); 2084 auto EE = MapExprComponents.rend(); 2085 2086 assert(EI != EE && "Invalid map expression!"); 2087 2088 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2089 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2090 2091 ++EI; 2092 if (EI == EE) 2093 return false; 2094 2095 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2096 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2097 isa<MemberExpr>(EI->getAssociatedExpression()) || 2098 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2099 IsVariableAssociatedWithSection = true; 2100 // There is nothing more we need to know about this variable. 2101 return true; 2102 } 2103 2104 // Keep looking for more map info. 2105 return false; 2106 }); 2107 2108 if (IsVariableUsedInMapClause) { 2109 // If variable is identified in a map clause it is always captured by 2110 // reference except if it is a pointer that is dereferenced somehow. 2111 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2112 } else { 2113 // By default, all the data that has a scalar type is mapped by copy 2114 // (except for reduction variables). 2115 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2116 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2117 !Ty->isAnyPointerType()) || 2118 !Ty->isScalarType() || 2119 DSAStack->isDefaultmapCapturedByRef( 2120 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2121 DSAStack->hasExplicitDSA( 2122 D, 2123 [](OpenMPClauseKind K, bool AppliedToPointee) { 2124 return K == OMPC_reduction && !AppliedToPointee; 2125 }, 2126 Level); 2127 } 2128 } 2129 2130 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2131 IsByRef = 2132 ((IsVariableUsedInMapClause && 2133 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2134 OMPD_target) || 2135 !(DSAStack->hasExplicitDSA( 2136 D, 2137 [](OpenMPClauseKind K, bool AppliedToPointee) -> bool { 2138 return K == OMPC_firstprivate || 2139 (K == OMPC_reduction && AppliedToPointee); 2140 }, 2141 Level, /*NotLastprivate=*/true) || 2142 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2143 // If the variable is artificial and must be captured by value - try to 2144 // capture by value. 2145 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2146 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2147 // If the variable is implicitly firstprivate and scalar - capture by 2148 // copy 2149 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2150 !DSAStack->hasExplicitDSA( 2151 D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; }, 2152 Level) && 2153 !DSAStack->isLoopControlVariable(D, Level).first); 2154 } 2155 2156 // When passing data by copy, we need to make sure it fits the uintptr size 2157 // and alignment, because the runtime library only deals with uintptr types. 2158 // If it does not fit the uintptr size, we need to pass the data by reference 2159 // instead. 2160 if (!IsByRef && 2161 (Ctx.getTypeSizeInChars(Ty) > 2162 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2163 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2164 IsByRef = true; 2165 } 2166 2167 return IsByRef; 2168 } 2169 2170 unsigned Sema::getOpenMPNestingLevel() const { 2171 assert(getLangOpts().OpenMP); 2172 return DSAStack->getNestingLevel(); 2173 } 2174 2175 bool Sema::isInOpenMPTargetExecutionDirective() const { 2176 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2177 !DSAStack->isClauseParsingMode()) || 2178 DSAStack->hasDirective( 2179 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2180 SourceLocation) -> bool { 2181 return isOpenMPTargetExecutionDirective(K); 2182 }, 2183 false); 2184 } 2185 2186 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2187 unsigned StopAt) { 2188 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2189 D = getCanonicalDecl(D); 2190 2191 auto *VD = dyn_cast<VarDecl>(D); 2192 // Do not capture constexpr variables. 2193 if (VD && VD->isConstexpr()) 2194 return nullptr; 2195 2196 // If we want to determine whether the variable should be captured from the 2197 // perspective of the current capturing scope, and we've already left all the 2198 // capturing scopes of the top directive on the stack, check from the 2199 // perspective of its parent directive (if any) instead. 2200 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2201 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2202 2203 // If we are attempting to capture a global variable in a directive with 2204 // 'target' we return true so that this global is also mapped to the device. 2205 // 2206 if (VD && !VD->hasLocalStorage() && 2207 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2208 if (isInOpenMPTargetExecutionDirective()) { 2209 DSAStackTy::DSAVarData DVarTop = 2210 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2211 if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr) 2212 return VD; 2213 // If the declaration is enclosed in a 'declare target' directive, 2214 // then it should not be captured. 2215 // 2216 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2217 return nullptr; 2218 CapturedRegionScopeInfo *CSI = nullptr; 2219 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2220 llvm::reverse(FunctionScopes), 2221 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2222 if (!isa<CapturingScopeInfo>(FSI)) 2223 return nullptr; 2224 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2225 if (RSI->CapRegionKind == CR_OpenMP) { 2226 CSI = RSI; 2227 break; 2228 } 2229 } 2230 assert(CSI && "Failed to find CapturedRegionScopeInfo"); 2231 SmallVector<OpenMPDirectiveKind, 4> Regions; 2232 getOpenMPCaptureRegions(Regions, 2233 DSAStack->getDirective(CSI->OpenMPLevel)); 2234 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2235 return VD; 2236 } 2237 if (isInOpenMPDeclareTargetContext()) { 2238 // Try to mark variable as declare target if it is used in capturing 2239 // regions. 2240 if (LangOpts.OpenMP <= 45 && 2241 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2242 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2243 return nullptr; 2244 } 2245 } 2246 2247 if (CheckScopeInfo) { 2248 bool OpenMPFound = false; 2249 for (unsigned I = StopAt + 1; I > 0; --I) { 2250 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2251 if(!isa<CapturingScopeInfo>(FSI)) 2252 return nullptr; 2253 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2254 if (RSI->CapRegionKind == CR_OpenMP) { 2255 OpenMPFound = true; 2256 break; 2257 } 2258 } 2259 if (!OpenMPFound) 2260 return nullptr; 2261 } 2262 2263 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2264 (!DSAStack->isClauseParsingMode() || 2265 DSAStack->getParentDirective() != OMPD_unknown)) { 2266 auto &&Info = DSAStack->isLoopControlVariable(D); 2267 if (Info.first || 2268 (VD && VD->hasLocalStorage() && 2269 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2270 (VD && DSAStack->isForceVarCapturing())) 2271 return VD ? VD : Info.second; 2272 DSAStackTy::DSAVarData DVarTop = 2273 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2274 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) && 2275 (!VD || VD->hasLocalStorage() || !DVarTop.AppliedToPointee)) 2276 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2277 // Threadprivate variables must not be captured. 2278 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2279 return nullptr; 2280 // The variable is not private or it is the variable in the directive with 2281 // default(none) clause and not used in any clause. 2282 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2283 D, 2284 [](OpenMPClauseKind C, bool AppliedToPointee) { 2285 return isOpenMPPrivate(C) && !AppliedToPointee; 2286 }, 2287 [](OpenMPDirectiveKind) { return true; }, 2288 DSAStack->isClauseParsingMode()); 2289 // Global shared must not be captured. 2290 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2291 ((DSAStack->getDefaultDSA() != DSA_none && 2292 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2293 DVarTop.CKind == OMPC_shared)) 2294 return nullptr; 2295 if (DVarPrivate.CKind != OMPC_unknown || 2296 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2297 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2298 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2299 } 2300 return nullptr; 2301 } 2302 2303 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2304 unsigned Level) const { 2305 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2306 } 2307 2308 void Sema::startOpenMPLoop() { 2309 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2310 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2311 DSAStack->loopInit(); 2312 } 2313 2314 void Sema::startOpenMPCXXRangeFor() { 2315 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2316 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2317 DSAStack->resetPossibleLoopCounter(); 2318 DSAStack->loopStart(); 2319 } 2320 } 2321 2322 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2323 unsigned CapLevel) const { 2324 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2325 if (DSAStack->hasExplicitDirective( 2326 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2327 Level)) { 2328 bool IsTriviallyCopyable = 2329 D->getType().getNonReferenceType().isTriviallyCopyableType(Context) && 2330 !D->getType() 2331 .getNonReferenceType() 2332 .getCanonicalType() 2333 ->getAsCXXRecordDecl(); 2334 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2335 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2336 getOpenMPCaptureRegions(CaptureRegions, DKind); 2337 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2338 (IsTriviallyCopyable || 2339 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2340 if (DSAStack->hasExplicitDSA( 2341 D, 2342 [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; }, 2343 Level, /*NotLastprivate=*/true)) 2344 return OMPC_firstprivate; 2345 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2346 if (DVar.CKind != OMPC_shared && 2347 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2348 DSAStack->addImplicitTaskFirstprivate(Level, D); 2349 return OMPC_firstprivate; 2350 } 2351 } 2352 } 2353 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2354 if (DSAStack->getAssociatedLoops() > 0 && 2355 !DSAStack->isLoopStarted()) { 2356 DSAStack->resetPossibleLoopCounter(D); 2357 DSAStack->loopStart(); 2358 return OMPC_private; 2359 } 2360 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2361 DSAStack->isLoopControlVariable(D).first) && 2362 !DSAStack->hasExplicitDSA( 2363 D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; }, 2364 Level) && 2365 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2366 return OMPC_private; 2367 } 2368 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2369 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2370 DSAStack->isForceVarCapturing() && 2371 !DSAStack->hasExplicitDSA( 2372 D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; }, 2373 Level)) 2374 return OMPC_private; 2375 } 2376 // User-defined allocators are private since they must be defined in the 2377 // context of target region. 2378 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2379 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2380 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2381 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2382 return OMPC_private; 2383 return (DSAStack->hasExplicitDSA( 2384 D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; }, 2385 Level) || 2386 (DSAStack->isClauseParsingMode() && 2387 DSAStack->getClauseParsingMode() == OMPC_private) || 2388 // Consider taskgroup reduction descriptor variable a private 2389 // to avoid possible capture in the region. 2390 (DSAStack->hasExplicitDirective( 2391 [](OpenMPDirectiveKind K) { 2392 return K == OMPD_taskgroup || 2393 ((isOpenMPParallelDirective(K) || 2394 isOpenMPWorksharingDirective(K)) && 2395 !isOpenMPSimdDirective(K)); 2396 }, 2397 Level) && 2398 DSAStack->isTaskgroupReductionRef(D, Level))) 2399 ? OMPC_private 2400 : OMPC_unknown; 2401 } 2402 2403 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2404 unsigned Level) { 2405 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2406 D = getCanonicalDecl(D); 2407 OpenMPClauseKind OMPC = OMPC_unknown; 2408 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2409 const unsigned NewLevel = I - 1; 2410 if (DSAStack->hasExplicitDSA( 2411 D, 2412 [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) { 2413 if (isOpenMPPrivate(K) && !AppliedToPointee) { 2414 OMPC = K; 2415 return true; 2416 } 2417 return false; 2418 }, 2419 NewLevel)) 2420 break; 2421 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2422 D, NewLevel, 2423 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2424 OpenMPClauseKind) { return true; })) { 2425 OMPC = OMPC_map; 2426 break; 2427 } 2428 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2429 NewLevel)) { 2430 OMPC = OMPC_map; 2431 if (DSAStack->mustBeFirstprivateAtLevel( 2432 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2433 OMPC = OMPC_firstprivate; 2434 break; 2435 } 2436 } 2437 if (OMPC != OMPC_unknown) 2438 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2439 } 2440 2441 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2442 unsigned CaptureLevel) const { 2443 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2444 // Return true if the current level is no longer enclosed in a target region. 2445 2446 SmallVector<OpenMPDirectiveKind, 4> Regions; 2447 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2448 const auto *VD = dyn_cast<VarDecl>(D); 2449 return VD && !VD->hasLocalStorage() && 2450 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2451 Level) && 2452 Regions[CaptureLevel] != OMPD_task; 2453 } 2454 2455 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2456 unsigned CaptureLevel) const { 2457 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2458 // Return true if the current level is no longer enclosed in a target region. 2459 2460 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2461 if (!VD->hasLocalStorage()) { 2462 if (isInOpenMPTargetExecutionDirective()) 2463 return true; 2464 DSAStackTy::DSAVarData TopDVar = 2465 DSAStack->getTopDSA(D, /*FromParent=*/false); 2466 unsigned NumLevels = 2467 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2468 if (Level == 0) 2469 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2470 do { 2471 --Level; 2472 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2473 if (DVar.CKind != OMPC_shared) 2474 return true; 2475 } while (Level > 0); 2476 } 2477 } 2478 return true; 2479 } 2480 2481 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2482 2483 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2484 OMPTraitInfo &TI) { 2485 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2486 } 2487 2488 void Sema::ActOnOpenMPEndDeclareVariant() { 2489 assert(isInOpenMPDeclareVariantScope() && 2490 "Not in OpenMP declare variant scope!"); 2491 2492 OMPDeclareVariantScopes.pop_back(); 2493 } 2494 2495 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2496 const FunctionDecl *Callee, 2497 SourceLocation Loc) { 2498 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2499 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2500 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2501 // Ignore host functions during device analyzis. 2502 if (LangOpts.OpenMPIsDevice && 2503 (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)) 2504 return; 2505 // Ignore nohost functions during host analyzis. 2506 if (!LangOpts.OpenMPIsDevice && DevTy && 2507 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2508 return; 2509 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2510 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2511 if (LangOpts.OpenMPIsDevice && DevTy && 2512 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2513 // Diagnose host function called during device codegen. 2514 StringRef HostDevTy = 2515 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2516 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2517 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2518 diag::note_omp_marked_device_type_here) 2519 << HostDevTy; 2520 return; 2521 } 2522 if (!LangOpts.OpenMPIsDevice && DevTy && 2523 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2524 // Diagnose nohost function called during host codegen. 2525 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2526 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2527 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2528 Diag(*OMPDeclareTargetDeclAttr::getLocation(FD), 2529 diag::note_omp_marked_device_type_here) 2530 << NoHostDevTy; 2531 } 2532 } 2533 2534 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2535 const DeclarationNameInfo &DirName, 2536 Scope *CurScope, SourceLocation Loc) { 2537 DSAStack->push(DKind, DirName, CurScope, Loc); 2538 PushExpressionEvaluationContext( 2539 ExpressionEvaluationContext::PotentiallyEvaluated); 2540 } 2541 2542 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2543 DSAStack->setClauseParsingMode(K); 2544 } 2545 2546 void Sema::EndOpenMPClause() { 2547 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2548 CleanupVarDeclMarking(); 2549 } 2550 2551 static std::pair<ValueDecl *, bool> 2552 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2553 SourceRange &ERange, bool AllowArraySection = false); 2554 2555 /// Check consistency of the reduction clauses. 2556 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2557 ArrayRef<OMPClause *> Clauses) { 2558 bool InscanFound = false; 2559 SourceLocation InscanLoc; 2560 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2561 // A reduction clause without the inscan reduction-modifier may not appear on 2562 // a construct on which a reduction clause with the inscan reduction-modifier 2563 // appears. 2564 for (OMPClause *C : Clauses) { 2565 if (C->getClauseKind() != OMPC_reduction) 2566 continue; 2567 auto *RC = cast<OMPReductionClause>(C); 2568 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2569 InscanFound = true; 2570 InscanLoc = RC->getModifierLoc(); 2571 continue; 2572 } 2573 if (RC->getModifier() == OMPC_REDUCTION_task) { 2574 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2575 // A reduction clause with the task reduction-modifier may only appear on 2576 // a parallel construct, a worksharing construct or a combined or 2577 // composite construct for which any of the aforementioned constructs is a 2578 // constituent construct and simd or loop are not constituent constructs. 2579 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2580 if (!(isOpenMPParallelDirective(CurDir) || 2581 isOpenMPWorksharingDirective(CurDir)) || 2582 isOpenMPSimdDirective(CurDir)) 2583 S.Diag(RC->getModifierLoc(), 2584 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2585 continue; 2586 } 2587 } 2588 if (InscanFound) { 2589 for (OMPClause *C : Clauses) { 2590 if (C->getClauseKind() != OMPC_reduction) 2591 continue; 2592 auto *RC = cast<OMPReductionClause>(C); 2593 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2594 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2595 ? RC->getBeginLoc() 2596 : RC->getModifierLoc(), 2597 diag::err_omp_inscan_reduction_expected); 2598 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2599 continue; 2600 } 2601 for (Expr *Ref : RC->varlists()) { 2602 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2603 SourceLocation ELoc; 2604 SourceRange ERange; 2605 Expr *SimpleRefExpr = Ref; 2606 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2607 /*AllowArraySection=*/true); 2608 ValueDecl *D = Res.first; 2609 if (!D) 2610 continue; 2611 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2612 S.Diag(Ref->getExprLoc(), 2613 diag::err_omp_reduction_not_inclusive_exclusive) 2614 << Ref->getSourceRange(); 2615 } 2616 } 2617 } 2618 } 2619 } 2620 2621 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2622 ArrayRef<OMPClause *> Clauses); 2623 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2624 bool WithInit); 2625 2626 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2627 const ValueDecl *D, 2628 const DSAStackTy::DSAVarData &DVar, 2629 bool IsLoopIterVar = false); 2630 2631 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2632 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2633 // A variable of class type (or array thereof) that appears in a lastprivate 2634 // clause requires an accessible, unambiguous default constructor for the 2635 // class type, unless the list item is also specified in a firstprivate 2636 // clause. 2637 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2638 for (OMPClause *C : D->clauses()) { 2639 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2640 SmallVector<Expr *, 8> PrivateCopies; 2641 for (Expr *DE : Clause->varlists()) { 2642 if (DE->isValueDependent() || DE->isTypeDependent()) { 2643 PrivateCopies.push_back(nullptr); 2644 continue; 2645 } 2646 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2647 auto *VD = cast<VarDecl>(DRE->getDecl()); 2648 QualType Type = VD->getType().getNonReferenceType(); 2649 const DSAStackTy::DSAVarData DVar = 2650 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2651 if (DVar.CKind == OMPC_lastprivate) { 2652 // Generate helper private variable and initialize it with the 2653 // default value. The address of the original variable is replaced 2654 // by the address of the new private variable in CodeGen. This new 2655 // variable is not added to IdResolver, so the code in the OpenMP 2656 // region uses original variable for proper diagnostics. 2657 VarDecl *VDPrivate = buildVarDecl( 2658 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2659 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2660 ActOnUninitializedDecl(VDPrivate); 2661 if (VDPrivate->isInvalidDecl()) { 2662 PrivateCopies.push_back(nullptr); 2663 continue; 2664 } 2665 PrivateCopies.push_back(buildDeclRefExpr( 2666 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2667 } else { 2668 // The variable is also a firstprivate, so initialization sequence 2669 // for private copy is generated already. 2670 PrivateCopies.push_back(nullptr); 2671 } 2672 } 2673 Clause->setPrivateCopies(PrivateCopies); 2674 continue; 2675 } 2676 // Finalize nontemporal clause by handling private copies, if any. 2677 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2678 SmallVector<Expr *, 8> PrivateRefs; 2679 for (Expr *RefExpr : Clause->varlists()) { 2680 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2681 SourceLocation ELoc; 2682 SourceRange ERange; 2683 Expr *SimpleRefExpr = RefExpr; 2684 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2685 if (Res.second) 2686 // It will be analyzed later. 2687 PrivateRefs.push_back(RefExpr); 2688 ValueDecl *D = Res.first; 2689 if (!D) 2690 continue; 2691 2692 const DSAStackTy::DSAVarData DVar = 2693 DSAStack->getTopDSA(D, /*FromParent=*/false); 2694 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2695 : SimpleRefExpr); 2696 } 2697 Clause->setPrivateRefs(PrivateRefs); 2698 continue; 2699 } 2700 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2701 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2702 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2703 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2704 if (!DRE) 2705 continue; 2706 ValueDecl *VD = DRE->getDecl(); 2707 if (!VD || !isa<VarDecl>(VD)) 2708 continue; 2709 DSAStackTy::DSAVarData DVar = 2710 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2711 // OpenMP [2.12.5, target Construct] 2712 // Memory allocators that appear in a uses_allocators clause cannot 2713 // appear in other data-sharing attribute clauses or data-mapping 2714 // attribute clauses in the same construct. 2715 Expr *MapExpr = nullptr; 2716 if (DVar.RefExpr || 2717 DSAStack->checkMappableExprComponentListsForDecl( 2718 VD, /*CurrentRegionOnly=*/true, 2719 [VD, &MapExpr]( 2720 OMPClauseMappableExprCommon::MappableExprComponentListRef 2721 MapExprComponents, 2722 OpenMPClauseKind C) { 2723 auto MI = MapExprComponents.rbegin(); 2724 auto ME = MapExprComponents.rend(); 2725 if (MI != ME && 2726 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2727 VD->getCanonicalDecl()) { 2728 MapExpr = MI->getAssociatedExpression(); 2729 return true; 2730 } 2731 return false; 2732 })) { 2733 Diag(D.Allocator->getExprLoc(), 2734 diag::err_omp_allocator_used_in_clauses) 2735 << D.Allocator->getSourceRange(); 2736 if (DVar.RefExpr) 2737 reportOriginalDsa(*this, DSAStack, VD, DVar); 2738 else 2739 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2740 << MapExpr->getSourceRange(); 2741 } 2742 } 2743 continue; 2744 } 2745 } 2746 // Check allocate clauses. 2747 if (!CurContext->isDependentContext()) 2748 checkAllocateClauses(*this, DSAStack, D->clauses()); 2749 checkReductionClauses(*this, DSAStack, D->clauses()); 2750 } 2751 2752 DSAStack->pop(); 2753 DiscardCleanupsInEvaluationContext(); 2754 PopExpressionEvaluationContext(); 2755 } 2756 2757 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2758 Expr *NumIterations, Sema &SemaRef, 2759 Scope *S, DSAStackTy *Stack); 2760 2761 namespace { 2762 2763 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2764 private: 2765 Sema &SemaRef; 2766 2767 public: 2768 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2769 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2770 NamedDecl *ND = Candidate.getCorrectionDecl(); 2771 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2772 return VD->hasGlobalStorage() && 2773 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2774 SemaRef.getCurScope()); 2775 } 2776 return false; 2777 } 2778 2779 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2780 return std::make_unique<VarDeclFilterCCC>(*this); 2781 } 2782 2783 }; 2784 2785 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2786 private: 2787 Sema &SemaRef; 2788 2789 public: 2790 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2791 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2792 NamedDecl *ND = Candidate.getCorrectionDecl(); 2793 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2794 isa<FunctionDecl>(ND))) { 2795 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2796 SemaRef.getCurScope()); 2797 } 2798 return false; 2799 } 2800 2801 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2802 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2803 } 2804 }; 2805 2806 } // namespace 2807 2808 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2809 CXXScopeSpec &ScopeSpec, 2810 const DeclarationNameInfo &Id, 2811 OpenMPDirectiveKind Kind) { 2812 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2813 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2814 2815 if (Lookup.isAmbiguous()) 2816 return ExprError(); 2817 2818 VarDecl *VD; 2819 if (!Lookup.isSingleResult()) { 2820 VarDeclFilterCCC CCC(*this); 2821 if (TypoCorrection Corrected = 2822 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2823 CTK_ErrorRecovery)) { 2824 diagnoseTypo(Corrected, 2825 PDiag(Lookup.empty() 2826 ? diag::err_undeclared_var_use_suggest 2827 : diag::err_omp_expected_var_arg_suggest) 2828 << Id.getName()); 2829 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2830 } else { 2831 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2832 : diag::err_omp_expected_var_arg) 2833 << Id.getName(); 2834 return ExprError(); 2835 } 2836 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2837 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2838 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2839 return ExprError(); 2840 } 2841 Lookup.suppressDiagnostics(); 2842 2843 // OpenMP [2.9.2, Syntax, C/C++] 2844 // Variables must be file-scope, namespace-scope, or static block-scope. 2845 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2846 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2847 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2848 bool IsDecl = 2849 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2850 Diag(VD->getLocation(), 2851 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2852 << VD; 2853 return ExprError(); 2854 } 2855 2856 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2857 NamedDecl *ND = CanonicalVD; 2858 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2859 // A threadprivate directive for file-scope variables must appear outside 2860 // any definition or declaration. 2861 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2862 !getCurLexicalContext()->isTranslationUnit()) { 2863 Diag(Id.getLoc(), diag::err_omp_var_scope) 2864 << getOpenMPDirectiveName(Kind) << VD; 2865 bool IsDecl = 2866 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2867 Diag(VD->getLocation(), 2868 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2869 << VD; 2870 return ExprError(); 2871 } 2872 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2873 // A threadprivate directive for static class member variables must appear 2874 // in the class definition, in the same scope in which the member 2875 // variables are declared. 2876 if (CanonicalVD->isStaticDataMember() && 2877 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2878 Diag(Id.getLoc(), diag::err_omp_var_scope) 2879 << getOpenMPDirectiveName(Kind) << VD; 2880 bool IsDecl = 2881 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2882 Diag(VD->getLocation(), 2883 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2884 << VD; 2885 return ExprError(); 2886 } 2887 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2888 // A threadprivate directive for namespace-scope variables must appear 2889 // outside any definition or declaration other than the namespace 2890 // definition itself. 2891 if (CanonicalVD->getDeclContext()->isNamespace() && 2892 (!getCurLexicalContext()->isFileContext() || 2893 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2894 Diag(Id.getLoc(), diag::err_omp_var_scope) 2895 << getOpenMPDirectiveName(Kind) << VD; 2896 bool IsDecl = 2897 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2898 Diag(VD->getLocation(), 2899 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2900 << VD; 2901 return ExprError(); 2902 } 2903 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2904 // A threadprivate directive for static block-scope variables must appear 2905 // in the scope of the variable and not in a nested scope. 2906 if (CanonicalVD->isLocalVarDecl() && CurScope && 2907 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2908 Diag(Id.getLoc(), diag::err_omp_var_scope) 2909 << getOpenMPDirectiveName(Kind) << VD; 2910 bool IsDecl = 2911 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2912 Diag(VD->getLocation(), 2913 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2914 << VD; 2915 return ExprError(); 2916 } 2917 2918 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2919 // A threadprivate directive must lexically precede all references to any 2920 // of the variables in its list. 2921 if (Kind == OMPD_threadprivate && VD->isUsed() && 2922 !DSAStack->isThreadPrivate(VD)) { 2923 Diag(Id.getLoc(), diag::err_omp_var_used) 2924 << getOpenMPDirectiveName(Kind) << VD; 2925 return ExprError(); 2926 } 2927 2928 QualType ExprType = VD->getType().getNonReferenceType(); 2929 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2930 SourceLocation(), VD, 2931 /*RefersToEnclosingVariableOrCapture=*/false, 2932 Id.getLoc(), ExprType, VK_LValue); 2933 } 2934 2935 Sema::DeclGroupPtrTy 2936 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2937 ArrayRef<Expr *> VarList) { 2938 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2939 CurContext->addDecl(D); 2940 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2941 } 2942 return nullptr; 2943 } 2944 2945 namespace { 2946 class LocalVarRefChecker final 2947 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2948 Sema &SemaRef; 2949 2950 public: 2951 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2952 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2953 if (VD->hasLocalStorage()) { 2954 SemaRef.Diag(E->getBeginLoc(), 2955 diag::err_omp_local_var_in_threadprivate_init) 2956 << E->getSourceRange(); 2957 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2958 << VD << VD->getSourceRange(); 2959 return true; 2960 } 2961 } 2962 return false; 2963 } 2964 bool VisitStmt(const Stmt *S) { 2965 for (const Stmt *Child : S->children()) { 2966 if (Child && Visit(Child)) 2967 return true; 2968 } 2969 return false; 2970 } 2971 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2972 }; 2973 } // namespace 2974 2975 OMPThreadPrivateDecl * 2976 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2977 SmallVector<Expr *, 8> Vars; 2978 for (Expr *RefExpr : VarList) { 2979 auto *DE = cast<DeclRefExpr>(RefExpr); 2980 auto *VD = cast<VarDecl>(DE->getDecl()); 2981 SourceLocation ILoc = DE->getExprLoc(); 2982 2983 // Mark variable as used. 2984 VD->setReferenced(); 2985 VD->markUsed(Context); 2986 2987 QualType QType = VD->getType(); 2988 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2989 // It will be analyzed later. 2990 Vars.push_back(DE); 2991 continue; 2992 } 2993 2994 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2995 // A threadprivate variable must not have an incomplete type. 2996 if (RequireCompleteType(ILoc, VD->getType(), 2997 diag::err_omp_threadprivate_incomplete_type)) { 2998 continue; 2999 } 3000 3001 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 3002 // A threadprivate variable must not have a reference type. 3003 if (VD->getType()->isReferenceType()) { 3004 Diag(ILoc, diag::err_omp_ref_type_arg) 3005 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 3006 bool IsDecl = 3007 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3008 Diag(VD->getLocation(), 3009 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3010 << VD; 3011 continue; 3012 } 3013 3014 // Check if this is a TLS variable. If TLS is not being supported, produce 3015 // the corresponding diagnostic. 3016 if ((VD->getTLSKind() != VarDecl::TLS_None && 3017 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 3018 getLangOpts().OpenMPUseTLS && 3019 getASTContext().getTargetInfo().isTLSSupported())) || 3020 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3021 !VD->isLocalVarDecl())) { 3022 Diag(ILoc, diag::err_omp_var_thread_local) 3023 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 3024 bool IsDecl = 3025 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3026 Diag(VD->getLocation(), 3027 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3028 << VD; 3029 continue; 3030 } 3031 3032 // Check if initial value of threadprivate variable reference variable with 3033 // local storage (it is not supported by runtime). 3034 if (const Expr *Init = VD->getAnyInitializer()) { 3035 LocalVarRefChecker Checker(*this); 3036 if (Checker.Visit(Init)) 3037 continue; 3038 } 3039 3040 Vars.push_back(RefExpr); 3041 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 3042 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 3043 Context, SourceRange(Loc, Loc))); 3044 if (ASTMutationListener *ML = Context.getASTMutationListener()) 3045 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 3046 } 3047 OMPThreadPrivateDecl *D = nullptr; 3048 if (!Vars.empty()) { 3049 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 3050 Vars); 3051 D->setAccess(AS_public); 3052 } 3053 return D; 3054 } 3055 3056 static OMPAllocateDeclAttr::AllocatorTypeTy 3057 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 3058 if (!Allocator) 3059 return OMPAllocateDeclAttr::OMPNullMemAlloc; 3060 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3061 Allocator->isInstantiationDependent() || 3062 Allocator->containsUnexpandedParameterPack()) 3063 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3064 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 3065 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3066 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 3067 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 3068 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 3069 llvm::FoldingSetNodeID AEId, DAEId; 3070 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 3071 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 3072 if (AEId == DAEId) { 3073 AllocatorKindRes = AllocatorKind; 3074 break; 3075 } 3076 } 3077 return AllocatorKindRes; 3078 } 3079 3080 static bool checkPreviousOMPAllocateAttribute( 3081 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 3082 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 3083 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 3084 return false; 3085 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 3086 Expr *PrevAllocator = A->getAllocator(); 3087 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 3088 getAllocatorKind(S, Stack, PrevAllocator); 3089 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 3090 if (AllocatorsMatch && 3091 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3092 Allocator && PrevAllocator) { 3093 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3094 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3095 llvm::FoldingSetNodeID AEId, PAEId; 3096 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3097 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3098 AllocatorsMatch = AEId == PAEId; 3099 } 3100 if (!AllocatorsMatch) { 3101 SmallString<256> AllocatorBuffer; 3102 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3103 if (Allocator) 3104 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3105 SmallString<256> PrevAllocatorBuffer; 3106 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3107 if (PrevAllocator) 3108 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3109 S.getPrintingPolicy()); 3110 3111 SourceLocation AllocatorLoc = 3112 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3113 SourceRange AllocatorRange = 3114 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3115 SourceLocation PrevAllocatorLoc = 3116 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3117 SourceRange PrevAllocatorRange = 3118 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3119 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3120 << (Allocator ? 1 : 0) << AllocatorStream.str() 3121 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3122 << AllocatorRange; 3123 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3124 << PrevAllocatorRange; 3125 return true; 3126 } 3127 return false; 3128 } 3129 3130 static void 3131 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3132 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3133 Expr *Allocator, SourceRange SR) { 3134 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3135 return; 3136 if (Allocator && 3137 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3138 Allocator->isInstantiationDependent() || 3139 Allocator->containsUnexpandedParameterPack())) 3140 return; 3141 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3142 Allocator, SR); 3143 VD->addAttr(A); 3144 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3145 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3146 } 3147 3148 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3149 SourceLocation Loc, ArrayRef<Expr *> VarList, 3150 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3151 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3152 Expr *Allocator = nullptr; 3153 if (Clauses.empty()) { 3154 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3155 // allocate directives that appear in a target region must specify an 3156 // allocator clause unless a requires directive with the dynamic_allocators 3157 // clause is present in the same compilation unit. 3158 if (LangOpts.OpenMPIsDevice && 3159 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3160 targetDiag(Loc, diag::err_expected_allocator_clause); 3161 } else { 3162 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3163 } 3164 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3165 getAllocatorKind(*this, DSAStack, Allocator); 3166 SmallVector<Expr *, 8> Vars; 3167 for (Expr *RefExpr : VarList) { 3168 auto *DE = cast<DeclRefExpr>(RefExpr); 3169 auto *VD = cast<VarDecl>(DE->getDecl()); 3170 3171 // Check if this is a TLS variable or global register. 3172 if (VD->getTLSKind() != VarDecl::TLS_None || 3173 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3174 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3175 !VD->isLocalVarDecl())) 3176 continue; 3177 3178 // If the used several times in the allocate directive, the same allocator 3179 // must be used. 3180 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3181 AllocatorKind, Allocator)) 3182 continue; 3183 3184 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3185 // If a list item has a static storage type, the allocator expression in the 3186 // allocator clause must be a constant expression that evaluates to one of 3187 // the predefined memory allocator values. 3188 if (Allocator && VD->hasGlobalStorage()) { 3189 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3190 Diag(Allocator->getExprLoc(), 3191 diag::err_omp_expected_predefined_allocator) 3192 << Allocator->getSourceRange(); 3193 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3194 VarDecl::DeclarationOnly; 3195 Diag(VD->getLocation(), 3196 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3197 << VD; 3198 continue; 3199 } 3200 } 3201 3202 Vars.push_back(RefExpr); 3203 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3204 DE->getSourceRange()); 3205 } 3206 if (Vars.empty()) 3207 return nullptr; 3208 if (!Owner) 3209 Owner = getCurLexicalContext(); 3210 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3211 D->setAccess(AS_public); 3212 Owner->addDecl(D); 3213 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3214 } 3215 3216 Sema::DeclGroupPtrTy 3217 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3218 ArrayRef<OMPClause *> ClauseList) { 3219 OMPRequiresDecl *D = nullptr; 3220 if (!CurContext->isFileContext()) { 3221 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3222 } else { 3223 D = CheckOMPRequiresDecl(Loc, ClauseList); 3224 if (D) { 3225 CurContext->addDecl(D); 3226 DSAStack->addRequiresDecl(D); 3227 } 3228 } 3229 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3230 } 3231 3232 void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc, 3233 OpenMPDirectiveKind DKind, 3234 ArrayRef<std::string> Assumptions, 3235 bool SkippedClauses) { 3236 if (!SkippedClauses && Assumptions.empty()) 3237 Diag(Loc, diag::err_omp_no_clause_for_directive) 3238 << llvm::omp::getAllAssumeClauseOptions() 3239 << llvm::omp::getOpenMPDirectiveName(DKind); 3240 3241 auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc); 3242 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) { 3243 OMPAssumeScoped.push_back(AA); 3244 return; 3245 } 3246 3247 // Global assumes without assumption clauses are ignored. 3248 if (Assumptions.empty()) 3249 return; 3250 3251 assert(DKind == llvm::omp::Directive::OMPD_assumes && 3252 "Unexpected omp assumption directive!"); 3253 OMPAssumeGlobal.push_back(AA); 3254 3255 // The OMPAssumeGlobal scope above will take care of new declarations but 3256 // we also want to apply the assumption to existing ones, e.g., to 3257 // declarations in included headers. To this end, we traverse all existing 3258 // declaration contexts and annotate function declarations here. 3259 SmallVector<DeclContext *, 8> DeclContexts; 3260 auto *Ctx = CurContext; 3261 while (Ctx->getLexicalParent()) 3262 Ctx = Ctx->getLexicalParent(); 3263 DeclContexts.push_back(Ctx); 3264 while (!DeclContexts.empty()) { 3265 DeclContext *DC = DeclContexts.pop_back_val(); 3266 for (auto *SubDC : DC->decls()) { 3267 if (SubDC->isInvalidDecl()) 3268 continue; 3269 if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) { 3270 DeclContexts.push_back(CTD->getTemplatedDecl()); 3271 for (auto *S : CTD->specializations()) 3272 DeclContexts.push_back(S); 3273 continue; 3274 } 3275 if (auto *DC = dyn_cast<DeclContext>(SubDC)) 3276 DeclContexts.push_back(DC); 3277 if (auto *F = dyn_cast<FunctionDecl>(SubDC)) { 3278 F->addAttr(AA); 3279 continue; 3280 } 3281 } 3282 } 3283 } 3284 3285 void Sema::ActOnOpenMPEndAssumesDirective() { 3286 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!"); 3287 OMPAssumeScoped.pop_back(); 3288 } 3289 3290 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3291 ArrayRef<OMPClause *> ClauseList) { 3292 /// For target specific clauses, the requires directive cannot be 3293 /// specified after the handling of any of the target regions in the 3294 /// current compilation unit. 3295 ArrayRef<SourceLocation> TargetLocations = 3296 DSAStack->getEncounteredTargetLocs(); 3297 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3298 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3299 for (const OMPClause *CNew : ClauseList) { 3300 // Check if any of the requires clauses affect target regions. 3301 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3302 isa<OMPUnifiedAddressClause>(CNew) || 3303 isa<OMPReverseOffloadClause>(CNew) || 3304 isa<OMPDynamicAllocatorsClause>(CNew)) { 3305 Diag(Loc, diag::err_omp_directive_before_requires) 3306 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3307 for (SourceLocation TargetLoc : TargetLocations) { 3308 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3309 << "target"; 3310 } 3311 } else if (!AtomicLoc.isInvalid() && 3312 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3313 Diag(Loc, diag::err_omp_directive_before_requires) 3314 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3315 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3316 << "atomic"; 3317 } 3318 } 3319 } 3320 3321 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3322 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3323 ClauseList); 3324 return nullptr; 3325 } 3326 3327 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3328 const ValueDecl *D, 3329 const DSAStackTy::DSAVarData &DVar, 3330 bool IsLoopIterVar) { 3331 if (DVar.RefExpr) { 3332 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3333 << getOpenMPClauseName(DVar.CKind); 3334 return; 3335 } 3336 enum { 3337 PDSA_StaticMemberShared, 3338 PDSA_StaticLocalVarShared, 3339 PDSA_LoopIterVarPrivate, 3340 PDSA_LoopIterVarLinear, 3341 PDSA_LoopIterVarLastprivate, 3342 PDSA_ConstVarShared, 3343 PDSA_GlobalVarShared, 3344 PDSA_TaskVarFirstprivate, 3345 PDSA_LocalVarPrivate, 3346 PDSA_Implicit 3347 } Reason = PDSA_Implicit; 3348 bool ReportHint = false; 3349 auto ReportLoc = D->getLocation(); 3350 auto *VD = dyn_cast<VarDecl>(D); 3351 if (IsLoopIterVar) { 3352 if (DVar.CKind == OMPC_private) 3353 Reason = PDSA_LoopIterVarPrivate; 3354 else if (DVar.CKind == OMPC_lastprivate) 3355 Reason = PDSA_LoopIterVarLastprivate; 3356 else 3357 Reason = PDSA_LoopIterVarLinear; 3358 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3359 DVar.CKind == OMPC_firstprivate) { 3360 Reason = PDSA_TaskVarFirstprivate; 3361 ReportLoc = DVar.ImplicitDSALoc; 3362 } else if (VD && VD->isStaticLocal()) 3363 Reason = PDSA_StaticLocalVarShared; 3364 else if (VD && VD->isStaticDataMember()) 3365 Reason = PDSA_StaticMemberShared; 3366 else if (VD && VD->isFileVarDecl()) 3367 Reason = PDSA_GlobalVarShared; 3368 else if (D->getType().isConstant(SemaRef.getASTContext())) 3369 Reason = PDSA_ConstVarShared; 3370 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3371 ReportHint = true; 3372 Reason = PDSA_LocalVarPrivate; 3373 } 3374 if (Reason != PDSA_Implicit) { 3375 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3376 << Reason << ReportHint 3377 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3378 } else if (DVar.ImplicitDSALoc.isValid()) { 3379 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3380 << getOpenMPClauseName(DVar.CKind); 3381 } 3382 } 3383 3384 static OpenMPMapClauseKind 3385 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3386 bool IsAggregateOrDeclareTarget) { 3387 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3388 switch (M) { 3389 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3390 Kind = OMPC_MAP_alloc; 3391 break; 3392 case OMPC_DEFAULTMAP_MODIFIER_to: 3393 Kind = OMPC_MAP_to; 3394 break; 3395 case OMPC_DEFAULTMAP_MODIFIER_from: 3396 Kind = OMPC_MAP_from; 3397 break; 3398 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3399 Kind = OMPC_MAP_tofrom; 3400 break; 3401 case OMPC_DEFAULTMAP_MODIFIER_present: 3402 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description] 3403 // If implicit-behavior is present, each variable referenced in the 3404 // construct in the category specified by variable-category is treated as if 3405 // it had been listed in a map clause with the map-type of alloc and 3406 // map-type-modifier of present. 3407 Kind = OMPC_MAP_alloc; 3408 break; 3409 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3410 case OMPC_DEFAULTMAP_MODIFIER_last: 3411 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3412 case OMPC_DEFAULTMAP_MODIFIER_none: 3413 case OMPC_DEFAULTMAP_MODIFIER_default: 3414 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3415 // IsAggregateOrDeclareTarget could be true if: 3416 // 1. the implicit behavior for aggregate is tofrom 3417 // 2. it's a declare target link 3418 if (IsAggregateOrDeclareTarget) { 3419 Kind = OMPC_MAP_tofrom; 3420 break; 3421 } 3422 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3423 } 3424 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3425 return Kind; 3426 } 3427 3428 namespace { 3429 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3430 DSAStackTy *Stack; 3431 Sema &SemaRef; 3432 bool ErrorFound = false; 3433 bool TryCaptureCXXThisMembers = false; 3434 CapturedStmt *CS = nullptr; 3435 const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 3436 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3437 llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete]; 3438 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 3439 ImplicitMapModifier[DefaultmapKindNum]; 3440 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3441 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3442 3443 void VisitSubCaptures(OMPExecutableDirective *S) { 3444 // Check implicitly captured variables. 3445 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3446 return; 3447 if (S->getDirectiveKind() == OMPD_atomic || 3448 S->getDirectiveKind() == OMPD_critical || 3449 S->getDirectiveKind() == OMPD_section || 3450 S->getDirectiveKind() == OMPD_master || 3451 S->getDirectiveKind() == OMPD_masked || 3452 isOpenMPLoopTransformationDirective(S->getDirectiveKind())) { 3453 Visit(S->getAssociatedStmt()); 3454 return; 3455 } 3456 visitSubCaptures(S->getInnermostCapturedStmt()); 3457 // Try to capture inner this->member references to generate correct mappings 3458 // and diagnostics. 3459 if (TryCaptureCXXThisMembers || 3460 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3461 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3462 [](const CapturedStmt::Capture &C) { 3463 return C.capturesThis(); 3464 }))) { 3465 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3466 TryCaptureCXXThisMembers = true; 3467 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3468 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3469 } 3470 // In tasks firstprivates are not captured anymore, need to analyze them 3471 // explicitly. 3472 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3473 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3474 for (OMPClause *C : S->clauses()) 3475 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3476 for (Expr *Ref : FC->varlists()) 3477 Visit(Ref); 3478 } 3479 } 3480 } 3481 3482 public: 3483 void VisitDeclRefExpr(DeclRefExpr *E) { 3484 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3485 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3486 E->isInstantiationDependent()) 3487 return; 3488 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3489 // Check the datasharing rules for the expressions in the clauses. 3490 if (!CS) { 3491 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3492 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3493 Visit(CED->getInit()); 3494 return; 3495 } 3496 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3497 // Do not analyze internal variables and do not enclose them into 3498 // implicit clauses. 3499 return; 3500 VD = VD->getCanonicalDecl(); 3501 // Skip internally declared variables. 3502 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3503 !Stack->isImplicitTaskFirstprivate(VD)) 3504 return; 3505 // Skip allocators in uses_allocators clauses. 3506 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3507 return; 3508 3509 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3510 // Check if the variable has explicit DSA set and stop analysis if it so. 3511 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3512 return; 3513 3514 // Skip internally declared static variables. 3515 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3516 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3517 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3518 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3519 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3520 !Stack->isImplicitTaskFirstprivate(VD)) 3521 return; 3522 3523 SourceLocation ELoc = E->getExprLoc(); 3524 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3525 // The default(none) clause requires that each variable that is referenced 3526 // in the construct, and does not have a predetermined data-sharing 3527 // attribute, must have its data-sharing attribute explicitly determined 3528 // by being listed in a data-sharing attribute clause. 3529 if (DVar.CKind == OMPC_unknown && 3530 (Stack->getDefaultDSA() == DSA_none || 3531 Stack->getDefaultDSA() == DSA_firstprivate) && 3532 isImplicitOrExplicitTaskingRegion(DKind) && 3533 VarsWithInheritedDSA.count(VD) == 0) { 3534 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3535 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3536 DSAStackTy::DSAVarData DVar = 3537 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3538 InheritedDSA = DVar.CKind == OMPC_unknown; 3539 } 3540 if (InheritedDSA) 3541 VarsWithInheritedDSA[VD] = E; 3542 return; 3543 } 3544 3545 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3546 // If implicit-behavior is none, each variable referenced in the 3547 // construct that does not have a predetermined data-sharing attribute 3548 // and does not appear in a to or link clause on a declare target 3549 // directive must be listed in a data-mapping attribute clause, a 3550 // data-haring attribute clause (including a data-sharing attribute 3551 // clause on a combined construct where target. is one of the 3552 // constituent constructs), or an is_device_ptr clause. 3553 OpenMPDefaultmapClauseKind ClauseKind = 3554 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3555 if (SemaRef.getLangOpts().OpenMP >= 50) { 3556 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3557 OMPC_DEFAULTMAP_MODIFIER_none; 3558 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3559 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3560 // Only check for data-mapping attribute and is_device_ptr here 3561 // since we have already make sure that the declaration does not 3562 // have a data-sharing attribute above 3563 if (!Stack->checkMappableExprComponentListsForDecl( 3564 VD, /*CurrentRegionOnly=*/true, 3565 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3566 MapExprComponents, 3567 OpenMPClauseKind) { 3568 auto MI = MapExprComponents.rbegin(); 3569 auto ME = MapExprComponents.rend(); 3570 return MI != ME && MI->getAssociatedDeclaration() == VD; 3571 })) { 3572 VarsWithInheritedDSA[VD] = E; 3573 return; 3574 } 3575 } 3576 } 3577 if (SemaRef.getLangOpts().OpenMP > 50) { 3578 bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) == 3579 OMPC_DEFAULTMAP_MODIFIER_present; 3580 if (IsModifierPresent) { 3581 if (llvm::find(ImplicitMapModifier[ClauseKind], 3582 OMPC_MAP_MODIFIER_present) == 3583 std::end(ImplicitMapModifier[ClauseKind])) { 3584 ImplicitMapModifier[ClauseKind].push_back( 3585 OMPC_MAP_MODIFIER_present); 3586 } 3587 } 3588 } 3589 3590 if (isOpenMPTargetExecutionDirective(DKind) && 3591 !Stack->isLoopControlVariable(VD).first) { 3592 if (!Stack->checkMappableExprComponentListsForDecl( 3593 VD, /*CurrentRegionOnly=*/true, 3594 [this](OMPClauseMappableExprCommon::MappableExprComponentListRef 3595 StackComponents, 3596 OpenMPClauseKind) { 3597 if (SemaRef.LangOpts.OpenMP >= 50) 3598 return !StackComponents.empty(); 3599 // Variable is used if it has been marked as an array, array 3600 // section, array shaping or the variable iself. 3601 return StackComponents.size() == 1 || 3602 std::all_of( 3603 std::next(StackComponents.rbegin()), 3604 StackComponents.rend(), 3605 [](const OMPClauseMappableExprCommon:: 3606 MappableComponent &MC) { 3607 return MC.getAssociatedDeclaration() == 3608 nullptr && 3609 (isa<OMPArraySectionExpr>( 3610 MC.getAssociatedExpression()) || 3611 isa<OMPArrayShapingExpr>( 3612 MC.getAssociatedExpression()) || 3613 isa<ArraySubscriptExpr>( 3614 MC.getAssociatedExpression())); 3615 }); 3616 })) { 3617 bool IsFirstprivate = false; 3618 // By default lambdas are captured as firstprivates. 3619 if (const auto *RD = 3620 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3621 IsFirstprivate = RD->isLambda(); 3622 IsFirstprivate = 3623 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3624 if (IsFirstprivate) { 3625 ImplicitFirstprivate.emplace_back(E); 3626 } else { 3627 OpenMPDefaultmapClauseModifier M = 3628 Stack->getDefaultmapModifier(ClauseKind); 3629 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3630 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3631 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3632 } 3633 return; 3634 } 3635 } 3636 3637 // OpenMP [2.9.3.6, Restrictions, p.2] 3638 // A list item that appears in a reduction clause of the innermost 3639 // enclosing worksharing or parallel construct may not be accessed in an 3640 // explicit task. 3641 DVar = Stack->hasInnermostDSA( 3642 VD, 3643 [](OpenMPClauseKind C, bool AppliedToPointee) { 3644 return C == OMPC_reduction && !AppliedToPointee; 3645 }, 3646 [](OpenMPDirectiveKind K) { 3647 return isOpenMPParallelDirective(K) || 3648 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3649 }, 3650 /*FromParent=*/true); 3651 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3652 ErrorFound = true; 3653 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3654 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3655 return; 3656 } 3657 3658 // Define implicit data-sharing attributes for task. 3659 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3660 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3661 (Stack->getDefaultDSA() == DSA_firstprivate && 3662 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3663 !Stack->isLoopControlVariable(VD).first) { 3664 ImplicitFirstprivate.push_back(E); 3665 return; 3666 } 3667 3668 // Store implicitly used globals with declare target link for parent 3669 // target. 3670 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3671 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3672 Stack->addToParentTargetRegionLinkGlobals(E); 3673 return; 3674 } 3675 } 3676 } 3677 void VisitMemberExpr(MemberExpr *E) { 3678 if (E->isTypeDependent() || E->isValueDependent() || 3679 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3680 return; 3681 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3682 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3683 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3684 if (!FD) 3685 return; 3686 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3687 // Check if the variable has explicit DSA set and stop analysis if it 3688 // so. 3689 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3690 return; 3691 3692 if (isOpenMPTargetExecutionDirective(DKind) && 3693 !Stack->isLoopControlVariable(FD).first && 3694 !Stack->checkMappableExprComponentListsForDecl( 3695 FD, /*CurrentRegionOnly=*/true, 3696 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3697 StackComponents, 3698 OpenMPClauseKind) { 3699 return isa<CXXThisExpr>( 3700 cast<MemberExpr>( 3701 StackComponents.back().getAssociatedExpression()) 3702 ->getBase() 3703 ->IgnoreParens()); 3704 })) { 3705 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3706 // A bit-field cannot appear in a map clause. 3707 // 3708 if (FD->isBitField()) 3709 return; 3710 3711 // Check to see if the member expression is referencing a class that 3712 // has already been explicitly mapped 3713 if (Stack->isClassPreviouslyMapped(TE->getType())) 3714 return; 3715 3716 OpenMPDefaultmapClauseModifier Modifier = 3717 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3718 OpenMPDefaultmapClauseKind ClauseKind = 3719 getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD); 3720 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3721 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3722 ImplicitMap[ClauseKind][Kind].emplace_back(E); 3723 return; 3724 } 3725 3726 SourceLocation ELoc = E->getExprLoc(); 3727 // OpenMP [2.9.3.6, Restrictions, p.2] 3728 // A list item that appears in a reduction clause of the innermost 3729 // enclosing worksharing or parallel construct may not be accessed in 3730 // an explicit task. 3731 DVar = Stack->hasInnermostDSA( 3732 FD, 3733 [](OpenMPClauseKind C, bool AppliedToPointee) { 3734 return C == OMPC_reduction && !AppliedToPointee; 3735 }, 3736 [](OpenMPDirectiveKind K) { 3737 return isOpenMPParallelDirective(K) || 3738 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3739 }, 3740 /*FromParent=*/true); 3741 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3742 ErrorFound = true; 3743 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3744 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3745 return; 3746 } 3747 3748 // Define implicit data-sharing attributes for task. 3749 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3750 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3751 !Stack->isLoopControlVariable(FD).first) { 3752 // Check if there is a captured expression for the current field in the 3753 // region. Do not mark it as firstprivate unless there is no captured 3754 // expression. 3755 // TODO: try to make it firstprivate. 3756 if (DVar.CKind != OMPC_unknown) 3757 ImplicitFirstprivate.push_back(E); 3758 } 3759 return; 3760 } 3761 if (isOpenMPTargetExecutionDirective(DKind)) { 3762 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3763 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3764 Stack->getCurrentDirective(), 3765 /*NoDiagnose=*/true)) 3766 return; 3767 const auto *VD = cast<ValueDecl>( 3768 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3769 if (!Stack->checkMappableExprComponentListsForDecl( 3770 VD, /*CurrentRegionOnly=*/true, 3771 [&CurComponents]( 3772 OMPClauseMappableExprCommon::MappableExprComponentListRef 3773 StackComponents, 3774 OpenMPClauseKind) { 3775 auto CCI = CurComponents.rbegin(); 3776 auto CCE = CurComponents.rend(); 3777 for (const auto &SC : llvm::reverse(StackComponents)) { 3778 // Do both expressions have the same kind? 3779 if (CCI->getAssociatedExpression()->getStmtClass() != 3780 SC.getAssociatedExpression()->getStmtClass()) 3781 if (!((isa<OMPArraySectionExpr>( 3782 SC.getAssociatedExpression()) || 3783 isa<OMPArrayShapingExpr>( 3784 SC.getAssociatedExpression())) && 3785 isa<ArraySubscriptExpr>( 3786 CCI->getAssociatedExpression()))) 3787 return false; 3788 3789 const Decl *CCD = CCI->getAssociatedDeclaration(); 3790 const Decl *SCD = SC.getAssociatedDeclaration(); 3791 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3792 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3793 if (SCD != CCD) 3794 return false; 3795 std::advance(CCI, 1); 3796 if (CCI == CCE) 3797 break; 3798 } 3799 return true; 3800 })) { 3801 Visit(E->getBase()); 3802 } 3803 } else if (!TryCaptureCXXThisMembers) { 3804 Visit(E->getBase()); 3805 } 3806 } 3807 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3808 for (OMPClause *C : S->clauses()) { 3809 // Skip analysis of arguments of implicitly defined firstprivate clause 3810 // for task|target directives. 3811 // Skip analysis of arguments of implicitly defined map clause for target 3812 // directives. 3813 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3814 C->isImplicit() && 3815 !isOpenMPTaskingDirective(Stack->getCurrentDirective()))) { 3816 for (Stmt *CC : C->children()) { 3817 if (CC) 3818 Visit(CC); 3819 } 3820 } 3821 } 3822 // Check implicitly captured variables. 3823 VisitSubCaptures(S); 3824 } 3825 3826 void VisitOMPTileDirective(OMPTileDirective *S) { 3827 // #pragma omp tile does not introduce data sharing. 3828 VisitStmt(S); 3829 } 3830 3831 void VisitOMPUnrollDirective(OMPUnrollDirective *S) { 3832 // #pragma omp unroll does not introduce data sharing. 3833 VisitStmt(S); 3834 } 3835 3836 void VisitStmt(Stmt *S) { 3837 for (Stmt *C : S->children()) { 3838 if (C) { 3839 // Check implicitly captured variables in the task-based directives to 3840 // check if they must be firstprivatized. 3841 Visit(C); 3842 } 3843 } 3844 } 3845 3846 void visitSubCaptures(CapturedStmt *S) { 3847 for (const CapturedStmt::Capture &Cap : S->captures()) { 3848 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3849 continue; 3850 VarDecl *VD = Cap.getCapturedVar(); 3851 // Do not try to map the variable if it or its sub-component was mapped 3852 // already. 3853 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3854 Stack->checkMappableExprComponentListsForDecl( 3855 VD, /*CurrentRegionOnly=*/true, 3856 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3857 OpenMPClauseKind) { return true; })) 3858 continue; 3859 DeclRefExpr *DRE = buildDeclRefExpr( 3860 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3861 Cap.getLocation(), /*RefersToCapture=*/true); 3862 Visit(DRE); 3863 } 3864 } 3865 bool isErrorFound() const { return ErrorFound; } 3866 ArrayRef<Expr *> getImplicitFirstprivate() const { 3867 return ImplicitFirstprivate; 3868 } 3869 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK, 3870 OpenMPMapClauseKind MK) const { 3871 return ImplicitMap[DK][MK]; 3872 } 3873 ArrayRef<OpenMPMapModifierKind> 3874 getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const { 3875 return ImplicitMapModifier[Kind]; 3876 } 3877 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3878 return VarsWithInheritedDSA; 3879 } 3880 3881 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3882 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3883 // Process declare target link variables for the target directives. 3884 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3885 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3886 Visit(E); 3887 } 3888 } 3889 }; 3890 } // namespace 3891 3892 static void handleDeclareVariantConstructTrait(DSAStackTy *Stack, 3893 OpenMPDirectiveKind DKind, 3894 bool ScopeEntry) { 3895 SmallVector<llvm::omp::TraitProperty, 8> Traits; 3896 if (isOpenMPTargetExecutionDirective(DKind)) 3897 Traits.emplace_back(llvm::omp::TraitProperty::construct_target_target); 3898 if (isOpenMPTeamsDirective(DKind)) 3899 Traits.emplace_back(llvm::omp::TraitProperty::construct_teams_teams); 3900 if (isOpenMPParallelDirective(DKind)) 3901 Traits.emplace_back(llvm::omp::TraitProperty::construct_parallel_parallel); 3902 if (isOpenMPWorksharingDirective(DKind)) 3903 Traits.emplace_back(llvm::omp::TraitProperty::construct_for_for); 3904 if (isOpenMPSimdDirective(DKind)) 3905 Traits.emplace_back(llvm::omp::TraitProperty::construct_simd_simd); 3906 Stack->handleConstructTrait(Traits, ScopeEntry); 3907 } 3908 3909 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3910 switch (DKind) { 3911 case OMPD_parallel: 3912 case OMPD_parallel_for: 3913 case OMPD_parallel_for_simd: 3914 case OMPD_parallel_sections: 3915 case OMPD_parallel_master: 3916 case OMPD_teams: 3917 case OMPD_teams_distribute: 3918 case OMPD_teams_distribute_simd: { 3919 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3920 QualType KmpInt32PtrTy = 3921 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3922 Sema::CapturedParamNameType Params[] = { 3923 std::make_pair(".global_tid.", KmpInt32PtrTy), 3924 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3925 std::make_pair(StringRef(), QualType()) // __context with shared vars 3926 }; 3927 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3928 Params); 3929 break; 3930 } 3931 case OMPD_target_teams: 3932 case OMPD_target_parallel: 3933 case OMPD_target_parallel_for: 3934 case OMPD_target_parallel_for_simd: 3935 case OMPD_target_teams_distribute: 3936 case OMPD_target_teams_distribute_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 Sema::CapturedParamNameType ParamsTarget[] = { 3964 std::make_pair(StringRef(), QualType()) // __context with shared vars 3965 }; 3966 // Start a captured region for 'target' with no implicit parameters. 3967 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3968 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3969 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3970 std::make_pair(".global_tid.", KmpInt32PtrTy), 3971 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3972 std::make_pair(StringRef(), QualType()) // __context with shared vars 3973 }; 3974 // Start a captured region for 'teams' or 'parallel'. Both regions have 3975 // the same implicit parameters. 3976 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3977 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3978 break; 3979 } 3980 case OMPD_target: 3981 case OMPD_target_simd: { 3982 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3983 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3984 QualType KmpInt32PtrTy = 3985 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3986 QualType Args[] = {VoidPtrTy}; 3987 FunctionProtoType::ExtProtoInfo EPI; 3988 EPI.Variadic = true; 3989 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3990 Sema::CapturedParamNameType Params[] = { 3991 std::make_pair(".global_tid.", KmpInt32Ty), 3992 std::make_pair(".part_id.", KmpInt32PtrTy), 3993 std::make_pair(".privates.", VoidPtrTy), 3994 std::make_pair( 3995 ".copy_fn.", 3996 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3997 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3998 std::make_pair(StringRef(), QualType()) // __context with shared vars 3999 }; 4000 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4001 Params, /*OpenMPCaptureLevel=*/0); 4002 // Mark this captured region as inlined, because we don't use outlined 4003 // function directly. 4004 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4005 AlwaysInlineAttr::CreateImplicit( 4006 Context, {}, AttributeCommonInfo::AS_Keyword, 4007 AlwaysInlineAttr::Keyword_forceinline)); 4008 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4009 std::make_pair(StringRef(), QualType()), 4010 /*OpenMPCaptureLevel=*/1); 4011 break; 4012 } 4013 case OMPD_atomic: 4014 case OMPD_critical: 4015 case OMPD_section: 4016 case OMPD_master: 4017 case OMPD_masked: 4018 case OMPD_tile: 4019 case OMPD_unroll: 4020 break; 4021 case OMPD_simd: 4022 case OMPD_for: 4023 case OMPD_for_simd: 4024 case OMPD_sections: 4025 case OMPD_single: 4026 case OMPD_taskgroup: 4027 case OMPD_distribute: 4028 case OMPD_distribute_simd: 4029 case OMPD_ordered: 4030 case OMPD_target_data: 4031 case OMPD_dispatch: { 4032 Sema::CapturedParamNameType Params[] = { 4033 std::make_pair(StringRef(), QualType()) // __context with shared vars 4034 }; 4035 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4036 Params); 4037 break; 4038 } 4039 case OMPD_task: { 4040 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4041 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4042 QualType KmpInt32PtrTy = 4043 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4044 QualType Args[] = {VoidPtrTy}; 4045 FunctionProtoType::ExtProtoInfo EPI; 4046 EPI.Variadic = true; 4047 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4048 Sema::CapturedParamNameType Params[] = { 4049 std::make_pair(".global_tid.", KmpInt32Ty), 4050 std::make_pair(".part_id.", KmpInt32PtrTy), 4051 std::make_pair(".privates.", VoidPtrTy), 4052 std::make_pair( 4053 ".copy_fn.", 4054 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4055 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4056 std::make_pair(StringRef(), QualType()) // __context with shared vars 4057 }; 4058 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4059 Params); 4060 // Mark this captured region as inlined, because we don't use outlined 4061 // function directly. 4062 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4063 AlwaysInlineAttr::CreateImplicit( 4064 Context, {}, AttributeCommonInfo::AS_Keyword, 4065 AlwaysInlineAttr::Keyword_forceinline)); 4066 break; 4067 } 4068 case OMPD_taskloop: 4069 case OMPD_taskloop_simd: 4070 case OMPD_master_taskloop: 4071 case OMPD_master_taskloop_simd: { 4072 QualType KmpInt32Ty = 4073 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4074 .withConst(); 4075 QualType KmpUInt64Ty = 4076 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4077 .withConst(); 4078 QualType KmpInt64Ty = 4079 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4080 .withConst(); 4081 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4082 QualType KmpInt32PtrTy = 4083 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4084 QualType Args[] = {VoidPtrTy}; 4085 FunctionProtoType::ExtProtoInfo EPI; 4086 EPI.Variadic = true; 4087 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4088 Sema::CapturedParamNameType Params[] = { 4089 std::make_pair(".global_tid.", KmpInt32Ty), 4090 std::make_pair(".part_id.", KmpInt32PtrTy), 4091 std::make_pair(".privates.", VoidPtrTy), 4092 std::make_pair( 4093 ".copy_fn.", 4094 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4095 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4096 std::make_pair(".lb.", KmpUInt64Ty), 4097 std::make_pair(".ub.", KmpUInt64Ty), 4098 std::make_pair(".st.", KmpInt64Ty), 4099 std::make_pair(".liter.", KmpInt32Ty), 4100 std::make_pair(".reductions.", VoidPtrTy), 4101 std::make_pair(StringRef(), QualType()) // __context with shared vars 4102 }; 4103 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4104 Params); 4105 // Mark this captured region as inlined, because we don't use outlined 4106 // function directly. 4107 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4108 AlwaysInlineAttr::CreateImplicit( 4109 Context, {}, AttributeCommonInfo::AS_Keyword, 4110 AlwaysInlineAttr::Keyword_forceinline)); 4111 break; 4112 } 4113 case OMPD_parallel_master_taskloop: 4114 case OMPD_parallel_master_taskloop_simd: { 4115 QualType KmpInt32Ty = 4116 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 4117 .withConst(); 4118 QualType KmpUInt64Ty = 4119 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 4120 .withConst(); 4121 QualType KmpInt64Ty = 4122 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 4123 .withConst(); 4124 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4125 QualType KmpInt32PtrTy = 4126 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4127 Sema::CapturedParamNameType ParamsParallel[] = { 4128 std::make_pair(".global_tid.", KmpInt32PtrTy), 4129 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4130 std::make_pair(StringRef(), QualType()) // __context with shared vars 4131 }; 4132 // Start a captured region for 'parallel'. 4133 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4134 ParamsParallel, /*OpenMPCaptureLevel=*/0); 4135 QualType Args[] = {VoidPtrTy}; 4136 FunctionProtoType::ExtProtoInfo EPI; 4137 EPI.Variadic = true; 4138 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4139 Sema::CapturedParamNameType Params[] = { 4140 std::make_pair(".global_tid.", KmpInt32Ty), 4141 std::make_pair(".part_id.", KmpInt32PtrTy), 4142 std::make_pair(".privates.", VoidPtrTy), 4143 std::make_pair( 4144 ".copy_fn.", 4145 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4146 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4147 std::make_pair(".lb.", KmpUInt64Ty), 4148 std::make_pair(".ub.", KmpUInt64Ty), 4149 std::make_pair(".st.", KmpInt64Ty), 4150 std::make_pair(".liter.", KmpInt32Ty), 4151 std::make_pair(".reductions.", VoidPtrTy), 4152 std::make_pair(StringRef(), QualType()) // __context with shared vars 4153 }; 4154 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4155 Params, /*OpenMPCaptureLevel=*/1); 4156 // Mark this captured region as inlined, because we don't use outlined 4157 // function directly. 4158 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4159 AlwaysInlineAttr::CreateImplicit( 4160 Context, {}, AttributeCommonInfo::AS_Keyword, 4161 AlwaysInlineAttr::Keyword_forceinline)); 4162 break; 4163 } 4164 case OMPD_distribute_parallel_for_simd: 4165 case OMPD_distribute_parallel_for: { 4166 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4167 QualType KmpInt32PtrTy = 4168 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4169 Sema::CapturedParamNameType Params[] = { 4170 std::make_pair(".global_tid.", KmpInt32PtrTy), 4171 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4172 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4173 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4174 std::make_pair(StringRef(), QualType()) // __context with shared vars 4175 }; 4176 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4177 Params); 4178 break; 4179 } 4180 case OMPD_target_teams_distribute_parallel_for: 4181 case OMPD_target_teams_distribute_parallel_for_simd: { 4182 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4183 QualType KmpInt32PtrTy = 4184 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4185 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4186 4187 QualType Args[] = {VoidPtrTy}; 4188 FunctionProtoType::ExtProtoInfo EPI; 4189 EPI.Variadic = true; 4190 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4191 Sema::CapturedParamNameType Params[] = { 4192 std::make_pair(".global_tid.", KmpInt32Ty), 4193 std::make_pair(".part_id.", KmpInt32PtrTy), 4194 std::make_pair(".privates.", VoidPtrTy), 4195 std::make_pair( 4196 ".copy_fn.", 4197 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4198 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4199 std::make_pair(StringRef(), QualType()) // __context with shared vars 4200 }; 4201 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4202 Params, /*OpenMPCaptureLevel=*/0); 4203 // Mark this captured region as inlined, because we don't use outlined 4204 // function directly. 4205 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4206 AlwaysInlineAttr::CreateImplicit( 4207 Context, {}, AttributeCommonInfo::AS_Keyword, 4208 AlwaysInlineAttr::Keyword_forceinline)); 4209 Sema::CapturedParamNameType ParamsTarget[] = { 4210 std::make_pair(StringRef(), QualType()) // __context with shared vars 4211 }; 4212 // Start a captured region for 'target' with no implicit parameters. 4213 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4214 ParamsTarget, /*OpenMPCaptureLevel=*/1); 4215 4216 Sema::CapturedParamNameType ParamsTeams[] = { 4217 std::make_pair(".global_tid.", KmpInt32PtrTy), 4218 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4219 std::make_pair(StringRef(), QualType()) // __context with shared vars 4220 }; 4221 // Start a captured region for 'target' with no implicit parameters. 4222 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4223 ParamsTeams, /*OpenMPCaptureLevel=*/2); 4224 4225 Sema::CapturedParamNameType ParamsParallel[] = { 4226 std::make_pair(".global_tid.", KmpInt32PtrTy), 4227 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4228 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4229 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4230 std::make_pair(StringRef(), QualType()) // __context with shared vars 4231 }; 4232 // Start a captured region for 'teams' or 'parallel'. Both regions have 4233 // the same implicit parameters. 4234 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4235 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4236 break; 4237 } 4238 4239 case OMPD_teams_distribute_parallel_for: 4240 case OMPD_teams_distribute_parallel_for_simd: { 4241 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4242 QualType KmpInt32PtrTy = 4243 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4244 4245 Sema::CapturedParamNameType ParamsTeams[] = { 4246 std::make_pair(".global_tid.", KmpInt32PtrTy), 4247 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4248 std::make_pair(StringRef(), QualType()) // __context with shared vars 4249 }; 4250 // Start a captured region for 'target' with no implicit parameters. 4251 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4252 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4253 4254 Sema::CapturedParamNameType ParamsParallel[] = { 4255 std::make_pair(".global_tid.", KmpInt32PtrTy), 4256 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4257 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4258 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4259 std::make_pair(StringRef(), QualType()) // __context with shared vars 4260 }; 4261 // Start a captured region for 'teams' or 'parallel'. Both regions have 4262 // the same implicit parameters. 4263 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4264 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4265 break; 4266 } 4267 case OMPD_target_update: 4268 case OMPD_target_enter_data: 4269 case OMPD_target_exit_data: { 4270 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4271 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4272 QualType KmpInt32PtrTy = 4273 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4274 QualType Args[] = {VoidPtrTy}; 4275 FunctionProtoType::ExtProtoInfo EPI; 4276 EPI.Variadic = true; 4277 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4278 Sema::CapturedParamNameType Params[] = { 4279 std::make_pair(".global_tid.", KmpInt32Ty), 4280 std::make_pair(".part_id.", KmpInt32PtrTy), 4281 std::make_pair(".privates.", VoidPtrTy), 4282 std::make_pair( 4283 ".copy_fn.", 4284 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4285 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4286 std::make_pair(StringRef(), QualType()) // __context with shared vars 4287 }; 4288 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4289 Params); 4290 // Mark this captured region as inlined, because we don't use outlined 4291 // function directly. 4292 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4293 AlwaysInlineAttr::CreateImplicit( 4294 Context, {}, AttributeCommonInfo::AS_Keyword, 4295 AlwaysInlineAttr::Keyword_forceinline)); 4296 break; 4297 } 4298 case OMPD_threadprivate: 4299 case OMPD_allocate: 4300 case OMPD_taskyield: 4301 case OMPD_barrier: 4302 case OMPD_taskwait: 4303 case OMPD_cancellation_point: 4304 case OMPD_cancel: 4305 case OMPD_flush: 4306 case OMPD_depobj: 4307 case OMPD_scan: 4308 case OMPD_declare_reduction: 4309 case OMPD_declare_mapper: 4310 case OMPD_declare_simd: 4311 case OMPD_declare_target: 4312 case OMPD_end_declare_target: 4313 case OMPD_requires: 4314 case OMPD_declare_variant: 4315 case OMPD_begin_declare_variant: 4316 case OMPD_end_declare_variant: 4317 case OMPD_metadirective: 4318 llvm_unreachable("OpenMP Directive is not allowed"); 4319 case OMPD_unknown: 4320 default: 4321 llvm_unreachable("Unknown OpenMP directive"); 4322 } 4323 DSAStack->setContext(CurContext); 4324 handleDeclareVariantConstructTrait(DSAStack, DKind, /* ScopeEntry */ true); 4325 } 4326 4327 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4328 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4329 } 4330 4331 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4332 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4333 getOpenMPCaptureRegions(CaptureRegions, DKind); 4334 return CaptureRegions.size(); 4335 } 4336 4337 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4338 Expr *CaptureExpr, bool WithInit, 4339 bool AsExpression) { 4340 assert(CaptureExpr); 4341 ASTContext &C = S.getASTContext(); 4342 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4343 QualType Ty = Init->getType(); 4344 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4345 if (S.getLangOpts().CPlusPlus) { 4346 Ty = C.getLValueReferenceType(Ty); 4347 } else { 4348 Ty = C.getPointerType(Ty); 4349 ExprResult Res = 4350 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4351 if (!Res.isUsable()) 4352 return nullptr; 4353 Init = Res.get(); 4354 } 4355 WithInit = true; 4356 } 4357 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4358 CaptureExpr->getBeginLoc()); 4359 if (!WithInit) 4360 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4361 S.CurContext->addHiddenDecl(CED); 4362 Sema::TentativeAnalysisScope Trap(S); 4363 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4364 return CED; 4365 } 4366 4367 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4368 bool WithInit) { 4369 OMPCapturedExprDecl *CD; 4370 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4371 CD = cast<OMPCapturedExprDecl>(VD); 4372 else 4373 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4374 /*AsExpression=*/false); 4375 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4376 CaptureExpr->getExprLoc()); 4377 } 4378 4379 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4380 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4381 if (!Ref) { 4382 OMPCapturedExprDecl *CD = buildCaptureDecl( 4383 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4384 /*WithInit=*/true, /*AsExpression=*/true); 4385 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4386 CaptureExpr->getExprLoc()); 4387 } 4388 ExprResult Res = Ref; 4389 if (!S.getLangOpts().CPlusPlus && 4390 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4391 Ref->getType()->isPointerType()) { 4392 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4393 if (!Res.isUsable()) 4394 return ExprError(); 4395 } 4396 return S.DefaultLvalueConversion(Res.get()); 4397 } 4398 4399 namespace { 4400 // OpenMP directives parsed in this section are represented as a 4401 // CapturedStatement with an associated statement. If a syntax error 4402 // is detected during the parsing of the associated statement, the 4403 // compiler must abort processing and close the CapturedStatement. 4404 // 4405 // Combined directives such as 'target parallel' have more than one 4406 // nested CapturedStatements. This RAII ensures that we unwind out 4407 // of all the nested CapturedStatements when an error is found. 4408 class CaptureRegionUnwinderRAII { 4409 private: 4410 Sema &S; 4411 bool &ErrorFound; 4412 OpenMPDirectiveKind DKind = OMPD_unknown; 4413 4414 public: 4415 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4416 OpenMPDirectiveKind DKind) 4417 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4418 ~CaptureRegionUnwinderRAII() { 4419 if (ErrorFound) { 4420 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4421 while (--ThisCaptureLevel >= 0) 4422 S.ActOnCapturedRegionError(); 4423 } 4424 } 4425 }; 4426 } // namespace 4427 4428 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4429 // Capture variables captured by reference in lambdas for target-based 4430 // directives. 4431 if (!CurContext->isDependentContext() && 4432 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4433 isOpenMPTargetDataManagementDirective( 4434 DSAStack->getCurrentDirective()))) { 4435 QualType Type = V->getType(); 4436 if (const auto *RD = Type.getCanonicalType() 4437 .getNonReferenceType() 4438 ->getAsCXXRecordDecl()) { 4439 bool SavedForceCaptureByReferenceInTargetExecutable = 4440 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4441 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4442 /*V=*/true); 4443 if (RD->isLambda()) { 4444 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4445 FieldDecl *ThisCapture; 4446 RD->getCaptureFields(Captures, ThisCapture); 4447 for (const LambdaCapture &LC : RD->captures()) { 4448 if (LC.getCaptureKind() == LCK_ByRef) { 4449 VarDecl *VD = LC.getCapturedVar(); 4450 DeclContext *VDC = VD->getDeclContext(); 4451 if (!VDC->Encloses(CurContext)) 4452 continue; 4453 MarkVariableReferenced(LC.getLocation(), VD); 4454 } else if (LC.getCaptureKind() == LCK_This) { 4455 QualType ThisTy = getCurrentThisType(); 4456 if (!ThisTy.isNull() && 4457 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4458 CheckCXXThisCapture(LC.getLocation()); 4459 } 4460 } 4461 } 4462 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4463 SavedForceCaptureByReferenceInTargetExecutable); 4464 } 4465 } 4466 } 4467 4468 static bool checkOrderedOrderSpecified(Sema &S, 4469 const ArrayRef<OMPClause *> Clauses) { 4470 const OMPOrderedClause *Ordered = nullptr; 4471 const OMPOrderClause *Order = nullptr; 4472 4473 for (const OMPClause *Clause : Clauses) { 4474 if (Clause->getClauseKind() == OMPC_ordered) 4475 Ordered = cast<OMPOrderedClause>(Clause); 4476 else if (Clause->getClauseKind() == OMPC_order) { 4477 Order = cast<OMPOrderClause>(Clause); 4478 if (Order->getKind() != OMPC_ORDER_concurrent) 4479 Order = nullptr; 4480 } 4481 if (Ordered && Order) 4482 break; 4483 } 4484 4485 if (Ordered && Order) { 4486 S.Diag(Order->getKindKwLoc(), 4487 diag::err_omp_simple_clause_incompatible_with_ordered) 4488 << getOpenMPClauseName(OMPC_order) 4489 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4490 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4491 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4492 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4493 return true; 4494 } 4495 return false; 4496 } 4497 4498 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4499 ArrayRef<OMPClause *> Clauses) { 4500 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(), 4501 /* ScopeEntry */ false); 4502 if (DSAStack->getCurrentDirective() == OMPD_atomic || 4503 DSAStack->getCurrentDirective() == OMPD_critical || 4504 DSAStack->getCurrentDirective() == OMPD_section || 4505 DSAStack->getCurrentDirective() == OMPD_master || 4506 DSAStack->getCurrentDirective() == OMPD_masked) 4507 return S; 4508 4509 bool ErrorFound = false; 4510 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4511 *this, ErrorFound, DSAStack->getCurrentDirective()); 4512 if (!S.isUsable()) { 4513 ErrorFound = true; 4514 return StmtError(); 4515 } 4516 4517 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4518 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4519 OMPOrderedClause *OC = nullptr; 4520 OMPScheduleClause *SC = nullptr; 4521 SmallVector<const OMPLinearClause *, 4> LCs; 4522 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4523 // This is required for proper codegen. 4524 for (OMPClause *Clause : Clauses) { 4525 if (!LangOpts.OpenMPSimd && 4526 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4527 Clause->getClauseKind() == OMPC_in_reduction) { 4528 // Capture taskgroup task_reduction descriptors inside the tasking regions 4529 // with the corresponding in_reduction items. 4530 auto *IRC = cast<OMPInReductionClause>(Clause); 4531 for (Expr *E : IRC->taskgroup_descriptors()) 4532 if (E) 4533 MarkDeclarationsReferencedInExpr(E); 4534 } 4535 if (isOpenMPPrivate(Clause->getClauseKind()) || 4536 Clause->getClauseKind() == OMPC_copyprivate || 4537 (getLangOpts().OpenMPUseTLS && 4538 getASTContext().getTargetInfo().isTLSSupported() && 4539 Clause->getClauseKind() == OMPC_copyin)) { 4540 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4541 // Mark all variables in private list clauses as used in inner region. 4542 for (Stmt *VarRef : Clause->children()) { 4543 if (auto *E = cast_or_null<Expr>(VarRef)) { 4544 MarkDeclarationsReferencedInExpr(E); 4545 } 4546 } 4547 DSAStack->setForceVarCapturing(/*V=*/false); 4548 } else if (isOpenMPLoopTransformationDirective( 4549 DSAStack->getCurrentDirective())) { 4550 assert(CaptureRegions.empty() && 4551 "No captured regions in loop transformation directives."); 4552 } else if (CaptureRegions.size() > 1 || 4553 CaptureRegions.back() != OMPD_unknown) { 4554 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4555 PICs.push_back(C); 4556 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4557 if (Expr *E = C->getPostUpdateExpr()) 4558 MarkDeclarationsReferencedInExpr(E); 4559 } 4560 } 4561 if (Clause->getClauseKind() == OMPC_schedule) 4562 SC = cast<OMPScheduleClause>(Clause); 4563 else if (Clause->getClauseKind() == OMPC_ordered) 4564 OC = cast<OMPOrderedClause>(Clause); 4565 else if (Clause->getClauseKind() == OMPC_linear) 4566 LCs.push_back(cast<OMPLinearClause>(Clause)); 4567 } 4568 // Capture allocator expressions if used. 4569 for (Expr *E : DSAStack->getInnerAllocators()) 4570 MarkDeclarationsReferencedInExpr(E); 4571 // OpenMP, 2.7.1 Loop Construct, Restrictions 4572 // The nonmonotonic modifier cannot be specified if an ordered clause is 4573 // specified. 4574 if (SC && 4575 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4576 SC->getSecondScheduleModifier() == 4577 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4578 OC) { 4579 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4580 ? SC->getFirstScheduleModifierLoc() 4581 : SC->getSecondScheduleModifierLoc(), 4582 diag::err_omp_simple_clause_incompatible_with_ordered) 4583 << getOpenMPClauseName(OMPC_schedule) 4584 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4585 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4586 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4587 ErrorFound = true; 4588 } 4589 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4590 // If an order(concurrent) clause is present, an ordered clause may not appear 4591 // on the same directive. 4592 if (checkOrderedOrderSpecified(*this, Clauses)) 4593 ErrorFound = true; 4594 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4595 for (const OMPLinearClause *C : LCs) { 4596 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4597 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4598 } 4599 ErrorFound = true; 4600 } 4601 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4602 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4603 OC->getNumForLoops()) { 4604 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4605 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4606 ErrorFound = true; 4607 } 4608 if (ErrorFound) { 4609 return StmtError(); 4610 } 4611 StmtResult SR = S; 4612 unsigned CompletedRegions = 0; 4613 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4614 // Mark all variables in private list clauses as used in inner region. 4615 // Required for proper codegen of combined directives. 4616 // TODO: add processing for other clauses. 4617 if (ThisCaptureRegion != OMPD_unknown) { 4618 for (const clang::OMPClauseWithPreInit *C : PICs) { 4619 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4620 // Find the particular capture region for the clause if the 4621 // directive is a combined one with multiple capture regions. 4622 // If the directive is not a combined one, the capture region 4623 // associated with the clause is OMPD_unknown and is generated 4624 // only once. 4625 if (CaptureRegion == ThisCaptureRegion || 4626 CaptureRegion == OMPD_unknown) { 4627 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4628 for (Decl *D : DS->decls()) 4629 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4630 } 4631 } 4632 } 4633 } 4634 if (ThisCaptureRegion == OMPD_target) { 4635 // Capture allocator traits in the target region. They are used implicitly 4636 // and, thus, are not captured by default. 4637 for (OMPClause *C : Clauses) { 4638 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4639 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4640 ++I) { 4641 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4642 if (Expr *E = D.AllocatorTraits) 4643 MarkDeclarationsReferencedInExpr(E); 4644 } 4645 continue; 4646 } 4647 } 4648 } 4649 if (ThisCaptureRegion == OMPD_parallel) { 4650 // Capture temp arrays for inscan reductions and locals in aligned 4651 // clauses. 4652 for (OMPClause *C : Clauses) { 4653 if (auto *RC = dyn_cast<OMPReductionClause>(C)) { 4654 if (RC->getModifier() != OMPC_REDUCTION_inscan) 4655 continue; 4656 for (Expr *E : RC->copy_array_temps()) 4657 MarkDeclarationsReferencedInExpr(E); 4658 } 4659 if (auto *AC = dyn_cast<OMPAlignedClause>(C)) { 4660 for (Expr *E : AC->varlists()) 4661 MarkDeclarationsReferencedInExpr(E); 4662 } 4663 } 4664 } 4665 if (++CompletedRegions == CaptureRegions.size()) 4666 DSAStack->setBodyComplete(); 4667 SR = ActOnCapturedRegionEnd(SR.get()); 4668 } 4669 return SR; 4670 } 4671 4672 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4673 OpenMPDirectiveKind CancelRegion, 4674 SourceLocation StartLoc) { 4675 // CancelRegion is only needed for cancel and cancellation_point. 4676 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4677 return false; 4678 4679 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4680 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4681 return false; 4682 4683 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4684 << getOpenMPDirectiveName(CancelRegion); 4685 return true; 4686 } 4687 4688 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4689 OpenMPDirectiveKind CurrentRegion, 4690 const DeclarationNameInfo &CurrentName, 4691 OpenMPDirectiveKind CancelRegion, 4692 SourceLocation StartLoc) { 4693 if (Stack->getCurScope()) { 4694 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4695 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4696 bool NestingProhibited = false; 4697 bool CloseNesting = true; 4698 bool OrphanSeen = false; 4699 enum { 4700 NoRecommend, 4701 ShouldBeInParallelRegion, 4702 ShouldBeInOrderedRegion, 4703 ShouldBeInTargetRegion, 4704 ShouldBeInTeamsRegion, 4705 ShouldBeInLoopSimdRegion, 4706 } Recommend = NoRecommend; 4707 if (isOpenMPSimdDirective(ParentRegion) && 4708 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4709 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4710 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4711 CurrentRegion != OMPD_scan))) { 4712 // OpenMP [2.16, Nesting of Regions] 4713 // OpenMP constructs may not be nested inside a simd region. 4714 // OpenMP [2.8.1,simd Construct, Restrictions] 4715 // An ordered construct with the simd clause is the only OpenMP 4716 // construct that can appear in the simd region. 4717 // Allowing a SIMD construct nested in another SIMD construct is an 4718 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4719 // message. 4720 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4721 // The only OpenMP constructs that can be encountered during execution of 4722 // a simd region are the atomic construct, the loop construct, the simd 4723 // construct and the ordered construct with the simd clause. 4724 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4725 ? diag::err_omp_prohibited_region_simd 4726 : diag::warn_omp_nesting_simd) 4727 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4728 return CurrentRegion != OMPD_simd; 4729 } 4730 if (ParentRegion == OMPD_atomic) { 4731 // OpenMP [2.16, Nesting of Regions] 4732 // OpenMP constructs may not be nested inside an atomic region. 4733 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4734 return true; 4735 } 4736 if (CurrentRegion == OMPD_section) { 4737 // OpenMP [2.7.2, sections Construct, Restrictions] 4738 // Orphaned section directives are prohibited. That is, the section 4739 // directives must appear within the sections construct and must not be 4740 // encountered elsewhere in the sections region. 4741 if (ParentRegion != OMPD_sections && 4742 ParentRegion != OMPD_parallel_sections) { 4743 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4744 << (ParentRegion != OMPD_unknown) 4745 << getOpenMPDirectiveName(ParentRegion); 4746 return true; 4747 } 4748 return false; 4749 } 4750 // Allow some constructs (except teams and cancellation constructs) to be 4751 // orphaned (they could be used in functions, called from OpenMP regions 4752 // with the required preconditions). 4753 if (ParentRegion == OMPD_unknown && 4754 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4755 CurrentRegion != OMPD_cancellation_point && 4756 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4757 return false; 4758 if (CurrentRegion == OMPD_cancellation_point || 4759 CurrentRegion == OMPD_cancel) { 4760 // OpenMP [2.16, Nesting of Regions] 4761 // A cancellation point construct for which construct-type-clause is 4762 // taskgroup must be nested inside a task construct. A cancellation 4763 // point construct for which construct-type-clause is not taskgroup must 4764 // be closely nested inside an OpenMP construct that matches the type 4765 // specified in construct-type-clause. 4766 // A cancel construct for which construct-type-clause is taskgroup must be 4767 // nested inside a task construct. A cancel construct for which 4768 // construct-type-clause is not taskgroup must be closely nested inside an 4769 // OpenMP construct that matches the type specified in 4770 // construct-type-clause. 4771 NestingProhibited = 4772 !((CancelRegion == OMPD_parallel && 4773 (ParentRegion == OMPD_parallel || 4774 ParentRegion == OMPD_target_parallel)) || 4775 (CancelRegion == OMPD_for && 4776 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4777 ParentRegion == OMPD_target_parallel_for || 4778 ParentRegion == OMPD_distribute_parallel_for || 4779 ParentRegion == OMPD_teams_distribute_parallel_for || 4780 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4781 (CancelRegion == OMPD_taskgroup && 4782 (ParentRegion == OMPD_task || 4783 (SemaRef.getLangOpts().OpenMP >= 50 && 4784 (ParentRegion == OMPD_taskloop || 4785 ParentRegion == OMPD_master_taskloop || 4786 ParentRegion == OMPD_parallel_master_taskloop)))) || 4787 (CancelRegion == OMPD_sections && 4788 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4789 ParentRegion == OMPD_parallel_sections))); 4790 OrphanSeen = ParentRegion == OMPD_unknown; 4791 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) { 4792 // OpenMP 5.1 [2.22, Nesting of Regions] 4793 // A masked region may not be closely nested inside a worksharing, loop, 4794 // atomic, task, or taskloop region. 4795 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4796 isOpenMPTaskingDirective(ParentRegion); 4797 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4798 // OpenMP [2.16, Nesting of Regions] 4799 // A critical region may not be nested (closely or otherwise) inside a 4800 // critical region with the same name. Note that this restriction is not 4801 // sufficient to prevent deadlock. 4802 SourceLocation PreviousCriticalLoc; 4803 bool DeadLock = Stack->hasDirective( 4804 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4805 const DeclarationNameInfo &DNI, 4806 SourceLocation Loc) { 4807 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4808 PreviousCriticalLoc = Loc; 4809 return true; 4810 } 4811 return false; 4812 }, 4813 false /* skip top directive */); 4814 if (DeadLock) { 4815 SemaRef.Diag(StartLoc, 4816 diag::err_omp_prohibited_region_critical_same_name) 4817 << CurrentName.getName(); 4818 if (PreviousCriticalLoc.isValid()) 4819 SemaRef.Diag(PreviousCriticalLoc, 4820 diag::note_omp_previous_critical_region); 4821 return true; 4822 } 4823 } else if (CurrentRegion == OMPD_barrier) { 4824 // OpenMP 5.1 [2.22, Nesting of Regions] 4825 // A barrier region may not be closely nested inside a worksharing, loop, 4826 // task, taskloop, critical, ordered, atomic, or masked region. 4827 NestingProhibited = 4828 isOpenMPWorksharingDirective(ParentRegion) || 4829 isOpenMPTaskingDirective(ParentRegion) || 4830 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4831 ParentRegion == OMPD_parallel_master || 4832 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4833 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4834 !isOpenMPParallelDirective(CurrentRegion) && 4835 !isOpenMPTeamsDirective(CurrentRegion)) { 4836 // OpenMP 5.1 [2.22, Nesting of Regions] 4837 // A loop region that binds to a parallel region or a worksharing region 4838 // may not be closely nested inside a worksharing, loop, task, taskloop, 4839 // critical, ordered, atomic, or masked region. 4840 NestingProhibited = 4841 isOpenMPWorksharingDirective(ParentRegion) || 4842 isOpenMPTaskingDirective(ParentRegion) || 4843 ParentRegion == OMPD_master || ParentRegion == OMPD_masked || 4844 ParentRegion == OMPD_parallel_master || 4845 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 4846 Recommend = ShouldBeInParallelRegion; 4847 } else if (CurrentRegion == OMPD_ordered) { 4848 // OpenMP [2.16, Nesting of Regions] 4849 // An ordered region may not be closely nested inside a critical, 4850 // atomic, or explicit task region. 4851 // An ordered region must be closely nested inside a loop region (or 4852 // parallel loop region) with an ordered clause. 4853 // OpenMP [2.8.1,simd Construct, Restrictions] 4854 // An ordered construct with the simd clause is the only OpenMP construct 4855 // that can appear in the simd region. 4856 NestingProhibited = ParentRegion == OMPD_critical || 4857 isOpenMPTaskingDirective(ParentRegion) || 4858 !(isOpenMPSimdDirective(ParentRegion) || 4859 Stack->isParentOrderedRegion()); 4860 Recommend = ShouldBeInOrderedRegion; 4861 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4862 // OpenMP [2.16, Nesting of Regions] 4863 // If specified, a teams construct must be contained within a target 4864 // construct. 4865 NestingProhibited = 4866 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4867 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4868 ParentRegion != OMPD_target); 4869 OrphanSeen = ParentRegion == OMPD_unknown; 4870 Recommend = ShouldBeInTargetRegion; 4871 } else if (CurrentRegion == OMPD_scan) { 4872 // OpenMP [2.16, Nesting of Regions] 4873 // If specified, a teams construct must be contained within a target 4874 // construct. 4875 NestingProhibited = 4876 SemaRef.LangOpts.OpenMP < 50 || 4877 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4878 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4879 ParentRegion != OMPD_parallel_for_simd); 4880 OrphanSeen = ParentRegion == OMPD_unknown; 4881 Recommend = ShouldBeInLoopSimdRegion; 4882 } 4883 if (!NestingProhibited && 4884 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4885 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4886 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4887 // OpenMP [2.16, Nesting of Regions] 4888 // distribute, parallel, parallel sections, parallel workshare, and the 4889 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4890 // constructs that can be closely nested in the teams region. 4891 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4892 !isOpenMPDistributeDirective(CurrentRegion); 4893 Recommend = ShouldBeInParallelRegion; 4894 } 4895 if (!NestingProhibited && 4896 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4897 // OpenMP 4.5 [2.17 Nesting of Regions] 4898 // The region associated with the distribute construct must be strictly 4899 // nested inside a teams region 4900 NestingProhibited = 4901 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4902 Recommend = ShouldBeInTeamsRegion; 4903 } 4904 if (!NestingProhibited && 4905 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4906 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4907 // OpenMP 4.5 [2.17 Nesting of Regions] 4908 // If a target, target update, target data, target enter data, or 4909 // target exit data construct is encountered during execution of a 4910 // target region, the behavior is unspecified. 4911 NestingProhibited = Stack->hasDirective( 4912 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4913 SourceLocation) { 4914 if (isOpenMPTargetExecutionDirective(K)) { 4915 OffendingRegion = K; 4916 return true; 4917 } 4918 return false; 4919 }, 4920 false /* don't skip top directive */); 4921 CloseNesting = false; 4922 } 4923 if (NestingProhibited) { 4924 if (OrphanSeen) { 4925 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4926 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4927 } else { 4928 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4929 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4930 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4931 } 4932 return true; 4933 } 4934 } 4935 return false; 4936 } 4937 4938 struct Kind2Unsigned { 4939 using argument_type = OpenMPDirectiveKind; 4940 unsigned operator()(argument_type DK) { return unsigned(DK); } 4941 }; 4942 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4943 ArrayRef<OMPClause *> Clauses, 4944 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4945 bool ErrorFound = false; 4946 unsigned NamedModifiersNumber = 0; 4947 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4948 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4949 SmallVector<SourceLocation, 4> NameModifierLoc; 4950 for (const OMPClause *C : Clauses) { 4951 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4952 // At most one if clause without a directive-name-modifier can appear on 4953 // the directive. 4954 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4955 if (FoundNameModifiers[CurNM]) { 4956 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4957 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4958 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4959 ErrorFound = true; 4960 } else if (CurNM != OMPD_unknown) { 4961 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4962 ++NamedModifiersNumber; 4963 } 4964 FoundNameModifiers[CurNM] = IC; 4965 if (CurNM == OMPD_unknown) 4966 continue; 4967 // Check if the specified name modifier is allowed for the current 4968 // directive. 4969 // At most one if clause with the particular directive-name-modifier can 4970 // appear on the directive. 4971 bool MatchFound = false; 4972 for (auto NM : AllowedNameModifiers) { 4973 if (CurNM == NM) { 4974 MatchFound = true; 4975 break; 4976 } 4977 } 4978 if (!MatchFound) { 4979 S.Diag(IC->getNameModifierLoc(), 4980 diag::err_omp_wrong_if_directive_name_modifier) 4981 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4982 ErrorFound = true; 4983 } 4984 } 4985 } 4986 // If any if clause on the directive includes a directive-name-modifier then 4987 // all if clauses on the directive must include a directive-name-modifier. 4988 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4989 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4990 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4991 diag::err_omp_no_more_if_clause); 4992 } else { 4993 std::string Values; 4994 std::string Sep(", "); 4995 unsigned AllowedCnt = 0; 4996 unsigned TotalAllowedNum = 4997 AllowedNameModifiers.size() - NamedModifiersNumber; 4998 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4999 ++Cnt) { 5000 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 5001 if (!FoundNameModifiers[NM]) { 5002 Values += "'"; 5003 Values += getOpenMPDirectiveName(NM); 5004 Values += "'"; 5005 if (AllowedCnt + 2 == TotalAllowedNum) 5006 Values += " or "; 5007 else if (AllowedCnt + 1 != TotalAllowedNum) 5008 Values += Sep; 5009 ++AllowedCnt; 5010 } 5011 } 5012 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 5013 diag::err_omp_unnamed_if_clause) 5014 << (TotalAllowedNum > 1) << Values; 5015 } 5016 for (SourceLocation Loc : NameModifierLoc) { 5017 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 5018 } 5019 ErrorFound = true; 5020 } 5021 return ErrorFound; 5022 } 5023 5024 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 5025 SourceLocation &ELoc, 5026 SourceRange &ERange, 5027 bool AllowArraySection) { 5028 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 5029 RefExpr->containsUnexpandedParameterPack()) 5030 return std::make_pair(nullptr, true); 5031 5032 // OpenMP [3.1, C/C++] 5033 // A list item is a variable name. 5034 // OpenMP [2.9.3.3, Restrictions, p.1] 5035 // A variable that is part of another variable (as an array or 5036 // structure element) cannot appear in a private clause. 5037 RefExpr = RefExpr->IgnoreParens(); 5038 enum { 5039 NoArrayExpr = -1, 5040 ArraySubscript = 0, 5041 OMPArraySection = 1 5042 } IsArrayExpr = NoArrayExpr; 5043 if (AllowArraySection) { 5044 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 5045 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 5046 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5047 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5048 RefExpr = Base; 5049 IsArrayExpr = ArraySubscript; 5050 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 5051 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 5052 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 5053 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 5054 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 5055 Base = TempASE->getBase()->IgnoreParenImpCasts(); 5056 RefExpr = Base; 5057 IsArrayExpr = OMPArraySection; 5058 } 5059 } 5060 ELoc = RefExpr->getExprLoc(); 5061 ERange = RefExpr->getSourceRange(); 5062 RefExpr = RefExpr->IgnoreParenImpCasts(); 5063 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 5064 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 5065 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 5066 (S.getCurrentThisType().isNull() || !ME || 5067 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 5068 !isa<FieldDecl>(ME->getMemberDecl()))) { 5069 if (IsArrayExpr != NoArrayExpr) { 5070 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 5071 << ERange; 5072 } else { 5073 S.Diag(ELoc, 5074 AllowArraySection 5075 ? diag::err_omp_expected_var_name_member_expr_or_array_item 5076 : diag::err_omp_expected_var_name_member_expr) 5077 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 5078 } 5079 return std::make_pair(nullptr, false); 5080 } 5081 return std::make_pair( 5082 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 5083 } 5084 5085 namespace { 5086 /// Checks if the allocator is used in uses_allocators clause to be allowed in 5087 /// target regions. 5088 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 5089 DSAStackTy *S = nullptr; 5090 5091 public: 5092 bool VisitDeclRefExpr(const DeclRefExpr *E) { 5093 return S->isUsesAllocatorsDecl(E->getDecl()) 5094 .getValueOr( 5095 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 5096 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 5097 } 5098 bool VisitStmt(const Stmt *S) { 5099 for (const Stmt *Child : S->children()) { 5100 if (Child && Visit(Child)) 5101 return true; 5102 } 5103 return false; 5104 } 5105 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 5106 }; 5107 } // namespace 5108 5109 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 5110 ArrayRef<OMPClause *> Clauses) { 5111 assert(!S.CurContext->isDependentContext() && 5112 "Expected non-dependent context."); 5113 auto AllocateRange = 5114 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 5115 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 5116 DeclToCopy; 5117 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 5118 return isOpenMPPrivate(C->getClauseKind()); 5119 }); 5120 for (OMPClause *Cl : PrivateRange) { 5121 MutableArrayRef<Expr *>::iterator I, It, Et; 5122 if (Cl->getClauseKind() == OMPC_private) { 5123 auto *PC = cast<OMPPrivateClause>(Cl); 5124 I = PC->private_copies().begin(); 5125 It = PC->varlist_begin(); 5126 Et = PC->varlist_end(); 5127 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 5128 auto *PC = cast<OMPFirstprivateClause>(Cl); 5129 I = PC->private_copies().begin(); 5130 It = PC->varlist_begin(); 5131 Et = PC->varlist_end(); 5132 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 5133 auto *PC = cast<OMPLastprivateClause>(Cl); 5134 I = PC->private_copies().begin(); 5135 It = PC->varlist_begin(); 5136 Et = PC->varlist_end(); 5137 } else if (Cl->getClauseKind() == OMPC_linear) { 5138 auto *PC = cast<OMPLinearClause>(Cl); 5139 I = PC->privates().begin(); 5140 It = PC->varlist_begin(); 5141 Et = PC->varlist_end(); 5142 } else if (Cl->getClauseKind() == OMPC_reduction) { 5143 auto *PC = cast<OMPReductionClause>(Cl); 5144 I = PC->privates().begin(); 5145 It = PC->varlist_begin(); 5146 Et = PC->varlist_end(); 5147 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 5148 auto *PC = cast<OMPTaskReductionClause>(Cl); 5149 I = PC->privates().begin(); 5150 It = PC->varlist_begin(); 5151 Et = PC->varlist_end(); 5152 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 5153 auto *PC = cast<OMPInReductionClause>(Cl); 5154 I = PC->privates().begin(); 5155 It = PC->varlist_begin(); 5156 Et = PC->varlist_end(); 5157 } else { 5158 llvm_unreachable("Expected private clause."); 5159 } 5160 for (Expr *E : llvm::make_range(It, Et)) { 5161 if (!*I) { 5162 ++I; 5163 continue; 5164 } 5165 SourceLocation ELoc; 5166 SourceRange ERange; 5167 Expr *SimpleRefExpr = E; 5168 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 5169 /*AllowArraySection=*/true); 5170 DeclToCopy.try_emplace(Res.first, 5171 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 5172 ++I; 5173 } 5174 } 5175 for (OMPClause *C : AllocateRange) { 5176 auto *AC = cast<OMPAllocateClause>(C); 5177 if (S.getLangOpts().OpenMP >= 50 && 5178 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 5179 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 5180 AC->getAllocator()) { 5181 Expr *Allocator = AC->getAllocator(); 5182 // OpenMP, 2.12.5 target Construct 5183 // Memory allocators that do not appear in a uses_allocators clause cannot 5184 // appear as an allocator in an allocate clause or be used in the target 5185 // region unless a requires directive with the dynamic_allocators clause 5186 // is present in the same compilation unit. 5187 AllocatorChecker Checker(Stack); 5188 if (Checker.Visit(Allocator)) 5189 S.Diag(Allocator->getExprLoc(), 5190 diag::err_omp_allocator_not_in_uses_allocators) 5191 << Allocator->getSourceRange(); 5192 } 5193 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 5194 getAllocatorKind(S, Stack, AC->getAllocator()); 5195 // OpenMP, 2.11.4 allocate Clause, Restrictions. 5196 // For task, taskloop or target directives, allocation requests to memory 5197 // allocators with the trait access set to thread result in unspecified 5198 // behavior. 5199 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 5200 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 5201 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 5202 S.Diag(AC->getAllocator()->getExprLoc(), 5203 diag::warn_omp_allocate_thread_on_task_target_directive) 5204 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 5205 } 5206 for (Expr *E : AC->varlists()) { 5207 SourceLocation ELoc; 5208 SourceRange ERange; 5209 Expr *SimpleRefExpr = E; 5210 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 5211 ValueDecl *VD = Res.first; 5212 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 5213 if (!isOpenMPPrivate(Data.CKind)) { 5214 S.Diag(E->getExprLoc(), 5215 diag::err_omp_expected_private_copy_for_allocate); 5216 continue; 5217 } 5218 VarDecl *PrivateVD = DeclToCopy[VD]; 5219 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 5220 AllocatorKind, AC->getAllocator())) 5221 continue; 5222 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 5223 E->getSourceRange()); 5224 } 5225 } 5226 } 5227 5228 namespace { 5229 /// Rewrite statements and expressions for Sema \p Actions CurContext. 5230 /// 5231 /// Used to wrap already parsed statements/expressions into a new CapturedStmt 5232 /// context. DeclRefExpr used inside the new context are changed to refer to the 5233 /// captured variable instead. 5234 class CaptureVars : public TreeTransform<CaptureVars> { 5235 using BaseTransform = TreeTransform<CaptureVars>; 5236 5237 public: 5238 CaptureVars(Sema &Actions) : BaseTransform(Actions) {} 5239 5240 bool AlwaysRebuild() { return true; } 5241 }; 5242 } // namespace 5243 5244 static VarDecl *precomputeExpr(Sema &Actions, 5245 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E, 5246 StringRef Name) { 5247 Expr *NewE = AssertSuccess(CaptureVars(Actions).TransformExpr(E)); 5248 VarDecl *NewVar = buildVarDecl(Actions, {}, NewE->getType(), Name, nullptr, 5249 dyn_cast<DeclRefExpr>(E->IgnoreImplicit())); 5250 auto *NewDeclStmt = cast<DeclStmt>(AssertSuccess( 5251 Actions.ActOnDeclStmt(Actions.ConvertDeclToDeclGroup(NewVar), {}, {}))); 5252 Actions.AddInitializerToDecl(NewDeclStmt->getSingleDecl(), NewE, false); 5253 BodyStmts.push_back(NewDeclStmt); 5254 return NewVar; 5255 } 5256 5257 /// Create a closure that computes the number of iterations of a loop. 5258 /// 5259 /// \param Actions The Sema object. 5260 /// \param LogicalTy Type for the logical iteration number. 5261 /// \param Rel Comparison operator of the loop condition. 5262 /// \param StartExpr Value of the loop counter at the first iteration. 5263 /// \param StopExpr Expression the loop counter is compared against in the loop 5264 /// condition. \param StepExpr Amount of increment after each iteration. 5265 /// 5266 /// \return Closure (CapturedStmt) of the distance calculation. 5267 static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy, 5268 BinaryOperator::Opcode Rel, 5269 Expr *StartExpr, Expr *StopExpr, 5270 Expr *StepExpr) { 5271 ASTContext &Ctx = Actions.getASTContext(); 5272 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(LogicalTy); 5273 5274 // Captured regions currently don't support return values, we use an 5275 // out-parameter instead. All inputs are implicit captures. 5276 // TODO: Instead of capturing each DeclRefExpr occurring in 5277 // StartExpr/StopExpr/Step, these could also be passed as a value capture. 5278 QualType ResultTy = Ctx.getLValueReferenceType(LogicalTy); 5279 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy}, 5280 {StringRef(), QualType()}}; 5281 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5282 5283 Stmt *Body; 5284 { 5285 Sema::CompoundScopeRAII CompoundScope(Actions); 5286 CapturedDecl *CS = cast<CapturedDecl>(Actions.CurContext); 5287 5288 // Get the LValue expression for the result. 5289 ImplicitParamDecl *DistParam = CS->getParam(0); 5290 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr( 5291 DistParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5292 5293 SmallVector<Stmt *, 4> BodyStmts; 5294 5295 // Capture all referenced variable references. 5296 // TODO: Instead of computing NewStart/NewStop/NewStep inside the 5297 // CapturedStmt, we could compute them before and capture the result, to be 5298 // used jointly with the LoopVar function. 5299 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, StartExpr, ".start"); 5300 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, StopExpr, ".stop"); 5301 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, StepExpr, ".step"); 5302 auto BuildVarRef = [&](VarDecl *VD) { 5303 return buildDeclRefExpr(Actions, VD, VD->getType(), {}); 5304 }; 5305 5306 IntegerLiteral *Zero = IntegerLiteral::Create( 5307 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {}); 5308 Expr *Dist; 5309 if (Rel == BO_NE) { 5310 // When using a != comparison, the increment can be +1 or -1. This can be 5311 // dynamic at runtime, so we need to check for the direction. 5312 Expr *IsNegStep = AssertSuccess( 5313 Actions.BuildBinOp(nullptr, {}, BO_LT, BuildVarRef(NewStep), Zero)); 5314 5315 // Positive increment. 5316 Expr *ForwardRange = AssertSuccess(Actions.BuildBinOp( 5317 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5318 ForwardRange = AssertSuccess( 5319 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, ForwardRange)); 5320 Expr *ForwardDist = AssertSuccess(Actions.BuildBinOp( 5321 nullptr, {}, BO_Div, ForwardRange, BuildVarRef(NewStep))); 5322 5323 // Negative increment. 5324 Expr *BackwardRange = AssertSuccess(Actions.BuildBinOp( 5325 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5326 BackwardRange = AssertSuccess( 5327 Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, BackwardRange)); 5328 Expr *NegIncAmount = AssertSuccess( 5329 Actions.BuildUnaryOp(nullptr, {}, UO_Minus, BuildVarRef(NewStep))); 5330 Expr *BackwardDist = AssertSuccess( 5331 Actions.BuildBinOp(nullptr, {}, BO_Div, BackwardRange, NegIncAmount)); 5332 5333 // Use the appropriate case. 5334 Dist = AssertSuccess(Actions.ActOnConditionalOp( 5335 {}, {}, IsNegStep, BackwardDist, ForwardDist)); 5336 } else { 5337 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) && 5338 "Expected one of these relational operators"); 5339 5340 // We can derive the direction from any other comparison operator. It is 5341 // non well-formed OpenMP if Step increments/decrements in the other 5342 // directions. Whether at least the first iteration passes the loop 5343 // condition. 5344 Expr *HasAnyIteration = AssertSuccess(Actions.BuildBinOp( 5345 nullptr, {}, Rel, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5346 5347 // Compute the range between first and last counter value. 5348 Expr *Range; 5349 if (Rel == BO_GE || Rel == BO_GT) 5350 Range = AssertSuccess(Actions.BuildBinOp( 5351 nullptr, {}, BO_Sub, BuildVarRef(NewStart), BuildVarRef(NewStop))); 5352 else 5353 Range = AssertSuccess(Actions.BuildBinOp( 5354 nullptr, {}, BO_Sub, BuildVarRef(NewStop), BuildVarRef(NewStart))); 5355 5356 // Ensure unsigned range space. 5357 Range = 5358 AssertSuccess(Actions.BuildCStyleCastExpr({}, LogicalTSI, {}, Range)); 5359 5360 if (Rel == BO_LE || Rel == BO_GE) { 5361 // Add one to the range if the relational operator is inclusive. 5362 Range = AssertSuccess(Actions.BuildBinOp( 5363 nullptr, {}, BO_Add, Range, 5364 Actions.ActOnIntegerConstant(SourceLocation(), 1).get())); 5365 } 5366 5367 // Divide by the absolute step amount. 5368 Expr *Divisor = BuildVarRef(NewStep); 5369 if (Rel == BO_GE || Rel == BO_GT) 5370 Divisor = 5371 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, Divisor)); 5372 Dist = AssertSuccess( 5373 Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor)); 5374 5375 // If there is not at least one iteration, the range contains garbage. Fix 5376 // to zero in this case. 5377 Dist = AssertSuccess( 5378 Actions.ActOnConditionalOp({}, {}, HasAnyIteration, Dist, Zero)); 5379 } 5380 5381 // Assign the result to the out-parameter. 5382 Stmt *ResultAssign = AssertSuccess(Actions.BuildBinOp( 5383 Actions.getCurScope(), {}, BO_Assign, DistRef, Dist)); 5384 BodyStmts.push_back(ResultAssign); 5385 5386 Body = AssertSuccess(Actions.ActOnCompoundStmt({}, {}, BodyStmts, false)); 5387 } 5388 5389 return cast<CapturedStmt>( 5390 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5391 } 5392 5393 /// Create a closure that computes the loop variable from the logical iteration 5394 /// number. 5395 /// 5396 /// \param Actions The Sema object. 5397 /// \param LoopVarTy Type for the loop variable used for result value. 5398 /// \param LogicalTy Type for the logical iteration number. 5399 /// \param StartExpr Value of the loop counter at the first iteration. 5400 /// \param Step Amount of increment after each iteration. 5401 /// \param Deref Whether the loop variable is a dereference of the loop 5402 /// counter variable. 5403 /// 5404 /// \return Closure (CapturedStmt) of the loop value calculation. 5405 static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy, 5406 QualType LogicalTy, 5407 DeclRefExpr *StartExpr, Expr *Step, 5408 bool Deref) { 5409 ASTContext &Ctx = Actions.getASTContext(); 5410 5411 // Pass the result as an out-parameter. Passing as return value would require 5412 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to 5413 // invoke a copy constructor. 5414 QualType TargetParamTy = Ctx.getLValueReferenceType(LoopVarTy); 5415 Sema::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy}, 5416 {"Logical", LogicalTy}, 5417 {StringRef(), QualType()}}; 5418 Actions.ActOnCapturedRegionStart({}, nullptr, CR_Default, Params); 5419 5420 // Capture the initial iterator which represents the LoopVar value at the 5421 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update 5422 // it in every iteration, capture it by value before it is modified. 5423 VarDecl *StartVar = cast<VarDecl>(StartExpr->getDecl()); 5424 bool Invalid = Actions.tryCaptureVariable(StartVar, {}, 5425 Sema::TryCapture_ExplicitByVal, {}); 5426 (void)Invalid; 5427 assert(!Invalid && "Expecting capture-by-value to work."); 5428 5429 Expr *Body; 5430 { 5431 Sema::CompoundScopeRAII CompoundScope(Actions); 5432 auto *CS = cast<CapturedDecl>(Actions.CurContext); 5433 5434 ImplicitParamDecl *TargetParam = CS->getParam(0); 5435 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr( 5436 TargetParam, LoopVarTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5437 ImplicitParamDecl *IndvarParam = CS->getParam(1); 5438 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr( 5439 IndvarParam, LogicalTy, VK_LValue, {}, nullptr, nullptr, {}, nullptr); 5440 5441 // Capture the Start expression. 5442 CaptureVars Recap(Actions); 5443 Expr *NewStart = AssertSuccess(Recap.TransformExpr(StartExpr)); 5444 Expr *NewStep = AssertSuccess(Recap.TransformExpr(Step)); 5445 5446 Expr *Skip = AssertSuccess( 5447 Actions.BuildBinOp(nullptr, {}, BO_Mul, NewStep, LogicalRef)); 5448 // TODO: Explicitly cast to the iterator's difference_type instead of 5449 // relying on implicit conversion. 5450 Expr *Advanced = 5451 AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, NewStart, Skip)); 5452 5453 if (Deref) { 5454 // For range-based for-loops convert the loop counter value to a concrete 5455 // loop variable value by dereferencing the iterator. 5456 Advanced = 5457 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Deref, Advanced)); 5458 } 5459 5460 // Assign the result to the output parameter. 5461 Body = AssertSuccess(Actions.BuildBinOp(Actions.getCurScope(), {}, 5462 BO_Assign, TargetRef, Advanced)); 5463 } 5464 return cast<CapturedStmt>( 5465 AssertSuccess(Actions.ActOnCapturedRegionEnd(Body))); 5466 } 5467 5468 StmtResult Sema::ActOnOpenMPCanonicalLoop(Stmt *AStmt) { 5469 ASTContext &Ctx = getASTContext(); 5470 5471 // Extract the common elements of ForStmt and CXXForRangeStmt: 5472 // Loop variable, repeat condition, increment 5473 Expr *Cond, *Inc; 5474 VarDecl *LIVDecl, *LUVDecl; 5475 if (auto *For = dyn_cast<ForStmt>(AStmt)) { 5476 Stmt *Init = For->getInit(); 5477 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Init)) { 5478 // For statement declares loop variable. 5479 LIVDecl = cast<VarDecl>(LCVarDeclStmt->getSingleDecl()); 5480 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Init)) { 5481 // For statement reuses variable. 5482 assert(LCAssign->getOpcode() == BO_Assign && 5483 "init part must be a loop variable assignment"); 5484 auto *CounterRef = cast<DeclRefExpr>(LCAssign->getLHS()); 5485 LIVDecl = cast<VarDecl>(CounterRef->getDecl()); 5486 } else 5487 llvm_unreachable("Cannot determine loop variable"); 5488 LUVDecl = LIVDecl; 5489 5490 Cond = For->getCond(); 5491 Inc = For->getInc(); 5492 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(AStmt)) { 5493 DeclStmt *BeginStmt = RangeFor->getBeginStmt(); 5494 LIVDecl = cast<VarDecl>(BeginStmt->getSingleDecl()); 5495 LUVDecl = RangeFor->getLoopVariable(); 5496 5497 Cond = RangeFor->getCond(); 5498 Inc = RangeFor->getInc(); 5499 } else 5500 llvm_unreachable("unhandled kind of loop"); 5501 5502 QualType CounterTy = LIVDecl->getType(); 5503 QualType LVTy = LUVDecl->getType(); 5504 5505 // Analyze the loop condition. 5506 Expr *LHS, *RHS; 5507 BinaryOperator::Opcode CondRel; 5508 Cond = Cond->IgnoreImplicit(); 5509 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Cond)) { 5510 LHS = CondBinExpr->getLHS(); 5511 RHS = CondBinExpr->getRHS(); 5512 CondRel = CondBinExpr->getOpcode(); 5513 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Cond)) { 5514 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands"); 5515 LHS = CondCXXOp->getArg(0); 5516 RHS = CondCXXOp->getArg(1); 5517 switch (CondCXXOp->getOperator()) { 5518 case OO_ExclaimEqual: 5519 CondRel = BO_NE; 5520 break; 5521 case OO_Less: 5522 CondRel = BO_LT; 5523 break; 5524 case OO_LessEqual: 5525 CondRel = BO_LE; 5526 break; 5527 case OO_Greater: 5528 CondRel = BO_GT; 5529 break; 5530 case OO_GreaterEqual: 5531 CondRel = BO_GE; 5532 break; 5533 default: 5534 llvm_unreachable("unexpected iterator operator"); 5535 } 5536 } else 5537 llvm_unreachable("unexpected loop condition"); 5538 5539 // Normalize such that the loop counter is on the LHS. 5540 if (!isa<DeclRefExpr>(LHS->IgnoreImplicit()) || 5541 cast<DeclRefExpr>(LHS->IgnoreImplicit())->getDecl() != LIVDecl) { 5542 std::swap(LHS, RHS); 5543 CondRel = BinaryOperator::reverseComparisonOp(CondRel); 5544 } 5545 auto *CounterRef = cast<DeclRefExpr>(LHS->IgnoreImplicit()); 5546 5547 // Decide the bit width for the logical iteration counter. By default use the 5548 // unsigned ptrdiff_t integer size (for iterators and pointers). 5549 // TODO: For iterators, use iterator::difference_type, 5550 // std::iterator_traits<>::difference_type or decltype(it - end). 5551 QualType LogicalTy = Ctx.getUnsignedPointerDiffType(); 5552 if (CounterTy->isIntegerType()) { 5553 unsigned BitWidth = Ctx.getIntWidth(CounterTy); 5554 LogicalTy = Ctx.getIntTypeForBitwidth(BitWidth, false); 5555 } 5556 5557 // Analyze the loop increment. 5558 Expr *Step; 5559 if (auto *IncUn = dyn_cast<UnaryOperator>(Inc)) { 5560 int Direction; 5561 switch (IncUn->getOpcode()) { 5562 case UO_PreInc: 5563 case UO_PostInc: 5564 Direction = 1; 5565 break; 5566 case UO_PreDec: 5567 case UO_PostDec: 5568 Direction = -1; 5569 break; 5570 default: 5571 llvm_unreachable("unhandled unary increment operator"); 5572 } 5573 Step = IntegerLiteral::Create( 5574 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {}); 5575 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) { 5576 if (IncBin->getOpcode() == BO_AddAssign) { 5577 Step = IncBin->getRHS(); 5578 } else if (IncBin->getOpcode() == BO_SubAssign) { 5579 Step = 5580 AssertSuccess(BuildUnaryOp(nullptr, {}, UO_Minus, IncBin->getRHS())); 5581 } else 5582 llvm_unreachable("unhandled binary increment operator"); 5583 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Inc)) { 5584 switch (CondCXXOp->getOperator()) { 5585 case OO_PlusPlus: 5586 Step = IntegerLiteral::Create( 5587 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {}); 5588 break; 5589 case OO_MinusMinus: 5590 Step = IntegerLiteral::Create( 5591 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), -1), LogicalTy, {}); 5592 break; 5593 case OO_PlusEqual: 5594 Step = CondCXXOp->getArg(1); 5595 break; 5596 case OO_MinusEqual: 5597 Step = AssertSuccess( 5598 BuildUnaryOp(nullptr, {}, UO_Minus, CondCXXOp->getArg(1))); 5599 break; 5600 default: 5601 llvm_unreachable("unhandled overloaded increment operator"); 5602 } 5603 } else 5604 llvm_unreachable("unknown increment expression"); 5605 5606 CapturedStmt *DistanceFunc = 5607 buildDistanceFunc(*this, LogicalTy, CondRel, LHS, RHS, Step); 5608 CapturedStmt *LoopVarFunc = buildLoopVarFunc( 5609 *this, LVTy, LogicalTy, CounterRef, Step, isa<CXXForRangeStmt>(AStmt)); 5610 DeclRefExpr *LVRef = BuildDeclRefExpr(LUVDecl, LUVDecl->getType(), VK_LValue, 5611 {}, nullptr, nullptr, {}, nullptr); 5612 return OMPCanonicalLoop::create(getASTContext(), AStmt, DistanceFunc, 5613 LoopVarFunc, LVRef); 5614 } 5615 5616 StmtResult Sema::ActOnOpenMPLoopnest(Stmt *AStmt) { 5617 // Handle a literal loop. 5618 if (isa<ForStmt>(AStmt) || isa<CXXForRangeStmt>(AStmt)) 5619 return ActOnOpenMPCanonicalLoop(AStmt); 5620 5621 // If not a literal loop, it must be the result of a loop transformation. 5622 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(AStmt); 5623 assert( 5624 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) && 5625 "Loop transformation directive expected"); 5626 return LoopTransform; 5627 } 5628 5629 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 5630 CXXScopeSpec &MapperIdScopeSpec, 5631 const DeclarationNameInfo &MapperId, 5632 QualType Type, 5633 Expr *UnresolvedMapper); 5634 5635 /// Perform DFS through the structure/class data members trying to find 5636 /// member(s) with user-defined 'default' mapper and generate implicit map 5637 /// clauses for such members with the found 'default' mapper. 5638 static void 5639 processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack, 5640 SmallVectorImpl<OMPClause *> &Clauses) { 5641 // Check for the deault mapper for data members. 5642 if (S.getLangOpts().OpenMP < 50) 5643 return; 5644 SmallVector<OMPClause *, 4> ImplicitMaps; 5645 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) { 5646 auto *C = dyn_cast<OMPMapClause>(Clauses[Cnt]); 5647 if (!C) 5648 continue; 5649 SmallVector<Expr *, 4> SubExprs; 5650 auto *MI = C->mapperlist_begin(); 5651 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End; 5652 ++I, ++MI) { 5653 // Expression is mapped using mapper - skip it. 5654 if (*MI) 5655 continue; 5656 Expr *E = *I; 5657 // Expression is dependent - skip it, build the mapper when it gets 5658 // instantiated. 5659 if (E->isTypeDependent() || E->isValueDependent() || 5660 E->containsUnexpandedParameterPack()) 5661 continue; 5662 // Array section - need to check for the mapping of the array section 5663 // element. 5664 QualType CanonType = E->getType().getCanonicalType(); 5665 if (CanonType->isSpecificBuiltinType(BuiltinType::OMPArraySection)) { 5666 const auto *OASE = cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts()); 5667 QualType BaseType = 5668 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 5669 QualType ElemType; 5670 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 5671 ElemType = ATy->getElementType(); 5672 else 5673 ElemType = BaseType->getPointeeType(); 5674 CanonType = ElemType; 5675 } 5676 5677 // DFS over data members in structures/classes. 5678 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types( 5679 1, {CanonType, nullptr}); 5680 llvm::DenseMap<const Type *, Expr *> Visited; 5681 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain( 5682 1, {nullptr, 1}); 5683 while (!Types.empty()) { 5684 QualType BaseType; 5685 FieldDecl *CurFD; 5686 std::tie(BaseType, CurFD) = Types.pop_back_val(); 5687 while (ParentChain.back().second == 0) 5688 ParentChain.pop_back(); 5689 --ParentChain.back().second; 5690 if (BaseType.isNull()) 5691 continue; 5692 // Only structs/classes are allowed to have mappers. 5693 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl(); 5694 if (!RD) 5695 continue; 5696 auto It = Visited.find(BaseType.getTypePtr()); 5697 if (It == Visited.end()) { 5698 // Try to find the associated user-defined mapper. 5699 CXXScopeSpec MapperIdScopeSpec; 5700 DeclarationNameInfo DefaultMapperId; 5701 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier( 5702 &S.Context.Idents.get("default"))); 5703 DefaultMapperId.setLoc(E->getExprLoc()); 5704 ExprResult ER = buildUserDefinedMapperRef( 5705 S, Stack->getCurScope(), MapperIdScopeSpec, DefaultMapperId, 5706 BaseType, /*UnresolvedMapper=*/nullptr); 5707 if (ER.isInvalid()) 5708 continue; 5709 It = Visited.try_emplace(BaseType.getTypePtr(), ER.get()).first; 5710 } 5711 // Found default mapper. 5712 if (It->second) { 5713 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType, 5714 VK_LValue, OK_Ordinary, E); 5715 OE->setIsUnique(/*V=*/true); 5716 Expr *BaseExpr = OE; 5717 for (const auto &P : ParentChain) { 5718 if (P.first) { 5719 BaseExpr = S.BuildMemberExpr( 5720 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5721 NestedNameSpecifierLoc(), SourceLocation(), P.first, 5722 DeclAccessPair::make(P.first, P.first->getAccess()), 5723 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5724 P.first->getType(), VK_LValue, OK_Ordinary); 5725 BaseExpr = S.DefaultLvalueConversion(BaseExpr).get(); 5726 } 5727 } 5728 if (CurFD) 5729 BaseExpr = S.BuildMemberExpr( 5730 BaseExpr, /*IsArrow=*/false, E->getExprLoc(), 5731 NestedNameSpecifierLoc(), SourceLocation(), CurFD, 5732 DeclAccessPair::make(CurFD, CurFD->getAccess()), 5733 /*HadMultipleCandidates=*/false, DeclarationNameInfo(), 5734 CurFD->getType(), VK_LValue, OK_Ordinary); 5735 SubExprs.push_back(BaseExpr); 5736 continue; 5737 } 5738 // Check for the "default" mapper for data members. 5739 bool FirstIter = true; 5740 for (FieldDecl *FD : RD->fields()) { 5741 if (!FD) 5742 continue; 5743 QualType FieldTy = FD->getType(); 5744 if (FieldTy.isNull() || 5745 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType())) 5746 continue; 5747 if (FirstIter) { 5748 FirstIter = false; 5749 ParentChain.emplace_back(CurFD, 1); 5750 } else { 5751 ++ParentChain.back().second; 5752 } 5753 Types.emplace_back(FieldTy, FD); 5754 } 5755 } 5756 } 5757 if (SubExprs.empty()) 5758 continue; 5759 CXXScopeSpec MapperIdScopeSpec; 5760 DeclarationNameInfo MapperId; 5761 if (OMPClause *NewClause = S.ActOnOpenMPMapClause( 5762 C->getMapTypeModifiers(), C->getMapTypeModifiersLoc(), 5763 MapperIdScopeSpec, MapperId, C->getMapType(), 5764 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5765 SubExprs, OMPVarListLocTy())) 5766 Clauses.push_back(NewClause); 5767 } 5768 } 5769 5770 StmtResult Sema::ActOnOpenMPExecutableDirective( 5771 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 5772 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 5773 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5774 StmtResult Res = StmtError(); 5775 // First check CancelRegion which is then used in checkNestingOfRegions. 5776 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 5777 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 5778 StartLoc)) 5779 return StmtError(); 5780 5781 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 5782 VarsWithInheritedDSAType VarsWithInheritedDSA; 5783 bool ErrorFound = false; 5784 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 5785 if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic && 5786 Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master && 5787 Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind)) { 5788 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5789 5790 // Check default data sharing attributes for referenced variables. 5791 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 5792 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 5793 Stmt *S = AStmt; 5794 while (--ThisCaptureLevel >= 0) 5795 S = cast<CapturedStmt>(S)->getCapturedStmt(); 5796 DSAChecker.Visit(S); 5797 if (!isOpenMPTargetDataManagementDirective(Kind) && 5798 !isOpenMPTaskingDirective(Kind)) { 5799 // Visit subcaptures to generate implicit clauses for captured vars. 5800 auto *CS = cast<CapturedStmt>(AStmt); 5801 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 5802 getOpenMPCaptureRegions(CaptureRegions, Kind); 5803 // Ignore outer tasking regions for target directives. 5804 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 5805 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 5806 DSAChecker.visitSubCaptures(CS); 5807 } 5808 if (DSAChecker.isErrorFound()) 5809 return StmtError(); 5810 // Generate list of implicitly defined firstprivate variables. 5811 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5812 5813 SmallVector<Expr *, 4> ImplicitFirstprivates( 5814 DSAChecker.getImplicitFirstprivate().begin(), 5815 DSAChecker.getImplicitFirstprivate().end()); 5816 const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1; 5817 SmallVector<Expr *, 4> ImplicitMaps[DefaultmapKindNum][OMPC_MAP_delete]; 5818 SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers> 5819 ImplicitMapModifiers[DefaultmapKindNum]; 5820 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers> 5821 ImplicitMapModifiersLoc[DefaultmapKindNum]; 5822 // Get the original location of present modifier from Defaultmap clause. 5823 SourceLocation PresentModifierLocs[DefaultmapKindNum]; 5824 for (OMPClause *C : Clauses) { 5825 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(C)) 5826 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present) 5827 PresentModifierLocs[DMC->getDefaultmapKind()] = 5828 DMC->getDefaultmapModifierLoc(); 5829 } 5830 for (unsigned VC = 0; VC < DefaultmapKindNum; ++VC) { 5831 auto Kind = static_cast<OpenMPDefaultmapClauseKind>(VC); 5832 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5833 ArrayRef<Expr *> ImplicitMap = DSAChecker.getImplicitMap( 5834 Kind, static_cast<OpenMPMapClauseKind>(I)); 5835 ImplicitMaps[VC][I].append(ImplicitMap.begin(), ImplicitMap.end()); 5836 } 5837 ArrayRef<OpenMPMapModifierKind> ImplicitModifier = 5838 DSAChecker.getImplicitMapModifier(Kind); 5839 ImplicitMapModifiers[VC].append(ImplicitModifier.begin(), 5840 ImplicitModifier.end()); 5841 std::fill_n(std::back_inserter(ImplicitMapModifiersLoc[VC]), 5842 ImplicitModifier.size(), PresentModifierLocs[VC]); 5843 } 5844 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5845 for (OMPClause *C : Clauses) { 5846 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5847 for (Expr *E : IRC->taskgroup_descriptors()) 5848 if (E) 5849 ImplicitFirstprivates.emplace_back(E); 5850 } 5851 // OpenMP 5.0, 2.10.1 task Construct 5852 // [detach clause]... The event-handle will be considered as if it was 5853 // specified on a firstprivate clause. 5854 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5855 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5856 } 5857 if (!ImplicitFirstprivates.empty()) { 5858 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5859 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5860 SourceLocation())) { 5861 ClausesWithImplicit.push_back(Implicit); 5862 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5863 ImplicitFirstprivates.size(); 5864 } else { 5865 ErrorFound = true; 5866 } 5867 } 5868 // OpenMP 5.0 [2.19.7] 5869 // If a list item appears in a reduction, lastprivate or linear 5870 // clause on a combined target construct then it is treated as 5871 // if it also appears in a map clause with a map-type of tofrom 5872 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target && 5873 isOpenMPTargetExecutionDirective(Kind)) { 5874 SmallVector<Expr *, 4> ImplicitExprs; 5875 for (OMPClause *C : Clauses) { 5876 if (auto *RC = dyn_cast<OMPReductionClause>(C)) 5877 for (Expr *E : RC->varlists()) 5878 if (!isa<DeclRefExpr>(E->IgnoreParenImpCasts())) 5879 ImplicitExprs.emplace_back(E); 5880 } 5881 if (!ImplicitExprs.empty()) { 5882 ArrayRef<Expr *> Exprs = ImplicitExprs; 5883 CXXScopeSpec MapperIdScopeSpec; 5884 DeclarationNameInfo MapperId; 5885 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5886 OMPC_MAP_MODIFIER_unknown, SourceLocation(), MapperIdScopeSpec, 5887 MapperId, OMPC_MAP_tofrom, 5888 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5889 Exprs, OMPVarListLocTy(), /*NoDiagnose=*/true)) 5890 ClausesWithImplicit.emplace_back(Implicit); 5891 } 5892 } 5893 for (unsigned I = 0, E = DefaultmapKindNum; I < E; ++I) { 5894 int ClauseKindCnt = -1; 5895 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps[I]) { 5896 ++ClauseKindCnt; 5897 if (ImplicitMap.empty()) 5898 continue; 5899 CXXScopeSpec MapperIdScopeSpec; 5900 DeclarationNameInfo MapperId; 5901 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5902 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5903 ImplicitMapModifiers[I], ImplicitMapModifiersLoc[I], 5904 MapperIdScopeSpec, MapperId, Kind, /*IsMapTypeImplicit=*/true, 5905 SourceLocation(), SourceLocation(), ImplicitMap, 5906 OMPVarListLocTy())) { 5907 ClausesWithImplicit.emplace_back(Implicit); 5908 ErrorFound |= cast<OMPMapClause>(Implicit)->varlist_size() != 5909 ImplicitMap.size(); 5910 } else { 5911 ErrorFound = true; 5912 } 5913 } 5914 } 5915 // Build expressions for implicit maps of data members with 'default' 5916 // mappers. 5917 if (LangOpts.OpenMP >= 50) 5918 processImplicitMapsWithDefaultMappers(*this, DSAStack, 5919 ClausesWithImplicit); 5920 } 5921 5922 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5923 switch (Kind) { 5924 case OMPD_parallel: 5925 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5926 EndLoc); 5927 AllowedNameModifiers.push_back(OMPD_parallel); 5928 break; 5929 case OMPD_simd: 5930 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5931 VarsWithInheritedDSA); 5932 if (LangOpts.OpenMP >= 50) 5933 AllowedNameModifiers.push_back(OMPD_simd); 5934 break; 5935 case OMPD_tile: 5936 Res = 5937 ActOnOpenMPTileDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5938 break; 5939 case OMPD_unroll: 5940 Res = ActOnOpenMPUnrollDirective(ClausesWithImplicit, AStmt, StartLoc, 5941 EndLoc); 5942 break; 5943 case OMPD_for: 5944 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5945 VarsWithInheritedDSA); 5946 break; 5947 case OMPD_for_simd: 5948 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5949 EndLoc, VarsWithInheritedDSA); 5950 if (LangOpts.OpenMP >= 50) 5951 AllowedNameModifiers.push_back(OMPD_simd); 5952 break; 5953 case OMPD_sections: 5954 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5955 EndLoc); 5956 break; 5957 case OMPD_section: 5958 assert(ClausesWithImplicit.empty() && 5959 "No clauses are allowed for 'omp section' directive"); 5960 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5961 break; 5962 case OMPD_single: 5963 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5964 EndLoc); 5965 break; 5966 case OMPD_master: 5967 assert(ClausesWithImplicit.empty() && 5968 "No clauses are allowed for 'omp master' directive"); 5969 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5970 break; 5971 case OMPD_masked: 5972 Res = ActOnOpenMPMaskedDirective(ClausesWithImplicit, AStmt, StartLoc, 5973 EndLoc); 5974 break; 5975 case OMPD_critical: 5976 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5977 StartLoc, EndLoc); 5978 break; 5979 case OMPD_parallel_for: 5980 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5981 EndLoc, VarsWithInheritedDSA); 5982 AllowedNameModifiers.push_back(OMPD_parallel); 5983 break; 5984 case OMPD_parallel_for_simd: 5985 Res = ActOnOpenMPParallelForSimdDirective( 5986 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5987 AllowedNameModifiers.push_back(OMPD_parallel); 5988 if (LangOpts.OpenMP >= 50) 5989 AllowedNameModifiers.push_back(OMPD_simd); 5990 break; 5991 case OMPD_parallel_master: 5992 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5993 StartLoc, EndLoc); 5994 AllowedNameModifiers.push_back(OMPD_parallel); 5995 break; 5996 case OMPD_parallel_sections: 5997 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5998 StartLoc, EndLoc); 5999 AllowedNameModifiers.push_back(OMPD_parallel); 6000 break; 6001 case OMPD_task: 6002 Res = 6003 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6004 AllowedNameModifiers.push_back(OMPD_task); 6005 break; 6006 case OMPD_taskyield: 6007 assert(ClausesWithImplicit.empty() && 6008 "No clauses are allowed for 'omp taskyield' directive"); 6009 assert(AStmt == nullptr && 6010 "No associated statement allowed for 'omp taskyield' directive"); 6011 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 6012 break; 6013 case OMPD_barrier: 6014 assert(ClausesWithImplicit.empty() && 6015 "No clauses are allowed for 'omp barrier' directive"); 6016 assert(AStmt == nullptr && 6017 "No associated statement allowed for 'omp barrier' directive"); 6018 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 6019 break; 6020 case OMPD_taskwait: 6021 assert(ClausesWithImplicit.empty() && 6022 "No clauses are allowed for 'omp taskwait' directive"); 6023 assert(AStmt == nullptr && 6024 "No associated statement allowed for 'omp taskwait' directive"); 6025 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 6026 break; 6027 case OMPD_taskgroup: 6028 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 6029 EndLoc); 6030 break; 6031 case OMPD_flush: 6032 assert(AStmt == nullptr && 6033 "No associated statement allowed for 'omp flush' directive"); 6034 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 6035 break; 6036 case OMPD_depobj: 6037 assert(AStmt == nullptr && 6038 "No associated statement allowed for 'omp depobj' directive"); 6039 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 6040 break; 6041 case OMPD_scan: 6042 assert(AStmt == nullptr && 6043 "No associated statement allowed for 'omp scan' directive"); 6044 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 6045 break; 6046 case OMPD_ordered: 6047 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 6048 EndLoc); 6049 break; 6050 case OMPD_atomic: 6051 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 6052 EndLoc); 6053 break; 6054 case OMPD_teams: 6055 Res = 6056 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 6057 break; 6058 case OMPD_target: 6059 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 6060 EndLoc); 6061 AllowedNameModifiers.push_back(OMPD_target); 6062 break; 6063 case OMPD_target_parallel: 6064 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 6065 StartLoc, EndLoc); 6066 AllowedNameModifiers.push_back(OMPD_target); 6067 AllowedNameModifiers.push_back(OMPD_parallel); 6068 break; 6069 case OMPD_target_parallel_for: 6070 Res = ActOnOpenMPTargetParallelForDirective( 6071 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6072 AllowedNameModifiers.push_back(OMPD_target); 6073 AllowedNameModifiers.push_back(OMPD_parallel); 6074 break; 6075 case OMPD_cancellation_point: 6076 assert(ClausesWithImplicit.empty() && 6077 "No clauses are allowed for 'omp cancellation point' directive"); 6078 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 6079 "cancellation point' directive"); 6080 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 6081 break; 6082 case OMPD_cancel: 6083 assert(AStmt == nullptr && 6084 "No associated statement allowed for 'omp cancel' directive"); 6085 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 6086 CancelRegion); 6087 AllowedNameModifiers.push_back(OMPD_cancel); 6088 break; 6089 case OMPD_target_data: 6090 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 6091 EndLoc); 6092 AllowedNameModifiers.push_back(OMPD_target_data); 6093 break; 6094 case OMPD_target_enter_data: 6095 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 6096 EndLoc, AStmt); 6097 AllowedNameModifiers.push_back(OMPD_target_enter_data); 6098 break; 6099 case OMPD_target_exit_data: 6100 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 6101 EndLoc, AStmt); 6102 AllowedNameModifiers.push_back(OMPD_target_exit_data); 6103 break; 6104 case OMPD_taskloop: 6105 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 6106 EndLoc, VarsWithInheritedDSA); 6107 AllowedNameModifiers.push_back(OMPD_taskloop); 6108 break; 6109 case OMPD_taskloop_simd: 6110 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6111 EndLoc, VarsWithInheritedDSA); 6112 AllowedNameModifiers.push_back(OMPD_taskloop); 6113 if (LangOpts.OpenMP >= 50) 6114 AllowedNameModifiers.push_back(OMPD_simd); 6115 break; 6116 case OMPD_master_taskloop: 6117 Res = ActOnOpenMPMasterTaskLoopDirective( 6118 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6119 AllowedNameModifiers.push_back(OMPD_taskloop); 6120 break; 6121 case OMPD_master_taskloop_simd: 6122 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 6123 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6124 AllowedNameModifiers.push_back(OMPD_taskloop); 6125 if (LangOpts.OpenMP >= 50) 6126 AllowedNameModifiers.push_back(OMPD_simd); 6127 break; 6128 case OMPD_parallel_master_taskloop: 6129 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 6130 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6131 AllowedNameModifiers.push_back(OMPD_taskloop); 6132 AllowedNameModifiers.push_back(OMPD_parallel); 6133 break; 6134 case OMPD_parallel_master_taskloop_simd: 6135 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 6136 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6137 AllowedNameModifiers.push_back(OMPD_taskloop); 6138 AllowedNameModifiers.push_back(OMPD_parallel); 6139 if (LangOpts.OpenMP >= 50) 6140 AllowedNameModifiers.push_back(OMPD_simd); 6141 break; 6142 case OMPD_distribute: 6143 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 6144 EndLoc, VarsWithInheritedDSA); 6145 break; 6146 case OMPD_target_update: 6147 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 6148 EndLoc, AStmt); 6149 AllowedNameModifiers.push_back(OMPD_target_update); 6150 break; 6151 case OMPD_distribute_parallel_for: 6152 Res = ActOnOpenMPDistributeParallelForDirective( 6153 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6154 AllowedNameModifiers.push_back(OMPD_parallel); 6155 break; 6156 case OMPD_distribute_parallel_for_simd: 6157 Res = ActOnOpenMPDistributeParallelForSimdDirective( 6158 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6159 AllowedNameModifiers.push_back(OMPD_parallel); 6160 if (LangOpts.OpenMP >= 50) 6161 AllowedNameModifiers.push_back(OMPD_simd); 6162 break; 6163 case OMPD_distribute_simd: 6164 Res = ActOnOpenMPDistributeSimdDirective( 6165 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6166 if (LangOpts.OpenMP >= 50) 6167 AllowedNameModifiers.push_back(OMPD_simd); 6168 break; 6169 case OMPD_target_parallel_for_simd: 6170 Res = ActOnOpenMPTargetParallelForSimdDirective( 6171 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6172 AllowedNameModifiers.push_back(OMPD_target); 6173 AllowedNameModifiers.push_back(OMPD_parallel); 6174 if (LangOpts.OpenMP >= 50) 6175 AllowedNameModifiers.push_back(OMPD_simd); 6176 break; 6177 case OMPD_target_simd: 6178 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 6179 EndLoc, VarsWithInheritedDSA); 6180 AllowedNameModifiers.push_back(OMPD_target); 6181 if (LangOpts.OpenMP >= 50) 6182 AllowedNameModifiers.push_back(OMPD_simd); 6183 break; 6184 case OMPD_teams_distribute: 6185 Res = ActOnOpenMPTeamsDistributeDirective( 6186 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6187 break; 6188 case OMPD_teams_distribute_simd: 6189 Res = ActOnOpenMPTeamsDistributeSimdDirective( 6190 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6191 if (LangOpts.OpenMP >= 50) 6192 AllowedNameModifiers.push_back(OMPD_simd); 6193 break; 6194 case OMPD_teams_distribute_parallel_for_simd: 6195 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6196 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6197 AllowedNameModifiers.push_back(OMPD_parallel); 6198 if (LangOpts.OpenMP >= 50) 6199 AllowedNameModifiers.push_back(OMPD_simd); 6200 break; 6201 case OMPD_teams_distribute_parallel_for: 6202 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 6203 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6204 AllowedNameModifiers.push_back(OMPD_parallel); 6205 break; 6206 case OMPD_target_teams: 6207 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 6208 EndLoc); 6209 AllowedNameModifiers.push_back(OMPD_target); 6210 break; 6211 case OMPD_target_teams_distribute: 6212 Res = ActOnOpenMPTargetTeamsDistributeDirective( 6213 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6214 AllowedNameModifiers.push_back(OMPD_target); 6215 break; 6216 case OMPD_target_teams_distribute_parallel_for: 6217 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6218 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6219 AllowedNameModifiers.push_back(OMPD_target); 6220 AllowedNameModifiers.push_back(OMPD_parallel); 6221 break; 6222 case OMPD_target_teams_distribute_parallel_for_simd: 6223 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6224 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6225 AllowedNameModifiers.push_back(OMPD_target); 6226 AllowedNameModifiers.push_back(OMPD_parallel); 6227 if (LangOpts.OpenMP >= 50) 6228 AllowedNameModifiers.push_back(OMPD_simd); 6229 break; 6230 case OMPD_target_teams_distribute_simd: 6231 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 6232 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 6233 AllowedNameModifiers.push_back(OMPD_target); 6234 if (LangOpts.OpenMP >= 50) 6235 AllowedNameModifiers.push_back(OMPD_simd); 6236 break; 6237 case OMPD_interop: 6238 assert(AStmt == nullptr && 6239 "No associated statement allowed for 'omp interop' directive"); 6240 Res = ActOnOpenMPInteropDirective(ClausesWithImplicit, StartLoc, EndLoc); 6241 break; 6242 case OMPD_dispatch: 6243 Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc, 6244 EndLoc); 6245 break; 6246 case OMPD_declare_target: 6247 case OMPD_end_declare_target: 6248 case OMPD_threadprivate: 6249 case OMPD_allocate: 6250 case OMPD_declare_reduction: 6251 case OMPD_declare_mapper: 6252 case OMPD_declare_simd: 6253 case OMPD_requires: 6254 case OMPD_declare_variant: 6255 case OMPD_begin_declare_variant: 6256 case OMPD_end_declare_variant: 6257 llvm_unreachable("OpenMP Directive is not allowed"); 6258 case OMPD_unknown: 6259 default: 6260 llvm_unreachable("Unknown OpenMP directive"); 6261 } 6262 6263 ErrorFound = Res.isInvalid() || ErrorFound; 6264 6265 // Check variables in the clauses if default(none) or 6266 // default(firstprivate) was specified. 6267 if (DSAStack->getDefaultDSA() == DSA_none || 6268 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6269 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 6270 for (OMPClause *C : Clauses) { 6271 switch (C->getClauseKind()) { 6272 case OMPC_num_threads: 6273 case OMPC_dist_schedule: 6274 // Do not analyse if no parent teams directive. 6275 if (isOpenMPTeamsDirective(Kind)) 6276 break; 6277 continue; 6278 case OMPC_if: 6279 if (isOpenMPTeamsDirective(Kind) && 6280 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 6281 break; 6282 if (isOpenMPParallelDirective(Kind) && 6283 isOpenMPTaskLoopDirective(Kind) && 6284 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 6285 break; 6286 continue; 6287 case OMPC_schedule: 6288 case OMPC_detach: 6289 break; 6290 case OMPC_grainsize: 6291 case OMPC_num_tasks: 6292 case OMPC_final: 6293 case OMPC_priority: 6294 case OMPC_novariants: 6295 case OMPC_nocontext: 6296 // Do not analyze if no parent parallel directive. 6297 if (isOpenMPParallelDirective(Kind)) 6298 break; 6299 continue; 6300 case OMPC_ordered: 6301 case OMPC_device: 6302 case OMPC_num_teams: 6303 case OMPC_thread_limit: 6304 case OMPC_hint: 6305 case OMPC_collapse: 6306 case OMPC_safelen: 6307 case OMPC_simdlen: 6308 case OMPC_sizes: 6309 case OMPC_default: 6310 case OMPC_proc_bind: 6311 case OMPC_private: 6312 case OMPC_firstprivate: 6313 case OMPC_lastprivate: 6314 case OMPC_shared: 6315 case OMPC_reduction: 6316 case OMPC_task_reduction: 6317 case OMPC_in_reduction: 6318 case OMPC_linear: 6319 case OMPC_aligned: 6320 case OMPC_copyin: 6321 case OMPC_copyprivate: 6322 case OMPC_nowait: 6323 case OMPC_untied: 6324 case OMPC_mergeable: 6325 case OMPC_allocate: 6326 case OMPC_read: 6327 case OMPC_write: 6328 case OMPC_update: 6329 case OMPC_capture: 6330 case OMPC_seq_cst: 6331 case OMPC_acq_rel: 6332 case OMPC_acquire: 6333 case OMPC_release: 6334 case OMPC_relaxed: 6335 case OMPC_depend: 6336 case OMPC_threads: 6337 case OMPC_simd: 6338 case OMPC_map: 6339 case OMPC_nogroup: 6340 case OMPC_defaultmap: 6341 case OMPC_to: 6342 case OMPC_from: 6343 case OMPC_use_device_ptr: 6344 case OMPC_use_device_addr: 6345 case OMPC_is_device_ptr: 6346 case OMPC_nontemporal: 6347 case OMPC_order: 6348 case OMPC_destroy: 6349 case OMPC_inclusive: 6350 case OMPC_exclusive: 6351 case OMPC_uses_allocators: 6352 case OMPC_affinity: 6353 continue; 6354 case OMPC_allocator: 6355 case OMPC_flush: 6356 case OMPC_depobj: 6357 case OMPC_threadprivate: 6358 case OMPC_uniform: 6359 case OMPC_unknown: 6360 case OMPC_unified_address: 6361 case OMPC_unified_shared_memory: 6362 case OMPC_reverse_offload: 6363 case OMPC_dynamic_allocators: 6364 case OMPC_atomic_default_mem_order: 6365 case OMPC_device_type: 6366 case OMPC_match: 6367 case OMPC_when: 6368 default: 6369 llvm_unreachable("Unexpected clause"); 6370 } 6371 for (Stmt *CC : C->children()) { 6372 if (CC) 6373 DSAChecker.Visit(CC); 6374 } 6375 } 6376 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 6377 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 6378 } 6379 for (const auto &P : VarsWithInheritedDSA) { 6380 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 6381 continue; 6382 ErrorFound = true; 6383 if (DSAStack->getDefaultDSA() == DSA_none || 6384 DSAStack->getDefaultDSA() == DSA_firstprivate) { 6385 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 6386 << P.first << P.second->getSourceRange(); 6387 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 6388 } else if (getLangOpts().OpenMP >= 50) { 6389 Diag(P.second->getExprLoc(), 6390 diag::err_omp_defaultmap_no_attr_for_variable) 6391 << P.first << P.second->getSourceRange(); 6392 Diag(DSAStack->getDefaultDSALocation(), 6393 diag::note_omp_defaultmap_attr_none); 6394 } 6395 } 6396 6397 if (!AllowedNameModifiers.empty()) 6398 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 6399 ErrorFound; 6400 6401 if (ErrorFound) 6402 return StmtError(); 6403 6404 if (!CurContext->isDependentContext() && 6405 isOpenMPTargetExecutionDirective(Kind) && 6406 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 6407 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 6408 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 6409 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 6410 // Register target to DSA Stack. 6411 DSAStack->addTargetDirLocation(StartLoc); 6412 } 6413 6414 return Res; 6415 } 6416 6417 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 6418 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 6419 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 6420 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 6421 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 6422 assert(Aligneds.size() == Alignments.size()); 6423 assert(Linears.size() == LinModifiers.size()); 6424 assert(Linears.size() == Steps.size()); 6425 if (!DG || DG.get().isNull()) 6426 return DeclGroupPtrTy(); 6427 6428 const int SimdId = 0; 6429 if (!DG.get().isSingleDecl()) { 6430 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6431 << SimdId; 6432 return DG; 6433 } 6434 Decl *ADecl = DG.get().getSingleDecl(); 6435 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6436 ADecl = FTD->getTemplatedDecl(); 6437 6438 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6439 if (!FD) { 6440 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 6441 return DeclGroupPtrTy(); 6442 } 6443 6444 // OpenMP [2.8.2, declare simd construct, Description] 6445 // The parameter of the simdlen clause must be a constant positive integer 6446 // expression. 6447 ExprResult SL; 6448 if (Simdlen) 6449 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 6450 // OpenMP [2.8.2, declare simd construct, Description] 6451 // The special this pointer can be used as if was one of the arguments to the 6452 // function in any of the linear, aligned, or uniform clauses. 6453 // The uniform clause declares one or more arguments to have an invariant 6454 // value for all concurrent invocations of the function in the execution of a 6455 // single SIMD loop. 6456 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 6457 const Expr *UniformedLinearThis = nullptr; 6458 for (const Expr *E : Uniforms) { 6459 E = E->IgnoreParenImpCasts(); 6460 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6461 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 6462 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6463 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6464 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 6465 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 6466 continue; 6467 } 6468 if (isa<CXXThisExpr>(E)) { 6469 UniformedLinearThis = E; 6470 continue; 6471 } 6472 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6473 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6474 } 6475 // OpenMP [2.8.2, declare simd construct, Description] 6476 // The aligned clause declares that the object to which each list item points 6477 // is aligned to the number of bytes expressed in the optional parameter of 6478 // the aligned clause. 6479 // The special this pointer can be used as if was one of the arguments to the 6480 // function in any of the linear, aligned, or uniform clauses. 6481 // The type of list items appearing in the aligned clause must be array, 6482 // pointer, reference to array, or reference to pointer. 6483 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 6484 const Expr *AlignedThis = nullptr; 6485 for (const Expr *E : Aligneds) { 6486 E = E->IgnoreParenImpCasts(); 6487 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6488 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6489 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6490 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6491 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6492 ->getCanonicalDecl() == CanonPVD) { 6493 // OpenMP [2.8.1, simd construct, Restrictions] 6494 // A list-item cannot appear in more than one aligned clause. 6495 if (AlignedArgs.count(CanonPVD) > 0) { 6496 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6497 << 1 << getOpenMPClauseName(OMPC_aligned) 6498 << E->getSourceRange(); 6499 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 6500 diag::note_omp_explicit_dsa) 6501 << getOpenMPClauseName(OMPC_aligned); 6502 continue; 6503 } 6504 AlignedArgs[CanonPVD] = E; 6505 QualType QTy = PVD->getType() 6506 .getNonReferenceType() 6507 .getUnqualifiedType() 6508 .getCanonicalType(); 6509 const Type *Ty = QTy.getTypePtrOrNull(); 6510 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 6511 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 6512 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 6513 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 6514 } 6515 continue; 6516 } 6517 } 6518 if (isa<CXXThisExpr>(E)) { 6519 if (AlignedThis) { 6520 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 6521 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 6522 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 6523 << getOpenMPClauseName(OMPC_aligned); 6524 } 6525 AlignedThis = E; 6526 continue; 6527 } 6528 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6529 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6530 } 6531 // The optional parameter of the aligned clause, alignment, must be a constant 6532 // positive integer expression. If no optional parameter is specified, 6533 // implementation-defined default alignments for SIMD instructions on the 6534 // target platforms are assumed. 6535 SmallVector<const Expr *, 4> NewAligns; 6536 for (Expr *E : Alignments) { 6537 ExprResult Align; 6538 if (E) 6539 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 6540 NewAligns.push_back(Align.get()); 6541 } 6542 // OpenMP [2.8.2, declare simd construct, Description] 6543 // The linear clause declares one or more list items to be private to a SIMD 6544 // lane and to have a linear relationship with respect to the iteration space 6545 // of a loop. 6546 // The special this pointer can be used as if was one of the arguments to the 6547 // function in any of the linear, aligned, or uniform clauses. 6548 // When a linear-step expression is specified in a linear clause it must be 6549 // either a constant integer expression or an integer-typed parameter that is 6550 // specified in a uniform clause on the directive. 6551 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 6552 const bool IsUniformedThis = UniformedLinearThis != nullptr; 6553 auto MI = LinModifiers.begin(); 6554 for (const Expr *E : Linears) { 6555 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 6556 ++MI; 6557 E = E->IgnoreParenImpCasts(); 6558 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 6559 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6560 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6561 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 6562 FD->getParamDecl(PVD->getFunctionScopeIndex()) 6563 ->getCanonicalDecl() == CanonPVD) { 6564 // OpenMP [2.15.3.7, linear Clause, Restrictions] 6565 // A list-item cannot appear in more than one linear clause. 6566 if (LinearArgs.count(CanonPVD) > 0) { 6567 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6568 << getOpenMPClauseName(OMPC_linear) 6569 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 6570 Diag(LinearArgs[CanonPVD]->getExprLoc(), 6571 diag::note_omp_explicit_dsa) 6572 << getOpenMPClauseName(OMPC_linear); 6573 continue; 6574 } 6575 // Each argument can appear in at most one uniform or linear clause. 6576 if (UniformedArgs.count(CanonPVD) > 0) { 6577 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6578 << getOpenMPClauseName(OMPC_linear) 6579 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 6580 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 6581 diag::note_omp_explicit_dsa) 6582 << getOpenMPClauseName(OMPC_uniform); 6583 continue; 6584 } 6585 LinearArgs[CanonPVD] = E; 6586 if (E->isValueDependent() || E->isTypeDependent() || 6587 E->isInstantiationDependent() || 6588 E->containsUnexpandedParameterPack()) 6589 continue; 6590 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 6591 PVD->getOriginalType(), 6592 /*IsDeclareSimd=*/true); 6593 continue; 6594 } 6595 } 6596 if (isa<CXXThisExpr>(E)) { 6597 if (UniformedLinearThis) { 6598 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 6599 << getOpenMPClauseName(OMPC_linear) 6600 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 6601 << E->getSourceRange(); 6602 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 6603 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 6604 : OMPC_linear); 6605 continue; 6606 } 6607 UniformedLinearThis = E; 6608 if (E->isValueDependent() || E->isTypeDependent() || 6609 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6610 continue; 6611 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 6612 E->getType(), /*IsDeclareSimd=*/true); 6613 continue; 6614 } 6615 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 6616 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 6617 } 6618 Expr *Step = nullptr; 6619 Expr *NewStep = nullptr; 6620 SmallVector<Expr *, 4> NewSteps; 6621 for (Expr *E : Steps) { 6622 // Skip the same step expression, it was checked already. 6623 if (Step == E || !E) { 6624 NewSteps.push_back(E ? NewStep : nullptr); 6625 continue; 6626 } 6627 Step = E; 6628 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 6629 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 6630 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 6631 if (UniformedArgs.count(CanonPVD) == 0) { 6632 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 6633 << Step->getSourceRange(); 6634 } else if (E->isValueDependent() || E->isTypeDependent() || 6635 E->isInstantiationDependent() || 6636 E->containsUnexpandedParameterPack() || 6637 CanonPVD->getType()->hasIntegerRepresentation()) { 6638 NewSteps.push_back(Step); 6639 } else { 6640 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 6641 << Step->getSourceRange(); 6642 } 6643 continue; 6644 } 6645 NewStep = Step; 6646 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 6647 !Step->isInstantiationDependent() && 6648 !Step->containsUnexpandedParameterPack()) { 6649 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 6650 .get(); 6651 if (NewStep) 6652 NewStep = 6653 VerifyIntegerConstantExpression(NewStep, /*FIXME*/ AllowFold).get(); 6654 } 6655 NewSteps.push_back(NewStep); 6656 } 6657 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 6658 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 6659 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 6660 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 6661 const_cast<Expr **>(Linears.data()), Linears.size(), 6662 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 6663 NewSteps.data(), NewSteps.size(), SR); 6664 ADecl->addAttr(NewAttr); 6665 return DG; 6666 } 6667 6668 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 6669 QualType NewType) { 6670 assert(NewType->isFunctionProtoType() && 6671 "Expected function type with prototype."); 6672 assert(FD->getType()->isFunctionNoProtoType() && 6673 "Expected function with type with no prototype."); 6674 assert(FDWithProto->getType()->isFunctionProtoType() && 6675 "Expected function with prototype."); 6676 // Synthesize parameters with the same types. 6677 FD->setType(NewType); 6678 SmallVector<ParmVarDecl *, 16> Params; 6679 for (const ParmVarDecl *P : FDWithProto->parameters()) { 6680 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 6681 SourceLocation(), nullptr, P->getType(), 6682 /*TInfo=*/nullptr, SC_None, nullptr); 6683 Param->setScopeInfo(0, Params.size()); 6684 Param->setImplicit(); 6685 Params.push_back(Param); 6686 } 6687 6688 FD->setParams(Params); 6689 } 6690 6691 void Sema::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) { 6692 if (D->isInvalidDecl()) 6693 return; 6694 FunctionDecl *FD = nullptr; 6695 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6696 FD = UTemplDecl->getTemplatedDecl(); 6697 else 6698 FD = cast<FunctionDecl>(D); 6699 assert(FD && "Expected a function declaration!"); 6700 6701 // If we are instantiating templates we do *not* apply scoped assumptions but 6702 // only global ones. We apply scoped assumption to the template definition 6703 // though. 6704 if (!inTemplateInstantiation()) { 6705 for (AssumptionAttr *AA : OMPAssumeScoped) 6706 FD->addAttr(AA); 6707 } 6708 for (AssumptionAttr *AA : OMPAssumeGlobal) 6709 FD->addAttr(AA); 6710 } 6711 6712 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 6713 : TI(&TI), NameSuffix(TI.getMangledName()) {} 6714 6715 void Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope( 6716 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists, 6717 SmallVectorImpl<FunctionDecl *> &Bases) { 6718 if (!D.getIdentifier()) 6719 return; 6720 6721 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6722 6723 // Template specialization is an extension, check if we do it. 6724 bool IsTemplated = !TemplateParamLists.empty(); 6725 if (IsTemplated & 6726 !DVScope.TI->isExtensionActive( 6727 llvm::omp::TraitProperty::implementation_extension_allow_templates)) 6728 return; 6729 6730 IdentifierInfo *BaseII = D.getIdentifier(); 6731 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 6732 LookupOrdinaryName); 6733 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 6734 6735 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 6736 QualType FType = TInfo->getType(); 6737 6738 bool IsConstexpr = 6739 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr; 6740 bool IsConsteval = 6741 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval; 6742 6743 for (auto *Candidate : Lookup) { 6744 auto *CandidateDecl = Candidate->getUnderlyingDecl(); 6745 FunctionDecl *UDecl = nullptr; 6746 if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) 6747 UDecl = cast<FunctionTemplateDecl>(CandidateDecl)->getTemplatedDecl(); 6748 else if (!IsTemplated) 6749 UDecl = dyn_cast<FunctionDecl>(CandidateDecl); 6750 if (!UDecl) 6751 continue; 6752 6753 // Don't specialize constexpr/consteval functions with 6754 // non-constexpr/consteval functions. 6755 if (UDecl->isConstexpr() && !IsConstexpr) 6756 continue; 6757 if (UDecl->isConsteval() && !IsConsteval) 6758 continue; 6759 6760 QualType UDeclTy = UDecl->getType(); 6761 if (!UDeclTy->isDependentType()) { 6762 QualType NewType = Context.mergeFunctionTypes( 6763 FType, UDeclTy, /* OfBlockPointer */ false, 6764 /* Unqualified */ false, /* AllowCXX */ true); 6765 if (NewType.isNull()) 6766 continue; 6767 } 6768 6769 // Found a base! 6770 Bases.push_back(UDecl); 6771 } 6772 6773 bool UseImplicitBase = !DVScope.TI->isExtensionActive( 6774 llvm::omp::TraitProperty::implementation_extension_disable_implicit_base); 6775 // If no base was found we create a declaration that we use as base. 6776 if (Bases.empty() && UseImplicitBase) { 6777 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration); 6778 Decl *BaseD = HandleDeclarator(S, D, TemplateParamLists); 6779 BaseD->setImplicit(true); 6780 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(BaseD)) 6781 Bases.push_back(BaseTemplD->getTemplatedDecl()); 6782 else 6783 Bases.push_back(cast<FunctionDecl>(BaseD)); 6784 } 6785 6786 std::string MangledName; 6787 MangledName += D.getIdentifier()->getName(); 6788 MangledName += getOpenMPVariantManglingSeparatorStr(); 6789 MangledName += DVScope.NameSuffix; 6790 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 6791 6792 VariantII.setMangledOpenMPVariantName(true); 6793 D.SetIdentifier(&VariantII, D.getBeginLoc()); 6794 } 6795 6796 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 6797 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) { 6798 // Do not mark function as is used to prevent its emission if this is the 6799 // only place where it is used. 6800 EnterExpressionEvaluationContext Unevaluated( 6801 *this, Sema::ExpressionEvaluationContext::Unevaluated); 6802 6803 FunctionDecl *FD = nullptr; 6804 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(D)) 6805 FD = UTemplDecl->getTemplatedDecl(); 6806 else 6807 FD = cast<FunctionDecl>(D); 6808 auto *VariantFuncRef = DeclRefExpr::Create( 6809 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 6810 /* RefersToEnclosingVariableOrCapture */ false, 6811 /* NameLoc */ FD->getLocation(), FD->getType(), 6812 ExprValueKind::VK_PRValue); 6813 6814 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 6815 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 6816 Context, VariantFuncRef, DVScope.TI); 6817 for (FunctionDecl *BaseFD : Bases) 6818 BaseFD->addAttr(OMPDeclareVariantA); 6819 } 6820 6821 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 6822 SourceLocation LParenLoc, 6823 MultiExprArg ArgExprs, 6824 SourceLocation RParenLoc, Expr *ExecConfig) { 6825 // The common case is a regular call we do not want to specialize at all. Try 6826 // to make that case fast by bailing early. 6827 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 6828 if (!CE) 6829 return Call; 6830 6831 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 6832 if (!CalleeFnDecl) 6833 return Call; 6834 6835 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 6836 return Call; 6837 6838 ASTContext &Context = getASTContext(); 6839 std::function<void(StringRef)> DiagUnknownTrait = [this, 6840 CE](StringRef ISATrait) { 6841 // TODO Track the selector locations in a way that is accessible here to 6842 // improve the diagnostic location. 6843 Diag(CE->getBeginLoc(), diag::warn_unknown_declare_variant_isa_trait) 6844 << ISATrait; 6845 }; 6846 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait), 6847 getCurFunctionDecl(), DSAStack->getConstructTraits()); 6848 6849 QualType CalleeFnType = CalleeFnDecl->getType(); 6850 6851 SmallVector<Expr *, 4> Exprs; 6852 SmallVector<VariantMatchInfo, 4> VMIs; 6853 while (CalleeFnDecl) { 6854 for (OMPDeclareVariantAttr *A : 6855 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 6856 Expr *VariantRef = A->getVariantFuncRef(); 6857 6858 VariantMatchInfo VMI; 6859 OMPTraitInfo &TI = A->getTraitInfo(); 6860 TI.getAsVariantMatchInfo(Context, VMI); 6861 if (!isVariantApplicableInContext(VMI, OMPCtx, 6862 /* DeviceSetOnly */ false)) 6863 continue; 6864 6865 VMIs.push_back(VMI); 6866 Exprs.push_back(VariantRef); 6867 } 6868 6869 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 6870 } 6871 6872 ExprResult NewCall; 6873 do { 6874 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 6875 if (BestIdx < 0) 6876 return Call; 6877 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 6878 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 6879 6880 { 6881 // Try to build a (member) call expression for the current best applicable 6882 // variant expression. We allow this to fail in which case we continue 6883 // with the next best variant expression. The fail case is part of the 6884 // implementation defined behavior in the OpenMP standard when it talks 6885 // about what differences in the function prototypes: "Any differences 6886 // that the specific OpenMP context requires in the prototype of the 6887 // variant from the base function prototype are implementation defined." 6888 // This wording is there to allow the specialized variant to have a 6889 // different type than the base function. This is intended and OK but if 6890 // we cannot create a call the difference is not in the "implementation 6891 // defined range" we allow. 6892 Sema::TentativeAnalysisScope Trap(*this); 6893 6894 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 6895 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 6896 BestExpr = MemberExpr::CreateImplicit( 6897 Context, MemberCall->getImplicitObjectArgument(), 6898 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 6899 MemberCall->getValueKind(), MemberCall->getObjectKind()); 6900 } 6901 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 6902 ExecConfig); 6903 if (NewCall.isUsable()) { 6904 if (CallExpr *NCE = dyn_cast<CallExpr>(NewCall.get())) { 6905 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee(); 6906 QualType NewType = Context.mergeFunctionTypes( 6907 CalleeFnType, NewCalleeFnDecl->getType(), 6908 /* OfBlockPointer */ false, 6909 /* Unqualified */ false, /* AllowCXX */ true); 6910 if (!NewType.isNull()) 6911 break; 6912 // Don't use the call if the function type was not compatible. 6913 NewCall = nullptr; 6914 } 6915 } 6916 } 6917 6918 VMIs.erase(VMIs.begin() + BestIdx); 6919 Exprs.erase(Exprs.begin() + BestIdx); 6920 } while (!VMIs.empty()); 6921 6922 if (!NewCall.isUsable()) 6923 return Call; 6924 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 6925 } 6926 6927 Optional<std::pair<FunctionDecl *, Expr *>> 6928 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 6929 Expr *VariantRef, OMPTraitInfo &TI, 6930 SourceRange SR) { 6931 if (!DG || DG.get().isNull()) 6932 return None; 6933 6934 const int VariantId = 1; 6935 // Must be applied only to single decl. 6936 if (!DG.get().isSingleDecl()) { 6937 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 6938 << VariantId << SR; 6939 return None; 6940 } 6941 Decl *ADecl = DG.get().getSingleDecl(); 6942 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 6943 ADecl = FTD->getTemplatedDecl(); 6944 6945 // Decl must be a function. 6946 auto *FD = dyn_cast<FunctionDecl>(ADecl); 6947 if (!FD) { 6948 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 6949 << VariantId << SR; 6950 return None; 6951 } 6952 6953 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 6954 return FD->hasAttrs() && 6955 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 6956 FD->hasAttr<TargetAttr>()); 6957 }; 6958 // OpenMP is not compatible with CPU-specific attributes. 6959 if (HasMultiVersionAttributes(FD)) { 6960 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 6961 << SR; 6962 return None; 6963 } 6964 6965 // Allow #pragma omp declare variant only if the function is not used. 6966 if (FD->isUsed(false)) 6967 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6968 << FD->getLocation(); 6969 6970 // Check if the function was emitted already. 6971 const FunctionDecl *Definition; 6972 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6973 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6974 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6975 << FD->getLocation(); 6976 6977 // The VariantRef must point to function. 6978 if (!VariantRef) { 6979 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6980 return None; 6981 } 6982 6983 auto ShouldDelayChecks = [](Expr *&E, bool) { 6984 return E && (E->isTypeDependent() || E->isValueDependent() || 6985 E->containsUnexpandedParameterPack() || 6986 E->isInstantiationDependent()); 6987 }; 6988 // Do not check templates, wait until instantiation. 6989 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6990 TI.anyScoreOrCondition(ShouldDelayChecks)) 6991 return std::make_pair(FD, VariantRef); 6992 6993 // Deal with non-constant score and user condition expressions. 6994 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6995 bool IsScore) -> bool { 6996 if (!E || E->isIntegerConstantExpr(Context)) 6997 return false; 6998 6999 if (IsScore) { 7000 // We warn on non-constant scores and pretend they were not present. 7001 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 7002 << E; 7003 E = nullptr; 7004 } else { 7005 // We could replace a non-constant user condition with "false" but we 7006 // will soon need to handle these anyway for the dynamic version of 7007 // OpenMP context selectors. 7008 Diag(E->getExprLoc(), 7009 diag::err_omp_declare_variant_user_condition_not_constant) 7010 << E; 7011 } 7012 return true; 7013 }; 7014 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 7015 return None; 7016 7017 // Convert VariantRef expression to the type of the original function to 7018 // resolve possible conflicts. 7019 ExprResult VariantRefCast = VariantRef; 7020 if (LangOpts.CPlusPlus) { 7021 QualType FnPtrType; 7022 auto *Method = dyn_cast<CXXMethodDecl>(FD); 7023 if (Method && !Method->isStatic()) { 7024 const Type *ClassType = 7025 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 7026 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 7027 ExprResult ER; 7028 { 7029 // Build adrr_of unary op to correctly handle type checks for member 7030 // functions. 7031 Sema::TentativeAnalysisScope Trap(*this); 7032 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 7033 VariantRef); 7034 } 7035 if (!ER.isUsable()) { 7036 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7037 << VariantId << VariantRef->getSourceRange(); 7038 return None; 7039 } 7040 VariantRef = ER.get(); 7041 } else { 7042 FnPtrType = Context.getPointerType(FD->getType()); 7043 } 7044 QualType VarianPtrType = Context.getPointerType(VariantRef->getType()); 7045 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) { 7046 ImplicitConversionSequence ICS = TryImplicitConversion( 7047 VariantRef, FnPtrType.getUnqualifiedType(), 7048 /*SuppressUserConversions=*/false, AllowedExplicit::None, 7049 /*InOverloadResolution=*/false, 7050 /*CStyle=*/false, 7051 /*AllowObjCWritebackConversion=*/false); 7052 if (ICS.isFailure()) { 7053 Diag(VariantRef->getExprLoc(), 7054 diag::err_omp_declare_variant_incompat_types) 7055 << VariantRef->getType() 7056 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 7057 << VariantRef->getSourceRange(); 7058 return None; 7059 } 7060 VariantRefCast = PerformImplicitConversion( 7061 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 7062 if (!VariantRefCast.isUsable()) 7063 return None; 7064 } 7065 // Drop previously built artificial addr_of unary op for member functions. 7066 if (Method && !Method->isStatic()) { 7067 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 7068 if (auto *UO = dyn_cast<UnaryOperator>( 7069 PossibleAddrOfVariantRef->IgnoreImplicit())) 7070 VariantRefCast = UO->getSubExpr(); 7071 } 7072 } 7073 7074 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 7075 if (!ER.isUsable() || 7076 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 7077 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7078 << VariantId << VariantRef->getSourceRange(); 7079 return None; 7080 } 7081 7082 // The VariantRef must point to function. 7083 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 7084 if (!DRE) { 7085 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7086 << VariantId << VariantRef->getSourceRange(); 7087 return None; 7088 } 7089 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 7090 if (!NewFD) { 7091 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 7092 << VariantId << VariantRef->getSourceRange(); 7093 return None; 7094 } 7095 7096 // Check if function types are compatible in C. 7097 if (!LangOpts.CPlusPlus) { 7098 QualType NewType = 7099 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 7100 if (NewType.isNull()) { 7101 Diag(VariantRef->getExprLoc(), 7102 diag::err_omp_declare_variant_incompat_types) 7103 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 7104 return None; 7105 } 7106 if (NewType->isFunctionProtoType()) { 7107 if (FD->getType()->isFunctionNoProtoType()) 7108 setPrototype(*this, FD, NewFD, NewType); 7109 else if (NewFD->getType()->isFunctionNoProtoType()) 7110 setPrototype(*this, NewFD, FD, NewType); 7111 } 7112 } 7113 7114 // Check if variant function is not marked with declare variant directive. 7115 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 7116 Diag(VariantRef->getExprLoc(), 7117 diag::warn_omp_declare_variant_marked_as_declare_variant) 7118 << VariantRef->getSourceRange(); 7119 SourceRange SR = 7120 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 7121 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 7122 return None; 7123 } 7124 7125 enum DoesntSupport { 7126 VirtFuncs = 1, 7127 Constructors = 3, 7128 Destructors = 4, 7129 DeletedFuncs = 5, 7130 DefaultedFuncs = 6, 7131 ConstexprFuncs = 7, 7132 ConstevalFuncs = 8, 7133 }; 7134 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 7135 if (CXXFD->isVirtual()) { 7136 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7137 << VirtFuncs; 7138 return None; 7139 } 7140 7141 if (isa<CXXConstructorDecl>(FD)) { 7142 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7143 << Constructors; 7144 return None; 7145 } 7146 7147 if (isa<CXXDestructorDecl>(FD)) { 7148 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7149 << Destructors; 7150 return None; 7151 } 7152 } 7153 7154 if (FD->isDeleted()) { 7155 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7156 << DeletedFuncs; 7157 return None; 7158 } 7159 7160 if (FD->isDefaulted()) { 7161 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7162 << DefaultedFuncs; 7163 return None; 7164 } 7165 7166 if (FD->isConstexpr()) { 7167 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 7168 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 7169 return None; 7170 } 7171 7172 // Check general compatibility. 7173 if (areMultiversionVariantFunctionsCompatible( 7174 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 7175 PartialDiagnosticAt(SourceLocation(), 7176 PartialDiagnostic::NullDiagnostic()), 7177 PartialDiagnosticAt( 7178 VariantRef->getExprLoc(), 7179 PDiag(diag::err_omp_declare_variant_doesnt_support)), 7180 PartialDiagnosticAt(VariantRef->getExprLoc(), 7181 PDiag(diag::err_omp_declare_variant_diff) 7182 << FD->getLocation()), 7183 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 7184 /*CLinkageMayDiffer=*/true)) 7185 return None; 7186 return std::make_pair(FD, cast<Expr>(DRE)); 7187 } 7188 7189 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 7190 Expr *VariantRef, 7191 OMPTraitInfo &TI, 7192 SourceRange SR) { 7193 auto *NewAttr = 7194 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR); 7195 FD->addAttr(NewAttr); 7196 } 7197 7198 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 7199 Stmt *AStmt, 7200 SourceLocation StartLoc, 7201 SourceLocation EndLoc) { 7202 if (!AStmt) 7203 return StmtError(); 7204 7205 auto *CS = cast<CapturedStmt>(AStmt); 7206 // 1.2.2 OpenMP Language Terminology 7207 // Structured block - An executable statement with a single entry at the 7208 // top and a single exit at the bottom. 7209 // The point of exit cannot be a branch out of the structured block. 7210 // longjmp() and throw() must not violate the entry/exit criteria. 7211 CS->getCapturedDecl()->setNothrow(); 7212 7213 setFunctionHasBranchProtectedScope(); 7214 7215 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7216 DSAStack->getTaskgroupReductionRef(), 7217 DSAStack->isCancelRegion()); 7218 } 7219 7220 namespace { 7221 /// Iteration space of a single for loop. 7222 struct LoopIterationSpace final { 7223 /// True if the condition operator is the strict compare operator (<, > or 7224 /// !=). 7225 bool IsStrictCompare = false; 7226 /// Condition of the loop. 7227 Expr *PreCond = nullptr; 7228 /// This expression calculates the number of iterations in the loop. 7229 /// It is always possible to calculate it before starting the loop. 7230 Expr *NumIterations = nullptr; 7231 /// The loop counter variable. 7232 Expr *CounterVar = nullptr; 7233 /// Private loop counter variable. 7234 Expr *PrivateCounterVar = nullptr; 7235 /// This is initializer for the initial value of #CounterVar. 7236 Expr *CounterInit = nullptr; 7237 /// This is step for the #CounterVar used to generate its update: 7238 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 7239 Expr *CounterStep = nullptr; 7240 /// Should step be subtracted? 7241 bool Subtract = false; 7242 /// Source range of the loop init. 7243 SourceRange InitSrcRange; 7244 /// Source range of the loop condition. 7245 SourceRange CondSrcRange; 7246 /// Source range of the loop increment. 7247 SourceRange IncSrcRange; 7248 /// Minimum value that can have the loop control variable. Used to support 7249 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 7250 /// since only such variables can be used in non-loop invariant expressions. 7251 Expr *MinValue = nullptr; 7252 /// Maximum value that can have the loop control variable. Used to support 7253 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 7254 /// since only such variables can be used in non-loop invariant expressions. 7255 Expr *MaxValue = nullptr; 7256 /// true, if the lower bound depends on the outer loop control var. 7257 bool IsNonRectangularLB = false; 7258 /// true, if the upper bound depends on the outer loop control var. 7259 bool IsNonRectangularUB = false; 7260 /// Index of the loop this loop depends on and forms non-rectangular loop 7261 /// nest. 7262 unsigned LoopDependentIdx = 0; 7263 /// Final condition for the non-rectangular loop nest support. It is used to 7264 /// check that the number of iterations for this particular counter must be 7265 /// finished. 7266 Expr *FinalCondition = nullptr; 7267 }; 7268 7269 /// Helper class for checking canonical form of the OpenMP loops and 7270 /// extracting iteration space of each loop in the loop nest, that will be used 7271 /// for IR generation. 7272 class OpenMPIterationSpaceChecker { 7273 /// Reference to Sema. 7274 Sema &SemaRef; 7275 /// Does the loop associated directive support non-rectangular loops? 7276 bool SupportsNonRectangular; 7277 /// Data-sharing stack. 7278 DSAStackTy &Stack; 7279 /// A location for diagnostics (when there is no some better location). 7280 SourceLocation DefaultLoc; 7281 /// A location for diagnostics (when increment is not compatible). 7282 SourceLocation ConditionLoc; 7283 /// A source location for referring to loop init later. 7284 SourceRange InitSrcRange; 7285 /// A source location for referring to condition later. 7286 SourceRange ConditionSrcRange; 7287 /// A source location for referring to increment later. 7288 SourceRange IncrementSrcRange; 7289 /// Loop variable. 7290 ValueDecl *LCDecl = nullptr; 7291 /// Reference to loop variable. 7292 Expr *LCRef = nullptr; 7293 /// Lower bound (initializer for the var). 7294 Expr *LB = nullptr; 7295 /// Upper bound. 7296 Expr *UB = nullptr; 7297 /// Loop step (increment). 7298 Expr *Step = nullptr; 7299 /// This flag is true when condition is one of: 7300 /// Var < UB 7301 /// Var <= UB 7302 /// UB > Var 7303 /// UB >= Var 7304 /// This will have no value when the condition is != 7305 llvm::Optional<bool> TestIsLessOp; 7306 /// This flag is true when condition is strict ( < or > ). 7307 bool TestIsStrictOp = false; 7308 /// This flag is true when step is subtracted on each iteration. 7309 bool SubtractStep = false; 7310 /// The outer loop counter this loop depends on (if any). 7311 const ValueDecl *DepDecl = nullptr; 7312 /// Contains number of loop (starts from 1) on which loop counter init 7313 /// expression of this loop depends on. 7314 Optional<unsigned> InitDependOnLC; 7315 /// Contains number of loop (starts from 1) on which loop counter condition 7316 /// expression of this loop depends on. 7317 Optional<unsigned> CondDependOnLC; 7318 /// Checks if the provide statement depends on the loop counter. 7319 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 7320 /// Original condition required for checking of the exit condition for 7321 /// non-rectangular loop. 7322 Expr *Condition = nullptr; 7323 7324 public: 7325 OpenMPIterationSpaceChecker(Sema &SemaRef, bool SupportsNonRectangular, 7326 DSAStackTy &Stack, SourceLocation DefaultLoc) 7327 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular), 7328 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 7329 /// Check init-expr for canonical loop form and save loop counter 7330 /// variable - #Var and its initialization value - #LB. 7331 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 7332 /// Check test-expr for canonical form, save upper-bound (#UB), flags 7333 /// for less/greater and for strict/non-strict comparison. 7334 bool checkAndSetCond(Expr *S); 7335 /// Check incr-expr for canonical loop form and return true if it 7336 /// does not conform, otherwise save loop step (#Step). 7337 bool checkAndSetInc(Expr *S); 7338 /// Return the loop counter variable. 7339 ValueDecl *getLoopDecl() const { return LCDecl; } 7340 /// Return the reference expression to loop counter variable. 7341 Expr *getLoopDeclRefExpr() const { return LCRef; } 7342 /// Source range of the loop init. 7343 SourceRange getInitSrcRange() const { return InitSrcRange; } 7344 /// Source range of the loop condition. 7345 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 7346 /// Source range of the loop increment. 7347 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 7348 /// True if the step should be subtracted. 7349 bool shouldSubtractStep() const { return SubtractStep; } 7350 /// True, if the compare operator is strict (<, > or !=). 7351 bool isStrictTestOp() const { return TestIsStrictOp; } 7352 /// Build the expression to calculate the number of iterations. 7353 Expr *buildNumIterations( 7354 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7355 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7356 /// Build the precondition expression for the loops. 7357 Expr * 7358 buildPreCond(Scope *S, Expr *Cond, 7359 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7360 /// Build reference expression to the counter be used for codegen. 7361 DeclRefExpr * 7362 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7363 DSAStackTy &DSA) const; 7364 /// Build reference expression to the private counter be used for 7365 /// codegen. 7366 Expr *buildPrivateCounterVar() const; 7367 /// Build initialization of the counter be used for codegen. 7368 Expr *buildCounterInit() const; 7369 /// Build step of the counter be used for codegen. 7370 Expr *buildCounterStep() const; 7371 /// Build loop data with counter value for depend clauses in ordered 7372 /// directives. 7373 Expr * 7374 buildOrderedLoopData(Scope *S, Expr *Counter, 7375 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7376 SourceLocation Loc, Expr *Inc = nullptr, 7377 OverloadedOperatorKind OOK = OO_Amp); 7378 /// Builds the minimum value for the loop counter. 7379 std::pair<Expr *, Expr *> buildMinMaxValues( 7380 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 7381 /// Builds final condition for the non-rectangular loops. 7382 Expr *buildFinalCondition(Scope *S) const; 7383 /// Return true if any expression is dependent. 7384 bool dependent() const; 7385 /// Returns true if the initializer forms non-rectangular loop. 7386 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 7387 /// Returns true if the condition forms non-rectangular loop. 7388 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 7389 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 7390 unsigned getLoopDependentIdx() const { 7391 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 7392 } 7393 7394 private: 7395 /// Check the right-hand side of an assignment in the increment 7396 /// expression. 7397 bool checkAndSetIncRHS(Expr *RHS); 7398 /// Helper to set loop counter variable and its initializer. 7399 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 7400 bool EmitDiags); 7401 /// Helper to set upper bound. 7402 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 7403 SourceRange SR, SourceLocation SL); 7404 /// Helper to set loop increment. 7405 bool setStep(Expr *NewStep, bool Subtract); 7406 }; 7407 7408 bool OpenMPIterationSpaceChecker::dependent() const { 7409 if (!LCDecl) { 7410 assert(!LB && !UB && !Step); 7411 return false; 7412 } 7413 return LCDecl->getType()->isDependentType() || 7414 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 7415 (Step && Step->isValueDependent()); 7416 } 7417 7418 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 7419 Expr *NewLCRefExpr, 7420 Expr *NewLB, bool EmitDiags) { 7421 // State consistency checking to ensure correct usage. 7422 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 7423 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7424 if (!NewLCDecl || !NewLB || NewLB->containsErrors()) 7425 return true; 7426 LCDecl = getCanonicalDecl(NewLCDecl); 7427 LCRef = NewLCRefExpr; 7428 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 7429 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7430 if ((Ctor->isCopyOrMoveConstructor() || 7431 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7432 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7433 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 7434 LB = NewLB; 7435 if (EmitDiags) 7436 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 7437 return false; 7438 } 7439 7440 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 7441 llvm::Optional<bool> LessOp, 7442 bool StrictOp, SourceRange SR, 7443 SourceLocation SL) { 7444 // State consistency checking to ensure correct usage. 7445 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 7446 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 7447 if (!NewUB || NewUB->containsErrors()) 7448 return true; 7449 UB = NewUB; 7450 if (LessOp) 7451 TestIsLessOp = LessOp; 7452 TestIsStrictOp = StrictOp; 7453 ConditionSrcRange = SR; 7454 ConditionLoc = SL; 7455 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 7456 return false; 7457 } 7458 7459 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 7460 // State consistency checking to ensure correct usage. 7461 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 7462 if (!NewStep || NewStep->containsErrors()) 7463 return true; 7464 if (!NewStep->isValueDependent()) { 7465 // Check that the step is integer expression. 7466 SourceLocation StepLoc = NewStep->getBeginLoc(); 7467 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 7468 StepLoc, getExprAsWritten(NewStep)); 7469 if (Val.isInvalid()) 7470 return true; 7471 NewStep = Val.get(); 7472 7473 // OpenMP [2.6, Canonical Loop Form, Restrictions] 7474 // If test-expr is of form var relational-op b and relational-op is < or 7475 // <= then incr-expr must cause var to increase on each iteration of the 7476 // loop. If test-expr is of form var relational-op b and relational-op is 7477 // > or >= then incr-expr must cause var to decrease on each iteration of 7478 // the loop. 7479 // If test-expr is of form b relational-op var and relational-op is < or 7480 // <= then incr-expr must cause var to decrease on each iteration of the 7481 // loop. If test-expr is of form b relational-op var and relational-op is 7482 // > or >= then incr-expr must cause var to increase on each iteration of 7483 // the loop. 7484 Optional<llvm::APSInt> Result = 7485 NewStep->getIntegerConstantExpr(SemaRef.Context); 7486 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 7487 bool IsConstNeg = 7488 Result && Result->isSigned() && (Subtract != Result->isNegative()); 7489 bool IsConstPos = 7490 Result && Result->isSigned() && (Subtract == Result->isNegative()); 7491 bool IsConstZero = Result && !Result->getBoolValue(); 7492 7493 // != with increment is treated as <; != with decrement is treated as > 7494 if (!TestIsLessOp.hasValue()) 7495 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 7496 if (UB && (IsConstZero || 7497 (TestIsLessOp.getValue() ? 7498 (IsConstNeg || (IsUnsigned && Subtract)) : 7499 (IsConstPos || (IsUnsigned && !Subtract))))) { 7500 SemaRef.Diag(NewStep->getExprLoc(), 7501 diag::err_omp_loop_incr_not_compatible) 7502 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 7503 SemaRef.Diag(ConditionLoc, 7504 diag::note_omp_loop_cond_requres_compatible_incr) 7505 << TestIsLessOp.getValue() << ConditionSrcRange; 7506 return true; 7507 } 7508 if (TestIsLessOp.getValue() == Subtract) { 7509 NewStep = 7510 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 7511 .get(); 7512 Subtract = !Subtract; 7513 } 7514 } 7515 7516 Step = NewStep; 7517 SubtractStep = Subtract; 7518 return false; 7519 } 7520 7521 namespace { 7522 /// Checker for the non-rectangular loops. Checks if the initializer or 7523 /// condition expression references loop counter variable. 7524 class LoopCounterRefChecker final 7525 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 7526 Sema &SemaRef; 7527 DSAStackTy &Stack; 7528 const ValueDecl *CurLCDecl = nullptr; 7529 const ValueDecl *DepDecl = nullptr; 7530 const ValueDecl *PrevDepDecl = nullptr; 7531 bool IsInitializer = true; 7532 bool SupportsNonRectangular; 7533 unsigned BaseLoopId = 0; 7534 bool checkDecl(const Expr *E, const ValueDecl *VD) { 7535 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 7536 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 7537 << (IsInitializer ? 0 : 1); 7538 return false; 7539 } 7540 const auto &&Data = Stack.isLoopControlVariable(VD); 7541 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 7542 // The type of the loop iterator on which we depend may not have a random 7543 // access iterator type. 7544 if (Data.first && VD->getType()->isRecordType()) { 7545 SmallString<128> Name; 7546 llvm::raw_svector_ostream OS(Name); 7547 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7548 /*Qualified=*/true); 7549 SemaRef.Diag(E->getExprLoc(), 7550 diag::err_omp_wrong_dependency_iterator_type) 7551 << OS.str(); 7552 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 7553 return false; 7554 } 7555 if (Data.first && !SupportsNonRectangular) { 7556 SemaRef.Diag(E->getExprLoc(), diag::err_omp_invariant_dependency); 7557 return false; 7558 } 7559 if (Data.first && 7560 (DepDecl || (PrevDepDecl && 7561 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 7562 if (!DepDecl && PrevDepDecl) 7563 DepDecl = PrevDepDecl; 7564 SmallString<128> Name; 7565 llvm::raw_svector_ostream OS(Name); 7566 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 7567 /*Qualified=*/true); 7568 SemaRef.Diag(E->getExprLoc(), 7569 diag::err_omp_invariant_or_linear_dependency) 7570 << OS.str(); 7571 return false; 7572 } 7573 if (Data.first) { 7574 DepDecl = VD; 7575 BaseLoopId = Data.first; 7576 } 7577 return Data.first; 7578 } 7579 7580 public: 7581 bool VisitDeclRefExpr(const DeclRefExpr *E) { 7582 const ValueDecl *VD = E->getDecl(); 7583 if (isa<VarDecl>(VD)) 7584 return checkDecl(E, VD); 7585 return false; 7586 } 7587 bool VisitMemberExpr(const MemberExpr *E) { 7588 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 7589 const ValueDecl *VD = E->getMemberDecl(); 7590 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 7591 return checkDecl(E, VD); 7592 } 7593 return false; 7594 } 7595 bool VisitStmt(const Stmt *S) { 7596 bool Res = false; 7597 for (const Stmt *Child : S->children()) 7598 Res = (Child && Visit(Child)) || Res; 7599 return Res; 7600 } 7601 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 7602 const ValueDecl *CurLCDecl, bool IsInitializer, 7603 const ValueDecl *PrevDepDecl = nullptr, 7604 bool SupportsNonRectangular = true) 7605 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 7606 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer), 7607 SupportsNonRectangular(SupportsNonRectangular) {} 7608 unsigned getBaseLoopId() const { 7609 assert(CurLCDecl && "Expected loop dependency."); 7610 return BaseLoopId; 7611 } 7612 const ValueDecl *getDepDecl() const { 7613 assert(CurLCDecl && "Expected loop dependency."); 7614 return DepDecl; 7615 } 7616 }; 7617 } // namespace 7618 7619 Optional<unsigned> 7620 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 7621 bool IsInitializer) { 7622 // Check for the non-rectangular loops. 7623 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 7624 DepDecl, SupportsNonRectangular); 7625 if (LoopStmtChecker.Visit(S)) { 7626 DepDecl = LoopStmtChecker.getDepDecl(); 7627 return LoopStmtChecker.getBaseLoopId(); 7628 } 7629 return llvm::None; 7630 } 7631 7632 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 7633 // Check init-expr for canonical loop form and save loop counter 7634 // variable - #Var and its initialization value - #LB. 7635 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 7636 // var = lb 7637 // integer-type var = lb 7638 // random-access-iterator-type var = lb 7639 // pointer-type var = lb 7640 // 7641 if (!S) { 7642 if (EmitDiags) { 7643 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 7644 } 7645 return true; 7646 } 7647 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7648 if (!ExprTemp->cleanupsHaveSideEffects()) 7649 S = ExprTemp->getSubExpr(); 7650 7651 InitSrcRange = S->getSourceRange(); 7652 if (Expr *E = dyn_cast<Expr>(S)) 7653 S = E->IgnoreParens(); 7654 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7655 if (BO->getOpcode() == BO_Assign) { 7656 Expr *LHS = BO->getLHS()->IgnoreParens(); 7657 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7658 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7659 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7660 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7661 EmitDiags); 7662 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 7663 } 7664 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7665 if (ME->isArrow() && 7666 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7667 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7668 EmitDiags); 7669 } 7670 } 7671 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 7672 if (DS->isSingleDecl()) { 7673 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 7674 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 7675 // Accept non-canonical init form here but emit ext. warning. 7676 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 7677 SemaRef.Diag(S->getBeginLoc(), 7678 diag::ext_omp_loop_not_canonical_init) 7679 << S->getSourceRange(); 7680 return setLCDeclAndLB( 7681 Var, 7682 buildDeclRefExpr(SemaRef, Var, 7683 Var->getType().getNonReferenceType(), 7684 DS->getBeginLoc()), 7685 Var->getInit(), EmitDiags); 7686 } 7687 } 7688 } 7689 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7690 if (CE->getOperator() == OO_Equal) { 7691 Expr *LHS = CE->getArg(0); 7692 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 7693 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 7694 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 7695 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7696 EmitDiags); 7697 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 7698 } 7699 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 7700 if (ME->isArrow() && 7701 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7702 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 7703 EmitDiags); 7704 } 7705 } 7706 } 7707 7708 if (dependent() || SemaRef.CurContext->isDependentContext()) 7709 return false; 7710 if (EmitDiags) { 7711 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 7712 << S->getSourceRange(); 7713 } 7714 return true; 7715 } 7716 7717 /// Ignore parenthesizes, implicit casts, copy constructor and return the 7718 /// variable (which may be the loop variable) if possible. 7719 static const ValueDecl *getInitLCDecl(const Expr *E) { 7720 if (!E) 7721 return nullptr; 7722 E = getExprAsWritten(E); 7723 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 7724 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 7725 if ((Ctor->isCopyOrMoveConstructor() || 7726 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 7727 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 7728 E = CE->getArg(0)->IgnoreParenImpCasts(); 7729 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 7730 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 7731 return getCanonicalDecl(VD); 7732 } 7733 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 7734 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 7735 return getCanonicalDecl(ME->getMemberDecl()); 7736 return nullptr; 7737 } 7738 7739 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 7740 // Check test-expr for canonical form, save upper-bound UB, flags for 7741 // less/greater and for strict/non-strict comparison. 7742 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 7743 // var relational-op b 7744 // b relational-op var 7745 // 7746 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 7747 if (!S) { 7748 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 7749 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 7750 return true; 7751 } 7752 Condition = S; 7753 S = getExprAsWritten(S); 7754 SourceLocation CondLoc = S->getBeginLoc(); 7755 auto &&CheckAndSetCond = [this, IneqCondIsCanonical]( 7756 BinaryOperatorKind Opcode, const Expr *LHS, 7757 const Expr *RHS, SourceRange SR, 7758 SourceLocation OpLoc) -> llvm::Optional<bool> { 7759 if (BinaryOperator::isRelationalOp(Opcode)) { 7760 if (getInitLCDecl(LHS) == LCDecl) 7761 return setUB(const_cast<Expr *>(RHS), 7762 (Opcode == BO_LT || Opcode == BO_LE), 7763 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7764 if (getInitLCDecl(RHS) == LCDecl) 7765 return setUB(const_cast<Expr *>(LHS), 7766 (Opcode == BO_GT || Opcode == BO_GE), 7767 (Opcode == BO_LT || Opcode == BO_GT), SR, OpLoc); 7768 } else if (IneqCondIsCanonical && Opcode == BO_NE) { 7769 return setUB(const_cast<Expr *>(getInitLCDecl(LHS) == LCDecl ? RHS : LHS), 7770 /*LessOp=*/llvm::None, 7771 /*StrictOp=*/true, SR, OpLoc); 7772 } 7773 return llvm::None; 7774 }; 7775 llvm::Optional<bool> Res; 7776 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(S)) { 7777 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm(); 7778 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(), 7779 RBO->getOperatorLoc()); 7780 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7781 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(), 7782 BO->getSourceRange(), BO->getOperatorLoc()); 7783 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7784 if (CE->getNumArgs() == 2) { 7785 Res = CheckAndSetCond( 7786 BinaryOperator::getOverloadedOpcode(CE->getOperator()), CE->getArg(0), 7787 CE->getArg(1), CE->getSourceRange(), CE->getOperatorLoc()); 7788 } 7789 } 7790 if (Res.hasValue()) 7791 return *Res; 7792 if (dependent() || SemaRef.CurContext->isDependentContext()) 7793 return false; 7794 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 7795 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 7796 return true; 7797 } 7798 7799 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 7800 // RHS of canonical loop form increment can be: 7801 // var + incr 7802 // incr + var 7803 // var - incr 7804 // 7805 RHS = RHS->IgnoreParenImpCasts(); 7806 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 7807 if (BO->isAdditiveOp()) { 7808 bool IsAdd = BO->getOpcode() == BO_Add; 7809 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7810 return setStep(BO->getRHS(), !IsAdd); 7811 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 7812 return setStep(BO->getLHS(), /*Subtract=*/false); 7813 } 7814 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 7815 bool IsAdd = CE->getOperator() == OO_Plus; 7816 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 7817 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7818 return setStep(CE->getArg(1), !IsAdd); 7819 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 7820 return setStep(CE->getArg(0), /*Subtract=*/false); 7821 } 7822 } 7823 if (dependent() || SemaRef.CurContext->isDependentContext()) 7824 return false; 7825 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7826 << RHS->getSourceRange() << LCDecl; 7827 return true; 7828 } 7829 7830 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 7831 // Check incr-expr for canonical loop form and return true if it 7832 // does not conform. 7833 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 7834 // ++var 7835 // var++ 7836 // --var 7837 // var-- 7838 // var += incr 7839 // var -= incr 7840 // var = var + incr 7841 // var = incr + var 7842 // var = var - incr 7843 // 7844 if (!S) { 7845 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 7846 return true; 7847 } 7848 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 7849 if (!ExprTemp->cleanupsHaveSideEffects()) 7850 S = ExprTemp->getSubExpr(); 7851 7852 IncrementSrcRange = S->getSourceRange(); 7853 S = S->IgnoreParens(); 7854 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 7855 if (UO->isIncrementDecrementOp() && 7856 getInitLCDecl(UO->getSubExpr()) == LCDecl) 7857 return setStep(SemaRef 7858 .ActOnIntegerConstant(UO->getBeginLoc(), 7859 (UO->isDecrementOp() ? -1 : 1)) 7860 .get(), 7861 /*Subtract=*/false); 7862 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 7863 switch (BO->getOpcode()) { 7864 case BO_AddAssign: 7865 case BO_SubAssign: 7866 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7867 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 7868 break; 7869 case BO_Assign: 7870 if (getInitLCDecl(BO->getLHS()) == LCDecl) 7871 return checkAndSetIncRHS(BO->getRHS()); 7872 break; 7873 default: 7874 break; 7875 } 7876 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 7877 switch (CE->getOperator()) { 7878 case OO_PlusPlus: 7879 case OO_MinusMinus: 7880 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7881 return setStep(SemaRef 7882 .ActOnIntegerConstant( 7883 CE->getBeginLoc(), 7884 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 7885 .get(), 7886 /*Subtract=*/false); 7887 break; 7888 case OO_PlusEqual: 7889 case OO_MinusEqual: 7890 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7891 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 7892 break; 7893 case OO_Equal: 7894 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 7895 return checkAndSetIncRHS(CE->getArg(1)); 7896 break; 7897 default: 7898 break; 7899 } 7900 } 7901 if (dependent() || SemaRef.CurContext->isDependentContext()) 7902 return false; 7903 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 7904 << S->getSourceRange() << LCDecl; 7905 return true; 7906 } 7907 7908 static ExprResult 7909 tryBuildCapture(Sema &SemaRef, Expr *Capture, 7910 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7911 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 7912 return Capture; 7913 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 7914 return SemaRef.PerformImplicitConversion( 7915 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 7916 /*AllowExplicit=*/true); 7917 auto I = Captures.find(Capture); 7918 if (I != Captures.end()) 7919 return buildCapture(SemaRef, Capture, I->second); 7920 DeclRefExpr *Ref = nullptr; 7921 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 7922 Captures[Capture] = Ref; 7923 return Res; 7924 } 7925 7926 /// Calculate number of iterations, transforming to unsigned, if number of 7927 /// iterations may be larger than the original type. 7928 static Expr * 7929 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 7930 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 7931 bool TestIsStrictOp, bool RoundToStep, 7932 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7933 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7934 if (!NewStep.isUsable()) 7935 return nullptr; 7936 llvm::APSInt LRes, SRes; 7937 bool IsLowerConst = false, IsStepConst = false; 7938 if (Optional<llvm::APSInt> Res = Lower->getIntegerConstantExpr(SemaRef.Context)) { 7939 LRes = *Res; 7940 IsLowerConst = true; 7941 } 7942 if (Optional<llvm::APSInt> Res = Step->getIntegerConstantExpr(SemaRef.Context)) { 7943 SRes = *Res; 7944 IsStepConst = true; 7945 } 7946 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 7947 ((!TestIsStrictOp && LRes.isNonNegative()) || 7948 (TestIsStrictOp && LRes.isStrictlyPositive())); 7949 bool NeedToReorganize = false; 7950 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 7951 if (!NoNeedToConvert && IsLowerConst && 7952 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 7953 NoNeedToConvert = true; 7954 if (RoundToStep) { 7955 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 7956 ? LRes.getBitWidth() 7957 : SRes.getBitWidth(); 7958 LRes = LRes.extend(BW + 1); 7959 LRes.setIsSigned(true); 7960 SRes = SRes.extend(BW + 1); 7961 SRes.setIsSigned(true); 7962 LRes -= SRes; 7963 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 7964 LRes = LRes.trunc(BW); 7965 } 7966 if (TestIsStrictOp) { 7967 unsigned BW = LRes.getBitWidth(); 7968 LRes = LRes.extend(BW + 1); 7969 LRes.setIsSigned(true); 7970 ++LRes; 7971 NoNeedToConvert = 7972 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 7973 // truncate to the original bitwidth. 7974 LRes = LRes.trunc(BW); 7975 } 7976 NeedToReorganize = NoNeedToConvert; 7977 } 7978 llvm::APSInt URes; 7979 bool IsUpperConst = false; 7980 if (Optional<llvm::APSInt> Res = Upper->getIntegerConstantExpr(SemaRef.Context)) { 7981 URes = *Res; 7982 IsUpperConst = true; 7983 } 7984 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 7985 (!RoundToStep || IsStepConst)) { 7986 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 7987 : URes.getBitWidth(); 7988 LRes = LRes.extend(BW + 1); 7989 LRes.setIsSigned(true); 7990 URes = URes.extend(BW + 1); 7991 URes.setIsSigned(true); 7992 URes -= LRes; 7993 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 7994 NeedToReorganize = NoNeedToConvert; 7995 } 7996 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 7997 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 7998 // unsigned. 7999 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 8000 !LCTy->isDependentType() && LCTy->isIntegerType()) { 8001 QualType LowerTy = Lower->getType(); 8002 QualType UpperTy = Upper->getType(); 8003 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 8004 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 8005 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 8006 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 8007 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 8008 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 8009 Upper = 8010 SemaRef 8011 .PerformImplicitConversion( 8012 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8013 CastType, Sema::AA_Converting) 8014 .get(); 8015 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 8016 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 8017 } 8018 } 8019 if (!Lower || !Upper || NewStep.isInvalid()) 8020 return nullptr; 8021 8022 ExprResult Diff; 8023 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 8024 // 1]). 8025 if (NeedToReorganize) { 8026 Diff = Lower; 8027 8028 if (RoundToStep) { 8029 // Lower - Step 8030 Diff = 8031 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 8032 if (!Diff.isUsable()) 8033 return nullptr; 8034 } 8035 8036 // Lower - Step [+ 1] 8037 if (TestIsStrictOp) 8038 Diff = SemaRef.BuildBinOp( 8039 S, DefaultLoc, BO_Add, Diff.get(), 8040 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8041 if (!Diff.isUsable()) 8042 return nullptr; 8043 8044 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8045 if (!Diff.isUsable()) 8046 return nullptr; 8047 8048 // Upper - (Lower - Step [+ 1]). 8049 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 8050 if (!Diff.isUsable()) 8051 return nullptr; 8052 } else { 8053 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 8054 8055 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 8056 // BuildBinOp already emitted error, this one is to point user to upper 8057 // and lower bound, and to tell what is passed to 'operator-'. 8058 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 8059 << Upper->getSourceRange() << Lower->getSourceRange(); 8060 return nullptr; 8061 } 8062 8063 if (!Diff.isUsable()) 8064 return nullptr; 8065 8066 // Upper - Lower [- 1] 8067 if (TestIsStrictOp) 8068 Diff = SemaRef.BuildBinOp( 8069 S, DefaultLoc, BO_Sub, Diff.get(), 8070 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8071 if (!Diff.isUsable()) 8072 return nullptr; 8073 8074 if (RoundToStep) { 8075 // Upper - Lower [- 1] + Step 8076 Diff = 8077 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 8078 if (!Diff.isUsable()) 8079 return nullptr; 8080 } 8081 } 8082 8083 // Parentheses (for dumping/debugging purposes only). 8084 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8085 if (!Diff.isUsable()) 8086 return nullptr; 8087 8088 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 8089 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 8090 if (!Diff.isUsable()) 8091 return nullptr; 8092 8093 return Diff.get(); 8094 } 8095 8096 /// Build the expression to calculate the number of iterations. 8097 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 8098 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 8099 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8100 QualType VarType = LCDecl->getType().getNonReferenceType(); 8101 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8102 !SemaRef.getLangOpts().CPlusPlus) 8103 return nullptr; 8104 Expr *LBVal = LB; 8105 Expr *UBVal = UB; 8106 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 8107 // max(LB(MinVal), LB(MaxVal)) 8108 if (InitDependOnLC) { 8109 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1]; 8110 if (!IS.MinValue || !IS.MaxValue) 8111 return nullptr; 8112 // OuterVar = Min 8113 ExprResult MinValue = 8114 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8115 if (!MinValue.isUsable()) 8116 return nullptr; 8117 8118 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8119 IS.CounterVar, MinValue.get()); 8120 if (!LBMinVal.isUsable()) 8121 return nullptr; 8122 // OuterVar = Min, LBVal 8123 LBMinVal = 8124 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 8125 if (!LBMinVal.isUsable()) 8126 return nullptr; 8127 // (OuterVar = Min, LBVal) 8128 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 8129 if (!LBMinVal.isUsable()) 8130 return nullptr; 8131 8132 // OuterVar = Max 8133 ExprResult MaxValue = 8134 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8135 if (!MaxValue.isUsable()) 8136 return nullptr; 8137 8138 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8139 IS.CounterVar, MaxValue.get()); 8140 if (!LBMaxVal.isUsable()) 8141 return nullptr; 8142 // OuterVar = Max, LBVal 8143 LBMaxVal = 8144 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 8145 if (!LBMaxVal.isUsable()) 8146 return nullptr; 8147 // (OuterVar = Max, LBVal) 8148 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 8149 if (!LBMaxVal.isUsable()) 8150 return nullptr; 8151 8152 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 8153 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 8154 if (!LBMin || !LBMax) 8155 return nullptr; 8156 // LB(MinVal) < LB(MaxVal) 8157 ExprResult MinLessMaxRes = 8158 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 8159 if (!MinLessMaxRes.isUsable()) 8160 return nullptr; 8161 Expr *MinLessMax = 8162 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 8163 if (!MinLessMax) 8164 return nullptr; 8165 if (TestIsLessOp.getValue()) { 8166 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 8167 // LB(MaxVal)) 8168 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8169 MinLessMax, LBMin, LBMax); 8170 if (!MinLB.isUsable()) 8171 return nullptr; 8172 LBVal = MinLB.get(); 8173 } else { 8174 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 8175 // LB(MaxVal)) 8176 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 8177 MinLessMax, LBMax, LBMin); 8178 if (!MaxLB.isUsable()) 8179 return nullptr; 8180 LBVal = MaxLB.get(); 8181 } 8182 } 8183 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 8184 // min(UB(MinVal), UB(MaxVal)) 8185 if (CondDependOnLC) { 8186 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1]; 8187 if (!IS.MinValue || !IS.MaxValue) 8188 return nullptr; 8189 // OuterVar = Min 8190 ExprResult MinValue = 8191 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 8192 if (!MinValue.isUsable()) 8193 return nullptr; 8194 8195 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8196 IS.CounterVar, MinValue.get()); 8197 if (!UBMinVal.isUsable()) 8198 return nullptr; 8199 // OuterVar = Min, UBVal 8200 UBMinVal = 8201 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 8202 if (!UBMinVal.isUsable()) 8203 return nullptr; 8204 // (OuterVar = Min, UBVal) 8205 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 8206 if (!UBMinVal.isUsable()) 8207 return nullptr; 8208 8209 // OuterVar = Max 8210 ExprResult MaxValue = 8211 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 8212 if (!MaxValue.isUsable()) 8213 return nullptr; 8214 8215 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 8216 IS.CounterVar, MaxValue.get()); 8217 if (!UBMaxVal.isUsable()) 8218 return nullptr; 8219 // OuterVar = Max, UBVal 8220 UBMaxVal = 8221 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 8222 if (!UBMaxVal.isUsable()) 8223 return nullptr; 8224 // (OuterVar = Max, UBVal) 8225 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 8226 if (!UBMaxVal.isUsable()) 8227 return nullptr; 8228 8229 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 8230 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 8231 if (!UBMin || !UBMax) 8232 return nullptr; 8233 // UB(MinVal) > UB(MaxVal) 8234 ExprResult MinGreaterMaxRes = 8235 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 8236 if (!MinGreaterMaxRes.isUsable()) 8237 return nullptr; 8238 Expr *MinGreaterMax = 8239 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 8240 if (!MinGreaterMax) 8241 return nullptr; 8242 if (TestIsLessOp.getValue()) { 8243 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 8244 // UB(MaxVal)) 8245 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 8246 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 8247 if (!MaxUB.isUsable()) 8248 return nullptr; 8249 UBVal = MaxUB.get(); 8250 } else { 8251 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 8252 // UB(MaxVal)) 8253 ExprResult MinUB = SemaRef.ActOnConditionalOp( 8254 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 8255 if (!MinUB.isUsable()) 8256 return nullptr; 8257 UBVal = MinUB.get(); 8258 } 8259 } 8260 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 8261 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 8262 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8263 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8264 if (!Upper || !Lower) 8265 return nullptr; 8266 8267 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8268 Step, VarType, TestIsStrictOp, 8269 /*RoundToStep=*/true, Captures); 8270 if (!Diff.isUsable()) 8271 return nullptr; 8272 8273 // OpenMP runtime requires 32-bit or 64-bit loop variables. 8274 QualType Type = Diff.get()->getType(); 8275 ASTContext &C = SemaRef.Context; 8276 bool UseVarType = VarType->hasIntegerRepresentation() && 8277 C.getTypeSize(Type) > C.getTypeSize(VarType); 8278 if (!Type->isIntegerType() || UseVarType) { 8279 unsigned NewSize = 8280 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 8281 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 8282 : Type->hasSignedIntegerRepresentation(); 8283 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 8284 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 8285 Diff = SemaRef.PerformImplicitConversion( 8286 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 8287 if (!Diff.isUsable()) 8288 return nullptr; 8289 } 8290 } 8291 if (LimitedType) { 8292 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 8293 if (NewSize != C.getTypeSize(Type)) { 8294 if (NewSize < C.getTypeSize(Type)) { 8295 assert(NewSize == 64 && "incorrect loop var size"); 8296 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 8297 << InitSrcRange << ConditionSrcRange; 8298 } 8299 QualType NewType = C.getIntTypeForBitwidth( 8300 NewSize, Type->hasSignedIntegerRepresentation() || 8301 C.getTypeSize(Type) < NewSize); 8302 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 8303 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 8304 Sema::AA_Converting, true); 8305 if (!Diff.isUsable()) 8306 return nullptr; 8307 } 8308 } 8309 } 8310 8311 return Diff.get(); 8312 } 8313 8314 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 8315 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8316 // Do not build for iterators, they cannot be used in non-rectangular loop 8317 // nests. 8318 if (LCDecl->getType()->isRecordType()) 8319 return std::make_pair(nullptr, nullptr); 8320 // If we subtract, the min is in the condition, otherwise the min is in the 8321 // init value. 8322 Expr *MinExpr = nullptr; 8323 Expr *MaxExpr = nullptr; 8324 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 8325 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 8326 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 8327 : CondDependOnLC.hasValue(); 8328 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 8329 : InitDependOnLC.hasValue(); 8330 Expr *Lower = 8331 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 8332 Expr *Upper = 8333 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 8334 if (!Upper || !Lower) 8335 return std::make_pair(nullptr, nullptr); 8336 8337 if (TestIsLessOp.getValue()) 8338 MinExpr = Lower; 8339 else 8340 MaxExpr = Upper; 8341 8342 // Build minimum/maximum value based on number of iterations. 8343 QualType VarType = LCDecl->getType().getNonReferenceType(); 8344 8345 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 8346 Step, VarType, TestIsStrictOp, 8347 /*RoundToStep=*/false, Captures); 8348 if (!Diff.isUsable()) 8349 return std::make_pair(nullptr, nullptr); 8350 8351 // ((Upper - Lower [- 1]) / Step) * Step 8352 // Parentheses (for dumping/debugging purposes only). 8353 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8354 if (!Diff.isUsable()) 8355 return std::make_pair(nullptr, nullptr); 8356 8357 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 8358 if (!NewStep.isUsable()) 8359 return std::make_pair(nullptr, nullptr); 8360 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 8361 if (!Diff.isUsable()) 8362 return std::make_pair(nullptr, nullptr); 8363 8364 // Parentheses (for dumping/debugging purposes only). 8365 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 8366 if (!Diff.isUsable()) 8367 return std::make_pair(nullptr, nullptr); 8368 8369 // Convert to the ptrdiff_t, if original type is pointer. 8370 if (VarType->isAnyPointerType() && 8371 !SemaRef.Context.hasSameType( 8372 Diff.get()->getType(), 8373 SemaRef.Context.getUnsignedPointerDiffType())) { 8374 Diff = SemaRef.PerformImplicitConversion( 8375 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 8376 Sema::AA_Converting, /*AllowExplicit=*/true); 8377 } 8378 if (!Diff.isUsable()) 8379 return std::make_pair(nullptr, nullptr); 8380 8381 if (TestIsLessOp.getValue()) { 8382 // MinExpr = Lower; 8383 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 8384 Diff = SemaRef.BuildBinOp( 8385 S, DefaultLoc, BO_Add, 8386 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 8387 Diff.get()); 8388 if (!Diff.isUsable()) 8389 return std::make_pair(nullptr, nullptr); 8390 } else { 8391 // MaxExpr = Upper; 8392 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 8393 Diff = SemaRef.BuildBinOp( 8394 S, DefaultLoc, BO_Sub, 8395 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 8396 Diff.get()); 8397 if (!Diff.isUsable()) 8398 return std::make_pair(nullptr, nullptr); 8399 } 8400 8401 // Convert to the original type. 8402 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 8403 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 8404 Sema::AA_Converting, 8405 /*AllowExplicit=*/true); 8406 if (!Diff.isUsable()) 8407 return std::make_pair(nullptr, nullptr); 8408 8409 Sema::TentativeAnalysisScope Trap(SemaRef); 8410 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 8411 if (!Diff.isUsable()) 8412 return std::make_pair(nullptr, nullptr); 8413 8414 if (TestIsLessOp.getValue()) 8415 MaxExpr = Diff.get(); 8416 else 8417 MinExpr = Diff.get(); 8418 8419 return std::make_pair(MinExpr, MaxExpr); 8420 } 8421 8422 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 8423 if (InitDependOnLC || CondDependOnLC) 8424 return Condition; 8425 return nullptr; 8426 } 8427 8428 Expr *OpenMPIterationSpaceChecker::buildPreCond( 8429 Scope *S, Expr *Cond, 8430 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 8431 // Do not build a precondition when the condition/initialization is dependent 8432 // to prevent pessimistic early loop exit. 8433 // TODO: this can be improved by calculating min/max values but not sure that 8434 // it will be very effective. 8435 if (CondDependOnLC || InitDependOnLC) 8436 return SemaRef.PerformImplicitConversion( 8437 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 8438 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8439 /*AllowExplicit=*/true).get(); 8440 8441 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 8442 Sema::TentativeAnalysisScope Trap(SemaRef); 8443 8444 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 8445 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 8446 if (!NewLB.isUsable() || !NewUB.isUsable()) 8447 return nullptr; 8448 8449 ExprResult CondExpr = 8450 SemaRef.BuildBinOp(S, DefaultLoc, 8451 TestIsLessOp.getValue() ? 8452 (TestIsStrictOp ? BO_LT : BO_LE) : 8453 (TestIsStrictOp ? BO_GT : BO_GE), 8454 NewLB.get(), NewUB.get()); 8455 if (CondExpr.isUsable()) { 8456 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 8457 SemaRef.Context.BoolTy)) 8458 CondExpr = SemaRef.PerformImplicitConversion( 8459 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 8460 /*AllowExplicit=*/true); 8461 } 8462 8463 // Otherwise use original loop condition and evaluate it in runtime. 8464 return CondExpr.isUsable() ? CondExpr.get() : Cond; 8465 } 8466 8467 /// Build reference expression to the counter be used for codegen. 8468 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 8469 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 8470 DSAStackTy &DSA) const { 8471 auto *VD = dyn_cast<VarDecl>(LCDecl); 8472 if (!VD) { 8473 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 8474 DeclRefExpr *Ref = buildDeclRefExpr( 8475 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 8476 const DSAStackTy::DSAVarData Data = 8477 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 8478 // If the loop control decl is explicitly marked as private, do not mark it 8479 // as captured again. 8480 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 8481 Captures.insert(std::make_pair(LCRef, Ref)); 8482 return Ref; 8483 } 8484 return cast<DeclRefExpr>(LCRef); 8485 } 8486 8487 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 8488 if (LCDecl && !LCDecl->isInvalidDecl()) { 8489 QualType Type = LCDecl->getType().getNonReferenceType(); 8490 VarDecl *PrivateVar = buildVarDecl( 8491 SemaRef, DefaultLoc, Type, LCDecl->getName(), 8492 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 8493 isa<VarDecl>(LCDecl) 8494 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 8495 : nullptr); 8496 if (PrivateVar->isInvalidDecl()) 8497 return nullptr; 8498 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 8499 } 8500 return nullptr; 8501 } 8502 8503 /// Build initialization of the counter to be used for codegen. 8504 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 8505 8506 /// Build step of the counter be used for codegen. 8507 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 8508 8509 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 8510 Scope *S, Expr *Counter, 8511 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 8512 Expr *Inc, OverloadedOperatorKind OOK) { 8513 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 8514 if (!Cnt) 8515 return nullptr; 8516 if (Inc) { 8517 assert((OOK == OO_Plus || OOK == OO_Minus) && 8518 "Expected only + or - operations for depend clauses."); 8519 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 8520 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 8521 if (!Cnt) 8522 return nullptr; 8523 } 8524 QualType VarType = LCDecl->getType().getNonReferenceType(); 8525 if (!VarType->isIntegerType() && !VarType->isPointerType() && 8526 !SemaRef.getLangOpts().CPlusPlus) 8527 return nullptr; 8528 // Upper - Lower 8529 Expr *Upper = TestIsLessOp.getValue() 8530 ? Cnt 8531 : tryBuildCapture(SemaRef, LB, Captures).get(); 8532 Expr *Lower = TestIsLessOp.getValue() 8533 ? tryBuildCapture(SemaRef, LB, Captures).get() 8534 : Cnt; 8535 if (!Upper || !Lower) 8536 return nullptr; 8537 8538 ExprResult Diff = calculateNumIters( 8539 SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 8540 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures); 8541 if (!Diff.isUsable()) 8542 return nullptr; 8543 8544 return Diff.get(); 8545 } 8546 } // namespace 8547 8548 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 8549 assert(getLangOpts().OpenMP && "OpenMP is not active."); 8550 assert(Init && "Expected loop in canonical form."); 8551 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 8552 if (AssociatedLoops > 0 && 8553 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 8554 DSAStack->loopStart(); 8555 OpenMPIterationSpaceChecker ISC(*this, /*SupportsNonRectangular=*/true, 8556 *DSAStack, ForLoc); 8557 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 8558 if (ValueDecl *D = ISC.getLoopDecl()) { 8559 auto *VD = dyn_cast<VarDecl>(D); 8560 DeclRefExpr *PrivateRef = nullptr; 8561 if (!VD) { 8562 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 8563 VD = Private; 8564 } else { 8565 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 8566 /*WithInit=*/false); 8567 VD = cast<VarDecl>(PrivateRef->getDecl()); 8568 } 8569 } 8570 DSAStack->addLoopControlVariable(D, VD); 8571 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 8572 if (LD != D->getCanonicalDecl()) { 8573 DSAStack->resetPossibleLoopCounter(); 8574 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 8575 MarkDeclarationsReferencedInExpr( 8576 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 8577 Var->getType().getNonLValueExprType(Context), 8578 ForLoc, /*RefersToCapture=*/true)); 8579 } 8580 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8581 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 8582 // Referenced in a Construct, C/C++]. The loop iteration variable in the 8583 // associated for-loop of a simd construct with just one associated 8584 // for-loop may be listed in a linear clause with a constant-linear-step 8585 // that is the increment of the associated for-loop. The loop iteration 8586 // variable(s) in the associated for-loop(s) of a for or parallel for 8587 // construct may be listed in a private or lastprivate clause. 8588 DSAStackTy::DSAVarData DVar = 8589 DSAStack->getTopDSA(D, /*FromParent=*/false); 8590 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 8591 // is declared in the loop and it is predetermined as a private. 8592 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 8593 OpenMPClauseKind PredeterminedCKind = 8594 isOpenMPSimdDirective(DKind) 8595 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 8596 : OMPC_private; 8597 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8598 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 8599 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 8600 DVar.CKind != OMPC_private))) || 8601 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 8602 DKind == OMPD_master_taskloop || 8603 DKind == OMPD_parallel_master_taskloop || 8604 isOpenMPDistributeDirective(DKind)) && 8605 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 8606 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 8607 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 8608 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 8609 << getOpenMPClauseName(DVar.CKind) 8610 << getOpenMPDirectiveName(DKind) 8611 << getOpenMPClauseName(PredeterminedCKind); 8612 if (DVar.RefExpr == nullptr) 8613 DVar.CKind = PredeterminedCKind; 8614 reportOriginalDsa(*this, DSAStack, D, DVar, 8615 /*IsLoopIterVar=*/true); 8616 } else if (LoopDeclRefExpr) { 8617 // Make the loop iteration variable private (for worksharing 8618 // constructs), linear (for simd directives with the only one 8619 // associated loop) or lastprivate (for simd directives with several 8620 // collapsed or ordered loops). 8621 if (DVar.CKind == OMPC_unknown) 8622 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 8623 PrivateRef); 8624 } 8625 } 8626 } 8627 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 8628 } 8629 } 8630 8631 /// Called on a for stmt to check and extract its iteration space 8632 /// for further processing (such as collapsing). 8633 static bool checkOpenMPIterationSpace( 8634 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 8635 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 8636 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 8637 Expr *OrderedLoopCountExpr, 8638 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8639 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 8640 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8641 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind); 8642 // OpenMP [2.9.1, Canonical Loop Form] 8643 // for (init-expr; test-expr; incr-expr) structured-block 8644 // for (range-decl: range-expr) structured-block 8645 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(S)) 8646 S = CanonLoop->getLoopStmt(); 8647 auto *For = dyn_cast_or_null<ForStmt>(S); 8648 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 8649 // Ranged for is supported only in OpenMP 5.0. 8650 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 8651 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 8652 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 8653 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 8654 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 8655 if (TotalNestedLoopCount > 1) { 8656 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 8657 SemaRef.Diag(DSA.getConstructLoc(), 8658 diag::note_omp_collapse_ordered_expr) 8659 << 2 << CollapseLoopCountExpr->getSourceRange() 8660 << OrderedLoopCountExpr->getSourceRange(); 8661 else if (CollapseLoopCountExpr) 8662 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8663 diag::note_omp_collapse_ordered_expr) 8664 << 0 << CollapseLoopCountExpr->getSourceRange(); 8665 else 8666 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8667 diag::note_omp_collapse_ordered_expr) 8668 << 1 << OrderedLoopCountExpr->getSourceRange(); 8669 } 8670 return true; 8671 } 8672 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 8673 "No loop body."); 8674 8675 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA, 8676 For ? For->getForLoc() : CXXFor->getForLoc()); 8677 8678 // Check init. 8679 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 8680 if (ISC.checkAndSetInit(Init)) 8681 return true; 8682 8683 bool HasErrors = false; 8684 8685 // Check loop variable's type. 8686 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 8687 // OpenMP [2.6, Canonical Loop Form] 8688 // Var is one of the following: 8689 // A variable of signed or unsigned integer type. 8690 // For C++, a variable of a random access iterator type. 8691 // For C, a variable of a pointer type. 8692 QualType VarType = LCDecl->getType().getNonReferenceType(); 8693 if (!VarType->isDependentType() && !VarType->isIntegerType() && 8694 !VarType->isPointerType() && 8695 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 8696 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 8697 << SemaRef.getLangOpts().CPlusPlus; 8698 HasErrors = true; 8699 } 8700 8701 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 8702 // a Construct 8703 // The loop iteration variable(s) in the associated for-loop(s) of a for or 8704 // parallel for construct is (are) private. 8705 // The loop iteration variable in the associated for-loop of a simd 8706 // construct with just one associated for-loop is linear with a 8707 // constant-linear-step that is the increment of the associated for-loop. 8708 // Exclude loop var from the list of variables with implicitly defined data 8709 // sharing attributes. 8710 VarsWithImplicitDSA.erase(LCDecl); 8711 8712 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 8713 8714 // Check test-expr. 8715 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 8716 8717 // Check incr-expr. 8718 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 8719 } 8720 8721 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 8722 return HasErrors; 8723 8724 // Build the loop's iteration space representation. 8725 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 8726 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 8727 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 8728 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 8729 (isOpenMPWorksharingDirective(DKind) || 8730 isOpenMPTaskLoopDirective(DKind) || 8731 isOpenMPDistributeDirective(DKind) || 8732 isOpenMPLoopTransformationDirective(DKind)), 8733 Captures); 8734 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 8735 ISC.buildCounterVar(Captures, DSA); 8736 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 8737 ISC.buildPrivateCounterVar(); 8738 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 8739 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 8740 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 8741 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 8742 ISC.getConditionSrcRange(); 8743 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 8744 ISC.getIncrementSrcRange(); 8745 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 8746 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 8747 ISC.isStrictTestOp(); 8748 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 8749 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 8750 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 8751 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 8752 ISC.buildFinalCondition(DSA.getCurScope()); 8753 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 8754 ISC.doesInitDependOnLC(); 8755 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 8756 ISC.doesCondDependOnLC(); 8757 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 8758 ISC.getLoopDependentIdx(); 8759 8760 HasErrors |= 8761 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 8762 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 8763 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 8764 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 8765 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 8766 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 8767 if (!HasErrors && DSA.isOrderedRegion()) { 8768 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 8769 if (CurrentNestedLoopCount < 8770 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 8771 DSA.getOrderedRegionParam().second->setLoopNumIterations( 8772 CurrentNestedLoopCount, 8773 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 8774 DSA.getOrderedRegionParam().second->setLoopCounter( 8775 CurrentNestedLoopCount, 8776 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 8777 } 8778 } 8779 for (auto &Pair : DSA.getDoacrossDependClauses()) { 8780 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 8781 // Erroneous case - clause has some problems. 8782 continue; 8783 } 8784 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 8785 Pair.second.size() <= CurrentNestedLoopCount) { 8786 // Erroneous case - clause has some problems. 8787 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 8788 continue; 8789 } 8790 Expr *CntValue; 8791 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 8792 CntValue = ISC.buildOrderedLoopData( 8793 DSA.getCurScope(), 8794 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8795 Pair.first->getDependencyLoc()); 8796 else 8797 CntValue = ISC.buildOrderedLoopData( 8798 DSA.getCurScope(), 8799 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 8800 Pair.first->getDependencyLoc(), 8801 Pair.second[CurrentNestedLoopCount].first, 8802 Pair.second[CurrentNestedLoopCount].second); 8803 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 8804 } 8805 } 8806 8807 return HasErrors; 8808 } 8809 8810 /// Build 'VarRef = Start. 8811 static ExprResult 8812 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8813 ExprResult Start, bool IsNonRectangularLB, 8814 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8815 // Build 'VarRef = Start. 8816 ExprResult NewStart = IsNonRectangularLB 8817 ? Start.get() 8818 : tryBuildCapture(SemaRef, Start.get(), Captures); 8819 if (!NewStart.isUsable()) 8820 return ExprError(); 8821 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 8822 VarRef.get()->getType())) { 8823 NewStart = SemaRef.PerformImplicitConversion( 8824 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 8825 /*AllowExplicit=*/true); 8826 if (!NewStart.isUsable()) 8827 return ExprError(); 8828 } 8829 8830 ExprResult Init = 8831 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8832 return Init; 8833 } 8834 8835 /// Build 'VarRef = Start + Iter * Step'. 8836 static ExprResult buildCounterUpdate( 8837 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 8838 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 8839 bool IsNonRectangularLB, 8840 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 8841 // Add parentheses (for debugging purposes only). 8842 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 8843 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 8844 !Step.isUsable()) 8845 return ExprError(); 8846 8847 ExprResult NewStep = Step; 8848 if (Captures) 8849 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 8850 if (NewStep.isInvalid()) 8851 return ExprError(); 8852 ExprResult Update = 8853 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 8854 if (!Update.isUsable()) 8855 return ExprError(); 8856 8857 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 8858 // 'VarRef = Start (+|-) Iter * Step'. 8859 if (!Start.isUsable()) 8860 return ExprError(); 8861 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 8862 if (!NewStart.isUsable()) 8863 return ExprError(); 8864 if (Captures && !IsNonRectangularLB) 8865 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 8866 if (NewStart.isInvalid()) 8867 return ExprError(); 8868 8869 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 8870 ExprResult SavedUpdate = Update; 8871 ExprResult UpdateVal; 8872 if (VarRef.get()->getType()->isOverloadableType() || 8873 NewStart.get()->getType()->isOverloadableType() || 8874 Update.get()->getType()->isOverloadableType()) { 8875 Sema::TentativeAnalysisScope Trap(SemaRef); 8876 8877 Update = 8878 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 8879 if (Update.isUsable()) { 8880 UpdateVal = 8881 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 8882 VarRef.get(), SavedUpdate.get()); 8883 if (UpdateVal.isUsable()) { 8884 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 8885 UpdateVal.get()); 8886 } 8887 } 8888 } 8889 8890 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 8891 if (!Update.isUsable() || !UpdateVal.isUsable()) { 8892 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 8893 NewStart.get(), SavedUpdate.get()); 8894 if (!Update.isUsable()) 8895 return ExprError(); 8896 8897 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 8898 VarRef.get()->getType())) { 8899 Update = SemaRef.PerformImplicitConversion( 8900 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 8901 if (!Update.isUsable()) 8902 return ExprError(); 8903 } 8904 8905 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 8906 } 8907 return Update; 8908 } 8909 8910 /// Convert integer expression \a E to make it have at least \a Bits 8911 /// bits. 8912 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 8913 if (E == nullptr) 8914 return ExprError(); 8915 ASTContext &C = SemaRef.Context; 8916 QualType OldType = E->getType(); 8917 unsigned HasBits = C.getTypeSize(OldType); 8918 if (HasBits >= Bits) 8919 return ExprResult(E); 8920 // OK to convert to signed, because new type has more bits than old. 8921 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 8922 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 8923 true); 8924 } 8925 8926 /// Check if the given expression \a E is a constant integer that fits 8927 /// into \a Bits bits. 8928 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 8929 if (E == nullptr) 8930 return false; 8931 if (Optional<llvm::APSInt> Result = 8932 E->getIntegerConstantExpr(SemaRef.Context)) 8933 return Signed ? Result->isSignedIntN(Bits) : Result->isIntN(Bits); 8934 return false; 8935 } 8936 8937 /// Build preinits statement for the given declarations. 8938 static Stmt *buildPreInits(ASTContext &Context, 8939 MutableArrayRef<Decl *> PreInits) { 8940 if (!PreInits.empty()) { 8941 return new (Context) DeclStmt( 8942 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 8943 SourceLocation(), SourceLocation()); 8944 } 8945 return nullptr; 8946 } 8947 8948 /// Build preinits statement for the given declarations. 8949 static Stmt * 8950 buildPreInits(ASTContext &Context, 8951 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 8952 if (!Captures.empty()) { 8953 SmallVector<Decl *, 16> PreInits; 8954 for (const auto &Pair : Captures) 8955 PreInits.push_back(Pair.second->getDecl()); 8956 return buildPreInits(Context, PreInits); 8957 } 8958 return nullptr; 8959 } 8960 8961 /// Build postupdate expression for the given list of postupdates expressions. 8962 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 8963 Expr *PostUpdate = nullptr; 8964 if (!PostUpdates.empty()) { 8965 for (Expr *E : PostUpdates) { 8966 Expr *ConvE = S.BuildCStyleCastExpr( 8967 E->getExprLoc(), 8968 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 8969 E->getExprLoc(), E) 8970 .get(); 8971 PostUpdate = PostUpdate 8972 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 8973 PostUpdate, ConvE) 8974 .get() 8975 : ConvE; 8976 } 8977 } 8978 return PostUpdate; 8979 } 8980 8981 /// Called on a for stmt to check itself and nested loops (if any). 8982 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 8983 /// number of collapsed loops otherwise. 8984 static unsigned 8985 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 8986 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 8987 DSAStackTy &DSA, 8988 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8989 OMPLoopBasedDirective::HelperExprs &Built) { 8990 unsigned NestedLoopCount = 1; 8991 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) && 8992 !isOpenMPLoopTransformationDirective(DKind); 8993 8994 if (CollapseLoopCountExpr) { 8995 // Found 'collapse' clause - calculate collapse number. 8996 Expr::EvalResult Result; 8997 if (!CollapseLoopCountExpr->isValueDependent() && 8998 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 8999 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 9000 } else { 9001 Built.clear(/*Size=*/1); 9002 return 1; 9003 } 9004 } 9005 unsigned OrderedLoopCount = 1; 9006 if (OrderedLoopCountExpr) { 9007 // Found 'ordered' clause - calculate collapse number. 9008 Expr::EvalResult EVResult; 9009 if (!OrderedLoopCountExpr->isValueDependent() && 9010 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 9011 SemaRef.getASTContext())) { 9012 llvm::APSInt Result = EVResult.Val.getInt(); 9013 if (Result.getLimitedValue() < NestedLoopCount) { 9014 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 9015 diag::err_omp_wrong_ordered_loop_count) 9016 << OrderedLoopCountExpr->getSourceRange(); 9017 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 9018 diag::note_collapse_loop_count) 9019 << CollapseLoopCountExpr->getSourceRange(); 9020 } 9021 OrderedLoopCount = Result.getLimitedValue(); 9022 } else { 9023 Built.clear(/*Size=*/1); 9024 return 1; 9025 } 9026 } 9027 // This is helper routine for loop directives (e.g., 'for', 'simd', 9028 // 'for simd', etc.). 9029 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9030 unsigned NumLoops = std::max(OrderedLoopCount, NestedLoopCount); 9031 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops); 9032 if (!OMPLoopBasedDirective::doForAllLoops( 9033 AStmt->IgnoreContainers(!isOpenMPLoopTransformationDirective(DKind)), 9034 SupportsNonPerfectlyNested, NumLoops, 9035 [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount, 9036 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA, 9037 &IterSpaces, &Captures](unsigned Cnt, Stmt *CurStmt) { 9038 if (checkOpenMPIterationSpace( 9039 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 9040 NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr, 9041 VarsWithImplicitDSA, IterSpaces, Captures)) 9042 return true; 9043 if (Cnt > 0 && Cnt >= NestedLoopCount && 9044 IterSpaces[Cnt].CounterVar) { 9045 // Handle initialization of captured loop iterator variables. 9046 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 9047 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 9048 Captures[DRE] = DRE; 9049 } 9050 } 9051 return false; 9052 }, 9053 [&SemaRef, &Captures](OMPLoopBasedDirective *Transform) { 9054 Stmt *DependentPreInits; 9055 if (auto *Dir = dyn_cast<OMPTileDirective>(Transform)) { 9056 DependentPreInits = Dir->getPreInits(); 9057 } else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform)) { 9058 DependentPreInits = Dir->getPreInits(); 9059 } else { 9060 llvm_unreachable("Unexpected loop transformation"); 9061 } 9062 if (!DependentPreInits) 9063 return; 9064 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) { 9065 auto *D = cast<VarDecl>(C); 9066 DeclRefExpr *Ref = buildDeclRefExpr(SemaRef, D, D->getType(), 9067 Transform->getBeginLoc()); 9068 Captures[Ref] = Ref; 9069 } 9070 })) 9071 return 0; 9072 9073 Built.clear(/* size */ NestedLoopCount); 9074 9075 if (SemaRef.CurContext->isDependentContext()) 9076 return NestedLoopCount; 9077 9078 // An example of what is generated for the following code: 9079 // 9080 // #pragma omp simd collapse(2) ordered(2) 9081 // for (i = 0; i < NI; ++i) 9082 // for (k = 0; k < NK; ++k) 9083 // for (j = J0; j < NJ; j+=2) { 9084 // <loop body> 9085 // } 9086 // 9087 // We generate the code below. 9088 // Note: the loop body may be outlined in CodeGen. 9089 // Note: some counters may be C++ classes, operator- is used to find number of 9090 // iterations and operator+= to calculate counter value. 9091 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 9092 // or i64 is currently supported). 9093 // 9094 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 9095 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 9096 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 9097 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 9098 // // similar updates for vars in clauses (e.g. 'linear') 9099 // <loop body (using local i and j)> 9100 // } 9101 // i = NI; // assign final values of counters 9102 // j = NJ; 9103 // 9104 9105 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 9106 // the iteration counts of the collapsed for loops. 9107 // Precondition tests if there is at least one iteration (all conditions are 9108 // true). 9109 auto PreCond = ExprResult(IterSpaces[0].PreCond); 9110 Expr *N0 = IterSpaces[0].NumIterations; 9111 ExprResult LastIteration32 = 9112 widenIterationCount(/*Bits=*/32, 9113 SemaRef 9114 .PerformImplicitConversion( 9115 N0->IgnoreImpCasts(), N0->getType(), 9116 Sema::AA_Converting, /*AllowExplicit=*/true) 9117 .get(), 9118 SemaRef); 9119 ExprResult LastIteration64 = widenIterationCount( 9120 /*Bits=*/64, 9121 SemaRef 9122 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 9123 Sema::AA_Converting, 9124 /*AllowExplicit=*/true) 9125 .get(), 9126 SemaRef); 9127 9128 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 9129 return NestedLoopCount; 9130 9131 ASTContext &C = SemaRef.Context; 9132 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 9133 9134 Scope *CurScope = DSA.getCurScope(); 9135 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 9136 if (PreCond.isUsable()) { 9137 PreCond = 9138 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 9139 PreCond.get(), IterSpaces[Cnt].PreCond); 9140 } 9141 Expr *N = IterSpaces[Cnt].NumIterations; 9142 SourceLocation Loc = N->getExprLoc(); 9143 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 9144 if (LastIteration32.isUsable()) 9145 LastIteration32 = SemaRef.BuildBinOp( 9146 CurScope, Loc, BO_Mul, LastIteration32.get(), 9147 SemaRef 9148 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9149 Sema::AA_Converting, 9150 /*AllowExplicit=*/true) 9151 .get()); 9152 if (LastIteration64.isUsable()) 9153 LastIteration64 = SemaRef.BuildBinOp( 9154 CurScope, Loc, BO_Mul, LastIteration64.get(), 9155 SemaRef 9156 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 9157 Sema::AA_Converting, 9158 /*AllowExplicit=*/true) 9159 .get()); 9160 } 9161 9162 // Choose either the 32-bit or 64-bit version. 9163 ExprResult LastIteration = LastIteration64; 9164 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 9165 (LastIteration32.isUsable() && 9166 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 9167 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 9168 fitsInto( 9169 /*Bits=*/32, 9170 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 9171 LastIteration64.get(), SemaRef)))) 9172 LastIteration = LastIteration32; 9173 QualType VType = LastIteration.get()->getType(); 9174 QualType RealVType = VType; 9175 QualType StrideVType = VType; 9176 if (isOpenMPTaskLoopDirective(DKind)) { 9177 VType = 9178 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 9179 StrideVType = 9180 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 9181 } 9182 9183 if (!LastIteration.isUsable()) 9184 return 0; 9185 9186 // Save the number of iterations. 9187 ExprResult NumIterations = LastIteration; 9188 { 9189 LastIteration = SemaRef.BuildBinOp( 9190 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 9191 LastIteration.get(), 9192 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9193 if (!LastIteration.isUsable()) 9194 return 0; 9195 } 9196 9197 // Calculate the last iteration number beforehand instead of doing this on 9198 // each iteration. Do not do this if the number of iterations may be kfold-ed. 9199 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(SemaRef.Context); 9200 ExprResult CalcLastIteration; 9201 if (!IsConstant) { 9202 ExprResult SaveRef = 9203 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 9204 LastIteration = SaveRef; 9205 9206 // Prepare SaveRef + 1. 9207 NumIterations = SemaRef.BuildBinOp( 9208 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 9209 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 9210 if (!NumIterations.isUsable()) 9211 return 0; 9212 } 9213 9214 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 9215 9216 // Build variables passed into runtime, necessary for worksharing directives. 9217 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 9218 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9219 isOpenMPDistributeDirective(DKind) || 9220 isOpenMPLoopTransformationDirective(DKind)) { 9221 // Lower bound variable, initialized with zero. 9222 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 9223 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 9224 SemaRef.AddInitializerToDecl(LBDecl, 9225 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9226 /*DirectInit*/ false); 9227 9228 // Upper bound variable, initialized with last iteration number. 9229 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 9230 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 9231 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 9232 /*DirectInit*/ false); 9233 9234 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 9235 // This will be used to implement clause 'lastprivate'. 9236 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 9237 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 9238 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 9239 SemaRef.AddInitializerToDecl(ILDecl, 9240 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9241 /*DirectInit*/ false); 9242 9243 // Stride variable returned by runtime (we initialize it to 1 by default). 9244 VarDecl *STDecl = 9245 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 9246 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 9247 SemaRef.AddInitializerToDecl(STDecl, 9248 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 9249 /*DirectInit*/ false); 9250 9251 // Build expression: UB = min(UB, LastIteration) 9252 // It is necessary for CodeGen of directives with static scheduling. 9253 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 9254 UB.get(), LastIteration.get()); 9255 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9256 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 9257 LastIteration.get(), UB.get()); 9258 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 9259 CondOp.get()); 9260 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 9261 9262 // If we have a combined directive that combines 'distribute', 'for' or 9263 // 'simd' we need to be able to access the bounds of the schedule of the 9264 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 9265 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 9266 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9267 // Lower bound variable, initialized with zero. 9268 VarDecl *CombLBDecl = 9269 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 9270 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 9271 SemaRef.AddInitializerToDecl( 9272 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 9273 /*DirectInit*/ false); 9274 9275 // Upper bound variable, initialized with last iteration number. 9276 VarDecl *CombUBDecl = 9277 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 9278 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 9279 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 9280 /*DirectInit*/ false); 9281 9282 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 9283 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 9284 ExprResult CombCondOp = 9285 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 9286 LastIteration.get(), CombUB.get()); 9287 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 9288 CombCondOp.get()); 9289 CombEUB = 9290 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 9291 9292 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 9293 // We expect to have at least 2 more parameters than the 'parallel' 9294 // directive does - the lower and upper bounds of the previous schedule. 9295 assert(CD->getNumParams() >= 4 && 9296 "Unexpected number of parameters in loop combined directive"); 9297 9298 // Set the proper type for the bounds given what we learned from the 9299 // enclosed loops. 9300 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 9301 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 9302 9303 // Previous lower and upper bounds are obtained from the region 9304 // parameters. 9305 PrevLB = 9306 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 9307 PrevUB = 9308 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 9309 } 9310 } 9311 9312 // Build the iteration variable and its initialization before loop. 9313 ExprResult IV; 9314 ExprResult Init, CombInit; 9315 { 9316 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 9317 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 9318 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 9319 isOpenMPTaskLoopDirective(DKind) || 9320 isOpenMPDistributeDirective(DKind) || 9321 isOpenMPLoopTransformationDirective(DKind)) 9322 ? LB.get() 9323 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9324 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 9325 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 9326 9327 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9328 Expr *CombRHS = 9329 (isOpenMPWorksharingDirective(DKind) || 9330 isOpenMPTaskLoopDirective(DKind) || 9331 isOpenMPDistributeDirective(DKind)) 9332 ? CombLB.get() 9333 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 9334 CombInit = 9335 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 9336 CombInit = 9337 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 9338 } 9339 } 9340 9341 bool UseStrictCompare = 9342 RealVType->hasUnsignedIntegerRepresentation() && 9343 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 9344 return LIS.IsStrictCompare; 9345 }); 9346 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 9347 // unsigned IV)) for worksharing loops. 9348 SourceLocation CondLoc = AStmt->getBeginLoc(); 9349 Expr *BoundUB = UB.get(); 9350 if (UseStrictCompare) { 9351 BoundUB = 9352 SemaRef 9353 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 9354 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9355 .get(); 9356 BoundUB = 9357 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 9358 } 9359 ExprResult Cond = 9360 (isOpenMPWorksharingDirective(DKind) || 9361 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || 9362 isOpenMPLoopTransformationDirective(DKind)) 9363 ? SemaRef.BuildBinOp(CurScope, CondLoc, 9364 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 9365 BoundUB) 9366 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9367 NumIterations.get()); 9368 ExprResult CombDistCond; 9369 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9370 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 9371 NumIterations.get()); 9372 } 9373 9374 ExprResult CombCond; 9375 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9376 Expr *BoundCombUB = CombUB.get(); 9377 if (UseStrictCompare) { 9378 BoundCombUB = 9379 SemaRef 9380 .BuildBinOp( 9381 CurScope, CondLoc, BO_Add, BoundCombUB, 9382 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9383 .get(); 9384 BoundCombUB = 9385 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 9386 .get(); 9387 } 9388 CombCond = 9389 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9390 IV.get(), BoundCombUB); 9391 } 9392 // Loop increment (IV = IV + 1) 9393 SourceLocation IncLoc = AStmt->getBeginLoc(); 9394 ExprResult Inc = 9395 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 9396 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 9397 if (!Inc.isUsable()) 9398 return 0; 9399 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 9400 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 9401 if (!Inc.isUsable()) 9402 return 0; 9403 9404 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 9405 // Used for directives with static scheduling. 9406 // In combined construct, add combined version that use CombLB and CombUB 9407 // base variables for the update 9408 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 9409 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 9410 isOpenMPDistributeDirective(DKind) || 9411 isOpenMPLoopTransformationDirective(DKind)) { 9412 // LB + ST 9413 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 9414 if (!NextLB.isUsable()) 9415 return 0; 9416 // LB = LB + ST 9417 NextLB = 9418 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 9419 NextLB = 9420 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 9421 if (!NextLB.isUsable()) 9422 return 0; 9423 // UB + ST 9424 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 9425 if (!NextUB.isUsable()) 9426 return 0; 9427 // UB = UB + ST 9428 NextUB = 9429 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 9430 NextUB = 9431 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 9432 if (!NextUB.isUsable()) 9433 return 0; 9434 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9435 CombNextLB = 9436 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 9437 if (!NextLB.isUsable()) 9438 return 0; 9439 // LB = LB + ST 9440 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 9441 CombNextLB.get()); 9442 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 9443 /*DiscardedValue*/ false); 9444 if (!CombNextLB.isUsable()) 9445 return 0; 9446 // UB + ST 9447 CombNextUB = 9448 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 9449 if (!CombNextUB.isUsable()) 9450 return 0; 9451 // UB = UB + ST 9452 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 9453 CombNextUB.get()); 9454 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 9455 /*DiscardedValue*/ false); 9456 if (!CombNextUB.isUsable()) 9457 return 0; 9458 } 9459 } 9460 9461 // Create increment expression for distribute loop when combined in a same 9462 // directive with for as IV = IV + ST; ensure upper bound expression based 9463 // on PrevUB instead of NumIterations - used to implement 'for' when found 9464 // in combination with 'distribute', like in 'distribute parallel for' 9465 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 9466 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 9467 if (isOpenMPLoopBoundSharingDirective(DKind)) { 9468 DistCond = SemaRef.BuildBinOp( 9469 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 9470 assert(DistCond.isUsable() && "distribute cond expr was not built"); 9471 9472 DistInc = 9473 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 9474 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9475 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 9476 DistInc.get()); 9477 DistInc = 9478 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 9479 assert(DistInc.isUsable() && "distribute inc expr was not built"); 9480 9481 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 9482 // construct 9483 ExprResult NewPrevUB = PrevUB; 9484 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 9485 if (!SemaRef.Context.hasSameType(UB.get()->getType(), 9486 PrevUB.get()->getType())) { 9487 NewPrevUB = SemaRef.BuildCStyleCastExpr( 9488 DistEUBLoc, 9489 SemaRef.Context.getTrivialTypeSourceInfo(UB.get()->getType()), 9490 DistEUBLoc, NewPrevUB.get()); 9491 if (!NewPrevUB.isUsable()) 9492 return 0; 9493 } 9494 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, 9495 UB.get(), NewPrevUB.get()); 9496 ExprResult CondOp = SemaRef.ActOnConditionalOp( 9497 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), NewPrevUB.get(), UB.get()); 9498 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 9499 CondOp.get()); 9500 PrevEUB = 9501 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 9502 9503 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 9504 // parallel for is in combination with a distribute directive with 9505 // schedule(static, 1) 9506 Expr *BoundPrevUB = PrevUB.get(); 9507 if (UseStrictCompare) { 9508 BoundPrevUB = 9509 SemaRef 9510 .BuildBinOp( 9511 CurScope, CondLoc, BO_Add, BoundPrevUB, 9512 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 9513 .get(); 9514 BoundPrevUB = 9515 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 9516 .get(); 9517 } 9518 ParForInDistCond = 9519 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 9520 IV.get(), BoundPrevUB); 9521 } 9522 9523 // Build updates and final values of the loop counters. 9524 bool HasErrors = false; 9525 Built.Counters.resize(NestedLoopCount); 9526 Built.Inits.resize(NestedLoopCount); 9527 Built.Updates.resize(NestedLoopCount); 9528 Built.Finals.resize(NestedLoopCount); 9529 Built.DependentCounters.resize(NestedLoopCount); 9530 Built.DependentInits.resize(NestedLoopCount); 9531 Built.FinalsConditions.resize(NestedLoopCount); 9532 { 9533 // We implement the following algorithm for obtaining the 9534 // original loop iteration variable values based on the 9535 // value of the collapsed loop iteration variable IV. 9536 // 9537 // Let n+1 be the number of collapsed loops in the nest. 9538 // Iteration variables (I0, I1, .... In) 9539 // Iteration counts (N0, N1, ... Nn) 9540 // 9541 // Acc = IV; 9542 // 9543 // To compute Ik for loop k, 0 <= k <= n, generate: 9544 // Prod = N(k+1) * N(k+2) * ... * Nn; 9545 // Ik = Acc / Prod; 9546 // Acc -= Ik * Prod; 9547 // 9548 ExprResult Acc = IV; 9549 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 9550 LoopIterationSpace &IS = IterSpaces[Cnt]; 9551 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 9552 ExprResult Iter; 9553 9554 // Compute prod 9555 ExprResult Prod = 9556 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9557 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 9558 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 9559 IterSpaces[K].NumIterations); 9560 9561 // Iter = Acc / Prod 9562 // If there is at least one more inner loop to avoid 9563 // multiplication by 1. 9564 if (Cnt + 1 < NestedLoopCount) 9565 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 9566 Acc.get(), Prod.get()); 9567 else 9568 Iter = Acc; 9569 if (!Iter.isUsable()) { 9570 HasErrors = true; 9571 break; 9572 } 9573 9574 // Update Acc: 9575 // Acc -= Iter * Prod 9576 // Check if there is at least one more inner loop to avoid 9577 // multiplication by 1. 9578 if (Cnt + 1 < NestedLoopCount) 9579 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 9580 Iter.get(), Prod.get()); 9581 else 9582 Prod = Iter; 9583 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 9584 Acc.get(), Prod.get()); 9585 9586 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 9587 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 9588 DeclRefExpr *CounterVar = buildDeclRefExpr( 9589 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 9590 /*RefersToCapture=*/true); 9591 ExprResult Init = 9592 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 9593 IS.CounterInit, IS.IsNonRectangularLB, Captures); 9594 if (!Init.isUsable()) { 9595 HasErrors = true; 9596 break; 9597 } 9598 ExprResult Update = buildCounterUpdate( 9599 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 9600 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 9601 if (!Update.isUsable()) { 9602 HasErrors = true; 9603 break; 9604 } 9605 9606 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 9607 ExprResult Final = 9608 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 9609 IS.CounterInit, IS.NumIterations, IS.CounterStep, 9610 IS.Subtract, IS.IsNonRectangularLB, &Captures); 9611 if (!Final.isUsable()) { 9612 HasErrors = true; 9613 break; 9614 } 9615 9616 if (!Update.isUsable() || !Final.isUsable()) { 9617 HasErrors = true; 9618 break; 9619 } 9620 // Save results 9621 Built.Counters[Cnt] = IS.CounterVar; 9622 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 9623 Built.Inits[Cnt] = Init.get(); 9624 Built.Updates[Cnt] = Update.get(); 9625 Built.Finals[Cnt] = Final.get(); 9626 Built.DependentCounters[Cnt] = nullptr; 9627 Built.DependentInits[Cnt] = nullptr; 9628 Built.FinalsConditions[Cnt] = nullptr; 9629 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 9630 Built.DependentCounters[Cnt] = 9631 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9632 Built.DependentInits[Cnt] = 9633 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 9634 Built.FinalsConditions[Cnt] = IS.FinalCondition; 9635 } 9636 } 9637 } 9638 9639 if (HasErrors) 9640 return 0; 9641 9642 // Save results 9643 Built.IterationVarRef = IV.get(); 9644 Built.LastIteration = LastIteration.get(); 9645 Built.NumIterations = NumIterations.get(); 9646 Built.CalcLastIteration = SemaRef 9647 .ActOnFinishFullExpr(CalcLastIteration.get(), 9648 /*DiscardedValue=*/false) 9649 .get(); 9650 Built.PreCond = PreCond.get(); 9651 Built.PreInits = buildPreInits(C, Captures); 9652 Built.Cond = Cond.get(); 9653 Built.Init = Init.get(); 9654 Built.Inc = Inc.get(); 9655 Built.LB = LB.get(); 9656 Built.UB = UB.get(); 9657 Built.IL = IL.get(); 9658 Built.ST = ST.get(); 9659 Built.EUB = EUB.get(); 9660 Built.NLB = NextLB.get(); 9661 Built.NUB = NextUB.get(); 9662 Built.PrevLB = PrevLB.get(); 9663 Built.PrevUB = PrevUB.get(); 9664 Built.DistInc = DistInc.get(); 9665 Built.PrevEUB = PrevEUB.get(); 9666 Built.DistCombinedFields.LB = CombLB.get(); 9667 Built.DistCombinedFields.UB = CombUB.get(); 9668 Built.DistCombinedFields.EUB = CombEUB.get(); 9669 Built.DistCombinedFields.Init = CombInit.get(); 9670 Built.DistCombinedFields.Cond = CombCond.get(); 9671 Built.DistCombinedFields.NLB = CombNextLB.get(); 9672 Built.DistCombinedFields.NUB = CombNextUB.get(); 9673 Built.DistCombinedFields.DistCond = CombDistCond.get(); 9674 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 9675 9676 return NestedLoopCount; 9677 } 9678 9679 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 9680 auto CollapseClauses = 9681 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 9682 if (CollapseClauses.begin() != CollapseClauses.end()) 9683 return (*CollapseClauses.begin())->getNumForLoops(); 9684 return nullptr; 9685 } 9686 9687 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 9688 auto OrderedClauses = 9689 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 9690 if (OrderedClauses.begin() != OrderedClauses.end()) 9691 return (*OrderedClauses.begin())->getNumForLoops(); 9692 return nullptr; 9693 } 9694 9695 static bool checkSimdlenSafelenSpecified(Sema &S, 9696 const ArrayRef<OMPClause *> Clauses) { 9697 const OMPSafelenClause *Safelen = nullptr; 9698 const OMPSimdlenClause *Simdlen = nullptr; 9699 9700 for (const OMPClause *Clause : Clauses) { 9701 if (Clause->getClauseKind() == OMPC_safelen) 9702 Safelen = cast<OMPSafelenClause>(Clause); 9703 else if (Clause->getClauseKind() == OMPC_simdlen) 9704 Simdlen = cast<OMPSimdlenClause>(Clause); 9705 if (Safelen && Simdlen) 9706 break; 9707 } 9708 9709 if (Simdlen && Safelen) { 9710 const Expr *SimdlenLength = Simdlen->getSimdlen(); 9711 const Expr *SafelenLength = Safelen->getSafelen(); 9712 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 9713 SimdlenLength->isInstantiationDependent() || 9714 SimdlenLength->containsUnexpandedParameterPack()) 9715 return false; 9716 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 9717 SafelenLength->isInstantiationDependent() || 9718 SafelenLength->containsUnexpandedParameterPack()) 9719 return false; 9720 Expr::EvalResult SimdlenResult, SafelenResult; 9721 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 9722 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 9723 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 9724 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 9725 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 9726 // If both simdlen and safelen clauses are specified, the value of the 9727 // simdlen parameter must be less than or equal to the value of the safelen 9728 // parameter. 9729 if (SimdlenRes > SafelenRes) { 9730 S.Diag(SimdlenLength->getExprLoc(), 9731 diag::err_omp_wrong_simdlen_safelen_values) 9732 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 9733 return true; 9734 } 9735 } 9736 return false; 9737 } 9738 9739 StmtResult 9740 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9741 SourceLocation StartLoc, SourceLocation EndLoc, 9742 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9743 if (!AStmt) 9744 return StmtError(); 9745 9746 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9747 OMPLoopBasedDirective::HelperExprs B; 9748 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9749 // define the nested loops number. 9750 unsigned NestedLoopCount = checkOpenMPLoop( 9751 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9752 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9753 if (NestedLoopCount == 0) 9754 return StmtError(); 9755 9756 assert((CurContext->isDependentContext() || B.builtAll()) && 9757 "omp simd loop exprs were not built"); 9758 9759 if (!CurContext->isDependentContext()) { 9760 // Finalize the clauses that need pre-built expressions for CodeGen. 9761 for (OMPClause *C : Clauses) { 9762 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9763 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9764 B.NumIterations, *this, CurScope, 9765 DSAStack)) 9766 return StmtError(); 9767 } 9768 } 9769 9770 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9771 return StmtError(); 9772 9773 setFunctionHasBranchProtectedScope(); 9774 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9775 Clauses, AStmt, B); 9776 } 9777 9778 StmtResult 9779 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 9780 SourceLocation StartLoc, SourceLocation EndLoc, 9781 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9782 if (!AStmt) 9783 return StmtError(); 9784 9785 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9786 OMPLoopBasedDirective::HelperExprs B; 9787 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9788 // define the nested loops number. 9789 unsigned NestedLoopCount = checkOpenMPLoop( 9790 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 9791 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 9792 if (NestedLoopCount == 0) 9793 return StmtError(); 9794 9795 assert((CurContext->isDependentContext() || B.builtAll()) && 9796 "omp for loop exprs were not built"); 9797 9798 if (!CurContext->isDependentContext()) { 9799 // Finalize the clauses that need pre-built expressions for CodeGen. 9800 for (OMPClause *C : Clauses) { 9801 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9802 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9803 B.NumIterations, *this, CurScope, 9804 DSAStack)) 9805 return StmtError(); 9806 } 9807 } 9808 9809 setFunctionHasBranchProtectedScope(); 9810 return OMPForDirective::Create( 9811 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9812 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9813 } 9814 9815 StmtResult Sema::ActOnOpenMPForSimdDirective( 9816 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9817 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9818 if (!AStmt) 9819 return StmtError(); 9820 9821 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9822 OMPLoopBasedDirective::HelperExprs B; 9823 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9824 // define the nested loops number. 9825 unsigned NestedLoopCount = 9826 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 9827 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9828 VarsWithImplicitDSA, B); 9829 if (NestedLoopCount == 0) 9830 return StmtError(); 9831 9832 assert((CurContext->isDependentContext() || B.builtAll()) && 9833 "omp for simd loop exprs were not built"); 9834 9835 if (!CurContext->isDependentContext()) { 9836 // Finalize the clauses that need pre-built expressions for CodeGen. 9837 for (OMPClause *C : Clauses) { 9838 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9839 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9840 B.NumIterations, *this, CurScope, 9841 DSAStack)) 9842 return StmtError(); 9843 } 9844 } 9845 9846 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9847 return StmtError(); 9848 9849 setFunctionHasBranchProtectedScope(); 9850 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 9851 Clauses, AStmt, B); 9852 } 9853 9854 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 9855 Stmt *AStmt, 9856 SourceLocation StartLoc, 9857 SourceLocation EndLoc) { 9858 if (!AStmt) 9859 return StmtError(); 9860 9861 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9862 auto BaseStmt = AStmt; 9863 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9864 BaseStmt = CS->getCapturedStmt(); 9865 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9866 auto S = C->children(); 9867 if (S.begin() == S.end()) 9868 return StmtError(); 9869 // All associated statements must be '#pragma omp section' except for 9870 // the first one. 9871 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9872 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9873 if (SectionStmt) 9874 Diag(SectionStmt->getBeginLoc(), 9875 diag::err_omp_sections_substmt_not_section); 9876 return StmtError(); 9877 } 9878 cast<OMPSectionDirective>(SectionStmt) 9879 ->setHasCancel(DSAStack->isCancelRegion()); 9880 } 9881 } else { 9882 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 9883 return StmtError(); 9884 } 9885 9886 setFunctionHasBranchProtectedScope(); 9887 9888 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9889 DSAStack->getTaskgroupReductionRef(), 9890 DSAStack->isCancelRegion()); 9891 } 9892 9893 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 9894 SourceLocation StartLoc, 9895 SourceLocation EndLoc) { 9896 if (!AStmt) 9897 return StmtError(); 9898 9899 setFunctionHasBranchProtectedScope(); 9900 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 9901 9902 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 9903 DSAStack->isCancelRegion()); 9904 } 9905 9906 static Expr *getDirectCallExpr(Expr *E) { 9907 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9908 if (auto *CE = dyn_cast<CallExpr>(E)) 9909 if (CE->getDirectCallee()) 9910 return E; 9911 return nullptr; 9912 } 9913 9914 StmtResult Sema::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses, 9915 Stmt *AStmt, 9916 SourceLocation StartLoc, 9917 SourceLocation EndLoc) { 9918 if (!AStmt) 9919 return StmtError(); 9920 9921 Stmt *S = cast<CapturedStmt>(AStmt)->getCapturedStmt(); 9922 9923 // 5.1 OpenMP 9924 // expression-stmt : an expression statement with one of the following forms: 9925 // expression = target-call ( [expression-list] ); 9926 // target-call ( [expression-list] ); 9927 9928 SourceLocation TargetCallLoc; 9929 9930 if (!CurContext->isDependentContext()) { 9931 Expr *TargetCall = nullptr; 9932 9933 auto *E = dyn_cast<Expr>(S); 9934 if (!E) { 9935 Diag(S->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9936 return StmtError(); 9937 } 9938 9939 E = E->IgnoreParenCasts()->IgnoreImplicit(); 9940 9941 if (auto *BO = dyn_cast<BinaryOperator>(E)) { 9942 if (BO->getOpcode() == BO_Assign) 9943 TargetCall = getDirectCallExpr(BO->getRHS()); 9944 } else { 9945 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(E)) 9946 if (COCE->getOperator() == OO_Equal) 9947 TargetCall = getDirectCallExpr(COCE->getArg(1)); 9948 if (!TargetCall) 9949 TargetCall = getDirectCallExpr(E); 9950 } 9951 if (!TargetCall) { 9952 Diag(E->getBeginLoc(), diag::err_omp_dispatch_statement_call); 9953 return StmtError(); 9954 } 9955 TargetCallLoc = TargetCall->getExprLoc(); 9956 } 9957 9958 setFunctionHasBranchProtectedScope(); 9959 9960 return OMPDispatchDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9961 TargetCallLoc); 9962 } 9963 9964 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 9965 Stmt *AStmt, 9966 SourceLocation StartLoc, 9967 SourceLocation EndLoc) { 9968 if (!AStmt) 9969 return StmtError(); 9970 9971 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9972 9973 setFunctionHasBranchProtectedScope(); 9974 9975 // OpenMP [2.7.3, single Construct, Restrictions] 9976 // The copyprivate clause must not be used with the nowait clause. 9977 const OMPClause *Nowait = nullptr; 9978 const OMPClause *Copyprivate = nullptr; 9979 for (const OMPClause *Clause : Clauses) { 9980 if (Clause->getClauseKind() == OMPC_nowait) 9981 Nowait = Clause; 9982 else if (Clause->getClauseKind() == OMPC_copyprivate) 9983 Copyprivate = Clause; 9984 if (Copyprivate && Nowait) { 9985 Diag(Copyprivate->getBeginLoc(), 9986 diag::err_omp_single_copyprivate_with_nowait); 9987 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 9988 return StmtError(); 9989 } 9990 } 9991 9992 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9993 } 9994 9995 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 9996 SourceLocation StartLoc, 9997 SourceLocation EndLoc) { 9998 if (!AStmt) 9999 return StmtError(); 10000 10001 setFunctionHasBranchProtectedScope(); 10002 10003 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 10004 } 10005 10006 StmtResult Sema::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses, 10007 Stmt *AStmt, 10008 SourceLocation StartLoc, 10009 SourceLocation EndLoc) { 10010 if (!AStmt) 10011 return StmtError(); 10012 10013 setFunctionHasBranchProtectedScope(); 10014 10015 return OMPMaskedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10016 } 10017 10018 StmtResult Sema::ActOnOpenMPCriticalDirective( 10019 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 10020 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 10021 if (!AStmt) 10022 return StmtError(); 10023 10024 bool ErrorFound = false; 10025 llvm::APSInt Hint; 10026 SourceLocation HintLoc; 10027 bool DependentHint = false; 10028 for (const OMPClause *C : Clauses) { 10029 if (C->getClauseKind() == OMPC_hint) { 10030 if (!DirName.getName()) { 10031 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 10032 ErrorFound = true; 10033 } 10034 Expr *E = cast<OMPHintClause>(C)->getHint(); 10035 if (E->isTypeDependent() || E->isValueDependent() || 10036 E->isInstantiationDependent()) { 10037 DependentHint = true; 10038 } else { 10039 Hint = E->EvaluateKnownConstInt(Context); 10040 HintLoc = C->getBeginLoc(); 10041 } 10042 } 10043 } 10044 if (ErrorFound) 10045 return StmtError(); 10046 const auto Pair = DSAStack->getCriticalWithHint(DirName); 10047 if (Pair.first && DirName.getName() && !DependentHint) { 10048 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 10049 Diag(StartLoc, diag::err_omp_critical_with_hint); 10050 if (HintLoc.isValid()) 10051 Diag(HintLoc, diag::note_omp_critical_hint_here) 10052 << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false); 10053 else 10054 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 10055 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 10056 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 10057 << 1 10058 << toString(C->getHint()->EvaluateKnownConstInt(Context), 10059 /*Radix=*/10, /*Signed=*/false); 10060 } else { 10061 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 10062 } 10063 } 10064 } 10065 10066 setFunctionHasBranchProtectedScope(); 10067 10068 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 10069 Clauses, AStmt); 10070 if (!Pair.first && DirName.getName() && !DependentHint) 10071 DSAStack->addCriticalWithHint(Dir, Hint); 10072 return Dir; 10073 } 10074 10075 StmtResult Sema::ActOnOpenMPParallelForDirective( 10076 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10077 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10078 if (!AStmt) 10079 return StmtError(); 10080 10081 auto *CS = cast<CapturedStmt>(AStmt); 10082 // 1.2.2 OpenMP Language Terminology 10083 // Structured block - An executable statement with a single entry at the 10084 // top and a single exit at the bottom. 10085 // The point of exit cannot be a branch out of the structured block. 10086 // longjmp() and throw() must not violate the entry/exit criteria. 10087 CS->getCapturedDecl()->setNothrow(); 10088 10089 OMPLoopBasedDirective::HelperExprs B; 10090 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10091 // define the nested loops number. 10092 unsigned NestedLoopCount = 10093 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 10094 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10095 VarsWithImplicitDSA, B); 10096 if (NestedLoopCount == 0) 10097 return StmtError(); 10098 10099 assert((CurContext->isDependentContext() || B.builtAll()) && 10100 "omp parallel for loop exprs were not built"); 10101 10102 if (!CurContext->isDependentContext()) { 10103 // Finalize the clauses that need pre-built expressions for CodeGen. 10104 for (OMPClause *C : Clauses) { 10105 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10106 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10107 B.NumIterations, *this, CurScope, 10108 DSAStack)) 10109 return StmtError(); 10110 } 10111 } 10112 10113 setFunctionHasBranchProtectedScope(); 10114 return OMPParallelForDirective::Create( 10115 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10116 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10117 } 10118 10119 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 10120 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10121 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10122 if (!AStmt) 10123 return StmtError(); 10124 10125 auto *CS = cast<CapturedStmt>(AStmt); 10126 // 1.2.2 OpenMP Language Terminology 10127 // Structured block - An executable statement with a single entry at the 10128 // top and a single exit at the bottom. 10129 // The point of exit cannot be a branch out of the structured block. 10130 // longjmp() and throw() must not violate the entry/exit criteria. 10131 CS->getCapturedDecl()->setNothrow(); 10132 10133 OMPLoopBasedDirective::HelperExprs B; 10134 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10135 // define the nested loops number. 10136 unsigned NestedLoopCount = 10137 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 10138 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 10139 VarsWithImplicitDSA, B); 10140 if (NestedLoopCount == 0) 10141 return StmtError(); 10142 10143 if (!CurContext->isDependentContext()) { 10144 // Finalize the clauses that need pre-built expressions for CodeGen. 10145 for (OMPClause *C : Clauses) { 10146 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10147 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10148 B.NumIterations, *this, CurScope, 10149 DSAStack)) 10150 return StmtError(); 10151 } 10152 } 10153 10154 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10155 return StmtError(); 10156 10157 setFunctionHasBranchProtectedScope(); 10158 return OMPParallelForSimdDirective::Create( 10159 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10160 } 10161 10162 StmtResult 10163 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 10164 Stmt *AStmt, SourceLocation StartLoc, 10165 SourceLocation EndLoc) { 10166 if (!AStmt) 10167 return StmtError(); 10168 10169 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10170 auto *CS = cast<CapturedStmt>(AStmt); 10171 // 1.2.2 OpenMP Language Terminology 10172 // Structured block - An executable statement with a single entry at the 10173 // top and a single exit at the bottom. 10174 // The point of exit cannot be a branch out of the structured block. 10175 // longjmp() and throw() must not violate the entry/exit criteria. 10176 CS->getCapturedDecl()->setNothrow(); 10177 10178 setFunctionHasBranchProtectedScope(); 10179 10180 return OMPParallelMasterDirective::Create( 10181 Context, StartLoc, EndLoc, Clauses, AStmt, 10182 DSAStack->getTaskgroupReductionRef()); 10183 } 10184 10185 StmtResult 10186 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 10187 Stmt *AStmt, SourceLocation StartLoc, 10188 SourceLocation EndLoc) { 10189 if (!AStmt) 10190 return StmtError(); 10191 10192 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10193 auto BaseStmt = AStmt; 10194 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 10195 BaseStmt = CS->getCapturedStmt(); 10196 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 10197 auto S = C->children(); 10198 if (S.begin() == S.end()) 10199 return StmtError(); 10200 // All associated statements must be '#pragma omp section' except for 10201 // the first one. 10202 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 10203 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 10204 if (SectionStmt) 10205 Diag(SectionStmt->getBeginLoc(), 10206 diag::err_omp_parallel_sections_substmt_not_section); 10207 return StmtError(); 10208 } 10209 cast<OMPSectionDirective>(SectionStmt) 10210 ->setHasCancel(DSAStack->isCancelRegion()); 10211 } 10212 } else { 10213 Diag(AStmt->getBeginLoc(), 10214 diag::err_omp_parallel_sections_not_compound_stmt); 10215 return StmtError(); 10216 } 10217 10218 setFunctionHasBranchProtectedScope(); 10219 10220 return OMPParallelSectionsDirective::Create( 10221 Context, StartLoc, EndLoc, Clauses, AStmt, 10222 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10223 } 10224 10225 /// Find and diagnose mutually exclusive clause kinds. 10226 static bool checkMutuallyExclusiveClauses( 10227 Sema &S, ArrayRef<OMPClause *> Clauses, 10228 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) { 10229 const OMPClause *PrevClause = nullptr; 10230 bool ErrorFound = false; 10231 for (const OMPClause *C : Clauses) { 10232 if (llvm::is_contained(MutuallyExclusiveClauses, C->getClauseKind())) { 10233 if (!PrevClause) { 10234 PrevClause = C; 10235 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10236 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10237 << getOpenMPClauseName(C->getClauseKind()) 10238 << getOpenMPClauseName(PrevClause->getClauseKind()); 10239 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10240 << getOpenMPClauseName(PrevClause->getClauseKind()); 10241 ErrorFound = true; 10242 } 10243 } 10244 } 10245 return ErrorFound; 10246 } 10247 10248 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 10249 Stmt *AStmt, SourceLocation StartLoc, 10250 SourceLocation EndLoc) { 10251 if (!AStmt) 10252 return StmtError(); 10253 10254 // OpenMP 5.0, 2.10.1 task Construct 10255 // If a detach clause appears on the directive, then a mergeable clause cannot 10256 // appear on the same directive. 10257 if (checkMutuallyExclusiveClauses(*this, Clauses, 10258 {OMPC_detach, OMPC_mergeable})) 10259 return StmtError(); 10260 10261 auto *CS = cast<CapturedStmt>(AStmt); 10262 // 1.2.2 OpenMP Language Terminology 10263 // Structured block - An executable statement with a single entry at the 10264 // top and a single exit at the bottom. 10265 // The point of exit cannot be a branch out of the structured block. 10266 // longjmp() and throw() must not violate the entry/exit criteria. 10267 CS->getCapturedDecl()->setNothrow(); 10268 10269 setFunctionHasBranchProtectedScope(); 10270 10271 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10272 DSAStack->isCancelRegion()); 10273 } 10274 10275 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 10276 SourceLocation EndLoc) { 10277 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 10278 } 10279 10280 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 10281 SourceLocation EndLoc) { 10282 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 10283 } 10284 10285 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 10286 SourceLocation EndLoc) { 10287 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 10288 } 10289 10290 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 10291 Stmt *AStmt, 10292 SourceLocation StartLoc, 10293 SourceLocation EndLoc) { 10294 if (!AStmt) 10295 return StmtError(); 10296 10297 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10298 10299 setFunctionHasBranchProtectedScope(); 10300 10301 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 10302 AStmt, 10303 DSAStack->getTaskgroupReductionRef()); 10304 } 10305 10306 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 10307 SourceLocation StartLoc, 10308 SourceLocation EndLoc) { 10309 OMPFlushClause *FC = nullptr; 10310 OMPClause *OrderClause = nullptr; 10311 for (OMPClause *C : Clauses) { 10312 if (C->getClauseKind() == OMPC_flush) 10313 FC = cast<OMPFlushClause>(C); 10314 else 10315 OrderClause = C; 10316 } 10317 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10318 SourceLocation MemOrderLoc; 10319 for (const OMPClause *C : Clauses) { 10320 if (C->getClauseKind() == OMPC_acq_rel || 10321 C->getClauseKind() == OMPC_acquire || 10322 C->getClauseKind() == OMPC_release) { 10323 if (MemOrderKind != OMPC_unknown) { 10324 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10325 << getOpenMPDirectiveName(OMPD_flush) << 1 10326 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10327 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10328 << getOpenMPClauseName(MemOrderKind); 10329 } else { 10330 MemOrderKind = C->getClauseKind(); 10331 MemOrderLoc = C->getBeginLoc(); 10332 } 10333 } 10334 } 10335 if (FC && OrderClause) { 10336 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 10337 << getOpenMPClauseName(OrderClause->getClauseKind()); 10338 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 10339 << getOpenMPClauseName(OrderClause->getClauseKind()); 10340 return StmtError(); 10341 } 10342 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 10343 } 10344 10345 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 10346 SourceLocation StartLoc, 10347 SourceLocation EndLoc) { 10348 if (Clauses.empty()) { 10349 Diag(StartLoc, diag::err_omp_depobj_expected); 10350 return StmtError(); 10351 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 10352 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 10353 return StmtError(); 10354 } 10355 // Only depobj expression and another single clause is allowed. 10356 if (Clauses.size() > 2) { 10357 Diag(Clauses[2]->getBeginLoc(), 10358 diag::err_omp_depobj_single_clause_expected); 10359 return StmtError(); 10360 } else if (Clauses.size() < 1) { 10361 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 10362 return StmtError(); 10363 } 10364 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 10365 } 10366 10367 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 10368 SourceLocation StartLoc, 10369 SourceLocation EndLoc) { 10370 // Check that exactly one clause is specified. 10371 if (Clauses.size() != 1) { 10372 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 10373 diag::err_omp_scan_single_clause_expected); 10374 return StmtError(); 10375 } 10376 // Check that scan directive is used in the scopeof the OpenMP loop body. 10377 if (Scope *S = DSAStack->getCurScope()) { 10378 Scope *ParentS = S->getParent(); 10379 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 10380 !ParentS->getBreakParent()->isOpenMPLoopScope()) 10381 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 10382 << getOpenMPDirectiveName(OMPD_scan) << 5); 10383 } 10384 // Check that only one instance of scan directives is used in the same outer 10385 // region. 10386 if (DSAStack->doesParentHasScanDirective()) { 10387 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 10388 Diag(DSAStack->getParentScanDirectiveLoc(), 10389 diag::note_omp_previous_directive) 10390 << "scan"; 10391 return StmtError(); 10392 } 10393 DSAStack->setParentHasScanDirective(StartLoc); 10394 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 10395 } 10396 10397 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 10398 Stmt *AStmt, 10399 SourceLocation StartLoc, 10400 SourceLocation EndLoc) { 10401 const OMPClause *DependFound = nullptr; 10402 const OMPClause *DependSourceClause = nullptr; 10403 const OMPClause *DependSinkClause = nullptr; 10404 bool ErrorFound = false; 10405 const OMPThreadsClause *TC = nullptr; 10406 const OMPSIMDClause *SC = nullptr; 10407 for (const OMPClause *C : Clauses) { 10408 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 10409 DependFound = C; 10410 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 10411 if (DependSourceClause) { 10412 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 10413 << getOpenMPDirectiveName(OMPD_ordered) 10414 << getOpenMPClauseName(OMPC_depend) << 2; 10415 ErrorFound = true; 10416 } else { 10417 DependSourceClause = C; 10418 } 10419 if (DependSinkClause) { 10420 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10421 << 0; 10422 ErrorFound = true; 10423 } 10424 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 10425 if (DependSourceClause) { 10426 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 10427 << 1; 10428 ErrorFound = true; 10429 } 10430 DependSinkClause = C; 10431 } 10432 } else if (C->getClauseKind() == OMPC_threads) { 10433 TC = cast<OMPThreadsClause>(C); 10434 } else if (C->getClauseKind() == OMPC_simd) { 10435 SC = cast<OMPSIMDClause>(C); 10436 } 10437 } 10438 if (!ErrorFound && !SC && 10439 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 10440 // OpenMP [2.8.1,simd Construct, Restrictions] 10441 // An ordered construct with the simd clause is the only OpenMP construct 10442 // that can appear in the simd region. 10443 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 10444 << (LangOpts.OpenMP >= 50 ? 1 : 0); 10445 ErrorFound = true; 10446 } else if (DependFound && (TC || SC)) { 10447 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 10448 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 10449 ErrorFound = true; 10450 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 10451 Diag(DependFound->getBeginLoc(), 10452 diag::err_omp_ordered_directive_without_param); 10453 ErrorFound = true; 10454 } else if (TC || Clauses.empty()) { 10455 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 10456 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 10457 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 10458 << (TC != nullptr); 10459 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 10460 ErrorFound = true; 10461 } 10462 } 10463 if ((!AStmt && !DependFound) || ErrorFound) 10464 return StmtError(); 10465 10466 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 10467 // During execution of an iteration of a worksharing-loop or a loop nest 10468 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 10469 // must not execute more than one ordered region corresponding to an ordered 10470 // construct without a depend clause. 10471 if (!DependFound) { 10472 if (DSAStack->doesParentHasOrderedDirective()) { 10473 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 10474 Diag(DSAStack->getParentOrderedDirectiveLoc(), 10475 diag::note_omp_previous_directive) 10476 << "ordered"; 10477 return StmtError(); 10478 } 10479 DSAStack->setParentHasOrderedDirective(StartLoc); 10480 } 10481 10482 if (AStmt) { 10483 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10484 10485 setFunctionHasBranchProtectedScope(); 10486 } 10487 10488 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10489 } 10490 10491 namespace { 10492 /// Helper class for checking expression in 'omp atomic [update]' 10493 /// construct. 10494 class OpenMPAtomicUpdateChecker { 10495 /// Error results for atomic update expressions. 10496 enum ExprAnalysisErrorCode { 10497 /// A statement is not an expression statement. 10498 NotAnExpression, 10499 /// Expression is not builtin binary or unary operation. 10500 NotABinaryOrUnaryExpression, 10501 /// Unary operation is not post-/pre- increment/decrement operation. 10502 NotAnUnaryIncDecExpression, 10503 /// An expression is not of scalar type. 10504 NotAScalarType, 10505 /// A binary operation is not an assignment operation. 10506 NotAnAssignmentOp, 10507 /// RHS part of the binary operation is not a binary expression. 10508 NotABinaryExpression, 10509 /// RHS part is not additive/multiplicative/shift/biwise binary 10510 /// expression. 10511 NotABinaryOperator, 10512 /// RHS binary operation does not have reference to the updated LHS 10513 /// part. 10514 NotAnUpdateExpression, 10515 /// No errors is found. 10516 NoError 10517 }; 10518 /// Reference to Sema. 10519 Sema &SemaRef; 10520 /// A location for note diagnostics (when error is found). 10521 SourceLocation NoteLoc; 10522 /// 'x' lvalue part of the source atomic expression. 10523 Expr *X; 10524 /// 'expr' rvalue part of the source atomic expression. 10525 Expr *E; 10526 /// Helper expression of the form 10527 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10528 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10529 Expr *UpdateExpr; 10530 /// Is 'x' a LHS in a RHS part of full update expression. It is 10531 /// important for non-associative operations. 10532 bool IsXLHSInRHSPart; 10533 BinaryOperatorKind Op; 10534 SourceLocation OpLoc; 10535 /// true if the source expression is a postfix unary operation, false 10536 /// if it is a prefix unary operation. 10537 bool IsPostfixUpdate; 10538 10539 public: 10540 OpenMPAtomicUpdateChecker(Sema &SemaRef) 10541 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 10542 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 10543 /// Check specified statement that it is suitable for 'atomic update' 10544 /// constructs and extract 'x', 'expr' and Operation from the original 10545 /// expression. If DiagId and NoteId == 0, then only check is performed 10546 /// without error notification. 10547 /// \param DiagId Diagnostic which should be emitted if error is found. 10548 /// \param NoteId Diagnostic note for the main error message. 10549 /// \return true if statement is not an update expression, false otherwise. 10550 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 10551 /// Return the 'x' lvalue part of the source atomic expression. 10552 Expr *getX() const { return X; } 10553 /// Return the 'expr' rvalue part of the source atomic expression. 10554 Expr *getExpr() const { return E; } 10555 /// Return the update expression used in calculation of the updated 10556 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 10557 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 10558 Expr *getUpdateExpr() const { return UpdateExpr; } 10559 /// Return true if 'x' is LHS in RHS part of full update expression, 10560 /// false otherwise. 10561 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 10562 10563 /// true if the source expression is a postfix unary operation, false 10564 /// if it is a prefix unary operation. 10565 bool isPostfixUpdate() const { return IsPostfixUpdate; } 10566 10567 private: 10568 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 10569 unsigned NoteId = 0); 10570 }; 10571 } // namespace 10572 10573 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 10574 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 10575 ExprAnalysisErrorCode ErrorFound = NoError; 10576 SourceLocation ErrorLoc, NoteLoc; 10577 SourceRange ErrorRange, NoteRange; 10578 // Allowed constructs are: 10579 // x = x binop expr; 10580 // x = expr binop x; 10581 if (AtomicBinOp->getOpcode() == BO_Assign) { 10582 X = AtomicBinOp->getLHS(); 10583 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 10584 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 10585 if (AtomicInnerBinOp->isMultiplicativeOp() || 10586 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 10587 AtomicInnerBinOp->isBitwiseOp()) { 10588 Op = AtomicInnerBinOp->getOpcode(); 10589 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 10590 Expr *LHS = AtomicInnerBinOp->getLHS(); 10591 Expr *RHS = AtomicInnerBinOp->getRHS(); 10592 llvm::FoldingSetNodeID XId, LHSId, RHSId; 10593 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 10594 /*Canonical=*/true); 10595 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 10596 /*Canonical=*/true); 10597 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 10598 /*Canonical=*/true); 10599 if (XId == LHSId) { 10600 E = RHS; 10601 IsXLHSInRHSPart = true; 10602 } else if (XId == RHSId) { 10603 E = LHS; 10604 IsXLHSInRHSPart = false; 10605 } else { 10606 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10607 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10608 NoteLoc = X->getExprLoc(); 10609 NoteRange = X->getSourceRange(); 10610 ErrorFound = NotAnUpdateExpression; 10611 } 10612 } else { 10613 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 10614 ErrorRange = AtomicInnerBinOp->getSourceRange(); 10615 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 10616 NoteRange = SourceRange(NoteLoc, NoteLoc); 10617 ErrorFound = NotABinaryOperator; 10618 } 10619 } else { 10620 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 10621 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 10622 ErrorFound = NotABinaryExpression; 10623 } 10624 } else { 10625 ErrorLoc = AtomicBinOp->getExprLoc(); 10626 ErrorRange = AtomicBinOp->getSourceRange(); 10627 NoteLoc = AtomicBinOp->getOperatorLoc(); 10628 NoteRange = SourceRange(NoteLoc, NoteLoc); 10629 ErrorFound = NotAnAssignmentOp; 10630 } 10631 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10632 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10633 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10634 return true; 10635 } 10636 if (SemaRef.CurContext->isDependentContext()) 10637 E = X = UpdateExpr = nullptr; 10638 return ErrorFound != NoError; 10639 } 10640 10641 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 10642 unsigned NoteId) { 10643 ExprAnalysisErrorCode ErrorFound = NoError; 10644 SourceLocation ErrorLoc, NoteLoc; 10645 SourceRange ErrorRange, NoteRange; 10646 // Allowed constructs are: 10647 // x++; 10648 // x--; 10649 // ++x; 10650 // --x; 10651 // x binop= expr; 10652 // x = x binop expr; 10653 // x = expr binop x; 10654 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 10655 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 10656 if (AtomicBody->getType()->isScalarType() || 10657 AtomicBody->isInstantiationDependent()) { 10658 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 10659 AtomicBody->IgnoreParenImpCasts())) { 10660 // Check for Compound Assignment Operation 10661 Op = BinaryOperator::getOpForCompoundAssignment( 10662 AtomicCompAssignOp->getOpcode()); 10663 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 10664 E = AtomicCompAssignOp->getRHS(); 10665 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 10666 IsXLHSInRHSPart = true; 10667 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 10668 AtomicBody->IgnoreParenImpCasts())) { 10669 // Check for Binary Operation 10670 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 10671 return true; 10672 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 10673 AtomicBody->IgnoreParenImpCasts())) { 10674 // Check for Unary Operation 10675 if (AtomicUnaryOp->isIncrementDecrementOp()) { 10676 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 10677 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 10678 OpLoc = AtomicUnaryOp->getOperatorLoc(); 10679 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 10680 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 10681 IsXLHSInRHSPart = true; 10682 } else { 10683 ErrorFound = NotAnUnaryIncDecExpression; 10684 ErrorLoc = AtomicUnaryOp->getExprLoc(); 10685 ErrorRange = AtomicUnaryOp->getSourceRange(); 10686 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 10687 NoteRange = SourceRange(NoteLoc, NoteLoc); 10688 } 10689 } else if (!AtomicBody->isInstantiationDependent()) { 10690 ErrorFound = NotABinaryOrUnaryExpression; 10691 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 10692 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 10693 } 10694 } else { 10695 ErrorFound = NotAScalarType; 10696 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 10697 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10698 } 10699 } else { 10700 ErrorFound = NotAnExpression; 10701 NoteLoc = ErrorLoc = S->getBeginLoc(); 10702 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10703 } 10704 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 10705 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 10706 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 10707 return true; 10708 } 10709 if (SemaRef.CurContext->isDependentContext()) 10710 E = X = UpdateExpr = nullptr; 10711 if (ErrorFound == NoError && E && X) { 10712 // Build an update expression of form 'OpaqueValueExpr(x) binop 10713 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 10714 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 10715 auto *OVEX = new (SemaRef.getASTContext()) 10716 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue); 10717 auto *OVEExpr = new (SemaRef.getASTContext()) 10718 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue); 10719 ExprResult Update = 10720 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 10721 IsXLHSInRHSPart ? OVEExpr : OVEX); 10722 if (Update.isInvalid()) 10723 return true; 10724 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 10725 Sema::AA_Casting); 10726 if (Update.isInvalid()) 10727 return true; 10728 UpdateExpr = Update.get(); 10729 } 10730 return ErrorFound != NoError; 10731 } 10732 10733 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 10734 Stmt *AStmt, 10735 SourceLocation StartLoc, 10736 SourceLocation EndLoc) { 10737 // Register location of the first atomic directive. 10738 DSAStack->addAtomicDirectiveLoc(StartLoc); 10739 if (!AStmt) 10740 return StmtError(); 10741 10742 // 1.2.2 OpenMP Language Terminology 10743 // Structured block - An executable statement with a single entry at the 10744 // top and a single exit at the bottom. 10745 // The point of exit cannot be a branch out of the structured block. 10746 // longjmp() and throw() must not violate the entry/exit criteria. 10747 OpenMPClauseKind AtomicKind = OMPC_unknown; 10748 SourceLocation AtomicKindLoc; 10749 OpenMPClauseKind MemOrderKind = OMPC_unknown; 10750 SourceLocation MemOrderLoc; 10751 for (const OMPClause *C : Clauses) { 10752 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 10753 C->getClauseKind() == OMPC_update || 10754 C->getClauseKind() == OMPC_capture) { 10755 if (AtomicKind != OMPC_unknown) { 10756 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 10757 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10758 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 10759 << getOpenMPClauseName(AtomicKind); 10760 } else { 10761 AtomicKind = C->getClauseKind(); 10762 AtomicKindLoc = C->getBeginLoc(); 10763 } 10764 } 10765 if (C->getClauseKind() == OMPC_seq_cst || 10766 C->getClauseKind() == OMPC_acq_rel || 10767 C->getClauseKind() == OMPC_acquire || 10768 C->getClauseKind() == OMPC_release || 10769 C->getClauseKind() == OMPC_relaxed) { 10770 if (MemOrderKind != OMPC_unknown) { 10771 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 10772 << getOpenMPDirectiveName(OMPD_atomic) << 0 10773 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 10774 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10775 << getOpenMPClauseName(MemOrderKind); 10776 } else { 10777 MemOrderKind = C->getClauseKind(); 10778 MemOrderLoc = C->getBeginLoc(); 10779 } 10780 } 10781 } 10782 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 10783 // If atomic-clause is read then memory-order-clause must not be acq_rel or 10784 // release. 10785 // If atomic-clause is write then memory-order-clause must not be acq_rel or 10786 // acquire. 10787 // If atomic-clause is update or not present then memory-order-clause must not 10788 // be acq_rel or acquire. 10789 if ((AtomicKind == OMPC_read && 10790 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 10791 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 10792 AtomicKind == OMPC_unknown) && 10793 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 10794 SourceLocation Loc = AtomicKindLoc; 10795 if (AtomicKind == OMPC_unknown) 10796 Loc = StartLoc; 10797 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 10798 << getOpenMPClauseName(AtomicKind) 10799 << (AtomicKind == OMPC_unknown ? 1 : 0) 10800 << getOpenMPClauseName(MemOrderKind); 10801 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 10802 << getOpenMPClauseName(MemOrderKind); 10803 } 10804 10805 Stmt *Body = AStmt; 10806 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 10807 Body = EWC->getSubExpr(); 10808 10809 Expr *X = nullptr; 10810 Expr *V = nullptr; 10811 Expr *E = nullptr; 10812 Expr *UE = nullptr; 10813 bool IsXLHSInRHSPart = false; 10814 bool IsPostfixUpdate = false; 10815 // OpenMP [2.12.6, atomic Construct] 10816 // In the next expressions: 10817 // * x and v (as applicable) are both l-value expressions with scalar type. 10818 // * During the execution of an atomic region, multiple syntactic 10819 // occurrences of x must designate the same storage location. 10820 // * Neither of v and expr (as applicable) may access the storage location 10821 // designated by x. 10822 // * Neither of x and expr (as applicable) may access the storage location 10823 // designated by v. 10824 // * expr is an expression with scalar type. 10825 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 10826 // * binop, binop=, ++, and -- are not overloaded operators. 10827 // * The expression x binop expr must be numerically equivalent to x binop 10828 // (expr). This requirement is satisfied if the operators in expr have 10829 // precedence greater than binop, or by using parentheses around expr or 10830 // subexpressions of expr. 10831 // * The expression expr binop x must be numerically equivalent to (expr) 10832 // binop x. This requirement is satisfied if the operators in expr have 10833 // precedence equal to or greater than binop, or by using parentheses around 10834 // expr or subexpressions of expr. 10835 // * For forms that allow multiple occurrences of x, the number of times 10836 // that x is evaluated is unspecified. 10837 if (AtomicKind == OMPC_read) { 10838 enum { 10839 NotAnExpression, 10840 NotAnAssignmentOp, 10841 NotAScalarType, 10842 NotAnLValue, 10843 NoError 10844 } ErrorFound = NoError; 10845 SourceLocation ErrorLoc, NoteLoc; 10846 SourceRange ErrorRange, NoteRange; 10847 // If clause is read: 10848 // v = x; 10849 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10850 const auto *AtomicBinOp = 10851 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10852 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10853 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 10854 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 10855 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10856 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 10857 if (!X->isLValue() || !V->isLValue()) { 10858 const Expr *NotLValueExpr = X->isLValue() ? V : X; 10859 ErrorFound = NotAnLValue; 10860 ErrorLoc = AtomicBinOp->getExprLoc(); 10861 ErrorRange = AtomicBinOp->getSourceRange(); 10862 NoteLoc = NotLValueExpr->getExprLoc(); 10863 NoteRange = NotLValueExpr->getSourceRange(); 10864 } 10865 } else if (!X->isInstantiationDependent() || 10866 !V->isInstantiationDependent()) { 10867 const Expr *NotScalarExpr = 10868 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10869 ? V 10870 : X; 10871 ErrorFound = NotAScalarType; 10872 ErrorLoc = AtomicBinOp->getExprLoc(); 10873 ErrorRange = AtomicBinOp->getSourceRange(); 10874 NoteLoc = NotScalarExpr->getExprLoc(); 10875 NoteRange = NotScalarExpr->getSourceRange(); 10876 } 10877 } else if (!AtomicBody->isInstantiationDependent()) { 10878 ErrorFound = NotAnAssignmentOp; 10879 ErrorLoc = AtomicBody->getExprLoc(); 10880 ErrorRange = AtomicBody->getSourceRange(); 10881 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10882 : AtomicBody->getExprLoc(); 10883 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10884 : AtomicBody->getSourceRange(); 10885 } 10886 } else { 10887 ErrorFound = NotAnExpression; 10888 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10889 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10890 } 10891 if (ErrorFound != NoError) { 10892 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 10893 << ErrorRange; 10894 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10895 << NoteRange; 10896 return StmtError(); 10897 } 10898 if (CurContext->isDependentContext()) 10899 V = X = nullptr; 10900 } else if (AtomicKind == OMPC_write) { 10901 enum { 10902 NotAnExpression, 10903 NotAnAssignmentOp, 10904 NotAScalarType, 10905 NotAnLValue, 10906 NoError 10907 } ErrorFound = NoError; 10908 SourceLocation ErrorLoc, NoteLoc; 10909 SourceRange ErrorRange, NoteRange; 10910 // If clause is write: 10911 // x = expr; 10912 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10913 const auto *AtomicBinOp = 10914 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 10915 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 10916 X = AtomicBinOp->getLHS(); 10917 E = AtomicBinOp->getRHS(); 10918 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 10919 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 10920 if (!X->isLValue()) { 10921 ErrorFound = NotAnLValue; 10922 ErrorLoc = AtomicBinOp->getExprLoc(); 10923 ErrorRange = AtomicBinOp->getSourceRange(); 10924 NoteLoc = X->getExprLoc(); 10925 NoteRange = X->getSourceRange(); 10926 } 10927 } else if (!X->isInstantiationDependent() || 10928 !E->isInstantiationDependent()) { 10929 const Expr *NotScalarExpr = 10930 (X->isInstantiationDependent() || X->getType()->isScalarType()) 10931 ? E 10932 : X; 10933 ErrorFound = NotAScalarType; 10934 ErrorLoc = AtomicBinOp->getExprLoc(); 10935 ErrorRange = AtomicBinOp->getSourceRange(); 10936 NoteLoc = NotScalarExpr->getExprLoc(); 10937 NoteRange = NotScalarExpr->getSourceRange(); 10938 } 10939 } else if (!AtomicBody->isInstantiationDependent()) { 10940 ErrorFound = NotAnAssignmentOp; 10941 ErrorLoc = AtomicBody->getExprLoc(); 10942 ErrorRange = AtomicBody->getSourceRange(); 10943 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 10944 : AtomicBody->getExprLoc(); 10945 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 10946 : AtomicBody->getSourceRange(); 10947 } 10948 } else { 10949 ErrorFound = NotAnExpression; 10950 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10951 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 10952 } 10953 if (ErrorFound != NoError) { 10954 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 10955 << ErrorRange; 10956 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 10957 << NoteRange; 10958 return StmtError(); 10959 } 10960 if (CurContext->isDependentContext()) 10961 E = X = nullptr; 10962 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 10963 // If clause is update: 10964 // x++; 10965 // x--; 10966 // ++x; 10967 // --x; 10968 // x binop= expr; 10969 // x = x binop expr; 10970 // x = expr binop x; 10971 OpenMPAtomicUpdateChecker Checker(*this); 10972 if (Checker.checkStatement( 10973 Body, (AtomicKind == OMPC_update) 10974 ? diag::err_omp_atomic_update_not_expression_statement 10975 : diag::err_omp_atomic_not_expression_statement, 10976 diag::note_omp_atomic_update)) 10977 return StmtError(); 10978 if (!CurContext->isDependentContext()) { 10979 E = Checker.getExpr(); 10980 X = Checker.getX(); 10981 UE = Checker.getUpdateExpr(); 10982 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10983 } 10984 } else if (AtomicKind == OMPC_capture) { 10985 enum { 10986 NotAnAssignmentOp, 10987 NotACompoundStatement, 10988 NotTwoSubstatements, 10989 NotASpecificExpression, 10990 NoError 10991 } ErrorFound = NoError; 10992 SourceLocation ErrorLoc, NoteLoc; 10993 SourceRange ErrorRange, NoteRange; 10994 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 10995 // If clause is a capture: 10996 // v = x++; 10997 // v = x--; 10998 // v = ++x; 10999 // v = --x; 11000 // v = x binop= expr; 11001 // v = x = x binop expr; 11002 // v = x = expr binop x; 11003 const auto *AtomicBinOp = 11004 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 11005 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 11006 V = AtomicBinOp->getLHS(); 11007 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 11008 OpenMPAtomicUpdateChecker Checker(*this); 11009 if (Checker.checkStatement( 11010 Body, diag::err_omp_atomic_capture_not_expression_statement, 11011 diag::note_omp_atomic_update)) 11012 return StmtError(); 11013 E = Checker.getExpr(); 11014 X = Checker.getX(); 11015 UE = Checker.getUpdateExpr(); 11016 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11017 IsPostfixUpdate = Checker.isPostfixUpdate(); 11018 } else if (!AtomicBody->isInstantiationDependent()) { 11019 ErrorLoc = AtomicBody->getExprLoc(); 11020 ErrorRange = AtomicBody->getSourceRange(); 11021 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 11022 : AtomicBody->getExprLoc(); 11023 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 11024 : AtomicBody->getSourceRange(); 11025 ErrorFound = NotAnAssignmentOp; 11026 } 11027 if (ErrorFound != NoError) { 11028 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 11029 << ErrorRange; 11030 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11031 return StmtError(); 11032 } 11033 if (CurContext->isDependentContext()) 11034 UE = V = E = X = nullptr; 11035 } else { 11036 // If clause is a capture: 11037 // { v = x; x = expr; } 11038 // { v = x; x++; } 11039 // { v = x; x--; } 11040 // { v = x; ++x; } 11041 // { v = x; --x; } 11042 // { v = x; x binop= expr; } 11043 // { v = x; x = x binop expr; } 11044 // { v = x; x = expr binop x; } 11045 // { x++; v = x; } 11046 // { x--; v = x; } 11047 // { ++x; v = x; } 11048 // { --x; v = x; } 11049 // { x binop= expr; v = x; } 11050 // { x = x binop expr; v = x; } 11051 // { x = expr binop x; v = x; } 11052 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 11053 // Check that this is { expr1; expr2; } 11054 if (CS->size() == 2) { 11055 Stmt *First = CS->body_front(); 11056 Stmt *Second = CS->body_back(); 11057 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 11058 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 11059 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 11060 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 11061 // Need to find what subexpression is 'v' and what is 'x'. 11062 OpenMPAtomicUpdateChecker Checker(*this); 11063 bool IsUpdateExprFound = !Checker.checkStatement(Second); 11064 BinaryOperator *BinOp = nullptr; 11065 if (IsUpdateExprFound) { 11066 BinOp = dyn_cast<BinaryOperator>(First); 11067 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11068 } 11069 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11070 // { v = x; x++; } 11071 // { v = x; x--; } 11072 // { v = x; ++x; } 11073 // { v = x; --x; } 11074 // { v = x; x binop= expr; } 11075 // { v = x; x = x binop expr; } 11076 // { v = x; x = expr binop x; } 11077 // Check that the first expression has form v = x. 11078 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11079 llvm::FoldingSetNodeID XId, PossibleXId; 11080 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11081 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11082 IsUpdateExprFound = XId == PossibleXId; 11083 if (IsUpdateExprFound) { 11084 V = BinOp->getLHS(); 11085 X = Checker.getX(); 11086 E = Checker.getExpr(); 11087 UE = Checker.getUpdateExpr(); 11088 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11089 IsPostfixUpdate = true; 11090 } 11091 } 11092 if (!IsUpdateExprFound) { 11093 IsUpdateExprFound = !Checker.checkStatement(First); 11094 BinOp = nullptr; 11095 if (IsUpdateExprFound) { 11096 BinOp = dyn_cast<BinaryOperator>(Second); 11097 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 11098 } 11099 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 11100 // { x++; v = x; } 11101 // { x--; v = x; } 11102 // { ++x; v = x; } 11103 // { --x; v = x; } 11104 // { x binop= expr; v = x; } 11105 // { x = x binop expr; v = x; } 11106 // { x = expr binop x; v = x; } 11107 // Check that the second expression has form v = x. 11108 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 11109 llvm::FoldingSetNodeID XId, PossibleXId; 11110 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 11111 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 11112 IsUpdateExprFound = XId == PossibleXId; 11113 if (IsUpdateExprFound) { 11114 V = BinOp->getLHS(); 11115 X = Checker.getX(); 11116 E = Checker.getExpr(); 11117 UE = Checker.getUpdateExpr(); 11118 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 11119 IsPostfixUpdate = false; 11120 } 11121 } 11122 } 11123 if (!IsUpdateExprFound) { 11124 // { v = x; x = expr; } 11125 auto *FirstExpr = dyn_cast<Expr>(First); 11126 auto *SecondExpr = dyn_cast<Expr>(Second); 11127 if (!FirstExpr || !SecondExpr || 11128 !(FirstExpr->isInstantiationDependent() || 11129 SecondExpr->isInstantiationDependent())) { 11130 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 11131 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 11132 ErrorFound = NotAnAssignmentOp; 11133 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 11134 : First->getBeginLoc(); 11135 NoteRange = ErrorRange = FirstBinOp 11136 ? FirstBinOp->getSourceRange() 11137 : SourceRange(ErrorLoc, ErrorLoc); 11138 } else { 11139 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 11140 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 11141 ErrorFound = NotAnAssignmentOp; 11142 NoteLoc = ErrorLoc = SecondBinOp 11143 ? SecondBinOp->getOperatorLoc() 11144 : Second->getBeginLoc(); 11145 NoteRange = ErrorRange = 11146 SecondBinOp ? SecondBinOp->getSourceRange() 11147 : SourceRange(ErrorLoc, ErrorLoc); 11148 } else { 11149 Expr *PossibleXRHSInFirst = 11150 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 11151 Expr *PossibleXLHSInSecond = 11152 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 11153 llvm::FoldingSetNodeID X1Id, X2Id; 11154 PossibleXRHSInFirst->Profile(X1Id, Context, 11155 /*Canonical=*/true); 11156 PossibleXLHSInSecond->Profile(X2Id, Context, 11157 /*Canonical=*/true); 11158 IsUpdateExprFound = X1Id == X2Id; 11159 if (IsUpdateExprFound) { 11160 V = FirstBinOp->getLHS(); 11161 X = SecondBinOp->getLHS(); 11162 E = SecondBinOp->getRHS(); 11163 UE = nullptr; 11164 IsXLHSInRHSPart = false; 11165 IsPostfixUpdate = true; 11166 } else { 11167 ErrorFound = NotASpecificExpression; 11168 ErrorLoc = FirstBinOp->getExprLoc(); 11169 ErrorRange = FirstBinOp->getSourceRange(); 11170 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 11171 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 11172 } 11173 } 11174 } 11175 } 11176 } 11177 } else { 11178 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11179 NoteRange = ErrorRange = 11180 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11181 ErrorFound = NotTwoSubstatements; 11182 } 11183 } else { 11184 NoteLoc = ErrorLoc = Body->getBeginLoc(); 11185 NoteRange = ErrorRange = 11186 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 11187 ErrorFound = NotACompoundStatement; 11188 } 11189 if (ErrorFound != NoError) { 11190 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 11191 << ErrorRange; 11192 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 11193 return StmtError(); 11194 } 11195 if (CurContext->isDependentContext()) 11196 UE = V = E = X = nullptr; 11197 } 11198 } 11199 11200 setFunctionHasBranchProtectedScope(); 11201 11202 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 11203 X, V, E, UE, IsXLHSInRHSPart, 11204 IsPostfixUpdate); 11205 } 11206 11207 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 11208 Stmt *AStmt, 11209 SourceLocation StartLoc, 11210 SourceLocation EndLoc) { 11211 if (!AStmt) 11212 return StmtError(); 11213 11214 auto *CS = cast<CapturedStmt>(AStmt); 11215 // 1.2.2 OpenMP Language Terminology 11216 // Structured block - An executable statement with a single entry at the 11217 // top and a single exit at the bottom. 11218 // The point of exit cannot be a branch out of the structured block. 11219 // longjmp() and throw() must not violate the entry/exit criteria. 11220 CS->getCapturedDecl()->setNothrow(); 11221 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 11222 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11223 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11224 // 1.2.2 OpenMP Language Terminology 11225 // Structured block - An executable statement with a single entry at the 11226 // top and a single exit at the bottom. 11227 // The point of exit cannot be a branch out of the structured block. 11228 // longjmp() and throw() must not violate the entry/exit criteria. 11229 CS->getCapturedDecl()->setNothrow(); 11230 } 11231 11232 // OpenMP [2.16, Nesting of Regions] 11233 // If specified, a teams construct must be contained within a target 11234 // construct. That target construct must contain no statements or directives 11235 // outside of the teams construct. 11236 if (DSAStack->hasInnerTeamsRegion()) { 11237 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 11238 bool OMPTeamsFound = true; 11239 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 11240 auto I = CS->body_begin(); 11241 while (I != CS->body_end()) { 11242 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 11243 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 11244 OMPTeamsFound) { 11245 11246 OMPTeamsFound = false; 11247 break; 11248 } 11249 ++I; 11250 } 11251 assert(I != CS->body_end() && "Not found statement"); 11252 S = *I; 11253 } else { 11254 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 11255 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 11256 } 11257 if (!OMPTeamsFound) { 11258 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 11259 Diag(DSAStack->getInnerTeamsRegionLoc(), 11260 diag::note_omp_nested_teams_construct_here); 11261 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 11262 << isa<OMPExecutableDirective>(S); 11263 return StmtError(); 11264 } 11265 } 11266 11267 setFunctionHasBranchProtectedScope(); 11268 11269 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11270 } 11271 11272 StmtResult 11273 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 11274 Stmt *AStmt, SourceLocation StartLoc, 11275 SourceLocation EndLoc) { 11276 if (!AStmt) 11277 return StmtError(); 11278 11279 auto *CS = cast<CapturedStmt>(AStmt); 11280 // 1.2.2 OpenMP Language Terminology 11281 // Structured block - An executable statement with a single entry at the 11282 // top and a single exit at the bottom. 11283 // The point of exit cannot be a branch out of the structured block. 11284 // longjmp() and throw() must not violate the entry/exit criteria. 11285 CS->getCapturedDecl()->setNothrow(); 11286 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 11287 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11288 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 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 } 11296 11297 setFunctionHasBranchProtectedScope(); 11298 11299 return OMPTargetParallelDirective::Create( 11300 Context, StartLoc, EndLoc, Clauses, AStmt, 11301 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11302 } 11303 11304 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 11305 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11306 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11307 if (!AStmt) 11308 return StmtError(); 11309 11310 auto *CS = cast<CapturedStmt>(AStmt); 11311 // 1.2.2 OpenMP Language Terminology 11312 // Structured block - An executable statement with a single entry at the 11313 // top and a single exit at the bottom. 11314 // The point of exit cannot be a branch out of the structured block. 11315 // longjmp() and throw() must not violate the entry/exit criteria. 11316 CS->getCapturedDecl()->setNothrow(); 11317 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11318 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11319 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11320 // 1.2.2 OpenMP Language Terminology 11321 // Structured block - An executable statement with a single entry at the 11322 // top and a single exit at the bottom. 11323 // The point of exit cannot be a branch out of the structured block. 11324 // longjmp() and throw() must not violate the entry/exit criteria. 11325 CS->getCapturedDecl()->setNothrow(); 11326 } 11327 11328 OMPLoopBasedDirective::HelperExprs B; 11329 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11330 // define the nested loops number. 11331 unsigned NestedLoopCount = 11332 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 11333 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11334 VarsWithImplicitDSA, B); 11335 if (NestedLoopCount == 0) 11336 return StmtError(); 11337 11338 assert((CurContext->isDependentContext() || B.builtAll()) && 11339 "omp target parallel for loop exprs were not built"); 11340 11341 if (!CurContext->isDependentContext()) { 11342 // Finalize the clauses that need pre-built expressions for CodeGen. 11343 for (OMPClause *C : Clauses) { 11344 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11345 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11346 B.NumIterations, *this, CurScope, 11347 DSAStack)) 11348 return StmtError(); 11349 } 11350 } 11351 11352 setFunctionHasBranchProtectedScope(); 11353 return OMPTargetParallelForDirective::Create( 11354 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11355 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11356 } 11357 11358 /// Check for existence of a map clause in the list of clauses. 11359 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 11360 const OpenMPClauseKind K) { 11361 return llvm::any_of( 11362 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 11363 } 11364 11365 template <typename... Params> 11366 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 11367 const Params... ClauseTypes) { 11368 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 11369 } 11370 11371 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 11372 Stmt *AStmt, 11373 SourceLocation StartLoc, 11374 SourceLocation EndLoc) { 11375 if (!AStmt) 11376 return StmtError(); 11377 11378 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11379 11380 // OpenMP [2.12.2, target data Construct, Restrictions] 11381 // At least one map, use_device_addr or use_device_ptr clause must appear on 11382 // the directive. 11383 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 11384 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 11385 StringRef Expected; 11386 if (LangOpts.OpenMP < 50) 11387 Expected = "'map' or 'use_device_ptr'"; 11388 else 11389 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 11390 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11391 << Expected << getOpenMPDirectiveName(OMPD_target_data); 11392 return StmtError(); 11393 } 11394 11395 setFunctionHasBranchProtectedScope(); 11396 11397 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11398 AStmt); 11399 } 11400 11401 StmtResult 11402 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 11403 SourceLocation StartLoc, 11404 SourceLocation EndLoc, Stmt *AStmt) { 11405 if (!AStmt) 11406 return StmtError(); 11407 11408 auto *CS = cast<CapturedStmt>(AStmt); 11409 // 1.2.2 OpenMP Language Terminology 11410 // Structured block - An executable statement with a single entry at the 11411 // top and a single exit at the bottom. 11412 // The point of exit cannot be a branch out of the structured block. 11413 // longjmp() and throw() must not violate the entry/exit criteria. 11414 CS->getCapturedDecl()->setNothrow(); 11415 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 11416 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11417 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11418 // 1.2.2 OpenMP Language Terminology 11419 // Structured block - An executable statement with a single entry at the 11420 // top and a single exit at the bottom. 11421 // The point of exit cannot be a branch out of the structured block. 11422 // longjmp() and throw() must not violate the entry/exit criteria. 11423 CS->getCapturedDecl()->setNothrow(); 11424 } 11425 11426 // OpenMP [2.10.2, Restrictions, p. 99] 11427 // At least one map clause must appear on the directive. 11428 if (!hasClauses(Clauses, OMPC_map)) { 11429 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11430 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 11431 return StmtError(); 11432 } 11433 11434 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11435 AStmt); 11436 } 11437 11438 StmtResult 11439 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 11440 SourceLocation StartLoc, 11441 SourceLocation EndLoc, Stmt *AStmt) { 11442 if (!AStmt) 11443 return StmtError(); 11444 11445 auto *CS = cast<CapturedStmt>(AStmt); 11446 // 1.2.2 OpenMP Language Terminology 11447 // Structured block - An executable statement with a single entry at the 11448 // top and a single exit at the bottom. 11449 // The point of exit cannot be a branch out of the structured block. 11450 // longjmp() and throw() must not violate the entry/exit criteria. 11451 CS->getCapturedDecl()->setNothrow(); 11452 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 11453 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11454 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11455 // 1.2.2 OpenMP Language Terminology 11456 // Structured block - An executable statement with a single entry at the 11457 // top and a single exit at the bottom. 11458 // The point of exit cannot be a branch out of the structured block. 11459 // longjmp() and throw() must not violate the entry/exit criteria. 11460 CS->getCapturedDecl()->setNothrow(); 11461 } 11462 11463 // OpenMP [2.10.3, Restrictions, p. 102] 11464 // At least one map clause must appear on the directive. 11465 if (!hasClauses(Clauses, OMPC_map)) { 11466 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 11467 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 11468 return StmtError(); 11469 } 11470 11471 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 11472 AStmt); 11473 } 11474 11475 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 11476 SourceLocation StartLoc, 11477 SourceLocation EndLoc, 11478 Stmt *AStmt) { 11479 if (!AStmt) 11480 return StmtError(); 11481 11482 auto *CS = cast<CapturedStmt>(AStmt); 11483 // 1.2.2 OpenMP Language Terminology 11484 // Structured block - An executable statement with a single entry at the 11485 // top and a single exit at the bottom. 11486 // The point of exit cannot be a branch out of the structured block. 11487 // longjmp() and throw() must not violate the entry/exit criteria. 11488 CS->getCapturedDecl()->setNothrow(); 11489 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 11490 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11491 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11492 // 1.2.2 OpenMP Language Terminology 11493 // Structured block - An executable statement with a single entry at the 11494 // top and a single exit at the bottom. 11495 // The point of exit cannot be a branch out of the structured block. 11496 // longjmp() and throw() must not violate the entry/exit criteria. 11497 CS->getCapturedDecl()->setNothrow(); 11498 } 11499 11500 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 11501 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 11502 return StmtError(); 11503 } 11504 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 11505 AStmt); 11506 } 11507 11508 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 11509 Stmt *AStmt, SourceLocation StartLoc, 11510 SourceLocation EndLoc) { 11511 if (!AStmt) 11512 return StmtError(); 11513 11514 auto *CS = cast<CapturedStmt>(AStmt); 11515 // 1.2.2 OpenMP Language Terminology 11516 // Structured block - An executable statement with a single entry at the 11517 // top and a single exit at the bottom. 11518 // The point of exit cannot be a branch out of the structured block. 11519 // longjmp() and throw() must not violate the entry/exit criteria. 11520 CS->getCapturedDecl()->setNothrow(); 11521 11522 setFunctionHasBranchProtectedScope(); 11523 11524 DSAStack->setParentTeamsRegionLoc(StartLoc); 11525 11526 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 11527 } 11528 11529 StmtResult 11530 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 11531 SourceLocation EndLoc, 11532 OpenMPDirectiveKind CancelRegion) { 11533 if (DSAStack->isParentNowaitRegion()) { 11534 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 11535 return StmtError(); 11536 } 11537 if (DSAStack->isParentOrderedRegion()) { 11538 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 11539 return StmtError(); 11540 } 11541 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 11542 CancelRegion); 11543 } 11544 11545 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 11546 SourceLocation StartLoc, 11547 SourceLocation EndLoc, 11548 OpenMPDirectiveKind CancelRegion) { 11549 if (DSAStack->isParentNowaitRegion()) { 11550 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 11551 return StmtError(); 11552 } 11553 if (DSAStack->isParentOrderedRegion()) { 11554 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 11555 return StmtError(); 11556 } 11557 DSAStack->setParentCancelRegion(/*Cancel=*/true); 11558 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 11559 CancelRegion); 11560 } 11561 11562 static bool checkReductionClauseWithNogroup(Sema &S, 11563 ArrayRef<OMPClause *> Clauses) { 11564 const OMPClause *ReductionClause = nullptr; 11565 const OMPClause *NogroupClause = nullptr; 11566 for (const OMPClause *C : Clauses) { 11567 if (C->getClauseKind() == OMPC_reduction) { 11568 ReductionClause = C; 11569 if (NogroupClause) 11570 break; 11571 continue; 11572 } 11573 if (C->getClauseKind() == OMPC_nogroup) { 11574 NogroupClause = C; 11575 if (ReductionClause) 11576 break; 11577 continue; 11578 } 11579 } 11580 if (ReductionClause && NogroupClause) { 11581 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 11582 << SourceRange(NogroupClause->getBeginLoc(), 11583 NogroupClause->getEndLoc()); 11584 return true; 11585 } 11586 return false; 11587 } 11588 11589 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 11590 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11591 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11592 if (!AStmt) 11593 return StmtError(); 11594 11595 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11596 OMPLoopBasedDirective::HelperExprs B; 11597 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11598 // define the nested loops number. 11599 unsigned NestedLoopCount = 11600 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 11601 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11602 VarsWithImplicitDSA, B); 11603 if (NestedLoopCount == 0) 11604 return StmtError(); 11605 11606 assert((CurContext->isDependentContext() || B.builtAll()) && 11607 "omp for loop exprs were not built"); 11608 11609 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11610 // The grainsize clause and num_tasks clause are mutually exclusive and may 11611 // not appear on the same taskloop directive. 11612 if (checkMutuallyExclusiveClauses(*this, Clauses, 11613 {OMPC_grainsize, OMPC_num_tasks})) 11614 return StmtError(); 11615 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11616 // If a reduction clause is present on the taskloop directive, the nogroup 11617 // clause must not be specified. 11618 if (checkReductionClauseWithNogroup(*this, Clauses)) 11619 return StmtError(); 11620 11621 setFunctionHasBranchProtectedScope(); 11622 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11623 NestedLoopCount, Clauses, AStmt, B, 11624 DSAStack->isCancelRegion()); 11625 } 11626 11627 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 11628 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11629 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11630 if (!AStmt) 11631 return StmtError(); 11632 11633 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11634 OMPLoopBasedDirective::HelperExprs B; 11635 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11636 // define the nested loops number. 11637 unsigned NestedLoopCount = 11638 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 11639 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11640 VarsWithImplicitDSA, B); 11641 if (NestedLoopCount == 0) 11642 return StmtError(); 11643 11644 assert((CurContext->isDependentContext() || B.builtAll()) && 11645 "omp for loop exprs were not built"); 11646 11647 if (!CurContext->isDependentContext()) { 11648 // Finalize the clauses that need pre-built expressions for CodeGen. 11649 for (OMPClause *C : Clauses) { 11650 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11651 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11652 B.NumIterations, *this, CurScope, 11653 DSAStack)) 11654 return StmtError(); 11655 } 11656 } 11657 11658 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11659 // The grainsize clause and num_tasks clause are mutually exclusive and may 11660 // not appear on the same taskloop directive. 11661 if (checkMutuallyExclusiveClauses(*this, Clauses, 11662 {OMPC_grainsize, OMPC_num_tasks})) 11663 return StmtError(); 11664 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11665 // If a reduction clause is present on the taskloop directive, the nogroup 11666 // clause must not be specified. 11667 if (checkReductionClauseWithNogroup(*this, Clauses)) 11668 return StmtError(); 11669 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11670 return StmtError(); 11671 11672 setFunctionHasBranchProtectedScope(); 11673 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 11674 NestedLoopCount, Clauses, AStmt, B); 11675 } 11676 11677 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 11678 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11679 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11680 if (!AStmt) 11681 return StmtError(); 11682 11683 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11684 OMPLoopBasedDirective::HelperExprs B; 11685 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11686 // define the nested loops number. 11687 unsigned NestedLoopCount = 11688 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 11689 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11690 VarsWithImplicitDSA, B); 11691 if (NestedLoopCount == 0) 11692 return StmtError(); 11693 11694 assert((CurContext->isDependentContext() || B.builtAll()) && 11695 "omp for loop exprs were not built"); 11696 11697 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11698 // The grainsize clause and num_tasks clause are mutually exclusive and may 11699 // not appear on the same taskloop directive. 11700 if (checkMutuallyExclusiveClauses(*this, Clauses, 11701 {OMPC_grainsize, OMPC_num_tasks})) 11702 return StmtError(); 11703 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11704 // If a reduction clause is present on the taskloop directive, the nogroup 11705 // clause must not be specified. 11706 if (checkReductionClauseWithNogroup(*this, Clauses)) 11707 return StmtError(); 11708 11709 setFunctionHasBranchProtectedScope(); 11710 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 11711 NestedLoopCount, Clauses, AStmt, B, 11712 DSAStack->isCancelRegion()); 11713 } 11714 11715 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 11716 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11717 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11718 if (!AStmt) 11719 return StmtError(); 11720 11721 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11722 OMPLoopBasedDirective::HelperExprs B; 11723 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11724 // define the nested loops number. 11725 unsigned NestedLoopCount = 11726 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11727 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 11728 VarsWithImplicitDSA, B); 11729 if (NestedLoopCount == 0) 11730 return StmtError(); 11731 11732 assert((CurContext->isDependentContext() || B.builtAll()) && 11733 "omp for loop exprs were not built"); 11734 11735 if (!CurContext->isDependentContext()) { 11736 // Finalize the clauses that need pre-built expressions for CodeGen. 11737 for (OMPClause *C : Clauses) { 11738 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11739 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11740 B.NumIterations, *this, CurScope, 11741 DSAStack)) 11742 return StmtError(); 11743 } 11744 } 11745 11746 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11747 // The grainsize clause and num_tasks clause are mutually exclusive and may 11748 // not appear on the same taskloop directive. 11749 if (checkMutuallyExclusiveClauses(*this, Clauses, 11750 {OMPC_grainsize, OMPC_num_tasks})) 11751 return StmtError(); 11752 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11753 // If a reduction clause is present on the taskloop directive, the nogroup 11754 // clause must not be specified. 11755 if (checkReductionClauseWithNogroup(*this, Clauses)) 11756 return StmtError(); 11757 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11758 return StmtError(); 11759 11760 setFunctionHasBranchProtectedScope(); 11761 return OMPMasterTaskLoopSimdDirective::Create( 11762 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11763 } 11764 11765 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 11766 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11767 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11768 if (!AStmt) 11769 return StmtError(); 11770 11771 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11772 auto *CS = cast<CapturedStmt>(AStmt); 11773 // 1.2.2 OpenMP Language Terminology 11774 // Structured block - An executable statement with a single entry at the 11775 // top and a single exit at the bottom. 11776 // The point of exit cannot be a branch out of the structured block. 11777 // longjmp() and throw() must not violate the entry/exit criteria. 11778 CS->getCapturedDecl()->setNothrow(); 11779 for (int ThisCaptureLevel = 11780 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 11781 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11782 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11783 // 1.2.2 OpenMP Language Terminology 11784 // Structured block - An executable statement with a single entry at the 11785 // top and a single exit at the bottom. 11786 // The point of exit cannot be a branch out of the structured block. 11787 // longjmp() and throw() must not violate the entry/exit criteria. 11788 CS->getCapturedDecl()->setNothrow(); 11789 } 11790 11791 OMPLoopBasedDirective::HelperExprs B; 11792 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11793 // define the nested loops number. 11794 unsigned NestedLoopCount = checkOpenMPLoop( 11795 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 11796 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11797 VarsWithImplicitDSA, B); 11798 if (NestedLoopCount == 0) 11799 return StmtError(); 11800 11801 assert((CurContext->isDependentContext() || B.builtAll()) && 11802 "omp for loop exprs were not built"); 11803 11804 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11805 // The grainsize clause and num_tasks clause are mutually exclusive and may 11806 // not appear on the same taskloop directive. 11807 if (checkMutuallyExclusiveClauses(*this, Clauses, 11808 {OMPC_grainsize, OMPC_num_tasks})) 11809 return StmtError(); 11810 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11811 // If a reduction clause is present on the taskloop directive, the nogroup 11812 // clause must not be specified. 11813 if (checkReductionClauseWithNogroup(*this, Clauses)) 11814 return StmtError(); 11815 11816 setFunctionHasBranchProtectedScope(); 11817 return OMPParallelMasterTaskLoopDirective::Create( 11818 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11819 DSAStack->isCancelRegion()); 11820 } 11821 11822 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 11823 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11824 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11825 if (!AStmt) 11826 return StmtError(); 11827 11828 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11829 auto *CS = cast<CapturedStmt>(AStmt); 11830 // 1.2.2 OpenMP Language Terminology 11831 // Structured block - An executable statement with a single entry at the 11832 // top and a single exit at the bottom. 11833 // The point of exit cannot be a branch out of the structured block. 11834 // longjmp() and throw() must not violate the entry/exit criteria. 11835 CS->getCapturedDecl()->setNothrow(); 11836 for (int ThisCaptureLevel = 11837 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 11838 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11839 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11840 // 1.2.2 OpenMP Language Terminology 11841 // Structured block - An executable statement with a single entry at the 11842 // top and a single exit at the bottom. 11843 // The point of exit cannot be a branch out of the structured block. 11844 // longjmp() and throw() must not violate the entry/exit criteria. 11845 CS->getCapturedDecl()->setNothrow(); 11846 } 11847 11848 OMPLoopBasedDirective::HelperExprs B; 11849 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11850 // define the nested loops number. 11851 unsigned NestedLoopCount = checkOpenMPLoop( 11852 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 11853 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 11854 VarsWithImplicitDSA, B); 11855 if (NestedLoopCount == 0) 11856 return StmtError(); 11857 11858 assert((CurContext->isDependentContext() || B.builtAll()) && 11859 "omp for loop exprs were not built"); 11860 11861 if (!CurContext->isDependentContext()) { 11862 // Finalize the clauses that need pre-built expressions for CodeGen. 11863 for (OMPClause *C : Clauses) { 11864 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11865 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11866 B.NumIterations, *this, CurScope, 11867 DSAStack)) 11868 return StmtError(); 11869 } 11870 } 11871 11872 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11873 // The grainsize clause and num_tasks clause are mutually exclusive and may 11874 // not appear on the same taskloop directive. 11875 if (checkMutuallyExclusiveClauses(*this, Clauses, 11876 {OMPC_grainsize, OMPC_num_tasks})) 11877 return StmtError(); 11878 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 11879 // If a reduction clause is present on the taskloop directive, the nogroup 11880 // clause must not be specified. 11881 if (checkReductionClauseWithNogroup(*this, Clauses)) 11882 return StmtError(); 11883 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11884 return StmtError(); 11885 11886 setFunctionHasBranchProtectedScope(); 11887 return OMPParallelMasterTaskLoopSimdDirective::Create( 11888 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11889 } 11890 11891 StmtResult Sema::ActOnOpenMPDistributeDirective( 11892 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11893 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11894 if (!AStmt) 11895 return StmtError(); 11896 11897 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 11898 OMPLoopBasedDirective::HelperExprs B; 11899 // In presence of clause 'collapse' with number of loops, it will 11900 // define the nested loops number. 11901 unsigned NestedLoopCount = 11902 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 11903 nullptr /*ordered not a clause on distribute*/, AStmt, 11904 *this, *DSAStack, VarsWithImplicitDSA, B); 11905 if (NestedLoopCount == 0) 11906 return StmtError(); 11907 11908 assert((CurContext->isDependentContext() || B.builtAll()) && 11909 "omp for loop exprs were not built"); 11910 11911 setFunctionHasBranchProtectedScope(); 11912 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 11913 NestedLoopCount, Clauses, AStmt, B); 11914 } 11915 11916 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 11917 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11918 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11919 if (!AStmt) 11920 return StmtError(); 11921 11922 auto *CS = cast<CapturedStmt>(AStmt); 11923 // 1.2.2 OpenMP Language Terminology 11924 // Structured block - An executable statement with a single entry at the 11925 // top and a single exit at the bottom. 11926 // The point of exit cannot be a branch out of the structured block. 11927 // longjmp() and throw() must not violate the entry/exit criteria. 11928 CS->getCapturedDecl()->setNothrow(); 11929 for (int ThisCaptureLevel = 11930 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 11931 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11932 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11933 // 1.2.2 OpenMP Language Terminology 11934 // Structured block - An executable statement with a single entry at the 11935 // top and a single exit at the bottom. 11936 // The point of exit cannot be a branch out of the structured block. 11937 // longjmp() and throw() must not violate the entry/exit criteria. 11938 CS->getCapturedDecl()->setNothrow(); 11939 } 11940 11941 OMPLoopBasedDirective::HelperExprs B; 11942 // In presence of clause 'collapse' with number of loops, it will 11943 // define the nested loops number. 11944 unsigned NestedLoopCount = checkOpenMPLoop( 11945 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11946 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11947 VarsWithImplicitDSA, B); 11948 if (NestedLoopCount == 0) 11949 return StmtError(); 11950 11951 assert((CurContext->isDependentContext() || B.builtAll()) && 11952 "omp for loop exprs were not built"); 11953 11954 setFunctionHasBranchProtectedScope(); 11955 return OMPDistributeParallelForDirective::Create( 11956 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11957 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11958 } 11959 11960 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 11961 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11962 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11963 if (!AStmt) 11964 return StmtError(); 11965 11966 auto *CS = cast<CapturedStmt>(AStmt); 11967 // 1.2.2 OpenMP Language Terminology 11968 // Structured block - An executable statement with a single entry at the 11969 // top and a single exit at the bottom. 11970 // The point of exit cannot be a branch out of the structured block. 11971 // longjmp() and throw() must not violate the entry/exit criteria. 11972 CS->getCapturedDecl()->setNothrow(); 11973 for (int ThisCaptureLevel = 11974 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 11975 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11976 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11977 // 1.2.2 OpenMP Language Terminology 11978 // Structured block - An executable statement with a single entry at the 11979 // top and a single exit at the bottom. 11980 // The point of exit cannot be a branch out of the structured block. 11981 // longjmp() and throw() must not violate the entry/exit criteria. 11982 CS->getCapturedDecl()->setNothrow(); 11983 } 11984 11985 OMPLoopBasedDirective::HelperExprs B; 11986 // In presence of clause 'collapse' with number of loops, it will 11987 // define the nested loops number. 11988 unsigned NestedLoopCount = checkOpenMPLoop( 11989 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11990 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11991 VarsWithImplicitDSA, B); 11992 if (NestedLoopCount == 0) 11993 return StmtError(); 11994 11995 assert((CurContext->isDependentContext() || B.builtAll()) && 11996 "omp for loop exprs were not built"); 11997 11998 if (!CurContext->isDependentContext()) { 11999 // Finalize the clauses that need pre-built expressions for CodeGen. 12000 for (OMPClause *C : Clauses) { 12001 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12002 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12003 B.NumIterations, *this, CurScope, 12004 DSAStack)) 12005 return StmtError(); 12006 } 12007 } 12008 12009 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12010 return StmtError(); 12011 12012 setFunctionHasBranchProtectedScope(); 12013 return OMPDistributeParallelForSimdDirective::Create( 12014 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12015 } 12016 12017 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 12018 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12019 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12020 if (!AStmt) 12021 return StmtError(); 12022 12023 auto *CS = cast<CapturedStmt>(AStmt); 12024 // 1.2.2 OpenMP Language Terminology 12025 // Structured block - An executable statement with a single entry at the 12026 // top and a single exit at the bottom. 12027 // The point of exit cannot be a branch out of the structured block. 12028 // longjmp() and throw() must not violate the entry/exit criteria. 12029 CS->getCapturedDecl()->setNothrow(); 12030 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 12031 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12032 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12033 // 1.2.2 OpenMP Language Terminology 12034 // Structured block - An executable statement with a single entry at the 12035 // top and a single exit at the bottom. 12036 // The point of exit cannot be a branch out of the structured block. 12037 // longjmp() and throw() must not violate the entry/exit criteria. 12038 CS->getCapturedDecl()->setNothrow(); 12039 } 12040 12041 OMPLoopBasedDirective::HelperExprs B; 12042 // In presence of clause 'collapse' with number of loops, it will 12043 // define the nested loops number. 12044 unsigned NestedLoopCount = 12045 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 12046 nullptr /*ordered not a clause on distribute*/, CS, *this, 12047 *DSAStack, VarsWithImplicitDSA, B); 12048 if (NestedLoopCount == 0) 12049 return StmtError(); 12050 12051 assert((CurContext->isDependentContext() || B.builtAll()) && 12052 "omp for loop exprs were not built"); 12053 12054 if (!CurContext->isDependentContext()) { 12055 // Finalize the clauses that need pre-built expressions for CodeGen. 12056 for (OMPClause *C : Clauses) { 12057 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12058 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12059 B.NumIterations, *this, CurScope, 12060 DSAStack)) 12061 return StmtError(); 12062 } 12063 } 12064 12065 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12066 return StmtError(); 12067 12068 setFunctionHasBranchProtectedScope(); 12069 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 12070 NestedLoopCount, Clauses, AStmt, B); 12071 } 12072 12073 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 12074 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12075 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12076 if (!AStmt) 12077 return StmtError(); 12078 12079 auto *CS = cast<CapturedStmt>(AStmt); 12080 // 1.2.2 OpenMP Language Terminology 12081 // Structured block - An executable statement with a single entry at the 12082 // top and a single exit at the bottom. 12083 // The point of exit cannot be a branch out of the structured block. 12084 // longjmp() and throw() must not violate the entry/exit criteria. 12085 CS->getCapturedDecl()->setNothrow(); 12086 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 12087 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12088 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12089 // 1.2.2 OpenMP Language Terminology 12090 // Structured block - An executable statement with a single entry at the 12091 // top and a single exit at the bottom. 12092 // The point of exit cannot be a branch out of the structured block. 12093 // longjmp() and throw() must not violate the entry/exit criteria. 12094 CS->getCapturedDecl()->setNothrow(); 12095 } 12096 12097 OMPLoopBasedDirective::HelperExprs B; 12098 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 12099 // define the nested loops number. 12100 unsigned NestedLoopCount = checkOpenMPLoop( 12101 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 12102 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12103 VarsWithImplicitDSA, B); 12104 if (NestedLoopCount == 0) 12105 return StmtError(); 12106 12107 assert((CurContext->isDependentContext() || B.builtAll()) && 12108 "omp target parallel for simd loop exprs were not built"); 12109 12110 if (!CurContext->isDependentContext()) { 12111 // Finalize the clauses that need pre-built expressions for CodeGen. 12112 for (OMPClause *C : Clauses) { 12113 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12114 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12115 B.NumIterations, *this, CurScope, 12116 DSAStack)) 12117 return StmtError(); 12118 } 12119 } 12120 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12121 return StmtError(); 12122 12123 setFunctionHasBranchProtectedScope(); 12124 return OMPTargetParallelForSimdDirective::Create( 12125 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12126 } 12127 12128 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 12129 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12130 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12131 if (!AStmt) 12132 return StmtError(); 12133 12134 auto *CS = cast<CapturedStmt>(AStmt); 12135 // 1.2.2 OpenMP Language Terminology 12136 // Structured block - An executable statement with a single entry at the 12137 // top and a single exit at the bottom. 12138 // The point of exit cannot be a branch out of the structured block. 12139 // longjmp() and throw() must not violate the entry/exit criteria. 12140 CS->getCapturedDecl()->setNothrow(); 12141 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 12142 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12143 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12144 // 1.2.2 OpenMP Language Terminology 12145 // Structured block - An executable statement with a single entry at the 12146 // top and a single exit at the bottom. 12147 // The point of exit cannot be a branch out of the structured block. 12148 // longjmp() and throw() must not violate the entry/exit criteria. 12149 CS->getCapturedDecl()->setNothrow(); 12150 } 12151 12152 OMPLoopBasedDirective::HelperExprs B; 12153 // In presence of clause 'collapse' with number of loops, it will define the 12154 // nested loops number. 12155 unsigned NestedLoopCount = 12156 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 12157 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 12158 VarsWithImplicitDSA, B); 12159 if (NestedLoopCount == 0) 12160 return StmtError(); 12161 12162 assert((CurContext->isDependentContext() || B.builtAll()) && 12163 "omp target simd loop exprs were not built"); 12164 12165 if (!CurContext->isDependentContext()) { 12166 // Finalize the clauses that need pre-built expressions for CodeGen. 12167 for (OMPClause *C : Clauses) { 12168 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12169 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12170 B.NumIterations, *this, CurScope, 12171 DSAStack)) 12172 return StmtError(); 12173 } 12174 } 12175 12176 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12177 return StmtError(); 12178 12179 setFunctionHasBranchProtectedScope(); 12180 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 12181 NestedLoopCount, Clauses, AStmt, B); 12182 } 12183 12184 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 12185 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12186 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12187 if (!AStmt) 12188 return StmtError(); 12189 12190 auto *CS = cast<CapturedStmt>(AStmt); 12191 // 1.2.2 OpenMP Language Terminology 12192 // Structured block - An executable statement with a single entry at the 12193 // top and a single exit at the bottom. 12194 // The point of exit cannot be a branch out of the structured block. 12195 // longjmp() and throw() must not violate the entry/exit criteria. 12196 CS->getCapturedDecl()->setNothrow(); 12197 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 12198 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12199 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12200 // 1.2.2 OpenMP Language Terminology 12201 // Structured block - An executable statement with a single entry at the 12202 // top and a single exit at the bottom. 12203 // The point of exit cannot be a branch out of the structured block. 12204 // longjmp() and throw() must not violate the entry/exit criteria. 12205 CS->getCapturedDecl()->setNothrow(); 12206 } 12207 12208 OMPLoopBasedDirective::HelperExprs B; 12209 // In presence of clause 'collapse' with number of loops, it will 12210 // define the nested loops number. 12211 unsigned NestedLoopCount = 12212 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 12213 nullptr /*ordered not a clause on distribute*/, CS, *this, 12214 *DSAStack, VarsWithImplicitDSA, B); 12215 if (NestedLoopCount == 0) 12216 return StmtError(); 12217 12218 assert((CurContext->isDependentContext() || B.builtAll()) && 12219 "omp teams distribute loop exprs were not built"); 12220 12221 setFunctionHasBranchProtectedScope(); 12222 12223 DSAStack->setParentTeamsRegionLoc(StartLoc); 12224 12225 return OMPTeamsDistributeDirective::Create( 12226 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12227 } 12228 12229 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 12230 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12231 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12232 if (!AStmt) 12233 return StmtError(); 12234 12235 auto *CS = cast<CapturedStmt>(AStmt); 12236 // 1.2.2 OpenMP Language Terminology 12237 // Structured block - An executable statement with a single entry at the 12238 // top and a single exit at the bottom. 12239 // The point of exit cannot be a branch out of the structured block. 12240 // longjmp() and throw() must not violate the entry/exit criteria. 12241 CS->getCapturedDecl()->setNothrow(); 12242 for (int ThisCaptureLevel = 12243 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 12244 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12245 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12246 // 1.2.2 OpenMP Language Terminology 12247 // Structured block - An executable statement with a single entry at the 12248 // top and a single exit at the bottom. 12249 // The point of exit cannot be a branch out of the structured block. 12250 // longjmp() and throw() must not violate the entry/exit criteria. 12251 CS->getCapturedDecl()->setNothrow(); 12252 } 12253 12254 OMPLoopBasedDirective::HelperExprs B; 12255 // In presence of clause 'collapse' with number of loops, it will 12256 // define the nested loops number. 12257 unsigned NestedLoopCount = checkOpenMPLoop( 12258 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12259 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12260 VarsWithImplicitDSA, B); 12261 12262 if (NestedLoopCount == 0) 12263 return StmtError(); 12264 12265 assert((CurContext->isDependentContext() || B.builtAll()) && 12266 "omp teams distribute simd loop exprs were not built"); 12267 12268 if (!CurContext->isDependentContext()) { 12269 // Finalize the clauses that need pre-built expressions for CodeGen. 12270 for (OMPClause *C : Clauses) { 12271 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12272 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12273 B.NumIterations, *this, CurScope, 12274 DSAStack)) 12275 return StmtError(); 12276 } 12277 } 12278 12279 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12280 return StmtError(); 12281 12282 setFunctionHasBranchProtectedScope(); 12283 12284 DSAStack->setParentTeamsRegionLoc(StartLoc); 12285 12286 return OMPTeamsDistributeSimdDirective::Create( 12287 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12288 } 12289 12290 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 12291 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12292 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12293 if (!AStmt) 12294 return StmtError(); 12295 12296 auto *CS = cast<CapturedStmt>(AStmt); 12297 // 1.2.2 OpenMP Language Terminology 12298 // Structured block - An executable statement with a single entry at the 12299 // top and a single exit at the bottom. 12300 // The point of exit cannot be a branch out of the structured block. 12301 // longjmp() and throw() must not violate the entry/exit criteria. 12302 CS->getCapturedDecl()->setNothrow(); 12303 12304 for (int ThisCaptureLevel = 12305 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 12306 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12307 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12308 // 1.2.2 OpenMP Language Terminology 12309 // Structured block - An executable statement with a single entry at the 12310 // top and a single exit at the bottom. 12311 // The point of exit cannot be a branch out of the structured block. 12312 // longjmp() and throw() must not violate the entry/exit criteria. 12313 CS->getCapturedDecl()->setNothrow(); 12314 } 12315 12316 OMPLoopBasedDirective::HelperExprs B; 12317 // In presence of clause 'collapse' with number of loops, it will 12318 // define the nested loops number. 12319 unsigned NestedLoopCount = checkOpenMPLoop( 12320 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 12321 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12322 VarsWithImplicitDSA, B); 12323 12324 if (NestedLoopCount == 0) 12325 return StmtError(); 12326 12327 assert((CurContext->isDependentContext() || B.builtAll()) && 12328 "omp for loop exprs were not built"); 12329 12330 if (!CurContext->isDependentContext()) { 12331 // Finalize the clauses that need pre-built expressions for CodeGen. 12332 for (OMPClause *C : Clauses) { 12333 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12334 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12335 B.NumIterations, *this, CurScope, 12336 DSAStack)) 12337 return StmtError(); 12338 } 12339 } 12340 12341 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12342 return StmtError(); 12343 12344 setFunctionHasBranchProtectedScope(); 12345 12346 DSAStack->setParentTeamsRegionLoc(StartLoc); 12347 12348 return OMPTeamsDistributeParallelForSimdDirective::Create( 12349 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12350 } 12351 12352 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 12353 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12354 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12355 if (!AStmt) 12356 return StmtError(); 12357 12358 auto *CS = cast<CapturedStmt>(AStmt); 12359 // 1.2.2 OpenMP Language Terminology 12360 // Structured block - An executable statement with a single entry at the 12361 // top and a single exit at the bottom. 12362 // The point of exit cannot be a branch out of the structured block. 12363 // longjmp() and throw() must not violate the entry/exit criteria. 12364 CS->getCapturedDecl()->setNothrow(); 12365 12366 for (int ThisCaptureLevel = 12367 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 12368 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12369 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12370 // 1.2.2 OpenMP Language Terminology 12371 // Structured block - An executable statement with a single entry at the 12372 // top and a single exit at the bottom. 12373 // The point of exit cannot be a branch out of the structured block. 12374 // longjmp() and throw() must not violate the entry/exit criteria. 12375 CS->getCapturedDecl()->setNothrow(); 12376 } 12377 12378 OMPLoopBasedDirective::HelperExprs B; 12379 // In presence of clause 'collapse' with number of loops, it will 12380 // define the nested loops number. 12381 unsigned NestedLoopCount = checkOpenMPLoop( 12382 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12383 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12384 VarsWithImplicitDSA, B); 12385 12386 if (NestedLoopCount == 0) 12387 return StmtError(); 12388 12389 assert((CurContext->isDependentContext() || B.builtAll()) && 12390 "omp for loop exprs were not built"); 12391 12392 setFunctionHasBranchProtectedScope(); 12393 12394 DSAStack->setParentTeamsRegionLoc(StartLoc); 12395 12396 return OMPTeamsDistributeParallelForDirective::Create( 12397 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12398 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12399 } 12400 12401 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 12402 Stmt *AStmt, 12403 SourceLocation StartLoc, 12404 SourceLocation EndLoc) { 12405 if (!AStmt) 12406 return StmtError(); 12407 12408 auto *CS = cast<CapturedStmt>(AStmt); 12409 // 1.2.2 OpenMP Language Terminology 12410 // Structured block - An executable statement with a single entry at the 12411 // top and a single exit at the bottom. 12412 // The point of exit cannot be a branch out of the structured block. 12413 // longjmp() and throw() must not violate the entry/exit criteria. 12414 CS->getCapturedDecl()->setNothrow(); 12415 12416 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 12417 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12418 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12419 // 1.2.2 OpenMP Language Terminology 12420 // Structured block - An executable statement with a single entry at the 12421 // top and a single exit at the bottom. 12422 // The point of exit cannot be a branch out of the structured block. 12423 // longjmp() and throw() must not violate the entry/exit criteria. 12424 CS->getCapturedDecl()->setNothrow(); 12425 } 12426 setFunctionHasBranchProtectedScope(); 12427 12428 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 12429 AStmt); 12430 } 12431 12432 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 12433 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12434 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12435 if (!AStmt) 12436 return StmtError(); 12437 12438 auto *CS = cast<CapturedStmt>(AStmt); 12439 // 1.2.2 OpenMP Language Terminology 12440 // Structured block - An executable statement with a single entry at the 12441 // top and a single exit at the bottom. 12442 // The point of exit cannot be a branch out of the structured block. 12443 // longjmp() and throw() must not violate the entry/exit criteria. 12444 CS->getCapturedDecl()->setNothrow(); 12445 for (int ThisCaptureLevel = 12446 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 12447 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12448 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12449 // 1.2.2 OpenMP Language Terminology 12450 // Structured block - An executable statement with a single entry at the 12451 // top and a single exit at the bottom. 12452 // The point of exit cannot be a branch out of the structured block. 12453 // longjmp() and throw() must not violate the entry/exit criteria. 12454 CS->getCapturedDecl()->setNothrow(); 12455 } 12456 12457 OMPLoopBasedDirective::HelperExprs B; 12458 // In presence of clause 'collapse' with number of loops, it will 12459 // define the nested loops number. 12460 unsigned NestedLoopCount = checkOpenMPLoop( 12461 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 12462 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12463 VarsWithImplicitDSA, B); 12464 if (NestedLoopCount == 0) 12465 return StmtError(); 12466 12467 assert((CurContext->isDependentContext() || B.builtAll()) && 12468 "omp target teams distribute loop exprs were not built"); 12469 12470 setFunctionHasBranchProtectedScope(); 12471 return OMPTargetTeamsDistributeDirective::Create( 12472 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12473 } 12474 12475 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 12476 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12477 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12478 if (!AStmt) 12479 return StmtError(); 12480 12481 auto *CS = cast<CapturedStmt>(AStmt); 12482 // 1.2.2 OpenMP Language Terminology 12483 // Structured block - An executable statement with a single entry at the 12484 // top and a single exit at the bottom. 12485 // The point of exit cannot be a branch out of the structured block. 12486 // longjmp() and throw() must not violate the entry/exit criteria. 12487 CS->getCapturedDecl()->setNothrow(); 12488 for (int ThisCaptureLevel = 12489 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 12490 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12491 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 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 } 12499 12500 OMPLoopBasedDirective::HelperExprs B; 12501 // In presence of clause 'collapse' with number of loops, it will 12502 // define the nested loops number. 12503 unsigned NestedLoopCount = checkOpenMPLoop( 12504 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 12505 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12506 VarsWithImplicitDSA, B); 12507 if (NestedLoopCount == 0) 12508 return StmtError(); 12509 12510 assert((CurContext->isDependentContext() || B.builtAll()) && 12511 "omp target teams distribute parallel for loop exprs were not built"); 12512 12513 if (!CurContext->isDependentContext()) { 12514 // Finalize the clauses that need pre-built expressions for CodeGen. 12515 for (OMPClause *C : Clauses) { 12516 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12517 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12518 B.NumIterations, *this, CurScope, 12519 DSAStack)) 12520 return StmtError(); 12521 } 12522 } 12523 12524 setFunctionHasBranchProtectedScope(); 12525 return OMPTargetTeamsDistributeParallelForDirective::Create( 12526 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 12527 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 12528 } 12529 12530 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 12531 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12532 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12533 if (!AStmt) 12534 return StmtError(); 12535 12536 auto *CS = cast<CapturedStmt>(AStmt); 12537 // 1.2.2 OpenMP Language Terminology 12538 // Structured block - An executable statement with a single entry at the 12539 // top and a single exit at the bottom. 12540 // The point of exit cannot be a branch out of the structured block. 12541 // longjmp() and throw() must not violate the entry/exit criteria. 12542 CS->getCapturedDecl()->setNothrow(); 12543 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 12544 OMPD_target_teams_distribute_parallel_for_simd); 12545 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12546 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12547 // 1.2.2 OpenMP Language Terminology 12548 // Structured block - An executable statement with a single entry at the 12549 // top and a single exit at the bottom. 12550 // The point of exit cannot be a branch out of the structured block. 12551 // longjmp() and throw() must not violate the entry/exit criteria. 12552 CS->getCapturedDecl()->setNothrow(); 12553 } 12554 12555 OMPLoopBasedDirective::HelperExprs B; 12556 // In presence of clause 'collapse' with number of loops, it will 12557 // define the nested loops number. 12558 unsigned NestedLoopCount = 12559 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 12560 getCollapseNumberExpr(Clauses), 12561 nullptr /*ordered not a clause on distribute*/, CS, *this, 12562 *DSAStack, VarsWithImplicitDSA, B); 12563 if (NestedLoopCount == 0) 12564 return StmtError(); 12565 12566 assert((CurContext->isDependentContext() || B.builtAll()) && 12567 "omp target teams distribute parallel for simd loop exprs were not " 12568 "built"); 12569 12570 if (!CurContext->isDependentContext()) { 12571 // Finalize the clauses that need pre-built expressions for CodeGen. 12572 for (OMPClause *C : Clauses) { 12573 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12574 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12575 B.NumIterations, *this, CurScope, 12576 DSAStack)) 12577 return StmtError(); 12578 } 12579 } 12580 12581 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12582 return StmtError(); 12583 12584 setFunctionHasBranchProtectedScope(); 12585 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 12586 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12587 } 12588 12589 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 12590 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 12591 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 12592 if (!AStmt) 12593 return StmtError(); 12594 12595 auto *CS = cast<CapturedStmt>(AStmt); 12596 // 1.2.2 OpenMP Language Terminology 12597 // Structured block - An executable statement with a single entry at the 12598 // top and a single exit at the bottom. 12599 // The point of exit cannot be a branch out of the structured block. 12600 // longjmp() and throw() must not violate the entry/exit criteria. 12601 CS->getCapturedDecl()->setNothrow(); 12602 for (int ThisCaptureLevel = 12603 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 12604 ThisCaptureLevel > 1; --ThisCaptureLevel) { 12605 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 12606 // 1.2.2 OpenMP Language Terminology 12607 // Structured block - An executable statement with a single entry at the 12608 // top and a single exit at the bottom. 12609 // The point of exit cannot be a branch out of the structured block. 12610 // longjmp() and throw() must not violate the entry/exit criteria. 12611 CS->getCapturedDecl()->setNothrow(); 12612 } 12613 12614 OMPLoopBasedDirective::HelperExprs B; 12615 // In presence of clause 'collapse' with number of loops, it will 12616 // define the nested loops number. 12617 unsigned NestedLoopCount = checkOpenMPLoop( 12618 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 12619 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 12620 VarsWithImplicitDSA, B); 12621 if (NestedLoopCount == 0) 12622 return StmtError(); 12623 12624 assert((CurContext->isDependentContext() || B.builtAll()) && 12625 "omp target teams distribute simd loop exprs were not built"); 12626 12627 if (!CurContext->isDependentContext()) { 12628 // Finalize the clauses that need pre-built expressions for CodeGen. 12629 for (OMPClause *C : Clauses) { 12630 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 12631 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 12632 B.NumIterations, *this, CurScope, 12633 DSAStack)) 12634 return StmtError(); 12635 } 12636 } 12637 12638 if (checkSimdlenSafelenSpecified(*this, Clauses)) 12639 return StmtError(); 12640 12641 setFunctionHasBranchProtectedScope(); 12642 return OMPTargetTeamsDistributeSimdDirective::Create( 12643 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 12644 } 12645 12646 bool Sema::checkTransformableLoopNest( 12647 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops, 12648 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers, 12649 Stmt *&Body, 12650 SmallVectorImpl<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>> 12651 &OriginalInits) { 12652 OriginalInits.emplace_back(); 12653 bool Result = OMPLoopBasedDirective::doForAllLoops( 12654 AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops, 12655 [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt, 12656 Stmt *CurStmt) { 12657 VarsWithInheritedDSAType TmpDSA; 12658 unsigned SingleNumLoops = 12659 checkOpenMPLoop(Kind, nullptr, nullptr, CurStmt, *this, *DSAStack, 12660 TmpDSA, LoopHelpers[Cnt]); 12661 if (SingleNumLoops == 0) 12662 return true; 12663 assert(SingleNumLoops == 1 && "Expect single loop iteration space"); 12664 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 12665 OriginalInits.back().push_back(For->getInit()); 12666 Body = For->getBody(); 12667 } else { 12668 assert(isa<CXXForRangeStmt>(CurStmt) && 12669 "Expected canonical for or range-based for loops."); 12670 auto *CXXFor = cast<CXXForRangeStmt>(CurStmt); 12671 OriginalInits.back().push_back(CXXFor->getBeginStmt()); 12672 Body = CXXFor->getBody(); 12673 } 12674 OriginalInits.emplace_back(); 12675 return false; 12676 }, 12677 [&OriginalInits](OMPLoopBasedDirective *Transform) { 12678 Stmt *DependentPreInits; 12679 if (auto *Dir = dyn_cast<OMPTileDirective>(Transform)) 12680 DependentPreInits = Dir->getPreInits(); 12681 else if (auto *Dir = dyn_cast<OMPUnrollDirective>(Transform)) 12682 DependentPreInits = Dir->getPreInits(); 12683 else 12684 llvm_unreachable("Unhandled loop transformation"); 12685 if (!DependentPreInits) 12686 return; 12687 for (Decl *C : cast<DeclStmt>(DependentPreInits)->getDeclGroup()) 12688 OriginalInits.back().push_back(C); 12689 }); 12690 assert(OriginalInits.back().empty() && "No preinit after innermost loop"); 12691 OriginalInits.pop_back(); 12692 return Result; 12693 } 12694 12695 StmtResult Sema::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses, 12696 Stmt *AStmt, SourceLocation StartLoc, 12697 SourceLocation EndLoc) { 12698 auto SizesClauses = 12699 OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses); 12700 if (SizesClauses.empty()) { 12701 // A missing 'sizes' clause is already reported by the parser. 12702 return StmtError(); 12703 } 12704 const OMPSizesClause *SizesClause = *SizesClauses.begin(); 12705 unsigned NumLoops = SizesClause->getNumSizes(); 12706 12707 // Empty statement should only be possible if there already was an error. 12708 if (!AStmt) 12709 return StmtError(); 12710 12711 // Verify and diagnose loop nest. 12712 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops); 12713 Stmt *Body = nullptr; 12714 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, 4> 12715 OriginalInits; 12716 if (!checkTransformableLoopNest(OMPD_tile, AStmt, NumLoops, LoopHelpers, Body, 12717 OriginalInits)) 12718 return StmtError(); 12719 12720 // Delay tiling to when template is completely instantiated. 12721 if (CurContext->isDependentContext()) 12722 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, 12723 NumLoops, AStmt, nullptr, nullptr); 12724 12725 SmallVector<Decl *, 4> PreInits; 12726 12727 // Create iteration variables for the generated loops. 12728 SmallVector<VarDecl *, 4> FloorIndVars; 12729 SmallVector<VarDecl *, 4> TileIndVars; 12730 FloorIndVars.resize(NumLoops); 12731 TileIndVars.resize(NumLoops); 12732 for (unsigned I = 0; I < NumLoops; ++I) { 12733 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12734 12735 assert(LoopHelper.Counters.size() == 1 && 12736 "Expect single-dimensional loop iteration space"); 12737 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 12738 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString(); 12739 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 12740 QualType CntTy = IterVarRef->getType(); 12741 12742 // Iteration variable for the floor (i.e. outer) loop. 12743 { 12744 std::string FloorCntName = 12745 (Twine(".floor_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12746 VarDecl *FloorCntDecl = 12747 buildVarDecl(*this, {}, CntTy, FloorCntName, nullptr, OrigCntVar); 12748 FloorIndVars[I] = FloorCntDecl; 12749 } 12750 12751 // Iteration variable for the tile (i.e. inner) loop. 12752 { 12753 std::string TileCntName = 12754 (Twine(".tile_") + llvm::utostr(I) + ".iv." + OrigVarName).str(); 12755 12756 // Reuse the iteration variable created by checkOpenMPLoop. It is also 12757 // used by the expressions to derive the original iteration variable's 12758 // value from the logical iteration number. 12759 auto *TileCntDecl = cast<VarDecl>(IterVarRef->getDecl()); 12760 TileCntDecl->setDeclName(&PP.getIdentifierTable().get(TileCntName)); 12761 TileIndVars[I] = TileCntDecl; 12762 } 12763 for (auto &P : OriginalInits[I]) { 12764 if (auto *D = P.dyn_cast<Decl *>()) 12765 PreInits.push_back(D); 12766 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 12767 PreInits.append(PI->decl_begin(), PI->decl_end()); 12768 } 12769 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 12770 PreInits.append(PI->decl_begin(), PI->decl_end()); 12771 // Gather declarations for the data members used as counters. 12772 for (Expr *CounterRef : LoopHelper.Counters) { 12773 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 12774 if (isa<OMPCapturedExprDecl>(CounterDecl)) 12775 PreInits.push_back(CounterDecl); 12776 } 12777 } 12778 12779 // Once the original iteration values are set, append the innermost body. 12780 Stmt *Inner = Body; 12781 12782 // Create tile loops from the inside to the outside. 12783 for (int I = NumLoops - 1; I >= 0; --I) { 12784 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I]; 12785 Expr *NumIterations = LoopHelper.NumIterations; 12786 auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12787 QualType CntTy = OrigCntVar->getType(); 12788 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12789 Scope *CurScope = getCurScope(); 12790 12791 // Commonly used variables. 12792 DeclRefExpr *TileIV = buildDeclRefExpr(*this, TileIndVars[I], CntTy, 12793 OrigCntVar->getExprLoc()); 12794 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12795 OrigCntVar->getExprLoc()); 12796 12797 // For init-statement: auto .tile.iv = .floor.iv 12798 AddInitializerToDecl(TileIndVars[I], DefaultLvalueConversion(FloorIV).get(), 12799 /*DirectInit=*/false); 12800 Decl *CounterDecl = TileIndVars[I]; 12801 StmtResult InitStmt = new (Context) 12802 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12803 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12804 if (!InitStmt.isUsable()) 12805 return StmtError(); 12806 12807 // For cond-expression: .tile.iv < min(.floor.iv + DimTileSize, 12808 // NumIterations) 12809 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12810 BO_Add, FloorIV, DimTileSize); 12811 if (!EndOfTile.isUsable()) 12812 return StmtError(); 12813 ExprResult IsPartialTile = 12814 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, 12815 NumIterations, EndOfTile.get()); 12816 if (!IsPartialTile.isUsable()) 12817 return StmtError(); 12818 ExprResult MinTileAndIterSpace = ActOnConditionalOp( 12819 LoopHelper.Cond->getBeginLoc(), LoopHelper.Cond->getEndLoc(), 12820 IsPartialTile.get(), NumIterations, EndOfTile.get()); 12821 if (!MinTileAndIterSpace.isUsable()) 12822 return StmtError(); 12823 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12824 BO_LT, TileIV, MinTileAndIterSpace.get()); 12825 if (!CondExpr.isUsable()) 12826 return StmtError(); 12827 12828 // For incr-statement: ++.tile.iv 12829 ExprResult IncrStmt = 12830 BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV); 12831 if (!IncrStmt.isUsable()) 12832 return StmtError(); 12833 12834 // Statements to set the original iteration variable's value from the 12835 // logical iteration number. 12836 // Generated for loop is: 12837 // Original_for_init; 12838 // for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize, 12839 // NumIterations); ++.tile.iv) { 12840 // Original_Body; 12841 // Original_counter_update; 12842 // } 12843 // FIXME: If the innermost body is an loop itself, inserting these 12844 // statements stops it being recognized as a perfectly nested loop (e.g. 12845 // for applying tiling again). If this is the case, sink the expressions 12846 // further into the inner loop. 12847 SmallVector<Stmt *, 4> BodyParts; 12848 BodyParts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 12849 BodyParts.push_back(Inner); 12850 Inner = CompoundStmt::Create(Context, BodyParts, Inner->getBeginLoc(), 12851 Inner->getEndLoc()); 12852 Inner = new (Context) 12853 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12854 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12855 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12856 } 12857 12858 // Create floor loops from the inside to the outside. 12859 for (int I = NumLoops - 1; I >= 0; --I) { 12860 auto &LoopHelper = LoopHelpers[I]; 12861 Expr *NumIterations = LoopHelper.NumIterations; 12862 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]); 12863 QualType CntTy = OrigCntVar->getType(); 12864 Expr *DimTileSize = SizesClause->getSizesRefs()[I]; 12865 Scope *CurScope = getCurScope(); 12866 12867 // Commonly used variables. 12868 DeclRefExpr *FloorIV = buildDeclRefExpr(*this, FloorIndVars[I], CntTy, 12869 OrigCntVar->getExprLoc()); 12870 12871 // For init-statement: auto .floor.iv = 0 12872 AddInitializerToDecl( 12873 FloorIndVars[I], 12874 ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 12875 /*DirectInit=*/false); 12876 Decl *CounterDecl = FloorIndVars[I]; 12877 StmtResult InitStmt = new (Context) 12878 DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1), 12879 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc()); 12880 if (!InitStmt.isUsable()) 12881 return StmtError(); 12882 12883 // For cond-expression: .floor.iv < NumIterations 12884 ExprResult CondExpr = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 12885 BO_LT, FloorIV, NumIterations); 12886 if (!CondExpr.isUsable()) 12887 return StmtError(); 12888 12889 // For incr-statement: .floor.iv += DimTileSize 12890 ExprResult IncrStmt = BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), 12891 BO_AddAssign, FloorIV, DimTileSize); 12892 if (!IncrStmt.isUsable()) 12893 return StmtError(); 12894 12895 Inner = new (Context) 12896 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, 12897 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(), 12898 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 12899 } 12900 12901 return OMPTileDirective::Create(Context, StartLoc, EndLoc, Clauses, NumLoops, 12902 AStmt, Inner, 12903 buildPreInits(Context, PreInits)); 12904 } 12905 12906 StmtResult Sema::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses, 12907 Stmt *AStmt, 12908 SourceLocation StartLoc, 12909 SourceLocation EndLoc) { 12910 // Empty statement should only be possible if there already was an error. 12911 if (!AStmt) 12912 return StmtError(); 12913 12914 if (checkMutuallyExclusiveClauses(*this, Clauses, {OMPC_partial, OMPC_full})) 12915 return StmtError(); 12916 12917 const OMPFullClause *FullClause = 12918 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses); 12919 const OMPPartialClause *PartialClause = 12920 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses); 12921 assert(!(FullClause && PartialClause) && 12922 "mutual exclusivity must have been checked before"); 12923 12924 constexpr unsigned NumLoops = 1; 12925 Stmt *Body = nullptr; 12926 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers( 12927 NumLoops); 12928 SmallVector<SmallVector<llvm::PointerUnion<Stmt *, Decl *>, 0>, NumLoops + 1> 12929 OriginalInits; 12930 if (!checkTransformableLoopNest(OMPD_unroll, AStmt, NumLoops, LoopHelpers, 12931 Body, OriginalInits)) 12932 return StmtError(); 12933 12934 // Delay unrolling to when template is completely instantiated. 12935 if (CurContext->isDependentContext()) 12936 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 12937 nullptr, nullptr); 12938 12939 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front(); 12940 12941 if (FullClause) { 12942 if (!VerifyPositiveIntegerConstantInClause( 12943 LoopHelper.NumIterations, OMPC_full, /*StrictlyPositive=*/false, 12944 /*SuppressExprDigs=*/true) 12945 .isUsable()) { 12946 Diag(AStmt->getBeginLoc(), diag::err_omp_unroll_full_variable_trip_count); 12947 Diag(FullClause->getBeginLoc(), diag::note_omp_directive_here) 12948 << "#pragma omp unroll full"; 12949 return StmtError(); 12950 } 12951 } 12952 12953 // The generated loop may only be passed to other loop-associated directive 12954 // when a partial clause is specified. Without the requirement it is 12955 // sufficient to generate loop unroll metadata at code-generation. 12956 if (!PartialClause) 12957 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 12958 nullptr, nullptr); 12959 12960 // Otherwise, we need to provide a de-sugared/transformed AST that can be 12961 // associated with another loop directive. 12962 // 12963 // The canonical loop analysis return by checkTransformableLoopNest assumes 12964 // the following structure to be the same loop without transformations or 12965 // directives applied: \code OriginalInits; LoopHelper.PreInits; 12966 // LoopHelper.Counters; 12967 // for (; IV < LoopHelper.NumIterations; ++IV) { 12968 // LoopHelper.Updates; 12969 // Body; 12970 // } 12971 // \endcode 12972 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits 12973 // and referenced by LoopHelper.IterationVarRef. 12974 // 12975 // The unrolling directive transforms this into the following loop: 12976 // \code 12977 // OriginalInits; \ 12978 // LoopHelper.PreInits; > NewPreInits 12979 // LoopHelper.Counters; / 12980 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) { 12981 // #pragma clang loop unroll_count(Factor) 12982 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV) 12983 // { 12984 // LoopHelper.Updates; 12985 // Body; 12986 // } 12987 // } 12988 // \endcode 12989 // where UIV is a new logical iteration counter. IV must be the same VarDecl 12990 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates 12991 // references it. If the partially unrolled loop is associated with another 12992 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to 12993 // analyze this loop, i.e. the outer loop must fulfill the constraints of an 12994 // OpenMP canonical loop. The inner loop is not an associable canonical loop 12995 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of 12996 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a 12997 // property of the OMPLoopBasedDirective instead of statements in 12998 // CompoundStatement. This is to allow the loop to become a non-outermost loop 12999 // of a canonical loop nest where these PreInits are emitted before the 13000 // outermost directive. 13001 13002 // Determine the PreInit declarations. 13003 SmallVector<Decl *, 4> PreInits; 13004 assert(OriginalInits.size() == 1 && 13005 "Expecting a single-dimensional loop iteration space"); 13006 for (auto &P : OriginalInits[0]) { 13007 if (auto *D = P.dyn_cast<Decl *>()) 13008 PreInits.push_back(D); 13009 else if (auto *PI = dyn_cast_or_null<DeclStmt>(P.dyn_cast<Stmt *>())) 13010 PreInits.append(PI->decl_begin(), PI->decl_end()); 13011 } 13012 if (auto *PI = cast_or_null<DeclStmt>(LoopHelper.PreInits)) 13013 PreInits.append(PI->decl_begin(), PI->decl_end()); 13014 // Gather declarations for the data members used as counters. 13015 for (Expr *CounterRef : LoopHelper.Counters) { 13016 auto *CounterDecl = cast<DeclRefExpr>(CounterRef)->getDecl(); 13017 if (isa<OMPCapturedExprDecl>(CounterDecl)) 13018 PreInits.push_back(CounterDecl); 13019 } 13020 13021 auto *IterationVarRef = cast<DeclRefExpr>(LoopHelper.IterationVarRef); 13022 QualType IVTy = IterationVarRef->getType(); 13023 assert(LoopHelper.Counters.size() == 1 && 13024 "Expecting a single-dimensional loop iteration space"); 13025 auto *OrigVar = cast<DeclRefExpr>(LoopHelper.Counters.front()); 13026 13027 // Determine the unroll factor. 13028 uint64_t Factor; 13029 SourceLocation FactorLoc; 13030 if (Expr *FactorVal = PartialClause->getFactor()) { 13031 Factor = 13032 FactorVal->getIntegerConstantExpr(Context).getValue().getZExtValue(); 13033 FactorLoc = FactorVal->getExprLoc(); 13034 } else { 13035 // TODO: Use a better profitability model. 13036 Factor = 2; 13037 } 13038 assert(Factor > 0 && "Expected positive unroll factor"); 13039 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() { 13040 return IntegerLiteral::Create( 13041 Context, llvm::APInt(Context.getIntWidth(IVTy), Factor), IVTy, 13042 FactorLoc); 13043 }; 13044 13045 // Iteration variable SourceLocations. 13046 SourceLocation OrigVarLoc = OrigVar->getExprLoc(); 13047 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc(); 13048 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc(); 13049 13050 // Internal variable names. 13051 std::string OrigVarName = OrigVar->getNameInfo().getAsString(); 13052 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str(); 13053 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str(); 13054 std::string InnerTripCountName = 13055 (Twine(".unroll_inner.tripcount.") + OrigVarName).str(); 13056 13057 // Create the iteration variable for the unrolled loop. 13058 VarDecl *OuterIVDecl = 13059 buildVarDecl(*this, {}, IVTy, OuterIVName, nullptr, OrigVar); 13060 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() { 13061 return buildDeclRefExpr(*this, OuterIVDecl, IVTy, OrigVarLoc); 13062 }; 13063 13064 // Iteration variable for the inner loop: Reuse the iteration variable created 13065 // by checkOpenMPLoop. 13066 auto *InnerIVDecl = cast<VarDecl>(IterationVarRef->getDecl()); 13067 InnerIVDecl->setDeclName(&PP.getIdentifierTable().get(InnerIVName)); 13068 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() { 13069 return buildDeclRefExpr(*this, InnerIVDecl, IVTy, OrigVarLoc); 13070 }; 13071 13072 // Make a copy of the NumIterations expression for each use: By the AST 13073 // constraints, every expression object in a DeclContext must be unique. 13074 CaptureVars CopyTransformer(*this); 13075 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * { 13076 return AssertSuccess( 13077 CopyTransformer.TransformExpr(LoopHelper.NumIterations)); 13078 }; 13079 13080 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv 13081 ExprResult LValueConv = DefaultLvalueConversion(MakeOuterRef()); 13082 AddInitializerToDecl(InnerIVDecl, LValueConv.get(), /*DirectInit=*/false); 13083 StmtResult InnerInit = new (Context) 13084 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13085 if (!InnerInit.isUsable()) 13086 return StmtError(); 13087 13088 // Inner For cond-expression: 13089 // \code 13090 // .unroll_inner.iv < .unrolled.iv + Factor && 13091 // .unroll_inner.iv < NumIterations 13092 // \endcode 13093 // This conjunction of two conditions allows ScalarEvolution to derive the 13094 // maximum trip count of the inner loop. 13095 ExprResult EndOfTile = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13096 BO_Add, MakeOuterRef(), MakeFactorExpr()); 13097 if (!EndOfTile.isUsable()) 13098 return StmtError(); 13099 ExprResult InnerCond1 = BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), 13100 BO_LE, MakeInnerRef(), EndOfTile.get()); 13101 if (!InnerCond1.isUsable()) 13102 return StmtError(); 13103 ExprResult InnerCond2 = 13104 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LE, MakeInnerRef(), 13105 MakeNumIterations()); 13106 if (!InnerCond2.isUsable()) 13107 return StmtError(); 13108 ExprResult InnerCond = 13109 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LAnd, 13110 InnerCond1.get(), InnerCond2.get()); 13111 if (!InnerCond.isUsable()) 13112 return StmtError(); 13113 13114 // Inner For incr-statement: ++.unroll_inner.iv 13115 ExprResult InnerIncr = BuildUnaryOp(CurScope, LoopHelper.Inc->getExprLoc(), 13116 UO_PreInc, MakeInnerRef()); 13117 if (!InnerIncr.isUsable()) 13118 return StmtError(); 13119 13120 // Inner For statement. 13121 SmallVector<Stmt *> InnerBodyStmts; 13122 InnerBodyStmts.append(LoopHelper.Updates.begin(), LoopHelper.Updates.end()); 13123 InnerBodyStmts.push_back(Body); 13124 CompoundStmt *InnerBody = CompoundStmt::Create( 13125 Context, InnerBodyStmts, Body->getBeginLoc(), Body->getEndLoc()); 13126 ForStmt *InnerFor = new (Context) 13127 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr, 13128 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(), 13129 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13130 13131 // Unroll metadata for the inner loop. 13132 // This needs to take into account the remainder portion of the unrolled loop, 13133 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass 13134 // supports multiple loop exits. Instead, unroll using a factor equivalent to 13135 // the maximum trip count, which will also generate a remainder loop. Just 13136 // `unroll(enable)` (which could have been useful if the user has not 13137 // specified a concrete factor; even though the outer loop cannot be 13138 // influenced anymore, would avoid more code bloat than necessary) will refuse 13139 // the loop because "Won't unroll; remainder loop could not be generated when 13140 // assuming runtime trip count". Even if it did work, it must not choose a 13141 // larger unroll factor than the maximum loop length, or it would always just 13142 // execute the remainder loop. 13143 LoopHintAttr *UnrollHintAttr = 13144 LoopHintAttr::CreateImplicit(Context, LoopHintAttr::UnrollCount, 13145 LoopHintAttr::Numeric, MakeFactorExpr()); 13146 AttributedStmt *InnerUnrolled = 13147 AttributedStmt::Create(Context, StartLoc, {UnrollHintAttr}, InnerFor); 13148 13149 // Outer For init-statement: auto .unrolled.iv = 0 13150 AddInitializerToDecl( 13151 OuterIVDecl, ActOnIntegerConstant(LoopHelper.Init->getExprLoc(), 0).get(), 13152 /*DirectInit=*/false); 13153 StmtResult OuterInit = new (Context) 13154 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd); 13155 if (!OuterInit.isUsable()) 13156 return StmtError(); 13157 13158 // Outer For cond-expression: .unrolled.iv < NumIterations 13159 ExprResult OuterConde = 13160 BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, MakeOuterRef(), 13161 MakeNumIterations()); 13162 if (!OuterConde.isUsable()) 13163 return StmtError(); 13164 13165 // Outer For incr-statement: .unrolled.iv += Factor 13166 ExprResult OuterIncr = 13167 BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign, 13168 MakeOuterRef(), MakeFactorExpr()); 13169 if (!OuterIncr.isUsable()) 13170 return StmtError(); 13171 13172 // Outer For statement. 13173 ForStmt *OuterFor = new (Context) 13174 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr, 13175 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(), 13176 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc()); 13177 13178 return OMPUnrollDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 13179 OuterFor, buildPreInits(Context, PreInits)); 13180 } 13181 13182 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 13183 SourceLocation StartLoc, 13184 SourceLocation LParenLoc, 13185 SourceLocation EndLoc) { 13186 OMPClause *Res = nullptr; 13187 switch (Kind) { 13188 case OMPC_final: 13189 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 13190 break; 13191 case OMPC_num_threads: 13192 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 13193 break; 13194 case OMPC_safelen: 13195 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 13196 break; 13197 case OMPC_simdlen: 13198 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 13199 break; 13200 case OMPC_allocator: 13201 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 13202 break; 13203 case OMPC_collapse: 13204 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 13205 break; 13206 case OMPC_ordered: 13207 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 13208 break; 13209 case OMPC_num_teams: 13210 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 13211 break; 13212 case OMPC_thread_limit: 13213 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 13214 break; 13215 case OMPC_priority: 13216 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 13217 break; 13218 case OMPC_grainsize: 13219 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 13220 break; 13221 case OMPC_num_tasks: 13222 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 13223 break; 13224 case OMPC_hint: 13225 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 13226 break; 13227 case OMPC_depobj: 13228 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 13229 break; 13230 case OMPC_detach: 13231 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 13232 break; 13233 case OMPC_novariants: 13234 Res = ActOnOpenMPNovariantsClause(Expr, StartLoc, LParenLoc, EndLoc); 13235 break; 13236 case OMPC_nocontext: 13237 Res = ActOnOpenMPNocontextClause(Expr, StartLoc, LParenLoc, EndLoc); 13238 break; 13239 case OMPC_filter: 13240 Res = ActOnOpenMPFilterClause(Expr, StartLoc, LParenLoc, EndLoc); 13241 break; 13242 case OMPC_partial: 13243 Res = ActOnOpenMPPartialClause(Expr, StartLoc, LParenLoc, EndLoc); 13244 break; 13245 case OMPC_device: 13246 case OMPC_if: 13247 case OMPC_default: 13248 case OMPC_proc_bind: 13249 case OMPC_schedule: 13250 case OMPC_private: 13251 case OMPC_firstprivate: 13252 case OMPC_lastprivate: 13253 case OMPC_shared: 13254 case OMPC_reduction: 13255 case OMPC_task_reduction: 13256 case OMPC_in_reduction: 13257 case OMPC_linear: 13258 case OMPC_aligned: 13259 case OMPC_copyin: 13260 case OMPC_copyprivate: 13261 case OMPC_nowait: 13262 case OMPC_untied: 13263 case OMPC_mergeable: 13264 case OMPC_threadprivate: 13265 case OMPC_sizes: 13266 case OMPC_allocate: 13267 case OMPC_flush: 13268 case OMPC_read: 13269 case OMPC_write: 13270 case OMPC_update: 13271 case OMPC_capture: 13272 case OMPC_seq_cst: 13273 case OMPC_acq_rel: 13274 case OMPC_acquire: 13275 case OMPC_release: 13276 case OMPC_relaxed: 13277 case OMPC_depend: 13278 case OMPC_threads: 13279 case OMPC_simd: 13280 case OMPC_map: 13281 case OMPC_nogroup: 13282 case OMPC_dist_schedule: 13283 case OMPC_defaultmap: 13284 case OMPC_unknown: 13285 case OMPC_uniform: 13286 case OMPC_to: 13287 case OMPC_from: 13288 case OMPC_use_device_ptr: 13289 case OMPC_use_device_addr: 13290 case OMPC_is_device_ptr: 13291 case OMPC_unified_address: 13292 case OMPC_unified_shared_memory: 13293 case OMPC_reverse_offload: 13294 case OMPC_dynamic_allocators: 13295 case OMPC_atomic_default_mem_order: 13296 case OMPC_device_type: 13297 case OMPC_match: 13298 case OMPC_nontemporal: 13299 case OMPC_order: 13300 case OMPC_destroy: 13301 case OMPC_inclusive: 13302 case OMPC_exclusive: 13303 case OMPC_uses_allocators: 13304 case OMPC_affinity: 13305 case OMPC_when: 13306 default: 13307 llvm_unreachable("Clause is not allowed."); 13308 } 13309 return Res; 13310 } 13311 13312 // An OpenMP directive such as 'target parallel' has two captured regions: 13313 // for the 'target' and 'parallel' respectively. This function returns 13314 // the region in which to capture expressions associated with a clause. 13315 // A return value of OMPD_unknown signifies that the expression should not 13316 // be captured. 13317 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 13318 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 13319 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 13320 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 13321 switch (CKind) { 13322 case OMPC_if: 13323 switch (DKind) { 13324 case OMPD_target_parallel_for_simd: 13325 if (OpenMPVersion >= 50 && 13326 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13327 CaptureRegion = OMPD_parallel; 13328 break; 13329 } 13330 LLVM_FALLTHROUGH; 13331 case OMPD_target_parallel: 13332 case OMPD_target_parallel_for: 13333 // If this clause applies to the nested 'parallel' region, capture within 13334 // the 'target' region, otherwise do not capture. 13335 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13336 CaptureRegion = OMPD_target; 13337 break; 13338 case OMPD_target_teams_distribute_parallel_for_simd: 13339 if (OpenMPVersion >= 50 && 13340 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13341 CaptureRegion = OMPD_parallel; 13342 break; 13343 } 13344 LLVM_FALLTHROUGH; 13345 case OMPD_target_teams_distribute_parallel_for: 13346 // If this clause applies to the nested 'parallel' region, capture within 13347 // the 'teams' region, otherwise do not capture. 13348 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 13349 CaptureRegion = OMPD_teams; 13350 break; 13351 case OMPD_teams_distribute_parallel_for_simd: 13352 if (OpenMPVersion >= 50 && 13353 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 13354 CaptureRegion = OMPD_parallel; 13355 break; 13356 } 13357 LLVM_FALLTHROUGH; 13358 case OMPD_teams_distribute_parallel_for: 13359 CaptureRegion = OMPD_teams; 13360 break; 13361 case OMPD_target_update: 13362 case OMPD_target_enter_data: 13363 case OMPD_target_exit_data: 13364 CaptureRegion = OMPD_task; 13365 break; 13366 case OMPD_parallel_master_taskloop: 13367 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 13368 CaptureRegion = OMPD_parallel; 13369 break; 13370 case OMPD_parallel_master_taskloop_simd: 13371 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 13372 NameModifier == OMPD_taskloop) { 13373 CaptureRegion = OMPD_parallel; 13374 break; 13375 } 13376 if (OpenMPVersion <= 45) 13377 break; 13378 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13379 CaptureRegion = OMPD_taskloop; 13380 break; 13381 case OMPD_parallel_for_simd: 13382 if (OpenMPVersion <= 45) 13383 break; 13384 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13385 CaptureRegion = OMPD_parallel; 13386 break; 13387 case OMPD_taskloop_simd: 13388 case OMPD_master_taskloop_simd: 13389 if (OpenMPVersion <= 45) 13390 break; 13391 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13392 CaptureRegion = OMPD_taskloop; 13393 break; 13394 case OMPD_distribute_parallel_for_simd: 13395 if (OpenMPVersion <= 45) 13396 break; 13397 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 13398 CaptureRegion = OMPD_parallel; 13399 break; 13400 case OMPD_target_simd: 13401 if (OpenMPVersion >= 50 && 13402 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13403 CaptureRegion = OMPD_target; 13404 break; 13405 case OMPD_teams_distribute_simd: 13406 case OMPD_target_teams_distribute_simd: 13407 if (OpenMPVersion >= 50 && 13408 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 13409 CaptureRegion = OMPD_teams; 13410 break; 13411 case OMPD_cancel: 13412 case OMPD_parallel: 13413 case OMPD_parallel_master: 13414 case OMPD_parallel_sections: 13415 case OMPD_parallel_for: 13416 case OMPD_target: 13417 case OMPD_target_teams: 13418 case OMPD_target_teams_distribute: 13419 case OMPD_distribute_parallel_for: 13420 case OMPD_task: 13421 case OMPD_taskloop: 13422 case OMPD_master_taskloop: 13423 case OMPD_target_data: 13424 case OMPD_simd: 13425 case OMPD_for_simd: 13426 case OMPD_distribute_simd: 13427 // Do not capture if-clause expressions. 13428 break; 13429 case OMPD_threadprivate: 13430 case OMPD_allocate: 13431 case OMPD_taskyield: 13432 case OMPD_barrier: 13433 case OMPD_taskwait: 13434 case OMPD_cancellation_point: 13435 case OMPD_flush: 13436 case OMPD_depobj: 13437 case OMPD_scan: 13438 case OMPD_declare_reduction: 13439 case OMPD_declare_mapper: 13440 case OMPD_declare_simd: 13441 case OMPD_declare_variant: 13442 case OMPD_begin_declare_variant: 13443 case OMPD_end_declare_variant: 13444 case OMPD_declare_target: 13445 case OMPD_end_declare_target: 13446 case OMPD_teams: 13447 case OMPD_tile: 13448 case OMPD_unroll: 13449 case OMPD_for: 13450 case OMPD_sections: 13451 case OMPD_section: 13452 case OMPD_single: 13453 case OMPD_master: 13454 case OMPD_masked: 13455 case OMPD_critical: 13456 case OMPD_taskgroup: 13457 case OMPD_distribute: 13458 case OMPD_ordered: 13459 case OMPD_atomic: 13460 case OMPD_teams_distribute: 13461 case OMPD_requires: 13462 case OMPD_metadirective: 13463 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 13464 case OMPD_unknown: 13465 default: 13466 llvm_unreachable("Unknown OpenMP directive"); 13467 } 13468 break; 13469 case OMPC_num_threads: 13470 switch (DKind) { 13471 case OMPD_target_parallel: 13472 case OMPD_target_parallel_for: 13473 case OMPD_target_parallel_for_simd: 13474 CaptureRegion = OMPD_target; 13475 break; 13476 case OMPD_teams_distribute_parallel_for: 13477 case OMPD_teams_distribute_parallel_for_simd: 13478 case OMPD_target_teams_distribute_parallel_for: 13479 case OMPD_target_teams_distribute_parallel_for_simd: 13480 CaptureRegion = OMPD_teams; 13481 break; 13482 case OMPD_parallel: 13483 case OMPD_parallel_master: 13484 case OMPD_parallel_sections: 13485 case OMPD_parallel_for: 13486 case OMPD_parallel_for_simd: 13487 case OMPD_distribute_parallel_for: 13488 case OMPD_distribute_parallel_for_simd: 13489 case OMPD_parallel_master_taskloop: 13490 case OMPD_parallel_master_taskloop_simd: 13491 // Do not capture num_threads-clause expressions. 13492 break; 13493 case OMPD_target_data: 13494 case OMPD_target_enter_data: 13495 case OMPD_target_exit_data: 13496 case OMPD_target_update: 13497 case OMPD_target: 13498 case OMPD_target_simd: 13499 case OMPD_target_teams: 13500 case OMPD_target_teams_distribute: 13501 case OMPD_target_teams_distribute_simd: 13502 case OMPD_cancel: 13503 case OMPD_task: 13504 case OMPD_taskloop: 13505 case OMPD_taskloop_simd: 13506 case OMPD_master_taskloop: 13507 case OMPD_master_taskloop_simd: 13508 case OMPD_threadprivate: 13509 case OMPD_allocate: 13510 case OMPD_taskyield: 13511 case OMPD_barrier: 13512 case OMPD_taskwait: 13513 case OMPD_cancellation_point: 13514 case OMPD_flush: 13515 case OMPD_depobj: 13516 case OMPD_scan: 13517 case OMPD_declare_reduction: 13518 case OMPD_declare_mapper: 13519 case OMPD_declare_simd: 13520 case OMPD_declare_variant: 13521 case OMPD_begin_declare_variant: 13522 case OMPD_end_declare_variant: 13523 case OMPD_declare_target: 13524 case OMPD_end_declare_target: 13525 case OMPD_teams: 13526 case OMPD_simd: 13527 case OMPD_tile: 13528 case OMPD_unroll: 13529 case OMPD_for: 13530 case OMPD_for_simd: 13531 case OMPD_sections: 13532 case OMPD_section: 13533 case OMPD_single: 13534 case OMPD_master: 13535 case OMPD_masked: 13536 case OMPD_critical: 13537 case OMPD_taskgroup: 13538 case OMPD_distribute: 13539 case OMPD_ordered: 13540 case OMPD_atomic: 13541 case OMPD_distribute_simd: 13542 case OMPD_teams_distribute: 13543 case OMPD_teams_distribute_simd: 13544 case OMPD_requires: 13545 case OMPD_metadirective: 13546 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 13547 case OMPD_unknown: 13548 default: 13549 llvm_unreachable("Unknown OpenMP directive"); 13550 } 13551 break; 13552 case OMPC_num_teams: 13553 switch (DKind) { 13554 case OMPD_target_teams: 13555 case OMPD_target_teams_distribute: 13556 case OMPD_target_teams_distribute_simd: 13557 case OMPD_target_teams_distribute_parallel_for: 13558 case OMPD_target_teams_distribute_parallel_for_simd: 13559 CaptureRegion = OMPD_target; 13560 break; 13561 case OMPD_teams_distribute_parallel_for: 13562 case OMPD_teams_distribute_parallel_for_simd: 13563 case OMPD_teams: 13564 case OMPD_teams_distribute: 13565 case OMPD_teams_distribute_simd: 13566 // Do not capture num_teams-clause expressions. 13567 break; 13568 case OMPD_distribute_parallel_for: 13569 case OMPD_distribute_parallel_for_simd: 13570 case OMPD_task: 13571 case OMPD_taskloop: 13572 case OMPD_taskloop_simd: 13573 case OMPD_master_taskloop: 13574 case OMPD_master_taskloop_simd: 13575 case OMPD_parallel_master_taskloop: 13576 case OMPD_parallel_master_taskloop_simd: 13577 case OMPD_target_data: 13578 case OMPD_target_enter_data: 13579 case OMPD_target_exit_data: 13580 case OMPD_target_update: 13581 case OMPD_cancel: 13582 case OMPD_parallel: 13583 case OMPD_parallel_master: 13584 case OMPD_parallel_sections: 13585 case OMPD_parallel_for: 13586 case OMPD_parallel_for_simd: 13587 case OMPD_target: 13588 case OMPD_target_simd: 13589 case OMPD_target_parallel: 13590 case OMPD_target_parallel_for: 13591 case OMPD_target_parallel_for_simd: 13592 case OMPD_threadprivate: 13593 case OMPD_allocate: 13594 case OMPD_taskyield: 13595 case OMPD_barrier: 13596 case OMPD_taskwait: 13597 case OMPD_cancellation_point: 13598 case OMPD_flush: 13599 case OMPD_depobj: 13600 case OMPD_scan: 13601 case OMPD_declare_reduction: 13602 case OMPD_declare_mapper: 13603 case OMPD_declare_simd: 13604 case OMPD_declare_variant: 13605 case OMPD_begin_declare_variant: 13606 case OMPD_end_declare_variant: 13607 case OMPD_declare_target: 13608 case OMPD_end_declare_target: 13609 case OMPD_simd: 13610 case OMPD_tile: 13611 case OMPD_unroll: 13612 case OMPD_for: 13613 case OMPD_for_simd: 13614 case OMPD_sections: 13615 case OMPD_section: 13616 case OMPD_single: 13617 case OMPD_master: 13618 case OMPD_masked: 13619 case OMPD_critical: 13620 case OMPD_taskgroup: 13621 case OMPD_distribute: 13622 case OMPD_ordered: 13623 case OMPD_atomic: 13624 case OMPD_distribute_simd: 13625 case OMPD_requires: 13626 case OMPD_metadirective: 13627 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 13628 case OMPD_unknown: 13629 default: 13630 llvm_unreachable("Unknown OpenMP directive"); 13631 } 13632 break; 13633 case OMPC_thread_limit: 13634 switch (DKind) { 13635 case OMPD_target_teams: 13636 case OMPD_target_teams_distribute: 13637 case OMPD_target_teams_distribute_simd: 13638 case OMPD_target_teams_distribute_parallel_for: 13639 case OMPD_target_teams_distribute_parallel_for_simd: 13640 CaptureRegion = OMPD_target; 13641 break; 13642 case OMPD_teams_distribute_parallel_for: 13643 case OMPD_teams_distribute_parallel_for_simd: 13644 case OMPD_teams: 13645 case OMPD_teams_distribute: 13646 case OMPD_teams_distribute_simd: 13647 // Do not capture thread_limit-clause expressions. 13648 break; 13649 case OMPD_distribute_parallel_for: 13650 case OMPD_distribute_parallel_for_simd: 13651 case OMPD_task: 13652 case OMPD_taskloop: 13653 case OMPD_taskloop_simd: 13654 case OMPD_master_taskloop: 13655 case OMPD_master_taskloop_simd: 13656 case OMPD_parallel_master_taskloop: 13657 case OMPD_parallel_master_taskloop_simd: 13658 case OMPD_target_data: 13659 case OMPD_target_enter_data: 13660 case OMPD_target_exit_data: 13661 case OMPD_target_update: 13662 case OMPD_cancel: 13663 case OMPD_parallel: 13664 case OMPD_parallel_master: 13665 case OMPD_parallel_sections: 13666 case OMPD_parallel_for: 13667 case OMPD_parallel_for_simd: 13668 case OMPD_target: 13669 case OMPD_target_simd: 13670 case OMPD_target_parallel: 13671 case OMPD_target_parallel_for: 13672 case OMPD_target_parallel_for_simd: 13673 case OMPD_threadprivate: 13674 case OMPD_allocate: 13675 case OMPD_taskyield: 13676 case OMPD_barrier: 13677 case OMPD_taskwait: 13678 case OMPD_cancellation_point: 13679 case OMPD_flush: 13680 case OMPD_depobj: 13681 case OMPD_scan: 13682 case OMPD_declare_reduction: 13683 case OMPD_declare_mapper: 13684 case OMPD_declare_simd: 13685 case OMPD_declare_variant: 13686 case OMPD_begin_declare_variant: 13687 case OMPD_end_declare_variant: 13688 case OMPD_declare_target: 13689 case OMPD_end_declare_target: 13690 case OMPD_simd: 13691 case OMPD_tile: 13692 case OMPD_unroll: 13693 case OMPD_for: 13694 case OMPD_for_simd: 13695 case OMPD_sections: 13696 case OMPD_section: 13697 case OMPD_single: 13698 case OMPD_master: 13699 case OMPD_masked: 13700 case OMPD_critical: 13701 case OMPD_taskgroup: 13702 case OMPD_distribute: 13703 case OMPD_ordered: 13704 case OMPD_atomic: 13705 case OMPD_distribute_simd: 13706 case OMPD_requires: 13707 case OMPD_metadirective: 13708 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 13709 case OMPD_unknown: 13710 default: 13711 llvm_unreachable("Unknown OpenMP directive"); 13712 } 13713 break; 13714 case OMPC_schedule: 13715 switch (DKind) { 13716 case OMPD_parallel_for: 13717 case OMPD_parallel_for_simd: 13718 case OMPD_distribute_parallel_for: 13719 case OMPD_distribute_parallel_for_simd: 13720 case OMPD_teams_distribute_parallel_for: 13721 case OMPD_teams_distribute_parallel_for_simd: 13722 case OMPD_target_parallel_for: 13723 case OMPD_target_parallel_for_simd: 13724 case OMPD_target_teams_distribute_parallel_for: 13725 case OMPD_target_teams_distribute_parallel_for_simd: 13726 CaptureRegion = OMPD_parallel; 13727 break; 13728 case OMPD_for: 13729 case OMPD_for_simd: 13730 // Do not capture schedule-clause expressions. 13731 break; 13732 case OMPD_task: 13733 case OMPD_taskloop: 13734 case OMPD_taskloop_simd: 13735 case OMPD_master_taskloop: 13736 case OMPD_master_taskloop_simd: 13737 case OMPD_parallel_master_taskloop: 13738 case OMPD_parallel_master_taskloop_simd: 13739 case OMPD_target_data: 13740 case OMPD_target_enter_data: 13741 case OMPD_target_exit_data: 13742 case OMPD_target_update: 13743 case OMPD_teams: 13744 case OMPD_teams_distribute: 13745 case OMPD_teams_distribute_simd: 13746 case OMPD_target_teams_distribute: 13747 case OMPD_target_teams_distribute_simd: 13748 case OMPD_target: 13749 case OMPD_target_simd: 13750 case OMPD_target_parallel: 13751 case OMPD_cancel: 13752 case OMPD_parallel: 13753 case OMPD_parallel_master: 13754 case OMPD_parallel_sections: 13755 case OMPD_threadprivate: 13756 case OMPD_allocate: 13757 case OMPD_taskyield: 13758 case OMPD_barrier: 13759 case OMPD_taskwait: 13760 case OMPD_cancellation_point: 13761 case OMPD_flush: 13762 case OMPD_depobj: 13763 case OMPD_scan: 13764 case OMPD_declare_reduction: 13765 case OMPD_declare_mapper: 13766 case OMPD_declare_simd: 13767 case OMPD_declare_variant: 13768 case OMPD_begin_declare_variant: 13769 case OMPD_end_declare_variant: 13770 case OMPD_declare_target: 13771 case OMPD_end_declare_target: 13772 case OMPD_simd: 13773 case OMPD_tile: 13774 case OMPD_unroll: 13775 case OMPD_sections: 13776 case OMPD_section: 13777 case OMPD_single: 13778 case OMPD_master: 13779 case OMPD_masked: 13780 case OMPD_critical: 13781 case OMPD_taskgroup: 13782 case OMPD_distribute: 13783 case OMPD_ordered: 13784 case OMPD_atomic: 13785 case OMPD_distribute_simd: 13786 case OMPD_target_teams: 13787 case OMPD_requires: 13788 case OMPD_metadirective: 13789 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 13790 case OMPD_unknown: 13791 default: 13792 llvm_unreachable("Unknown OpenMP directive"); 13793 } 13794 break; 13795 case OMPC_dist_schedule: 13796 switch (DKind) { 13797 case OMPD_teams_distribute_parallel_for: 13798 case OMPD_teams_distribute_parallel_for_simd: 13799 case OMPD_teams_distribute: 13800 case OMPD_teams_distribute_simd: 13801 case OMPD_target_teams_distribute_parallel_for: 13802 case OMPD_target_teams_distribute_parallel_for_simd: 13803 case OMPD_target_teams_distribute: 13804 case OMPD_target_teams_distribute_simd: 13805 CaptureRegion = OMPD_teams; 13806 break; 13807 case OMPD_distribute_parallel_for: 13808 case OMPD_distribute_parallel_for_simd: 13809 case OMPD_distribute: 13810 case OMPD_distribute_simd: 13811 // Do not capture dist_schedule-clause expressions. 13812 break; 13813 case OMPD_parallel_for: 13814 case OMPD_parallel_for_simd: 13815 case OMPD_target_parallel_for_simd: 13816 case OMPD_target_parallel_for: 13817 case OMPD_task: 13818 case OMPD_taskloop: 13819 case OMPD_taskloop_simd: 13820 case OMPD_master_taskloop: 13821 case OMPD_master_taskloop_simd: 13822 case OMPD_parallel_master_taskloop: 13823 case OMPD_parallel_master_taskloop_simd: 13824 case OMPD_target_data: 13825 case OMPD_target_enter_data: 13826 case OMPD_target_exit_data: 13827 case OMPD_target_update: 13828 case OMPD_teams: 13829 case OMPD_target: 13830 case OMPD_target_simd: 13831 case OMPD_target_parallel: 13832 case OMPD_cancel: 13833 case OMPD_parallel: 13834 case OMPD_parallel_master: 13835 case OMPD_parallel_sections: 13836 case OMPD_threadprivate: 13837 case OMPD_allocate: 13838 case OMPD_taskyield: 13839 case OMPD_barrier: 13840 case OMPD_taskwait: 13841 case OMPD_cancellation_point: 13842 case OMPD_flush: 13843 case OMPD_depobj: 13844 case OMPD_scan: 13845 case OMPD_declare_reduction: 13846 case OMPD_declare_mapper: 13847 case OMPD_declare_simd: 13848 case OMPD_declare_variant: 13849 case OMPD_begin_declare_variant: 13850 case OMPD_end_declare_variant: 13851 case OMPD_declare_target: 13852 case OMPD_end_declare_target: 13853 case OMPD_simd: 13854 case OMPD_tile: 13855 case OMPD_unroll: 13856 case OMPD_for: 13857 case OMPD_for_simd: 13858 case OMPD_sections: 13859 case OMPD_section: 13860 case OMPD_single: 13861 case OMPD_master: 13862 case OMPD_masked: 13863 case OMPD_critical: 13864 case OMPD_taskgroup: 13865 case OMPD_ordered: 13866 case OMPD_atomic: 13867 case OMPD_target_teams: 13868 case OMPD_requires: 13869 case OMPD_metadirective: 13870 llvm_unreachable("Unexpected OpenMP directive with dist_schedule clause"); 13871 case OMPD_unknown: 13872 default: 13873 llvm_unreachable("Unknown OpenMP directive"); 13874 } 13875 break; 13876 case OMPC_device: 13877 switch (DKind) { 13878 case OMPD_target_update: 13879 case OMPD_target_enter_data: 13880 case OMPD_target_exit_data: 13881 case OMPD_target: 13882 case OMPD_target_simd: 13883 case OMPD_target_teams: 13884 case OMPD_target_parallel: 13885 case OMPD_target_teams_distribute: 13886 case OMPD_target_teams_distribute_simd: 13887 case OMPD_target_parallel_for: 13888 case OMPD_target_parallel_for_simd: 13889 case OMPD_target_teams_distribute_parallel_for: 13890 case OMPD_target_teams_distribute_parallel_for_simd: 13891 case OMPD_dispatch: 13892 CaptureRegion = OMPD_task; 13893 break; 13894 case OMPD_target_data: 13895 case OMPD_interop: 13896 // Do not capture device-clause expressions. 13897 break; 13898 case OMPD_teams_distribute_parallel_for: 13899 case OMPD_teams_distribute_parallel_for_simd: 13900 case OMPD_teams: 13901 case OMPD_teams_distribute: 13902 case OMPD_teams_distribute_simd: 13903 case OMPD_distribute_parallel_for: 13904 case OMPD_distribute_parallel_for_simd: 13905 case OMPD_task: 13906 case OMPD_taskloop: 13907 case OMPD_taskloop_simd: 13908 case OMPD_master_taskloop: 13909 case OMPD_master_taskloop_simd: 13910 case OMPD_parallel_master_taskloop: 13911 case OMPD_parallel_master_taskloop_simd: 13912 case OMPD_cancel: 13913 case OMPD_parallel: 13914 case OMPD_parallel_master: 13915 case OMPD_parallel_sections: 13916 case OMPD_parallel_for: 13917 case OMPD_parallel_for_simd: 13918 case OMPD_threadprivate: 13919 case OMPD_allocate: 13920 case OMPD_taskyield: 13921 case OMPD_barrier: 13922 case OMPD_taskwait: 13923 case OMPD_cancellation_point: 13924 case OMPD_flush: 13925 case OMPD_depobj: 13926 case OMPD_scan: 13927 case OMPD_declare_reduction: 13928 case OMPD_declare_mapper: 13929 case OMPD_declare_simd: 13930 case OMPD_declare_variant: 13931 case OMPD_begin_declare_variant: 13932 case OMPD_end_declare_variant: 13933 case OMPD_declare_target: 13934 case OMPD_end_declare_target: 13935 case OMPD_simd: 13936 case OMPD_tile: 13937 case OMPD_unroll: 13938 case OMPD_for: 13939 case OMPD_for_simd: 13940 case OMPD_sections: 13941 case OMPD_section: 13942 case OMPD_single: 13943 case OMPD_master: 13944 case OMPD_masked: 13945 case OMPD_critical: 13946 case OMPD_taskgroup: 13947 case OMPD_distribute: 13948 case OMPD_ordered: 13949 case OMPD_atomic: 13950 case OMPD_distribute_simd: 13951 case OMPD_requires: 13952 case OMPD_metadirective: 13953 llvm_unreachable("Unexpected OpenMP directive with device-clause"); 13954 case OMPD_unknown: 13955 default: 13956 llvm_unreachable("Unknown OpenMP directive"); 13957 } 13958 break; 13959 case OMPC_grainsize: 13960 case OMPC_num_tasks: 13961 case OMPC_final: 13962 case OMPC_priority: 13963 switch (DKind) { 13964 case OMPD_task: 13965 case OMPD_taskloop: 13966 case OMPD_taskloop_simd: 13967 case OMPD_master_taskloop: 13968 case OMPD_master_taskloop_simd: 13969 break; 13970 case OMPD_parallel_master_taskloop: 13971 case OMPD_parallel_master_taskloop_simd: 13972 CaptureRegion = OMPD_parallel; 13973 break; 13974 case OMPD_target_update: 13975 case OMPD_target_enter_data: 13976 case OMPD_target_exit_data: 13977 case OMPD_target: 13978 case OMPD_target_simd: 13979 case OMPD_target_teams: 13980 case OMPD_target_parallel: 13981 case OMPD_target_teams_distribute: 13982 case OMPD_target_teams_distribute_simd: 13983 case OMPD_target_parallel_for: 13984 case OMPD_target_parallel_for_simd: 13985 case OMPD_target_teams_distribute_parallel_for: 13986 case OMPD_target_teams_distribute_parallel_for_simd: 13987 case OMPD_target_data: 13988 case OMPD_teams_distribute_parallel_for: 13989 case OMPD_teams_distribute_parallel_for_simd: 13990 case OMPD_teams: 13991 case OMPD_teams_distribute: 13992 case OMPD_teams_distribute_simd: 13993 case OMPD_distribute_parallel_for: 13994 case OMPD_distribute_parallel_for_simd: 13995 case OMPD_cancel: 13996 case OMPD_parallel: 13997 case OMPD_parallel_master: 13998 case OMPD_parallel_sections: 13999 case OMPD_parallel_for: 14000 case OMPD_parallel_for_simd: 14001 case OMPD_threadprivate: 14002 case OMPD_allocate: 14003 case OMPD_taskyield: 14004 case OMPD_barrier: 14005 case OMPD_taskwait: 14006 case OMPD_cancellation_point: 14007 case OMPD_flush: 14008 case OMPD_depobj: 14009 case OMPD_scan: 14010 case OMPD_declare_reduction: 14011 case OMPD_declare_mapper: 14012 case OMPD_declare_simd: 14013 case OMPD_declare_variant: 14014 case OMPD_begin_declare_variant: 14015 case OMPD_end_declare_variant: 14016 case OMPD_declare_target: 14017 case OMPD_end_declare_target: 14018 case OMPD_simd: 14019 case OMPD_tile: 14020 case OMPD_unroll: 14021 case OMPD_for: 14022 case OMPD_for_simd: 14023 case OMPD_sections: 14024 case OMPD_section: 14025 case OMPD_single: 14026 case OMPD_master: 14027 case OMPD_masked: 14028 case OMPD_critical: 14029 case OMPD_taskgroup: 14030 case OMPD_distribute: 14031 case OMPD_ordered: 14032 case OMPD_atomic: 14033 case OMPD_distribute_simd: 14034 case OMPD_requires: 14035 case OMPD_metadirective: 14036 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 14037 case OMPD_unknown: 14038 default: 14039 llvm_unreachable("Unknown OpenMP directive"); 14040 } 14041 break; 14042 case OMPC_novariants: 14043 case OMPC_nocontext: 14044 switch (DKind) { 14045 case OMPD_dispatch: 14046 CaptureRegion = OMPD_task; 14047 break; 14048 default: 14049 llvm_unreachable("Unexpected OpenMP directive"); 14050 } 14051 break; 14052 case OMPC_filter: 14053 // Do not capture filter-clause expressions. 14054 break; 14055 case OMPC_when: 14056 if (DKind == OMPD_metadirective) { 14057 CaptureRegion = OMPD_metadirective; 14058 } else if (DKind == OMPD_unknown) { 14059 llvm_unreachable("Unknown OpenMP directive"); 14060 } else { 14061 llvm_unreachable("Unexpected OpenMP directive with when clause"); 14062 } 14063 break; 14064 case OMPC_firstprivate: 14065 case OMPC_lastprivate: 14066 case OMPC_reduction: 14067 case OMPC_task_reduction: 14068 case OMPC_in_reduction: 14069 case OMPC_linear: 14070 case OMPC_default: 14071 case OMPC_proc_bind: 14072 case OMPC_safelen: 14073 case OMPC_simdlen: 14074 case OMPC_sizes: 14075 case OMPC_allocator: 14076 case OMPC_collapse: 14077 case OMPC_private: 14078 case OMPC_shared: 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_update: 14093 case OMPC_capture: 14094 case OMPC_seq_cst: 14095 case OMPC_acq_rel: 14096 case OMPC_acquire: 14097 case OMPC_release: 14098 case OMPC_relaxed: 14099 case OMPC_depend: 14100 case OMPC_threads: 14101 case OMPC_simd: 14102 case OMPC_map: 14103 case OMPC_nogroup: 14104 case OMPC_hint: 14105 case OMPC_defaultmap: 14106 case OMPC_unknown: 14107 case OMPC_uniform: 14108 case OMPC_to: 14109 case OMPC_from: 14110 case OMPC_use_device_ptr: 14111 case OMPC_use_device_addr: 14112 case OMPC_is_device_ptr: 14113 case OMPC_unified_address: 14114 case OMPC_unified_shared_memory: 14115 case OMPC_reverse_offload: 14116 case OMPC_dynamic_allocators: 14117 case OMPC_atomic_default_mem_order: 14118 case OMPC_device_type: 14119 case OMPC_match: 14120 case OMPC_nontemporal: 14121 case OMPC_order: 14122 case OMPC_destroy: 14123 case OMPC_detach: 14124 case OMPC_inclusive: 14125 case OMPC_exclusive: 14126 case OMPC_uses_allocators: 14127 case OMPC_affinity: 14128 default: 14129 llvm_unreachable("Unexpected OpenMP clause."); 14130 } 14131 return CaptureRegion; 14132 } 14133 14134 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 14135 Expr *Condition, SourceLocation StartLoc, 14136 SourceLocation LParenLoc, 14137 SourceLocation NameModifierLoc, 14138 SourceLocation ColonLoc, 14139 SourceLocation EndLoc) { 14140 Expr *ValExpr = Condition; 14141 Stmt *HelperValStmt = nullptr; 14142 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14143 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14144 !Condition->isInstantiationDependent() && 14145 !Condition->containsUnexpandedParameterPack()) { 14146 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14147 if (Val.isInvalid()) 14148 return nullptr; 14149 14150 ValExpr = Val.get(); 14151 14152 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14153 CaptureRegion = getOpenMPCaptureRegionForClause( 14154 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 14155 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14156 ValExpr = MakeFullExpr(ValExpr).get(); 14157 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14158 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14159 HelperValStmt = buildPreInits(Context, Captures); 14160 } 14161 } 14162 14163 return new (Context) 14164 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 14165 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 14166 } 14167 14168 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 14169 SourceLocation StartLoc, 14170 SourceLocation LParenLoc, 14171 SourceLocation EndLoc) { 14172 Expr *ValExpr = Condition; 14173 Stmt *HelperValStmt = nullptr; 14174 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 14175 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 14176 !Condition->isInstantiationDependent() && 14177 !Condition->containsUnexpandedParameterPack()) { 14178 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 14179 if (Val.isInvalid()) 14180 return nullptr; 14181 14182 ValExpr = MakeFullExpr(Val.get()).get(); 14183 14184 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14185 CaptureRegion = 14186 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 14187 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14188 ValExpr = MakeFullExpr(ValExpr).get(); 14189 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14190 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14191 HelperValStmt = buildPreInits(Context, Captures); 14192 } 14193 } 14194 14195 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 14196 StartLoc, LParenLoc, EndLoc); 14197 } 14198 14199 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 14200 Expr *Op) { 14201 if (!Op) 14202 return ExprError(); 14203 14204 class IntConvertDiagnoser : public ICEConvertDiagnoser { 14205 public: 14206 IntConvertDiagnoser() 14207 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 14208 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 14209 QualType T) override { 14210 return S.Diag(Loc, diag::err_omp_not_integral) << T; 14211 } 14212 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 14213 QualType T) override { 14214 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 14215 } 14216 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 14217 QualType T, 14218 QualType ConvTy) override { 14219 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 14220 } 14221 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 14222 QualType ConvTy) override { 14223 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14224 << ConvTy->isEnumeralType() << ConvTy; 14225 } 14226 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 14227 QualType T) override { 14228 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 14229 } 14230 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 14231 QualType ConvTy) override { 14232 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 14233 << ConvTy->isEnumeralType() << ConvTy; 14234 } 14235 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 14236 QualType) override { 14237 llvm_unreachable("conversion functions are permitted"); 14238 } 14239 } ConvertDiagnoser; 14240 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 14241 } 14242 14243 static bool 14244 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 14245 bool StrictlyPositive, bool BuildCapture = false, 14246 OpenMPDirectiveKind DKind = OMPD_unknown, 14247 OpenMPDirectiveKind *CaptureRegion = nullptr, 14248 Stmt **HelperValStmt = nullptr) { 14249 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 14250 !ValExpr->isInstantiationDependent()) { 14251 SourceLocation Loc = ValExpr->getExprLoc(); 14252 ExprResult Value = 14253 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 14254 if (Value.isInvalid()) 14255 return false; 14256 14257 ValExpr = Value.get(); 14258 // The expression must evaluate to a non-negative integer value. 14259 if (Optional<llvm::APSInt> Result = 14260 ValExpr->getIntegerConstantExpr(SemaRef.Context)) { 14261 if (Result->isSigned() && 14262 !((!StrictlyPositive && Result->isNonNegative()) || 14263 (StrictlyPositive && Result->isStrictlyPositive()))) { 14264 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 14265 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14266 << ValExpr->getSourceRange(); 14267 return false; 14268 } 14269 } 14270 if (!BuildCapture) 14271 return true; 14272 *CaptureRegion = 14273 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 14274 if (*CaptureRegion != OMPD_unknown && 14275 !SemaRef.CurContext->isDependentContext()) { 14276 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 14277 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14278 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 14279 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 14280 } 14281 } 14282 return true; 14283 } 14284 14285 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 14286 SourceLocation StartLoc, 14287 SourceLocation LParenLoc, 14288 SourceLocation EndLoc) { 14289 Expr *ValExpr = NumThreads; 14290 Stmt *HelperValStmt = nullptr; 14291 14292 // OpenMP [2.5, Restrictions] 14293 // The num_threads expression must evaluate to a positive integer value. 14294 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 14295 /*StrictlyPositive=*/true)) 14296 return nullptr; 14297 14298 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14299 OpenMPDirectiveKind CaptureRegion = 14300 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 14301 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14302 ValExpr = MakeFullExpr(ValExpr).get(); 14303 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14304 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14305 HelperValStmt = buildPreInits(Context, Captures); 14306 } 14307 14308 return new (Context) OMPNumThreadsClause( 14309 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14310 } 14311 14312 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 14313 OpenMPClauseKind CKind, 14314 bool StrictlyPositive, 14315 bool SuppressExprDiags) { 14316 if (!E) 14317 return ExprError(); 14318 if (E->isValueDependent() || E->isTypeDependent() || 14319 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 14320 return E; 14321 14322 llvm::APSInt Result; 14323 ExprResult ICE; 14324 if (SuppressExprDiags) { 14325 // Use a custom diagnoser that suppresses 'note' diagnostics about the 14326 // expression. 14327 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser { 14328 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {} 14329 Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, 14330 SourceLocation Loc) override { 14331 llvm_unreachable("Diagnostic suppressed"); 14332 } 14333 } Diagnoser; 14334 ICE = VerifyIntegerConstantExpression(E, &Result, Diagnoser, AllowFold); 14335 } else { 14336 ICE = VerifyIntegerConstantExpression(E, &Result, /*FIXME*/ AllowFold); 14337 } 14338 if (ICE.isInvalid()) 14339 return ExprError(); 14340 14341 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 14342 (!StrictlyPositive && !Result.isNonNegative())) { 14343 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 14344 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 14345 << E->getSourceRange(); 14346 return ExprError(); 14347 } 14348 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 14349 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 14350 << E->getSourceRange(); 14351 return ExprError(); 14352 } 14353 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 14354 DSAStack->setAssociatedLoops(Result.getExtValue()); 14355 else if (CKind == OMPC_ordered) 14356 DSAStack->setAssociatedLoops(Result.getExtValue()); 14357 return ICE; 14358 } 14359 14360 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 14361 SourceLocation LParenLoc, 14362 SourceLocation EndLoc) { 14363 // OpenMP [2.8.1, simd construct, Description] 14364 // The parameter of the safelen clause must be a constant 14365 // positive integer expression. 14366 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 14367 if (Safelen.isInvalid()) 14368 return nullptr; 14369 return new (Context) 14370 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 14371 } 14372 14373 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 14374 SourceLocation LParenLoc, 14375 SourceLocation EndLoc) { 14376 // OpenMP [2.8.1, simd construct, Description] 14377 // The parameter of the simdlen clause must be a constant 14378 // positive integer expression. 14379 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 14380 if (Simdlen.isInvalid()) 14381 return nullptr; 14382 return new (Context) 14383 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 14384 } 14385 14386 /// Tries to find omp_allocator_handle_t type. 14387 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 14388 DSAStackTy *Stack) { 14389 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 14390 if (!OMPAllocatorHandleT.isNull()) 14391 return true; 14392 // Build the predefined allocator expressions. 14393 bool ErrorFound = false; 14394 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 14395 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 14396 StringRef Allocator = 14397 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 14398 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 14399 auto *VD = dyn_cast_or_null<ValueDecl>( 14400 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 14401 if (!VD) { 14402 ErrorFound = true; 14403 break; 14404 } 14405 QualType AllocatorType = 14406 VD->getType().getNonLValueExprType(S.getASTContext()); 14407 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 14408 if (!Res.isUsable()) { 14409 ErrorFound = true; 14410 break; 14411 } 14412 if (OMPAllocatorHandleT.isNull()) 14413 OMPAllocatorHandleT = AllocatorType; 14414 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 14415 ErrorFound = true; 14416 break; 14417 } 14418 Stack->setAllocator(AllocatorKind, Res.get()); 14419 } 14420 if (ErrorFound) { 14421 S.Diag(Loc, diag::err_omp_implied_type_not_found) 14422 << "omp_allocator_handle_t"; 14423 return false; 14424 } 14425 OMPAllocatorHandleT.addConst(); 14426 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 14427 return true; 14428 } 14429 14430 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 14431 SourceLocation LParenLoc, 14432 SourceLocation EndLoc) { 14433 // OpenMP [2.11.3, allocate Directive, Description] 14434 // allocator is an expression of omp_allocator_handle_t type. 14435 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 14436 return nullptr; 14437 14438 ExprResult Allocator = DefaultLvalueConversion(A); 14439 if (Allocator.isInvalid()) 14440 return nullptr; 14441 Allocator = PerformImplicitConversion(Allocator.get(), 14442 DSAStack->getOMPAllocatorHandleT(), 14443 Sema::AA_Initializing, 14444 /*AllowExplicit=*/true); 14445 if (Allocator.isInvalid()) 14446 return nullptr; 14447 return new (Context) 14448 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 14449 } 14450 14451 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 14452 SourceLocation StartLoc, 14453 SourceLocation LParenLoc, 14454 SourceLocation EndLoc) { 14455 // OpenMP [2.7.1, loop construct, Description] 14456 // OpenMP [2.8.1, simd construct, Description] 14457 // OpenMP [2.9.6, distribute construct, Description] 14458 // The parameter of the collapse clause must be a constant 14459 // positive integer expression. 14460 ExprResult NumForLoopsResult = 14461 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 14462 if (NumForLoopsResult.isInvalid()) 14463 return nullptr; 14464 return new (Context) 14465 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 14466 } 14467 14468 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 14469 SourceLocation EndLoc, 14470 SourceLocation LParenLoc, 14471 Expr *NumForLoops) { 14472 // OpenMP [2.7.1, loop construct, Description] 14473 // OpenMP [2.8.1, simd construct, Description] 14474 // OpenMP [2.9.6, distribute construct, Description] 14475 // The parameter of the ordered clause must be a constant 14476 // positive integer expression if any. 14477 if (NumForLoops && LParenLoc.isValid()) { 14478 ExprResult NumForLoopsResult = 14479 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 14480 if (NumForLoopsResult.isInvalid()) 14481 return nullptr; 14482 NumForLoops = NumForLoopsResult.get(); 14483 } else { 14484 NumForLoops = nullptr; 14485 } 14486 auto *Clause = OMPOrderedClause::Create( 14487 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 14488 StartLoc, LParenLoc, EndLoc); 14489 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 14490 return Clause; 14491 } 14492 14493 OMPClause *Sema::ActOnOpenMPSimpleClause( 14494 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 14495 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14496 OMPClause *Res = nullptr; 14497 switch (Kind) { 14498 case OMPC_default: 14499 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 14500 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14501 break; 14502 case OMPC_proc_bind: 14503 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 14504 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14505 break; 14506 case OMPC_atomic_default_mem_order: 14507 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 14508 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 14509 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14510 break; 14511 case OMPC_order: 14512 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 14513 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14514 break; 14515 case OMPC_update: 14516 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 14517 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 14518 break; 14519 case OMPC_if: 14520 case OMPC_final: 14521 case OMPC_num_threads: 14522 case OMPC_safelen: 14523 case OMPC_simdlen: 14524 case OMPC_sizes: 14525 case OMPC_allocator: 14526 case OMPC_collapse: 14527 case OMPC_schedule: 14528 case OMPC_private: 14529 case OMPC_firstprivate: 14530 case OMPC_lastprivate: 14531 case OMPC_shared: 14532 case OMPC_reduction: 14533 case OMPC_task_reduction: 14534 case OMPC_in_reduction: 14535 case OMPC_linear: 14536 case OMPC_aligned: 14537 case OMPC_copyin: 14538 case OMPC_copyprivate: 14539 case OMPC_ordered: 14540 case OMPC_nowait: 14541 case OMPC_untied: 14542 case OMPC_mergeable: 14543 case OMPC_threadprivate: 14544 case OMPC_allocate: 14545 case OMPC_flush: 14546 case OMPC_depobj: 14547 case OMPC_read: 14548 case OMPC_write: 14549 case OMPC_capture: 14550 case OMPC_seq_cst: 14551 case OMPC_acq_rel: 14552 case OMPC_acquire: 14553 case OMPC_release: 14554 case OMPC_relaxed: 14555 case OMPC_depend: 14556 case OMPC_device: 14557 case OMPC_threads: 14558 case OMPC_simd: 14559 case OMPC_map: 14560 case OMPC_num_teams: 14561 case OMPC_thread_limit: 14562 case OMPC_priority: 14563 case OMPC_grainsize: 14564 case OMPC_nogroup: 14565 case OMPC_num_tasks: 14566 case OMPC_hint: 14567 case OMPC_dist_schedule: 14568 case OMPC_defaultmap: 14569 case OMPC_unknown: 14570 case OMPC_uniform: 14571 case OMPC_to: 14572 case OMPC_from: 14573 case OMPC_use_device_ptr: 14574 case OMPC_use_device_addr: 14575 case OMPC_is_device_ptr: 14576 case OMPC_unified_address: 14577 case OMPC_unified_shared_memory: 14578 case OMPC_reverse_offload: 14579 case OMPC_dynamic_allocators: 14580 case OMPC_device_type: 14581 case OMPC_match: 14582 case OMPC_nontemporal: 14583 case OMPC_destroy: 14584 case OMPC_novariants: 14585 case OMPC_nocontext: 14586 case OMPC_detach: 14587 case OMPC_inclusive: 14588 case OMPC_exclusive: 14589 case OMPC_uses_allocators: 14590 case OMPC_affinity: 14591 case OMPC_when: 14592 default: 14593 llvm_unreachable("Clause is not allowed."); 14594 } 14595 return Res; 14596 } 14597 14598 static std::string 14599 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 14600 ArrayRef<unsigned> Exclude = llvm::None) { 14601 SmallString<256> Buffer; 14602 llvm::raw_svector_ostream Out(Buffer); 14603 unsigned Skipped = Exclude.size(); 14604 auto S = Exclude.begin(), E = Exclude.end(); 14605 for (unsigned I = First; I < Last; ++I) { 14606 if (std::find(S, E, I) != E) { 14607 --Skipped; 14608 continue; 14609 } 14610 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 14611 if (I + Skipped + 2 == Last) 14612 Out << " or "; 14613 else if (I + Skipped + 1 != Last) 14614 Out << ", "; 14615 } 14616 return std::string(Out.str()); 14617 } 14618 14619 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 14620 SourceLocation KindKwLoc, 14621 SourceLocation StartLoc, 14622 SourceLocation LParenLoc, 14623 SourceLocation EndLoc) { 14624 if (Kind == OMP_DEFAULT_unknown) { 14625 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14626 << getListOfPossibleValues(OMPC_default, /*First=*/0, 14627 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 14628 << getOpenMPClauseName(OMPC_default); 14629 return nullptr; 14630 } 14631 14632 switch (Kind) { 14633 case OMP_DEFAULT_none: 14634 DSAStack->setDefaultDSANone(KindKwLoc); 14635 break; 14636 case OMP_DEFAULT_shared: 14637 DSAStack->setDefaultDSAShared(KindKwLoc); 14638 break; 14639 case OMP_DEFAULT_firstprivate: 14640 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 14641 break; 14642 default: 14643 llvm_unreachable("DSA unexpected in OpenMP default clause"); 14644 } 14645 14646 return new (Context) 14647 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14648 } 14649 14650 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 14651 SourceLocation KindKwLoc, 14652 SourceLocation StartLoc, 14653 SourceLocation LParenLoc, 14654 SourceLocation EndLoc) { 14655 if (Kind == OMP_PROC_BIND_unknown) { 14656 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14657 << getListOfPossibleValues(OMPC_proc_bind, 14658 /*First=*/unsigned(OMP_PROC_BIND_master), 14659 /*Last=*/ 14660 unsigned(LangOpts.OpenMP > 50 14661 ? OMP_PROC_BIND_primary 14662 : OMP_PROC_BIND_spread) + 14663 1) 14664 << getOpenMPClauseName(OMPC_proc_bind); 14665 return nullptr; 14666 } 14667 if (Kind == OMP_PROC_BIND_primary && LangOpts.OpenMP < 51) 14668 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14669 << getListOfPossibleValues(OMPC_proc_bind, 14670 /*First=*/unsigned(OMP_PROC_BIND_master), 14671 /*Last=*/ 14672 unsigned(OMP_PROC_BIND_spread) + 1) 14673 << getOpenMPClauseName(OMPC_proc_bind); 14674 return new (Context) 14675 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14676 } 14677 14678 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 14679 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 14680 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14681 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 14682 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14683 << getListOfPossibleValues( 14684 OMPC_atomic_default_mem_order, /*First=*/0, 14685 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 14686 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 14687 return nullptr; 14688 } 14689 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 14690 LParenLoc, EndLoc); 14691 } 14692 14693 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 14694 SourceLocation KindKwLoc, 14695 SourceLocation StartLoc, 14696 SourceLocation LParenLoc, 14697 SourceLocation EndLoc) { 14698 if (Kind == OMPC_ORDER_unknown) { 14699 static_assert(OMPC_ORDER_unknown > 0, 14700 "OMPC_ORDER_unknown not greater than 0"); 14701 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14702 << getListOfPossibleValues(OMPC_order, /*First=*/0, 14703 /*Last=*/OMPC_ORDER_unknown) 14704 << getOpenMPClauseName(OMPC_order); 14705 return nullptr; 14706 } 14707 return new (Context) 14708 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 14709 } 14710 14711 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 14712 SourceLocation KindKwLoc, 14713 SourceLocation StartLoc, 14714 SourceLocation LParenLoc, 14715 SourceLocation EndLoc) { 14716 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 14717 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 14718 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 14719 OMPC_DEPEND_depobj}; 14720 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 14721 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 14722 /*Last=*/OMPC_DEPEND_unknown, Except) 14723 << getOpenMPClauseName(OMPC_update); 14724 return nullptr; 14725 } 14726 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 14727 EndLoc); 14728 } 14729 14730 OMPClause *Sema::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs, 14731 SourceLocation StartLoc, 14732 SourceLocation LParenLoc, 14733 SourceLocation EndLoc) { 14734 for (Expr *SizeExpr : SizeExprs) { 14735 ExprResult NumForLoopsResult = VerifyPositiveIntegerConstantInClause( 14736 SizeExpr, OMPC_sizes, /*StrictlyPositive=*/true); 14737 if (!NumForLoopsResult.isUsable()) 14738 return nullptr; 14739 } 14740 14741 DSAStack->setAssociatedLoops(SizeExprs.size()); 14742 return OMPSizesClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14743 SizeExprs); 14744 } 14745 14746 OMPClause *Sema::ActOnOpenMPFullClause(SourceLocation StartLoc, 14747 SourceLocation EndLoc) { 14748 return OMPFullClause::Create(Context, StartLoc, EndLoc); 14749 } 14750 14751 OMPClause *Sema::ActOnOpenMPPartialClause(Expr *FactorExpr, 14752 SourceLocation StartLoc, 14753 SourceLocation LParenLoc, 14754 SourceLocation EndLoc) { 14755 if (FactorExpr) { 14756 // If an argument is specified, it must be a constant (or an unevaluated 14757 // template expression). 14758 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause( 14759 FactorExpr, OMPC_partial, /*StrictlyPositive=*/true); 14760 if (FactorResult.isInvalid()) 14761 return nullptr; 14762 FactorExpr = FactorResult.get(); 14763 } 14764 14765 return OMPPartialClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14766 FactorExpr); 14767 } 14768 14769 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 14770 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 14771 SourceLocation StartLoc, SourceLocation LParenLoc, 14772 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 14773 SourceLocation EndLoc) { 14774 OMPClause *Res = nullptr; 14775 switch (Kind) { 14776 case OMPC_schedule: 14777 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 14778 assert(Argument.size() == NumberOfElements && 14779 ArgumentLoc.size() == NumberOfElements); 14780 Res = ActOnOpenMPScheduleClause( 14781 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 14782 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 14783 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 14784 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 14785 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 14786 break; 14787 case OMPC_if: 14788 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14789 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 14790 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 14791 DelimLoc, EndLoc); 14792 break; 14793 case OMPC_dist_schedule: 14794 Res = ActOnOpenMPDistScheduleClause( 14795 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 14796 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 14797 break; 14798 case OMPC_defaultmap: 14799 enum { Modifier, DefaultmapKind }; 14800 Res = ActOnOpenMPDefaultmapClause( 14801 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 14802 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 14803 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 14804 EndLoc); 14805 break; 14806 case OMPC_device: 14807 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 14808 Res = ActOnOpenMPDeviceClause( 14809 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 14810 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 14811 break; 14812 case OMPC_final: 14813 case OMPC_num_threads: 14814 case OMPC_safelen: 14815 case OMPC_simdlen: 14816 case OMPC_sizes: 14817 case OMPC_allocator: 14818 case OMPC_collapse: 14819 case OMPC_default: 14820 case OMPC_proc_bind: 14821 case OMPC_private: 14822 case OMPC_firstprivate: 14823 case OMPC_lastprivate: 14824 case OMPC_shared: 14825 case OMPC_reduction: 14826 case OMPC_task_reduction: 14827 case OMPC_in_reduction: 14828 case OMPC_linear: 14829 case OMPC_aligned: 14830 case OMPC_copyin: 14831 case OMPC_copyprivate: 14832 case OMPC_ordered: 14833 case OMPC_nowait: 14834 case OMPC_untied: 14835 case OMPC_mergeable: 14836 case OMPC_threadprivate: 14837 case OMPC_allocate: 14838 case OMPC_flush: 14839 case OMPC_depobj: 14840 case OMPC_read: 14841 case OMPC_write: 14842 case OMPC_update: 14843 case OMPC_capture: 14844 case OMPC_seq_cst: 14845 case OMPC_acq_rel: 14846 case OMPC_acquire: 14847 case OMPC_release: 14848 case OMPC_relaxed: 14849 case OMPC_depend: 14850 case OMPC_threads: 14851 case OMPC_simd: 14852 case OMPC_map: 14853 case OMPC_num_teams: 14854 case OMPC_thread_limit: 14855 case OMPC_priority: 14856 case OMPC_grainsize: 14857 case OMPC_nogroup: 14858 case OMPC_num_tasks: 14859 case OMPC_hint: 14860 case OMPC_unknown: 14861 case OMPC_uniform: 14862 case OMPC_to: 14863 case OMPC_from: 14864 case OMPC_use_device_ptr: 14865 case OMPC_use_device_addr: 14866 case OMPC_is_device_ptr: 14867 case OMPC_unified_address: 14868 case OMPC_unified_shared_memory: 14869 case OMPC_reverse_offload: 14870 case OMPC_dynamic_allocators: 14871 case OMPC_atomic_default_mem_order: 14872 case OMPC_device_type: 14873 case OMPC_match: 14874 case OMPC_nontemporal: 14875 case OMPC_order: 14876 case OMPC_destroy: 14877 case OMPC_novariants: 14878 case OMPC_nocontext: 14879 case OMPC_detach: 14880 case OMPC_inclusive: 14881 case OMPC_exclusive: 14882 case OMPC_uses_allocators: 14883 case OMPC_affinity: 14884 case OMPC_when: 14885 default: 14886 llvm_unreachable("Clause is not allowed."); 14887 } 14888 return Res; 14889 } 14890 14891 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 14892 OpenMPScheduleClauseModifier M2, 14893 SourceLocation M1Loc, SourceLocation M2Loc) { 14894 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 14895 SmallVector<unsigned, 2> Excluded; 14896 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 14897 Excluded.push_back(M2); 14898 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 14899 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 14900 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 14901 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 14902 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 14903 << getListOfPossibleValues(OMPC_schedule, 14904 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 14905 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14906 Excluded) 14907 << getOpenMPClauseName(OMPC_schedule); 14908 return true; 14909 } 14910 return false; 14911 } 14912 14913 OMPClause *Sema::ActOnOpenMPScheduleClause( 14914 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 14915 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14916 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 14917 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 14918 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 14919 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 14920 return nullptr; 14921 // OpenMP, 2.7.1, Loop Construct, Restrictions 14922 // Either the monotonic modifier or the nonmonotonic modifier can be specified 14923 // but not both. 14924 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 14925 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 14926 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 14927 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 14928 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 14929 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 14930 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 14931 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 14932 return nullptr; 14933 } 14934 if (Kind == OMPC_SCHEDULE_unknown) { 14935 std::string Values; 14936 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 14937 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 14938 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14939 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 14940 Exclude); 14941 } else { 14942 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 14943 /*Last=*/OMPC_SCHEDULE_unknown); 14944 } 14945 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14946 << Values << getOpenMPClauseName(OMPC_schedule); 14947 return nullptr; 14948 } 14949 // OpenMP, 2.7.1, Loop Construct, Restrictions 14950 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 14951 // schedule(guided). 14952 // OpenMP 5.0 does not have this restriction. 14953 if (LangOpts.OpenMP < 50 && 14954 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 14955 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 14956 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 14957 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 14958 diag::err_omp_schedule_nonmonotonic_static); 14959 return nullptr; 14960 } 14961 Expr *ValExpr = ChunkSize; 14962 Stmt *HelperValStmt = nullptr; 14963 if (ChunkSize) { 14964 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 14965 !ChunkSize->isInstantiationDependent() && 14966 !ChunkSize->containsUnexpandedParameterPack()) { 14967 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 14968 ExprResult Val = 14969 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 14970 if (Val.isInvalid()) 14971 return nullptr; 14972 14973 ValExpr = Val.get(); 14974 14975 // OpenMP [2.7.1, Restrictions] 14976 // chunk_size must be a loop invariant integer expression with a positive 14977 // value. 14978 if (Optional<llvm::APSInt> Result = 14979 ValExpr->getIntegerConstantExpr(Context)) { 14980 if (Result->isSigned() && !Result->isStrictlyPositive()) { 14981 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 14982 << "schedule" << 1 << ChunkSize->getSourceRange(); 14983 return nullptr; 14984 } 14985 } else if (getOpenMPCaptureRegionForClause( 14986 DSAStack->getCurrentDirective(), OMPC_schedule, 14987 LangOpts.OpenMP) != OMPD_unknown && 14988 !CurContext->isDependentContext()) { 14989 ValExpr = MakeFullExpr(ValExpr).get(); 14990 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14991 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14992 HelperValStmt = buildPreInits(Context, Captures); 14993 } 14994 } 14995 } 14996 14997 return new (Context) 14998 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 14999 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 15000 } 15001 15002 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 15003 SourceLocation StartLoc, 15004 SourceLocation EndLoc) { 15005 OMPClause *Res = nullptr; 15006 switch (Kind) { 15007 case OMPC_ordered: 15008 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 15009 break; 15010 case OMPC_nowait: 15011 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 15012 break; 15013 case OMPC_untied: 15014 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 15015 break; 15016 case OMPC_mergeable: 15017 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 15018 break; 15019 case OMPC_read: 15020 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 15021 break; 15022 case OMPC_write: 15023 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 15024 break; 15025 case OMPC_update: 15026 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 15027 break; 15028 case OMPC_capture: 15029 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 15030 break; 15031 case OMPC_seq_cst: 15032 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 15033 break; 15034 case OMPC_acq_rel: 15035 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 15036 break; 15037 case OMPC_acquire: 15038 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 15039 break; 15040 case OMPC_release: 15041 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 15042 break; 15043 case OMPC_relaxed: 15044 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 15045 break; 15046 case OMPC_threads: 15047 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 15048 break; 15049 case OMPC_simd: 15050 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 15051 break; 15052 case OMPC_nogroup: 15053 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 15054 break; 15055 case OMPC_unified_address: 15056 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 15057 break; 15058 case OMPC_unified_shared_memory: 15059 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15060 break; 15061 case OMPC_reverse_offload: 15062 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 15063 break; 15064 case OMPC_dynamic_allocators: 15065 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 15066 break; 15067 case OMPC_destroy: 15068 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc, 15069 /*LParenLoc=*/SourceLocation(), 15070 /*VarLoc=*/SourceLocation(), EndLoc); 15071 break; 15072 case OMPC_full: 15073 Res = ActOnOpenMPFullClause(StartLoc, EndLoc); 15074 break; 15075 case OMPC_partial: 15076 Res = ActOnOpenMPPartialClause(nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc); 15077 break; 15078 case OMPC_if: 15079 case OMPC_final: 15080 case OMPC_num_threads: 15081 case OMPC_safelen: 15082 case OMPC_simdlen: 15083 case OMPC_sizes: 15084 case OMPC_allocator: 15085 case OMPC_collapse: 15086 case OMPC_schedule: 15087 case OMPC_private: 15088 case OMPC_firstprivate: 15089 case OMPC_lastprivate: 15090 case OMPC_shared: 15091 case OMPC_reduction: 15092 case OMPC_task_reduction: 15093 case OMPC_in_reduction: 15094 case OMPC_linear: 15095 case OMPC_aligned: 15096 case OMPC_copyin: 15097 case OMPC_copyprivate: 15098 case OMPC_default: 15099 case OMPC_proc_bind: 15100 case OMPC_threadprivate: 15101 case OMPC_allocate: 15102 case OMPC_flush: 15103 case OMPC_depobj: 15104 case OMPC_depend: 15105 case OMPC_device: 15106 case OMPC_map: 15107 case OMPC_num_teams: 15108 case OMPC_thread_limit: 15109 case OMPC_priority: 15110 case OMPC_grainsize: 15111 case OMPC_num_tasks: 15112 case OMPC_hint: 15113 case OMPC_dist_schedule: 15114 case OMPC_defaultmap: 15115 case OMPC_unknown: 15116 case OMPC_uniform: 15117 case OMPC_to: 15118 case OMPC_from: 15119 case OMPC_use_device_ptr: 15120 case OMPC_use_device_addr: 15121 case OMPC_is_device_ptr: 15122 case OMPC_atomic_default_mem_order: 15123 case OMPC_device_type: 15124 case OMPC_match: 15125 case OMPC_nontemporal: 15126 case OMPC_order: 15127 case OMPC_novariants: 15128 case OMPC_nocontext: 15129 case OMPC_detach: 15130 case OMPC_inclusive: 15131 case OMPC_exclusive: 15132 case OMPC_uses_allocators: 15133 case OMPC_affinity: 15134 case OMPC_when: 15135 default: 15136 llvm_unreachable("Clause is not allowed."); 15137 } 15138 return Res; 15139 } 15140 15141 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 15142 SourceLocation EndLoc) { 15143 DSAStack->setNowaitRegion(); 15144 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 15145 } 15146 15147 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 15148 SourceLocation EndLoc) { 15149 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 15150 } 15151 15152 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 15153 SourceLocation EndLoc) { 15154 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 15155 } 15156 15157 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 15158 SourceLocation EndLoc) { 15159 return new (Context) OMPReadClause(StartLoc, EndLoc); 15160 } 15161 15162 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 15163 SourceLocation EndLoc) { 15164 return new (Context) OMPWriteClause(StartLoc, EndLoc); 15165 } 15166 15167 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 15168 SourceLocation EndLoc) { 15169 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 15170 } 15171 15172 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 15173 SourceLocation EndLoc) { 15174 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 15175 } 15176 15177 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 15178 SourceLocation EndLoc) { 15179 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 15180 } 15181 15182 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 15183 SourceLocation EndLoc) { 15184 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 15185 } 15186 15187 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 15188 SourceLocation EndLoc) { 15189 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 15190 } 15191 15192 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 15193 SourceLocation EndLoc) { 15194 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 15195 } 15196 15197 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 15198 SourceLocation EndLoc) { 15199 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 15200 } 15201 15202 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 15203 SourceLocation EndLoc) { 15204 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 15205 } 15206 15207 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 15208 SourceLocation EndLoc) { 15209 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 15210 } 15211 15212 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 15213 SourceLocation EndLoc) { 15214 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 15215 } 15216 15217 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 15218 SourceLocation EndLoc) { 15219 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 15220 } 15221 15222 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 15223 SourceLocation EndLoc) { 15224 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 15225 } 15226 15227 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 15228 SourceLocation EndLoc) { 15229 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 15230 } 15231 15232 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 15233 SourceLocation EndLoc) { 15234 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 15235 } 15236 15237 StmtResult Sema::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses, 15238 SourceLocation StartLoc, 15239 SourceLocation EndLoc) { 15240 15241 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15242 // At least one action-clause must appear on a directive. 15243 if (!hasClauses(Clauses, OMPC_init, OMPC_use, OMPC_destroy, OMPC_nowait)) { 15244 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'"; 15245 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 15246 << Expected << getOpenMPDirectiveName(OMPD_interop); 15247 return StmtError(); 15248 } 15249 15250 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15251 // A depend clause can only appear on the directive if a targetsync 15252 // interop-type is present or the interop-var was initialized with 15253 // the targetsync interop-type. 15254 15255 // If there is any 'init' clause diagnose if there is no 'init' clause with 15256 // interop-type of 'targetsync'. Cases involving other directives cannot be 15257 // diagnosed. 15258 const OMPDependClause *DependClause = nullptr; 15259 bool HasInitClause = false; 15260 bool IsTargetSync = false; 15261 for (const OMPClause *C : Clauses) { 15262 if (IsTargetSync) 15263 break; 15264 if (const auto *InitClause = dyn_cast<OMPInitClause>(C)) { 15265 HasInitClause = true; 15266 if (InitClause->getIsTargetSync()) 15267 IsTargetSync = true; 15268 } else if (const auto *DC = dyn_cast<OMPDependClause>(C)) { 15269 DependClause = DC; 15270 } 15271 } 15272 if (DependClause && HasInitClause && !IsTargetSync) { 15273 Diag(DependClause->getBeginLoc(), diag::err_omp_interop_bad_depend_clause); 15274 return StmtError(); 15275 } 15276 15277 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15278 // Each interop-var may be specified for at most one action-clause of each 15279 // interop construct. 15280 llvm::SmallPtrSet<const VarDecl *, 4> InteropVars; 15281 for (const OMPClause *C : Clauses) { 15282 OpenMPClauseKind ClauseKind = C->getClauseKind(); 15283 const DeclRefExpr *DRE = nullptr; 15284 SourceLocation VarLoc; 15285 15286 if (ClauseKind == OMPC_init) { 15287 const auto *IC = cast<OMPInitClause>(C); 15288 VarLoc = IC->getVarLoc(); 15289 DRE = dyn_cast_or_null<DeclRefExpr>(IC->getInteropVar()); 15290 } else if (ClauseKind == OMPC_use) { 15291 const auto *UC = cast<OMPUseClause>(C); 15292 VarLoc = UC->getVarLoc(); 15293 DRE = dyn_cast_or_null<DeclRefExpr>(UC->getInteropVar()); 15294 } else if (ClauseKind == OMPC_destroy) { 15295 const auto *DC = cast<OMPDestroyClause>(C); 15296 VarLoc = DC->getVarLoc(); 15297 DRE = dyn_cast_or_null<DeclRefExpr>(DC->getInteropVar()); 15298 } 15299 15300 if (!DRE) 15301 continue; 15302 15303 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 15304 if (!InteropVars.insert(VD->getCanonicalDecl()).second) { 15305 Diag(VarLoc, diag::err_omp_interop_var_multiple_actions) << VD; 15306 return StmtError(); 15307 } 15308 } 15309 } 15310 15311 return OMPInteropDirective::Create(Context, StartLoc, EndLoc, Clauses); 15312 } 15313 15314 static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr, 15315 SourceLocation VarLoc, 15316 OpenMPClauseKind Kind) { 15317 if (InteropVarExpr->isValueDependent() || InteropVarExpr->isTypeDependent() || 15318 InteropVarExpr->isInstantiationDependent() || 15319 InteropVarExpr->containsUnexpandedParameterPack()) 15320 return true; 15321 15322 const auto *DRE = dyn_cast<DeclRefExpr>(InteropVarExpr); 15323 if (!DRE || !isa<VarDecl>(DRE->getDecl())) { 15324 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) << 0; 15325 return false; 15326 } 15327 15328 // Interop variable should be of type omp_interop_t. 15329 bool HasError = false; 15330 QualType InteropType; 15331 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get("omp_interop_t"), 15332 VarLoc, Sema::LookupOrdinaryName); 15333 if (SemaRef.LookupName(Result, SemaRef.getCurScope())) { 15334 NamedDecl *ND = Result.getFoundDecl(); 15335 if (const auto *TD = dyn_cast<TypeDecl>(ND)) { 15336 InteropType = QualType(TD->getTypeForDecl(), 0); 15337 } else { 15338 HasError = true; 15339 } 15340 } else { 15341 HasError = true; 15342 } 15343 15344 if (HasError) { 15345 SemaRef.Diag(VarLoc, diag::err_omp_implied_type_not_found) 15346 << "omp_interop_t"; 15347 return false; 15348 } 15349 15350 QualType VarType = InteropVarExpr->getType().getUnqualifiedType(); 15351 if (!SemaRef.Context.hasSameType(InteropType, VarType)) { 15352 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_wrong_type); 15353 return false; 15354 } 15355 15356 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions] 15357 // The interop-var passed to init or destroy must be non-const. 15358 if ((Kind == OMPC_init || Kind == OMPC_destroy) && 15359 isConstNotMutableType(SemaRef, InteropVarExpr->getType())) { 15360 SemaRef.Diag(VarLoc, diag::err_omp_interop_variable_expected) 15361 << /*non-const*/ 1; 15362 return false; 15363 } 15364 return true; 15365 } 15366 15367 OMPClause * 15368 Sema::ActOnOpenMPInitClause(Expr *InteropVar, ArrayRef<Expr *> PrefExprs, 15369 bool IsTarget, bool IsTargetSync, 15370 SourceLocation StartLoc, SourceLocation LParenLoc, 15371 SourceLocation VarLoc, SourceLocation EndLoc) { 15372 15373 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_init)) 15374 return nullptr; 15375 15376 // Check prefer_type values. These foreign-runtime-id values are either 15377 // string literals or constant integral expressions. 15378 for (const Expr *E : PrefExprs) { 15379 if (E->isValueDependent() || E->isTypeDependent() || 15380 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 15381 continue; 15382 if (E->isIntegerConstantExpr(Context)) 15383 continue; 15384 if (isa<StringLiteral>(E)) 15385 continue; 15386 Diag(E->getExprLoc(), diag::err_omp_interop_prefer_type); 15387 return nullptr; 15388 } 15389 15390 return OMPInitClause::Create(Context, InteropVar, PrefExprs, IsTarget, 15391 IsTargetSync, StartLoc, LParenLoc, VarLoc, 15392 EndLoc); 15393 } 15394 15395 OMPClause *Sema::ActOnOpenMPUseClause(Expr *InteropVar, SourceLocation StartLoc, 15396 SourceLocation LParenLoc, 15397 SourceLocation VarLoc, 15398 SourceLocation EndLoc) { 15399 15400 if (!isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_use)) 15401 return nullptr; 15402 15403 return new (Context) 15404 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15405 } 15406 15407 OMPClause *Sema::ActOnOpenMPDestroyClause(Expr *InteropVar, 15408 SourceLocation StartLoc, 15409 SourceLocation LParenLoc, 15410 SourceLocation VarLoc, 15411 SourceLocation EndLoc) { 15412 if (InteropVar && 15413 !isValidInteropVariable(*this, InteropVar, VarLoc, OMPC_destroy)) 15414 return nullptr; 15415 15416 return new (Context) 15417 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc); 15418 } 15419 15420 OMPClause *Sema::ActOnOpenMPNovariantsClause(Expr *Condition, 15421 SourceLocation StartLoc, 15422 SourceLocation LParenLoc, 15423 SourceLocation EndLoc) { 15424 Expr *ValExpr = Condition; 15425 Stmt *HelperValStmt = nullptr; 15426 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15427 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15428 !Condition->isInstantiationDependent() && 15429 !Condition->containsUnexpandedParameterPack()) { 15430 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15431 if (Val.isInvalid()) 15432 return nullptr; 15433 15434 ValExpr = MakeFullExpr(Val.get()).get(); 15435 15436 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15437 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_novariants, 15438 LangOpts.OpenMP); 15439 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15440 ValExpr = MakeFullExpr(ValExpr).get(); 15441 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15442 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15443 HelperValStmt = buildPreInits(Context, Captures); 15444 } 15445 } 15446 15447 return new (Context) OMPNovariantsClause( 15448 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 15449 } 15450 15451 OMPClause *Sema::ActOnOpenMPNocontextClause(Expr *Condition, 15452 SourceLocation StartLoc, 15453 SourceLocation LParenLoc, 15454 SourceLocation EndLoc) { 15455 Expr *ValExpr = Condition; 15456 Stmt *HelperValStmt = nullptr; 15457 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 15458 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 15459 !Condition->isInstantiationDependent() && 15460 !Condition->containsUnexpandedParameterPack()) { 15461 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 15462 if (Val.isInvalid()) 15463 return nullptr; 15464 15465 ValExpr = MakeFullExpr(Val.get()).get(); 15466 15467 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15468 CaptureRegion = 15469 getOpenMPCaptureRegionForClause(DKind, OMPC_nocontext, LangOpts.OpenMP); 15470 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15471 ValExpr = MakeFullExpr(ValExpr).get(); 15472 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15473 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15474 HelperValStmt = buildPreInits(Context, Captures); 15475 } 15476 } 15477 15478 return new (Context) OMPNocontextClause(ValExpr, HelperValStmt, CaptureRegion, 15479 StartLoc, LParenLoc, EndLoc); 15480 } 15481 15482 OMPClause *Sema::ActOnOpenMPFilterClause(Expr *ThreadID, 15483 SourceLocation StartLoc, 15484 SourceLocation LParenLoc, 15485 SourceLocation EndLoc) { 15486 Expr *ValExpr = ThreadID; 15487 Stmt *HelperValStmt = nullptr; 15488 15489 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 15490 OpenMPDirectiveKind CaptureRegion = 15491 getOpenMPCaptureRegionForClause(DKind, OMPC_filter, LangOpts.OpenMP); 15492 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 15493 ValExpr = MakeFullExpr(ValExpr).get(); 15494 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 15495 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 15496 HelperValStmt = buildPreInits(Context, Captures); 15497 } 15498 15499 return new (Context) OMPFilterClause(ValExpr, HelperValStmt, CaptureRegion, 15500 StartLoc, LParenLoc, EndLoc); 15501 } 15502 15503 OMPClause *Sema::ActOnOpenMPVarListClause( 15504 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 15505 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 15506 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 15507 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 15508 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 15509 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 15510 SourceLocation ExtraModifierLoc, 15511 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 15512 ArrayRef<SourceLocation> MotionModifiersLoc) { 15513 SourceLocation StartLoc = Locs.StartLoc; 15514 SourceLocation LParenLoc = Locs.LParenLoc; 15515 SourceLocation EndLoc = Locs.EndLoc; 15516 OMPClause *Res = nullptr; 15517 switch (Kind) { 15518 case OMPC_private: 15519 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15520 break; 15521 case OMPC_firstprivate: 15522 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15523 break; 15524 case OMPC_lastprivate: 15525 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 15526 "Unexpected lastprivate modifier."); 15527 Res = ActOnOpenMPLastprivateClause( 15528 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 15529 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 15530 break; 15531 case OMPC_shared: 15532 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 15533 break; 15534 case OMPC_reduction: 15535 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 15536 "Unexpected lastprivate modifier."); 15537 Res = ActOnOpenMPReductionClause( 15538 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 15539 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 15540 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 15541 break; 15542 case OMPC_task_reduction: 15543 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15544 EndLoc, ReductionOrMapperIdScopeSpec, 15545 ReductionOrMapperId); 15546 break; 15547 case OMPC_in_reduction: 15548 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 15549 EndLoc, ReductionOrMapperIdScopeSpec, 15550 ReductionOrMapperId); 15551 break; 15552 case OMPC_linear: 15553 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 15554 "Unexpected linear modifier."); 15555 Res = ActOnOpenMPLinearClause( 15556 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 15557 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 15558 ColonLoc, EndLoc); 15559 break; 15560 case OMPC_aligned: 15561 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 15562 LParenLoc, ColonLoc, EndLoc); 15563 break; 15564 case OMPC_copyin: 15565 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 15566 break; 15567 case OMPC_copyprivate: 15568 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 15569 break; 15570 case OMPC_flush: 15571 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 15572 break; 15573 case OMPC_depend: 15574 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 15575 "Unexpected depend modifier."); 15576 Res = ActOnOpenMPDependClause( 15577 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 15578 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 15579 break; 15580 case OMPC_map: 15581 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 15582 "Unexpected map modifier."); 15583 Res = ActOnOpenMPMapClause( 15584 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 15585 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 15586 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 15587 break; 15588 case OMPC_to: 15589 Res = ActOnOpenMPToClause(MotionModifiers, MotionModifiersLoc, 15590 ReductionOrMapperIdScopeSpec, ReductionOrMapperId, 15591 ColonLoc, VarList, Locs); 15592 break; 15593 case OMPC_from: 15594 Res = ActOnOpenMPFromClause(MotionModifiers, MotionModifiersLoc, 15595 ReductionOrMapperIdScopeSpec, 15596 ReductionOrMapperId, ColonLoc, VarList, Locs); 15597 break; 15598 case OMPC_use_device_ptr: 15599 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 15600 break; 15601 case OMPC_use_device_addr: 15602 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 15603 break; 15604 case OMPC_is_device_ptr: 15605 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 15606 break; 15607 case OMPC_allocate: 15608 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 15609 LParenLoc, ColonLoc, EndLoc); 15610 break; 15611 case OMPC_nontemporal: 15612 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 15613 break; 15614 case OMPC_inclusive: 15615 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15616 break; 15617 case OMPC_exclusive: 15618 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 15619 break; 15620 case OMPC_affinity: 15621 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 15622 DepModOrTailExpr, VarList); 15623 break; 15624 case OMPC_if: 15625 case OMPC_depobj: 15626 case OMPC_final: 15627 case OMPC_num_threads: 15628 case OMPC_safelen: 15629 case OMPC_simdlen: 15630 case OMPC_sizes: 15631 case OMPC_allocator: 15632 case OMPC_collapse: 15633 case OMPC_default: 15634 case OMPC_proc_bind: 15635 case OMPC_schedule: 15636 case OMPC_ordered: 15637 case OMPC_nowait: 15638 case OMPC_untied: 15639 case OMPC_mergeable: 15640 case OMPC_threadprivate: 15641 case OMPC_read: 15642 case OMPC_write: 15643 case OMPC_update: 15644 case OMPC_capture: 15645 case OMPC_seq_cst: 15646 case OMPC_acq_rel: 15647 case OMPC_acquire: 15648 case OMPC_release: 15649 case OMPC_relaxed: 15650 case OMPC_device: 15651 case OMPC_threads: 15652 case OMPC_simd: 15653 case OMPC_num_teams: 15654 case OMPC_thread_limit: 15655 case OMPC_priority: 15656 case OMPC_grainsize: 15657 case OMPC_nogroup: 15658 case OMPC_num_tasks: 15659 case OMPC_hint: 15660 case OMPC_dist_schedule: 15661 case OMPC_defaultmap: 15662 case OMPC_unknown: 15663 case OMPC_uniform: 15664 case OMPC_unified_address: 15665 case OMPC_unified_shared_memory: 15666 case OMPC_reverse_offload: 15667 case OMPC_dynamic_allocators: 15668 case OMPC_atomic_default_mem_order: 15669 case OMPC_device_type: 15670 case OMPC_match: 15671 case OMPC_order: 15672 case OMPC_destroy: 15673 case OMPC_novariants: 15674 case OMPC_nocontext: 15675 case OMPC_detach: 15676 case OMPC_uses_allocators: 15677 case OMPC_when: 15678 default: 15679 llvm_unreachable("Clause is not allowed."); 15680 } 15681 return Res; 15682 } 15683 15684 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 15685 ExprObjectKind OK, SourceLocation Loc) { 15686 ExprResult Res = BuildDeclRefExpr( 15687 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 15688 if (!Res.isUsable()) 15689 return ExprError(); 15690 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 15691 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 15692 if (!Res.isUsable()) 15693 return ExprError(); 15694 } 15695 if (VK != VK_LValue && Res.get()->isGLValue()) { 15696 Res = DefaultLvalueConversion(Res.get()); 15697 if (!Res.isUsable()) 15698 return ExprError(); 15699 } 15700 return Res; 15701 } 15702 15703 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 15704 SourceLocation StartLoc, 15705 SourceLocation LParenLoc, 15706 SourceLocation EndLoc) { 15707 SmallVector<Expr *, 8> Vars; 15708 SmallVector<Expr *, 8> PrivateCopies; 15709 for (Expr *RefExpr : VarList) { 15710 assert(RefExpr && "NULL expr in OpenMP private clause."); 15711 SourceLocation ELoc; 15712 SourceRange ERange; 15713 Expr *SimpleRefExpr = RefExpr; 15714 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15715 if (Res.second) { 15716 // It will be analyzed later. 15717 Vars.push_back(RefExpr); 15718 PrivateCopies.push_back(nullptr); 15719 } 15720 ValueDecl *D = Res.first; 15721 if (!D) 15722 continue; 15723 15724 QualType Type = D->getType(); 15725 auto *VD = dyn_cast<VarDecl>(D); 15726 15727 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15728 // A variable that appears in a private clause must not have an incomplete 15729 // type or a reference type. 15730 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 15731 continue; 15732 Type = Type.getNonReferenceType(); 15733 15734 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15735 // A variable that is privatized must not have a const-qualified type 15736 // unless it is of class type with a mutable member. This restriction does 15737 // not apply to the firstprivate clause. 15738 // 15739 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 15740 // A variable that appears in a private clause must not have a 15741 // const-qualified type unless it is of class type with a mutable member. 15742 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 15743 continue; 15744 15745 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15746 // in a Construct] 15747 // Variables with the predetermined data-sharing attributes may not be 15748 // listed in data-sharing attributes clauses, except for the cases 15749 // listed below. For these exceptions only, listing a predetermined 15750 // variable in a data-sharing attribute clause is allowed and overrides 15751 // the variable's predetermined data-sharing attributes. 15752 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15753 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 15754 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15755 << getOpenMPClauseName(OMPC_private); 15756 reportOriginalDsa(*this, DSAStack, D, DVar); 15757 continue; 15758 } 15759 15760 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15761 // Variably modified types are not supported for tasks. 15762 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 15763 isOpenMPTaskingDirective(CurrDir)) { 15764 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 15765 << getOpenMPClauseName(OMPC_private) << Type 15766 << getOpenMPDirectiveName(CurrDir); 15767 bool IsDecl = 15768 !VD || 15769 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15770 Diag(D->getLocation(), 15771 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15772 << D; 15773 continue; 15774 } 15775 15776 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 15777 // A list item cannot appear in both a map clause and a data-sharing 15778 // attribute clause on the same construct 15779 // 15780 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 15781 // A list item cannot appear in both a map clause and a data-sharing 15782 // attribute clause on the same construct unless the construct is a 15783 // combined construct. 15784 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 15785 CurrDir == OMPD_target) { 15786 OpenMPClauseKind ConflictKind; 15787 if (DSAStack->checkMappableExprComponentListsForDecl( 15788 VD, /*CurrentRegionOnly=*/true, 15789 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 15790 OpenMPClauseKind WhereFoundClauseKind) -> bool { 15791 ConflictKind = WhereFoundClauseKind; 15792 return true; 15793 })) { 15794 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 15795 << getOpenMPClauseName(OMPC_private) 15796 << getOpenMPClauseName(ConflictKind) 15797 << getOpenMPDirectiveName(CurrDir); 15798 reportOriginalDsa(*this, DSAStack, D, DVar); 15799 continue; 15800 } 15801 } 15802 15803 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 15804 // A variable of class type (or array thereof) that appears in a private 15805 // clause requires an accessible, unambiguous default constructor for the 15806 // class type. 15807 // Generate helper private variable and initialize it with the default 15808 // value. The address of the original variable is replaced by the address of 15809 // the new private variable in CodeGen. This new variable is not added to 15810 // IdResolver, so the code in the OpenMP region uses original variable for 15811 // proper diagnostics. 15812 Type = Type.getUnqualifiedType(); 15813 VarDecl *VDPrivate = 15814 buildVarDecl(*this, ELoc, Type, D->getName(), 15815 D->hasAttrs() ? &D->getAttrs() : nullptr, 15816 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15817 ActOnUninitializedDecl(VDPrivate); 15818 if (VDPrivate->isInvalidDecl()) 15819 continue; 15820 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 15821 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 15822 15823 DeclRefExpr *Ref = nullptr; 15824 if (!VD && !CurContext->isDependentContext()) 15825 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15826 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 15827 Vars.push_back((VD || CurContext->isDependentContext()) 15828 ? RefExpr->IgnoreParens() 15829 : Ref); 15830 PrivateCopies.push_back(VDPrivateRefExpr); 15831 } 15832 15833 if (Vars.empty()) 15834 return nullptr; 15835 15836 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 15837 PrivateCopies); 15838 } 15839 15840 namespace { 15841 class DiagsUninitializedSeveretyRAII { 15842 private: 15843 DiagnosticsEngine &Diags; 15844 SourceLocation SavedLoc; 15845 bool IsIgnored = false; 15846 15847 public: 15848 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 15849 bool IsIgnored) 15850 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 15851 if (!IsIgnored) { 15852 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 15853 /*Map*/ diag::Severity::Ignored, Loc); 15854 } 15855 } 15856 ~DiagsUninitializedSeveretyRAII() { 15857 if (!IsIgnored) 15858 Diags.popMappings(SavedLoc); 15859 } 15860 }; 15861 } 15862 15863 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 15864 SourceLocation StartLoc, 15865 SourceLocation LParenLoc, 15866 SourceLocation EndLoc) { 15867 SmallVector<Expr *, 8> Vars; 15868 SmallVector<Expr *, 8> PrivateCopies; 15869 SmallVector<Expr *, 8> Inits; 15870 SmallVector<Decl *, 4> ExprCaptures; 15871 bool IsImplicitClause = 15872 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 15873 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 15874 15875 for (Expr *RefExpr : VarList) { 15876 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 15877 SourceLocation ELoc; 15878 SourceRange ERange; 15879 Expr *SimpleRefExpr = RefExpr; 15880 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15881 if (Res.second) { 15882 // It will be analyzed later. 15883 Vars.push_back(RefExpr); 15884 PrivateCopies.push_back(nullptr); 15885 Inits.push_back(nullptr); 15886 } 15887 ValueDecl *D = Res.first; 15888 if (!D) 15889 continue; 15890 15891 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 15892 QualType Type = D->getType(); 15893 auto *VD = dyn_cast<VarDecl>(D); 15894 15895 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 15896 // A variable that appears in a private clause must not have an incomplete 15897 // type or a reference type. 15898 if (RequireCompleteType(ELoc, Type, 15899 diag::err_omp_firstprivate_incomplete_type)) 15900 continue; 15901 Type = Type.getNonReferenceType(); 15902 15903 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 15904 // A variable of class type (or array thereof) that appears in a private 15905 // clause requires an accessible, unambiguous copy constructor for the 15906 // class type. 15907 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 15908 15909 // If an implicit firstprivate variable found it was checked already. 15910 DSAStackTy::DSAVarData TopDVar; 15911 if (!IsImplicitClause) { 15912 DSAStackTy::DSAVarData DVar = 15913 DSAStack->getTopDSA(D, /*FromParent=*/false); 15914 TopDVar = DVar; 15915 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 15916 bool IsConstant = ElemType.isConstant(Context); 15917 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 15918 // A list item that specifies a given variable may not appear in more 15919 // than one clause on the same directive, except that a variable may be 15920 // specified in both firstprivate and lastprivate clauses. 15921 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 15922 // A list item may appear in a firstprivate or lastprivate clause but not 15923 // both. 15924 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 15925 (isOpenMPDistributeDirective(CurrDir) || 15926 DVar.CKind != OMPC_lastprivate) && 15927 DVar.RefExpr) { 15928 Diag(ELoc, diag::err_omp_wrong_dsa) 15929 << getOpenMPClauseName(DVar.CKind) 15930 << getOpenMPClauseName(OMPC_firstprivate); 15931 reportOriginalDsa(*this, DSAStack, D, DVar); 15932 continue; 15933 } 15934 15935 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15936 // in a Construct] 15937 // Variables with the predetermined data-sharing attributes may not be 15938 // listed in data-sharing attributes clauses, except for the cases 15939 // listed below. For these exceptions only, listing a predetermined 15940 // variable in a data-sharing attribute clause is allowed and overrides 15941 // the variable's predetermined data-sharing attributes. 15942 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 15943 // in a Construct, C/C++, p.2] 15944 // Variables with const-qualified type having no mutable member may be 15945 // listed in a firstprivate clause, even if they are static data members. 15946 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 15947 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 15948 Diag(ELoc, diag::err_omp_wrong_dsa) 15949 << getOpenMPClauseName(DVar.CKind) 15950 << getOpenMPClauseName(OMPC_firstprivate); 15951 reportOriginalDsa(*this, DSAStack, D, DVar); 15952 continue; 15953 } 15954 15955 // OpenMP [2.9.3.4, Restrictions, p.2] 15956 // A list item that is private within a parallel region must not appear 15957 // in a firstprivate clause on a worksharing construct if any of the 15958 // worksharing regions arising from the worksharing construct ever bind 15959 // to any of the parallel regions arising from the parallel construct. 15960 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15961 // A list item that is private within a teams region must not appear in a 15962 // firstprivate clause on a distribute construct if any of the distribute 15963 // regions arising from the distribute construct ever bind to any of the 15964 // teams regions arising from the teams construct. 15965 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 15966 // A list item that appears in a reduction clause of a teams construct 15967 // must not appear in a firstprivate clause on a distribute construct if 15968 // any of the distribute regions arising from the distribute construct 15969 // ever bind to any of the teams regions arising from the teams construct. 15970 if ((isOpenMPWorksharingDirective(CurrDir) || 15971 isOpenMPDistributeDirective(CurrDir)) && 15972 !isOpenMPParallelDirective(CurrDir) && 15973 !isOpenMPTeamsDirective(CurrDir)) { 15974 DVar = DSAStack->getImplicitDSA(D, true); 15975 if (DVar.CKind != OMPC_shared && 15976 (isOpenMPParallelDirective(DVar.DKind) || 15977 isOpenMPTeamsDirective(DVar.DKind) || 15978 DVar.DKind == OMPD_unknown)) { 15979 Diag(ELoc, diag::err_omp_required_access) 15980 << getOpenMPClauseName(OMPC_firstprivate) 15981 << getOpenMPClauseName(OMPC_shared); 15982 reportOriginalDsa(*this, DSAStack, D, DVar); 15983 continue; 15984 } 15985 } 15986 // OpenMP [2.9.3.4, Restrictions, p.3] 15987 // A list item that appears in a reduction clause of a parallel construct 15988 // must not appear in a firstprivate clause on a worksharing or task 15989 // construct if any of the worksharing or task regions arising from the 15990 // worksharing or task construct ever bind to any of the parallel regions 15991 // arising from the parallel construct. 15992 // OpenMP [2.9.3.4, Restrictions, p.4] 15993 // A list item that appears in a reduction clause in worksharing 15994 // construct must not appear in a firstprivate clause in a task construct 15995 // encountered during execution of any of the worksharing regions arising 15996 // from the worksharing construct. 15997 if (isOpenMPTaskingDirective(CurrDir)) { 15998 DVar = DSAStack->hasInnermostDSA( 15999 D, 16000 [](OpenMPClauseKind C, bool AppliedToPointee) { 16001 return C == OMPC_reduction && !AppliedToPointee; 16002 }, 16003 [](OpenMPDirectiveKind K) { 16004 return isOpenMPParallelDirective(K) || 16005 isOpenMPWorksharingDirective(K) || 16006 isOpenMPTeamsDirective(K); 16007 }, 16008 /*FromParent=*/true); 16009 if (DVar.CKind == OMPC_reduction && 16010 (isOpenMPParallelDirective(DVar.DKind) || 16011 isOpenMPWorksharingDirective(DVar.DKind) || 16012 isOpenMPTeamsDirective(DVar.DKind))) { 16013 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 16014 << getOpenMPDirectiveName(DVar.DKind); 16015 reportOriginalDsa(*this, DSAStack, D, DVar); 16016 continue; 16017 } 16018 } 16019 16020 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 16021 // A list item cannot appear in both a map clause and a data-sharing 16022 // attribute clause on the same construct 16023 // 16024 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 16025 // A list item cannot appear in both a map clause and a data-sharing 16026 // attribute clause on the same construct unless the construct is a 16027 // combined construct. 16028 if ((LangOpts.OpenMP <= 45 && 16029 isOpenMPTargetExecutionDirective(CurrDir)) || 16030 CurrDir == OMPD_target) { 16031 OpenMPClauseKind ConflictKind; 16032 if (DSAStack->checkMappableExprComponentListsForDecl( 16033 VD, /*CurrentRegionOnly=*/true, 16034 [&ConflictKind]( 16035 OMPClauseMappableExprCommon::MappableExprComponentListRef, 16036 OpenMPClauseKind WhereFoundClauseKind) { 16037 ConflictKind = WhereFoundClauseKind; 16038 return true; 16039 })) { 16040 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 16041 << getOpenMPClauseName(OMPC_firstprivate) 16042 << getOpenMPClauseName(ConflictKind) 16043 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16044 reportOriginalDsa(*this, DSAStack, D, DVar); 16045 continue; 16046 } 16047 } 16048 } 16049 16050 // Variably modified types are not supported for tasks. 16051 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 16052 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 16053 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16054 << getOpenMPClauseName(OMPC_firstprivate) << Type 16055 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16056 bool IsDecl = 16057 !VD || 16058 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16059 Diag(D->getLocation(), 16060 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16061 << D; 16062 continue; 16063 } 16064 16065 Type = Type.getUnqualifiedType(); 16066 VarDecl *VDPrivate = 16067 buildVarDecl(*this, ELoc, Type, D->getName(), 16068 D->hasAttrs() ? &D->getAttrs() : nullptr, 16069 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 16070 // Generate helper private variable and initialize it with the value of the 16071 // original variable. The address of the original variable is replaced by 16072 // the address of the new private variable in the CodeGen. This new variable 16073 // is not added to IdResolver, so the code in the OpenMP region uses 16074 // original variable for proper diagnostics and variable capturing. 16075 Expr *VDInitRefExpr = nullptr; 16076 // For arrays generate initializer for single element and replace it by the 16077 // original array element in CodeGen. 16078 if (Type->isArrayType()) { 16079 VarDecl *VDInit = 16080 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 16081 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 16082 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 16083 ElemType = ElemType.getUnqualifiedType(); 16084 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 16085 ".firstprivate.temp"); 16086 InitializedEntity Entity = 16087 InitializedEntity::InitializeVariable(VDInitTemp); 16088 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 16089 16090 InitializationSequence InitSeq(*this, Entity, Kind, Init); 16091 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 16092 if (Result.isInvalid()) 16093 VDPrivate->setInvalidDecl(); 16094 else 16095 VDPrivate->setInit(Result.getAs<Expr>()); 16096 // Remove temp variable declaration. 16097 Context.Deallocate(VDInitTemp); 16098 } else { 16099 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 16100 ".firstprivate.temp"); 16101 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 16102 RefExpr->getExprLoc()); 16103 AddInitializerToDecl(VDPrivate, 16104 DefaultLvalueConversion(VDInitRefExpr).get(), 16105 /*DirectInit=*/false); 16106 } 16107 if (VDPrivate->isInvalidDecl()) { 16108 if (IsImplicitClause) { 16109 Diag(RefExpr->getExprLoc(), 16110 diag::note_omp_task_predetermined_firstprivate_here); 16111 } 16112 continue; 16113 } 16114 CurContext->addDecl(VDPrivate); 16115 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 16116 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 16117 RefExpr->getExprLoc()); 16118 DeclRefExpr *Ref = nullptr; 16119 if (!VD && !CurContext->isDependentContext()) { 16120 if (TopDVar.CKind == OMPC_lastprivate) { 16121 Ref = TopDVar.PrivateCopy; 16122 } else { 16123 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16124 if (!isOpenMPCapturedDecl(D)) 16125 ExprCaptures.push_back(Ref->getDecl()); 16126 } 16127 } 16128 if (!IsImplicitClause) 16129 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 16130 Vars.push_back((VD || CurContext->isDependentContext()) 16131 ? RefExpr->IgnoreParens() 16132 : Ref); 16133 PrivateCopies.push_back(VDPrivateRefExpr); 16134 Inits.push_back(VDInitRefExpr); 16135 } 16136 16137 if (Vars.empty()) 16138 return nullptr; 16139 16140 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16141 Vars, PrivateCopies, Inits, 16142 buildPreInits(Context, ExprCaptures)); 16143 } 16144 16145 OMPClause *Sema::ActOnOpenMPLastprivateClause( 16146 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 16147 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 16148 SourceLocation LParenLoc, SourceLocation EndLoc) { 16149 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 16150 assert(ColonLoc.isValid() && "Colon location must be valid."); 16151 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 16152 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 16153 /*Last=*/OMPC_LASTPRIVATE_unknown) 16154 << getOpenMPClauseName(OMPC_lastprivate); 16155 return nullptr; 16156 } 16157 16158 SmallVector<Expr *, 8> Vars; 16159 SmallVector<Expr *, 8> SrcExprs; 16160 SmallVector<Expr *, 8> DstExprs; 16161 SmallVector<Expr *, 8> AssignmentOps; 16162 SmallVector<Decl *, 4> ExprCaptures; 16163 SmallVector<Expr *, 4> ExprPostUpdates; 16164 for (Expr *RefExpr : VarList) { 16165 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16166 SourceLocation ELoc; 16167 SourceRange ERange; 16168 Expr *SimpleRefExpr = RefExpr; 16169 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16170 if (Res.second) { 16171 // It will be analyzed later. 16172 Vars.push_back(RefExpr); 16173 SrcExprs.push_back(nullptr); 16174 DstExprs.push_back(nullptr); 16175 AssignmentOps.push_back(nullptr); 16176 } 16177 ValueDecl *D = Res.first; 16178 if (!D) 16179 continue; 16180 16181 QualType Type = D->getType(); 16182 auto *VD = dyn_cast<VarDecl>(D); 16183 16184 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 16185 // A variable that appears in a lastprivate clause must not have an 16186 // incomplete type or a reference type. 16187 if (RequireCompleteType(ELoc, Type, 16188 diag::err_omp_lastprivate_incomplete_type)) 16189 continue; 16190 Type = Type.getNonReferenceType(); 16191 16192 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 16193 // A variable that is privatized must not have a const-qualified type 16194 // unless it is of class type with a mutable member. This restriction does 16195 // not apply to the firstprivate clause. 16196 // 16197 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 16198 // A variable that appears in a lastprivate clause must not have a 16199 // const-qualified type unless it is of class type with a mutable member. 16200 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 16201 continue; 16202 16203 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 16204 // A list item that appears in a lastprivate clause with the conditional 16205 // modifier must be a scalar variable. 16206 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 16207 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 16208 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 16209 VarDecl::DeclarationOnly; 16210 Diag(D->getLocation(), 16211 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16212 << D; 16213 continue; 16214 } 16215 16216 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 16217 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 16218 // in a Construct] 16219 // Variables with the predetermined data-sharing attributes may not be 16220 // listed in data-sharing attributes clauses, except for the cases 16221 // listed below. 16222 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 16223 // A list item may appear in a firstprivate or lastprivate clause but not 16224 // both. 16225 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16226 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 16227 (isOpenMPDistributeDirective(CurrDir) || 16228 DVar.CKind != OMPC_firstprivate) && 16229 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 16230 Diag(ELoc, diag::err_omp_wrong_dsa) 16231 << getOpenMPClauseName(DVar.CKind) 16232 << getOpenMPClauseName(OMPC_lastprivate); 16233 reportOriginalDsa(*this, DSAStack, D, DVar); 16234 continue; 16235 } 16236 16237 // OpenMP [2.14.3.5, Restrictions, p.2] 16238 // A list item that is private within a parallel region, or that appears in 16239 // the reduction clause of a parallel construct, must not appear in a 16240 // lastprivate clause on a worksharing construct if any of the corresponding 16241 // worksharing regions ever binds to any of the corresponding parallel 16242 // regions. 16243 DSAStackTy::DSAVarData TopDVar = DVar; 16244 if (isOpenMPWorksharingDirective(CurrDir) && 16245 !isOpenMPParallelDirective(CurrDir) && 16246 !isOpenMPTeamsDirective(CurrDir)) { 16247 DVar = DSAStack->getImplicitDSA(D, true); 16248 if (DVar.CKind != OMPC_shared) { 16249 Diag(ELoc, diag::err_omp_required_access) 16250 << getOpenMPClauseName(OMPC_lastprivate) 16251 << getOpenMPClauseName(OMPC_shared); 16252 reportOriginalDsa(*this, DSAStack, D, DVar); 16253 continue; 16254 } 16255 } 16256 16257 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 16258 // A variable of class type (or array thereof) that appears in a 16259 // lastprivate clause requires an accessible, unambiguous default 16260 // constructor for the class type, unless the list item is also specified 16261 // in a firstprivate clause. 16262 // A variable of class type (or array thereof) that appears in a 16263 // lastprivate clause requires an accessible, unambiguous copy assignment 16264 // operator for the class type. 16265 Type = Context.getBaseElementType(Type).getNonReferenceType(); 16266 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 16267 Type.getUnqualifiedType(), ".lastprivate.src", 16268 D->hasAttrs() ? &D->getAttrs() : nullptr); 16269 DeclRefExpr *PseudoSrcExpr = 16270 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 16271 VarDecl *DstVD = 16272 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 16273 D->hasAttrs() ? &D->getAttrs() : nullptr); 16274 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16275 // For arrays generate assignment operation for single element and replace 16276 // it by the original array element in CodeGen. 16277 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 16278 PseudoDstExpr, PseudoSrcExpr); 16279 if (AssignmentOp.isInvalid()) 16280 continue; 16281 AssignmentOp = 16282 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16283 if (AssignmentOp.isInvalid()) 16284 continue; 16285 16286 DeclRefExpr *Ref = nullptr; 16287 if (!VD && !CurContext->isDependentContext()) { 16288 if (TopDVar.CKind == OMPC_firstprivate) { 16289 Ref = TopDVar.PrivateCopy; 16290 } else { 16291 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 16292 if (!isOpenMPCapturedDecl(D)) 16293 ExprCaptures.push_back(Ref->getDecl()); 16294 } 16295 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) || 16296 (!isOpenMPCapturedDecl(D) && 16297 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 16298 ExprResult RefRes = DefaultLvalueConversion(Ref); 16299 if (!RefRes.isUsable()) 16300 continue; 16301 ExprResult PostUpdateRes = 16302 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 16303 RefRes.get()); 16304 if (!PostUpdateRes.isUsable()) 16305 continue; 16306 ExprPostUpdates.push_back( 16307 IgnoredValueConversions(PostUpdateRes.get()).get()); 16308 } 16309 } 16310 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 16311 Vars.push_back((VD || CurContext->isDependentContext()) 16312 ? RefExpr->IgnoreParens() 16313 : Ref); 16314 SrcExprs.push_back(PseudoSrcExpr); 16315 DstExprs.push_back(PseudoDstExpr); 16316 AssignmentOps.push_back(AssignmentOp.get()); 16317 } 16318 16319 if (Vars.empty()) 16320 return nullptr; 16321 16322 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16323 Vars, SrcExprs, DstExprs, AssignmentOps, 16324 LPKind, LPKindLoc, ColonLoc, 16325 buildPreInits(Context, ExprCaptures), 16326 buildPostUpdate(*this, ExprPostUpdates)); 16327 } 16328 16329 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 16330 SourceLocation StartLoc, 16331 SourceLocation LParenLoc, 16332 SourceLocation EndLoc) { 16333 SmallVector<Expr *, 8> Vars; 16334 for (Expr *RefExpr : VarList) { 16335 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 16336 SourceLocation ELoc; 16337 SourceRange ERange; 16338 Expr *SimpleRefExpr = RefExpr; 16339 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16340 if (Res.second) { 16341 // It will be analyzed later. 16342 Vars.push_back(RefExpr); 16343 } 16344 ValueDecl *D = Res.first; 16345 if (!D) 16346 continue; 16347 16348 auto *VD = dyn_cast<VarDecl>(D); 16349 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 16350 // in a Construct] 16351 // Variables with the predetermined data-sharing attributes may not be 16352 // listed in data-sharing attributes clauses, except for the cases 16353 // listed below. For these exceptions only, listing a predetermined 16354 // variable in a data-sharing attribute clause is allowed and overrides 16355 // the variable's predetermined data-sharing attributes. 16356 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 16357 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 16358 DVar.RefExpr) { 16359 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 16360 << getOpenMPClauseName(OMPC_shared); 16361 reportOriginalDsa(*this, DSAStack, D, DVar); 16362 continue; 16363 } 16364 16365 DeclRefExpr *Ref = nullptr; 16366 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 16367 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 16368 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 16369 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 16370 ? RefExpr->IgnoreParens() 16371 : Ref); 16372 } 16373 16374 if (Vars.empty()) 16375 return nullptr; 16376 16377 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 16378 } 16379 16380 namespace { 16381 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 16382 DSAStackTy *Stack; 16383 16384 public: 16385 bool VisitDeclRefExpr(DeclRefExpr *E) { 16386 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 16387 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 16388 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 16389 return false; 16390 if (DVar.CKind != OMPC_unknown) 16391 return true; 16392 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 16393 VD, 16394 [](OpenMPClauseKind C, bool AppliedToPointee) { 16395 return isOpenMPPrivate(C) && !AppliedToPointee; 16396 }, 16397 [](OpenMPDirectiveKind) { return true; }, 16398 /*FromParent=*/true); 16399 return DVarPrivate.CKind != OMPC_unknown; 16400 } 16401 return false; 16402 } 16403 bool VisitStmt(Stmt *S) { 16404 for (Stmt *Child : S->children()) { 16405 if (Child && Visit(Child)) 16406 return true; 16407 } 16408 return false; 16409 } 16410 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 16411 }; 16412 } // namespace 16413 16414 namespace { 16415 // Transform MemberExpression for specified FieldDecl of current class to 16416 // DeclRefExpr to specified OMPCapturedExprDecl. 16417 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 16418 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 16419 ValueDecl *Field = nullptr; 16420 DeclRefExpr *CapturedExpr = nullptr; 16421 16422 public: 16423 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 16424 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 16425 16426 ExprResult TransformMemberExpr(MemberExpr *E) { 16427 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 16428 E->getMemberDecl() == Field) { 16429 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 16430 return CapturedExpr; 16431 } 16432 return BaseTransform::TransformMemberExpr(E); 16433 } 16434 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 16435 }; 16436 } // namespace 16437 16438 template <typename T, typename U> 16439 static T filterLookupForUDReductionAndMapper( 16440 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 16441 for (U &Set : Lookups) { 16442 for (auto *D : Set) { 16443 if (T Res = Gen(cast<ValueDecl>(D))) 16444 return Res; 16445 } 16446 } 16447 return T(); 16448 } 16449 16450 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 16451 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 16452 16453 for (auto RD : D->redecls()) { 16454 // Don't bother with extra checks if we already know this one isn't visible. 16455 if (RD == D) 16456 continue; 16457 16458 auto ND = cast<NamedDecl>(RD); 16459 if (LookupResult::isVisible(SemaRef, ND)) 16460 return ND; 16461 } 16462 16463 return nullptr; 16464 } 16465 16466 static void 16467 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 16468 SourceLocation Loc, QualType Ty, 16469 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 16470 // Find all of the associated namespaces and classes based on the 16471 // arguments we have. 16472 Sema::AssociatedNamespaceSet AssociatedNamespaces; 16473 Sema::AssociatedClassSet AssociatedClasses; 16474 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 16475 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 16476 AssociatedClasses); 16477 16478 // C++ [basic.lookup.argdep]p3: 16479 // Let X be the lookup set produced by unqualified lookup (3.4.1) 16480 // and let Y be the lookup set produced by argument dependent 16481 // lookup (defined as follows). If X contains [...] then Y is 16482 // empty. Otherwise Y is the set of declarations found in the 16483 // namespaces associated with the argument types as described 16484 // below. The set of declarations found by the lookup of the name 16485 // is the union of X and Y. 16486 // 16487 // Here, we compute Y and add its members to the overloaded 16488 // candidate set. 16489 for (auto *NS : AssociatedNamespaces) { 16490 // When considering an associated namespace, the lookup is the 16491 // same as the lookup performed when the associated namespace is 16492 // used as a qualifier (3.4.3.2) except that: 16493 // 16494 // -- Any using-directives in the associated namespace are 16495 // ignored. 16496 // 16497 // -- Any namespace-scope friend functions declared in 16498 // associated classes are visible within their respective 16499 // namespaces even if they are not visible during an ordinary 16500 // lookup (11.4). 16501 DeclContext::lookup_result R = NS->lookup(Id.getName()); 16502 for (auto *D : R) { 16503 auto *Underlying = D; 16504 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16505 Underlying = USD->getTargetDecl(); 16506 16507 if (!isa<OMPDeclareReductionDecl>(Underlying) && 16508 !isa<OMPDeclareMapperDecl>(Underlying)) 16509 continue; 16510 16511 if (!SemaRef.isVisible(D)) { 16512 D = findAcceptableDecl(SemaRef, D); 16513 if (!D) 16514 continue; 16515 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 16516 Underlying = USD->getTargetDecl(); 16517 } 16518 Lookups.emplace_back(); 16519 Lookups.back().addDecl(Underlying); 16520 } 16521 } 16522 } 16523 16524 static ExprResult 16525 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 16526 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 16527 const DeclarationNameInfo &ReductionId, QualType Ty, 16528 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 16529 if (ReductionIdScopeSpec.isInvalid()) 16530 return ExprError(); 16531 SmallVector<UnresolvedSet<8>, 4> Lookups; 16532 if (S) { 16533 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16534 Lookup.suppressDiagnostics(); 16535 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 16536 NamedDecl *D = Lookup.getRepresentativeDecl(); 16537 do { 16538 S = S->getParent(); 16539 } while (S && !S->isDeclScope(D)); 16540 if (S) 16541 S = S->getParent(); 16542 Lookups.emplace_back(); 16543 Lookups.back().append(Lookup.begin(), Lookup.end()); 16544 Lookup.clear(); 16545 } 16546 } else if (auto *ULE = 16547 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 16548 Lookups.push_back(UnresolvedSet<8>()); 16549 Decl *PrevD = nullptr; 16550 for (NamedDecl *D : ULE->decls()) { 16551 if (D == PrevD) 16552 Lookups.push_back(UnresolvedSet<8>()); 16553 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 16554 Lookups.back().addDecl(DRD); 16555 PrevD = D; 16556 } 16557 } 16558 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 16559 Ty->isInstantiationDependentType() || 16560 Ty->containsUnexpandedParameterPack() || 16561 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 16562 return !D->isInvalidDecl() && 16563 (D->getType()->isDependentType() || 16564 D->getType()->isInstantiationDependentType() || 16565 D->getType()->containsUnexpandedParameterPack()); 16566 })) { 16567 UnresolvedSet<8> ResSet; 16568 for (const UnresolvedSet<8> &Set : Lookups) { 16569 if (Set.empty()) 16570 continue; 16571 ResSet.append(Set.begin(), Set.end()); 16572 // The last item marks the end of all declarations at the specified scope. 16573 ResSet.addDecl(Set[Set.size() - 1]); 16574 } 16575 return UnresolvedLookupExpr::Create( 16576 SemaRef.Context, /*NamingClass=*/nullptr, 16577 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 16578 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 16579 } 16580 // Lookup inside the classes. 16581 // C++ [over.match.oper]p3: 16582 // For a unary operator @ with an operand of a type whose 16583 // cv-unqualified version is T1, and for a binary operator @ with 16584 // a left operand of a type whose cv-unqualified version is T1 and 16585 // a right operand of a type whose cv-unqualified version is T2, 16586 // three sets of candidate functions, designated member 16587 // candidates, non-member candidates and built-in candidates, are 16588 // constructed as follows: 16589 // -- If T1 is a complete class type or a class currently being 16590 // defined, the set of member candidates is the result of the 16591 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 16592 // the set of member candidates is empty. 16593 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 16594 Lookup.suppressDiagnostics(); 16595 if (const auto *TyRec = Ty->getAs<RecordType>()) { 16596 // Complete the type if it can be completed. 16597 // If the type is neither complete nor being defined, bail out now. 16598 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 16599 TyRec->getDecl()->getDefinition()) { 16600 Lookup.clear(); 16601 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 16602 if (Lookup.empty()) { 16603 Lookups.emplace_back(); 16604 Lookups.back().append(Lookup.begin(), Lookup.end()); 16605 } 16606 } 16607 } 16608 // Perform ADL. 16609 if (SemaRef.getLangOpts().CPlusPlus) 16610 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 16611 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16612 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 16613 if (!D->isInvalidDecl() && 16614 SemaRef.Context.hasSameType(D->getType(), Ty)) 16615 return D; 16616 return nullptr; 16617 })) 16618 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 16619 VK_LValue, Loc); 16620 if (SemaRef.getLangOpts().CPlusPlus) { 16621 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 16622 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 16623 if (!D->isInvalidDecl() && 16624 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 16625 !Ty.isMoreQualifiedThan(D->getType())) 16626 return D; 16627 return nullptr; 16628 })) { 16629 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 16630 /*DetectVirtual=*/false); 16631 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 16632 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 16633 VD->getType().getUnqualifiedType()))) { 16634 if (SemaRef.CheckBaseClassAccess( 16635 Loc, VD->getType(), Ty, Paths.front(), 16636 /*DiagID=*/0) != Sema::AR_inaccessible) { 16637 SemaRef.BuildBasePathArray(Paths, BasePath); 16638 return SemaRef.BuildDeclRefExpr( 16639 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 16640 } 16641 } 16642 } 16643 } 16644 } 16645 if (ReductionIdScopeSpec.isSet()) { 16646 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 16647 << Ty << Range; 16648 return ExprError(); 16649 } 16650 return ExprEmpty(); 16651 } 16652 16653 namespace { 16654 /// Data for the reduction-based clauses. 16655 struct ReductionData { 16656 /// List of original reduction items. 16657 SmallVector<Expr *, 8> Vars; 16658 /// List of private copies of the reduction items. 16659 SmallVector<Expr *, 8> Privates; 16660 /// LHS expressions for the reduction_op expressions. 16661 SmallVector<Expr *, 8> LHSs; 16662 /// RHS expressions for the reduction_op expressions. 16663 SmallVector<Expr *, 8> RHSs; 16664 /// Reduction operation expression. 16665 SmallVector<Expr *, 8> ReductionOps; 16666 /// inscan copy operation expressions. 16667 SmallVector<Expr *, 8> InscanCopyOps; 16668 /// inscan copy temp array expressions for prefix sums. 16669 SmallVector<Expr *, 8> InscanCopyArrayTemps; 16670 /// inscan copy temp array element expressions for prefix sums. 16671 SmallVector<Expr *, 8> InscanCopyArrayElems; 16672 /// Taskgroup descriptors for the corresponding reduction items in 16673 /// in_reduction clauses. 16674 SmallVector<Expr *, 8> TaskgroupDescriptors; 16675 /// List of captures for clause. 16676 SmallVector<Decl *, 4> ExprCaptures; 16677 /// List of postupdate expressions. 16678 SmallVector<Expr *, 4> ExprPostUpdates; 16679 /// Reduction modifier. 16680 unsigned RedModifier = 0; 16681 ReductionData() = delete; 16682 /// Reserves required memory for the reduction data. 16683 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 16684 Vars.reserve(Size); 16685 Privates.reserve(Size); 16686 LHSs.reserve(Size); 16687 RHSs.reserve(Size); 16688 ReductionOps.reserve(Size); 16689 if (RedModifier == OMPC_REDUCTION_inscan) { 16690 InscanCopyOps.reserve(Size); 16691 InscanCopyArrayTemps.reserve(Size); 16692 InscanCopyArrayElems.reserve(Size); 16693 } 16694 TaskgroupDescriptors.reserve(Size); 16695 ExprCaptures.reserve(Size); 16696 ExprPostUpdates.reserve(Size); 16697 } 16698 /// Stores reduction item and reduction operation only (required for dependent 16699 /// reduction item). 16700 void push(Expr *Item, Expr *ReductionOp) { 16701 Vars.emplace_back(Item); 16702 Privates.emplace_back(nullptr); 16703 LHSs.emplace_back(nullptr); 16704 RHSs.emplace_back(nullptr); 16705 ReductionOps.emplace_back(ReductionOp); 16706 TaskgroupDescriptors.emplace_back(nullptr); 16707 if (RedModifier == OMPC_REDUCTION_inscan) { 16708 InscanCopyOps.push_back(nullptr); 16709 InscanCopyArrayTemps.push_back(nullptr); 16710 InscanCopyArrayElems.push_back(nullptr); 16711 } 16712 } 16713 /// Stores reduction data. 16714 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 16715 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 16716 Expr *CopyArrayElem) { 16717 Vars.emplace_back(Item); 16718 Privates.emplace_back(Private); 16719 LHSs.emplace_back(LHS); 16720 RHSs.emplace_back(RHS); 16721 ReductionOps.emplace_back(ReductionOp); 16722 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 16723 if (RedModifier == OMPC_REDUCTION_inscan) { 16724 InscanCopyOps.push_back(CopyOp); 16725 InscanCopyArrayTemps.push_back(CopyArrayTemp); 16726 InscanCopyArrayElems.push_back(CopyArrayElem); 16727 } else { 16728 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 16729 CopyArrayElem == nullptr && 16730 "Copy operation must be used for inscan reductions only."); 16731 } 16732 } 16733 }; 16734 } // namespace 16735 16736 static bool checkOMPArraySectionConstantForReduction( 16737 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 16738 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 16739 const Expr *Length = OASE->getLength(); 16740 if (Length == nullptr) { 16741 // For array sections of the form [1:] or [:], we would need to analyze 16742 // the lower bound... 16743 if (OASE->getColonLocFirst().isValid()) 16744 return false; 16745 16746 // This is an array subscript which has implicit length 1! 16747 SingleElement = true; 16748 ArraySizes.push_back(llvm::APSInt::get(1)); 16749 } else { 16750 Expr::EvalResult Result; 16751 if (!Length->EvaluateAsInt(Result, Context)) 16752 return false; 16753 16754 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16755 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 16756 ArraySizes.push_back(ConstantLengthValue); 16757 } 16758 16759 // Get the base of this array section and walk up from there. 16760 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 16761 16762 // We require length = 1 for all array sections except the right-most to 16763 // guarantee that the memory region is contiguous and has no holes in it. 16764 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 16765 Length = TempOASE->getLength(); 16766 if (Length == nullptr) { 16767 // For array sections of the form [1:] or [:], we would need to analyze 16768 // the lower bound... 16769 if (OASE->getColonLocFirst().isValid()) 16770 return false; 16771 16772 // This is an array subscript which has implicit length 1! 16773 ArraySizes.push_back(llvm::APSInt::get(1)); 16774 } else { 16775 Expr::EvalResult Result; 16776 if (!Length->EvaluateAsInt(Result, Context)) 16777 return false; 16778 16779 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 16780 if (ConstantLengthValue.getSExtValue() != 1) 16781 return false; 16782 16783 ArraySizes.push_back(ConstantLengthValue); 16784 } 16785 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 16786 } 16787 16788 // If we have a single element, we don't need to add the implicit lengths. 16789 if (!SingleElement) { 16790 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 16791 // Has implicit length 1! 16792 ArraySizes.push_back(llvm::APSInt::get(1)); 16793 Base = TempASE->getBase()->IgnoreParenImpCasts(); 16794 } 16795 } 16796 16797 // This array section can be privatized as a single value or as a constant 16798 // sized array. 16799 return true; 16800 } 16801 16802 static BinaryOperatorKind 16803 getRelatedCompoundReductionOp(BinaryOperatorKind BOK) { 16804 if (BOK == BO_Add) 16805 return BO_AddAssign; 16806 if (BOK == BO_Mul) 16807 return BO_MulAssign; 16808 if (BOK == BO_And) 16809 return BO_AndAssign; 16810 if (BOK == BO_Or) 16811 return BO_OrAssign; 16812 if (BOK == BO_Xor) 16813 return BO_XorAssign; 16814 return BOK; 16815 } 16816 16817 static bool actOnOMPReductionKindClause( 16818 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 16819 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 16820 SourceLocation ColonLoc, SourceLocation EndLoc, 16821 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 16822 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 16823 DeclarationName DN = ReductionId.getName(); 16824 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 16825 BinaryOperatorKind BOK = BO_Comma; 16826 16827 ASTContext &Context = S.Context; 16828 // OpenMP [2.14.3.6, reduction clause] 16829 // C 16830 // reduction-identifier is either an identifier or one of the following 16831 // operators: +, -, *, &, |, ^, && and || 16832 // C++ 16833 // reduction-identifier is either an id-expression or one of the following 16834 // operators: +, -, *, &, |, ^, && and || 16835 switch (OOK) { 16836 case OO_Plus: 16837 case OO_Minus: 16838 BOK = BO_Add; 16839 break; 16840 case OO_Star: 16841 BOK = BO_Mul; 16842 break; 16843 case OO_Amp: 16844 BOK = BO_And; 16845 break; 16846 case OO_Pipe: 16847 BOK = BO_Or; 16848 break; 16849 case OO_Caret: 16850 BOK = BO_Xor; 16851 break; 16852 case OO_AmpAmp: 16853 BOK = BO_LAnd; 16854 break; 16855 case OO_PipePipe: 16856 BOK = BO_LOr; 16857 break; 16858 case OO_New: 16859 case OO_Delete: 16860 case OO_Array_New: 16861 case OO_Array_Delete: 16862 case OO_Slash: 16863 case OO_Percent: 16864 case OO_Tilde: 16865 case OO_Exclaim: 16866 case OO_Equal: 16867 case OO_Less: 16868 case OO_Greater: 16869 case OO_LessEqual: 16870 case OO_GreaterEqual: 16871 case OO_PlusEqual: 16872 case OO_MinusEqual: 16873 case OO_StarEqual: 16874 case OO_SlashEqual: 16875 case OO_PercentEqual: 16876 case OO_CaretEqual: 16877 case OO_AmpEqual: 16878 case OO_PipeEqual: 16879 case OO_LessLess: 16880 case OO_GreaterGreater: 16881 case OO_LessLessEqual: 16882 case OO_GreaterGreaterEqual: 16883 case OO_EqualEqual: 16884 case OO_ExclaimEqual: 16885 case OO_Spaceship: 16886 case OO_PlusPlus: 16887 case OO_MinusMinus: 16888 case OO_Comma: 16889 case OO_ArrowStar: 16890 case OO_Arrow: 16891 case OO_Call: 16892 case OO_Subscript: 16893 case OO_Conditional: 16894 case OO_Coawait: 16895 case NUM_OVERLOADED_OPERATORS: 16896 llvm_unreachable("Unexpected reduction identifier"); 16897 case OO_None: 16898 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 16899 if (II->isStr("max")) 16900 BOK = BO_GT; 16901 else if (II->isStr("min")) 16902 BOK = BO_LT; 16903 } 16904 break; 16905 } 16906 SourceRange ReductionIdRange; 16907 if (ReductionIdScopeSpec.isValid()) 16908 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 16909 else 16910 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 16911 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 16912 16913 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 16914 bool FirstIter = true; 16915 for (Expr *RefExpr : VarList) { 16916 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 16917 // OpenMP [2.1, C/C++] 16918 // A list item is a variable or array section, subject to the restrictions 16919 // specified in Section 2.4 on page 42 and in each of the sections 16920 // describing clauses and directives for which a list appears. 16921 // OpenMP [2.14.3.3, Restrictions, p.1] 16922 // A variable that is part of another variable (as an array or 16923 // structure element) cannot appear in a private clause. 16924 if (!FirstIter && IR != ER) 16925 ++IR; 16926 FirstIter = false; 16927 SourceLocation ELoc; 16928 SourceRange ERange; 16929 Expr *SimpleRefExpr = RefExpr; 16930 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 16931 /*AllowArraySection=*/true); 16932 if (Res.second) { 16933 // Try to find 'declare reduction' corresponding construct before using 16934 // builtin/overloaded operators. 16935 QualType Type = Context.DependentTy; 16936 CXXCastPath BasePath; 16937 ExprResult DeclareReductionRef = buildDeclareReductionRef( 16938 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 16939 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 16940 Expr *ReductionOp = nullptr; 16941 if (S.CurContext->isDependentContext() && 16942 (DeclareReductionRef.isUnset() || 16943 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 16944 ReductionOp = DeclareReductionRef.get(); 16945 // It will be analyzed later. 16946 RD.push(RefExpr, ReductionOp); 16947 } 16948 ValueDecl *D = Res.first; 16949 if (!D) 16950 continue; 16951 16952 Expr *TaskgroupDescriptor = nullptr; 16953 QualType Type; 16954 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 16955 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 16956 if (ASE) { 16957 Type = ASE->getType().getNonReferenceType(); 16958 } else if (OASE) { 16959 QualType BaseType = 16960 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16961 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16962 Type = ATy->getElementType(); 16963 else 16964 Type = BaseType->getPointeeType(); 16965 Type = Type.getNonReferenceType(); 16966 } else { 16967 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 16968 } 16969 auto *VD = dyn_cast<VarDecl>(D); 16970 16971 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 16972 // A variable that appears in a private clause must not have an incomplete 16973 // type or a reference type. 16974 if (S.RequireCompleteType(ELoc, D->getType(), 16975 diag::err_omp_reduction_incomplete_type)) 16976 continue; 16977 // OpenMP [2.14.3.6, reduction clause, Restrictions] 16978 // A list item that appears in a reduction clause must not be 16979 // const-qualified. 16980 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 16981 /*AcceptIfMutable*/ false, ASE || OASE)) 16982 continue; 16983 16984 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 16985 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 16986 // If a list-item is a reference type then it must bind to the same object 16987 // for all threads of the team. 16988 if (!ASE && !OASE) { 16989 if (VD) { 16990 VarDecl *VDDef = VD->getDefinition(); 16991 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 16992 DSARefChecker Check(Stack); 16993 if (Check.Visit(VDDef->getInit())) { 16994 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 16995 << getOpenMPClauseName(ClauseKind) << ERange; 16996 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 16997 continue; 16998 } 16999 } 17000 } 17001 17002 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 17003 // in a Construct] 17004 // Variables with the predetermined data-sharing attributes may not be 17005 // listed in data-sharing attributes clauses, except for the cases 17006 // listed below. For these exceptions only, listing a predetermined 17007 // variable in a data-sharing attribute clause is allowed and overrides 17008 // the variable's predetermined data-sharing attributes. 17009 // OpenMP [2.14.3.6, Restrictions, p.3] 17010 // Any number of reduction clauses can be specified on the directive, 17011 // but a list item can appear only once in the reduction clauses for that 17012 // directive. 17013 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17014 if (DVar.CKind == OMPC_reduction) { 17015 S.Diag(ELoc, diag::err_omp_once_referenced) 17016 << getOpenMPClauseName(ClauseKind); 17017 if (DVar.RefExpr) 17018 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 17019 continue; 17020 } 17021 if (DVar.CKind != OMPC_unknown) { 17022 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17023 << getOpenMPClauseName(DVar.CKind) 17024 << getOpenMPClauseName(OMPC_reduction); 17025 reportOriginalDsa(S, Stack, D, DVar); 17026 continue; 17027 } 17028 17029 // OpenMP [2.14.3.6, Restrictions, p.1] 17030 // A list item that appears in a reduction clause of a worksharing 17031 // construct must be shared in the parallel regions to which any of the 17032 // worksharing regions arising from the worksharing construct bind. 17033 if (isOpenMPWorksharingDirective(CurrDir) && 17034 !isOpenMPParallelDirective(CurrDir) && 17035 !isOpenMPTeamsDirective(CurrDir)) { 17036 DVar = Stack->getImplicitDSA(D, true); 17037 if (DVar.CKind != OMPC_shared) { 17038 S.Diag(ELoc, diag::err_omp_required_access) 17039 << getOpenMPClauseName(OMPC_reduction) 17040 << getOpenMPClauseName(OMPC_shared); 17041 reportOriginalDsa(S, Stack, D, DVar); 17042 continue; 17043 } 17044 } 17045 } else { 17046 // Threadprivates cannot be shared between threads, so dignose if the base 17047 // is a threadprivate variable. 17048 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 17049 if (DVar.CKind == OMPC_threadprivate) { 17050 S.Diag(ELoc, diag::err_omp_wrong_dsa) 17051 << getOpenMPClauseName(DVar.CKind) 17052 << getOpenMPClauseName(OMPC_reduction); 17053 reportOriginalDsa(S, Stack, D, DVar); 17054 continue; 17055 } 17056 } 17057 17058 // Try to find 'declare reduction' corresponding construct before using 17059 // builtin/overloaded operators. 17060 CXXCastPath BasePath; 17061 ExprResult DeclareReductionRef = buildDeclareReductionRef( 17062 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 17063 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 17064 if (DeclareReductionRef.isInvalid()) 17065 continue; 17066 if (S.CurContext->isDependentContext() && 17067 (DeclareReductionRef.isUnset() || 17068 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 17069 RD.push(RefExpr, DeclareReductionRef.get()); 17070 continue; 17071 } 17072 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 17073 // Not allowed reduction identifier is found. 17074 S.Diag(ReductionId.getBeginLoc(), 17075 diag::err_omp_unknown_reduction_identifier) 17076 << Type << ReductionIdRange; 17077 continue; 17078 } 17079 17080 // OpenMP [2.14.3.6, reduction clause, Restrictions] 17081 // The type of a list item that appears in a reduction clause must be valid 17082 // for the reduction-identifier. For a max or min reduction in C, the type 17083 // of the list item must be an allowed arithmetic data type: char, int, 17084 // float, double, or _Bool, possibly modified with long, short, signed, or 17085 // unsigned. For a max or min reduction in C++, the type of the list item 17086 // must be an allowed arithmetic data type: char, wchar_t, int, float, 17087 // double, or bool, possibly modified with long, short, signed, or unsigned. 17088 if (DeclareReductionRef.isUnset()) { 17089 if ((BOK == BO_GT || BOK == BO_LT) && 17090 !(Type->isScalarType() || 17091 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 17092 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 17093 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 17094 if (!ASE && !OASE) { 17095 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17096 VarDecl::DeclarationOnly; 17097 S.Diag(D->getLocation(), 17098 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17099 << D; 17100 } 17101 continue; 17102 } 17103 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 17104 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 17105 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 17106 << getOpenMPClauseName(ClauseKind); 17107 if (!ASE && !OASE) { 17108 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17109 VarDecl::DeclarationOnly; 17110 S.Diag(D->getLocation(), 17111 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17112 << D; 17113 } 17114 continue; 17115 } 17116 } 17117 17118 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 17119 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 17120 D->hasAttrs() ? &D->getAttrs() : nullptr); 17121 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 17122 D->hasAttrs() ? &D->getAttrs() : nullptr); 17123 QualType PrivateTy = Type; 17124 17125 // Try if we can determine constant lengths for all array sections and avoid 17126 // the VLA. 17127 bool ConstantLengthOASE = false; 17128 if (OASE) { 17129 bool SingleElement; 17130 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 17131 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 17132 Context, OASE, SingleElement, ArraySizes); 17133 17134 // If we don't have a single element, we must emit a constant array type. 17135 if (ConstantLengthOASE && !SingleElement) { 17136 for (llvm::APSInt &Size : ArraySizes) 17137 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 17138 ArrayType::Normal, 17139 /*IndexTypeQuals=*/0); 17140 } 17141 } 17142 17143 if ((OASE && !ConstantLengthOASE) || 17144 (!OASE && !ASE && 17145 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 17146 if (!Context.getTargetInfo().isVLASupported()) { 17147 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 17148 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17149 S.Diag(ELoc, diag::note_vla_unsupported); 17150 continue; 17151 } else { 17152 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 17153 S.targetDiag(ELoc, diag::note_vla_unsupported); 17154 } 17155 } 17156 // For arrays/array sections only: 17157 // Create pseudo array type for private copy. The size for this array will 17158 // be generated during codegen. 17159 // For array subscripts or single variables Private Ty is the same as Type 17160 // (type of the variable or single array element). 17161 PrivateTy = Context.getVariableArrayType( 17162 Type, 17163 new (Context) 17164 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue), 17165 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 17166 } else if (!ASE && !OASE && 17167 Context.getAsArrayType(D->getType().getNonReferenceType())) { 17168 PrivateTy = D->getType().getNonReferenceType(); 17169 } 17170 // Private copy. 17171 VarDecl *PrivateVD = 17172 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17173 D->hasAttrs() ? &D->getAttrs() : nullptr, 17174 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17175 // Add initializer for private variable. 17176 Expr *Init = nullptr; 17177 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 17178 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 17179 if (DeclareReductionRef.isUsable()) { 17180 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 17181 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 17182 if (DRD->getInitializer()) { 17183 Init = DRDRef; 17184 RHSVD->setInit(DRDRef); 17185 RHSVD->setInitStyle(VarDecl::CallInit); 17186 } 17187 } else { 17188 switch (BOK) { 17189 case BO_Add: 17190 case BO_Xor: 17191 case BO_Or: 17192 case BO_LOr: 17193 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 17194 if (Type->isScalarType() || Type->isAnyComplexType()) 17195 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 17196 break; 17197 case BO_Mul: 17198 case BO_LAnd: 17199 if (Type->isScalarType() || Type->isAnyComplexType()) { 17200 // '*' and '&&' reduction ops - initializer is '1'. 17201 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 17202 } 17203 break; 17204 case BO_And: { 17205 // '&' reduction op - initializer is '~0'. 17206 QualType OrigType = Type; 17207 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 17208 Type = ComplexTy->getElementType(); 17209 if (Type->isRealFloatingType()) { 17210 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 17211 Context.getFloatTypeSemantics(Type), 17212 Context.getTypeSize(Type)); 17213 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17214 Type, ELoc); 17215 } else if (Type->isScalarType()) { 17216 uint64_t Size = Context.getTypeSize(Type); 17217 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 17218 llvm::APInt InitValue = llvm::APInt::getAllOnes(Size); 17219 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17220 } 17221 if (Init && OrigType->isAnyComplexType()) { 17222 // Init = 0xFFFF + 0xFFFFi; 17223 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 17224 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 17225 } 17226 Type = OrigType; 17227 break; 17228 } 17229 case BO_LT: 17230 case BO_GT: { 17231 // 'min' reduction op - initializer is 'Largest representable number in 17232 // the reduction list item type'. 17233 // 'max' reduction op - initializer is 'Least representable number in 17234 // the reduction list item type'. 17235 if (Type->isIntegerType() || Type->isPointerType()) { 17236 bool IsSigned = Type->hasSignedIntegerRepresentation(); 17237 uint64_t Size = Context.getTypeSize(Type); 17238 QualType IntTy = 17239 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 17240 llvm::APInt InitValue = 17241 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 17242 : llvm::APInt::getMinValue(Size) 17243 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 17244 : llvm::APInt::getMaxValue(Size); 17245 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 17246 if (Type->isPointerType()) { 17247 // Cast to pointer type. 17248 ExprResult CastExpr = S.BuildCStyleCastExpr( 17249 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 17250 if (CastExpr.isInvalid()) 17251 continue; 17252 Init = CastExpr.get(); 17253 } 17254 } else if (Type->isRealFloatingType()) { 17255 llvm::APFloat InitValue = llvm::APFloat::getLargest( 17256 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 17257 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 17258 Type, ELoc); 17259 } 17260 break; 17261 } 17262 case BO_PtrMemD: 17263 case BO_PtrMemI: 17264 case BO_MulAssign: 17265 case BO_Div: 17266 case BO_Rem: 17267 case BO_Sub: 17268 case BO_Shl: 17269 case BO_Shr: 17270 case BO_LE: 17271 case BO_GE: 17272 case BO_EQ: 17273 case BO_NE: 17274 case BO_Cmp: 17275 case BO_AndAssign: 17276 case BO_XorAssign: 17277 case BO_OrAssign: 17278 case BO_Assign: 17279 case BO_AddAssign: 17280 case BO_SubAssign: 17281 case BO_DivAssign: 17282 case BO_RemAssign: 17283 case BO_ShlAssign: 17284 case BO_ShrAssign: 17285 case BO_Comma: 17286 llvm_unreachable("Unexpected reduction operation"); 17287 } 17288 } 17289 if (Init && DeclareReductionRef.isUnset()) { 17290 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 17291 // Store initializer for single element in private copy. Will be used 17292 // during codegen. 17293 PrivateVD->setInit(RHSVD->getInit()); 17294 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17295 } else if (!Init) { 17296 S.ActOnUninitializedDecl(RHSVD); 17297 // Store initializer for single element in private copy. Will be used 17298 // during codegen. 17299 PrivateVD->setInit(RHSVD->getInit()); 17300 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 17301 } 17302 if (RHSVD->isInvalidDecl()) 17303 continue; 17304 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 17305 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 17306 << Type << ReductionIdRange; 17307 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 17308 VarDecl::DeclarationOnly; 17309 S.Diag(D->getLocation(), 17310 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17311 << D; 17312 continue; 17313 } 17314 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 17315 ExprResult ReductionOp; 17316 if (DeclareReductionRef.isUsable()) { 17317 QualType RedTy = DeclareReductionRef.get()->getType(); 17318 QualType PtrRedTy = Context.getPointerType(RedTy); 17319 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 17320 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 17321 if (!BasePath.empty()) { 17322 LHS = S.DefaultLvalueConversion(LHS.get()); 17323 RHS = S.DefaultLvalueConversion(RHS.get()); 17324 LHS = ImplicitCastExpr::Create( 17325 Context, PtrRedTy, CK_UncheckedDerivedToBase, LHS.get(), &BasePath, 17326 LHS.get()->getValueKind(), FPOptionsOverride()); 17327 RHS = ImplicitCastExpr::Create( 17328 Context, PtrRedTy, CK_UncheckedDerivedToBase, RHS.get(), &BasePath, 17329 RHS.get()->getValueKind(), FPOptionsOverride()); 17330 } 17331 FunctionProtoType::ExtProtoInfo EPI; 17332 QualType Params[] = {PtrRedTy, PtrRedTy}; 17333 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 17334 auto *OVE = new (Context) OpaqueValueExpr( 17335 ELoc, Context.getPointerType(FnTy), VK_PRValue, OK_Ordinary, 17336 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 17337 Expr *Args[] = {LHS.get(), RHS.get()}; 17338 ReductionOp = 17339 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_PRValue, ELoc, 17340 S.CurFPFeatureOverrides()); 17341 } else { 17342 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK); 17343 if (Type->isRecordType() && CombBOK != BOK) { 17344 Sema::TentativeAnalysisScope Trap(S); 17345 ReductionOp = 17346 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17347 CombBOK, LHSDRE, RHSDRE); 17348 } 17349 if (!ReductionOp.isUsable()) { 17350 ReductionOp = 17351 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, 17352 LHSDRE, RHSDRE); 17353 if (ReductionOp.isUsable()) { 17354 if (BOK != BO_LT && BOK != BO_GT) { 17355 ReductionOp = 17356 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17357 BO_Assign, LHSDRE, ReductionOp.get()); 17358 } else { 17359 auto *ConditionalOp = new (Context) 17360 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, 17361 RHSDRE, Type, VK_LValue, OK_Ordinary); 17362 ReductionOp = 17363 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 17364 BO_Assign, LHSDRE, ConditionalOp); 17365 } 17366 } 17367 } 17368 if (ReductionOp.isUsable()) 17369 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 17370 /*DiscardedValue*/ false); 17371 if (!ReductionOp.isUsable()) 17372 continue; 17373 } 17374 17375 // Add copy operations for inscan reductions. 17376 // LHS = RHS; 17377 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 17378 if (ClauseKind == OMPC_reduction && 17379 RD.RedModifier == OMPC_REDUCTION_inscan) { 17380 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 17381 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 17382 RHS.get()); 17383 if (!CopyOpRes.isUsable()) 17384 continue; 17385 CopyOpRes = 17386 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 17387 if (!CopyOpRes.isUsable()) 17388 continue; 17389 // For simd directive and simd-based directives in simd mode no need to 17390 // construct temp array, need just a single temp element. 17391 if (Stack->getCurrentDirective() == OMPD_simd || 17392 (S.getLangOpts().OpenMPSimd && 17393 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 17394 VarDecl *TempArrayVD = 17395 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 17396 D->hasAttrs() ? &D->getAttrs() : nullptr); 17397 // Add a constructor to the temp decl. 17398 S.ActOnUninitializedDecl(TempArrayVD); 17399 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 17400 } else { 17401 // Build temp array for prefix sum. 17402 auto *Dim = new (S.Context) 17403 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17404 QualType ArrayTy = 17405 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 17406 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 17407 VarDecl *TempArrayVD = 17408 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 17409 D->hasAttrs() ? &D->getAttrs() : nullptr); 17410 // Add a constructor to the temp decl. 17411 S.ActOnUninitializedDecl(TempArrayVD); 17412 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 17413 TempArrayElem = 17414 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 17415 auto *Idx = new (S.Context) 17416 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue); 17417 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 17418 ELoc, Idx, ELoc); 17419 } 17420 } 17421 17422 // OpenMP [2.15.4.6, Restrictions, p.2] 17423 // A list item that appears in an in_reduction clause of a task construct 17424 // must appear in a task_reduction clause of a construct associated with a 17425 // taskgroup region that includes the participating task in its taskgroup 17426 // set. The construct associated with the innermost region that meets this 17427 // condition must specify the same reduction-identifier as the in_reduction 17428 // clause. 17429 if (ClauseKind == OMPC_in_reduction) { 17430 SourceRange ParentSR; 17431 BinaryOperatorKind ParentBOK; 17432 const Expr *ParentReductionOp = nullptr; 17433 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 17434 DSAStackTy::DSAVarData ParentBOKDSA = 17435 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 17436 ParentBOKTD); 17437 DSAStackTy::DSAVarData ParentReductionOpDSA = 17438 Stack->getTopMostTaskgroupReductionData( 17439 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 17440 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 17441 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 17442 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 17443 (DeclareReductionRef.isUsable() && IsParentBOK) || 17444 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 17445 bool EmitError = true; 17446 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 17447 llvm::FoldingSetNodeID RedId, ParentRedId; 17448 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 17449 DeclareReductionRef.get()->Profile(RedId, Context, 17450 /*Canonical=*/true); 17451 EmitError = RedId != ParentRedId; 17452 } 17453 if (EmitError) { 17454 S.Diag(ReductionId.getBeginLoc(), 17455 diag::err_omp_reduction_identifier_mismatch) 17456 << ReductionIdRange << RefExpr->getSourceRange(); 17457 S.Diag(ParentSR.getBegin(), 17458 diag::note_omp_previous_reduction_identifier) 17459 << ParentSR 17460 << (IsParentBOK ? ParentBOKDSA.RefExpr 17461 : ParentReductionOpDSA.RefExpr) 17462 ->getSourceRange(); 17463 continue; 17464 } 17465 } 17466 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 17467 } 17468 17469 DeclRefExpr *Ref = nullptr; 17470 Expr *VarsExpr = RefExpr->IgnoreParens(); 17471 if (!VD && !S.CurContext->isDependentContext()) { 17472 if (ASE || OASE) { 17473 TransformExprToCaptures RebuildToCapture(S, D); 17474 VarsExpr = 17475 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 17476 Ref = RebuildToCapture.getCapturedExpr(); 17477 } else { 17478 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 17479 } 17480 if (!S.isOpenMPCapturedDecl(D)) { 17481 RD.ExprCaptures.emplace_back(Ref->getDecl()); 17482 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17483 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 17484 if (!RefRes.isUsable()) 17485 continue; 17486 ExprResult PostUpdateRes = 17487 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 17488 RefRes.get()); 17489 if (!PostUpdateRes.isUsable()) 17490 continue; 17491 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 17492 Stack->getCurrentDirective() == OMPD_taskgroup) { 17493 S.Diag(RefExpr->getExprLoc(), 17494 diag::err_omp_reduction_non_addressable_expression) 17495 << RefExpr->getSourceRange(); 17496 continue; 17497 } 17498 RD.ExprPostUpdates.emplace_back( 17499 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 17500 } 17501 } 17502 } 17503 // All reduction items are still marked as reduction (to do not increase 17504 // code base size). 17505 unsigned Modifier = RD.RedModifier; 17506 // Consider task_reductions as reductions with task modifier. Required for 17507 // correct analysis of in_reduction clauses. 17508 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 17509 Modifier = OMPC_REDUCTION_task; 17510 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier, 17511 ASE || OASE); 17512 if (Modifier == OMPC_REDUCTION_task && 17513 (CurrDir == OMPD_taskgroup || 17514 ((isOpenMPParallelDirective(CurrDir) || 17515 isOpenMPWorksharingDirective(CurrDir)) && 17516 !isOpenMPSimdDirective(CurrDir)))) { 17517 if (DeclareReductionRef.isUsable()) 17518 Stack->addTaskgroupReductionData(D, ReductionIdRange, 17519 DeclareReductionRef.get()); 17520 else 17521 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 17522 } 17523 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 17524 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 17525 TempArrayElem.get()); 17526 } 17527 return RD.Vars.empty(); 17528 } 17529 17530 OMPClause *Sema::ActOnOpenMPReductionClause( 17531 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 17532 SourceLocation StartLoc, SourceLocation LParenLoc, 17533 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 17534 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17535 ArrayRef<Expr *> UnresolvedReductions) { 17536 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 17537 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 17538 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 17539 /*Last=*/OMPC_REDUCTION_unknown) 17540 << getOpenMPClauseName(OMPC_reduction); 17541 return nullptr; 17542 } 17543 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 17544 // A reduction clause with the inscan reduction-modifier may only appear on a 17545 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 17546 // construct, a parallel worksharing-loop construct or a parallel 17547 // worksharing-loop SIMD construct. 17548 if (Modifier == OMPC_REDUCTION_inscan && 17549 (DSAStack->getCurrentDirective() != OMPD_for && 17550 DSAStack->getCurrentDirective() != OMPD_for_simd && 17551 DSAStack->getCurrentDirective() != OMPD_simd && 17552 DSAStack->getCurrentDirective() != OMPD_parallel_for && 17553 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 17554 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 17555 return nullptr; 17556 } 17557 17558 ReductionData RD(VarList.size(), Modifier); 17559 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 17560 StartLoc, LParenLoc, ColonLoc, EndLoc, 17561 ReductionIdScopeSpec, ReductionId, 17562 UnresolvedReductions, RD)) 17563 return nullptr; 17564 17565 return OMPReductionClause::Create( 17566 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 17567 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17568 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 17569 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 17570 buildPreInits(Context, RD.ExprCaptures), 17571 buildPostUpdate(*this, RD.ExprPostUpdates)); 17572 } 17573 17574 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 17575 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17576 SourceLocation ColonLoc, SourceLocation EndLoc, 17577 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17578 ArrayRef<Expr *> UnresolvedReductions) { 17579 ReductionData RD(VarList.size()); 17580 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 17581 StartLoc, LParenLoc, ColonLoc, EndLoc, 17582 ReductionIdScopeSpec, ReductionId, 17583 UnresolvedReductions, RD)) 17584 return nullptr; 17585 17586 return OMPTaskReductionClause::Create( 17587 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17588 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17589 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 17590 buildPreInits(Context, RD.ExprCaptures), 17591 buildPostUpdate(*this, RD.ExprPostUpdates)); 17592 } 17593 17594 OMPClause *Sema::ActOnOpenMPInReductionClause( 17595 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 17596 SourceLocation ColonLoc, SourceLocation EndLoc, 17597 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 17598 ArrayRef<Expr *> UnresolvedReductions) { 17599 ReductionData RD(VarList.size()); 17600 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 17601 StartLoc, LParenLoc, ColonLoc, EndLoc, 17602 ReductionIdScopeSpec, ReductionId, 17603 UnresolvedReductions, RD)) 17604 return nullptr; 17605 17606 return OMPInReductionClause::Create( 17607 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 17608 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 17609 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 17610 buildPreInits(Context, RD.ExprCaptures), 17611 buildPostUpdate(*this, RD.ExprPostUpdates)); 17612 } 17613 17614 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 17615 SourceLocation LinLoc) { 17616 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 17617 LinKind == OMPC_LINEAR_unknown) { 17618 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 17619 return true; 17620 } 17621 return false; 17622 } 17623 17624 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 17625 OpenMPLinearClauseKind LinKind, QualType Type, 17626 bool IsDeclareSimd) { 17627 const auto *VD = dyn_cast_or_null<VarDecl>(D); 17628 // A variable must not have an incomplete type or a reference type. 17629 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 17630 return true; 17631 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 17632 !Type->isReferenceType()) { 17633 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 17634 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 17635 return true; 17636 } 17637 Type = Type.getNonReferenceType(); 17638 17639 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 17640 // A variable that is privatized must not have a const-qualified type 17641 // unless it is of class type with a mutable member. This restriction does 17642 // not apply to the firstprivate clause, nor to the linear clause on 17643 // declarative directives (like declare simd). 17644 if (!IsDeclareSimd && 17645 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 17646 return true; 17647 17648 // A list item must be of integral or pointer type. 17649 Type = Type.getUnqualifiedType().getCanonicalType(); 17650 const auto *Ty = Type.getTypePtrOrNull(); 17651 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 17652 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 17653 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 17654 if (D) { 17655 bool IsDecl = 17656 !VD || 17657 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17658 Diag(D->getLocation(), 17659 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17660 << D; 17661 } 17662 return true; 17663 } 17664 return false; 17665 } 17666 17667 OMPClause *Sema::ActOnOpenMPLinearClause( 17668 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 17669 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 17670 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17671 SmallVector<Expr *, 8> Vars; 17672 SmallVector<Expr *, 8> Privates; 17673 SmallVector<Expr *, 8> Inits; 17674 SmallVector<Decl *, 4> ExprCaptures; 17675 SmallVector<Expr *, 4> ExprPostUpdates; 17676 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 17677 LinKind = OMPC_LINEAR_val; 17678 for (Expr *RefExpr : VarList) { 17679 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17680 SourceLocation ELoc; 17681 SourceRange ERange; 17682 Expr *SimpleRefExpr = RefExpr; 17683 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17684 if (Res.second) { 17685 // It will be analyzed later. 17686 Vars.push_back(RefExpr); 17687 Privates.push_back(nullptr); 17688 Inits.push_back(nullptr); 17689 } 17690 ValueDecl *D = Res.first; 17691 if (!D) 17692 continue; 17693 17694 QualType Type = D->getType(); 17695 auto *VD = dyn_cast<VarDecl>(D); 17696 17697 // OpenMP [2.14.3.7, linear clause] 17698 // A list-item cannot appear in more than one linear clause. 17699 // A list-item that appears in a linear clause cannot appear in any 17700 // other data-sharing attribute clause. 17701 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 17702 if (DVar.RefExpr) { 17703 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 17704 << getOpenMPClauseName(OMPC_linear); 17705 reportOriginalDsa(*this, DSAStack, D, DVar); 17706 continue; 17707 } 17708 17709 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 17710 continue; 17711 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17712 17713 // Build private copy of original var. 17714 VarDecl *Private = 17715 buildVarDecl(*this, ELoc, Type, D->getName(), 17716 D->hasAttrs() ? &D->getAttrs() : nullptr, 17717 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 17718 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 17719 // Build var to save initial value. 17720 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 17721 Expr *InitExpr; 17722 DeclRefExpr *Ref = nullptr; 17723 if (!VD && !CurContext->isDependentContext()) { 17724 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 17725 if (!isOpenMPCapturedDecl(D)) { 17726 ExprCaptures.push_back(Ref->getDecl()); 17727 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 17728 ExprResult RefRes = DefaultLvalueConversion(Ref); 17729 if (!RefRes.isUsable()) 17730 continue; 17731 ExprResult PostUpdateRes = 17732 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 17733 SimpleRefExpr, RefRes.get()); 17734 if (!PostUpdateRes.isUsable()) 17735 continue; 17736 ExprPostUpdates.push_back( 17737 IgnoredValueConversions(PostUpdateRes.get()).get()); 17738 } 17739 } 17740 } 17741 if (LinKind == OMPC_LINEAR_uval) 17742 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 17743 else 17744 InitExpr = VD ? SimpleRefExpr : Ref; 17745 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 17746 /*DirectInit=*/false); 17747 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 17748 17749 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 17750 Vars.push_back((VD || CurContext->isDependentContext()) 17751 ? RefExpr->IgnoreParens() 17752 : Ref); 17753 Privates.push_back(PrivateRef); 17754 Inits.push_back(InitRef); 17755 } 17756 17757 if (Vars.empty()) 17758 return nullptr; 17759 17760 Expr *StepExpr = Step; 17761 Expr *CalcStepExpr = nullptr; 17762 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 17763 !Step->isInstantiationDependent() && 17764 !Step->containsUnexpandedParameterPack()) { 17765 SourceLocation StepLoc = Step->getBeginLoc(); 17766 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 17767 if (Val.isInvalid()) 17768 return nullptr; 17769 StepExpr = Val.get(); 17770 17771 // Build var to save the step value. 17772 VarDecl *SaveVar = 17773 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 17774 ExprResult SaveRef = 17775 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 17776 ExprResult CalcStep = 17777 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 17778 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 17779 17780 // Warn about zero linear step (it would be probably better specified as 17781 // making corresponding variables 'const'). 17782 if (Optional<llvm::APSInt> Result = 17783 StepExpr->getIntegerConstantExpr(Context)) { 17784 if (!Result->isNegative() && !Result->isStrictlyPositive()) 17785 Diag(StepLoc, diag::warn_omp_linear_step_zero) 17786 << Vars[0] << (Vars.size() > 1); 17787 } else if (CalcStep.isUsable()) { 17788 // Calculate the step beforehand instead of doing this on each iteration. 17789 // (This is not used if the number of iterations may be kfold-ed). 17790 CalcStepExpr = CalcStep.get(); 17791 } 17792 } 17793 17794 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 17795 ColonLoc, EndLoc, Vars, Privates, Inits, 17796 StepExpr, CalcStepExpr, 17797 buildPreInits(Context, ExprCaptures), 17798 buildPostUpdate(*this, ExprPostUpdates)); 17799 } 17800 17801 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 17802 Expr *NumIterations, Sema &SemaRef, 17803 Scope *S, DSAStackTy *Stack) { 17804 // Walk the vars and build update/final expressions for the CodeGen. 17805 SmallVector<Expr *, 8> Updates; 17806 SmallVector<Expr *, 8> Finals; 17807 SmallVector<Expr *, 8> UsedExprs; 17808 Expr *Step = Clause.getStep(); 17809 Expr *CalcStep = Clause.getCalcStep(); 17810 // OpenMP [2.14.3.7, linear clause] 17811 // If linear-step is not specified it is assumed to be 1. 17812 if (!Step) 17813 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 17814 else if (CalcStep) 17815 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 17816 bool HasErrors = false; 17817 auto CurInit = Clause.inits().begin(); 17818 auto CurPrivate = Clause.privates().begin(); 17819 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 17820 for (Expr *RefExpr : Clause.varlists()) { 17821 SourceLocation ELoc; 17822 SourceRange ERange; 17823 Expr *SimpleRefExpr = RefExpr; 17824 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 17825 ValueDecl *D = Res.first; 17826 if (Res.second || !D) { 17827 Updates.push_back(nullptr); 17828 Finals.push_back(nullptr); 17829 HasErrors = true; 17830 continue; 17831 } 17832 auto &&Info = Stack->isLoopControlVariable(D); 17833 // OpenMP [2.15.11, distribute simd Construct] 17834 // A list item may not appear in a linear clause, unless it is the loop 17835 // iteration variable. 17836 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 17837 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 17838 SemaRef.Diag(ELoc, 17839 diag::err_omp_linear_distribute_var_non_loop_iteration); 17840 Updates.push_back(nullptr); 17841 Finals.push_back(nullptr); 17842 HasErrors = true; 17843 continue; 17844 } 17845 Expr *InitExpr = *CurInit; 17846 17847 // Build privatized reference to the current linear var. 17848 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 17849 Expr *CapturedRef; 17850 if (LinKind == OMPC_LINEAR_uval) 17851 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 17852 else 17853 CapturedRef = 17854 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 17855 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 17856 /*RefersToCapture=*/true); 17857 17858 // Build update: Var = InitExpr + IV * Step 17859 ExprResult Update; 17860 if (!Info.first) 17861 Update = buildCounterUpdate( 17862 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 17863 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 17864 else 17865 Update = *CurPrivate; 17866 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 17867 /*DiscardedValue*/ false); 17868 17869 // Build final: Var = InitExpr + NumIterations * Step 17870 ExprResult Final; 17871 if (!Info.first) 17872 Final = 17873 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 17874 InitExpr, NumIterations, Step, /*Subtract=*/false, 17875 /*IsNonRectangularLB=*/false); 17876 else 17877 Final = *CurPrivate; 17878 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 17879 /*DiscardedValue*/ false); 17880 17881 if (!Update.isUsable() || !Final.isUsable()) { 17882 Updates.push_back(nullptr); 17883 Finals.push_back(nullptr); 17884 UsedExprs.push_back(nullptr); 17885 HasErrors = true; 17886 } else { 17887 Updates.push_back(Update.get()); 17888 Finals.push_back(Final.get()); 17889 if (!Info.first) 17890 UsedExprs.push_back(SimpleRefExpr); 17891 } 17892 ++CurInit; 17893 ++CurPrivate; 17894 } 17895 if (Expr *S = Clause.getStep()) 17896 UsedExprs.push_back(S); 17897 // Fill the remaining part with the nullptr. 17898 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 17899 Clause.setUpdates(Updates); 17900 Clause.setFinals(Finals); 17901 Clause.setUsedExprs(UsedExprs); 17902 return HasErrors; 17903 } 17904 17905 OMPClause *Sema::ActOnOpenMPAlignedClause( 17906 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 17907 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 17908 SmallVector<Expr *, 8> Vars; 17909 for (Expr *RefExpr : VarList) { 17910 assert(RefExpr && "NULL expr in OpenMP linear clause."); 17911 SourceLocation ELoc; 17912 SourceRange ERange; 17913 Expr *SimpleRefExpr = RefExpr; 17914 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 17915 if (Res.second) { 17916 // It will be analyzed later. 17917 Vars.push_back(RefExpr); 17918 } 17919 ValueDecl *D = Res.first; 17920 if (!D) 17921 continue; 17922 17923 QualType QType = D->getType(); 17924 auto *VD = dyn_cast<VarDecl>(D); 17925 17926 // OpenMP [2.8.1, simd construct, Restrictions] 17927 // The type of list items appearing in the aligned clause must be 17928 // array, pointer, reference to array, or reference to pointer. 17929 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 17930 const Type *Ty = QType.getTypePtrOrNull(); 17931 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 17932 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 17933 << QType << getLangOpts().CPlusPlus << ERange; 17934 bool IsDecl = 17935 !VD || 17936 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 17937 Diag(D->getLocation(), 17938 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 17939 << D; 17940 continue; 17941 } 17942 17943 // OpenMP [2.8.1, simd construct, Restrictions] 17944 // A list-item cannot appear in more than one aligned clause. 17945 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 17946 Diag(ELoc, diag::err_omp_used_in_clause_twice) 17947 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 17948 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 17949 << getOpenMPClauseName(OMPC_aligned); 17950 continue; 17951 } 17952 17953 DeclRefExpr *Ref = nullptr; 17954 if (!VD && isOpenMPCapturedDecl(D)) 17955 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 17956 Vars.push_back(DefaultFunctionArrayConversion( 17957 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 17958 .get()); 17959 } 17960 17961 // OpenMP [2.8.1, simd construct, Description] 17962 // The parameter of the aligned clause, alignment, must be a constant 17963 // positive integer expression. 17964 // If no optional parameter is specified, implementation-defined default 17965 // alignments for SIMD instructions on the target platforms are assumed. 17966 if (Alignment != nullptr) { 17967 ExprResult AlignResult = 17968 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 17969 if (AlignResult.isInvalid()) 17970 return nullptr; 17971 Alignment = AlignResult.get(); 17972 } 17973 if (Vars.empty()) 17974 return nullptr; 17975 17976 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 17977 EndLoc, Vars, Alignment); 17978 } 17979 17980 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 17981 SourceLocation StartLoc, 17982 SourceLocation LParenLoc, 17983 SourceLocation EndLoc) { 17984 SmallVector<Expr *, 8> Vars; 17985 SmallVector<Expr *, 8> SrcExprs; 17986 SmallVector<Expr *, 8> DstExprs; 17987 SmallVector<Expr *, 8> AssignmentOps; 17988 for (Expr *RefExpr : VarList) { 17989 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 17990 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 17991 // It will be analyzed later. 17992 Vars.push_back(RefExpr); 17993 SrcExprs.push_back(nullptr); 17994 DstExprs.push_back(nullptr); 17995 AssignmentOps.push_back(nullptr); 17996 continue; 17997 } 17998 17999 SourceLocation ELoc = RefExpr->getExprLoc(); 18000 // OpenMP [2.1, C/C++] 18001 // A list item is a variable name. 18002 // OpenMP [2.14.4.1, Restrictions, p.1] 18003 // A list item that appears in a copyin clause must be threadprivate. 18004 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 18005 if (!DE || !isa<VarDecl>(DE->getDecl())) { 18006 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 18007 << 0 << RefExpr->getSourceRange(); 18008 continue; 18009 } 18010 18011 Decl *D = DE->getDecl(); 18012 auto *VD = cast<VarDecl>(D); 18013 18014 QualType Type = VD->getType(); 18015 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 18016 // It will be analyzed later. 18017 Vars.push_back(DE); 18018 SrcExprs.push_back(nullptr); 18019 DstExprs.push_back(nullptr); 18020 AssignmentOps.push_back(nullptr); 18021 continue; 18022 } 18023 18024 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 18025 // A list item that appears in a copyin clause must be threadprivate. 18026 if (!DSAStack->isThreadPrivate(VD)) { 18027 Diag(ELoc, diag::err_omp_required_access) 18028 << getOpenMPClauseName(OMPC_copyin) 18029 << getOpenMPDirectiveName(OMPD_threadprivate); 18030 continue; 18031 } 18032 18033 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18034 // A variable of class type (or array thereof) that appears in a 18035 // copyin clause requires an accessible, unambiguous copy assignment 18036 // operator for the class type. 18037 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 18038 VarDecl *SrcVD = 18039 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 18040 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18041 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 18042 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 18043 VarDecl *DstVD = 18044 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 18045 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 18046 DeclRefExpr *PseudoDstExpr = 18047 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 18048 // For arrays generate assignment operation for single element and replace 18049 // it by the original array element in CodeGen. 18050 ExprResult AssignmentOp = 18051 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 18052 PseudoSrcExpr); 18053 if (AssignmentOp.isInvalid()) 18054 continue; 18055 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 18056 /*DiscardedValue*/ false); 18057 if (AssignmentOp.isInvalid()) 18058 continue; 18059 18060 DSAStack->addDSA(VD, DE, OMPC_copyin); 18061 Vars.push_back(DE); 18062 SrcExprs.push_back(PseudoSrcExpr); 18063 DstExprs.push_back(PseudoDstExpr); 18064 AssignmentOps.push_back(AssignmentOp.get()); 18065 } 18066 18067 if (Vars.empty()) 18068 return nullptr; 18069 18070 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 18071 SrcExprs, DstExprs, AssignmentOps); 18072 } 18073 18074 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 18075 SourceLocation StartLoc, 18076 SourceLocation LParenLoc, 18077 SourceLocation EndLoc) { 18078 SmallVector<Expr *, 8> Vars; 18079 SmallVector<Expr *, 8> SrcExprs; 18080 SmallVector<Expr *, 8> DstExprs; 18081 SmallVector<Expr *, 8> AssignmentOps; 18082 for (Expr *RefExpr : VarList) { 18083 assert(RefExpr && "NULL expr in OpenMP linear clause."); 18084 SourceLocation ELoc; 18085 SourceRange ERange; 18086 Expr *SimpleRefExpr = RefExpr; 18087 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18088 if (Res.second) { 18089 // It will be analyzed later. 18090 Vars.push_back(RefExpr); 18091 SrcExprs.push_back(nullptr); 18092 DstExprs.push_back(nullptr); 18093 AssignmentOps.push_back(nullptr); 18094 } 18095 ValueDecl *D = Res.first; 18096 if (!D) 18097 continue; 18098 18099 QualType Type = D->getType(); 18100 auto *VD = dyn_cast<VarDecl>(D); 18101 18102 // OpenMP [2.14.4.2, Restrictions, p.2] 18103 // A list item that appears in a copyprivate clause may not appear in a 18104 // private or firstprivate clause on the single construct. 18105 if (!VD || !DSAStack->isThreadPrivate(VD)) { 18106 DSAStackTy::DSAVarData DVar = 18107 DSAStack->getTopDSA(D, /*FromParent=*/false); 18108 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 18109 DVar.RefExpr) { 18110 Diag(ELoc, diag::err_omp_wrong_dsa) 18111 << getOpenMPClauseName(DVar.CKind) 18112 << getOpenMPClauseName(OMPC_copyprivate); 18113 reportOriginalDsa(*this, DSAStack, D, DVar); 18114 continue; 18115 } 18116 18117 // OpenMP [2.11.4.2, Restrictions, p.1] 18118 // All list items that appear in a copyprivate clause must be either 18119 // threadprivate or private in the enclosing context. 18120 if (DVar.CKind == OMPC_unknown) { 18121 DVar = DSAStack->getImplicitDSA(D, false); 18122 if (DVar.CKind == OMPC_shared) { 18123 Diag(ELoc, diag::err_omp_required_access) 18124 << getOpenMPClauseName(OMPC_copyprivate) 18125 << "threadprivate or private in the enclosing context"; 18126 reportOriginalDsa(*this, DSAStack, D, DVar); 18127 continue; 18128 } 18129 } 18130 } 18131 18132 // Variably modified types are not supported. 18133 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 18134 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 18135 << getOpenMPClauseName(OMPC_copyprivate) << Type 18136 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18137 bool IsDecl = 18138 !VD || 18139 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 18140 Diag(D->getLocation(), 18141 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 18142 << D; 18143 continue; 18144 } 18145 18146 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 18147 // A variable of class type (or array thereof) that appears in a 18148 // copyin clause requires an accessible, unambiguous copy assignment 18149 // operator for the class type. 18150 Type = Context.getBaseElementType(Type.getNonReferenceType()) 18151 .getUnqualifiedType(); 18152 VarDecl *SrcVD = 18153 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 18154 D->hasAttrs() ? &D->getAttrs() : nullptr); 18155 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 18156 VarDecl *DstVD = 18157 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 18158 D->hasAttrs() ? &D->getAttrs() : nullptr); 18159 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 18160 ExprResult AssignmentOp = BuildBinOp( 18161 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 18162 if (AssignmentOp.isInvalid()) 18163 continue; 18164 AssignmentOp = 18165 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 18166 if (AssignmentOp.isInvalid()) 18167 continue; 18168 18169 // No need to mark vars as copyprivate, they are already threadprivate or 18170 // implicitly private. 18171 assert(VD || isOpenMPCapturedDecl(D)); 18172 Vars.push_back( 18173 VD ? RefExpr->IgnoreParens() 18174 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 18175 SrcExprs.push_back(PseudoSrcExpr); 18176 DstExprs.push_back(PseudoDstExpr); 18177 AssignmentOps.push_back(AssignmentOp.get()); 18178 } 18179 18180 if (Vars.empty()) 18181 return nullptr; 18182 18183 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18184 Vars, SrcExprs, DstExprs, AssignmentOps); 18185 } 18186 18187 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 18188 SourceLocation StartLoc, 18189 SourceLocation LParenLoc, 18190 SourceLocation EndLoc) { 18191 if (VarList.empty()) 18192 return nullptr; 18193 18194 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 18195 } 18196 18197 /// Tries to find omp_depend_t. type. 18198 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 18199 bool Diagnose = true) { 18200 QualType OMPDependT = Stack->getOMPDependT(); 18201 if (!OMPDependT.isNull()) 18202 return true; 18203 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 18204 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18205 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18206 if (Diagnose) 18207 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 18208 return false; 18209 } 18210 Stack->setOMPDependT(PT.get()); 18211 return true; 18212 } 18213 18214 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 18215 SourceLocation LParenLoc, 18216 SourceLocation EndLoc) { 18217 if (!Depobj) 18218 return nullptr; 18219 18220 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 18221 18222 // OpenMP 5.0, 2.17.10.1 depobj Construct 18223 // depobj is an lvalue expression of type omp_depend_t. 18224 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 18225 !Depobj->isInstantiationDependent() && 18226 !Depobj->containsUnexpandedParameterPack() && 18227 (OMPDependTFound && 18228 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 18229 /*CompareUnqualified=*/true))) { 18230 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18231 << 0 << Depobj->getType() << Depobj->getSourceRange(); 18232 } 18233 18234 if (!Depobj->isLValue()) { 18235 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 18236 << 1 << Depobj->getSourceRange(); 18237 } 18238 18239 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 18240 } 18241 18242 OMPClause * 18243 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 18244 SourceLocation DepLoc, SourceLocation ColonLoc, 18245 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18246 SourceLocation LParenLoc, SourceLocation EndLoc) { 18247 if (DSAStack->getCurrentDirective() == OMPD_ordered && 18248 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 18249 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18250 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 18251 return nullptr; 18252 } 18253 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 18254 DSAStack->getCurrentDirective() == OMPD_depobj) && 18255 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 18256 DepKind == OMPC_DEPEND_sink || 18257 ((LangOpts.OpenMP < 50 || 18258 DSAStack->getCurrentDirective() == OMPD_depobj) && 18259 DepKind == OMPC_DEPEND_depobj))) { 18260 SmallVector<unsigned, 3> Except; 18261 Except.push_back(OMPC_DEPEND_source); 18262 Except.push_back(OMPC_DEPEND_sink); 18263 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 18264 Except.push_back(OMPC_DEPEND_depobj); 18265 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 18266 ? "depend modifier(iterator) or " 18267 : ""; 18268 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 18269 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 18270 /*Last=*/OMPC_DEPEND_unknown, 18271 Except) 18272 << getOpenMPClauseName(OMPC_depend); 18273 return nullptr; 18274 } 18275 if (DepModifier && 18276 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 18277 Diag(DepModifier->getExprLoc(), 18278 diag::err_omp_depend_sink_source_with_modifier); 18279 return nullptr; 18280 } 18281 if (DepModifier && 18282 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 18283 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 18284 18285 SmallVector<Expr *, 8> Vars; 18286 DSAStackTy::OperatorOffsetTy OpsOffs; 18287 llvm::APSInt DepCounter(/*BitWidth=*/32); 18288 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 18289 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 18290 if (const Expr *OrderedCountExpr = 18291 DSAStack->getParentOrderedRegionParam().first) { 18292 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 18293 TotalDepCount.setIsUnsigned(/*Val=*/true); 18294 } 18295 } 18296 for (Expr *RefExpr : VarList) { 18297 assert(RefExpr && "NULL expr in OpenMP shared clause."); 18298 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 18299 // It will be analyzed later. 18300 Vars.push_back(RefExpr); 18301 continue; 18302 } 18303 18304 SourceLocation ELoc = RefExpr->getExprLoc(); 18305 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 18306 if (DepKind == OMPC_DEPEND_sink) { 18307 if (DSAStack->getParentOrderedRegionParam().first && 18308 DepCounter >= TotalDepCount) { 18309 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 18310 continue; 18311 } 18312 ++DepCounter; 18313 // OpenMP [2.13.9, Summary] 18314 // depend(dependence-type : vec), where dependence-type is: 18315 // 'sink' and where vec is the iteration vector, which has the form: 18316 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 18317 // where n is the value specified by the ordered clause in the loop 18318 // directive, xi denotes the loop iteration variable of the i-th nested 18319 // loop associated with the loop directive, and di is a constant 18320 // non-negative integer. 18321 if (CurContext->isDependentContext()) { 18322 // It will be analyzed later. 18323 Vars.push_back(RefExpr); 18324 continue; 18325 } 18326 SimpleExpr = SimpleExpr->IgnoreImplicit(); 18327 OverloadedOperatorKind OOK = OO_None; 18328 SourceLocation OOLoc; 18329 Expr *LHS = SimpleExpr; 18330 Expr *RHS = nullptr; 18331 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 18332 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 18333 OOLoc = BO->getOperatorLoc(); 18334 LHS = BO->getLHS()->IgnoreParenImpCasts(); 18335 RHS = BO->getRHS()->IgnoreParenImpCasts(); 18336 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 18337 OOK = OCE->getOperator(); 18338 OOLoc = OCE->getOperatorLoc(); 18339 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18340 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 18341 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 18342 OOK = MCE->getMethodDecl() 18343 ->getNameInfo() 18344 .getName() 18345 .getCXXOverloadedOperator(); 18346 OOLoc = MCE->getCallee()->getExprLoc(); 18347 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 18348 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 18349 } 18350 SourceLocation ELoc; 18351 SourceRange ERange; 18352 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 18353 if (Res.second) { 18354 // It will be analyzed later. 18355 Vars.push_back(RefExpr); 18356 } 18357 ValueDecl *D = Res.first; 18358 if (!D) 18359 continue; 18360 18361 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 18362 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 18363 continue; 18364 } 18365 if (RHS) { 18366 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 18367 RHS, OMPC_depend, /*StrictlyPositive=*/false); 18368 if (RHSRes.isInvalid()) 18369 continue; 18370 } 18371 if (!CurContext->isDependentContext() && 18372 DSAStack->getParentOrderedRegionParam().first && 18373 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 18374 const ValueDecl *VD = 18375 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 18376 if (VD) 18377 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 18378 << 1 << VD; 18379 else 18380 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 18381 continue; 18382 } 18383 OpsOffs.emplace_back(RHS, OOK); 18384 } else { 18385 bool OMPDependTFound = LangOpts.OpenMP >= 50; 18386 if (OMPDependTFound) 18387 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 18388 DepKind == OMPC_DEPEND_depobj); 18389 if (DepKind == OMPC_DEPEND_depobj) { 18390 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18391 // List items used in depend clauses with the depobj dependence type 18392 // must be expressions of the omp_depend_t type. 18393 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18394 !RefExpr->isInstantiationDependent() && 18395 !RefExpr->containsUnexpandedParameterPack() && 18396 (OMPDependTFound && 18397 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 18398 RefExpr->getType()))) { 18399 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18400 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 18401 continue; 18402 } 18403 if (!RefExpr->isLValue()) { 18404 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 18405 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 18406 continue; 18407 } 18408 } else { 18409 // OpenMP 5.0 [2.17.11, Restrictions] 18410 // List items used in depend clauses cannot be zero-length array 18411 // sections. 18412 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 18413 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 18414 if (OASE) { 18415 QualType BaseType = 18416 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 18417 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 18418 ExprTy = ATy->getElementType(); 18419 else 18420 ExprTy = BaseType->getPointeeType(); 18421 ExprTy = ExprTy.getNonReferenceType(); 18422 const Expr *Length = OASE->getLength(); 18423 Expr::EvalResult Result; 18424 if (Length && !Length->isValueDependent() && 18425 Length->EvaluateAsInt(Result, Context) && 18426 Result.Val.getInt().isZero()) { 18427 Diag(ELoc, 18428 diag::err_omp_depend_zero_length_array_section_not_allowed) 18429 << SimpleExpr->getSourceRange(); 18430 continue; 18431 } 18432 } 18433 18434 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 18435 // List items used in depend clauses with the in, out, inout or 18436 // mutexinoutset dependence types cannot be expressions of the 18437 // omp_depend_t type. 18438 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 18439 !RefExpr->isInstantiationDependent() && 18440 !RefExpr->containsUnexpandedParameterPack() && 18441 (OMPDependTFound && 18442 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 18443 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18444 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 18445 << RefExpr->getSourceRange(); 18446 continue; 18447 } 18448 18449 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 18450 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 18451 (ASE && !ASE->getBase()->isTypeDependent() && 18452 !ASE->getBase() 18453 ->getType() 18454 .getNonReferenceType() 18455 ->isPointerType() && 18456 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 18457 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18458 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18459 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18460 continue; 18461 } 18462 18463 ExprResult Res; 18464 { 18465 Sema::TentativeAnalysisScope Trap(*this); 18466 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 18467 RefExpr->IgnoreParenImpCasts()); 18468 } 18469 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 18470 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 18471 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 18472 << (LangOpts.OpenMP >= 50 ? 1 : 0) 18473 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 18474 continue; 18475 } 18476 } 18477 } 18478 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 18479 } 18480 18481 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 18482 TotalDepCount > VarList.size() && 18483 DSAStack->getParentOrderedRegionParam().first && 18484 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 18485 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 18486 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 18487 } 18488 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 18489 Vars.empty()) 18490 return nullptr; 18491 18492 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18493 DepModifier, DepKind, DepLoc, ColonLoc, 18494 Vars, TotalDepCount.getZExtValue()); 18495 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 18496 DSAStack->isParentOrderedRegion()) 18497 DSAStack->addDoacrossDependClause(C, OpsOffs); 18498 return C; 18499 } 18500 18501 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 18502 Expr *Device, SourceLocation StartLoc, 18503 SourceLocation LParenLoc, 18504 SourceLocation ModifierLoc, 18505 SourceLocation EndLoc) { 18506 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 18507 "Unexpected device modifier in OpenMP < 50."); 18508 18509 bool ErrorFound = false; 18510 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 18511 std::string Values = 18512 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 18513 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 18514 << Values << getOpenMPClauseName(OMPC_device); 18515 ErrorFound = true; 18516 } 18517 18518 Expr *ValExpr = Device; 18519 Stmt *HelperValStmt = nullptr; 18520 18521 // OpenMP [2.9.1, Restrictions] 18522 // The device expression must evaluate to a non-negative integer value. 18523 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 18524 /*StrictlyPositive=*/false) || 18525 ErrorFound; 18526 if (ErrorFound) 18527 return nullptr; 18528 18529 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18530 OpenMPDirectiveKind CaptureRegion = 18531 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 18532 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18533 ValExpr = MakeFullExpr(ValExpr).get(); 18534 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18535 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18536 HelperValStmt = buildPreInits(Context, Captures); 18537 } 18538 18539 return new (Context) 18540 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 18541 LParenLoc, ModifierLoc, EndLoc); 18542 } 18543 18544 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 18545 DSAStackTy *Stack, QualType QTy, 18546 bool FullCheck = true) { 18547 if (SemaRef.RequireCompleteType(SL, QTy, diag::err_incomplete_type)) 18548 return false; 18549 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 18550 !QTy.isTriviallyCopyableType(SemaRef.Context)) 18551 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 18552 return true; 18553 } 18554 18555 /// Return true if it can be proven that the provided array expression 18556 /// (array section or array subscript) does NOT specify the whole size of the 18557 /// array whose base type is \a BaseQTy. 18558 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 18559 const Expr *E, 18560 QualType BaseQTy) { 18561 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18562 18563 // If this is an array subscript, it refers to the whole size if the size of 18564 // the dimension is constant and equals 1. Also, an array section assumes the 18565 // format of an array subscript if no colon is used. 18566 if (isa<ArraySubscriptExpr>(E) || 18567 (OASE && OASE->getColonLocFirst().isInvalid())) { 18568 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18569 return ATy->getSize().getSExtValue() != 1; 18570 // Size can't be evaluated statically. 18571 return false; 18572 } 18573 18574 assert(OASE && "Expecting array section if not an array subscript."); 18575 const Expr *LowerBound = OASE->getLowerBound(); 18576 const Expr *Length = OASE->getLength(); 18577 18578 // If there is a lower bound that does not evaluates to zero, we are not 18579 // covering the whole dimension. 18580 if (LowerBound) { 18581 Expr::EvalResult Result; 18582 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 18583 return false; // Can't get the integer value as a constant. 18584 18585 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 18586 if (ConstLowerBound.getSExtValue()) 18587 return true; 18588 } 18589 18590 // If we don't have a length we covering the whole dimension. 18591 if (!Length) 18592 return false; 18593 18594 // If the base is a pointer, we don't have a way to get the size of the 18595 // pointee. 18596 if (BaseQTy->isPointerType()) 18597 return false; 18598 18599 // We can only check if the length is the same as the size of the dimension 18600 // if we have a constant array. 18601 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 18602 if (!CATy) 18603 return false; 18604 18605 Expr::EvalResult Result; 18606 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18607 return false; // Can't get the integer value as a constant. 18608 18609 llvm::APSInt ConstLength = Result.Val.getInt(); 18610 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 18611 } 18612 18613 // Return true if it can be proven that the provided array expression (array 18614 // section or array subscript) does NOT specify a single element of the array 18615 // whose base type is \a BaseQTy. 18616 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 18617 const Expr *E, 18618 QualType BaseQTy) { 18619 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 18620 18621 // An array subscript always refer to a single element. Also, an array section 18622 // assumes the format of an array subscript if no colon is used. 18623 if (isa<ArraySubscriptExpr>(E) || 18624 (OASE && OASE->getColonLocFirst().isInvalid())) 18625 return false; 18626 18627 assert(OASE && "Expecting array section if not an array subscript."); 18628 const Expr *Length = OASE->getLength(); 18629 18630 // If we don't have a length we have to check if the array has unitary size 18631 // for this dimension. Also, we should always expect a length if the base type 18632 // is pointer. 18633 if (!Length) { 18634 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 18635 return ATy->getSize().getSExtValue() != 1; 18636 // We cannot assume anything. 18637 return false; 18638 } 18639 18640 // Check if the length evaluates to 1. 18641 Expr::EvalResult Result; 18642 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 18643 return false; // Can't get the integer value as a constant. 18644 18645 llvm::APSInt ConstLength = Result.Val.getInt(); 18646 return ConstLength.getSExtValue() != 1; 18647 } 18648 18649 // The base of elements of list in a map clause have to be either: 18650 // - a reference to variable or field. 18651 // - a member expression. 18652 // - an array expression. 18653 // 18654 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 18655 // reference to 'r'. 18656 // 18657 // If we have: 18658 // 18659 // struct SS { 18660 // Bla S; 18661 // foo() { 18662 // #pragma omp target map (S.Arr[:12]); 18663 // } 18664 // } 18665 // 18666 // We want to retrieve the member expression 'this->S'; 18667 18668 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 18669 // If a list item is an array section, it must specify contiguous storage. 18670 // 18671 // For this restriction it is sufficient that we make sure only references 18672 // to variables or fields and array expressions, and that no array sections 18673 // exist except in the rightmost expression (unless they cover the whole 18674 // dimension of the array). E.g. these would be invalid: 18675 // 18676 // r.ArrS[3:5].Arr[6:7] 18677 // 18678 // r.ArrS[3:5].x 18679 // 18680 // but these would be valid: 18681 // r.ArrS[3].Arr[6:7] 18682 // 18683 // r.ArrS[3].x 18684 namespace { 18685 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 18686 Sema &SemaRef; 18687 OpenMPClauseKind CKind = OMPC_unknown; 18688 OpenMPDirectiveKind DKind = OMPD_unknown; 18689 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 18690 bool IsNonContiguous = false; 18691 bool NoDiagnose = false; 18692 const Expr *RelevantExpr = nullptr; 18693 bool AllowUnitySizeArraySection = true; 18694 bool AllowWholeSizeArraySection = true; 18695 bool AllowAnotherPtr = true; 18696 SourceLocation ELoc; 18697 SourceRange ERange; 18698 18699 void emitErrorMsg() { 18700 // If nothing else worked, this is not a valid map clause expression. 18701 if (SemaRef.getLangOpts().OpenMP < 50) { 18702 SemaRef.Diag(ELoc, 18703 diag::err_omp_expected_named_var_member_or_array_expression) 18704 << ERange; 18705 } else { 18706 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 18707 << getOpenMPClauseName(CKind) << ERange; 18708 } 18709 } 18710 18711 public: 18712 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 18713 if (!isa<VarDecl>(DRE->getDecl())) { 18714 emitErrorMsg(); 18715 return false; 18716 } 18717 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18718 RelevantExpr = DRE; 18719 // Record the component. 18720 Components.emplace_back(DRE, DRE->getDecl(), IsNonContiguous); 18721 return true; 18722 } 18723 18724 bool VisitMemberExpr(MemberExpr *ME) { 18725 Expr *E = ME; 18726 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 18727 18728 if (isa<CXXThisExpr>(BaseE)) { 18729 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18730 // We found a base expression: this->Val. 18731 RelevantExpr = ME; 18732 } else { 18733 E = BaseE; 18734 } 18735 18736 if (!isa<FieldDecl>(ME->getMemberDecl())) { 18737 if (!NoDiagnose) { 18738 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 18739 << ME->getSourceRange(); 18740 return false; 18741 } 18742 if (RelevantExpr) 18743 return false; 18744 return Visit(E); 18745 } 18746 18747 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 18748 18749 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 18750 // A bit-field cannot appear in a map clause. 18751 // 18752 if (FD->isBitField()) { 18753 if (!NoDiagnose) { 18754 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 18755 << ME->getSourceRange() << getOpenMPClauseName(CKind); 18756 return false; 18757 } 18758 if (RelevantExpr) 18759 return false; 18760 return Visit(E); 18761 } 18762 18763 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18764 // If the type of a list item is a reference to a type T then the type 18765 // will be considered to be T for all purposes of this clause. 18766 QualType CurType = BaseE->getType().getNonReferenceType(); 18767 18768 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 18769 // A list item cannot be a variable that is a member of a structure with 18770 // a union type. 18771 // 18772 if (CurType->isUnionType()) { 18773 if (!NoDiagnose) { 18774 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 18775 << ME->getSourceRange(); 18776 return false; 18777 } 18778 return RelevantExpr || Visit(E); 18779 } 18780 18781 // If we got a member expression, we should not expect any array section 18782 // before that: 18783 // 18784 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 18785 // If a list item is an element of a structure, only the rightmost symbol 18786 // of the variable reference can be an array section. 18787 // 18788 AllowUnitySizeArraySection = false; 18789 AllowWholeSizeArraySection = false; 18790 18791 // Record the component. 18792 Components.emplace_back(ME, FD, IsNonContiguous); 18793 return RelevantExpr || Visit(E); 18794 } 18795 18796 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 18797 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 18798 18799 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 18800 if (!NoDiagnose) { 18801 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18802 << 0 << AE->getSourceRange(); 18803 return false; 18804 } 18805 return RelevantExpr || Visit(E); 18806 } 18807 18808 // If we got an array subscript that express the whole dimension we 18809 // can have any array expressions before. If it only expressing part of 18810 // the dimension, we can only have unitary-size array expressions. 18811 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 18812 E->getType())) 18813 AllowWholeSizeArraySection = false; 18814 18815 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 18816 Expr::EvalResult Result; 18817 if (!AE->getIdx()->isValueDependent() && 18818 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 18819 !Result.Val.getInt().isZero()) { 18820 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18821 diag::err_omp_invalid_map_this_expr); 18822 SemaRef.Diag(AE->getIdx()->getExprLoc(), 18823 diag::note_omp_invalid_subscript_on_this_ptr_map); 18824 } 18825 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18826 RelevantExpr = TE; 18827 } 18828 18829 // Record the component - we don't have any declaration associated. 18830 Components.emplace_back(AE, nullptr, IsNonContiguous); 18831 18832 return RelevantExpr || Visit(E); 18833 } 18834 18835 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 18836 // After OMP 5.0 Array section in reduction clause will be implicitly 18837 // mapped 18838 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) && 18839 "Array sections cannot be implicitly mapped."); 18840 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 18841 QualType CurType = 18842 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 18843 18844 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 18845 // If the type of a list item is a reference to a type T then the type 18846 // will be considered to be T for all purposes of this clause. 18847 if (CurType->isReferenceType()) 18848 CurType = CurType->getPointeeType(); 18849 18850 bool IsPointer = CurType->isAnyPointerType(); 18851 18852 if (!IsPointer && !CurType->isArrayType()) { 18853 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 18854 << 0 << OASE->getSourceRange(); 18855 return false; 18856 } 18857 18858 bool NotWhole = 18859 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 18860 bool NotUnity = 18861 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 18862 18863 if (AllowWholeSizeArraySection) { 18864 // Any array section is currently allowed. Allowing a whole size array 18865 // section implies allowing a unity array section as well. 18866 // 18867 // If this array section refers to the whole dimension we can still 18868 // accept other array sections before this one, except if the base is a 18869 // pointer. Otherwise, only unitary sections are accepted. 18870 if (NotWhole || IsPointer) 18871 AllowWholeSizeArraySection = false; 18872 } else if (DKind == OMPD_target_update && 18873 SemaRef.getLangOpts().OpenMP >= 50) { 18874 if (IsPointer && !AllowAnotherPtr) 18875 SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined) 18876 << /*array of unknown bound */ 1; 18877 else 18878 IsNonContiguous = true; 18879 } else if (AllowUnitySizeArraySection && NotUnity) { 18880 // A unity or whole array section is not allowed and that is not 18881 // compatible with the properties of the current array section. 18882 if (NoDiagnose) 18883 return false; 18884 SemaRef.Diag( 18885 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 18886 << OASE->getSourceRange(); 18887 return false; 18888 } 18889 18890 if (IsPointer) 18891 AllowAnotherPtr = false; 18892 18893 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 18894 Expr::EvalResult ResultR; 18895 Expr::EvalResult ResultL; 18896 if (!OASE->getLength()->isValueDependent() && 18897 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 18898 !ResultR.Val.getInt().isOneValue()) { 18899 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18900 diag::err_omp_invalid_map_this_expr); 18901 SemaRef.Diag(OASE->getLength()->getExprLoc(), 18902 diag::note_omp_invalid_length_on_this_ptr_mapping); 18903 } 18904 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 18905 OASE->getLowerBound()->EvaluateAsInt(ResultL, 18906 SemaRef.getASTContext()) && 18907 !ResultL.Val.getInt().isZero()) { 18908 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18909 diag::err_omp_invalid_map_this_expr); 18910 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 18911 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 18912 } 18913 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18914 RelevantExpr = TE; 18915 } 18916 18917 // Record the component - we don't have any declaration associated. 18918 Components.emplace_back(OASE, nullptr, /*IsNonContiguous=*/false); 18919 return RelevantExpr || Visit(E); 18920 } 18921 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 18922 Expr *Base = E->getBase(); 18923 18924 // Record the component - we don't have any declaration associated. 18925 Components.emplace_back(E, nullptr, IsNonContiguous); 18926 18927 return Visit(Base->IgnoreParenImpCasts()); 18928 } 18929 18930 bool VisitUnaryOperator(UnaryOperator *UO) { 18931 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 18932 UO->getOpcode() != UO_Deref) { 18933 emitErrorMsg(); 18934 return false; 18935 } 18936 if (!RelevantExpr) { 18937 // Record the component if haven't found base decl. 18938 Components.emplace_back(UO, nullptr, /*IsNonContiguous=*/false); 18939 } 18940 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 18941 } 18942 bool VisitBinaryOperator(BinaryOperator *BO) { 18943 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 18944 emitErrorMsg(); 18945 return false; 18946 } 18947 18948 // Pointer arithmetic is the only thing we expect to happen here so after we 18949 // make sure the binary operator is a pointer type, the we only thing need 18950 // to to is to visit the subtree that has the same type as root (so that we 18951 // know the other subtree is just an offset) 18952 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 18953 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 18954 Components.emplace_back(BO, nullptr, false); 18955 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 18956 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 18957 "Either LHS or RHS have base decl inside"); 18958 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 18959 return RelevantExpr || Visit(LE); 18960 return RelevantExpr || Visit(RE); 18961 } 18962 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 18963 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18964 RelevantExpr = CTE; 18965 Components.emplace_back(CTE, nullptr, IsNonContiguous); 18966 return true; 18967 } 18968 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) { 18969 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 18970 Components.emplace_back(COCE, nullptr, IsNonContiguous); 18971 return true; 18972 } 18973 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) { 18974 Expr *Source = E->getSourceExpr(); 18975 if (!Source) { 18976 emitErrorMsg(); 18977 return false; 18978 } 18979 return Visit(Source); 18980 } 18981 bool VisitStmt(Stmt *) { 18982 emitErrorMsg(); 18983 return false; 18984 } 18985 const Expr *getFoundBase() const { 18986 return RelevantExpr; 18987 } 18988 explicit MapBaseChecker( 18989 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, 18990 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 18991 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 18992 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components), 18993 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 18994 }; 18995 } // namespace 18996 18997 /// Return the expression of the base of the mappable expression or null if it 18998 /// cannot be determined and do all the necessary checks to see if the expression 18999 /// is valid as a standalone mappable expression. In the process, record all the 19000 /// components of the expression. 19001 static const Expr *checkMapClauseExpressionBase( 19002 Sema &SemaRef, Expr *E, 19003 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 19004 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) { 19005 SourceLocation ELoc = E->getExprLoc(); 19006 SourceRange ERange = E->getSourceRange(); 19007 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc, 19008 ERange); 19009 if (Checker.Visit(E->IgnoreParens())) { 19010 // Check if the highest dimension array section has length specified 19011 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() && 19012 (CKind == OMPC_to || CKind == OMPC_from)) { 19013 auto CI = CurComponents.rbegin(); 19014 auto CE = CurComponents.rend(); 19015 for (; CI != CE; ++CI) { 19016 const auto *OASE = 19017 dyn_cast<OMPArraySectionExpr>(CI->getAssociatedExpression()); 19018 if (!OASE) 19019 continue; 19020 if (OASE && OASE->getLength()) 19021 break; 19022 SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length) 19023 << ERange; 19024 } 19025 } 19026 return Checker.getFoundBase(); 19027 } 19028 return nullptr; 19029 } 19030 19031 // Return true if expression E associated with value VD has conflicts with other 19032 // map information. 19033 static bool checkMapConflicts( 19034 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 19035 bool CurrentRegionOnly, 19036 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 19037 OpenMPClauseKind CKind) { 19038 assert(VD && E); 19039 SourceLocation ELoc = E->getExprLoc(); 19040 SourceRange ERange = E->getSourceRange(); 19041 19042 // In order to easily check the conflicts we need to match each component of 19043 // the expression under test with the components of the expressions that are 19044 // already in the stack. 19045 19046 assert(!CurComponents.empty() && "Map clause expression with no components!"); 19047 assert(CurComponents.back().getAssociatedDeclaration() == VD && 19048 "Map clause expression with unexpected base!"); 19049 19050 // Variables to help detecting enclosing problems in data environment nests. 19051 bool IsEnclosedByDataEnvironmentExpr = false; 19052 const Expr *EnclosingExpr = nullptr; 19053 19054 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 19055 VD, CurrentRegionOnly, 19056 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 19057 ERange, CKind, &EnclosingExpr, 19058 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 19059 StackComponents, 19060 OpenMPClauseKind Kind) { 19061 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50) 19062 return false; 19063 assert(!StackComponents.empty() && 19064 "Map clause expression with no components!"); 19065 assert(StackComponents.back().getAssociatedDeclaration() == VD && 19066 "Map clause expression with unexpected base!"); 19067 (void)VD; 19068 19069 // The whole expression in the stack. 19070 const Expr *RE = StackComponents.front().getAssociatedExpression(); 19071 19072 // Expressions must start from the same base. Here we detect at which 19073 // point both expressions diverge from each other and see if we can 19074 // detect if the memory referred to both expressions is contiguous and 19075 // do not overlap. 19076 auto CI = CurComponents.rbegin(); 19077 auto CE = CurComponents.rend(); 19078 auto SI = StackComponents.rbegin(); 19079 auto SE = StackComponents.rend(); 19080 for (; CI != CE && SI != SE; ++CI, ++SI) { 19081 19082 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 19083 // At most one list item can be an array item derived from a given 19084 // variable in map clauses of the same construct. 19085 if (CurrentRegionOnly && 19086 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 19087 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 19088 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 19089 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 19090 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 19091 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 19092 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 19093 diag::err_omp_multiple_array_items_in_map_clause) 19094 << CI->getAssociatedExpression()->getSourceRange(); 19095 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 19096 diag::note_used_here) 19097 << SI->getAssociatedExpression()->getSourceRange(); 19098 return true; 19099 } 19100 19101 // Do both expressions have the same kind? 19102 if (CI->getAssociatedExpression()->getStmtClass() != 19103 SI->getAssociatedExpression()->getStmtClass()) 19104 break; 19105 19106 // Are we dealing with different variables/fields? 19107 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 19108 break; 19109 } 19110 // Check if the extra components of the expressions in the enclosing 19111 // data environment are redundant for the current base declaration. 19112 // If they are, the maps completely overlap, which is legal. 19113 for (; SI != SE; ++SI) { 19114 QualType Type; 19115 if (const auto *ASE = 19116 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 19117 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 19118 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 19119 SI->getAssociatedExpression())) { 19120 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 19121 Type = 19122 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 19123 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 19124 SI->getAssociatedExpression())) { 19125 Type = OASE->getBase()->getType()->getPointeeType(); 19126 } 19127 if (Type.isNull() || Type->isAnyPointerType() || 19128 checkArrayExpressionDoesNotReferToWholeSize( 19129 SemaRef, SI->getAssociatedExpression(), Type)) 19130 break; 19131 } 19132 19133 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19134 // List items of map clauses in the same construct must not share 19135 // original storage. 19136 // 19137 // If the expressions are exactly the same or one is a subset of the 19138 // other, it means they are sharing storage. 19139 if (CI == CE && SI == SE) { 19140 if (CurrentRegionOnly) { 19141 if (CKind == OMPC_map) { 19142 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19143 } else { 19144 assert(CKind == OMPC_to || CKind == OMPC_from); 19145 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19146 << ERange; 19147 } 19148 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19149 << RE->getSourceRange(); 19150 return true; 19151 } 19152 // If we find the same expression in the enclosing data environment, 19153 // that is legal. 19154 IsEnclosedByDataEnvironmentExpr = true; 19155 return false; 19156 } 19157 19158 QualType DerivedType = 19159 std::prev(CI)->getAssociatedDeclaration()->getType(); 19160 SourceLocation DerivedLoc = 19161 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 19162 19163 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19164 // If the type of a list item is a reference to a type T then the type 19165 // will be considered to be T for all purposes of this clause. 19166 DerivedType = DerivedType.getNonReferenceType(); 19167 19168 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 19169 // A variable for which the type is pointer and an array section 19170 // derived from that variable must not appear as list items of map 19171 // clauses of the same construct. 19172 // 19173 // Also, cover one of the cases in: 19174 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19175 // If any part of the original storage of a list item has corresponding 19176 // storage in the device data environment, all of the original storage 19177 // must have corresponding storage in the device data environment. 19178 // 19179 if (DerivedType->isAnyPointerType()) { 19180 if (CI == CE || SI == SE) { 19181 SemaRef.Diag( 19182 DerivedLoc, 19183 diag::err_omp_pointer_mapped_along_with_derived_section) 19184 << DerivedLoc; 19185 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19186 << RE->getSourceRange(); 19187 return true; 19188 } 19189 if (CI->getAssociatedExpression()->getStmtClass() != 19190 SI->getAssociatedExpression()->getStmtClass() || 19191 CI->getAssociatedDeclaration()->getCanonicalDecl() == 19192 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 19193 assert(CI != CE && SI != SE); 19194 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 19195 << DerivedLoc; 19196 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19197 << RE->getSourceRange(); 19198 return true; 19199 } 19200 } 19201 19202 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 19203 // List items of map clauses in the same construct must not share 19204 // original storage. 19205 // 19206 // An expression is a subset of the other. 19207 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 19208 if (CKind == OMPC_map) { 19209 if (CI != CE || SI != SE) { 19210 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 19211 // a pointer. 19212 auto Begin = 19213 CI != CE ? CurComponents.begin() : StackComponents.begin(); 19214 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 19215 auto It = Begin; 19216 while (It != End && !It->getAssociatedDeclaration()) 19217 std::advance(It, 1); 19218 assert(It != End && 19219 "Expected at least one component with the declaration."); 19220 if (It != Begin && It->getAssociatedDeclaration() 19221 ->getType() 19222 .getCanonicalType() 19223 ->isAnyPointerType()) { 19224 IsEnclosedByDataEnvironmentExpr = false; 19225 EnclosingExpr = nullptr; 19226 return false; 19227 } 19228 } 19229 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 19230 } else { 19231 assert(CKind == OMPC_to || CKind == OMPC_from); 19232 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 19233 << ERange; 19234 } 19235 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 19236 << RE->getSourceRange(); 19237 return true; 19238 } 19239 19240 // The current expression uses the same base as other expression in the 19241 // data environment but does not contain it completely. 19242 if (!CurrentRegionOnly && SI != SE) 19243 EnclosingExpr = RE; 19244 19245 // The current expression is a subset of the expression in the data 19246 // environment. 19247 IsEnclosedByDataEnvironmentExpr |= 19248 (!CurrentRegionOnly && CI != CE && SI == SE); 19249 19250 return false; 19251 }); 19252 19253 if (CurrentRegionOnly) 19254 return FoundError; 19255 19256 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 19257 // If any part of the original storage of a list item has corresponding 19258 // storage in the device data environment, all of the original storage must 19259 // have corresponding storage in the device data environment. 19260 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 19261 // If a list item is an element of a structure, and a different element of 19262 // the structure has a corresponding list item in the device data environment 19263 // prior to a task encountering the construct associated with the map clause, 19264 // then the list item must also have a corresponding list item in the device 19265 // data environment prior to the task encountering the construct. 19266 // 19267 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 19268 SemaRef.Diag(ELoc, 19269 diag::err_omp_original_storage_is_shared_and_does_not_contain) 19270 << ERange; 19271 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 19272 << EnclosingExpr->getSourceRange(); 19273 return true; 19274 } 19275 19276 return FoundError; 19277 } 19278 19279 // Look up the user-defined mapper given the mapper name and mapped type, and 19280 // build a reference to it. 19281 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 19282 CXXScopeSpec &MapperIdScopeSpec, 19283 const DeclarationNameInfo &MapperId, 19284 QualType Type, 19285 Expr *UnresolvedMapper) { 19286 if (MapperIdScopeSpec.isInvalid()) 19287 return ExprError(); 19288 // Get the actual type for the array type. 19289 if (Type->isArrayType()) { 19290 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 19291 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 19292 } 19293 // Find all user-defined mappers with the given MapperId. 19294 SmallVector<UnresolvedSet<8>, 4> Lookups; 19295 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 19296 Lookup.suppressDiagnostics(); 19297 if (S) { 19298 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 19299 NamedDecl *D = Lookup.getRepresentativeDecl(); 19300 while (S && !S->isDeclScope(D)) 19301 S = S->getParent(); 19302 if (S) 19303 S = S->getParent(); 19304 Lookups.emplace_back(); 19305 Lookups.back().append(Lookup.begin(), Lookup.end()); 19306 Lookup.clear(); 19307 } 19308 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 19309 // Extract the user-defined mappers with the given MapperId. 19310 Lookups.push_back(UnresolvedSet<8>()); 19311 for (NamedDecl *D : ULE->decls()) { 19312 auto *DMD = cast<OMPDeclareMapperDecl>(D); 19313 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 19314 Lookups.back().addDecl(DMD); 19315 } 19316 } 19317 // Defer the lookup for dependent types. The results will be passed through 19318 // UnresolvedMapper on instantiation. 19319 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 19320 Type->isInstantiationDependentType() || 19321 Type->containsUnexpandedParameterPack() || 19322 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 19323 return !D->isInvalidDecl() && 19324 (D->getType()->isDependentType() || 19325 D->getType()->isInstantiationDependentType() || 19326 D->getType()->containsUnexpandedParameterPack()); 19327 })) { 19328 UnresolvedSet<8> URS; 19329 for (const UnresolvedSet<8> &Set : Lookups) { 19330 if (Set.empty()) 19331 continue; 19332 URS.append(Set.begin(), Set.end()); 19333 } 19334 return UnresolvedLookupExpr::Create( 19335 SemaRef.Context, /*NamingClass=*/nullptr, 19336 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 19337 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 19338 } 19339 SourceLocation Loc = MapperId.getLoc(); 19340 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 19341 // The type must be of struct, union or class type in C and C++ 19342 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 19343 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 19344 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 19345 return ExprError(); 19346 } 19347 // Perform argument dependent lookup. 19348 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 19349 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 19350 // Return the first user-defined mapper with the desired type. 19351 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19352 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 19353 if (!D->isInvalidDecl() && 19354 SemaRef.Context.hasSameType(D->getType(), Type)) 19355 return D; 19356 return nullptr; 19357 })) 19358 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19359 // Find the first user-defined mapper with a type derived from the desired 19360 // type. 19361 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 19362 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 19363 if (!D->isInvalidDecl() && 19364 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 19365 !Type.isMoreQualifiedThan(D->getType())) 19366 return D; 19367 return nullptr; 19368 })) { 19369 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 19370 /*DetectVirtual=*/false); 19371 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 19372 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 19373 VD->getType().getUnqualifiedType()))) { 19374 if (SemaRef.CheckBaseClassAccess( 19375 Loc, VD->getType(), Type, Paths.front(), 19376 /*DiagID=*/0) != Sema::AR_inaccessible) { 19377 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 19378 } 19379 } 19380 } 19381 } 19382 // Report error if a mapper is specified, but cannot be found. 19383 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 19384 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 19385 << Type << MapperId.getName(); 19386 return ExprError(); 19387 } 19388 return ExprEmpty(); 19389 } 19390 19391 namespace { 19392 // Utility struct that gathers all the related lists associated with a mappable 19393 // expression. 19394 struct MappableVarListInfo { 19395 // The list of expressions. 19396 ArrayRef<Expr *> VarList; 19397 // The list of processed expressions. 19398 SmallVector<Expr *, 16> ProcessedVarList; 19399 // The mappble components for each expression. 19400 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 19401 // The base declaration of the variable. 19402 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 19403 // The reference to the user-defined mapper associated with every expression. 19404 SmallVector<Expr *, 16> UDMapperList; 19405 19406 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 19407 // We have a list of components and base declarations for each entry in the 19408 // variable list. 19409 VarComponents.reserve(VarList.size()); 19410 VarBaseDeclarations.reserve(VarList.size()); 19411 } 19412 }; 19413 } 19414 19415 // Check the validity of the provided variable list for the provided clause kind 19416 // \a CKind. In the check process the valid expressions, mappable expression 19417 // components, variables, and user-defined mappers are extracted and used to 19418 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 19419 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 19420 // and \a MapperId are expected to be valid if the clause kind is 'map'. 19421 static void checkMappableExpressionList( 19422 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 19423 MappableVarListInfo &MVLI, SourceLocation StartLoc, 19424 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 19425 ArrayRef<Expr *> UnresolvedMappers, 19426 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 19427 ArrayRef<OpenMPMapModifierKind> Modifiers = None, 19428 bool IsMapTypeImplicit = false, bool NoDiagnose = false) { 19429 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 19430 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 19431 "Unexpected clause kind with mappable expressions!"); 19432 19433 // If the identifier of user-defined mapper is not specified, it is "default". 19434 // We do not change the actual name in this clause to distinguish whether a 19435 // mapper is specified explicitly, i.e., it is not explicitly specified when 19436 // MapperId.getName() is empty. 19437 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 19438 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 19439 MapperId.setName(DeclNames.getIdentifier( 19440 &SemaRef.getASTContext().Idents.get("default"))); 19441 MapperId.setLoc(StartLoc); 19442 } 19443 19444 // Iterators to find the current unresolved mapper expression. 19445 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 19446 bool UpdateUMIt = false; 19447 Expr *UnresolvedMapper = nullptr; 19448 19449 bool HasHoldModifier = 19450 Modifiers.end() != std::find(Modifiers.begin(), Modifiers.end(), 19451 OMPC_MAP_MODIFIER_ompx_hold); 19452 19453 // Keep track of the mappable components and base declarations in this clause. 19454 // Each entry in the list is going to have a list of components associated. We 19455 // record each set of the components so that we can build the clause later on. 19456 // In the end we should have the same amount of declarations and component 19457 // lists. 19458 19459 for (Expr *RE : MVLI.VarList) { 19460 assert(RE && "Null expr in omp to/from/map clause"); 19461 SourceLocation ELoc = RE->getExprLoc(); 19462 19463 // Find the current unresolved mapper expression. 19464 if (UpdateUMIt && UMIt != UMEnd) { 19465 UMIt++; 19466 assert( 19467 UMIt != UMEnd && 19468 "Expect the size of UnresolvedMappers to match with that of VarList"); 19469 } 19470 UpdateUMIt = true; 19471 if (UMIt != UMEnd) 19472 UnresolvedMapper = *UMIt; 19473 19474 const Expr *VE = RE->IgnoreParenLValueCasts(); 19475 19476 if (VE->isValueDependent() || VE->isTypeDependent() || 19477 VE->isInstantiationDependent() || 19478 VE->containsUnexpandedParameterPack()) { 19479 // Try to find the associated user-defined mapper. 19480 ExprResult ER = buildUserDefinedMapperRef( 19481 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19482 VE->getType().getCanonicalType(), UnresolvedMapper); 19483 if (ER.isInvalid()) 19484 continue; 19485 MVLI.UDMapperList.push_back(ER.get()); 19486 // We can only analyze this information once the missing information is 19487 // resolved. 19488 MVLI.ProcessedVarList.push_back(RE); 19489 continue; 19490 } 19491 19492 Expr *SimpleExpr = RE->IgnoreParenCasts(); 19493 19494 if (!RE->isLValue()) { 19495 if (SemaRef.getLangOpts().OpenMP < 50) { 19496 SemaRef.Diag( 19497 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 19498 << RE->getSourceRange(); 19499 } else { 19500 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 19501 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 19502 } 19503 continue; 19504 } 19505 19506 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 19507 ValueDecl *CurDeclaration = nullptr; 19508 19509 // Obtain the array or member expression bases if required. Also, fill the 19510 // components array with all the components identified in the process. 19511 const Expr *BE = 19512 checkMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind, 19513 DSAS->getCurrentDirective(), NoDiagnose); 19514 if (!BE) 19515 continue; 19516 19517 assert(!CurComponents.empty() && 19518 "Invalid mappable expression information."); 19519 19520 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 19521 // Add store "this" pointer to class in DSAStackTy for future checking 19522 DSAS->addMappedClassesQualTypes(TE->getType()); 19523 // Try to find the associated user-defined mapper. 19524 ExprResult ER = buildUserDefinedMapperRef( 19525 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19526 VE->getType().getCanonicalType(), UnresolvedMapper); 19527 if (ER.isInvalid()) 19528 continue; 19529 MVLI.UDMapperList.push_back(ER.get()); 19530 // Skip restriction checking for variable or field declarations 19531 MVLI.ProcessedVarList.push_back(RE); 19532 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19533 MVLI.VarComponents.back().append(CurComponents.begin(), 19534 CurComponents.end()); 19535 MVLI.VarBaseDeclarations.push_back(nullptr); 19536 continue; 19537 } 19538 19539 // For the following checks, we rely on the base declaration which is 19540 // expected to be associated with the last component. The declaration is 19541 // expected to be a variable or a field (if 'this' is being mapped). 19542 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 19543 assert(CurDeclaration && "Null decl on map clause."); 19544 assert( 19545 CurDeclaration->isCanonicalDecl() && 19546 "Expecting components to have associated only canonical declarations."); 19547 19548 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 19549 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 19550 19551 assert((VD || FD) && "Only variables or fields are expected here!"); 19552 (void)FD; 19553 19554 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 19555 // threadprivate variables cannot appear in a map clause. 19556 // OpenMP 4.5 [2.10.5, target update Construct] 19557 // threadprivate variables cannot appear in a from clause. 19558 if (VD && DSAS->isThreadPrivate(VD)) { 19559 if (NoDiagnose) 19560 continue; 19561 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19562 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 19563 << getOpenMPClauseName(CKind); 19564 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 19565 continue; 19566 } 19567 19568 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19569 // A list item cannot appear in both a map clause and a data-sharing 19570 // attribute clause on the same construct. 19571 19572 // Check conflicts with other map clause expressions. We check the conflicts 19573 // with the current construct separately from the enclosing data 19574 // environment, because the restrictions are different. We only have to 19575 // check conflicts across regions for the map clauses. 19576 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19577 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 19578 break; 19579 if (CKind == OMPC_map && 19580 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) && 19581 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 19582 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 19583 break; 19584 19585 // OpenMP 4.5 [2.10.5, target update Construct] 19586 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 19587 // If the type of a list item is a reference to a type T then the type will 19588 // be considered to be T for all purposes of this clause. 19589 auto I = llvm::find_if( 19590 CurComponents, 19591 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 19592 return MC.getAssociatedDeclaration(); 19593 }); 19594 assert(I != CurComponents.end() && "Null decl on map clause."); 19595 (void)I; 19596 QualType Type; 19597 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 19598 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 19599 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 19600 if (ASE) { 19601 Type = ASE->getType().getNonReferenceType(); 19602 } else if (OASE) { 19603 QualType BaseType = 19604 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 19605 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 19606 Type = ATy->getElementType(); 19607 else 19608 Type = BaseType->getPointeeType(); 19609 Type = Type.getNonReferenceType(); 19610 } else if (OAShE) { 19611 Type = OAShE->getBase()->getType()->getPointeeType(); 19612 } else { 19613 Type = VE->getType(); 19614 } 19615 19616 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 19617 // A list item in a to or from clause must have a mappable type. 19618 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 19619 // A list item must have a mappable type. 19620 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 19621 DSAS, Type, /*FullCheck=*/true)) 19622 continue; 19623 19624 if (CKind == OMPC_map) { 19625 // target enter data 19626 // OpenMP [2.10.2, Restrictions, p. 99] 19627 // A map-type must be specified in all map clauses and must be either 19628 // to or alloc. 19629 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 19630 if (DKind == OMPD_target_enter_data && 19631 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 19632 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19633 << (IsMapTypeImplicit ? 1 : 0) 19634 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19635 << getOpenMPDirectiveName(DKind); 19636 continue; 19637 } 19638 19639 // target exit_data 19640 // OpenMP [2.10.3, Restrictions, p. 102] 19641 // A map-type must be specified in all map clauses and must be either 19642 // from, release, or delete. 19643 if (DKind == OMPD_target_exit_data && 19644 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 19645 MapType == OMPC_MAP_delete)) { 19646 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19647 << (IsMapTypeImplicit ? 1 : 0) 19648 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19649 << getOpenMPDirectiveName(DKind); 19650 continue; 19651 } 19652 19653 // The 'ompx_hold' modifier is specifically intended to be used on a 19654 // 'target' or 'target data' directive to prevent data from being unmapped 19655 // during the associated statement. It is not permitted on a 'target 19656 // enter data' or 'target exit data' directive, which have no associated 19657 // statement. 19658 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) && 19659 HasHoldModifier) { 19660 SemaRef.Diag(StartLoc, 19661 diag::err_omp_invalid_map_type_modifier_for_directive) 19662 << getOpenMPSimpleClauseTypeName(OMPC_map, 19663 OMPC_MAP_MODIFIER_ompx_hold) 19664 << getOpenMPDirectiveName(DKind); 19665 continue; 19666 } 19667 19668 // target, target data 19669 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 19670 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 19671 // A map-type in a map clause must be to, from, tofrom or alloc 19672 if ((DKind == OMPD_target_data || 19673 isOpenMPTargetExecutionDirective(DKind)) && 19674 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 19675 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 19676 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 19677 << (IsMapTypeImplicit ? 1 : 0) 19678 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 19679 << getOpenMPDirectiveName(DKind); 19680 continue; 19681 } 19682 19683 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 19684 // A list item cannot appear in both a map clause and a data-sharing 19685 // attribute clause on the same construct 19686 // 19687 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 19688 // A list item cannot appear in both a map clause and a data-sharing 19689 // attribute clause on the same construct unless the construct is a 19690 // combined construct. 19691 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 19692 isOpenMPTargetExecutionDirective(DKind)) || 19693 DKind == OMPD_target)) { 19694 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 19695 if (isOpenMPPrivate(DVar.CKind)) { 19696 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 19697 << getOpenMPClauseName(DVar.CKind) 19698 << getOpenMPClauseName(OMPC_map) 19699 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 19700 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 19701 continue; 19702 } 19703 } 19704 } 19705 19706 // Try to find the associated user-defined mapper. 19707 ExprResult ER = buildUserDefinedMapperRef( 19708 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 19709 Type.getCanonicalType(), UnresolvedMapper); 19710 if (ER.isInvalid()) 19711 continue; 19712 MVLI.UDMapperList.push_back(ER.get()); 19713 19714 // Save the current expression. 19715 MVLI.ProcessedVarList.push_back(RE); 19716 19717 // Store the components in the stack so that they can be used to check 19718 // against other clauses later on. 19719 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 19720 /*WhereFoundClauseKind=*/OMPC_map); 19721 19722 // Save the components and declaration to create the clause. For purposes of 19723 // the clause creation, any component list that has has base 'this' uses 19724 // null as base declaration. 19725 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 19726 MVLI.VarComponents.back().append(CurComponents.begin(), 19727 CurComponents.end()); 19728 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 19729 : CurDeclaration); 19730 } 19731 } 19732 19733 OMPClause *Sema::ActOnOpenMPMapClause( 19734 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 19735 ArrayRef<SourceLocation> MapTypeModifiersLoc, 19736 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 19737 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 19738 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 19739 const OMPVarListLocTy &Locs, bool NoDiagnose, 19740 ArrayRef<Expr *> UnresolvedMappers) { 19741 OpenMPMapModifierKind Modifiers[] = { 19742 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19743 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown, 19744 OMPC_MAP_MODIFIER_unknown}; 19745 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 19746 19747 // Process map-type-modifiers, flag errors for duplicate modifiers. 19748 unsigned Count = 0; 19749 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 19750 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 19751 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 19752 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 19753 continue; 19754 } 19755 assert(Count < NumberOfOMPMapClauseModifiers && 19756 "Modifiers exceed the allowed number of map type modifiers"); 19757 Modifiers[Count] = MapTypeModifiers[I]; 19758 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 19759 ++Count; 19760 } 19761 19762 MappableVarListInfo MVLI(VarList); 19763 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 19764 MapperIdScopeSpec, MapperId, UnresolvedMappers, 19765 MapType, Modifiers, IsMapTypeImplicit, 19766 NoDiagnose); 19767 19768 // We need to produce a map clause even if we don't have variables so that 19769 // other diagnostics related with non-existing map clauses are accurate. 19770 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 19771 MVLI.VarBaseDeclarations, MVLI.VarComponents, 19772 MVLI.UDMapperList, Modifiers, ModifiersLoc, 19773 MapperIdScopeSpec.getWithLocInContext(Context), 19774 MapperId, MapType, IsMapTypeImplicit, MapLoc); 19775 } 19776 19777 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 19778 TypeResult ParsedType) { 19779 assert(ParsedType.isUsable()); 19780 19781 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 19782 if (ReductionType.isNull()) 19783 return QualType(); 19784 19785 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 19786 // A type name in a declare reduction directive cannot be a function type, an 19787 // array type, a reference type, or a type qualified with const, volatile or 19788 // restrict. 19789 if (ReductionType.hasQualifiers()) { 19790 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 19791 return QualType(); 19792 } 19793 19794 if (ReductionType->isFunctionType()) { 19795 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 19796 return QualType(); 19797 } 19798 if (ReductionType->isReferenceType()) { 19799 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 19800 return QualType(); 19801 } 19802 if (ReductionType->isArrayType()) { 19803 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 19804 return QualType(); 19805 } 19806 return ReductionType; 19807 } 19808 19809 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 19810 Scope *S, DeclContext *DC, DeclarationName Name, 19811 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 19812 AccessSpecifier AS, Decl *PrevDeclInScope) { 19813 SmallVector<Decl *, 8> Decls; 19814 Decls.reserve(ReductionTypes.size()); 19815 19816 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 19817 forRedeclarationInCurContext()); 19818 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 19819 // A reduction-identifier may not be re-declared in the current scope for the 19820 // same type or for a type that is compatible according to the base language 19821 // rules. 19822 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 19823 OMPDeclareReductionDecl *PrevDRD = nullptr; 19824 bool InCompoundScope = true; 19825 if (S != nullptr) { 19826 // Find previous declaration with the same name not referenced in other 19827 // declarations. 19828 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 19829 InCompoundScope = 19830 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 19831 LookupName(Lookup, S); 19832 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 19833 /*AllowInlineNamespace=*/false); 19834 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 19835 LookupResult::Filter Filter = Lookup.makeFilter(); 19836 while (Filter.hasNext()) { 19837 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 19838 if (InCompoundScope) { 19839 auto I = UsedAsPrevious.find(PrevDecl); 19840 if (I == UsedAsPrevious.end()) 19841 UsedAsPrevious[PrevDecl] = false; 19842 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 19843 UsedAsPrevious[D] = true; 19844 } 19845 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 19846 PrevDecl->getLocation(); 19847 } 19848 Filter.done(); 19849 if (InCompoundScope) { 19850 for (const auto &PrevData : UsedAsPrevious) { 19851 if (!PrevData.second) { 19852 PrevDRD = PrevData.first; 19853 break; 19854 } 19855 } 19856 } 19857 } else if (PrevDeclInScope != nullptr) { 19858 auto *PrevDRDInScope = PrevDRD = 19859 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 19860 do { 19861 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 19862 PrevDRDInScope->getLocation(); 19863 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 19864 } while (PrevDRDInScope != nullptr); 19865 } 19866 for (const auto &TyData : ReductionTypes) { 19867 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 19868 bool Invalid = false; 19869 if (I != PreviousRedeclTypes.end()) { 19870 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 19871 << TyData.first; 19872 Diag(I->second, diag::note_previous_definition); 19873 Invalid = true; 19874 } 19875 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 19876 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 19877 Name, TyData.first, PrevDRD); 19878 DC->addDecl(DRD); 19879 DRD->setAccess(AS); 19880 Decls.push_back(DRD); 19881 if (Invalid) 19882 DRD->setInvalidDecl(); 19883 else 19884 PrevDRD = DRD; 19885 } 19886 19887 return DeclGroupPtrTy::make( 19888 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 19889 } 19890 19891 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 19892 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19893 19894 // Enter new function scope. 19895 PushFunctionScope(); 19896 setFunctionHasBranchProtectedScope(); 19897 getCurFunction()->setHasOMPDeclareReductionCombiner(); 19898 19899 if (S != nullptr) 19900 PushDeclContext(S, DRD); 19901 else 19902 CurContext = DRD; 19903 19904 PushExpressionEvaluationContext( 19905 ExpressionEvaluationContext::PotentiallyEvaluated); 19906 19907 QualType ReductionType = DRD->getType(); 19908 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 19909 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 19910 // uses semantics of argument handles by value, but it should be passed by 19911 // reference. C lang does not support references, so pass all parameters as 19912 // pointers. 19913 // Create 'T omp_in;' variable. 19914 VarDecl *OmpInParm = 19915 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 19916 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 19917 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 19918 // uses semantics of argument handles by value, but it should be passed by 19919 // reference. C lang does not support references, so pass all parameters as 19920 // pointers. 19921 // Create 'T omp_out;' variable. 19922 VarDecl *OmpOutParm = 19923 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 19924 if (S != nullptr) { 19925 PushOnScopeChains(OmpInParm, S); 19926 PushOnScopeChains(OmpOutParm, S); 19927 } else { 19928 DRD->addDecl(OmpInParm); 19929 DRD->addDecl(OmpOutParm); 19930 } 19931 Expr *InE = 19932 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 19933 Expr *OutE = 19934 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 19935 DRD->setCombinerData(InE, OutE); 19936 } 19937 19938 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 19939 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19940 DiscardCleanupsInEvaluationContext(); 19941 PopExpressionEvaluationContext(); 19942 19943 PopDeclContext(); 19944 PopFunctionScopeInfo(); 19945 19946 if (Combiner != nullptr) 19947 DRD->setCombiner(Combiner); 19948 else 19949 DRD->setInvalidDecl(); 19950 } 19951 19952 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 19953 auto *DRD = cast<OMPDeclareReductionDecl>(D); 19954 19955 // Enter new function scope. 19956 PushFunctionScope(); 19957 setFunctionHasBranchProtectedScope(); 19958 19959 if (S != nullptr) 19960 PushDeclContext(S, DRD); 19961 else 19962 CurContext = DRD; 19963 19964 PushExpressionEvaluationContext( 19965 ExpressionEvaluationContext::PotentiallyEvaluated); 19966 19967 QualType ReductionType = DRD->getType(); 19968 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 19969 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 19970 // uses semantics of argument handles by value, but it should be passed by 19971 // reference. C lang does not support references, so pass all parameters as 19972 // pointers. 19973 // Create 'T omp_priv;' variable. 19974 VarDecl *OmpPrivParm = 19975 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 19976 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 19977 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 19978 // uses semantics of argument handles by value, but it should be passed by 19979 // reference. C lang does not support references, so pass all parameters as 19980 // pointers. 19981 // Create 'T omp_orig;' variable. 19982 VarDecl *OmpOrigParm = 19983 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 19984 if (S != nullptr) { 19985 PushOnScopeChains(OmpPrivParm, S); 19986 PushOnScopeChains(OmpOrigParm, S); 19987 } else { 19988 DRD->addDecl(OmpPrivParm); 19989 DRD->addDecl(OmpOrigParm); 19990 } 19991 Expr *OrigE = 19992 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 19993 Expr *PrivE = 19994 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 19995 DRD->setInitializerData(OrigE, PrivE); 19996 return OmpPrivParm; 19997 } 19998 19999 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 20000 VarDecl *OmpPrivParm) { 20001 auto *DRD = cast<OMPDeclareReductionDecl>(D); 20002 DiscardCleanupsInEvaluationContext(); 20003 PopExpressionEvaluationContext(); 20004 20005 PopDeclContext(); 20006 PopFunctionScopeInfo(); 20007 20008 if (Initializer != nullptr) { 20009 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 20010 } else if (OmpPrivParm->hasInit()) { 20011 DRD->setInitializer(OmpPrivParm->getInit(), 20012 OmpPrivParm->isDirectInit() 20013 ? OMPDeclareReductionDecl::DirectInit 20014 : OMPDeclareReductionDecl::CopyInit); 20015 } else { 20016 DRD->setInvalidDecl(); 20017 } 20018 } 20019 20020 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 20021 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 20022 for (Decl *D : DeclReductions.get()) { 20023 if (IsValid) { 20024 if (S) 20025 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 20026 /*AddToContext=*/false); 20027 } else { 20028 D->setInvalidDecl(); 20029 } 20030 } 20031 return DeclReductions; 20032 } 20033 20034 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 20035 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 20036 QualType T = TInfo->getType(); 20037 if (D.isInvalidType()) 20038 return true; 20039 20040 if (getLangOpts().CPlusPlus) { 20041 // Check that there are no default arguments (C++ only). 20042 CheckExtraCXXDefaultArguments(D); 20043 } 20044 20045 return CreateParsedType(T, TInfo); 20046 } 20047 20048 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 20049 TypeResult ParsedType) { 20050 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 20051 20052 QualType MapperType = GetTypeFromParser(ParsedType.get()); 20053 assert(!MapperType.isNull() && "Expect valid mapper type"); 20054 20055 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20056 // The type must be of struct, union or class type in C and C++ 20057 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 20058 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 20059 return QualType(); 20060 } 20061 return MapperType; 20062 } 20063 20064 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareMapperDirective( 20065 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 20066 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 20067 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) { 20068 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 20069 forRedeclarationInCurContext()); 20070 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 20071 // A mapper-identifier may not be redeclared in the current scope for the 20072 // same type or for a type that is compatible according to the base language 20073 // rules. 20074 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 20075 OMPDeclareMapperDecl *PrevDMD = nullptr; 20076 bool InCompoundScope = true; 20077 if (S != nullptr) { 20078 // Find previous declaration with the same name not referenced in other 20079 // declarations. 20080 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 20081 InCompoundScope = 20082 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 20083 LookupName(Lookup, S); 20084 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 20085 /*AllowInlineNamespace=*/false); 20086 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 20087 LookupResult::Filter Filter = Lookup.makeFilter(); 20088 while (Filter.hasNext()) { 20089 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 20090 if (InCompoundScope) { 20091 auto I = UsedAsPrevious.find(PrevDecl); 20092 if (I == UsedAsPrevious.end()) 20093 UsedAsPrevious[PrevDecl] = false; 20094 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 20095 UsedAsPrevious[D] = true; 20096 } 20097 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 20098 PrevDecl->getLocation(); 20099 } 20100 Filter.done(); 20101 if (InCompoundScope) { 20102 for (const auto &PrevData : UsedAsPrevious) { 20103 if (!PrevData.second) { 20104 PrevDMD = PrevData.first; 20105 break; 20106 } 20107 } 20108 } 20109 } else if (PrevDeclInScope) { 20110 auto *PrevDMDInScope = PrevDMD = 20111 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 20112 do { 20113 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 20114 PrevDMDInScope->getLocation(); 20115 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 20116 } while (PrevDMDInScope != nullptr); 20117 } 20118 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 20119 bool Invalid = false; 20120 if (I != PreviousRedeclTypes.end()) { 20121 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 20122 << MapperType << Name; 20123 Diag(I->second, diag::note_previous_definition); 20124 Invalid = true; 20125 } 20126 // Build expressions for implicit maps of data members with 'default' 20127 // mappers. 20128 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses.begin(), 20129 Clauses.end()); 20130 if (LangOpts.OpenMP >= 50) 20131 processImplicitMapsWithDefaultMappers(*this, DSAStack, ClausesWithImplicit); 20132 auto *DMD = 20133 OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, MapperType, VN, 20134 ClausesWithImplicit, PrevDMD); 20135 if (S) 20136 PushOnScopeChains(DMD, S); 20137 else 20138 DC->addDecl(DMD); 20139 DMD->setAccess(AS); 20140 if (Invalid) 20141 DMD->setInvalidDecl(); 20142 20143 auto *VD = cast<DeclRefExpr>(MapperVarRef)->getDecl(); 20144 VD->setDeclContext(DMD); 20145 VD->setLexicalDeclContext(DMD); 20146 DMD->addDecl(VD); 20147 DMD->setMapperVarRef(MapperVarRef); 20148 20149 return DeclGroupPtrTy::make(DeclGroupRef(DMD)); 20150 } 20151 20152 ExprResult 20153 Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(Scope *S, QualType MapperType, 20154 SourceLocation StartLoc, 20155 DeclarationName VN) { 20156 TypeSourceInfo *TInfo = 20157 Context.getTrivialTypeSourceInfo(MapperType, StartLoc); 20158 auto *VD = VarDecl::Create(Context, Context.getTranslationUnitDecl(), 20159 StartLoc, StartLoc, VN.getAsIdentifierInfo(), 20160 MapperType, TInfo, SC_None); 20161 if (S) 20162 PushOnScopeChains(VD, S, /*AddToContext=*/false); 20163 Expr *E = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 20164 DSAStack->addDeclareMapperVarRef(E); 20165 return E; 20166 } 20167 20168 bool Sema::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const { 20169 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20170 const Expr *Ref = DSAStack->getDeclareMapperVarRef(); 20171 if (const auto *DRE = cast_or_null<DeclRefExpr>(Ref)) { 20172 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl()) 20173 return true; 20174 if (VD->isUsableInConstantExpressions(Context)) 20175 return true; 20176 return false; 20177 } 20178 return true; 20179 } 20180 20181 const ValueDecl *Sema::getOpenMPDeclareMapperVarName() const { 20182 assert(LangOpts.OpenMP && "Expected OpenMP mode."); 20183 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl(); 20184 } 20185 20186 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 20187 SourceLocation StartLoc, 20188 SourceLocation LParenLoc, 20189 SourceLocation EndLoc) { 20190 Expr *ValExpr = NumTeams; 20191 Stmt *HelperValStmt = nullptr; 20192 20193 // OpenMP [teams Constrcut, Restrictions] 20194 // The num_teams expression must evaluate to a positive integer value. 20195 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 20196 /*StrictlyPositive=*/true)) 20197 return nullptr; 20198 20199 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20200 OpenMPDirectiveKind CaptureRegion = 20201 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 20202 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20203 ValExpr = MakeFullExpr(ValExpr).get(); 20204 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20205 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20206 HelperValStmt = buildPreInits(Context, Captures); 20207 } 20208 20209 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 20210 StartLoc, LParenLoc, EndLoc); 20211 } 20212 20213 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 20214 SourceLocation StartLoc, 20215 SourceLocation LParenLoc, 20216 SourceLocation EndLoc) { 20217 Expr *ValExpr = ThreadLimit; 20218 Stmt *HelperValStmt = nullptr; 20219 20220 // OpenMP [teams Constrcut, Restrictions] 20221 // The thread_limit expression must evaluate to a positive integer value. 20222 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 20223 /*StrictlyPositive=*/true)) 20224 return nullptr; 20225 20226 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 20227 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 20228 DKind, OMPC_thread_limit, LangOpts.OpenMP); 20229 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 20230 ValExpr = MakeFullExpr(ValExpr).get(); 20231 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20232 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20233 HelperValStmt = buildPreInits(Context, Captures); 20234 } 20235 20236 return new (Context) OMPThreadLimitClause( 20237 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 20238 } 20239 20240 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 20241 SourceLocation StartLoc, 20242 SourceLocation LParenLoc, 20243 SourceLocation EndLoc) { 20244 Expr *ValExpr = Priority; 20245 Stmt *HelperValStmt = nullptr; 20246 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20247 20248 // OpenMP [2.9.1, task Constrcut] 20249 // The priority-value is a non-negative numerical scalar expression. 20250 if (!isNonNegativeIntegerValue( 20251 ValExpr, *this, OMPC_priority, 20252 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 20253 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20254 return nullptr; 20255 20256 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 20257 StartLoc, LParenLoc, EndLoc); 20258 } 20259 20260 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 20261 SourceLocation StartLoc, 20262 SourceLocation LParenLoc, 20263 SourceLocation EndLoc) { 20264 Expr *ValExpr = Grainsize; 20265 Stmt *HelperValStmt = nullptr; 20266 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20267 20268 // OpenMP [2.9.2, taskloop Constrcut] 20269 // The parameter of the grainsize clause must be a positive integer 20270 // expression. 20271 if (!isNonNegativeIntegerValue( 20272 ValExpr, *this, OMPC_grainsize, 20273 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20274 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20275 return nullptr; 20276 20277 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 20278 StartLoc, LParenLoc, EndLoc); 20279 } 20280 20281 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 20282 SourceLocation StartLoc, 20283 SourceLocation LParenLoc, 20284 SourceLocation EndLoc) { 20285 Expr *ValExpr = NumTasks; 20286 Stmt *HelperValStmt = nullptr; 20287 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 20288 20289 // OpenMP [2.9.2, taskloop Constrcut] 20290 // The parameter of the num_tasks clause must be a positive integer 20291 // expression. 20292 if (!isNonNegativeIntegerValue( 20293 ValExpr, *this, OMPC_num_tasks, 20294 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 20295 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 20296 return nullptr; 20297 20298 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 20299 StartLoc, LParenLoc, EndLoc); 20300 } 20301 20302 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 20303 SourceLocation LParenLoc, 20304 SourceLocation EndLoc) { 20305 // OpenMP [2.13.2, critical construct, Description] 20306 // ... where hint-expression is an integer constant expression that evaluates 20307 // to a valid lock hint. 20308 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 20309 if (HintExpr.isInvalid()) 20310 return nullptr; 20311 return new (Context) 20312 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 20313 } 20314 20315 /// Tries to find omp_event_handle_t type. 20316 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 20317 DSAStackTy *Stack) { 20318 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 20319 if (!OMPEventHandleT.isNull()) 20320 return true; 20321 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 20322 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 20323 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 20324 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 20325 return false; 20326 } 20327 Stack->setOMPEventHandleT(PT.get()); 20328 return true; 20329 } 20330 20331 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 20332 SourceLocation LParenLoc, 20333 SourceLocation EndLoc) { 20334 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 20335 !Evt->isInstantiationDependent() && 20336 !Evt->containsUnexpandedParameterPack()) { 20337 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 20338 return nullptr; 20339 // OpenMP 5.0, 2.10.1 task Construct. 20340 // event-handle is a variable of the omp_event_handle_t type. 20341 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 20342 if (!Ref) { 20343 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20344 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20345 return nullptr; 20346 } 20347 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 20348 if (!VD) { 20349 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20350 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 20351 return nullptr; 20352 } 20353 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 20354 VD->getType()) || 20355 VD->getType().isConstant(Context)) { 20356 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 20357 << "omp_event_handle_t" << 1 << VD->getType() 20358 << Evt->getSourceRange(); 20359 return nullptr; 20360 } 20361 // OpenMP 5.0, 2.10.1 task Construct 20362 // [detach clause]... The event-handle will be considered as if it was 20363 // specified on a firstprivate clause. 20364 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 20365 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 20366 DVar.RefExpr) { 20367 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 20368 << getOpenMPClauseName(DVar.CKind) 20369 << getOpenMPClauseName(OMPC_firstprivate); 20370 reportOriginalDsa(*this, DSAStack, VD, DVar); 20371 return nullptr; 20372 } 20373 } 20374 20375 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 20376 } 20377 20378 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 20379 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 20380 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 20381 SourceLocation EndLoc) { 20382 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 20383 std::string Values; 20384 Values += "'"; 20385 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 20386 Values += "'"; 20387 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20388 << Values << getOpenMPClauseName(OMPC_dist_schedule); 20389 return nullptr; 20390 } 20391 Expr *ValExpr = ChunkSize; 20392 Stmt *HelperValStmt = nullptr; 20393 if (ChunkSize) { 20394 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 20395 !ChunkSize->isInstantiationDependent() && 20396 !ChunkSize->containsUnexpandedParameterPack()) { 20397 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 20398 ExprResult Val = 20399 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 20400 if (Val.isInvalid()) 20401 return nullptr; 20402 20403 ValExpr = Val.get(); 20404 20405 // OpenMP [2.7.1, Restrictions] 20406 // chunk_size must be a loop invariant integer expression with a positive 20407 // value. 20408 if (Optional<llvm::APSInt> Result = 20409 ValExpr->getIntegerConstantExpr(Context)) { 20410 if (Result->isSigned() && !Result->isStrictlyPositive()) { 20411 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 20412 << "dist_schedule" << ChunkSize->getSourceRange(); 20413 return nullptr; 20414 } 20415 } else if (getOpenMPCaptureRegionForClause( 20416 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 20417 LangOpts.OpenMP) != OMPD_unknown && 20418 !CurContext->isDependentContext()) { 20419 ValExpr = MakeFullExpr(ValExpr).get(); 20420 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 20421 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 20422 HelperValStmt = buildPreInits(Context, Captures); 20423 } 20424 } 20425 } 20426 20427 return new (Context) 20428 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 20429 Kind, ValExpr, HelperValStmt); 20430 } 20431 20432 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 20433 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 20434 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 20435 SourceLocation KindLoc, SourceLocation EndLoc) { 20436 if (getLangOpts().OpenMP < 50) { 20437 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 20438 Kind != OMPC_DEFAULTMAP_scalar) { 20439 std::string Value; 20440 SourceLocation Loc; 20441 Value += "'"; 20442 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 20443 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20444 OMPC_DEFAULTMAP_MODIFIER_tofrom); 20445 Loc = MLoc; 20446 } else { 20447 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 20448 OMPC_DEFAULTMAP_scalar); 20449 Loc = KindLoc; 20450 } 20451 Value += "'"; 20452 Diag(Loc, diag::err_omp_unexpected_clause_value) 20453 << Value << getOpenMPClauseName(OMPC_defaultmap); 20454 return nullptr; 20455 } 20456 } else { 20457 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 20458 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 20459 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 20460 if (!isDefaultmapKind || !isDefaultmapModifier) { 20461 StringRef KindValue = "'scalar', 'aggregate', 'pointer'"; 20462 if (LangOpts.OpenMP == 50) { 20463 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 20464 "'firstprivate', 'none', 'default'"; 20465 if (!isDefaultmapKind && isDefaultmapModifier) { 20466 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20467 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20468 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20469 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20470 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20471 } else { 20472 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20473 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20474 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20475 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20476 } 20477 } else { 20478 StringRef ModifierValue = 20479 "'alloc', 'from', 'to', 'tofrom', " 20480 "'firstprivate', 'none', 'default', 'present'"; 20481 if (!isDefaultmapKind && isDefaultmapModifier) { 20482 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20483 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20484 } else if (isDefaultmapKind && !isDefaultmapModifier) { 20485 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20486 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20487 } else { 20488 Diag(MLoc, diag::err_omp_unexpected_clause_value) 20489 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 20490 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 20491 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 20492 } 20493 } 20494 return nullptr; 20495 } 20496 20497 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 20498 // At most one defaultmap clause for each category can appear on the 20499 // directive. 20500 if (DSAStack->checkDefaultmapCategory(Kind)) { 20501 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 20502 return nullptr; 20503 } 20504 } 20505 if (Kind == OMPC_DEFAULTMAP_unknown) { 20506 // Variable category is not specified - mark all categories. 20507 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 20508 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 20509 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 20510 } else { 20511 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 20512 } 20513 20514 return new (Context) 20515 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 20516 } 20517 20518 bool Sema::ActOnStartOpenMPDeclareTargetContext( 20519 DeclareTargetContextInfo &DTCI) { 20520 DeclContext *CurLexicalContext = getCurLexicalContext(); 20521 if (!CurLexicalContext->isFileContext() && 20522 !CurLexicalContext->isExternCContext() && 20523 !CurLexicalContext->isExternCXXContext() && 20524 !isa<CXXRecordDecl>(CurLexicalContext) && 20525 !isa<ClassTemplateDecl>(CurLexicalContext) && 20526 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 20527 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 20528 Diag(DTCI.Loc, diag::err_omp_region_not_file_context); 20529 return false; 20530 } 20531 DeclareTargetNesting.push_back(DTCI); 20532 return true; 20533 } 20534 20535 const Sema::DeclareTargetContextInfo 20536 Sema::ActOnOpenMPEndDeclareTargetDirective() { 20537 assert(!DeclareTargetNesting.empty() && 20538 "check isInOpenMPDeclareTargetContext() first!"); 20539 return DeclareTargetNesting.pop_back_val(); 20540 } 20541 20542 void Sema::ActOnFinishedOpenMPDeclareTargetContext( 20543 DeclareTargetContextInfo &DTCI) { 20544 for (auto &It : DTCI.ExplicitlyMapped) 20545 ActOnOpenMPDeclareTargetName(It.first, It.second.Loc, It.second.MT, 20546 DTCI.DT); 20547 } 20548 20549 NamedDecl *Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, 20550 CXXScopeSpec &ScopeSpec, 20551 const DeclarationNameInfo &Id) { 20552 LookupResult Lookup(*this, Id, LookupOrdinaryName); 20553 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 20554 20555 if (Lookup.isAmbiguous()) 20556 return nullptr; 20557 Lookup.suppressDiagnostics(); 20558 20559 if (!Lookup.isSingleResult()) { 20560 VarOrFuncDeclFilterCCC CCC(*this); 20561 if (TypoCorrection Corrected = 20562 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 20563 CTK_ErrorRecovery)) { 20564 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 20565 << Id.getName()); 20566 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 20567 return nullptr; 20568 } 20569 20570 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 20571 return nullptr; 20572 } 20573 20574 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 20575 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 20576 !isa<FunctionTemplateDecl>(ND)) { 20577 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 20578 return nullptr; 20579 } 20580 return ND; 20581 } 20582 20583 void Sema::ActOnOpenMPDeclareTargetName( 20584 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 20585 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 20586 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 20587 isa<FunctionTemplateDecl>(ND)) && 20588 "Expected variable, function or function template."); 20589 20590 // Diagnose marking after use as it may lead to incorrect diagnosis and 20591 // codegen. 20592 if (LangOpts.OpenMP >= 50 && 20593 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 20594 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 20595 20596 // Explicit declare target lists have precedence. 20597 const unsigned Level = -1; 20598 20599 auto *VD = cast<ValueDecl>(ND); 20600 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20601 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20602 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getDevType() != DT && 20603 ActiveAttr.getValue()->getLevel() == Level) { 20604 Diag(Loc, diag::err_omp_device_type_mismatch) 20605 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 20606 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr( 20607 ActiveAttr.getValue()->getDevType()); 20608 return; 20609 } 20610 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getMapType() != MT && 20611 ActiveAttr.getValue()->getLevel() == Level) { 20612 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 20613 return; 20614 } 20615 20616 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() == Level) 20617 return; 20618 20619 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, Level, 20620 SourceRange(Loc, Loc)); 20621 ND->addAttr(A); 20622 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20623 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 20624 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 20625 } 20626 20627 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 20628 Sema &SemaRef, Decl *D) { 20629 if (!D || !isa<VarDecl>(D)) 20630 return; 20631 auto *VD = cast<VarDecl>(D); 20632 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 20633 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 20634 if (SemaRef.LangOpts.OpenMP >= 50 && 20635 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 20636 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 20637 VD->hasGlobalStorage()) { 20638 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 20639 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 20640 // If a lambda declaration and definition appears between a 20641 // declare target directive and the matching end declare target 20642 // directive, all variables that are captured by the lambda 20643 // expression must also appear in a to clause. 20644 SemaRef.Diag(VD->getLocation(), 20645 diag::err_omp_lambda_capture_in_declare_target_not_to); 20646 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 20647 << VD << 0 << SR; 20648 return; 20649 } 20650 } 20651 if (MapTy.hasValue()) 20652 return; 20653 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 20654 SemaRef.Diag(SL, diag::note_used_here) << SR; 20655 } 20656 20657 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 20658 Sema &SemaRef, DSAStackTy *Stack, 20659 ValueDecl *VD) { 20660 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 20661 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 20662 /*FullCheck=*/false); 20663 } 20664 20665 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 20666 SourceLocation IdLoc) { 20667 if (!D || D->isInvalidDecl()) 20668 return; 20669 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 20670 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 20671 if (auto *VD = dyn_cast<VarDecl>(D)) { 20672 // Only global variables can be marked as declare target. 20673 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 20674 !VD->isStaticDataMember()) 20675 return; 20676 // 2.10.6: threadprivate variable cannot appear in a declare target 20677 // directive. 20678 if (DSAStack->isThreadPrivate(VD)) { 20679 Diag(SL, diag::err_omp_threadprivate_in_target); 20680 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 20681 return; 20682 } 20683 } 20684 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 20685 D = FTD->getTemplatedDecl(); 20686 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 20687 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 20688 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 20689 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 20690 Diag(IdLoc, diag::err_omp_function_in_link_clause); 20691 Diag(FD->getLocation(), diag::note_defined_here) << FD; 20692 return; 20693 } 20694 } 20695 if (auto *VD = dyn_cast<ValueDecl>(D)) { 20696 // Problem if any with var declared with incomplete type will be reported 20697 // as normal, so no need to check it here. 20698 if ((E || !VD->getType()->isIncompleteType()) && 20699 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 20700 return; 20701 if (!E && isInOpenMPDeclareTargetContext()) { 20702 // Checking declaration inside declare target region. 20703 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 20704 isa<FunctionTemplateDecl>(D)) { 20705 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 20706 OMPDeclareTargetDeclAttr::getActiveAttr(VD); 20707 unsigned Level = DeclareTargetNesting.size(); 20708 if (ActiveAttr.hasValue() && ActiveAttr.getValue()->getLevel() >= Level) 20709 return; 20710 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back(); 20711 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 20712 Context, OMPDeclareTargetDeclAttr::MT_To, DTCI.DT, Level, 20713 SourceRange(DTCI.Loc, DTCI.Loc)); 20714 D->addAttr(A); 20715 if (ASTMutationListener *ML = Context.getASTMutationListener()) 20716 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 20717 } 20718 return; 20719 } 20720 } 20721 if (!E) 20722 return; 20723 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 20724 } 20725 20726 OMPClause *Sema::ActOnOpenMPToClause( 20727 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20728 ArrayRef<SourceLocation> MotionModifiersLoc, 20729 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20730 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20731 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20732 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20733 OMPC_MOTION_MODIFIER_unknown}; 20734 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20735 20736 // Process motion-modifiers, flag errors for duplicate modifiers. 20737 unsigned Count = 0; 20738 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20739 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20740 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 20741 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20742 continue; 20743 } 20744 assert(Count < NumberOfOMPMotionModifiers && 20745 "Modifiers exceed the allowed number of motion modifiers"); 20746 Modifiers[Count] = MotionModifiers[I]; 20747 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20748 ++Count; 20749 } 20750 20751 MappableVarListInfo MVLI(VarList); 20752 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 20753 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20754 if (MVLI.ProcessedVarList.empty()) 20755 return nullptr; 20756 20757 return OMPToClause::Create( 20758 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20759 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20760 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20761 } 20762 20763 OMPClause *Sema::ActOnOpenMPFromClause( 20764 ArrayRef<OpenMPMotionModifierKind> MotionModifiers, 20765 ArrayRef<SourceLocation> MotionModifiersLoc, 20766 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 20767 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 20768 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 20769 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown, 20770 OMPC_MOTION_MODIFIER_unknown}; 20771 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers]; 20772 20773 // Process motion-modifiers, flag errors for duplicate modifiers. 20774 unsigned Count = 0; 20775 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) { 20776 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown && 20777 llvm::find(Modifiers, MotionModifiers[I]) != std::end(Modifiers)) { 20778 Diag(MotionModifiersLoc[I], diag::err_omp_duplicate_motion_modifier); 20779 continue; 20780 } 20781 assert(Count < NumberOfOMPMotionModifiers && 20782 "Modifiers exceed the allowed number of motion modifiers"); 20783 Modifiers[Count] = MotionModifiers[I]; 20784 ModifiersLoc[Count] = MotionModifiersLoc[I]; 20785 ++Count; 20786 } 20787 20788 MappableVarListInfo MVLI(VarList); 20789 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 20790 MapperIdScopeSpec, MapperId, UnresolvedMappers); 20791 if (MVLI.ProcessedVarList.empty()) 20792 return nullptr; 20793 20794 return OMPFromClause::Create( 20795 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 20796 MVLI.VarComponents, MVLI.UDMapperList, Modifiers, ModifiersLoc, 20797 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 20798 } 20799 20800 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 20801 const OMPVarListLocTy &Locs) { 20802 MappableVarListInfo MVLI(VarList); 20803 SmallVector<Expr *, 8> PrivateCopies; 20804 SmallVector<Expr *, 8> Inits; 20805 20806 for (Expr *RefExpr : VarList) { 20807 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 20808 SourceLocation ELoc; 20809 SourceRange ERange; 20810 Expr *SimpleRefExpr = RefExpr; 20811 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20812 if (Res.second) { 20813 // It will be analyzed later. 20814 MVLI.ProcessedVarList.push_back(RefExpr); 20815 PrivateCopies.push_back(nullptr); 20816 Inits.push_back(nullptr); 20817 } 20818 ValueDecl *D = Res.first; 20819 if (!D) 20820 continue; 20821 20822 QualType Type = D->getType(); 20823 Type = Type.getNonReferenceType().getUnqualifiedType(); 20824 20825 auto *VD = dyn_cast<VarDecl>(D); 20826 20827 // Item should be a pointer or reference to pointer. 20828 if (!Type->isPointerType()) { 20829 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 20830 << 0 << RefExpr->getSourceRange(); 20831 continue; 20832 } 20833 20834 // Build the private variable and the expression that refers to it. 20835 auto VDPrivate = 20836 buildVarDecl(*this, ELoc, Type, D->getName(), 20837 D->hasAttrs() ? &D->getAttrs() : nullptr, 20838 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 20839 if (VDPrivate->isInvalidDecl()) 20840 continue; 20841 20842 CurContext->addDecl(VDPrivate); 20843 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 20844 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 20845 20846 // Add temporary variable to initialize the private copy of the pointer. 20847 VarDecl *VDInit = 20848 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 20849 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 20850 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 20851 AddInitializerToDecl(VDPrivate, 20852 DefaultLvalueConversion(VDInitRefExpr).get(), 20853 /*DirectInit=*/false); 20854 20855 // If required, build a capture to implement the privatization initialized 20856 // with the current list item value. 20857 DeclRefExpr *Ref = nullptr; 20858 if (!VD) 20859 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20860 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20861 PrivateCopies.push_back(VDPrivateRefExpr); 20862 Inits.push_back(VDInitRefExpr); 20863 20864 // We need to add a data sharing attribute for this variable to make sure it 20865 // is correctly captured. A variable that shows up in a use_device_ptr has 20866 // similar properties of a first private variable. 20867 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20868 20869 // Create a mappable component for the list item. List items in this clause 20870 // only need a component. 20871 MVLI.VarBaseDeclarations.push_back(D); 20872 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 20873 MVLI.VarComponents.back().emplace_back(SimpleRefExpr, D, 20874 /*IsNonContiguous=*/false); 20875 } 20876 20877 if (MVLI.ProcessedVarList.empty()) 20878 return nullptr; 20879 20880 return OMPUseDevicePtrClause::Create( 20881 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 20882 MVLI.VarBaseDeclarations, MVLI.VarComponents); 20883 } 20884 20885 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 20886 const OMPVarListLocTy &Locs) { 20887 MappableVarListInfo MVLI(VarList); 20888 20889 for (Expr *RefExpr : VarList) { 20890 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 20891 SourceLocation ELoc; 20892 SourceRange ERange; 20893 Expr *SimpleRefExpr = RefExpr; 20894 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 20895 /*AllowArraySection=*/true); 20896 if (Res.second) { 20897 // It will be analyzed later. 20898 MVLI.ProcessedVarList.push_back(RefExpr); 20899 } 20900 ValueDecl *D = Res.first; 20901 if (!D) 20902 continue; 20903 auto *VD = dyn_cast<VarDecl>(D); 20904 20905 // If required, build a capture to implement the privatization initialized 20906 // with the current list item value. 20907 DeclRefExpr *Ref = nullptr; 20908 if (!VD) 20909 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 20910 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 20911 20912 // We need to add a data sharing attribute for this variable to make sure it 20913 // is correctly captured. A variable that shows up in a use_device_addr has 20914 // similar properties of a first private variable. 20915 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 20916 20917 // Create a mappable component for the list item. List items in this clause 20918 // only need a component. 20919 MVLI.VarBaseDeclarations.push_back(D); 20920 MVLI.VarComponents.emplace_back(); 20921 Expr *Component = SimpleRefExpr; 20922 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 20923 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 20924 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 20925 MVLI.VarComponents.back().emplace_back(Component, D, 20926 /*IsNonContiguous=*/false); 20927 } 20928 20929 if (MVLI.ProcessedVarList.empty()) 20930 return nullptr; 20931 20932 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 20933 MVLI.VarBaseDeclarations, 20934 MVLI.VarComponents); 20935 } 20936 20937 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 20938 const OMPVarListLocTy &Locs) { 20939 MappableVarListInfo MVLI(VarList); 20940 for (Expr *RefExpr : VarList) { 20941 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 20942 SourceLocation ELoc; 20943 SourceRange ERange; 20944 Expr *SimpleRefExpr = RefExpr; 20945 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 20946 if (Res.second) { 20947 // It will be analyzed later. 20948 MVLI.ProcessedVarList.push_back(RefExpr); 20949 } 20950 ValueDecl *D = Res.first; 20951 if (!D) 20952 continue; 20953 20954 QualType Type = D->getType(); 20955 // item should be a pointer or array or reference to pointer or array 20956 if (!Type.getNonReferenceType()->isPointerType() && 20957 !Type.getNonReferenceType()->isArrayType()) { 20958 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 20959 << 0 << RefExpr->getSourceRange(); 20960 continue; 20961 } 20962 20963 // Check if the declaration in the clause does not show up in any data 20964 // sharing attribute. 20965 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 20966 if (isOpenMPPrivate(DVar.CKind)) { 20967 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 20968 << getOpenMPClauseName(DVar.CKind) 20969 << getOpenMPClauseName(OMPC_is_device_ptr) 20970 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 20971 reportOriginalDsa(*this, DSAStack, D, DVar); 20972 continue; 20973 } 20974 20975 const Expr *ConflictExpr; 20976 if (DSAStack->checkMappableExprComponentListsForDecl( 20977 D, /*CurrentRegionOnly=*/true, 20978 [&ConflictExpr]( 20979 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 20980 OpenMPClauseKind) -> bool { 20981 ConflictExpr = R.front().getAssociatedExpression(); 20982 return true; 20983 })) { 20984 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 20985 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 20986 << ConflictExpr->getSourceRange(); 20987 continue; 20988 } 20989 20990 // Store the components in the stack so that they can be used to check 20991 // against other clauses later on. 20992 OMPClauseMappableExprCommon::MappableComponent MC( 20993 SimpleRefExpr, D, /*IsNonContiguous=*/false); 20994 DSAStack->addMappableExpressionComponents( 20995 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 20996 20997 // Record the expression we've just processed. 20998 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 20999 21000 // Create a mappable component for the list item. List items in this clause 21001 // only need a component. We use a null declaration to signal fields in 21002 // 'this'. 21003 assert((isa<DeclRefExpr>(SimpleRefExpr) || 21004 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 21005 "Unexpected device pointer expression!"); 21006 MVLI.VarBaseDeclarations.push_back( 21007 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 21008 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 21009 MVLI.VarComponents.back().push_back(MC); 21010 } 21011 21012 if (MVLI.ProcessedVarList.empty()) 21013 return nullptr; 21014 21015 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 21016 MVLI.VarBaseDeclarations, 21017 MVLI.VarComponents); 21018 } 21019 21020 OMPClause *Sema::ActOnOpenMPAllocateClause( 21021 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 21022 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 21023 if (Allocator) { 21024 // OpenMP [2.11.4 allocate Clause, Description] 21025 // allocator is an expression of omp_allocator_handle_t type. 21026 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 21027 return nullptr; 21028 21029 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 21030 if (AllocatorRes.isInvalid()) 21031 return nullptr; 21032 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 21033 DSAStack->getOMPAllocatorHandleT(), 21034 Sema::AA_Initializing, 21035 /*AllowExplicit=*/true); 21036 if (AllocatorRes.isInvalid()) 21037 return nullptr; 21038 Allocator = AllocatorRes.get(); 21039 } else { 21040 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 21041 // allocate clauses that appear on a target construct or on constructs in a 21042 // target region must specify an allocator expression unless a requires 21043 // directive with the dynamic_allocators clause is present in the same 21044 // compilation unit. 21045 if (LangOpts.OpenMPIsDevice && 21046 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 21047 targetDiag(StartLoc, diag::err_expected_allocator_expression); 21048 } 21049 // Analyze and build list of variables. 21050 SmallVector<Expr *, 8> Vars; 21051 for (Expr *RefExpr : VarList) { 21052 assert(RefExpr && "NULL expr in OpenMP private clause."); 21053 SourceLocation ELoc; 21054 SourceRange ERange; 21055 Expr *SimpleRefExpr = RefExpr; 21056 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21057 if (Res.second) { 21058 // It will be analyzed later. 21059 Vars.push_back(RefExpr); 21060 } 21061 ValueDecl *D = Res.first; 21062 if (!D) 21063 continue; 21064 21065 auto *VD = dyn_cast<VarDecl>(D); 21066 DeclRefExpr *Ref = nullptr; 21067 if (!VD && !CurContext->isDependentContext()) 21068 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 21069 Vars.push_back((VD || CurContext->isDependentContext()) 21070 ? RefExpr->IgnoreParens() 21071 : Ref); 21072 } 21073 21074 if (Vars.empty()) 21075 return nullptr; 21076 21077 if (Allocator) 21078 DSAStack->addInnerAllocatorExpr(Allocator); 21079 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 21080 ColonLoc, EndLoc, Vars); 21081 } 21082 21083 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 21084 SourceLocation StartLoc, 21085 SourceLocation LParenLoc, 21086 SourceLocation EndLoc) { 21087 SmallVector<Expr *, 8> Vars; 21088 for (Expr *RefExpr : VarList) { 21089 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21090 SourceLocation ELoc; 21091 SourceRange ERange; 21092 Expr *SimpleRefExpr = RefExpr; 21093 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 21094 if (Res.second) 21095 // It will be analyzed later. 21096 Vars.push_back(RefExpr); 21097 ValueDecl *D = Res.first; 21098 if (!D) 21099 continue; 21100 21101 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 21102 // A list-item cannot appear in more than one nontemporal clause. 21103 if (const Expr *PrevRef = 21104 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 21105 Diag(ELoc, diag::err_omp_used_in_clause_twice) 21106 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 21107 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 21108 << getOpenMPClauseName(OMPC_nontemporal); 21109 continue; 21110 } 21111 21112 Vars.push_back(RefExpr); 21113 } 21114 21115 if (Vars.empty()) 21116 return nullptr; 21117 21118 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21119 Vars); 21120 } 21121 21122 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 21123 SourceLocation StartLoc, 21124 SourceLocation LParenLoc, 21125 SourceLocation EndLoc) { 21126 SmallVector<Expr *, 8> Vars; 21127 for (Expr *RefExpr : VarList) { 21128 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21129 SourceLocation ELoc; 21130 SourceRange ERange; 21131 Expr *SimpleRefExpr = RefExpr; 21132 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21133 /*AllowArraySection=*/true); 21134 if (Res.second) 21135 // It will be analyzed later. 21136 Vars.push_back(RefExpr); 21137 ValueDecl *D = Res.first; 21138 if (!D) 21139 continue; 21140 21141 const DSAStackTy::DSAVarData DVar = 21142 DSAStack->getTopDSA(D, /*FromParent=*/true); 21143 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21144 // A list item that appears in the inclusive or exclusive clause must appear 21145 // in a reduction clause with the inscan modifier on the enclosing 21146 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21147 if (DVar.CKind != OMPC_reduction || 21148 DVar.Modifier != OMPC_REDUCTION_inscan) 21149 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21150 << RefExpr->getSourceRange(); 21151 21152 if (DSAStack->getParentDirective() != OMPD_unknown) 21153 DSAStack->markDeclAsUsedInScanDirective(D); 21154 Vars.push_back(RefExpr); 21155 } 21156 21157 if (Vars.empty()) 21158 return nullptr; 21159 21160 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21161 } 21162 21163 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 21164 SourceLocation StartLoc, 21165 SourceLocation LParenLoc, 21166 SourceLocation EndLoc) { 21167 SmallVector<Expr *, 8> Vars; 21168 for (Expr *RefExpr : VarList) { 21169 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 21170 SourceLocation ELoc; 21171 SourceRange ERange; 21172 Expr *SimpleRefExpr = RefExpr; 21173 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 21174 /*AllowArraySection=*/true); 21175 if (Res.second) 21176 // It will be analyzed later. 21177 Vars.push_back(RefExpr); 21178 ValueDecl *D = Res.first; 21179 if (!D) 21180 continue; 21181 21182 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 21183 DSAStackTy::DSAVarData DVar; 21184 if (ParentDirective != OMPD_unknown) 21185 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 21186 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 21187 // A list item that appears in the inclusive or exclusive clause must appear 21188 // in a reduction clause with the inscan modifier on the enclosing 21189 // worksharing-loop, worksharing-loop SIMD, or simd construct. 21190 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 21191 DVar.Modifier != OMPC_REDUCTION_inscan) { 21192 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 21193 << RefExpr->getSourceRange(); 21194 } else { 21195 DSAStack->markDeclAsUsedInScanDirective(D); 21196 } 21197 Vars.push_back(RefExpr); 21198 } 21199 21200 if (Vars.empty()) 21201 return nullptr; 21202 21203 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 21204 } 21205 21206 /// Tries to find omp_alloctrait_t type. 21207 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 21208 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 21209 if (!OMPAlloctraitT.isNull()) 21210 return true; 21211 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 21212 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 21213 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 21214 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 21215 return false; 21216 } 21217 Stack->setOMPAlloctraitT(PT.get()); 21218 return true; 21219 } 21220 21221 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 21222 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 21223 ArrayRef<UsesAllocatorsData> Data) { 21224 // OpenMP [2.12.5, target Construct] 21225 // allocator is an identifier of omp_allocator_handle_t type. 21226 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 21227 return nullptr; 21228 // OpenMP [2.12.5, target Construct] 21229 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 21230 if (llvm::any_of( 21231 Data, 21232 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 21233 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 21234 return nullptr; 21235 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 21236 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 21237 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 21238 StringRef Allocator = 21239 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 21240 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 21241 PredefinedAllocators.insert(LookupSingleName( 21242 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 21243 } 21244 21245 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 21246 for (const UsesAllocatorsData &D : Data) { 21247 Expr *AllocatorExpr = nullptr; 21248 // Check allocator expression. 21249 if (D.Allocator->isTypeDependent()) { 21250 AllocatorExpr = D.Allocator; 21251 } else { 21252 // Traits were specified - need to assign new allocator to the specified 21253 // allocator, so it must be an lvalue. 21254 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 21255 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 21256 bool IsPredefinedAllocator = false; 21257 if (DRE) 21258 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 21259 if (!DRE || 21260 !(Context.hasSameUnqualifiedType( 21261 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 21262 Context.typesAreCompatible(AllocatorExpr->getType(), 21263 DSAStack->getOMPAllocatorHandleT(), 21264 /*CompareUnqualified=*/true)) || 21265 (!IsPredefinedAllocator && 21266 (AllocatorExpr->getType().isConstant(Context) || 21267 !AllocatorExpr->isLValue()))) { 21268 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 21269 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 21270 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 21271 continue; 21272 } 21273 // OpenMP [2.12.5, target Construct] 21274 // Predefined allocators appearing in a uses_allocators clause cannot have 21275 // traits specified. 21276 if (IsPredefinedAllocator && D.AllocatorTraits) { 21277 Diag(D.AllocatorTraits->getExprLoc(), 21278 diag::err_omp_predefined_allocator_with_traits) 21279 << D.AllocatorTraits->getSourceRange(); 21280 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 21281 << cast<NamedDecl>(DRE->getDecl())->getName() 21282 << D.Allocator->getSourceRange(); 21283 continue; 21284 } 21285 // OpenMP [2.12.5, target Construct] 21286 // Non-predefined allocators appearing in a uses_allocators clause must 21287 // have traits specified. 21288 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 21289 Diag(D.Allocator->getExprLoc(), 21290 diag::err_omp_nonpredefined_allocator_without_traits); 21291 continue; 21292 } 21293 // No allocator traits - just convert it to rvalue. 21294 if (!D.AllocatorTraits) 21295 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 21296 DSAStack->addUsesAllocatorsDecl( 21297 DRE->getDecl(), 21298 IsPredefinedAllocator 21299 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 21300 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 21301 } 21302 Expr *AllocatorTraitsExpr = nullptr; 21303 if (D.AllocatorTraits) { 21304 if (D.AllocatorTraits->isTypeDependent()) { 21305 AllocatorTraitsExpr = D.AllocatorTraits; 21306 } else { 21307 // OpenMP [2.12.5, target Construct] 21308 // Arrays that contain allocator traits that appear in a uses_allocators 21309 // clause must be constant arrays, have constant values and be defined 21310 // in the same scope as the construct in which the clause appears. 21311 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 21312 // Check that traits expr is a constant array. 21313 QualType TraitTy; 21314 if (const ArrayType *Ty = 21315 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 21316 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 21317 TraitTy = ConstArrayTy->getElementType(); 21318 if (TraitTy.isNull() || 21319 !(Context.hasSameUnqualifiedType(TraitTy, 21320 DSAStack->getOMPAlloctraitT()) || 21321 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 21322 /*CompareUnqualified=*/true))) { 21323 Diag(D.AllocatorTraits->getExprLoc(), 21324 diag::err_omp_expected_array_alloctraits) 21325 << AllocatorTraitsExpr->getType(); 21326 continue; 21327 } 21328 // Do not map by default allocator traits if it is a standalone 21329 // variable. 21330 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 21331 DSAStack->addUsesAllocatorsDecl( 21332 DRE->getDecl(), 21333 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 21334 } 21335 } 21336 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 21337 NewD.Allocator = AllocatorExpr; 21338 NewD.AllocatorTraits = AllocatorTraitsExpr; 21339 NewD.LParenLoc = D.LParenLoc; 21340 NewD.RParenLoc = D.RParenLoc; 21341 } 21342 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 21343 NewData); 21344 } 21345 21346 OMPClause *Sema::ActOnOpenMPAffinityClause( 21347 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 21348 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 21349 SmallVector<Expr *, 8> Vars; 21350 for (Expr *RefExpr : Locators) { 21351 assert(RefExpr && "NULL expr in OpenMP shared clause."); 21352 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 21353 // It will be analyzed later. 21354 Vars.push_back(RefExpr); 21355 continue; 21356 } 21357 21358 SourceLocation ELoc = RefExpr->getExprLoc(); 21359 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 21360 21361 if (!SimpleExpr->isLValue()) { 21362 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21363 << 1 << 0 << RefExpr->getSourceRange(); 21364 continue; 21365 } 21366 21367 ExprResult Res; 21368 { 21369 Sema::TentativeAnalysisScope Trap(*this); 21370 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 21371 } 21372 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 21373 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 21374 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 21375 << 1 << 0 << RefExpr->getSourceRange(); 21376 continue; 21377 } 21378 Vars.push_back(SimpleExpr); 21379 } 21380 21381 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 21382 EndLoc, Modifier, Vars); 21383 } 21384