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/StmtCXX.h" 22 #include "clang/AST/StmtOpenMP.h" 23 #include "clang/AST/StmtVisitor.h" 24 #include "clang/AST/TypeOrdering.h" 25 #include "clang/Basic/OpenMPKinds.h" 26 #include "clang/Sema/Initialization.h" 27 #include "clang/Sema/Lookup.h" 28 #include "clang/Sema/Scope.h" 29 #include "clang/Sema/ScopeInfo.h" 30 #include "clang/Sema/SemaInternal.h" 31 #include "llvm/ADT/PointerEmbeddedInt.h" 32 using namespace clang; 33 34 //===----------------------------------------------------------------------===// 35 // Stack of data-sharing attributes for variables 36 //===----------------------------------------------------------------------===// 37 38 static const Expr *checkMapClauseExpressionBase( 39 Sema &SemaRef, Expr *E, 40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 41 OpenMPClauseKind CKind, bool NoDiagnose); 42 43 namespace { 44 /// Default data sharing attributes, which can be applied to directive. 45 enum DefaultDataSharingAttributes { 46 DSA_unspecified = 0, /// Data sharing attribute not specified. 47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 49 }; 50 51 /// Attributes of the defaultmap clause. 52 enum DefaultMapAttributes { 53 DMA_unspecified, /// Default mapping is not specified. 54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'. 55 }; 56 57 /// Stack for tracking declarations used in OpenMP directives and 58 /// clauses and their data-sharing attributes. 59 class DSAStackTy { 60 public: 61 struct DSAVarData { 62 OpenMPDirectiveKind DKind = OMPD_unknown; 63 OpenMPClauseKind CKind = OMPC_unknown; 64 const Expr *RefExpr = nullptr; 65 DeclRefExpr *PrivateCopy = nullptr; 66 SourceLocation ImplicitDSALoc; 67 DSAVarData() = default; 68 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 69 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 70 SourceLocation ImplicitDSALoc) 71 : DKind(DKind), CKind(CKind), RefExpr(RefExpr), 72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {} 73 }; 74 using OperatorOffsetTy = 75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 76 using DoacrossDependMapTy = 77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 78 79 private: 80 struct DSAInfo { 81 OpenMPClauseKind Attributes = OMPC_unknown; 82 /// Pointer to a reference expression and a flag which shows that the 83 /// variable is marked as lastprivate(true) or not (false). 84 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 85 DeclRefExpr *PrivateCopy = nullptr; 86 }; 87 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 89 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 90 using LoopControlVariablesMapTy = 91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 92 /// Struct that associates a component with the clause kind where they are 93 /// found. 94 struct MappedExprComponentTy { 95 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 96 OpenMPClauseKind Kind = OMPC_unknown; 97 }; 98 using MappedExprComponentsTy = 99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 100 using CriticalsWithHintsTy = 101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 102 struct ReductionData { 103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 104 SourceRange ReductionRange; 105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 106 ReductionData() = default; 107 void set(BinaryOperatorKind BO, SourceRange RR) { 108 ReductionRange = RR; 109 ReductionOp = BO; 110 } 111 void set(const Expr *RefExpr, SourceRange RR) { 112 ReductionRange = RR; 113 ReductionOp = RefExpr; 114 } 115 }; 116 using DeclReductionMapTy = 117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 118 119 struct SharingMapTy { 120 DeclSAMapTy SharingMap; 121 DeclReductionMapTy ReductionMap; 122 AlignedMapTy AlignedMap; 123 MappedExprComponentsTy MappedExprComponents; 124 LoopControlVariablesMapTy LCVMap; 125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 126 SourceLocation DefaultAttrLoc; 127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified; 128 SourceLocation DefaultMapAttrLoc; 129 OpenMPDirectiveKind Directive = OMPD_unknown; 130 DeclarationNameInfo DirectiveName; 131 Scope *CurScope = nullptr; 132 SourceLocation ConstructLoc; 133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 134 /// get the data (loop counters etc.) about enclosing loop-based construct. 135 /// This data is required during codegen. 136 DoacrossDependMapTy DoacrossDepends; 137 /// First argument (Expr *) contains optional argument of the 138 /// 'ordered' clause, the second one is true if the regions has 'ordered' 139 /// clause, false otherwise. 140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 141 unsigned AssociatedLoops = 1; 142 const Decl *PossiblyLoopCounter = nullptr; 143 bool NowaitRegion = false; 144 bool CancelRegion = false; 145 bool LoopStart = false; 146 SourceLocation InnerTeamsRegionLoc; 147 /// Reference to the taskgroup task_reduction reference expression. 148 Expr *TaskgroupReductionRef = nullptr; 149 llvm::DenseSet<QualType> MappedClassesQualTypes; 150 /// List of globals marked as declare target link in this target region 151 /// (isOpenMPTargetExecutionDirective(Directive) == true). 152 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 153 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 154 Scope *CurScope, SourceLocation Loc) 155 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 156 ConstructLoc(Loc) {} 157 SharingMapTy() = default; 158 }; 159 160 using StackTy = SmallVector<SharingMapTy, 4>; 161 162 /// Stack of used declaration and their data-sharing attributes. 163 DeclSAMapTy Threadprivates; 164 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 165 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 166 /// true, if check for DSA must be from parent directive, false, if 167 /// from current directive. 168 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 169 Sema &SemaRef; 170 bool ForceCapturing = false; 171 /// true if all the vaiables in the target executable directives must be 172 /// captured by reference. 173 bool ForceCaptureByReferenceInTargetExecutable = false; 174 CriticalsWithHintsTy Criticals; 175 176 using iterator = StackTy::const_reverse_iterator; 177 178 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const; 179 180 /// Checks if the variable is a local for OpenMP region. 181 bool isOpenMPLocal(VarDecl *D, iterator Iter) const; 182 183 bool isStackEmpty() const { 184 return Stack.empty() || 185 Stack.back().second != CurrentNonCapturingFunctionScope || 186 Stack.back().first.empty(); 187 } 188 189 /// Vector of previously declared requires directives 190 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 191 /// omp_allocator_handle_t type. 192 QualType OMPAllocatorHandleT; 193 /// Expression for the predefined allocators. 194 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 195 nullptr}; 196 /// Vector of previously encountered target directives 197 SmallVector<SourceLocation, 2> TargetLocations; 198 199 public: 200 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 201 202 /// Sets omp_allocator_handle_t type. 203 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 204 /// Gets omp_allocator_handle_t type. 205 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 206 /// Sets the given default allocator. 207 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 208 Expr *Allocator) { 209 OMPPredefinedAllocators[AllocatorKind] = Allocator; 210 } 211 /// Returns the specified default allocator. 212 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 213 return OMPPredefinedAllocators[AllocatorKind]; 214 } 215 216 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 217 OpenMPClauseKind getClauseParsingMode() const { 218 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 219 return ClauseKindMode; 220 } 221 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 222 223 bool isForceVarCapturing() const { return ForceCapturing; } 224 void setForceVarCapturing(bool V) { ForceCapturing = V; } 225 226 void setForceCaptureByReferenceInTargetExecutable(bool V) { 227 ForceCaptureByReferenceInTargetExecutable = V; 228 } 229 bool isForceCaptureByReferenceInTargetExecutable() const { 230 return ForceCaptureByReferenceInTargetExecutable; 231 } 232 233 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 234 Scope *CurScope, SourceLocation Loc) { 235 if (Stack.empty() || 236 Stack.back().second != CurrentNonCapturingFunctionScope) 237 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 238 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 239 Stack.back().first.back().DefaultAttrLoc = Loc; 240 } 241 242 void pop() { 243 assert(!Stack.back().first.empty() && 244 "Data-sharing attributes stack is empty!"); 245 Stack.back().first.pop_back(); 246 } 247 248 /// Marks that we're started loop parsing. 249 void loopInit() { 250 assert(isOpenMPLoopDirective(getCurrentDirective()) && 251 "Expected loop-based directive."); 252 Stack.back().first.back().LoopStart = true; 253 } 254 /// Start capturing of the variables in the loop context. 255 void loopStart() { 256 assert(isOpenMPLoopDirective(getCurrentDirective()) && 257 "Expected loop-based directive."); 258 Stack.back().first.back().LoopStart = false; 259 } 260 /// true, if variables are captured, false otherwise. 261 bool isLoopStarted() const { 262 assert(isOpenMPLoopDirective(getCurrentDirective()) && 263 "Expected loop-based directive."); 264 return !Stack.back().first.back().LoopStart; 265 } 266 /// Marks (or clears) declaration as possibly loop counter. 267 void resetPossibleLoopCounter(const Decl *D = nullptr) { 268 Stack.back().first.back().PossiblyLoopCounter = 269 D ? D->getCanonicalDecl() : D; 270 } 271 /// Gets the possible loop counter decl. 272 const Decl *getPossiblyLoopCunter() const { 273 return Stack.back().first.back().PossiblyLoopCounter; 274 } 275 /// Start new OpenMP region stack in new non-capturing function. 276 void pushFunction() { 277 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 278 assert(!isa<CapturingScopeInfo>(CurFnScope)); 279 CurrentNonCapturingFunctionScope = CurFnScope; 280 } 281 /// Pop region stack for non-capturing function. 282 void popFunction(const FunctionScopeInfo *OldFSI) { 283 if (!Stack.empty() && Stack.back().second == OldFSI) { 284 assert(Stack.back().first.empty()); 285 Stack.pop_back(); 286 } 287 CurrentNonCapturingFunctionScope = nullptr; 288 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 289 if (!isa<CapturingScopeInfo>(FSI)) { 290 CurrentNonCapturingFunctionScope = FSI; 291 break; 292 } 293 } 294 } 295 296 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 297 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 298 } 299 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 300 getCriticalWithHint(const DeclarationNameInfo &Name) const { 301 auto I = Criticals.find(Name.getAsString()); 302 if (I != Criticals.end()) 303 return I->second; 304 return std::make_pair(nullptr, llvm::APSInt()); 305 } 306 /// If 'aligned' declaration for given variable \a D was not seen yet, 307 /// add it and return NULL; otherwise return previous occurrence's expression 308 /// for diagnostics. 309 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 310 311 /// Register specified variable as loop control variable. 312 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 313 /// Check if the specified variable is a loop control variable for 314 /// current region. 315 /// \return The index of the loop control variable in the list of associated 316 /// for-loops (from outer to inner). 317 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 318 /// Check if the specified variable is a loop control variable for 319 /// parent region. 320 /// \return The index of the loop control variable in the list of associated 321 /// for-loops (from outer to inner). 322 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 323 /// Get the loop control variable for the I-th loop (or nullptr) in 324 /// parent directive. 325 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 326 327 /// Adds explicit data sharing attribute to the specified declaration. 328 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 329 DeclRefExpr *PrivateCopy = nullptr); 330 331 /// Adds additional information for the reduction items with the reduction id 332 /// represented as an operator. 333 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 334 BinaryOperatorKind BOK); 335 /// Adds additional information for the reduction items with the reduction id 336 /// represented as reduction identifier. 337 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 338 const Expr *ReductionRef); 339 /// Returns the location and reduction operation from the innermost parent 340 /// region for the given \p D. 341 const DSAVarData 342 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 343 BinaryOperatorKind &BOK, 344 Expr *&TaskgroupDescriptor) const; 345 /// Returns the location and reduction operation from the innermost parent 346 /// region for the given \p D. 347 const DSAVarData 348 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 349 const Expr *&ReductionRef, 350 Expr *&TaskgroupDescriptor) const; 351 /// Return reduction reference expression for the current taskgroup. 352 Expr *getTaskgroupReductionRef() const { 353 assert(Stack.back().first.back().Directive == OMPD_taskgroup && 354 "taskgroup reference expression requested for non taskgroup " 355 "directive."); 356 return Stack.back().first.back().TaskgroupReductionRef; 357 } 358 /// Checks if the given \p VD declaration is actually a taskgroup reduction 359 /// descriptor variable at the \p Level of OpenMP regions. 360 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 361 return Stack.back().first[Level].TaskgroupReductionRef && 362 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef) 363 ->getDecl() == VD; 364 } 365 366 /// Returns data sharing attributes from top of the stack for the 367 /// specified declaration. 368 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 369 /// Returns data-sharing attributes for the specified declaration. 370 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 371 /// Checks if the specified variables has data-sharing attributes which 372 /// match specified \a CPred predicate in any directive which matches \a DPred 373 /// predicate. 374 const DSAVarData 375 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 376 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 377 bool FromParent) const; 378 /// Checks if the specified variables has data-sharing attributes which 379 /// match specified \a CPred predicate in any innermost directive which 380 /// matches \a DPred predicate. 381 const DSAVarData 382 hasInnermostDSA(ValueDecl *D, 383 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 384 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 385 bool FromParent) const; 386 /// Checks if the specified variables has explicit data-sharing 387 /// attributes which match specified \a CPred predicate at the specified 388 /// OpenMP region. 389 bool hasExplicitDSA(const ValueDecl *D, 390 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 391 unsigned Level, bool NotLastprivate = false) const; 392 393 /// Returns true if the directive at level \Level matches in the 394 /// specified \a DPred predicate. 395 bool hasExplicitDirective( 396 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 397 unsigned Level) const; 398 399 /// Finds a directive which matches specified \a DPred predicate. 400 bool hasDirective( 401 const llvm::function_ref<bool( 402 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 403 DPred, 404 bool FromParent) const; 405 406 /// Returns currently analyzed directive. 407 OpenMPDirectiveKind getCurrentDirective() const { 408 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; 409 } 410 /// Returns directive kind at specified level. 411 OpenMPDirectiveKind getDirective(unsigned Level) const { 412 assert(!isStackEmpty() && "No directive at specified level."); 413 return Stack.back().first[Level].Directive; 414 } 415 /// Returns parent directive. 416 OpenMPDirectiveKind getParentDirective() const { 417 if (isStackEmpty() || Stack.back().first.size() == 1) 418 return OMPD_unknown; 419 return std::next(Stack.back().first.rbegin())->Directive; 420 } 421 422 /// Add requires decl to internal vector 423 void addRequiresDecl(OMPRequiresDecl *RD) { 424 RequiresDecls.push_back(RD); 425 } 426 427 /// Checks if the defined 'requires' directive has specified type of clause. 428 template <typename ClauseType> 429 bool hasRequiresDeclWithClause() { 430 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 431 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 432 return isa<ClauseType>(C); 433 }); 434 }); 435 } 436 437 /// Checks for a duplicate clause amongst previously declared requires 438 /// directives 439 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 440 bool IsDuplicate = false; 441 for (OMPClause *CNew : ClauseList) { 442 for (const OMPRequiresDecl *D : RequiresDecls) { 443 for (const OMPClause *CPrev : D->clauselists()) { 444 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 445 SemaRef.Diag(CNew->getBeginLoc(), 446 diag::err_omp_requires_clause_redeclaration) 447 << getOpenMPClauseName(CNew->getClauseKind()); 448 SemaRef.Diag(CPrev->getBeginLoc(), 449 diag::note_omp_requires_previous_clause) 450 << getOpenMPClauseName(CPrev->getClauseKind()); 451 IsDuplicate = true; 452 } 453 } 454 } 455 } 456 return IsDuplicate; 457 } 458 459 /// Add location of previously encountered target to internal vector 460 void addTargetDirLocation(SourceLocation LocStart) { 461 TargetLocations.push_back(LocStart); 462 } 463 464 // Return previously encountered target region locations. 465 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 466 return TargetLocations; 467 } 468 469 /// Set default data sharing attribute to none. 470 void setDefaultDSANone(SourceLocation Loc) { 471 assert(!isStackEmpty()); 472 Stack.back().first.back().DefaultAttr = DSA_none; 473 Stack.back().first.back().DefaultAttrLoc = Loc; 474 } 475 /// Set default data sharing attribute to shared. 476 void setDefaultDSAShared(SourceLocation Loc) { 477 assert(!isStackEmpty()); 478 Stack.back().first.back().DefaultAttr = DSA_shared; 479 Stack.back().first.back().DefaultAttrLoc = Loc; 480 } 481 /// Set default data mapping attribute to 'tofrom:scalar'. 482 void setDefaultDMAToFromScalar(SourceLocation Loc) { 483 assert(!isStackEmpty()); 484 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar; 485 Stack.back().first.back().DefaultMapAttrLoc = Loc; 486 } 487 488 DefaultDataSharingAttributes getDefaultDSA() const { 489 return isStackEmpty() ? DSA_unspecified 490 : Stack.back().first.back().DefaultAttr; 491 } 492 SourceLocation getDefaultDSALocation() const { 493 return isStackEmpty() ? SourceLocation() 494 : Stack.back().first.back().DefaultAttrLoc; 495 } 496 DefaultMapAttributes getDefaultDMA() const { 497 return isStackEmpty() ? DMA_unspecified 498 : Stack.back().first.back().DefaultMapAttr; 499 } 500 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const { 501 return Stack.back().first[Level].DefaultMapAttr; 502 } 503 SourceLocation getDefaultDMALocation() const { 504 return isStackEmpty() ? SourceLocation() 505 : Stack.back().first.back().DefaultMapAttrLoc; 506 } 507 508 /// Checks if the specified variable is a threadprivate. 509 bool isThreadPrivate(VarDecl *D) { 510 const DSAVarData DVar = getTopDSA(D, false); 511 return isOpenMPThreadPrivate(DVar.CKind); 512 } 513 514 /// Marks current region as ordered (it has an 'ordered' clause). 515 void setOrderedRegion(bool IsOrdered, const Expr *Param, 516 OMPOrderedClause *Clause) { 517 assert(!isStackEmpty()); 518 if (IsOrdered) 519 Stack.back().first.back().OrderedRegion.emplace(Param, Clause); 520 else 521 Stack.back().first.back().OrderedRegion.reset(); 522 } 523 /// Returns true, if region is ordered (has associated 'ordered' clause), 524 /// false - otherwise. 525 bool isOrderedRegion() const { 526 if (isStackEmpty()) 527 return false; 528 return Stack.back().first.rbegin()->OrderedRegion.hasValue(); 529 } 530 /// Returns optional parameter for the ordered region. 531 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 532 if (isStackEmpty() || 533 !Stack.back().first.rbegin()->OrderedRegion.hasValue()) 534 return std::make_pair(nullptr, nullptr); 535 return Stack.back().first.rbegin()->OrderedRegion.getValue(); 536 } 537 /// Returns true, if parent region is ordered (has associated 538 /// 'ordered' clause), false - otherwise. 539 bool isParentOrderedRegion() const { 540 if (isStackEmpty() || Stack.back().first.size() == 1) 541 return false; 542 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue(); 543 } 544 /// Returns optional parameter for the ordered region. 545 std::pair<const Expr *, OMPOrderedClause *> 546 getParentOrderedRegionParam() const { 547 if (isStackEmpty() || Stack.back().first.size() == 1 || 548 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue()) 549 return std::make_pair(nullptr, nullptr); 550 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue(); 551 } 552 /// Marks current region as nowait (it has a 'nowait' clause). 553 void setNowaitRegion(bool IsNowait = true) { 554 assert(!isStackEmpty()); 555 Stack.back().first.back().NowaitRegion = IsNowait; 556 } 557 /// Returns true, if parent region is nowait (has associated 558 /// 'nowait' clause), false - otherwise. 559 bool isParentNowaitRegion() const { 560 if (isStackEmpty() || Stack.back().first.size() == 1) 561 return false; 562 return std::next(Stack.back().first.rbegin())->NowaitRegion; 563 } 564 /// Marks parent region as cancel region. 565 void setParentCancelRegion(bool Cancel = true) { 566 if (!isStackEmpty() && Stack.back().first.size() > 1) { 567 auto &StackElemRef = *std::next(Stack.back().first.rbegin()); 568 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; 569 } 570 } 571 /// Return true if current region has inner cancel construct. 572 bool isCancelRegion() const { 573 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; 574 } 575 576 /// Set collapse value for the region. 577 void setAssociatedLoops(unsigned Val) { 578 assert(!isStackEmpty()); 579 Stack.back().first.back().AssociatedLoops = Val; 580 } 581 /// Return collapse value for region. 582 unsigned getAssociatedLoops() const { 583 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; 584 } 585 586 /// Marks current target region as one with closely nested teams 587 /// region. 588 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 589 if (!isStackEmpty() && Stack.back().first.size() > 1) { 590 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = 591 TeamsRegionLoc; 592 } 593 } 594 /// Returns true, if current region has closely nested teams region. 595 bool hasInnerTeamsRegion() const { 596 return getInnerTeamsRegionLoc().isValid(); 597 } 598 /// Returns location of the nested teams region (if any). 599 SourceLocation getInnerTeamsRegionLoc() const { 600 return isStackEmpty() ? SourceLocation() 601 : Stack.back().first.back().InnerTeamsRegionLoc; 602 } 603 604 Scope *getCurScope() const { 605 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 606 } 607 SourceLocation getConstructLoc() const { 608 return isStackEmpty() ? SourceLocation() 609 : Stack.back().first.back().ConstructLoc; 610 } 611 612 /// Do the check specified in \a Check to all component lists and return true 613 /// if any issue is found. 614 bool checkMappableExprComponentListsForDecl( 615 const ValueDecl *VD, bool CurrentRegionOnly, 616 const llvm::function_ref< 617 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 618 OpenMPClauseKind)> 619 Check) const { 620 if (isStackEmpty()) 621 return false; 622 auto SI = Stack.back().first.rbegin(); 623 auto SE = Stack.back().first.rend(); 624 625 if (SI == SE) 626 return false; 627 628 if (CurrentRegionOnly) 629 SE = std::next(SI); 630 else 631 std::advance(SI, 1); 632 633 for (; SI != SE; ++SI) { 634 auto MI = SI->MappedExprComponents.find(VD); 635 if (MI != SI->MappedExprComponents.end()) 636 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 637 MI->second.Components) 638 if (Check(L, MI->second.Kind)) 639 return true; 640 } 641 return false; 642 } 643 644 /// Do the check specified in \a Check to all component lists at a given level 645 /// and return true if any issue is found. 646 bool checkMappableExprComponentListsForDeclAtLevel( 647 const ValueDecl *VD, unsigned Level, 648 const llvm::function_ref< 649 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 650 OpenMPClauseKind)> 651 Check) const { 652 if (isStackEmpty()) 653 return false; 654 655 auto StartI = Stack.back().first.begin(); 656 auto EndI = Stack.back().first.end(); 657 if (std::distance(StartI, EndI) <= (int)Level) 658 return false; 659 std::advance(StartI, Level); 660 661 auto MI = StartI->MappedExprComponents.find(VD); 662 if (MI != StartI->MappedExprComponents.end()) 663 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 664 MI->second.Components) 665 if (Check(L, MI->second.Kind)) 666 return true; 667 return false; 668 } 669 670 /// Create a new mappable expression component list associated with a given 671 /// declaration and initialize it with the provided list of components. 672 void addMappableExpressionComponents( 673 const ValueDecl *VD, 674 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 675 OpenMPClauseKind WhereFoundClauseKind) { 676 assert(!isStackEmpty() && 677 "Not expecting to retrieve components from a empty stack!"); 678 MappedExprComponentTy &MEC = 679 Stack.back().first.back().MappedExprComponents[VD]; 680 // Create new entry and append the new components there. 681 MEC.Components.resize(MEC.Components.size() + 1); 682 MEC.Components.back().append(Components.begin(), Components.end()); 683 MEC.Kind = WhereFoundClauseKind; 684 } 685 686 unsigned getNestingLevel() const { 687 assert(!isStackEmpty()); 688 return Stack.back().first.size() - 1; 689 } 690 void addDoacrossDependClause(OMPDependClause *C, 691 const OperatorOffsetTy &OpsOffs) { 692 assert(!isStackEmpty() && Stack.back().first.size() > 1); 693 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 694 assert(isOpenMPWorksharingDirective(StackElem.Directive)); 695 StackElem.DoacrossDepends.try_emplace(C, OpsOffs); 696 } 697 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 698 getDoacrossDependClauses() const { 699 assert(!isStackEmpty()); 700 const SharingMapTy &StackElem = Stack.back().first.back(); 701 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 702 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 703 return llvm::make_range(Ref.begin(), Ref.end()); 704 } 705 return llvm::make_range(StackElem.DoacrossDepends.end(), 706 StackElem.DoacrossDepends.end()); 707 } 708 709 // Store types of classes which have been explicitly mapped 710 void addMappedClassesQualTypes(QualType QT) { 711 SharingMapTy &StackElem = Stack.back().first.back(); 712 StackElem.MappedClassesQualTypes.insert(QT); 713 } 714 715 // Return set of mapped classes types 716 bool isClassPreviouslyMapped(QualType QT) const { 717 const SharingMapTy &StackElem = Stack.back().first.back(); 718 return StackElem.MappedClassesQualTypes.count(QT) != 0; 719 } 720 721 /// Adds global declare target to the parent target region. 722 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 723 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 724 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 725 "Expected declare target link global."); 726 if (isStackEmpty()) 727 return; 728 auto It = Stack.back().first.rbegin(); 729 while (It != Stack.back().first.rend() && 730 !isOpenMPTargetExecutionDirective(It->Directive)) 731 ++It; 732 if (It != Stack.back().first.rend()) { 733 assert(isOpenMPTargetExecutionDirective(It->Directive) && 734 "Expected target executable directive."); 735 It->DeclareTargetLinkVarDecls.push_back(E); 736 } 737 } 738 739 /// Returns the list of globals with declare target link if current directive 740 /// is target. 741 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 742 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 743 "Expected target executable directive."); 744 return Stack.back().first.back().DeclareTargetLinkVarDecls; 745 } 746 }; 747 748 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 749 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 750 } 751 752 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 753 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown; 754 } 755 756 } // namespace 757 758 static const Expr *getExprAsWritten(const Expr *E) { 759 if (const auto *FE = dyn_cast<FullExpr>(E)) 760 E = FE->getSubExpr(); 761 762 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 763 E = MTE->GetTemporaryExpr(); 764 765 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 766 E = Binder->getSubExpr(); 767 768 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 769 E = ICE->getSubExprAsWritten(); 770 return E->IgnoreParens(); 771 } 772 773 static Expr *getExprAsWritten(Expr *E) { 774 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 775 } 776 777 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 778 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 779 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 780 D = ME->getMemberDecl(); 781 const auto *VD = dyn_cast<VarDecl>(D); 782 const auto *FD = dyn_cast<FieldDecl>(D); 783 if (VD != nullptr) { 784 VD = VD->getCanonicalDecl(); 785 D = VD; 786 } else { 787 assert(FD); 788 FD = FD->getCanonicalDecl(); 789 D = FD; 790 } 791 return D; 792 } 793 794 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 795 return const_cast<ValueDecl *>( 796 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 797 } 798 799 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter, 800 ValueDecl *D) const { 801 D = getCanonicalDecl(D); 802 auto *VD = dyn_cast<VarDecl>(D); 803 const auto *FD = dyn_cast<FieldDecl>(D); 804 DSAVarData DVar; 805 if (isStackEmpty() || Iter == Stack.back().first.rend()) { 806 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 807 // in a region but not in construct] 808 // File-scope or namespace-scope variables referenced in called routines 809 // in the region are shared unless they appear in a threadprivate 810 // directive. 811 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 812 DVar.CKind = OMPC_shared; 813 814 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 815 // in a region but not in construct] 816 // Variables with static storage duration that are declared in called 817 // routines in the region are shared. 818 if (VD && VD->hasGlobalStorage()) 819 DVar.CKind = OMPC_shared; 820 821 // Non-static data members are shared by default. 822 if (FD) 823 DVar.CKind = OMPC_shared; 824 825 return DVar; 826 } 827 828 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 829 // in a Construct, C/C++, predetermined, p.1] 830 // Variables with automatic storage duration that are declared in a scope 831 // inside the construct are private. 832 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 833 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 834 DVar.CKind = OMPC_private; 835 return DVar; 836 } 837 838 DVar.DKind = Iter->Directive; 839 // Explicitly specified attributes and local variables with predetermined 840 // attributes. 841 if (Iter->SharingMap.count(D)) { 842 const DSAInfo &Data = Iter->SharingMap.lookup(D); 843 DVar.RefExpr = Data.RefExpr.getPointer(); 844 DVar.PrivateCopy = Data.PrivateCopy; 845 DVar.CKind = Data.Attributes; 846 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 847 return DVar; 848 } 849 850 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 851 // in a Construct, C/C++, implicitly determined, p.1] 852 // In a parallel or task construct, the data-sharing attributes of these 853 // variables are determined by the default clause, if present. 854 switch (Iter->DefaultAttr) { 855 case DSA_shared: 856 DVar.CKind = OMPC_shared; 857 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 858 return DVar; 859 case DSA_none: 860 return DVar; 861 case DSA_unspecified: 862 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 863 // in a Construct, implicitly determined, p.2] 864 // In a parallel construct, if no default clause is present, these 865 // variables are shared. 866 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 867 if (isOpenMPParallelDirective(DVar.DKind) || 868 isOpenMPTeamsDirective(DVar.DKind)) { 869 DVar.CKind = OMPC_shared; 870 return DVar; 871 } 872 873 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 874 // in a Construct, implicitly determined, p.4] 875 // In a task construct, if no default clause is present, a variable that in 876 // the enclosing context is determined to be shared by all implicit tasks 877 // bound to the current team is shared. 878 if (isOpenMPTaskingDirective(DVar.DKind)) { 879 DSAVarData DVarTemp; 880 iterator I = Iter, E = Stack.back().first.rend(); 881 do { 882 ++I; 883 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 884 // Referenced in a Construct, implicitly determined, p.6] 885 // In a task construct, if no default clause is present, a variable 886 // whose data-sharing attribute is not determined by the rules above is 887 // firstprivate. 888 DVarTemp = getDSA(I, D); 889 if (DVarTemp.CKind != OMPC_shared) { 890 DVar.RefExpr = nullptr; 891 DVar.CKind = OMPC_firstprivate; 892 return DVar; 893 } 894 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 895 DVar.CKind = 896 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 897 return DVar; 898 } 899 } 900 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 901 // in a Construct, implicitly determined, p.3] 902 // For constructs other than task, if no default clause is present, these 903 // variables inherit their data-sharing attributes from the enclosing 904 // context. 905 return getDSA(++Iter, D); 906 } 907 908 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 909 const Expr *NewDE) { 910 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 911 D = getCanonicalDecl(D); 912 SharingMapTy &StackElem = Stack.back().first.back(); 913 auto It = StackElem.AlignedMap.find(D); 914 if (It == StackElem.AlignedMap.end()) { 915 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 916 StackElem.AlignedMap[D] = NewDE; 917 return nullptr; 918 } 919 assert(It->second && "Unexpected nullptr expr in the aligned map"); 920 return It->second; 921 } 922 923 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 924 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 925 D = getCanonicalDecl(D); 926 SharingMapTy &StackElem = Stack.back().first.back(); 927 StackElem.LCVMap.try_emplace( 928 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 929 } 930 931 const DSAStackTy::LCDeclInfo 932 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 933 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 934 D = getCanonicalDecl(D); 935 const SharingMapTy &StackElem = Stack.back().first.back(); 936 auto It = StackElem.LCVMap.find(D); 937 if (It != StackElem.LCVMap.end()) 938 return It->second; 939 return {0, nullptr}; 940 } 941 942 const DSAStackTy::LCDeclInfo 943 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 944 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 945 "Data-sharing attributes stack is empty"); 946 D = getCanonicalDecl(D); 947 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 948 auto It = StackElem.LCVMap.find(D); 949 if (It != StackElem.LCVMap.end()) 950 return It->second; 951 return {0, nullptr}; 952 } 953 954 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 955 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 956 "Data-sharing attributes stack is empty"); 957 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 958 if (StackElem.LCVMap.size() < I) 959 return nullptr; 960 for (const auto &Pair : StackElem.LCVMap) 961 if (Pair.second.first == I) 962 return Pair.first; 963 return nullptr; 964 } 965 966 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 967 DeclRefExpr *PrivateCopy) { 968 D = getCanonicalDecl(D); 969 if (A == OMPC_threadprivate) { 970 DSAInfo &Data = Threadprivates[D]; 971 Data.Attributes = A; 972 Data.RefExpr.setPointer(E); 973 Data.PrivateCopy = nullptr; 974 } else { 975 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 976 DSAInfo &Data = Stack.back().first.back().SharingMap[D]; 977 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 978 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 979 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 980 (isLoopControlVariable(D).first && A == OMPC_private)); 981 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 982 Data.RefExpr.setInt(/*IntVal=*/true); 983 return; 984 } 985 const bool IsLastprivate = 986 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 987 Data.Attributes = A; 988 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 989 Data.PrivateCopy = PrivateCopy; 990 if (PrivateCopy) { 991 DSAInfo &Data = 992 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; 993 Data.Attributes = A; 994 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 995 Data.PrivateCopy = nullptr; 996 } 997 } 998 } 999 1000 /// Build a variable declaration for OpenMP loop iteration variable. 1001 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1002 StringRef Name, const AttrVec *Attrs = nullptr, 1003 DeclRefExpr *OrigRef = nullptr) { 1004 DeclContext *DC = SemaRef.CurContext; 1005 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1006 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1007 auto *Decl = 1008 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1009 if (Attrs) { 1010 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1011 I != E; ++I) 1012 Decl->addAttr(*I); 1013 } 1014 Decl->setImplicit(); 1015 if (OrigRef) { 1016 Decl->addAttr( 1017 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1018 } 1019 return Decl; 1020 } 1021 1022 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1023 SourceLocation Loc, 1024 bool RefersToCapture = false) { 1025 D->setReferenced(); 1026 D->markUsed(S.Context); 1027 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1028 SourceLocation(), D, RefersToCapture, Loc, Ty, 1029 VK_LValue); 1030 } 1031 1032 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1033 BinaryOperatorKind BOK) { 1034 D = getCanonicalDecl(D); 1035 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1036 assert( 1037 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 1038 "Additional reduction info may be specified only for reduction items."); 1039 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 1040 assert(ReductionData.ReductionRange.isInvalid() && 1041 Stack.back().first.back().Directive == OMPD_taskgroup && 1042 "Additional reduction info may be specified only once for reduction " 1043 "items."); 1044 ReductionData.set(BOK, SR); 1045 Expr *&TaskgroupReductionRef = 1046 Stack.back().first.back().TaskgroupReductionRef; 1047 if (!TaskgroupReductionRef) { 1048 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1049 SemaRef.Context.VoidPtrTy, ".task_red."); 1050 TaskgroupReductionRef = 1051 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1052 } 1053 } 1054 1055 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1056 const Expr *ReductionRef) { 1057 D = getCanonicalDecl(D); 1058 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1059 assert( 1060 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 1061 "Additional reduction info may be specified only for reduction items."); 1062 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 1063 assert(ReductionData.ReductionRange.isInvalid() && 1064 Stack.back().first.back().Directive == OMPD_taskgroup && 1065 "Additional reduction info may be specified only once for reduction " 1066 "items."); 1067 ReductionData.set(ReductionRef, SR); 1068 Expr *&TaskgroupReductionRef = 1069 Stack.back().first.back().TaskgroupReductionRef; 1070 if (!TaskgroupReductionRef) { 1071 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1072 SemaRef.Context.VoidPtrTy, ".task_red."); 1073 TaskgroupReductionRef = 1074 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1075 } 1076 } 1077 1078 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1079 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1080 Expr *&TaskgroupDescriptor) const { 1081 D = getCanonicalDecl(D); 1082 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1083 if (Stack.back().first.empty()) 1084 return DSAVarData(); 1085 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 1086 E = Stack.back().first.rend(); 1087 I != E; std::advance(I, 1)) { 1088 const DSAInfo &Data = I->SharingMap.lookup(D); 1089 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1090 continue; 1091 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1092 if (!ReductionData.ReductionOp || 1093 ReductionData.ReductionOp.is<const Expr *>()) 1094 return DSAVarData(); 1095 SR = ReductionData.ReductionRange; 1096 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1097 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1098 "expression for the descriptor is not " 1099 "set."); 1100 TaskgroupDescriptor = I->TaskgroupReductionRef; 1101 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1102 Data.PrivateCopy, I->DefaultAttrLoc); 1103 } 1104 return DSAVarData(); 1105 } 1106 1107 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1108 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1109 Expr *&TaskgroupDescriptor) const { 1110 D = getCanonicalDecl(D); 1111 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1112 if (Stack.back().first.empty()) 1113 return DSAVarData(); 1114 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 1115 E = Stack.back().first.rend(); 1116 I != E; std::advance(I, 1)) { 1117 const DSAInfo &Data = I->SharingMap.lookup(D); 1118 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1119 continue; 1120 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1121 if (!ReductionData.ReductionOp || 1122 !ReductionData.ReductionOp.is<const Expr *>()) 1123 return DSAVarData(); 1124 SR = ReductionData.ReductionRange; 1125 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1126 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1127 "expression for the descriptor is not " 1128 "set."); 1129 TaskgroupDescriptor = I->TaskgroupReductionRef; 1130 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1131 Data.PrivateCopy, I->DefaultAttrLoc); 1132 } 1133 return DSAVarData(); 1134 } 1135 1136 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const { 1137 D = D->getCanonicalDecl(); 1138 if (!isStackEmpty()) { 1139 iterator I = Iter, E = Stack.back().first.rend(); 1140 Scope *TopScope = nullptr; 1141 while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) && 1142 !isOpenMPTargetExecutionDirective(I->Directive)) 1143 ++I; 1144 if (I == E) 1145 return false; 1146 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1147 Scope *CurScope = getCurScope(); 1148 while (CurScope != TopScope && !CurScope->isDeclScope(D)) 1149 CurScope = CurScope->getParent(); 1150 return CurScope != TopScope; 1151 } 1152 return false; 1153 } 1154 1155 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1156 bool AcceptIfMutable = true, 1157 bool *IsClassType = nullptr) { 1158 ASTContext &Context = SemaRef.getASTContext(); 1159 Type = Type.getNonReferenceType().getCanonicalType(); 1160 bool IsConstant = Type.isConstant(Context); 1161 Type = Context.getBaseElementType(Type); 1162 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1163 ? Type->getAsCXXRecordDecl() 1164 : nullptr; 1165 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1166 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1167 RD = CTD->getTemplatedDecl(); 1168 if (IsClassType) 1169 *IsClassType = RD; 1170 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1171 RD->hasDefinition() && RD->hasMutableFields()); 1172 } 1173 1174 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1175 QualType Type, OpenMPClauseKind CKind, 1176 SourceLocation ELoc, 1177 bool AcceptIfMutable = true, 1178 bool ListItemNotVar = false) { 1179 ASTContext &Context = SemaRef.getASTContext(); 1180 bool IsClassType; 1181 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1182 unsigned Diag = ListItemNotVar 1183 ? diag::err_omp_const_list_item 1184 : IsClassType ? diag::err_omp_const_not_mutable_variable 1185 : diag::err_omp_const_variable; 1186 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1187 if (!ListItemNotVar && D) { 1188 const VarDecl *VD = dyn_cast<VarDecl>(D); 1189 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1190 VarDecl::DeclarationOnly; 1191 SemaRef.Diag(D->getLocation(), 1192 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1193 << D; 1194 } 1195 return true; 1196 } 1197 return false; 1198 } 1199 1200 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1201 bool FromParent) { 1202 D = getCanonicalDecl(D); 1203 DSAVarData DVar; 1204 1205 auto *VD = dyn_cast<VarDecl>(D); 1206 auto TI = Threadprivates.find(D); 1207 if (TI != Threadprivates.end()) { 1208 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1209 DVar.CKind = OMPC_threadprivate; 1210 return DVar; 1211 } 1212 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1213 DVar.RefExpr = buildDeclRefExpr( 1214 SemaRef, VD, D->getType().getNonReferenceType(), 1215 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1216 DVar.CKind = OMPC_threadprivate; 1217 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1218 return DVar; 1219 } 1220 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1221 // in a Construct, C/C++, predetermined, p.1] 1222 // Variables appearing in threadprivate directives are threadprivate. 1223 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1224 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1225 SemaRef.getLangOpts().OpenMPUseTLS && 1226 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1227 (VD && VD->getStorageClass() == SC_Register && 1228 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1229 DVar.RefExpr = buildDeclRefExpr( 1230 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1231 DVar.CKind = OMPC_threadprivate; 1232 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1233 return DVar; 1234 } 1235 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1236 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1237 !isLoopControlVariable(D).first) { 1238 iterator IterTarget = 1239 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(), 1240 [](const SharingMapTy &Data) { 1241 return isOpenMPTargetExecutionDirective(Data.Directive); 1242 }); 1243 if (IterTarget != Stack.back().first.rend()) { 1244 iterator ParentIterTarget = std::next(IterTarget, 1); 1245 for (iterator Iter = Stack.back().first.rbegin(); 1246 Iter != ParentIterTarget; std::advance(Iter, 1)) { 1247 if (isOpenMPLocal(VD, Iter)) { 1248 DVar.RefExpr = 1249 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1250 D->getLocation()); 1251 DVar.CKind = OMPC_threadprivate; 1252 return DVar; 1253 } 1254 } 1255 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) { 1256 auto DSAIter = IterTarget->SharingMap.find(D); 1257 if (DSAIter != IterTarget->SharingMap.end() && 1258 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1259 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1260 DVar.CKind = OMPC_threadprivate; 1261 return DVar; 1262 } 1263 iterator End = Stack.back().first.rend(); 1264 if (!SemaRef.isOpenMPCapturedByRef( 1265 D, std::distance(ParentIterTarget, End))) { 1266 DVar.RefExpr = 1267 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1268 IterTarget->ConstructLoc); 1269 DVar.CKind = OMPC_threadprivate; 1270 return DVar; 1271 } 1272 } 1273 } 1274 } 1275 1276 if (isStackEmpty()) 1277 // Not in OpenMP execution region and top scope was already checked. 1278 return DVar; 1279 1280 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1281 // in a Construct, C/C++, predetermined, p.4] 1282 // Static data members are shared. 1283 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1284 // in a Construct, C/C++, predetermined, p.7] 1285 // Variables with static storage duration that are declared in a scope 1286 // inside the construct are shared. 1287 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1288 if (VD && VD->isStaticDataMember()) { 1289 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 1290 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1291 return DVar; 1292 1293 DVar.CKind = OMPC_shared; 1294 return DVar; 1295 } 1296 1297 // The predetermined shared attribute for const-qualified types having no 1298 // mutable members was removed after OpenMP 3.1. 1299 if (SemaRef.LangOpts.OpenMP <= 31) { 1300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1301 // in a Construct, C/C++, predetermined, p.6] 1302 // Variables with const qualified type having no mutable member are 1303 // shared. 1304 if (isConstNotMutableType(SemaRef, D->getType())) { 1305 // Variables with const-qualified type having no mutable member may be 1306 // listed in a firstprivate clause, even if they are static data members. 1307 DSAVarData DVarTemp = hasInnermostDSA( 1308 D, 1309 [](OpenMPClauseKind C) { 1310 return C == OMPC_firstprivate || C == OMPC_shared; 1311 }, 1312 MatchesAlways, FromParent); 1313 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1314 return DVarTemp; 1315 1316 DVar.CKind = OMPC_shared; 1317 return DVar; 1318 } 1319 } 1320 1321 // Explicitly specified attributes and local variables with predetermined 1322 // attributes. 1323 iterator I = Stack.back().first.rbegin(); 1324 iterator EndI = Stack.back().first.rend(); 1325 if (FromParent && I != EndI) 1326 std::advance(I, 1); 1327 auto It = I->SharingMap.find(D); 1328 if (It != I->SharingMap.end()) { 1329 const DSAInfo &Data = It->getSecond(); 1330 DVar.RefExpr = Data.RefExpr.getPointer(); 1331 DVar.PrivateCopy = Data.PrivateCopy; 1332 DVar.CKind = Data.Attributes; 1333 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1334 DVar.DKind = I->Directive; 1335 } 1336 1337 return DVar; 1338 } 1339 1340 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1341 bool FromParent) const { 1342 if (isStackEmpty()) { 1343 iterator I; 1344 return getDSA(I, D); 1345 } 1346 D = getCanonicalDecl(D); 1347 iterator StartI = Stack.back().first.rbegin(); 1348 iterator EndI = Stack.back().first.rend(); 1349 if (FromParent && StartI != EndI) 1350 std::advance(StartI, 1); 1351 return getDSA(StartI, D); 1352 } 1353 1354 const DSAStackTy::DSAVarData 1355 DSAStackTy::hasDSA(ValueDecl *D, 1356 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1357 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1358 bool FromParent) const { 1359 if (isStackEmpty()) 1360 return {}; 1361 D = getCanonicalDecl(D); 1362 iterator I = Stack.back().first.rbegin(); 1363 iterator EndI = Stack.back().first.rend(); 1364 if (FromParent && I != EndI) 1365 std::advance(I, 1); 1366 for (; I != EndI; std::advance(I, 1)) { 1367 if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive)) 1368 continue; 1369 iterator NewI = I; 1370 DSAVarData DVar = getDSA(NewI, D); 1371 if (I == NewI && CPred(DVar.CKind)) 1372 return DVar; 1373 } 1374 return {}; 1375 } 1376 1377 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1378 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1379 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1380 bool FromParent) const { 1381 if (isStackEmpty()) 1382 return {}; 1383 D = getCanonicalDecl(D); 1384 iterator StartI = Stack.back().first.rbegin(); 1385 iterator EndI = Stack.back().first.rend(); 1386 if (FromParent && StartI != EndI) 1387 std::advance(StartI, 1); 1388 if (StartI == EndI || !DPred(StartI->Directive)) 1389 return {}; 1390 iterator NewI = StartI; 1391 DSAVarData DVar = getDSA(NewI, D); 1392 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1393 } 1394 1395 bool DSAStackTy::hasExplicitDSA( 1396 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1397 unsigned Level, bool NotLastprivate) const { 1398 if (isStackEmpty()) 1399 return false; 1400 D = getCanonicalDecl(D); 1401 auto StartI = Stack.back().first.begin(); 1402 auto EndI = Stack.back().first.end(); 1403 if (std::distance(StartI, EndI) <= (int)Level) 1404 return false; 1405 std::advance(StartI, Level); 1406 auto I = StartI->SharingMap.find(D); 1407 if ((I != StartI->SharingMap.end()) && 1408 I->getSecond().RefExpr.getPointer() && 1409 CPred(I->getSecond().Attributes) && 1410 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1411 return true; 1412 // Check predetermined rules for the loop control variables. 1413 auto LI = StartI->LCVMap.find(D); 1414 if (LI != StartI->LCVMap.end()) 1415 return CPred(OMPC_private); 1416 return false; 1417 } 1418 1419 bool DSAStackTy::hasExplicitDirective( 1420 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1421 unsigned Level) const { 1422 if (isStackEmpty()) 1423 return false; 1424 auto StartI = Stack.back().first.begin(); 1425 auto EndI = Stack.back().first.end(); 1426 if (std::distance(StartI, EndI) <= (int)Level) 1427 return false; 1428 std::advance(StartI, Level); 1429 return DPred(StartI->Directive); 1430 } 1431 1432 bool DSAStackTy::hasDirective( 1433 const llvm::function_ref<bool(OpenMPDirectiveKind, 1434 const DeclarationNameInfo &, SourceLocation)> 1435 DPred, 1436 bool FromParent) const { 1437 // We look only in the enclosing region. 1438 if (isStackEmpty()) 1439 return false; 1440 auto StartI = std::next(Stack.back().first.rbegin()); 1441 auto EndI = Stack.back().first.rend(); 1442 if (FromParent && StartI != EndI) 1443 StartI = std::next(StartI); 1444 for (auto I = StartI, EE = EndI; I != EE; ++I) { 1445 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1446 return true; 1447 } 1448 return false; 1449 } 1450 1451 void Sema::InitDataSharingAttributesStack() { 1452 VarDataSharingAttributesStack = new DSAStackTy(*this); 1453 } 1454 1455 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1456 1457 void Sema::pushOpenMPFunctionRegion() { 1458 DSAStack->pushFunction(); 1459 } 1460 1461 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1462 DSAStack->popFunction(OldFSI); 1463 } 1464 1465 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1466 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1467 "Expected OpenMP device compilation."); 1468 return !S.isInOpenMPTargetExecutionDirective() && 1469 !S.isInOpenMPDeclareTargetContext(); 1470 } 1471 1472 /// Do we know that we will eventually codegen the given function? 1473 static bool isKnownEmitted(Sema &S, FunctionDecl *FD) { 1474 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1475 "Expected OpenMP device compilation."); 1476 // Templates are emitted when they're instantiated. 1477 if (FD->isDependentContext()) 1478 return false; 1479 1480 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1481 FD->getCanonicalDecl())) 1482 return true; 1483 1484 // Otherwise, the function is known-emitted if it's in our set of 1485 // known-emitted functions. 1486 return S.DeviceKnownEmittedFns.count(FD) > 0; 1487 } 1488 1489 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1490 unsigned DiagID) { 1491 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1492 "Expected OpenMP device compilation."); 1493 return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) && 1494 !isKnownEmitted(*this, getCurFunctionDecl())) 1495 ? DeviceDiagBuilder::K_Deferred 1496 : DeviceDiagBuilder::K_Immediate, 1497 Loc, DiagID, getCurFunctionDecl(), *this); 1498 } 1499 1500 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) { 1501 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1502 "Expected OpenMP device compilation."); 1503 assert(Callee && "Callee may not be null."); 1504 FunctionDecl *Caller = getCurFunctionDecl(); 1505 1506 // If the caller is known-emitted, mark the callee as known-emitted. 1507 // Otherwise, mark the call in our call graph so we can traverse it later. 1508 if (!isOpenMPDeviceDelayedContext(*this) || 1509 (Caller && isKnownEmitted(*this, Caller))) 1510 markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted); 1511 else if (Caller) 1512 DeviceCallGraph[Caller].insert({Callee, Loc}); 1513 } 1514 1515 void Sema::checkOpenMPDeviceExpr(const Expr *E) { 1516 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && 1517 "OpenMP device compilation mode is expected."); 1518 QualType Ty = E->getType(); 1519 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) || 1520 (Ty->isFloat128Type() && !Context.getTargetInfo().hasFloat128Type()) || 1521 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 && 1522 !Context.getTargetInfo().hasInt128Type())) 1523 targetDiag(E->getExprLoc(), diag::err_type_unsupported) 1524 << Ty << E->getSourceRange(); 1525 } 1526 1527 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { 1528 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1529 1530 ASTContext &Ctx = getASTContext(); 1531 bool IsByRef = true; 1532 1533 // Find the directive that is associated with the provided scope. 1534 D = cast<ValueDecl>(D->getCanonicalDecl()); 1535 QualType Ty = D->getType(); 1536 1537 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1538 // This table summarizes how a given variable should be passed to the device 1539 // given its type and the clauses where it appears. This table is based on 1540 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1541 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1542 // 1543 // ========================================================================= 1544 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1545 // | |(tofrom:scalar)| | pvt | | | | 1546 // ========================================================================= 1547 // | scl | | | | - | | bycopy| 1548 // | scl | | - | x | - | - | bycopy| 1549 // | scl | | x | - | - | - | null | 1550 // | scl | x | | | - | | byref | 1551 // | scl | x | - | x | - | - | bycopy| 1552 // | scl | x | x | - | - | - | null | 1553 // | scl | | - | - | - | x | byref | 1554 // | scl | x | - | - | - | x | byref | 1555 // 1556 // | agg | n.a. | | | - | | byref | 1557 // | agg | n.a. | - | x | - | - | byref | 1558 // | agg | n.a. | x | - | - | - | null | 1559 // | agg | n.a. | - | - | - | x | byref | 1560 // | agg | n.a. | - | - | - | x[] | byref | 1561 // 1562 // | ptr | n.a. | | | - | | bycopy| 1563 // | ptr | n.a. | - | x | - | - | bycopy| 1564 // | ptr | n.a. | x | - | - | - | null | 1565 // | ptr | n.a. | - | - | - | x | byref | 1566 // | ptr | n.a. | - | - | - | x[] | bycopy| 1567 // | ptr | n.a. | - | - | x | | bycopy| 1568 // | ptr | n.a. | - | - | x | x | bycopy| 1569 // | ptr | n.a. | - | - | x | x[] | bycopy| 1570 // ========================================================================= 1571 // Legend: 1572 // scl - scalar 1573 // ptr - pointer 1574 // agg - aggregate 1575 // x - applies 1576 // - - invalid in this combination 1577 // [] - mapped with an array section 1578 // byref - should be mapped by reference 1579 // byval - should be mapped by value 1580 // null - initialize a local variable to null on the device 1581 // 1582 // Observations: 1583 // - All scalar declarations that show up in a map clause have to be passed 1584 // by reference, because they may have been mapped in the enclosing data 1585 // environment. 1586 // - If the scalar value does not fit the size of uintptr, it has to be 1587 // passed by reference, regardless the result in the table above. 1588 // - For pointers mapped by value that have either an implicit map or an 1589 // array section, the runtime library may pass the NULL value to the 1590 // device instead of the value passed to it by the compiler. 1591 1592 if (Ty->isReferenceType()) 1593 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1594 1595 // Locate map clauses and see if the variable being captured is referred to 1596 // in any of those clauses. Here we only care about variables, not fields, 1597 // because fields are part of aggregates. 1598 bool IsVariableUsedInMapClause = false; 1599 bool IsVariableAssociatedWithSection = false; 1600 1601 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1602 D, Level, 1603 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1604 OMPClauseMappableExprCommon::MappableExprComponentListRef 1605 MapExprComponents, 1606 OpenMPClauseKind WhereFoundClauseKind) { 1607 // Only the map clause information influences how a variable is 1608 // captured. E.g. is_device_ptr does not require changing the default 1609 // behavior. 1610 if (WhereFoundClauseKind != OMPC_map) 1611 return false; 1612 1613 auto EI = MapExprComponents.rbegin(); 1614 auto EE = MapExprComponents.rend(); 1615 1616 assert(EI != EE && "Invalid map expression!"); 1617 1618 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1619 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1620 1621 ++EI; 1622 if (EI == EE) 1623 return false; 1624 1625 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1626 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1627 isa<MemberExpr>(EI->getAssociatedExpression())) { 1628 IsVariableAssociatedWithSection = true; 1629 // There is nothing more we need to know about this variable. 1630 return true; 1631 } 1632 1633 // Keep looking for more map info. 1634 return false; 1635 }); 1636 1637 if (IsVariableUsedInMapClause) { 1638 // If variable is identified in a map clause it is always captured by 1639 // reference except if it is a pointer that is dereferenced somehow. 1640 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1641 } else { 1642 // By default, all the data that has a scalar type is mapped by copy 1643 // (except for reduction variables). 1644 IsByRef = 1645 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1646 !Ty->isAnyPointerType()) || 1647 !Ty->isScalarType() || 1648 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar || 1649 DSAStack->hasExplicitDSA( 1650 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 1651 } 1652 } 1653 1654 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1655 IsByRef = 1656 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1657 !Ty->isAnyPointerType()) || 1658 !DSAStack->hasExplicitDSA( 1659 D, 1660 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1661 Level, /*NotLastprivate=*/true)) && 1662 // If the variable is artificial and must be captured by value - try to 1663 // capture by value. 1664 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 1665 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 1666 } 1667 1668 // When passing data by copy, we need to make sure it fits the uintptr size 1669 // and alignment, because the runtime library only deals with uintptr types. 1670 // If it does not fit the uintptr size, we need to pass the data by reference 1671 // instead. 1672 if (!IsByRef && 1673 (Ctx.getTypeSizeInChars(Ty) > 1674 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1675 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1676 IsByRef = true; 1677 } 1678 1679 return IsByRef; 1680 } 1681 1682 unsigned Sema::getOpenMPNestingLevel() const { 1683 assert(getLangOpts().OpenMP); 1684 return DSAStack->getNestingLevel(); 1685 } 1686 1687 bool Sema::isInOpenMPTargetExecutionDirective() const { 1688 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 1689 !DSAStack->isClauseParsingMode()) || 1690 DSAStack->hasDirective( 1691 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1692 SourceLocation) -> bool { 1693 return isOpenMPTargetExecutionDirective(K); 1694 }, 1695 false); 1696 } 1697 1698 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) { 1699 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1700 D = getCanonicalDecl(D); 1701 1702 // If we are attempting to capture a global variable in a directive with 1703 // 'target' we return true so that this global is also mapped to the device. 1704 // 1705 auto *VD = dyn_cast<VarDecl>(D); 1706 if (VD && !VD->hasLocalStorage()) { 1707 if (isInOpenMPDeclareTargetContext() && 1708 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 1709 // Try to mark variable as declare target if it is used in capturing 1710 // regions. 1711 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1712 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 1713 return nullptr; 1714 } else if (isInOpenMPTargetExecutionDirective()) { 1715 // If the declaration is enclosed in a 'declare target' directive, 1716 // then it should not be captured. 1717 // 1718 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1719 return nullptr; 1720 return VD; 1721 } 1722 } 1723 // Capture variables captured by reference in lambdas for target-based 1724 // directives. 1725 if (VD && !DSAStack->isClauseParsingMode()) { 1726 if (const auto *RD = VD->getType() 1727 .getCanonicalType() 1728 .getNonReferenceType() 1729 ->getAsCXXRecordDecl()) { 1730 bool SavedForceCaptureByReferenceInTargetExecutable = 1731 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 1732 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true); 1733 if (RD->isLambda()) { 1734 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 1735 FieldDecl *ThisCapture; 1736 RD->getCaptureFields(Captures, ThisCapture); 1737 for (const LambdaCapture &LC : RD->captures()) { 1738 if (LC.getCaptureKind() == LCK_ByRef) { 1739 VarDecl *VD = LC.getCapturedVar(); 1740 DeclContext *VDC = VD->getDeclContext(); 1741 if (!VDC->Encloses(CurContext)) 1742 continue; 1743 DSAStackTy::DSAVarData DVarPrivate = 1744 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1745 // Do not capture already captured variables. 1746 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && 1747 DVarPrivate.CKind == OMPC_unknown && 1748 !DSAStack->checkMappableExprComponentListsForDecl( 1749 D, /*CurrentRegionOnly=*/true, 1750 [](OMPClauseMappableExprCommon:: 1751 MappableExprComponentListRef, 1752 OpenMPClauseKind) { return true; })) 1753 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar()); 1754 } else if (LC.getCaptureKind() == LCK_This) { 1755 QualType ThisTy = getCurrentThisType(); 1756 if (!ThisTy.isNull() && 1757 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 1758 CheckCXXThisCapture(LC.getLocation()); 1759 } 1760 } 1761 } 1762 DSAStack->setForceCaptureByReferenceInTargetExecutable( 1763 SavedForceCaptureByReferenceInTargetExecutable); 1764 } 1765 } 1766 1767 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1768 (!DSAStack->isClauseParsingMode() || 1769 DSAStack->getParentDirective() != OMPD_unknown)) { 1770 auto &&Info = DSAStack->isLoopControlVariable(D); 1771 if (Info.first || 1772 (VD && VD->hasLocalStorage() && 1773 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 1774 (VD && DSAStack->isForceVarCapturing())) 1775 return VD ? VD : Info.second; 1776 DSAStackTy::DSAVarData DVarPrivate = 1777 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1778 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1779 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1780 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1781 [](OpenMPDirectiveKind) { return true; }, 1782 DSAStack->isClauseParsingMode()); 1783 if (DVarPrivate.CKind != OMPC_unknown) 1784 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1785 } 1786 return nullptr; 1787 } 1788 1789 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1790 unsigned Level) const { 1791 SmallVector<OpenMPDirectiveKind, 4> Regions; 1792 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1793 FunctionScopesIndex -= Regions.size(); 1794 } 1795 1796 void Sema::startOpenMPLoop() { 1797 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 1798 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 1799 DSAStack->loopInit(); 1800 } 1801 1802 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1803 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1804 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 1805 if (DSAStack->getAssociatedLoops() > 0 && 1806 !DSAStack->isLoopStarted()) { 1807 DSAStack->resetPossibleLoopCounter(D); 1808 DSAStack->loopStart(); 1809 return true; 1810 } 1811 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 1812 DSAStack->isLoopControlVariable(D).first) && 1813 !DSAStack->hasExplicitDSA( 1814 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 1815 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 1816 return true; 1817 } 1818 return DSAStack->hasExplicitDSA( 1819 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1820 (DSAStack->isClauseParsingMode() && 1821 DSAStack->getClauseParsingMode() == OMPC_private) || 1822 // Consider taskgroup reduction descriptor variable a private to avoid 1823 // possible capture in the region. 1824 (DSAStack->hasExplicitDirective( 1825 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1826 Level) && 1827 DSAStack->isTaskgroupReductionRef(D, Level)); 1828 } 1829 1830 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1831 unsigned Level) { 1832 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1833 D = getCanonicalDecl(D); 1834 OpenMPClauseKind OMPC = OMPC_unknown; 1835 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1836 const unsigned NewLevel = I - 1; 1837 if (DSAStack->hasExplicitDSA(D, 1838 [&OMPC](const OpenMPClauseKind K) { 1839 if (isOpenMPPrivate(K)) { 1840 OMPC = K; 1841 return true; 1842 } 1843 return false; 1844 }, 1845 NewLevel)) 1846 break; 1847 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1848 D, NewLevel, 1849 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1850 OpenMPClauseKind) { return true; })) { 1851 OMPC = OMPC_map; 1852 break; 1853 } 1854 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1855 NewLevel)) { 1856 OMPC = OMPC_map; 1857 if (D->getType()->isScalarType() && 1858 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1859 DefaultMapAttributes::DMA_tofrom_scalar) 1860 OMPC = OMPC_firstprivate; 1861 break; 1862 } 1863 } 1864 if (OMPC != OMPC_unknown) 1865 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1866 } 1867 1868 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1869 unsigned Level) const { 1870 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1871 // Return true if the current level is no longer enclosed in a target region. 1872 1873 const auto *VD = dyn_cast<VarDecl>(D); 1874 return VD && !VD->hasLocalStorage() && 1875 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1876 Level); 1877 } 1878 1879 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1880 1881 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1882 const DeclarationNameInfo &DirName, 1883 Scope *CurScope, SourceLocation Loc) { 1884 DSAStack->push(DKind, DirName, CurScope, Loc); 1885 PushExpressionEvaluationContext( 1886 ExpressionEvaluationContext::PotentiallyEvaluated); 1887 } 1888 1889 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1890 DSAStack->setClauseParsingMode(K); 1891 } 1892 1893 void Sema::EndOpenMPClause() { 1894 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1895 } 1896 1897 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 1898 ArrayRef<OMPClause *> Clauses); 1899 1900 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1901 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1902 // A variable of class type (or array thereof) that appears in a lastprivate 1903 // clause requires an accessible, unambiguous default constructor for the 1904 // class type, unless the list item is also specified in a firstprivate 1905 // clause. 1906 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1907 for (OMPClause *C : D->clauses()) { 1908 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1909 SmallVector<Expr *, 8> PrivateCopies; 1910 for (Expr *DE : Clause->varlists()) { 1911 if (DE->isValueDependent() || DE->isTypeDependent()) { 1912 PrivateCopies.push_back(nullptr); 1913 continue; 1914 } 1915 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1916 auto *VD = cast<VarDecl>(DRE->getDecl()); 1917 QualType Type = VD->getType().getNonReferenceType(); 1918 const DSAStackTy::DSAVarData DVar = 1919 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1920 if (DVar.CKind == OMPC_lastprivate) { 1921 // Generate helper private variable and initialize it with the 1922 // default value. The address of the original variable is replaced 1923 // by the address of the new private variable in CodeGen. This new 1924 // variable is not added to IdResolver, so the code in the OpenMP 1925 // region uses original variable for proper diagnostics. 1926 VarDecl *VDPrivate = buildVarDecl( 1927 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1928 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1929 ActOnUninitializedDecl(VDPrivate); 1930 if (VDPrivate->isInvalidDecl()) { 1931 PrivateCopies.push_back(nullptr); 1932 continue; 1933 } 1934 PrivateCopies.push_back(buildDeclRefExpr( 1935 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1936 } else { 1937 // The variable is also a firstprivate, so initialization sequence 1938 // for private copy is generated already. 1939 PrivateCopies.push_back(nullptr); 1940 } 1941 } 1942 Clause->setPrivateCopies(PrivateCopies); 1943 } 1944 } 1945 // Check allocate clauses. 1946 if (!CurContext->isDependentContext()) 1947 checkAllocateClauses(*this, DSAStack, D->clauses()); 1948 } 1949 1950 DSAStack->pop(); 1951 DiscardCleanupsInEvaluationContext(); 1952 PopExpressionEvaluationContext(); 1953 } 1954 1955 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1956 Expr *NumIterations, Sema &SemaRef, 1957 Scope *S, DSAStackTy *Stack); 1958 1959 namespace { 1960 1961 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 1962 private: 1963 Sema &SemaRef; 1964 1965 public: 1966 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1967 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1968 NamedDecl *ND = Candidate.getCorrectionDecl(); 1969 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1970 return VD->hasGlobalStorage() && 1971 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1972 SemaRef.getCurScope()); 1973 } 1974 return false; 1975 } 1976 1977 std::unique_ptr<CorrectionCandidateCallback> clone() override { 1978 return llvm::make_unique<VarDeclFilterCCC>(*this); 1979 } 1980 1981 }; 1982 1983 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 1984 private: 1985 Sema &SemaRef; 1986 1987 public: 1988 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1989 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1990 NamedDecl *ND = Candidate.getCorrectionDecl(); 1991 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 1992 isa<FunctionDecl>(ND))) { 1993 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1994 SemaRef.getCurScope()); 1995 } 1996 return false; 1997 } 1998 1999 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2000 return llvm::make_unique<VarOrFuncDeclFilterCCC>(*this); 2001 } 2002 }; 2003 2004 } // namespace 2005 2006 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2007 CXXScopeSpec &ScopeSpec, 2008 const DeclarationNameInfo &Id, 2009 OpenMPDirectiveKind Kind) { 2010 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2011 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2012 2013 if (Lookup.isAmbiguous()) 2014 return ExprError(); 2015 2016 VarDecl *VD; 2017 if (!Lookup.isSingleResult()) { 2018 VarDeclFilterCCC CCC(*this); 2019 if (TypoCorrection Corrected = 2020 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2021 CTK_ErrorRecovery)) { 2022 diagnoseTypo(Corrected, 2023 PDiag(Lookup.empty() 2024 ? diag::err_undeclared_var_use_suggest 2025 : diag::err_omp_expected_var_arg_suggest) 2026 << Id.getName()); 2027 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2028 } else { 2029 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2030 : diag::err_omp_expected_var_arg) 2031 << Id.getName(); 2032 return ExprError(); 2033 } 2034 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2035 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2036 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2037 return ExprError(); 2038 } 2039 Lookup.suppressDiagnostics(); 2040 2041 // OpenMP [2.9.2, Syntax, C/C++] 2042 // Variables must be file-scope, namespace-scope, or static block-scope. 2043 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2044 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2045 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2046 bool IsDecl = 2047 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2048 Diag(VD->getLocation(), 2049 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2050 << VD; 2051 return ExprError(); 2052 } 2053 2054 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2055 NamedDecl *ND = CanonicalVD; 2056 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2057 // A threadprivate directive for file-scope variables must appear outside 2058 // any definition or declaration. 2059 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2060 !getCurLexicalContext()->isTranslationUnit()) { 2061 Diag(Id.getLoc(), diag::err_omp_var_scope) 2062 << getOpenMPDirectiveName(Kind) << VD; 2063 bool IsDecl = 2064 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2065 Diag(VD->getLocation(), 2066 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2067 << VD; 2068 return ExprError(); 2069 } 2070 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2071 // A threadprivate directive for static class member variables must appear 2072 // in the class definition, in the same scope in which the member 2073 // variables are declared. 2074 if (CanonicalVD->isStaticDataMember() && 2075 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2076 Diag(Id.getLoc(), diag::err_omp_var_scope) 2077 << getOpenMPDirectiveName(Kind) << VD; 2078 bool IsDecl = 2079 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2080 Diag(VD->getLocation(), 2081 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2082 << VD; 2083 return ExprError(); 2084 } 2085 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2086 // A threadprivate directive for namespace-scope variables must appear 2087 // outside any definition or declaration other than the namespace 2088 // definition itself. 2089 if (CanonicalVD->getDeclContext()->isNamespace() && 2090 (!getCurLexicalContext()->isFileContext() || 2091 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2092 Diag(Id.getLoc(), diag::err_omp_var_scope) 2093 << getOpenMPDirectiveName(Kind) << VD; 2094 bool IsDecl = 2095 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2096 Diag(VD->getLocation(), 2097 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2098 << VD; 2099 return ExprError(); 2100 } 2101 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2102 // A threadprivate directive for static block-scope variables must appear 2103 // in the scope of the variable and not in a nested scope. 2104 if (CanonicalVD->isLocalVarDecl() && CurScope && 2105 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2106 Diag(Id.getLoc(), diag::err_omp_var_scope) 2107 << getOpenMPDirectiveName(Kind) << VD; 2108 bool IsDecl = 2109 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2110 Diag(VD->getLocation(), 2111 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2112 << VD; 2113 return ExprError(); 2114 } 2115 2116 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2117 // A threadprivate directive must lexically precede all references to any 2118 // of the variables in its list. 2119 if (Kind == OMPD_threadprivate && VD->isUsed() && 2120 !DSAStack->isThreadPrivate(VD)) { 2121 Diag(Id.getLoc(), diag::err_omp_var_used) 2122 << getOpenMPDirectiveName(Kind) << VD; 2123 return ExprError(); 2124 } 2125 2126 QualType ExprType = VD->getType().getNonReferenceType(); 2127 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2128 SourceLocation(), VD, 2129 /*RefersToEnclosingVariableOrCapture=*/false, 2130 Id.getLoc(), ExprType, VK_LValue); 2131 } 2132 2133 Sema::DeclGroupPtrTy 2134 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2135 ArrayRef<Expr *> VarList) { 2136 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2137 CurContext->addDecl(D); 2138 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2139 } 2140 return nullptr; 2141 } 2142 2143 namespace { 2144 class LocalVarRefChecker final 2145 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2146 Sema &SemaRef; 2147 2148 public: 2149 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2150 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2151 if (VD->hasLocalStorage()) { 2152 SemaRef.Diag(E->getBeginLoc(), 2153 diag::err_omp_local_var_in_threadprivate_init) 2154 << E->getSourceRange(); 2155 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2156 << VD << VD->getSourceRange(); 2157 return true; 2158 } 2159 } 2160 return false; 2161 } 2162 bool VisitStmt(const Stmt *S) { 2163 for (const Stmt *Child : S->children()) { 2164 if (Child && Visit(Child)) 2165 return true; 2166 } 2167 return false; 2168 } 2169 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2170 }; 2171 } // namespace 2172 2173 OMPThreadPrivateDecl * 2174 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2175 SmallVector<Expr *, 8> Vars; 2176 for (Expr *RefExpr : VarList) { 2177 auto *DE = cast<DeclRefExpr>(RefExpr); 2178 auto *VD = cast<VarDecl>(DE->getDecl()); 2179 SourceLocation ILoc = DE->getExprLoc(); 2180 2181 // Mark variable as used. 2182 VD->setReferenced(); 2183 VD->markUsed(Context); 2184 2185 QualType QType = VD->getType(); 2186 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2187 // It will be analyzed later. 2188 Vars.push_back(DE); 2189 continue; 2190 } 2191 2192 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2193 // A threadprivate variable must not have an incomplete type. 2194 if (RequireCompleteType(ILoc, VD->getType(), 2195 diag::err_omp_threadprivate_incomplete_type)) { 2196 continue; 2197 } 2198 2199 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2200 // A threadprivate variable must not have a reference type. 2201 if (VD->getType()->isReferenceType()) { 2202 Diag(ILoc, diag::err_omp_ref_type_arg) 2203 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2204 bool IsDecl = 2205 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2206 Diag(VD->getLocation(), 2207 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2208 << VD; 2209 continue; 2210 } 2211 2212 // Check if this is a TLS variable. If TLS is not being supported, produce 2213 // the corresponding diagnostic. 2214 if ((VD->getTLSKind() != VarDecl::TLS_None && 2215 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2216 getLangOpts().OpenMPUseTLS && 2217 getASTContext().getTargetInfo().isTLSSupported())) || 2218 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2219 !VD->isLocalVarDecl())) { 2220 Diag(ILoc, diag::err_omp_var_thread_local) 2221 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2222 bool IsDecl = 2223 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2224 Diag(VD->getLocation(), 2225 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2226 << VD; 2227 continue; 2228 } 2229 2230 // Check if initial value of threadprivate variable reference variable with 2231 // local storage (it is not supported by runtime). 2232 if (const Expr *Init = VD->getAnyInitializer()) { 2233 LocalVarRefChecker Checker(*this); 2234 if (Checker.Visit(Init)) 2235 continue; 2236 } 2237 2238 Vars.push_back(RefExpr); 2239 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2240 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2241 Context, SourceRange(Loc, Loc))); 2242 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2243 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2244 } 2245 OMPThreadPrivateDecl *D = nullptr; 2246 if (!Vars.empty()) { 2247 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2248 Vars); 2249 D->setAccess(AS_public); 2250 } 2251 return D; 2252 } 2253 2254 static OMPAllocateDeclAttr::AllocatorTypeTy 2255 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 2256 if (!Allocator) 2257 return OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2258 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2259 Allocator->isInstantiationDependent() || 2260 Allocator->containsUnexpandedParameterPack()) 2261 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2262 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2263 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2264 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2265 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 2266 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 2267 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 2268 llvm::FoldingSetNodeID AEId, DAEId; 2269 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 2270 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 2271 if (AEId == DAEId) { 2272 AllocatorKindRes = AllocatorKind; 2273 break; 2274 } 2275 } 2276 return AllocatorKindRes; 2277 } 2278 2279 static bool checkPreviousOMPAllocateAttribute( 2280 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 2281 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 2282 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 2283 return false; 2284 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 2285 Expr *PrevAllocator = A->getAllocator(); 2286 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 2287 getAllocatorKind(S, Stack, PrevAllocator); 2288 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 2289 if (AllocatorsMatch && 2290 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 2291 Allocator && PrevAllocator) { 2292 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2293 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 2294 llvm::FoldingSetNodeID AEId, PAEId; 2295 AE->Profile(AEId, S.Context, /*Canonical=*/true); 2296 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 2297 AllocatorsMatch = AEId == PAEId; 2298 } 2299 if (!AllocatorsMatch) { 2300 SmallString<256> AllocatorBuffer; 2301 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 2302 if (Allocator) 2303 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 2304 SmallString<256> PrevAllocatorBuffer; 2305 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 2306 if (PrevAllocator) 2307 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 2308 S.getPrintingPolicy()); 2309 2310 SourceLocation AllocatorLoc = 2311 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 2312 SourceRange AllocatorRange = 2313 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 2314 SourceLocation PrevAllocatorLoc = 2315 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 2316 SourceRange PrevAllocatorRange = 2317 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 2318 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 2319 << (Allocator ? 1 : 0) << AllocatorStream.str() 2320 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 2321 << AllocatorRange; 2322 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 2323 << PrevAllocatorRange; 2324 return true; 2325 } 2326 return false; 2327 } 2328 2329 static void 2330 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 2331 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 2332 Expr *Allocator, SourceRange SR) { 2333 if (VD->hasAttr<OMPAllocateDeclAttr>()) 2334 return; 2335 if (Allocator && 2336 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2337 Allocator->isInstantiationDependent() || 2338 Allocator->containsUnexpandedParameterPack())) 2339 return; 2340 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 2341 Allocator, SR); 2342 VD->addAttr(A); 2343 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 2344 ML->DeclarationMarkedOpenMPAllocate(VD, A); 2345 } 2346 2347 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 2348 SourceLocation Loc, ArrayRef<Expr *> VarList, 2349 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 2350 assert(Clauses.size() <= 1 && "Expected at most one clause."); 2351 Expr *Allocator = nullptr; 2352 if (Clauses.empty()) { 2353 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 2354 // allocate directives that appear in a target region must specify an 2355 // allocator clause unless a requires directive with the dynamic_allocators 2356 // clause is present in the same compilation unit. 2357 if (LangOpts.OpenMPIsDevice && 2358 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 2359 targetDiag(Loc, diag::err_expected_allocator_clause); 2360 } else { 2361 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 2362 } 2363 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 2364 getAllocatorKind(*this, DSAStack, Allocator); 2365 SmallVector<Expr *, 8> Vars; 2366 for (Expr *RefExpr : VarList) { 2367 auto *DE = cast<DeclRefExpr>(RefExpr); 2368 auto *VD = cast<VarDecl>(DE->getDecl()); 2369 2370 // Check if this is a TLS variable or global register. 2371 if (VD->getTLSKind() != VarDecl::TLS_None || 2372 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 2373 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2374 !VD->isLocalVarDecl())) 2375 continue; 2376 2377 // If the used several times in the allocate directive, the same allocator 2378 // must be used. 2379 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 2380 AllocatorKind, Allocator)) 2381 continue; 2382 2383 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 2384 // If a list item has a static storage type, the allocator expression in the 2385 // allocator clause must be a constant expression that evaluates to one of 2386 // the predefined memory allocator values. 2387 if (Allocator && VD->hasGlobalStorage()) { 2388 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 2389 Diag(Allocator->getExprLoc(), 2390 diag::err_omp_expected_predefined_allocator) 2391 << Allocator->getSourceRange(); 2392 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2393 VarDecl::DeclarationOnly; 2394 Diag(VD->getLocation(), 2395 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2396 << VD; 2397 continue; 2398 } 2399 } 2400 2401 Vars.push_back(RefExpr); 2402 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 2403 DE->getSourceRange()); 2404 } 2405 if (Vars.empty()) 2406 return nullptr; 2407 if (!Owner) 2408 Owner = getCurLexicalContext(); 2409 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 2410 D->setAccess(AS_public); 2411 Owner->addDecl(D); 2412 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2413 } 2414 2415 Sema::DeclGroupPtrTy 2416 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 2417 ArrayRef<OMPClause *> ClauseList) { 2418 OMPRequiresDecl *D = nullptr; 2419 if (!CurContext->isFileContext()) { 2420 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 2421 } else { 2422 D = CheckOMPRequiresDecl(Loc, ClauseList); 2423 if (D) { 2424 CurContext->addDecl(D); 2425 DSAStack->addRequiresDecl(D); 2426 } 2427 } 2428 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2429 } 2430 2431 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 2432 ArrayRef<OMPClause *> ClauseList) { 2433 /// For target specific clauses, the requires directive cannot be 2434 /// specified after the handling of any of the target regions in the 2435 /// current compilation unit. 2436 ArrayRef<SourceLocation> TargetLocations = 2437 DSAStack->getEncounteredTargetLocs(); 2438 if (!TargetLocations.empty()) { 2439 for (const OMPClause *CNew : ClauseList) { 2440 // Check if any of the requires clauses affect target regions. 2441 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 2442 isa<OMPUnifiedAddressClause>(CNew) || 2443 isa<OMPReverseOffloadClause>(CNew) || 2444 isa<OMPDynamicAllocatorsClause>(CNew)) { 2445 Diag(Loc, diag::err_omp_target_before_requires) 2446 << getOpenMPClauseName(CNew->getClauseKind()); 2447 for (SourceLocation TargetLoc : TargetLocations) { 2448 Diag(TargetLoc, diag::note_omp_requires_encountered_target); 2449 } 2450 } 2451 } 2452 } 2453 2454 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 2455 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 2456 ClauseList); 2457 return nullptr; 2458 } 2459 2460 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2461 const ValueDecl *D, 2462 const DSAStackTy::DSAVarData &DVar, 2463 bool IsLoopIterVar = false) { 2464 if (DVar.RefExpr) { 2465 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 2466 << getOpenMPClauseName(DVar.CKind); 2467 return; 2468 } 2469 enum { 2470 PDSA_StaticMemberShared, 2471 PDSA_StaticLocalVarShared, 2472 PDSA_LoopIterVarPrivate, 2473 PDSA_LoopIterVarLinear, 2474 PDSA_LoopIterVarLastprivate, 2475 PDSA_ConstVarShared, 2476 PDSA_GlobalVarShared, 2477 PDSA_TaskVarFirstprivate, 2478 PDSA_LocalVarPrivate, 2479 PDSA_Implicit 2480 } Reason = PDSA_Implicit; 2481 bool ReportHint = false; 2482 auto ReportLoc = D->getLocation(); 2483 auto *VD = dyn_cast<VarDecl>(D); 2484 if (IsLoopIterVar) { 2485 if (DVar.CKind == OMPC_private) 2486 Reason = PDSA_LoopIterVarPrivate; 2487 else if (DVar.CKind == OMPC_lastprivate) 2488 Reason = PDSA_LoopIterVarLastprivate; 2489 else 2490 Reason = PDSA_LoopIterVarLinear; 2491 } else if (isOpenMPTaskingDirective(DVar.DKind) && 2492 DVar.CKind == OMPC_firstprivate) { 2493 Reason = PDSA_TaskVarFirstprivate; 2494 ReportLoc = DVar.ImplicitDSALoc; 2495 } else if (VD && VD->isStaticLocal()) 2496 Reason = PDSA_StaticLocalVarShared; 2497 else if (VD && VD->isStaticDataMember()) 2498 Reason = PDSA_StaticMemberShared; 2499 else if (VD && VD->isFileVarDecl()) 2500 Reason = PDSA_GlobalVarShared; 2501 else if (D->getType().isConstant(SemaRef.getASTContext())) 2502 Reason = PDSA_ConstVarShared; 2503 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 2504 ReportHint = true; 2505 Reason = PDSA_LocalVarPrivate; 2506 } 2507 if (Reason != PDSA_Implicit) { 2508 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 2509 << Reason << ReportHint 2510 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 2511 } else if (DVar.ImplicitDSALoc.isValid()) { 2512 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 2513 << getOpenMPClauseName(DVar.CKind); 2514 } 2515 } 2516 2517 namespace { 2518 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 2519 DSAStackTy *Stack; 2520 Sema &SemaRef; 2521 bool ErrorFound = false; 2522 CapturedStmt *CS = nullptr; 2523 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 2524 llvm::SmallVector<Expr *, 4> ImplicitMap; 2525 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 2526 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 2527 2528 void VisitSubCaptures(OMPExecutableDirective *S) { 2529 // Check implicitly captured variables. 2530 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 2531 return; 2532 for (const CapturedStmt::Capture &Cap : 2533 S->getInnermostCapturedStmt()->captures()) { 2534 if (!Cap.capturesVariable()) 2535 continue; 2536 VarDecl *VD = Cap.getCapturedVar(); 2537 // Do not try to map the variable if it or its sub-component was mapped 2538 // already. 2539 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 2540 Stack->checkMappableExprComponentListsForDecl( 2541 VD, /*CurrentRegionOnly=*/true, 2542 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2543 OpenMPClauseKind) { return true; })) 2544 continue; 2545 DeclRefExpr *DRE = buildDeclRefExpr( 2546 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 2547 Cap.getLocation(), /*RefersToCapture=*/true); 2548 Visit(DRE); 2549 } 2550 } 2551 2552 public: 2553 void VisitDeclRefExpr(DeclRefExpr *E) { 2554 if (E->isTypeDependent() || E->isValueDependent() || 2555 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2556 return; 2557 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2558 VD = VD->getCanonicalDecl(); 2559 // Skip internally declared variables. 2560 if (VD->hasLocalStorage() && !CS->capturesVariable(VD)) 2561 return; 2562 2563 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 2564 // Check if the variable has explicit DSA set and stop analysis if it so. 2565 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 2566 return; 2567 2568 // Skip internally declared static variables. 2569 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 2570 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 2571 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) && 2572 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 2573 return; 2574 2575 SourceLocation ELoc = E->getExprLoc(); 2576 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2577 // The default(none) clause requires that each variable that is referenced 2578 // in the construct, and does not have a predetermined data-sharing 2579 // attribute, must have its data-sharing attribute explicitly determined 2580 // by being listed in a data-sharing attribute clause. 2581 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 2582 isImplicitOrExplicitTaskingRegion(DKind) && 2583 VarsWithInheritedDSA.count(VD) == 0) { 2584 VarsWithInheritedDSA[VD] = E; 2585 return; 2586 } 2587 2588 if (isOpenMPTargetExecutionDirective(DKind) && 2589 !Stack->isLoopControlVariable(VD).first) { 2590 if (!Stack->checkMappableExprComponentListsForDecl( 2591 VD, /*CurrentRegionOnly=*/true, 2592 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2593 StackComponents, 2594 OpenMPClauseKind) { 2595 // Variable is used if it has been marked as an array, array 2596 // section or the variable iself. 2597 return StackComponents.size() == 1 || 2598 std::all_of( 2599 std::next(StackComponents.rbegin()), 2600 StackComponents.rend(), 2601 [](const OMPClauseMappableExprCommon:: 2602 MappableComponent &MC) { 2603 return MC.getAssociatedDeclaration() == 2604 nullptr && 2605 (isa<OMPArraySectionExpr>( 2606 MC.getAssociatedExpression()) || 2607 isa<ArraySubscriptExpr>( 2608 MC.getAssociatedExpression())); 2609 }); 2610 })) { 2611 bool IsFirstprivate = false; 2612 // By default lambdas are captured as firstprivates. 2613 if (const auto *RD = 2614 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2615 IsFirstprivate = RD->isLambda(); 2616 IsFirstprivate = 2617 IsFirstprivate || 2618 (VD->getType().getNonReferenceType()->isScalarType() && 2619 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2620 if (IsFirstprivate) 2621 ImplicitFirstprivate.emplace_back(E); 2622 else 2623 ImplicitMap.emplace_back(E); 2624 return; 2625 } 2626 } 2627 2628 // OpenMP [2.9.3.6, Restrictions, p.2] 2629 // A list item that appears in a reduction clause of the innermost 2630 // enclosing worksharing or parallel construct may not be accessed in an 2631 // explicit task. 2632 DVar = Stack->hasInnermostDSA( 2633 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2634 [](OpenMPDirectiveKind K) { 2635 return isOpenMPParallelDirective(K) || 2636 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2637 }, 2638 /*FromParent=*/true); 2639 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2640 ErrorFound = true; 2641 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2642 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2643 return; 2644 } 2645 2646 // Define implicit data-sharing attributes for task. 2647 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2648 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2649 !Stack->isLoopControlVariable(VD).first) { 2650 ImplicitFirstprivate.push_back(E); 2651 return; 2652 } 2653 2654 // Store implicitly used globals with declare target link for parent 2655 // target. 2656 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 2657 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 2658 Stack->addToParentTargetRegionLinkGlobals(E); 2659 return; 2660 } 2661 } 2662 } 2663 void VisitMemberExpr(MemberExpr *E) { 2664 if (E->isTypeDependent() || E->isValueDependent() || 2665 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2666 return; 2667 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2668 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2669 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2670 if (!FD) 2671 return; 2672 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2673 // Check if the variable has explicit DSA set and stop analysis if it 2674 // so. 2675 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2676 return; 2677 2678 if (isOpenMPTargetExecutionDirective(DKind) && 2679 !Stack->isLoopControlVariable(FD).first && 2680 !Stack->checkMappableExprComponentListsForDecl( 2681 FD, /*CurrentRegionOnly=*/true, 2682 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2683 StackComponents, 2684 OpenMPClauseKind) { 2685 return isa<CXXThisExpr>( 2686 cast<MemberExpr>( 2687 StackComponents.back().getAssociatedExpression()) 2688 ->getBase() 2689 ->IgnoreParens()); 2690 })) { 2691 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2692 // A bit-field cannot appear in a map clause. 2693 // 2694 if (FD->isBitField()) 2695 return; 2696 2697 // Check to see if the member expression is referencing a class that 2698 // has already been explicitly mapped 2699 if (Stack->isClassPreviouslyMapped(TE->getType())) 2700 return; 2701 2702 ImplicitMap.emplace_back(E); 2703 return; 2704 } 2705 2706 SourceLocation ELoc = E->getExprLoc(); 2707 // OpenMP [2.9.3.6, Restrictions, p.2] 2708 // A list item that appears in a reduction clause of the innermost 2709 // enclosing worksharing or parallel construct may not be accessed in 2710 // an explicit task. 2711 DVar = Stack->hasInnermostDSA( 2712 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2713 [](OpenMPDirectiveKind K) { 2714 return isOpenMPParallelDirective(K) || 2715 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2716 }, 2717 /*FromParent=*/true); 2718 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2719 ErrorFound = true; 2720 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2721 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2722 return; 2723 } 2724 2725 // Define implicit data-sharing attributes for task. 2726 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2727 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2728 !Stack->isLoopControlVariable(FD).first) { 2729 // Check if there is a captured expression for the current field in the 2730 // region. Do not mark it as firstprivate unless there is no captured 2731 // expression. 2732 // TODO: try to make it firstprivate. 2733 if (DVar.CKind != OMPC_unknown) 2734 ImplicitFirstprivate.push_back(E); 2735 } 2736 return; 2737 } 2738 if (isOpenMPTargetExecutionDirective(DKind)) { 2739 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2740 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2741 /*NoDiagnose=*/true)) 2742 return; 2743 const auto *VD = cast<ValueDecl>( 2744 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2745 if (!Stack->checkMappableExprComponentListsForDecl( 2746 VD, /*CurrentRegionOnly=*/true, 2747 [&CurComponents]( 2748 OMPClauseMappableExprCommon::MappableExprComponentListRef 2749 StackComponents, 2750 OpenMPClauseKind) { 2751 auto CCI = CurComponents.rbegin(); 2752 auto CCE = CurComponents.rend(); 2753 for (const auto &SC : llvm::reverse(StackComponents)) { 2754 // Do both expressions have the same kind? 2755 if (CCI->getAssociatedExpression()->getStmtClass() != 2756 SC.getAssociatedExpression()->getStmtClass()) 2757 if (!(isa<OMPArraySectionExpr>( 2758 SC.getAssociatedExpression()) && 2759 isa<ArraySubscriptExpr>( 2760 CCI->getAssociatedExpression()))) 2761 return false; 2762 2763 const Decl *CCD = CCI->getAssociatedDeclaration(); 2764 const Decl *SCD = SC.getAssociatedDeclaration(); 2765 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2766 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2767 if (SCD != CCD) 2768 return false; 2769 std::advance(CCI, 1); 2770 if (CCI == CCE) 2771 break; 2772 } 2773 return true; 2774 })) { 2775 Visit(E->getBase()); 2776 } 2777 } else { 2778 Visit(E->getBase()); 2779 } 2780 } 2781 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2782 for (OMPClause *C : S->clauses()) { 2783 // Skip analysis of arguments of implicitly defined firstprivate clause 2784 // for task|target directives. 2785 // Skip analysis of arguments of implicitly defined map clause for target 2786 // directives. 2787 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2788 C->isImplicit())) { 2789 for (Stmt *CC : C->children()) { 2790 if (CC) 2791 Visit(CC); 2792 } 2793 } 2794 } 2795 // Check implicitly captured variables. 2796 VisitSubCaptures(S); 2797 } 2798 void VisitStmt(Stmt *S) { 2799 for (Stmt *C : S->children()) { 2800 if (C) { 2801 // Check implicitly captured variables in the task-based directives to 2802 // check if they must be firstprivatized. 2803 Visit(C); 2804 } 2805 } 2806 } 2807 2808 bool isErrorFound() const { return ErrorFound; } 2809 ArrayRef<Expr *> getImplicitFirstprivate() const { 2810 return ImplicitFirstprivate; 2811 } 2812 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2813 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2814 return VarsWithInheritedDSA; 2815 } 2816 2817 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2818 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 2819 // Process declare target link variables for the target directives. 2820 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 2821 for (DeclRefExpr *E : Stack->getLinkGlobals()) 2822 Visit(E); 2823 } 2824 } 2825 }; 2826 } // namespace 2827 2828 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2829 switch (DKind) { 2830 case OMPD_parallel: 2831 case OMPD_parallel_for: 2832 case OMPD_parallel_for_simd: 2833 case OMPD_parallel_sections: 2834 case OMPD_teams: 2835 case OMPD_teams_distribute: 2836 case OMPD_teams_distribute_simd: { 2837 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2838 QualType KmpInt32PtrTy = 2839 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2840 Sema::CapturedParamNameType Params[] = { 2841 std::make_pair(".global_tid.", KmpInt32PtrTy), 2842 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2843 std::make_pair(StringRef(), QualType()) // __context with shared vars 2844 }; 2845 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2846 Params); 2847 break; 2848 } 2849 case OMPD_target_teams: 2850 case OMPD_target_parallel: 2851 case OMPD_target_parallel_for: 2852 case OMPD_target_parallel_for_simd: 2853 case OMPD_target_teams_distribute: 2854 case OMPD_target_teams_distribute_simd: { 2855 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2856 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2857 QualType KmpInt32PtrTy = 2858 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2859 QualType Args[] = {VoidPtrTy}; 2860 FunctionProtoType::ExtProtoInfo EPI; 2861 EPI.Variadic = true; 2862 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2863 Sema::CapturedParamNameType Params[] = { 2864 std::make_pair(".global_tid.", KmpInt32Ty), 2865 std::make_pair(".part_id.", KmpInt32PtrTy), 2866 std::make_pair(".privates.", VoidPtrTy), 2867 std::make_pair( 2868 ".copy_fn.", 2869 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2870 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2871 std::make_pair(StringRef(), QualType()) // __context with shared vars 2872 }; 2873 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2874 Params); 2875 // Mark this captured region as inlined, because we don't use outlined 2876 // function directly. 2877 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2878 AlwaysInlineAttr::CreateImplicit( 2879 Context, AlwaysInlineAttr::Keyword_forceinline)); 2880 Sema::CapturedParamNameType ParamsTarget[] = { 2881 std::make_pair(StringRef(), QualType()) // __context with shared vars 2882 }; 2883 // Start a captured region for 'target' with no implicit parameters. 2884 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2885 ParamsTarget); 2886 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2887 std::make_pair(".global_tid.", KmpInt32PtrTy), 2888 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2889 std::make_pair(StringRef(), QualType()) // __context with shared vars 2890 }; 2891 // Start a captured region for 'teams' or 'parallel'. Both regions have 2892 // the same implicit parameters. 2893 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2894 ParamsTeamsOrParallel); 2895 break; 2896 } 2897 case OMPD_target: 2898 case OMPD_target_simd: { 2899 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2900 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2901 QualType KmpInt32PtrTy = 2902 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2903 QualType Args[] = {VoidPtrTy}; 2904 FunctionProtoType::ExtProtoInfo EPI; 2905 EPI.Variadic = true; 2906 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2907 Sema::CapturedParamNameType Params[] = { 2908 std::make_pair(".global_tid.", KmpInt32Ty), 2909 std::make_pair(".part_id.", KmpInt32PtrTy), 2910 std::make_pair(".privates.", VoidPtrTy), 2911 std::make_pair( 2912 ".copy_fn.", 2913 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2914 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2915 std::make_pair(StringRef(), QualType()) // __context with shared vars 2916 }; 2917 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2918 Params); 2919 // Mark this captured region as inlined, because we don't use outlined 2920 // function directly. 2921 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2922 AlwaysInlineAttr::CreateImplicit( 2923 Context, AlwaysInlineAttr::Keyword_forceinline)); 2924 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2925 std::make_pair(StringRef(), QualType())); 2926 break; 2927 } 2928 case OMPD_simd: 2929 case OMPD_for: 2930 case OMPD_for_simd: 2931 case OMPD_sections: 2932 case OMPD_section: 2933 case OMPD_single: 2934 case OMPD_master: 2935 case OMPD_critical: 2936 case OMPD_taskgroup: 2937 case OMPD_distribute: 2938 case OMPD_distribute_simd: 2939 case OMPD_ordered: 2940 case OMPD_atomic: 2941 case OMPD_target_data: { 2942 Sema::CapturedParamNameType Params[] = { 2943 std::make_pair(StringRef(), QualType()) // __context with shared vars 2944 }; 2945 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2946 Params); 2947 break; 2948 } 2949 case OMPD_task: { 2950 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2951 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2952 QualType KmpInt32PtrTy = 2953 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2954 QualType Args[] = {VoidPtrTy}; 2955 FunctionProtoType::ExtProtoInfo EPI; 2956 EPI.Variadic = true; 2957 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2958 Sema::CapturedParamNameType Params[] = { 2959 std::make_pair(".global_tid.", KmpInt32Ty), 2960 std::make_pair(".part_id.", KmpInt32PtrTy), 2961 std::make_pair(".privates.", VoidPtrTy), 2962 std::make_pair( 2963 ".copy_fn.", 2964 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2965 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2966 std::make_pair(StringRef(), QualType()) // __context with shared vars 2967 }; 2968 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2969 Params); 2970 // Mark this captured region as inlined, because we don't use outlined 2971 // function directly. 2972 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2973 AlwaysInlineAttr::CreateImplicit( 2974 Context, AlwaysInlineAttr::Keyword_forceinline)); 2975 break; 2976 } 2977 case OMPD_taskloop: 2978 case OMPD_taskloop_simd: { 2979 QualType KmpInt32Ty = 2980 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 2981 .withConst(); 2982 QualType KmpUInt64Ty = 2983 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 2984 .withConst(); 2985 QualType KmpInt64Ty = 2986 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 2987 .withConst(); 2988 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2989 QualType KmpInt32PtrTy = 2990 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2991 QualType Args[] = {VoidPtrTy}; 2992 FunctionProtoType::ExtProtoInfo EPI; 2993 EPI.Variadic = true; 2994 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2995 Sema::CapturedParamNameType Params[] = { 2996 std::make_pair(".global_tid.", KmpInt32Ty), 2997 std::make_pair(".part_id.", KmpInt32PtrTy), 2998 std::make_pair(".privates.", VoidPtrTy), 2999 std::make_pair( 3000 ".copy_fn.", 3001 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3002 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3003 std::make_pair(".lb.", KmpUInt64Ty), 3004 std::make_pair(".ub.", KmpUInt64Ty), 3005 std::make_pair(".st.", KmpInt64Ty), 3006 std::make_pair(".liter.", KmpInt32Ty), 3007 std::make_pair(".reductions.", VoidPtrTy), 3008 std::make_pair(StringRef(), QualType()) // __context with shared vars 3009 }; 3010 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3011 Params); 3012 // Mark this captured region as inlined, because we don't use outlined 3013 // function directly. 3014 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3015 AlwaysInlineAttr::CreateImplicit( 3016 Context, AlwaysInlineAttr::Keyword_forceinline)); 3017 break; 3018 } 3019 case OMPD_distribute_parallel_for_simd: 3020 case OMPD_distribute_parallel_for: { 3021 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3022 QualType KmpInt32PtrTy = 3023 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3024 Sema::CapturedParamNameType Params[] = { 3025 std::make_pair(".global_tid.", KmpInt32PtrTy), 3026 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3027 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3028 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3029 std::make_pair(StringRef(), QualType()) // __context with shared vars 3030 }; 3031 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3032 Params); 3033 break; 3034 } 3035 case OMPD_target_teams_distribute_parallel_for: 3036 case OMPD_target_teams_distribute_parallel_for_simd: { 3037 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3038 QualType KmpInt32PtrTy = 3039 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3040 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3041 3042 QualType Args[] = {VoidPtrTy}; 3043 FunctionProtoType::ExtProtoInfo EPI; 3044 EPI.Variadic = true; 3045 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3046 Sema::CapturedParamNameType Params[] = { 3047 std::make_pair(".global_tid.", KmpInt32Ty), 3048 std::make_pair(".part_id.", KmpInt32PtrTy), 3049 std::make_pair(".privates.", VoidPtrTy), 3050 std::make_pair( 3051 ".copy_fn.", 3052 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3053 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3054 std::make_pair(StringRef(), QualType()) // __context with shared vars 3055 }; 3056 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3057 Params); 3058 // Mark this captured region as inlined, because we don't use outlined 3059 // function directly. 3060 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3061 AlwaysInlineAttr::CreateImplicit( 3062 Context, AlwaysInlineAttr::Keyword_forceinline)); 3063 Sema::CapturedParamNameType ParamsTarget[] = { 3064 std::make_pair(StringRef(), QualType()) // __context with shared vars 3065 }; 3066 // Start a captured region for 'target' with no implicit parameters. 3067 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3068 ParamsTarget); 3069 3070 Sema::CapturedParamNameType ParamsTeams[] = { 3071 std::make_pair(".global_tid.", KmpInt32PtrTy), 3072 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3073 std::make_pair(StringRef(), QualType()) // __context with shared vars 3074 }; 3075 // Start a captured region for 'target' with no implicit parameters. 3076 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3077 ParamsTeams); 3078 3079 Sema::CapturedParamNameType ParamsParallel[] = { 3080 std::make_pair(".global_tid.", KmpInt32PtrTy), 3081 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3082 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3083 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3084 std::make_pair(StringRef(), QualType()) // __context with shared vars 3085 }; 3086 // Start a captured region for 'teams' or 'parallel'. Both regions have 3087 // the same implicit parameters. 3088 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3089 ParamsParallel); 3090 break; 3091 } 3092 3093 case OMPD_teams_distribute_parallel_for: 3094 case OMPD_teams_distribute_parallel_for_simd: { 3095 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3096 QualType KmpInt32PtrTy = 3097 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3098 3099 Sema::CapturedParamNameType ParamsTeams[] = { 3100 std::make_pair(".global_tid.", KmpInt32PtrTy), 3101 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3102 std::make_pair(StringRef(), QualType()) // __context with shared vars 3103 }; 3104 // Start a captured region for 'target' with no implicit parameters. 3105 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3106 ParamsTeams); 3107 3108 Sema::CapturedParamNameType ParamsParallel[] = { 3109 std::make_pair(".global_tid.", KmpInt32PtrTy), 3110 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3111 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3112 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3113 std::make_pair(StringRef(), QualType()) // __context with shared vars 3114 }; 3115 // Start a captured region for 'teams' or 'parallel'. Both regions have 3116 // the same implicit parameters. 3117 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3118 ParamsParallel); 3119 break; 3120 } 3121 case OMPD_target_update: 3122 case OMPD_target_enter_data: 3123 case OMPD_target_exit_data: { 3124 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3125 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3126 QualType KmpInt32PtrTy = 3127 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3128 QualType Args[] = {VoidPtrTy}; 3129 FunctionProtoType::ExtProtoInfo EPI; 3130 EPI.Variadic = true; 3131 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3132 Sema::CapturedParamNameType Params[] = { 3133 std::make_pair(".global_tid.", KmpInt32Ty), 3134 std::make_pair(".part_id.", KmpInt32PtrTy), 3135 std::make_pair(".privates.", VoidPtrTy), 3136 std::make_pair( 3137 ".copy_fn.", 3138 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3139 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3140 std::make_pair(StringRef(), QualType()) // __context with shared vars 3141 }; 3142 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3143 Params); 3144 // Mark this captured region as inlined, because we don't use outlined 3145 // function directly. 3146 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3147 AlwaysInlineAttr::CreateImplicit( 3148 Context, AlwaysInlineAttr::Keyword_forceinline)); 3149 break; 3150 } 3151 case OMPD_threadprivate: 3152 case OMPD_allocate: 3153 case OMPD_taskyield: 3154 case OMPD_barrier: 3155 case OMPD_taskwait: 3156 case OMPD_cancellation_point: 3157 case OMPD_cancel: 3158 case OMPD_flush: 3159 case OMPD_declare_reduction: 3160 case OMPD_declare_mapper: 3161 case OMPD_declare_simd: 3162 case OMPD_declare_target: 3163 case OMPD_end_declare_target: 3164 case OMPD_requires: 3165 llvm_unreachable("OpenMP Directive is not allowed"); 3166 case OMPD_unknown: 3167 llvm_unreachable("Unknown OpenMP directive"); 3168 } 3169 } 3170 3171 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 3172 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 3173 getOpenMPCaptureRegions(CaptureRegions, DKind); 3174 return CaptureRegions.size(); 3175 } 3176 3177 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 3178 Expr *CaptureExpr, bool WithInit, 3179 bool AsExpression) { 3180 assert(CaptureExpr); 3181 ASTContext &C = S.getASTContext(); 3182 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 3183 QualType Ty = Init->getType(); 3184 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 3185 if (S.getLangOpts().CPlusPlus) { 3186 Ty = C.getLValueReferenceType(Ty); 3187 } else { 3188 Ty = C.getPointerType(Ty); 3189 ExprResult Res = 3190 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 3191 if (!Res.isUsable()) 3192 return nullptr; 3193 Init = Res.get(); 3194 } 3195 WithInit = true; 3196 } 3197 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 3198 CaptureExpr->getBeginLoc()); 3199 if (!WithInit) 3200 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 3201 S.CurContext->addHiddenDecl(CED); 3202 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 3203 return CED; 3204 } 3205 3206 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 3207 bool WithInit) { 3208 OMPCapturedExprDecl *CD; 3209 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 3210 CD = cast<OMPCapturedExprDecl>(VD); 3211 else 3212 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 3213 /*AsExpression=*/false); 3214 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3215 CaptureExpr->getExprLoc()); 3216 } 3217 3218 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 3219 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 3220 if (!Ref) { 3221 OMPCapturedExprDecl *CD = buildCaptureDecl( 3222 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 3223 /*WithInit=*/true, /*AsExpression=*/true); 3224 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3225 CaptureExpr->getExprLoc()); 3226 } 3227 ExprResult Res = Ref; 3228 if (!S.getLangOpts().CPlusPlus && 3229 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 3230 Ref->getType()->isPointerType()) { 3231 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 3232 if (!Res.isUsable()) 3233 return ExprError(); 3234 } 3235 return S.DefaultLvalueConversion(Res.get()); 3236 } 3237 3238 namespace { 3239 // OpenMP directives parsed in this section are represented as a 3240 // CapturedStatement with an associated statement. If a syntax error 3241 // is detected during the parsing of the associated statement, the 3242 // compiler must abort processing and close the CapturedStatement. 3243 // 3244 // Combined directives such as 'target parallel' have more than one 3245 // nested CapturedStatements. This RAII ensures that we unwind out 3246 // of all the nested CapturedStatements when an error is found. 3247 class CaptureRegionUnwinderRAII { 3248 private: 3249 Sema &S; 3250 bool &ErrorFound; 3251 OpenMPDirectiveKind DKind = OMPD_unknown; 3252 3253 public: 3254 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 3255 OpenMPDirectiveKind DKind) 3256 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 3257 ~CaptureRegionUnwinderRAII() { 3258 if (ErrorFound) { 3259 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 3260 while (--ThisCaptureLevel >= 0) 3261 S.ActOnCapturedRegionError(); 3262 } 3263 } 3264 }; 3265 } // namespace 3266 3267 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 3268 ArrayRef<OMPClause *> Clauses) { 3269 bool ErrorFound = false; 3270 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 3271 *this, ErrorFound, DSAStack->getCurrentDirective()); 3272 if (!S.isUsable()) { 3273 ErrorFound = true; 3274 return StmtError(); 3275 } 3276 3277 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 3278 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 3279 OMPOrderedClause *OC = nullptr; 3280 OMPScheduleClause *SC = nullptr; 3281 SmallVector<const OMPLinearClause *, 4> LCs; 3282 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 3283 // This is required for proper codegen. 3284 for (OMPClause *Clause : Clauses) { 3285 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 3286 Clause->getClauseKind() == OMPC_in_reduction) { 3287 // Capture taskgroup task_reduction descriptors inside the tasking regions 3288 // with the corresponding in_reduction items. 3289 auto *IRC = cast<OMPInReductionClause>(Clause); 3290 for (Expr *E : IRC->taskgroup_descriptors()) 3291 if (E) 3292 MarkDeclarationsReferencedInExpr(E); 3293 } 3294 if (isOpenMPPrivate(Clause->getClauseKind()) || 3295 Clause->getClauseKind() == OMPC_copyprivate || 3296 (getLangOpts().OpenMPUseTLS && 3297 getASTContext().getTargetInfo().isTLSSupported() && 3298 Clause->getClauseKind() == OMPC_copyin)) { 3299 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 3300 // Mark all variables in private list clauses as used in inner region. 3301 for (Stmt *VarRef : Clause->children()) { 3302 if (auto *E = cast_or_null<Expr>(VarRef)) { 3303 MarkDeclarationsReferencedInExpr(E); 3304 } 3305 } 3306 DSAStack->setForceVarCapturing(/*V=*/false); 3307 } else if (CaptureRegions.size() > 1 || 3308 CaptureRegions.back() != OMPD_unknown) { 3309 if (auto *C = OMPClauseWithPreInit::get(Clause)) 3310 PICs.push_back(C); 3311 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 3312 if (Expr *E = C->getPostUpdateExpr()) 3313 MarkDeclarationsReferencedInExpr(E); 3314 } 3315 } 3316 if (Clause->getClauseKind() == OMPC_schedule) 3317 SC = cast<OMPScheduleClause>(Clause); 3318 else if (Clause->getClauseKind() == OMPC_ordered) 3319 OC = cast<OMPOrderedClause>(Clause); 3320 else if (Clause->getClauseKind() == OMPC_linear) 3321 LCs.push_back(cast<OMPLinearClause>(Clause)); 3322 } 3323 // OpenMP, 2.7.1 Loop Construct, Restrictions 3324 // The nonmonotonic modifier cannot be specified if an ordered clause is 3325 // specified. 3326 if (SC && 3327 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 3328 SC->getSecondScheduleModifier() == 3329 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 3330 OC) { 3331 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 3332 ? SC->getFirstScheduleModifierLoc() 3333 : SC->getSecondScheduleModifierLoc(), 3334 diag::err_omp_schedule_nonmonotonic_ordered) 3335 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3336 ErrorFound = true; 3337 } 3338 if (!LCs.empty() && OC && OC->getNumForLoops()) { 3339 for (const OMPLinearClause *C : LCs) { 3340 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 3341 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3342 } 3343 ErrorFound = true; 3344 } 3345 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 3346 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 3347 OC->getNumForLoops()) { 3348 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 3349 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 3350 ErrorFound = true; 3351 } 3352 if (ErrorFound) { 3353 return StmtError(); 3354 } 3355 StmtResult SR = S; 3356 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 3357 // Mark all variables in private list clauses as used in inner region. 3358 // Required for proper codegen of combined directives. 3359 // TODO: add processing for other clauses. 3360 if (ThisCaptureRegion != OMPD_unknown) { 3361 for (const clang::OMPClauseWithPreInit *C : PICs) { 3362 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 3363 // Find the particular capture region for the clause if the 3364 // directive is a combined one with multiple capture regions. 3365 // If the directive is not a combined one, the capture region 3366 // associated with the clause is OMPD_unknown and is generated 3367 // only once. 3368 if (CaptureRegion == ThisCaptureRegion || 3369 CaptureRegion == OMPD_unknown) { 3370 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 3371 for (Decl *D : DS->decls()) 3372 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 3373 } 3374 } 3375 } 3376 } 3377 SR = ActOnCapturedRegionEnd(SR.get()); 3378 } 3379 return SR; 3380 } 3381 3382 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 3383 OpenMPDirectiveKind CancelRegion, 3384 SourceLocation StartLoc) { 3385 // CancelRegion is only needed for cancel and cancellation_point. 3386 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 3387 return false; 3388 3389 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 3390 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 3391 return false; 3392 3393 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 3394 << getOpenMPDirectiveName(CancelRegion); 3395 return true; 3396 } 3397 3398 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 3399 OpenMPDirectiveKind CurrentRegion, 3400 const DeclarationNameInfo &CurrentName, 3401 OpenMPDirectiveKind CancelRegion, 3402 SourceLocation StartLoc) { 3403 if (Stack->getCurScope()) { 3404 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 3405 OpenMPDirectiveKind OffendingRegion = ParentRegion; 3406 bool NestingProhibited = false; 3407 bool CloseNesting = true; 3408 bool OrphanSeen = false; 3409 enum { 3410 NoRecommend, 3411 ShouldBeInParallelRegion, 3412 ShouldBeInOrderedRegion, 3413 ShouldBeInTargetRegion, 3414 ShouldBeInTeamsRegion 3415 } Recommend = NoRecommend; 3416 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 3417 // OpenMP [2.16, Nesting of Regions] 3418 // OpenMP constructs may not be nested inside a simd region. 3419 // OpenMP [2.8.1,simd Construct, Restrictions] 3420 // An ordered construct with the simd clause is the only OpenMP 3421 // construct that can appear in the simd region. 3422 // Allowing a SIMD construct nested in another SIMD construct is an 3423 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 3424 // message. 3425 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 3426 ? diag::err_omp_prohibited_region_simd 3427 : diag::warn_omp_nesting_simd); 3428 return CurrentRegion != OMPD_simd; 3429 } 3430 if (ParentRegion == OMPD_atomic) { 3431 // OpenMP [2.16, Nesting of Regions] 3432 // OpenMP constructs may not be nested inside an atomic region. 3433 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 3434 return true; 3435 } 3436 if (CurrentRegion == OMPD_section) { 3437 // OpenMP [2.7.2, sections Construct, Restrictions] 3438 // Orphaned section directives are prohibited. That is, the section 3439 // directives must appear within the sections construct and must not be 3440 // encountered elsewhere in the sections region. 3441 if (ParentRegion != OMPD_sections && 3442 ParentRegion != OMPD_parallel_sections) { 3443 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 3444 << (ParentRegion != OMPD_unknown) 3445 << getOpenMPDirectiveName(ParentRegion); 3446 return true; 3447 } 3448 return false; 3449 } 3450 // Allow some constructs (except teams and cancellation constructs) to be 3451 // orphaned (they could be used in functions, called from OpenMP regions 3452 // with the required preconditions). 3453 if (ParentRegion == OMPD_unknown && 3454 !isOpenMPNestingTeamsDirective(CurrentRegion) && 3455 CurrentRegion != OMPD_cancellation_point && 3456 CurrentRegion != OMPD_cancel) 3457 return false; 3458 if (CurrentRegion == OMPD_cancellation_point || 3459 CurrentRegion == OMPD_cancel) { 3460 // OpenMP [2.16, Nesting of Regions] 3461 // A cancellation point construct for which construct-type-clause is 3462 // taskgroup must be nested inside a task construct. A cancellation 3463 // point construct for which construct-type-clause is not taskgroup must 3464 // be closely nested inside an OpenMP construct that matches the type 3465 // specified in construct-type-clause. 3466 // A cancel construct for which construct-type-clause is taskgroup must be 3467 // nested inside a task construct. A cancel construct for which 3468 // construct-type-clause is not taskgroup must be closely nested inside an 3469 // OpenMP construct that matches the type specified in 3470 // construct-type-clause. 3471 NestingProhibited = 3472 !((CancelRegion == OMPD_parallel && 3473 (ParentRegion == OMPD_parallel || 3474 ParentRegion == OMPD_target_parallel)) || 3475 (CancelRegion == OMPD_for && 3476 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 3477 ParentRegion == OMPD_target_parallel_for || 3478 ParentRegion == OMPD_distribute_parallel_for || 3479 ParentRegion == OMPD_teams_distribute_parallel_for || 3480 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 3481 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 3482 (CancelRegion == OMPD_sections && 3483 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 3484 ParentRegion == OMPD_parallel_sections))); 3485 OrphanSeen = ParentRegion == OMPD_unknown; 3486 } else if (CurrentRegion == OMPD_master) { 3487 // OpenMP [2.16, Nesting of Regions] 3488 // A master region may not be closely nested inside a worksharing, 3489 // atomic, or explicit task region. 3490 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3491 isOpenMPTaskingDirective(ParentRegion); 3492 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 3493 // OpenMP [2.16, Nesting of Regions] 3494 // A critical region may not be nested (closely or otherwise) inside a 3495 // critical region with the same name. Note that this restriction is not 3496 // sufficient to prevent deadlock. 3497 SourceLocation PreviousCriticalLoc; 3498 bool DeadLock = Stack->hasDirective( 3499 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 3500 const DeclarationNameInfo &DNI, 3501 SourceLocation Loc) { 3502 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 3503 PreviousCriticalLoc = Loc; 3504 return true; 3505 } 3506 return false; 3507 }, 3508 false /* skip top directive */); 3509 if (DeadLock) { 3510 SemaRef.Diag(StartLoc, 3511 diag::err_omp_prohibited_region_critical_same_name) 3512 << CurrentName.getName(); 3513 if (PreviousCriticalLoc.isValid()) 3514 SemaRef.Diag(PreviousCriticalLoc, 3515 diag::note_omp_previous_critical_region); 3516 return true; 3517 } 3518 } else if (CurrentRegion == OMPD_barrier) { 3519 // OpenMP [2.16, Nesting of Regions] 3520 // A barrier region may not be closely nested inside a worksharing, 3521 // explicit task, critical, ordered, atomic, or master region. 3522 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3523 isOpenMPTaskingDirective(ParentRegion) || 3524 ParentRegion == OMPD_master || 3525 ParentRegion == OMPD_critical || 3526 ParentRegion == OMPD_ordered; 3527 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 3528 !isOpenMPParallelDirective(CurrentRegion) && 3529 !isOpenMPTeamsDirective(CurrentRegion)) { 3530 // OpenMP [2.16, Nesting of Regions] 3531 // A worksharing region may not be closely nested inside a worksharing, 3532 // explicit task, critical, ordered, atomic, or master region. 3533 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3534 isOpenMPTaskingDirective(ParentRegion) || 3535 ParentRegion == OMPD_master || 3536 ParentRegion == OMPD_critical || 3537 ParentRegion == OMPD_ordered; 3538 Recommend = ShouldBeInParallelRegion; 3539 } else if (CurrentRegion == OMPD_ordered) { 3540 // OpenMP [2.16, Nesting of Regions] 3541 // An ordered region may not be closely nested inside a critical, 3542 // atomic, or explicit task region. 3543 // An ordered region must be closely nested inside a loop region (or 3544 // parallel loop region) with an ordered clause. 3545 // OpenMP [2.8.1,simd Construct, Restrictions] 3546 // An ordered construct with the simd clause is the only OpenMP construct 3547 // that can appear in the simd region. 3548 NestingProhibited = ParentRegion == OMPD_critical || 3549 isOpenMPTaskingDirective(ParentRegion) || 3550 !(isOpenMPSimdDirective(ParentRegion) || 3551 Stack->isParentOrderedRegion()); 3552 Recommend = ShouldBeInOrderedRegion; 3553 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 3554 // OpenMP [2.16, Nesting of Regions] 3555 // If specified, a teams construct must be contained within a target 3556 // construct. 3557 NestingProhibited = ParentRegion != OMPD_target; 3558 OrphanSeen = ParentRegion == OMPD_unknown; 3559 Recommend = ShouldBeInTargetRegion; 3560 } 3561 if (!NestingProhibited && 3562 !isOpenMPTargetExecutionDirective(CurrentRegion) && 3563 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 3564 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 3565 // OpenMP [2.16, Nesting of Regions] 3566 // distribute, parallel, parallel sections, parallel workshare, and the 3567 // parallel loop and parallel loop SIMD constructs are the only OpenMP 3568 // constructs that can be closely nested in the teams region. 3569 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 3570 !isOpenMPDistributeDirective(CurrentRegion); 3571 Recommend = ShouldBeInParallelRegion; 3572 } 3573 if (!NestingProhibited && 3574 isOpenMPNestingDistributeDirective(CurrentRegion)) { 3575 // OpenMP 4.5 [2.17 Nesting of Regions] 3576 // The region associated with the distribute construct must be strictly 3577 // nested inside a teams region 3578 NestingProhibited = 3579 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 3580 Recommend = ShouldBeInTeamsRegion; 3581 } 3582 if (!NestingProhibited && 3583 (isOpenMPTargetExecutionDirective(CurrentRegion) || 3584 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 3585 // OpenMP 4.5 [2.17 Nesting of Regions] 3586 // If a target, target update, target data, target enter data, or 3587 // target exit data construct is encountered during execution of a 3588 // target region, the behavior is unspecified. 3589 NestingProhibited = Stack->hasDirective( 3590 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 3591 SourceLocation) { 3592 if (isOpenMPTargetExecutionDirective(K)) { 3593 OffendingRegion = K; 3594 return true; 3595 } 3596 return false; 3597 }, 3598 false /* don't skip top directive */); 3599 CloseNesting = false; 3600 } 3601 if (NestingProhibited) { 3602 if (OrphanSeen) { 3603 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 3604 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 3605 } else { 3606 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 3607 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 3608 << Recommend << getOpenMPDirectiveName(CurrentRegion); 3609 } 3610 return true; 3611 } 3612 } 3613 return false; 3614 } 3615 3616 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 3617 ArrayRef<OMPClause *> Clauses, 3618 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 3619 bool ErrorFound = false; 3620 unsigned NamedModifiersNumber = 0; 3621 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 3622 OMPD_unknown + 1); 3623 SmallVector<SourceLocation, 4> NameModifierLoc; 3624 for (const OMPClause *C : Clauses) { 3625 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 3626 // At most one if clause without a directive-name-modifier can appear on 3627 // the directive. 3628 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 3629 if (FoundNameModifiers[CurNM]) { 3630 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 3631 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 3632 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 3633 ErrorFound = true; 3634 } else if (CurNM != OMPD_unknown) { 3635 NameModifierLoc.push_back(IC->getNameModifierLoc()); 3636 ++NamedModifiersNumber; 3637 } 3638 FoundNameModifiers[CurNM] = IC; 3639 if (CurNM == OMPD_unknown) 3640 continue; 3641 // Check if the specified name modifier is allowed for the current 3642 // directive. 3643 // At most one if clause with the particular directive-name-modifier can 3644 // appear on the directive. 3645 bool MatchFound = false; 3646 for (auto NM : AllowedNameModifiers) { 3647 if (CurNM == NM) { 3648 MatchFound = true; 3649 break; 3650 } 3651 } 3652 if (!MatchFound) { 3653 S.Diag(IC->getNameModifierLoc(), 3654 diag::err_omp_wrong_if_directive_name_modifier) 3655 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3656 ErrorFound = true; 3657 } 3658 } 3659 } 3660 // If any if clause on the directive includes a directive-name-modifier then 3661 // all if clauses on the directive must include a directive-name-modifier. 3662 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3663 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3664 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 3665 diag::err_omp_no_more_if_clause); 3666 } else { 3667 std::string Values; 3668 std::string Sep(", "); 3669 unsigned AllowedCnt = 0; 3670 unsigned TotalAllowedNum = 3671 AllowedNameModifiers.size() - NamedModifiersNumber; 3672 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3673 ++Cnt) { 3674 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3675 if (!FoundNameModifiers[NM]) { 3676 Values += "'"; 3677 Values += getOpenMPDirectiveName(NM); 3678 Values += "'"; 3679 if (AllowedCnt + 2 == TotalAllowedNum) 3680 Values += " or "; 3681 else if (AllowedCnt + 1 != TotalAllowedNum) 3682 Values += Sep; 3683 ++AllowedCnt; 3684 } 3685 } 3686 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 3687 diag::err_omp_unnamed_if_clause) 3688 << (TotalAllowedNum > 1) << Values; 3689 } 3690 for (SourceLocation Loc : NameModifierLoc) { 3691 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3692 } 3693 ErrorFound = true; 3694 } 3695 return ErrorFound; 3696 } 3697 3698 static std::pair<ValueDecl *, bool> 3699 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 3700 SourceRange &ERange, bool AllowArraySection = false) { 3701 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 3702 RefExpr->containsUnexpandedParameterPack()) 3703 return std::make_pair(nullptr, true); 3704 3705 // OpenMP [3.1, C/C++] 3706 // A list item is a variable name. 3707 // OpenMP [2.9.3.3, Restrictions, p.1] 3708 // A variable that is part of another variable (as an array or 3709 // structure element) cannot appear in a private clause. 3710 RefExpr = RefExpr->IgnoreParens(); 3711 enum { 3712 NoArrayExpr = -1, 3713 ArraySubscript = 0, 3714 OMPArraySection = 1 3715 } IsArrayExpr = NoArrayExpr; 3716 if (AllowArraySection) { 3717 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 3718 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 3719 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 3720 Base = TempASE->getBase()->IgnoreParenImpCasts(); 3721 RefExpr = Base; 3722 IsArrayExpr = ArraySubscript; 3723 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 3724 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 3725 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 3726 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 3727 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 3728 Base = TempASE->getBase()->IgnoreParenImpCasts(); 3729 RefExpr = Base; 3730 IsArrayExpr = OMPArraySection; 3731 } 3732 } 3733 ELoc = RefExpr->getExprLoc(); 3734 ERange = RefExpr->getSourceRange(); 3735 RefExpr = RefExpr->IgnoreParenImpCasts(); 3736 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 3737 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 3738 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 3739 (S.getCurrentThisType().isNull() || !ME || 3740 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 3741 !isa<FieldDecl>(ME->getMemberDecl()))) { 3742 if (IsArrayExpr != NoArrayExpr) { 3743 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 3744 << ERange; 3745 } else { 3746 S.Diag(ELoc, 3747 AllowArraySection 3748 ? diag::err_omp_expected_var_name_member_expr_or_array_item 3749 : diag::err_omp_expected_var_name_member_expr) 3750 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 3751 } 3752 return std::make_pair(nullptr, false); 3753 } 3754 return std::make_pair( 3755 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 3756 } 3757 3758 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 3759 ArrayRef<OMPClause *> Clauses) { 3760 assert(!S.CurContext->isDependentContext() && 3761 "Expected non-dependent context."); 3762 auto AllocateRange = 3763 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 3764 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 3765 DeclToCopy; 3766 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 3767 return isOpenMPPrivate(C->getClauseKind()); 3768 }); 3769 for (OMPClause *Cl : PrivateRange) { 3770 MutableArrayRef<Expr *>::iterator I, It, Et; 3771 if (Cl->getClauseKind() == OMPC_private) { 3772 auto *PC = cast<OMPPrivateClause>(Cl); 3773 I = PC->private_copies().begin(); 3774 It = PC->varlist_begin(); 3775 Et = PC->varlist_end(); 3776 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 3777 auto *PC = cast<OMPFirstprivateClause>(Cl); 3778 I = PC->private_copies().begin(); 3779 It = PC->varlist_begin(); 3780 Et = PC->varlist_end(); 3781 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 3782 auto *PC = cast<OMPLastprivateClause>(Cl); 3783 I = PC->private_copies().begin(); 3784 It = PC->varlist_begin(); 3785 Et = PC->varlist_end(); 3786 } else if (Cl->getClauseKind() == OMPC_linear) { 3787 auto *PC = cast<OMPLinearClause>(Cl); 3788 I = PC->privates().begin(); 3789 It = PC->varlist_begin(); 3790 Et = PC->varlist_end(); 3791 } else if (Cl->getClauseKind() == OMPC_reduction) { 3792 auto *PC = cast<OMPReductionClause>(Cl); 3793 I = PC->privates().begin(); 3794 It = PC->varlist_begin(); 3795 Et = PC->varlist_end(); 3796 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 3797 auto *PC = cast<OMPTaskReductionClause>(Cl); 3798 I = PC->privates().begin(); 3799 It = PC->varlist_begin(); 3800 Et = PC->varlist_end(); 3801 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 3802 auto *PC = cast<OMPInReductionClause>(Cl); 3803 I = PC->privates().begin(); 3804 It = PC->varlist_begin(); 3805 Et = PC->varlist_end(); 3806 } else { 3807 llvm_unreachable("Expected private clause."); 3808 } 3809 for (Expr *E : llvm::make_range(It, Et)) { 3810 if (!*I) { 3811 ++I; 3812 continue; 3813 } 3814 SourceLocation ELoc; 3815 SourceRange ERange; 3816 Expr *SimpleRefExpr = E; 3817 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 3818 /*AllowArraySection=*/true); 3819 DeclToCopy.try_emplace(Res.first, 3820 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 3821 ++I; 3822 } 3823 } 3824 for (OMPClause *C : AllocateRange) { 3825 auto *AC = cast<OMPAllocateClause>(C); 3826 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3827 getAllocatorKind(S, Stack, AC->getAllocator()); 3828 // OpenMP, 2.11.4 allocate Clause, Restrictions. 3829 // For task, taskloop or target directives, allocation requests to memory 3830 // allocators with the trait access set to thread result in unspecified 3831 // behavior. 3832 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 3833 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 3834 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 3835 S.Diag(AC->getAllocator()->getExprLoc(), 3836 diag::warn_omp_allocate_thread_on_task_target_directive) 3837 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3838 } 3839 for (Expr *E : AC->varlists()) { 3840 SourceLocation ELoc; 3841 SourceRange ERange; 3842 Expr *SimpleRefExpr = E; 3843 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 3844 ValueDecl *VD = Res.first; 3845 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 3846 if (!isOpenMPPrivate(Data.CKind)) { 3847 S.Diag(E->getExprLoc(), 3848 diag::err_omp_expected_private_copy_for_allocate); 3849 continue; 3850 } 3851 VarDecl *PrivateVD = DeclToCopy[VD]; 3852 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 3853 AllocatorKind, AC->getAllocator())) 3854 continue; 3855 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 3856 E->getSourceRange()); 3857 } 3858 } 3859 } 3860 3861 StmtResult Sema::ActOnOpenMPExecutableDirective( 3862 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3863 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3864 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3865 StmtResult Res = StmtError(); 3866 // First check CancelRegion which is then used in checkNestingOfRegions. 3867 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3868 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3869 StartLoc)) 3870 return StmtError(); 3871 3872 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3873 VarsWithInheritedDSAType VarsWithInheritedDSA; 3874 bool ErrorFound = false; 3875 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3876 if (AStmt && !CurContext->isDependentContext()) { 3877 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3878 3879 // Check default data sharing attributes for referenced variables. 3880 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3881 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3882 Stmt *S = AStmt; 3883 while (--ThisCaptureLevel >= 0) 3884 S = cast<CapturedStmt>(S)->getCapturedStmt(); 3885 DSAChecker.Visit(S); 3886 if (DSAChecker.isErrorFound()) 3887 return StmtError(); 3888 // Generate list of implicitly defined firstprivate variables. 3889 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3890 3891 SmallVector<Expr *, 4> ImplicitFirstprivates( 3892 DSAChecker.getImplicitFirstprivate().begin(), 3893 DSAChecker.getImplicitFirstprivate().end()); 3894 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 3895 DSAChecker.getImplicitMap().end()); 3896 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 3897 for (OMPClause *C : Clauses) { 3898 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 3899 for (Expr *E : IRC->taskgroup_descriptors()) 3900 if (E) 3901 ImplicitFirstprivates.emplace_back(E); 3902 } 3903 } 3904 if (!ImplicitFirstprivates.empty()) { 3905 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3906 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 3907 SourceLocation())) { 3908 ClausesWithImplicit.push_back(Implicit); 3909 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3910 ImplicitFirstprivates.size(); 3911 } else { 3912 ErrorFound = true; 3913 } 3914 } 3915 if (!ImplicitMaps.empty()) { 3916 CXXScopeSpec MapperIdScopeSpec; 3917 DeclarationNameInfo MapperId; 3918 if (OMPClause *Implicit = ActOnOpenMPMapClause( 3919 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, 3920 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(), 3921 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) { 3922 ClausesWithImplicit.emplace_back(Implicit); 3923 ErrorFound |= 3924 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 3925 } else { 3926 ErrorFound = true; 3927 } 3928 } 3929 } 3930 3931 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3932 switch (Kind) { 3933 case OMPD_parallel: 3934 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3935 EndLoc); 3936 AllowedNameModifiers.push_back(OMPD_parallel); 3937 break; 3938 case OMPD_simd: 3939 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3940 VarsWithInheritedDSA); 3941 break; 3942 case OMPD_for: 3943 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3944 VarsWithInheritedDSA); 3945 break; 3946 case OMPD_for_simd: 3947 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3948 EndLoc, VarsWithInheritedDSA); 3949 break; 3950 case OMPD_sections: 3951 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3952 EndLoc); 3953 break; 3954 case OMPD_section: 3955 assert(ClausesWithImplicit.empty() && 3956 "No clauses are allowed for 'omp section' directive"); 3957 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3958 break; 3959 case OMPD_single: 3960 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3961 EndLoc); 3962 break; 3963 case OMPD_master: 3964 assert(ClausesWithImplicit.empty() && 3965 "No clauses are allowed for 'omp master' directive"); 3966 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3967 break; 3968 case OMPD_critical: 3969 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3970 StartLoc, EndLoc); 3971 break; 3972 case OMPD_parallel_for: 3973 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3974 EndLoc, VarsWithInheritedDSA); 3975 AllowedNameModifiers.push_back(OMPD_parallel); 3976 break; 3977 case OMPD_parallel_for_simd: 3978 Res = ActOnOpenMPParallelForSimdDirective( 3979 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3980 AllowedNameModifiers.push_back(OMPD_parallel); 3981 break; 3982 case OMPD_parallel_sections: 3983 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3984 StartLoc, EndLoc); 3985 AllowedNameModifiers.push_back(OMPD_parallel); 3986 break; 3987 case OMPD_task: 3988 Res = 3989 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3990 AllowedNameModifiers.push_back(OMPD_task); 3991 break; 3992 case OMPD_taskyield: 3993 assert(ClausesWithImplicit.empty() && 3994 "No clauses are allowed for 'omp taskyield' directive"); 3995 assert(AStmt == nullptr && 3996 "No associated statement allowed for 'omp taskyield' directive"); 3997 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3998 break; 3999 case OMPD_barrier: 4000 assert(ClausesWithImplicit.empty() && 4001 "No clauses are allowed for 'omp barrier' directive"); 4002 assert(AStmt == nullptr && 4003 "No associated statement allowed for 'omp barrier' directive"); 4004 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 4005 break; 4006 case OMPD_taskwait: 4007 assert(ClausesWithImplicit.empty() && 4008 "No clauses are allowed for 'omp taskwait' directive"); 4009 assert(AStmt == nullptr && 4010 "No associated statement allowed for 'omp taskwait' directive"); 4011 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 4012 break; 4013 case OMPD_taskgroup: 4014 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 4015 EndLoc); 4016 break; 4017 case OMPD_flush: 4018 assert(AStmt == nullptr && 4019 "No associated statement allowed for 'omp flush' directive"); 4020 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 4021 break; 4022 case OMPD_ordered: 4023 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 4024 EndLoc); 4025 break; 4026 case OMPD_atomic: 4027 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 4028 EndLoc); 4029 break; 4030 case OMPD_teams: 4031 Res = 4032 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 4033 break; 4034 case OMPD_target: 4035 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 4036 EndLoc); 4037 AllowedNameModifiers.push_back(OMPD_target); 4038 break; 4039 case OMPD_target_parallel: 4040 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 4041 StartLoc, EndLoc); 4042 AllowedNameModifiers.push_back(OMPD_target); 4043 AllowedNameModifiers.push_back(OMPD_parallel); 4044 break; 4045 case OMPD_target_parallel_for: 4046 Res = ActOnOpenMPTargetParallelForDirective( 4047 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4048 AllowedNameModifiers.push_back(OMPD_target); 4049 AllowedNameModifiers.push_back(OMPD_parallel); 4050 break; 4051 case OMPD_cancellation_point: 4052 assert(ClausesWithImplicit.empty() && 4053 "No clauses are allowed for 'omp cancellation point' directive"); 4054 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 4055 "cancellation point' directive"); 4056 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 4057 break; 4058 case OMPD_cancel: 4059 assert(AStmt == nullptr && 4060 "No associated statement allowed for 'omp cancel' directive"); 4061 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 4062 CancelRegion); 4063 AllowedNameModifiers.push_back(OMPD_cancel); 4064 break; 4065 case OMPD_target_data: 4066 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 4067 EndLoc); 4068 AllowedNameModifiers.push_back(OMPD_target_data); 4069 break; 4070 case OMPD_target_enter_data: 4071 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 4072 EndLoc, AStmt); 4073 AllowedNameModifiers.push_back(OMPD_target_enter_data); 4074 break; 4075 case OMPD_target_exit_data: 4076 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 4077 EndLoc, AStmt); 4078 AllowedNameModifiers.push_back(OMPD_target_exit_data); 4079 break; 4080 case OMPD_taskloop: 4081 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 4082 EndLoc, VarsWithInheritedDSA); 4083 AllowedNameModifiers.push_back(OMPD_taskloop); 4084 break; 4085 case OMPD_taskloop_simd: 4086 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4087 EndLoc, VarsWithInheritedDSA); 4088 AllowedNameModifiers.push_back(OMPD_taskloop); 4089 break; 4090 case OMPD_distribute: 4091 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 4092 EndLoc, VarsWithInheritedDSA); 4093 break; 4094 case OMPD_target_update: 4095 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 4096 EndLoc, AStmt); 4097 AllowedNameModifiers.push_back(OMPD_target_update); 4098 break; 4099 case OMPD_distribute_parallel_for: 4100 Res = ActOnOpenMPDistributeParallelForDirective( 4101 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4102 AllowedNameModifiers.push_back(OMPD_parallel); 4103 break; 4104 case OMPD_distribute_parallel_for_simd: 4105 Res = ActOnOpenMPDistributeParallelForSimdDirective( 4106 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4107 AllowedNameModifiers.push_back(OMPD_parallel); 4108 break; 4109 case OMPD_distribute_simd: 4110 Res = ActOnOpenMPDistributeSimdDirective( 4111 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4112 break; 4113 case OMPD_target_parallel_for_simd: 4114 Res = ActOnOpenMPTargetParallelForSimdDirective( 4115 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4116 AllowedNameModifiers.push_back(OMPD_target); 4117 AllowedNameModifiers.push_back(OMPD_parallel); 4118 break; 4119 case OMPD_target_simd: 4120 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4121 EndLoc, VarsWithInheritedDSA); 4122 AllowedNameModifiers.push_back(OMPD_target); 4123 break; 4124 case OMPD_teams_distribute: 4125 Res = ActOnOpenMPTeamsDistributeDirective( 4126 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4127 break; 4128 case OMPD_teams_distribute_simd: 4129 Res = ActOnOpenMPTeamsDistributeSimdDirective( 4130 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4131 break; 4132 case OMPD_teams_distribute_parallel_for_simd: 4133 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 4134 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4135 AllowedNameModifiers.push_back(OMPD_parallel); 4136 break; 4137 case OMPD_teams_distribute_parallel_for: 4138 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 4139 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4140 AllowedNameModifiers.push_back(OMPD_parallel); 4141 break; 4142 case OMPD_target_teams: 4143 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 4144 EndLoc); 4145 AllowedNameModifiers.push_back(OMPD_target); 4146 break; 4147 case OMPD_target_teams_distribute: 4148 Res = ActOnOpenMPTargetTeamsDistributeDirective( 4149 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4150 AllowedNameModifiers.push_back(OMPD_target); 4151 break; 4152 case OMPD_target_teams_distribute_parallel_for: 4153 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 4154 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4155 AllowedNameModifiers.push_back(OMPD_target); 4156 AllowedNameModifiers.push_back(OMPD_parallel); 4157 break; 4158 case OMPD_target_teams_distribute_parallel_for_simd: 4159 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 4160 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4161 AllowedNameModifiers.push_back(OMPD_target); 4162 AllowedNameModifiers.push_back(OMPD_parallel); 4163 break; 4164 case OMPD_target_teams_distribute_simd: 4165 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 4166 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4167 AllowedNameModifiers.push_back(OMPD_target); 4168 break; 4169 case OMPD_declare_target: 4170 case OMPD_end_declare_target: 4171 case OMPD_threadprivate: 4172 case OMPD_allocate: 4173 case OMPD_declare_reduction: 4174 case OMPD_declare_mapper: 4175 case OMPD_declare_simd: 4176 case OMPD_requires: 4177 llvm_unreachable("OpenMP Directive is not allowed"); 4178 case OMPD_unknown: 4179 llvm_unreachable("Unknown OpenMP directive"); 4180 } 4181 4182 ErrorFound = Res.isInvalid() || ErrorFound; 4183 4184 for (const auto &P : VarsWithInheritedDSA) { 4185 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 4186 << P.first << P.second->getSourceRange(); 4187 } 4188 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 4189 4190 if (!AllowedNameModifiers.empty()) 4191 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 4192 ErrorFound; 4193 4194 if (ErrorFound) 4195 return StmtError(); 4196 4197 if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) { 4198 Res.getAs<OMPExecutableDirective>() 4199 ->getStructuredBlock() 4200 ->setIsOMPStructuredBlock(true); 4201 } 4202 4203 if (!CurContext->isDependentContext() && 4204 isOpenMPTargetExecutionDirective(Kind) && 4205 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 4206 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 4207 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 4208 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 4209 // Register target to DSA Stack. 4210 DSAStack->addTargetDirLocation(StartLoc); 4211 } 4212 4213 return Res; 4214 } 4215 4216 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 4217 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 4218 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 4219 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 4220 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 4221 assert(Aligneds.size() == Alignments.size()); 4222 assert(Linears.size() == LinModifiers.size()); 4223 assert(Linears.size() == Steps.size()); 4224 if (!DG || DG.get().isNull()) 4225 return DeclGroupPtrTy(); 4226 4227 if (!DG.get().isSingleDecl()) { 4228 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 4229 return DG; 4230 } 4231 Decl *ADecl = DG.get().getSingleDecl(); 4232 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 4233 ADecl = FTD->getTemplatedDecl(); 4234 4235 auto *FD = dyn_cast<FunctionDecl>(ADecl); 4236 if (!FD) { 4237 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 4238 return DeclGroupPtrTy(); 4239 } 4240 4241 // OpenMP [2.8.2, declare simd construct, Description] 4242 // The parameter of the simdlen clause must be a constant positive integer 4243 // expression. 4244 ExprResult SL; 4245 if (Simdlen) 4246 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 4247 // OpenMP [2.8.2, declare simd construct, Description] 4248 // The special this pointer can be used as if was one of the arguments to the 4249 // function in any of the linear, aligned, or uniform clauses. 4250 // The uniform clause declares one or more arguments to have an invariant 4251 // value for all concurrent invocations of the function in the execution of a 4252 // single SIMD loop. 4253 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 4254 const Expr *UniformedLinearThis = nullptr; 4255 for (const Expr *E : Uniforms) { 4256 E = E->IgnoreParenImpCasts(); 4257 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4258 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 4259 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4260 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4261 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 4262 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 4263 continue; 4264 } 4265 if (isa<CXXThisExpr>(E)) { 4266 UniformedLinearThis = E; 4267 continue; 4268 } 4269 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4270 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4271 } 4272 // OpenMP [2.8.2, declare simd construct, Description] 4273 // The aligned clause declares that the object to which each list item points 4274 // is aligned to the number of bytes expressed in the optional parameter of 4275 // the aligned clause. 4276 // The special this pointer can be used as if was one of the arguments to the 4277 // function in any of the linear, aligned, or uniform clauses. 4278 // The type of list items appearing in the aligned clause must be array, 4279 // pointer, reference to array, or reference to pointer. 4280 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 4281 const Expr *AlignedThis = nullptr; 4282 for (const Expr *E : Aligneds) { 4283 E = E->IgnoreParenImpCasts(); 4284 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4285 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4286 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4287 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4288 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4289 ->getCanonicalDecl() == CanonPVD) { 4290 // OpenMP [2.8.1, simd construct, Restrictions] 4291 // A list-item cannot appear in more than one aligned clause. 4292 if (AlignedArgs.count(CanonPVD) > 0) { 4293 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 4294 << 1 << E->getSourceRange(); 4295 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 4296 diag::note_omp_explicit_dsa) 4297 << getOpenMPClauseName(OMPC_aligned); 4298 continue; 4299 } 4300 AlignedArgs[CanonPVD] = E; 4301 QualType QTy = PVD->getType() 4302 .getNonReferenceType() 4303 .getUnqualifiedType() 4304 .getCanonicalType(); 4305 const Type *Ty = QTy.getTypePtrOrNull(); 4306 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 4307 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 4308 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 4309 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 4310 } 4311 continue; 4312 } 4313 } 4314 if (isa<CXXThisExpr>(E)) { 4315 if (AlignedThis) { 4316 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 4317 << 2 << E->getSourceRange(); 4318 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 4319 << getOpenMPClauseName(OMPC_aligned); 4320 } 4321 AlignedThis = E; 4322 continue; 4323 } 4324 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4325 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4326 } 4327 // The optional parameter of the aligned clause, alignment, must be a constant 4328 // positive integer expression. If no optional parameter is specified, 4329 // implementation-defined default alignments for SIMD instructions on the 4330 // target platforms are assumed. 4331 SmallVector<const Expr *, 4> NewAligns; 4332 for (Expr *E : Alignments) { 4333 ExprResult Align; 4334 if (E) 4335 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 4336 NewAligns.push_back(Align.get()); 4337 } 4338 // OpenMP [2.8.2, declare simd construct, Description] 4339 // The linear clause declares one or more list items to be private to a SIMD 4340 // lane and to have a linear relationship with respect to the iteration space 4341 // of a loop. 4342 // The special this pointer can be used as if was one of the arguments to the 4343 // function in any of the linear, aligned, or uniform clauses. 4344 // When a linear-step expression is specified in a linear clause it must be 4345 // either a constant integer expression or an integer-typed parameter that is 4346 // specified in a uniform clause on the directive. 4347 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 4348 const bool IsUniformedThis = UniformedLinearThis != nullptr; 4349 auto MI = LinModifiers.begin(); 4350 for (const Expr *E : Linears) { 4351 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 4352 ++MI; 4353 E = E->IgnoreParenImpCasts(); 4354 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4355 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4356 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4357 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4358 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4359 ->getCanonicalDecl() == CanonPVD) { 4360 // OpenMP [2.15.3.7, linear Clause, Restrictions] 4361 // A list-item cannot appear in more than one linear clause. 4362 if (LinearArgs.count(CanonPVD) > 0) { 4363 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4364 << getOpenMPClauseName(OMPC_linear) 4365 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 4366 Diag(LinearArgs[CanonPVD]->getExprLoc(), 4367 diag::note_omp_explicit_dsa) 4368 << getOpenMPClauseName(OMPC_linear); 4369 continue; 4370 } 4371 // Each argument can appear in at most one uniform or linear clause. 4372 if (UniformedArgs.count(CanonPVD) > 0) { 4373 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4374 << getOpenMPClauseName(OMPC_linear) 4375 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 4376 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 4377 diag::note_omp_explicit_dsa) 4378 << getOpenMPClauseName(OMPC_uniform); 4379 continue; 4380 } 4381 LinearArgs[CanonPVD] = E; 4382 if (E->isValueDependent() || E->isTypeDependent() || 4383 E->isInstantiationDependent() || 4384 E->containsUnexpandedParameterPack()) 4385 continue; 4386 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 4387 PVD->getOriginalType()); 4388 continue; 4389 } 4390 } 4391 if (isa<CXXThisExpr>(E)) { 4392 if (UniformedLinearThis) { 4393 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4394 << getOpenMPClauseName(OMPC_linear) 4395 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 4396 << E->getSourceRange(); 4397 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 4398 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 4399 : OMPC_linear); 4400 continue; 4401 } 4402 UniformedLinearThis = E; 4403 if (E->isValueDependent() || E->isTypeDependent() || 4404 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 4405 continue; 4406 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 4407 E->getType()); 4408 continue; 4409 } 4410 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4411 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4412 } 4413 Expr *Step = nullptr; 4414 Expr *NewStep = nullptr; 4415 SmallVector<Expr *, 4> NewSteps; 4416 for (Expr *E : Steps) { 4417 // Skip the same step expression, it was checked already. 4418 if (Step == E || !E) { 4419 NewSteps.push_back(E ? NewStep : nullptr); 4420 continue; 4421 } 4422 Step = E; 4423 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 4424 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4425 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4426 if (UniformedArgs.count(CanonPVD) == 0) { 4427 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 4428 << Step->getSourceRange(); 4429 } else if (E->isValueDependent() || E->isTypeDependent() || 4430 E->isInstantiationDependent() || 4431 E->containsUnexpandedParameterPack() || 4432 CanonPVD->getType()->hasIntegerRepresentation()) { 4433 NewSteps.push_back(Step); 4434 } else { 4435 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 4436 << Step->getSourceRange(); 4437 } 4438 continue; 4439 } 4440 NewStep = Step; 4441 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 4442 !Step->isInstantiationDependent() && 4443 !Step->containsUnexpandedParameterPack()) { 4444 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 4445 .get(); 4446 if (NewStep) 4447 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 4448 } 4449 NewSteps.push_back(NewStep); 4450 } 4451 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 4452 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 4453 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 4454 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 4455 const_cast<Expr **>(Linears.data()), Linears.size(), 4456 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 4457 NewSteps.data(), NewSteps.size(), SR); 4458 ADecl->addAttr(NewAttr); 4459 return ConvertDeclToDeclGroup(ADecl); 4460 } 4461 4462 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 4463 Stmt *AStmt, 4464 SourceLocation StartLoc, 4465 SourceLocation EndLoc) { 4466 if (!AStmt) 4467 return StmtError(); 4468 4469 auto *CS = cast<CapturedStmt>(AStmt); 4470 // 1.2.2 OpenMP Language Terminology 4471 // Structured block - An executable statement with a single entry at the 4472 // top and a single exit at the bottom. 4473 // The point of exit cannot be a branch out of the structured block. 4474 // longjmp() and throw() must not violate the entry/exit criteria. 4475 CS->getCapturedDecl()->setNothrow(); 4476 4477 setFunctionHasBranchProtectedScope(); 4478 4479 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4480 DSAStack->isCancelRegion()); 4481 } 4482 4483 namespace { 4484 /// Helper class for checking canonical form of the OpenMP loops and 4485 /// extracting iteration space of each loop in the loop nest, that will be used 4486 /// for IR generation. 4487 class OpenMPIterationSpaceChecker { 4488 /// Reference to Sema. 4489 Sema &SemaRef; 4490 /// A location for diagnostics (when there is no some better location). 4491 SourceLocation DefaultLoc; 4492 /// A location for diagnostics (when increment is not compatible). 4493 SourceLocation ConditionLoc; 4494 /// A source location for referring to loop init later. 4495 SourceRange InitSrcRange; 4496 /// A source location for referring to condition later. 4497 SourceRange ConditionSrcRange; 4498 /// A source location for referring to increment later. 4499 SourceRange IncrementSrcRange; 4500 /// Loop variable. 4501 ValueDecl *LCDecl = nullptr; 4502 /// Reference to loop variable. 4503 Expr *LCRef = nullptr; 4504 /// Lower bound (initializer for the var). 4505 Expr *LB = nullptr; 4506 /// Upper bound. 4507 Expr *UB = nullptr; 4508 /// Loop step (increment). 4509 Expr *Step = nullptr; 4510 /// This flag is true when condition is one of: 4511 /// Var < UB 4512 /// Var <= UB 4513 /// UB > Var 4514 /// UB >= Var 4515 /// This will have no value when the condition is != 4516 llvm::Optional<bool> TestIsLessOp; 4517 /// This flag is true when condition is strict ( < or > ). 4518 bool TestIsStrictOp = false; 4519 /// This flag is true when step is subtracted on each iteration. 4520 bool SubtractStep = false; 4521 4522 public: 4523 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 4524 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 4525 /// Check init-expr for canonical loop form and save loop counter 4526 /// variable - #Var and its initialization value - #LB. 4527 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 4528 /// Check test-expr for canonical form, save upper-bound (#UB), flags 4529 /// for less/greater and for strict/non-strict comparison. 4530 bool checkAndSetCond(Expr *S); 4531 /// Check incr-expr for canonical loop form and return true if it 4532 /// does not conform, otherwise save loop step (#Step). 4533 bool checkAndSetInc(Expr *S); 4534 /// Return the loop counter variable. 4535 ValueDecl *getLoopDecl() const { return LCDecl; } 4536 /// Return the reference expression to loop counter variable. 4537 Expr *getLoopDeclRefExpr() const { return LCRef; } 4538 /// Source range of the loop init. 4539 SourceRange getInitSrcRange() const { return InitSrcRange; } 4540 /// Source range of the loop condition. 4541 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 4542 /// Source range of the loop increment. 4543 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 4544 /// True if the step should be subtracted. 4545 bool shouldSubtractStep() const { return SubtractStep; } 4546 /// True, if the compare operator is strict (<, > or !=). 4547 bool isStrictTestOp() const { return TestIsStrictOp; } 4548 /// Build the expression to calculate the number of iterations. 4549 Expr *buildNumIterations( 4550 Scope *S, const bool LimitedType, 4551 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4552 /// Build the precondition expression for the loops. 4553 Expr * 4554 buildPreCond(Scope *S, Expr *Cond, 4555 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4556 /// Build reference expression to the counter be used for codegen. 4557 DeclRefExpr * 4558 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4559 DSAStackTy &DSA) const; 4560 /// Build reference expression to the private counter be used for 4561 /// codegen. 4562 Expr *buildPrivateCounterVar() const; 4563 /// Build initialization of the counter be used for codegen. 4564 Expr *buildCounterInit() const; 4565 /// Build step of the counter be used for codegen. 4566 Expr *buildCounterStep() const; 4567 /// Build loop data with counter value for depend clauses in ordered 4568 /// directives. 4569 Expr * 4570 buildOrderedLoopData(Scope *S, Expr *Counter, 4571 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4572 SourceLocation Loc, Expr *Inc = nullptr, 4573 OverloadedOperatorKind OOK = OO_Amp); 4574 /// Return true if any expression is dependent. 4575 bool dependent() const; 4576 4577 private: 4578 /// Check the right-hand side of an assignment in the increment 4579 /// expression. 4580 bool checkAndSetIncRHS(Expr *RHS); 4581 /// Helper to set loop counter variable and its initializer. 4582 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 4583 /// Helper to set upper bound. 4584 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 4585 SourceRange SR, SourceLocation SL); 4586 /// Helper to set loop increment. 4587 bool setStep(Expr *NewStep, bool Subtract); 4588 }; 4589 4590 bool OpenMPIterationSpaceChecker::dependent() const { 4591 if (!LCDecl) { 4592 assert(!LB && !UB && !Step); 4593 return false; 4594 } 4595 return LCDecl->getType()->isDependentType() || 4596 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 4597 (Step && Step->isValueDependent()); 4598 } 4599 4600 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 4601 Expr *NewLCRefExpr, 4602 Expr *NewLB) { 4603 // State consistency checking to ensure correct usage. 4604 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 4605 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4606 if (!NewLCDecl || !NewLB) 4607 return true; 4608 LCDecl = getCanonicalDecl(NewLCDecl); 4609 LCRef = NewLCRefExpr; 4610 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 4611 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4612 if ((Ctor->isCopyOrMoveConstructor() || 4613 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4614 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4615 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 4616 LB = NewLB; 4617 return false; 4618 } 4619 4620 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 4621 llvm::Optional<bool> LessOp, 4622 bool StrictOp, SourceRange SR, 4623 SourceLocation SL) { 4624 // State consistency checking to ensure correct usage. 4625 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 4626 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4627 if (!NewUB) 4628 return true; 4629 UB = NewUB; 4630 if (LessOp) 4631 TestIsLessOp = LessOp; 4632 TestIsStrictOp = StrictOp; 4633 ConditionSrcRange = SR; 4634 ConditionLoc = SL; 4635 return false; 4636 } 4637 4638 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 4639 // State consistency checking to ensure correct usage. 4640 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 4641 if (!NewStep) 4642 return true; 4643 if (!NewStep->isValueDependent()) { 4644 // Check that the step is integer expression. 4645 SourceLocation StepLoc = NewStep->getBeginLoc(); 4646 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 4647 StepLoc, getExprAsWritten(NewStep)); 4648 if (Val.isInvalid()) 4649 return true; 4650 NewStep = Val.get(); 4651 4652 // OpenMP [2.6, Canonical Loop Form, Restrictions] 4653 // If test-expr is of form var relational-op b and relational-op is < or 4654 // <= then incr-expr must cause var to increase on each iteration of the 4655 // loop. If test-expr is of form var relational-op b and relational-op is 4656 // > or >= then incr-expr must cause var to decrease on each iteration of 4657 // the loop. 4658 // If test-expr is of form b relational-op var and relational-op is < or 4659 // <= then incr-expr must cause var to decrease on each iteration of the 4660 // loop. If test-expr is of form b relational-op var and relational-op is 4661 // > or >= then incr-expr must cause var to increase on each iteration of 4662 // the loop. 4663 llvm::APSInt Result; 4664 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 4665 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 4666 bool IsConstNeg = 4667 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 4668 bool IsConstPos = 4669 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 4670 bool IsConstZero = IsConstant && !Result.getBoolValue(); 4671 4672 // != with increment is treated as <; != with decrement is treated as > 4673 if (!TestIsLessOp.hasValue()) 4674 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 4675 if (UB && (IsConstZero || 4676 (TestIsLessOp.getValue() ? 4677 (IsConstNeg || (IsUnsigned && Subtract)) : 4678 (IsConstPos || (IsUnsigned && !Subtract))))) { 4679 SemaRef.Diag(NewStep->getExprLoc(), 4680 diag::err_omp_loop_incr_not_compatible) 4681 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 4682 SemaRef.Diag(ConditionLoc, 4683 diag::note_omp_loop_cond_requres_compatible_incr) 4684 << TestIsLessOp.getValue() << ConditionSrcRange; 4685 return true; 4686 } 4687 if (TestIsLessOp.getValue() == Subtract) { 4688 NewStep = 4689 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 4690 .get(); 4691 Subtract = !Subtract; 4692 } 4693 } 4694 4695 Step = NewStep; 4696 SubtractStep = Subtract; 4697 return false; 4698 } 4699 4700 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 4701 // Check init-expr for canonical loop form and save loop counter 4702 // variable - #Var and its initialization value - #LB. 4703 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 4704 // var = lb 4705 // integer-type var = lb 4706 // random-access-iterator-type var = lb 4707 // pointer-type var = lb 4708 // 4709 if (!S) { 4710 if (EmitDiags) { 4711 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 4712 } 4713 return true; 4714 } 4715 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4716 if (!ExprTemp->cleanupsHaveSideEffects()) 4717 S = ExprTemp->getSubExpr(); 4718 4719 InitSrcRange = S->getSourceRange(); 4720 if (Expr *E = dyn_cast<Expr>(S)) 4721 S = E->IgnoreParens(); 4722 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4723 if (BO->getOpcode() == BO_Assign) { 4724 Expr *LHS = BO->getLHS()->IgnoreParens(); 4725 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4726 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4727 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4728 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4729 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 4730 } 4731 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4732 if (ME->isArrow() && 4733 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4734 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4735 } 4736 } 4737 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 4738 if (DS->isSingleDecl()) { 4739 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 4740 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 4741 // Accept non-canonical init form here but emit ext. warning. 4742 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 4743 SemaRef.Diag(S->getBeginLoc(), 4744 diag::ext_omp_loop_not_canonical_init) 4745 << S->getSourceRange(); 4746 return setLCDeclAndLB( 4747 Var, 4748 buildDeclRefExpr(SemaRef, Var, 4749 Var->getType().getNonReferenceType(), 4750 DS->getBeginLoc()), 4751 Var->getInit()); 4752 } 4753 } 4754 } 4755 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4756 if (CE->getOperator() == OO_Equal) { 4757 Expr *LHS = CE->getArg(0); 4758 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4759 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4760 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4761 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4762 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 4763 } 4764 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4765 if (ME->isArrow() && 4766 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4767 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4768 } 4769 } 4770 } 4771 4772 if (dependent() || SemaRef.CurContext->isDependentContext()) 4773 return false; 4774 if (EmitDiags) { 4775 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 4776 << S->getSourceRange(); 4777 } 4778 return true; 4779 } 4780 4781 /// Ignore parenthesizes, implicit casts, copy constructor and return the 4782 /// variable (which may be the loop variable) if possible. 4783 static const ValueDecl *getInitLCDecl(const Expr *E) { 4784 if (!E) 4785 return nullptr; 4786 E = getExprAsWritten(E); 4787 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 4788 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4789 if ((Ctor->isCopyOrMoveConstructor() || 4790 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4791 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4792 E = CE->getArg(0)->IgnoreParenImpCasts(); 4793 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 4794 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 4795 return getCanonicalDecl(VD); 4796 } 4797 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 4798 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4799 return getCanonicalDecl(ME->getMemberDecl()); 4800 return nullptr; 4801 } 4802 4803 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 4804 // Check test-expr for canonical form, save upper-bound UB, flags for 4805 // less/greater and for strict/non-strict comparison. 4806 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4807 // var relational-op b 4808 // b relational-op var 4809 // 4810 if (!S) { 4811 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 4812 return true; 4813 } 4814 S = getExprAsWritten(S); 4815 SourceLocation CondLoc = S->getBeginLoc(); 4816 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4817 if (BO->isRelationalOp()) { 4818 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4819 return setUB(BO->getRHS(), 4820 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 4821 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4822 BO->getSourceRange(), BO->getOperatorLoc()); 4823 if (getInitLCDecl(BO->getRHS()) == LCDecl) 4824 return setUB(BO->getLHS(), 4825 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 4826 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4827 BO->getSourceRange(), BO->getOperatorLoc()); 4828 } else if (BO->getOpcode() == BO_NE) 4829 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ? 4830 BO->getRHS() : BO->getLHS(), 4831 /*LessOp=*/llvm::None, 4832 /*StrictOp=*/true, 4833 BO->getSourceRange(), BO->getOperatorLoc()); 4834 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4835 if (CE->getNumArgs() == 2) { 4836 auto Op = CE->getOperator(); 4837 switch (Op) { 4838 case OO_Greater: 4839 case OO_GreaterEqual: 4840 case OO_Less: 4841 case OO_LessEqual: 4842 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4843 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 4844 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4845 CE->getOperatorLoc()); 4846 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 4847 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 4848 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4849 CE->getOperatorLoc()); 4850 break; 4851 case OO_ExclaimEqual: 4852 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? 4853 CE->getArg(1) : CE->getArg(0), 4854 /*LessOp=*/llvm::None, 4855 /*StrictOp=*/true, 4856 CE->getSourceRange(), 4857 CE->getOperatorLoc()); 4858 break; 4859 default: 4860 break; 4861 } 4862 } 4863 } 4864 if (dependent() || SemaRef.CurContext->isDependentContext()) 4865 return false; 4866 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 4867 << S->getSourceRange() << LCDecl; 4868 return true; 4869 } 4870 4871 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 4872 // RHS of canonical loop form increment can be: 4873 // var + incr 4874 // incr + var 4875 // var - incr 4876 // 4877 RHS = RHS->IgnoreParenImpCasts(); 4878 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 4879 if (BO->isAdditiveOp()) { 4880 bool IsAdd = BO->getOpcode() == BO_Add; 4881 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4882 return setStep(BO->getRHS(), !IsAdd); 4883 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 4884 return setStep(BO->getLHS(), /*Subtract=*/false); 4885 } 4886 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 4887 bool IsAdd = CE->getOperator() == OO_Plus; 4888 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 4889 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4890 return setStep(CE->getArg(1), !IsAdd); 4891 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 4892 return setStep(CE->getArg(0), /*Subtract=*/false); 4893 } 4894 } 4895 if (dependent() || SemaRef.CurContext->isDependentContext()) 4896 return false; 4897 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4898 << RHS->getSourceRange() << LCDecl; 4899 return true; 4900 } 4901 4902 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 4903 // Check incr-expr for canonical loop form and return true if it 4904 // does not conform. 4905 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4906 // ++var 4907 // var++ 4908 // --var 4909 // var-- 4910 // var += incr 4911 // var -= incr 4912 // var = var + incr 4913 // var = incr + var 4914 // var = var - incr 4915 // 4916 if (!S) { 4917 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 4918 return true; 4919 } 4920 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4921 if (!ExprTemp->cleanupsHaveSideEffects()) 4922 S = ExprTemp->getSubExpr(); 4923 4924 IncrementSrcRange = S->getSourceRange(); 4925 S = S->IgnoreParens(); 4926 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 4927 if (UO->isIncrementDecrementOp() && 4928 getInitLCDecl(UO->getSubExpr()) == LCDecl) 4929 return setStep(SemaRef 4930 .ActOnIntegerConstant(UO->getBeginLoc(), 4931 (UO->isDecrementOp() ? -1 : 1)) 4932 .get(), 4933 /*Subtract=*/false); 4934 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4935 switch (BO->getOpcode()) { 4936 case BO_AddAssign: 4937 case BO_SubAssign: 4938 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4939 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 4940 break; 4941 case BO_Assign: 4942 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4943 return checkAndSetIncRHS(BO->getRHS()); 4944 break; 4945 default: 4946 break; 4947 } 4948 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4949 switch (CE->getOperator()) { 4950 case OO_PlusPlus: 4951 case OO_MinusMinus: 4952 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4953 return setStep(SemaRef 4954 .ActOnIntegerConstant( 4955 CE->getBeginLoc(), 4956 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 4957 .get(), 4958 /*Subtract=*/false); 4959 break; 4960 case OO_PlusEqual: 4961 case OO_MinusEqual: 4962 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4963 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 4964 break; 4965 case OO_Equal: 4966 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4967 return checkAndSetIncRHS(CE->getArg(1)); 4968 break; 4969 default: 4970 break; 4971 } 4972 } 4973 if (dependent() || SemaRef.CurContext->isDependentContext()) 4974 return false; 4975 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4976 << S->getSourceRange() << LCDecl; 4977 return true; 4978 } 4979 4980 static ExprResult 4981 tryBuildCapture(Sema &SemaRef, Expr *Capture, 4982 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4983 if (SemaRef.CurContext->isDependentContext()) 4984 return ExprResult(Capture); 4985 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 4986 return SemaRef.PerformImplicitConversion( 4987 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 4988 /*AllowExplicit=*/true); 4989 auto I = Captures.find(Capture); 4990 if (I != Captures.end()) 4991 return buildCapture(SemaRef, Capture, I->second); 4992 DeclRefExpr *Ref = nullptr; 4993 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 4994 Captures[Capture] = Ref; 4995 return Res; 4996 } 4997 4998 /// Build the expression to calculate the number of iterations. 4999 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 5000 Scope *S, const bool LimitedType, 5001 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 5002 ExprResult Diff; 5003 QualType VarType = LCDecl->getType().getNonReferenceType(); 5004 if (VarType->isIntegerType() || VarType->isPointerType() || 5005 SemaRef.getLangOpts().CPlusPlus) { 5006 // Upper - Lower 5007 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 5008 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 5009 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 5010 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 5011 if (!Upper || !Lower) 5012 return nullptr; 5013 5014 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 5015 5016 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 5017 // BuildBinOp already emitted error, this one is to point user to upper 5018 // and lower bound, and to tell what is passed to 'operator-'. 5019 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 5020 << Upper->getSourceRange() << Lower->getSourceRange(); 5021 return nullptr; 5022 } 5023 } 5024 5025 if (!Diff.isUsable()) 5026 return nullptr; 5027 5028 // Upper - Lower [- 1] 5029 if (TestIsStrictOp) 5030 Diff = SemaRef.BuildBinOp( 5031 S, DefaultLoc, BO_Sub, Diff.get(), 5032 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5033 if (!Diff.isUsable()) 5034 return nullptr; 5035 5036 // Upper - Lower [- 1] + Step 5037 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 5038 if (!NewStep.isUsable()) 5039 return nullptr; 5040 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 5041 if (!Diff.isUsable()) 5042 return nullptr; 5043 5044 // Parentheses (for dumping/debugging purposes only). 5045 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 5046 if (!Diff.isUsable()) 5047 return nullptr; 5048 5049 // (Upper - Lower [- 1] + Step) / Step 5050 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 5051 if (!Diff.isUsable()) 5052 return nullptr; 5053 5054 // OpenMP runtime requires 32-bit or 64-bit loop variables. 5055 QualType Type = Diff.get()->getType(); 5056 ASTContext &C = SemaRef.Context; 5057 bool UseVarType = VarType->hasIntegerRepresentation() && 5058 C.getTypeSize(Type) > C.getTypeSize(VarType); 5059 if (!Type->isIntegerType() || UseVarType) { 5060 unsigned NewSize = 5061 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 5062 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 5063 : Type->hasSignedIntegerRepresentation(); 5064 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 5065 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 5066 Diff = SemaRef.PerformImplicitConversion( 5067 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 5068 if (!Diff.isUsable()) 5069 return nullptr; 5070 } 5071 } 5072 if (LimitedType) { 5073 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 5074 if (NewSize != C.getTypeSize(Type)) { 5075 if (NewSize < C.getTypeSize(Type)) { 5076 assert(NewSize == 64 && "incorrect loop var size"); 5077 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 5078 << InitSrcRange << ConditionSrcRange; 5079 } 5080 QualType NewType = C.getIntTypeForBitwidth( 5081 NewSize, Type->hasSignedIntegerRepresentation() || 5082 C.getTypeSize(Type) < NewSize); 5083 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 5084 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 5085 Sema::AA_Converting, true); 5086 if (!Diff.isUsable()) 5087 return nullptr; 5088 } 5089 } 5090 } 5091 5092 return Diff.get(); 5093 } 5094 5095 Expr *OpenMPIterationSpaceChecker::buildPreCond( 5096 Scope *S, Expr *Cond, 5097 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 5098 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 5099 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 5100 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 5101 5102 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 5103 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 5104 if (!NewLB.isUsable() || !NewUB.isUsable()) 5105 return nullptr; 5106 5107 ExprResult CondExpr = 5108 SemaRef.BuildBinOp(S, DefaultLoc, 5109 TestIsLessOp.getValue() ? 5110 (TestIsStrictOp ? BO_LT : BO_LE) : 5111 (TestIsStrictOp ? BO_GT : BO_GE), 5112 NewLB.get(), NewUB.get()); 5113 if (CondExpr.isUsable()) { 5114 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 5115 SemaRef.Context.BoolTy)) 5116 CondExpr = SemaRef.PerformImplicitConversion( 5117 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 5118 /*AllowExplicit=*/true); 5119 } 5120 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 5121 // Otherwise use original loop condition and evaluate it in runtime. 5122 return CondExpr.isUsable() ? CondExpr.get() : Cond; 5123 } 5124 5125 /// Build reference expression to the counter be used for codegen. 5126 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 5127 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 5128 DSAStackTy &DSA) const { 5129 auto *VD = dyn_cast<VarDecl>(LCDecl); 5130 if (!VD) { 5131 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 5132 DeclRefExpr *Ref = buildDeclRefExpr( 5133 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 5134 const DSAStackTy::DSAVarData Data = 5135 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 5136 // If the loop control decl is explicitly marked as private, do not mark it 5137 // as captured again. 5138 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 5139 Captures.insert(std::make_pair(LCRef, Ref)); 5140 return Ref; 5141 } 5142 return cast<DeclRefExpr>(LCRef); 5143 } 5144 5145 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 5146 if (LCDecl && !LCDecl->isInvalidDecl()) { 5147 QualType Type = LCDecl->getType().getNonReferenceType(); 5148 VarDecl *PrivateVar = buildVarDecl( 5149 SemaRef, DefaultLoc, Type, LCDecl->getName(), 5150 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 5151 isa<VarDecl>(LCDecl) 5152 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 5153 : nullptr); 5154 if (PrivateVar->isInvalidDecl()) 5155 return nullptr; 5156 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 5157 } 5158 return nullptr; 5159 } 5160 5161 /// Build initialization of the counter to be used for codegen. 5162 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 5163 5164 /// Build step of the counter be used for codegen. 5165 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 5166 5167 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 5168 Scope *S, Expr *Counter, 5169 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 5170 Expr *Inc, OverloadedOperatorKind OOK) { 5171 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 5172 if (!Cnt) 5173 return nullptr; 5174 if (Inc) { 5175 assert((OOK == OO_Plus || OOK == OO_Minus) && 5176 "Expected only + or - operations for depend clauses."); 5177 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 5178 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 5179 if (!Cnt) 5180 return nullptr; 5181 } 5182 ExprResult Diff; 5183 QualType VarType = LCDecl->getType().getNonReferenceType(); 5184 if (VarType->isIntegerType() || VarType->isPointerType() || 5185 SemaRef.getLangOpts().CPlusPlus) { 5186 // Upper - Lower 5187 Expr *Upper = TestIsLessOp.getValue() 5188 ? Cnt 5189 : tryBuildCapture(SemaRef, UB, Captures).get(); 5190 Expr *Lower = TestIsLessOp.getValue() 5191 ? tryBuildCapture(SemaRef, LB, Captures).get() 5192 : Cnt; 5193 if (!Upper || !Lower) 5194 return nullptr; 5195 5196 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 5197 5198 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 5199 // BuildBinOp already emitted error, this one is to point user to upper 5200 // and lower bound, and to tell what is passed to 'operator-'. 5201 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 5202 << Upper->getSourceRange() << Lower->getSourceRange(); 5203 return nullptr; 5204 } 5205 } 5206 5207 if (!Diff.isUsable()) 5208 return nullptr; 5209 5210 // Parentheses (for dumping/debugging purposes only). 5211 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 5212 if (!Diff.isUsable()) 5213 return nullptr; 5214 5215 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 5216 if (!NewStep.isUsable()) 5217 return nullptr; 5218 // (Upper - Lower) / Step 5219 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 5220 if (!Diff.isUsable()) 5221 return nullptr; 5222 5223 return Diff.get(); 5224 } 5225 5226 /// Iteration space of a single for loop. 5227 struct LoopIterationSpace final { 5228 /// True if the condition operator is the strict compare operator (<, > or 5229 /// !=). 5230 bool IsStrictCompare = false; 5231 /// Condition of the loop. 5232 Expr *PreCond = nullptr; 5233 /// This expression calculates the number of iterations in the loop. 5234 /// It is always possible to calculate it before starting the loop. 5235 Expr *NumIterations = nullptr; 5236 /// The loop counter variable. 5237 Expr *CounterVar = nullptr; 5238 /// Private loop counter variable. 5239 Expr *PrivateCounterVar = nullptr; 5240 /// This is initializer for the initial value of #CounterVar. 5241 Expr *CounterInit = nullptr; 5242 /// This is step for the #CounterVar used to generate its update: 5243 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 5244 Expr *CounterStep = nullptr; 5245 /// Should step be subtracted? 5246 bool Subtract = false; 5247 /// Source range of the loop init. 5248 SourceRange InitSrcRange; 5249 /// Source range of the loop condition. 5250 SourceRange CondSrcRange; 5251 /// Source range of the loop increment. 5252 SourceRange IncSrcRange; 5253 }; 5254 5255 } // namespace 5256 5257 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 5258 assert(getLangOpts().OpenMP && "OpenMP is not active."); 5259 assert(Init && "Expected loop in canonical form."); 5260 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 5261 if (AssociatedLoops > 0 && 5262 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 5263 DSAStack->loopStart(); 5264 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 5265 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 5266 if (ValueDecl *D = ISC.getLoopDecl()) { 5267 auto *VD = dyn_cast<VarDecl>(D); 5268 if (!VD) { 5269 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 5270 VD = Private; 5271 } else { 5272 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 5273 /*WithInit=*/false); 5274 VD = cast<VarDecl>(Ref->getDecl()); 5275 } 5276 } 5277 DSAStack->addLoopControlVariable(D, VD); 5278 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 5279 if (LD != D->getCanonicalDecl()) { 5280 DSAStack->resetPossibleLoopCounter(); 5281 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 5282 MarkDeclarationsReferencedInExpr( 5283 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 5284 Var->getType().getNonLValueExprType(Context), 5285 ForLoc, /*RefersToCapture=*/true)); 5286 } 5287 } 5288 } 5289 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 5290 } 5291 } 5292 5293 /// Called on a for stmt to check and extract its iteration space 5294 /// for further processing (such as collapsing). 5295 static bool checkOpenMPIterationSpace( 5296 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 5297 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 5298 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 5299 Expr *OrderedLoopCountExpr, 5300 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5301 LoopIterationSpace &ResultIterSpace, 5302 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5303 // OpenMP [2.6, Canonical Loop Form] 5304 // for (init-expr; test-expr; incr-expr) structured-block 5305 auto *For = dyn_cast_or_null<ForStmt>(S); 5306 if (!For) { 5307 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 5308 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 5309 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 5310 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 5311 if (TotalNestedLoopCount > 1) { 5312 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 5313 SemaRef.Diag(DSA.getConstructLoc(), 5314 diag::note_omp_collapse_ordered_expr) 5315 << 2 << CollapseLoopCountExpr->getSourceRange() 5316 << OrderedLoopCountExpr->getSourceRange(); 5317 else if (CollapseLoopCountExpr) 5318 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 5319 diag::note_omp_collapse_ordered_expr) 5320 << 0 << CollapseLoopCountExpr->getSourceRange(); 5321 else 5322 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 5323 diag::note_omp_collapse_ordered_expr) 5324 << 1 << OrderedLoopCountExpr->getSourceRange(); 5325 } 5326 return true; 5327 } 5328 assert(For->getBody()); 5329 5330 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 5331 5332 // Check init. 5333 Stmt *Init = For->getInit(); 5334 if (ISC.checkAndSetInit(Init)) 5335 return true; 5336 5337 bool HasErrors = false; 5338 5339 // Check loop variable's type. 5340 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 5341 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 5342 5343 // OpenMP [2.6, Canonical Loop Form] 5344 // Var is one of the following: 5345 // A variable of signed or unsigned integer type. 5346 // For C++, a variable of a random access iterator type. 5347 // For C, a variable of a pointer type. 5348 QualType VarType = LCDecl->getType().getNonReferenceType(); 5349 if (!VarType->isDependentType() && !VarType->isIntegerType() && 5350 !VarType->isPointerType() && 5351 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 5352 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 5353 << SemaRef.getLangOpts().CPlusPlus; 5354 HasErrors = true; 5355 } 5356 5357 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 5358 // a Construct 5359 // The loop iteration variable(s) in the associated for-loop(s) of a for or 5360 // parallel for construct is (are) private. 5361 // The loop iteration variable in the associated for-loop of a simd 5362 // construct with just one associated for-loop is linear with a 5363 // constant-linear-step that is the increment of the associated for-loop. 5364 // Exclude loop var from the list of variables with implicitly defined data 5365 // sharing attributes. 5366 VarsWithImplicitDSA.erase(LCDecl); 5367 5368 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 5369 // in a Construct, C/C++]. 5370 // The loop iteration variable in the associated for-loop of a simd 5371 // construct with just one associated for-loop may be listed in a linear 5372 // clause with a constant-linear-step that is the increment of the 5373 // associated for-loop. 5374 // The loop iteration variable(s) in the associated for-loop(s) of a for or 5375 // parallel for construct may be listed in a private or lastprivate clause. 5376 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 5377 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 5378 // declared in the loop and it is predetermined as a private. 5379 OpenMPClauseKind PredeterminedCKind = 5380 isOpenMPSimdDirective(DKind) 5381 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 5382 : OMPC_private; 5383 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 5384 DVar.CKind != PredeterminedCKind) || 5385 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 5386 isOpenMPDistributeDirective(DKind)) && 5387 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 5388 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 5389 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 5390 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 5391 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 5392 << getOpenMPClauseName(PredeterminedCKind); 5393 if (DVar.RefExpr == nullptr) 5394 DVar.CKind = PredeterminedCKind; 5395 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 5396 HasErrors = true; 5397 } else if (LoopDeclRefExpr != nullptr) { 5398 // Make the loop iteration variable private (for worksharing constructs), 5399 // linear (for simd directives with the only one associated loop) or 5400 // lastprivate (for simd directives with several collapsed or ordered 5401 // loops). 5402 if (DVar.CKind == OMPC_unknown) 5403 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 5404 } 5405 5406 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 5407 5408 // Check test-expr. 5409 HasErrors |= ISC.checkAndSetCond(For->getCond()); 5410 5411 // Check incr-expr. 5412 HasErrors |= ISC.checkAndSetInc(For->getInc()); 5413 } 5414 5415 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 5416 return HasErrors; 5417 5418 // Build the loop's iteration space representation. 5419 ResultIterSpace.PreCond = 5420 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 5421 ResultIterSpace.NumIterations = ISC.buildNumIterations( 5422 DSA.getCurScope(), 5423 (isOpenMPWorksharingDirective(DKind) || 5424 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 5425 Captures); 5426 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 5427 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 5428 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 5429 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 5430 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 5431 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 5432 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 5433 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 5434 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp(); 5435 5436 HasErrors |= (ResultIterSpace.PreCond == nullptr || 5437 ResultIterSpace.NumIterations == nullptr || 5438 ResultIterSpace.CounterVar == nullptr || 5439 ResultIterSpace.PrivateCounterVar == nullptr || 5440 ResultIterSpace.CounterInit == nullptr || 5441 ResultIterSpace.CounterStep == nullptr); 5442 if (!HasErrors && DSA.isOrderedRegion()) { 5443 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 5444 if (CurrentNestedLoopCount < 5445 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 5446 DSA.getOrderedRegionParam().second->setLoopNumIterations( 5447 CurrentNestedLoopCount, ResultIterSpace.NumIterations); 5448 DSA.getOrderedRegionParam().second->setLoopCounter( 5449 CurrentNestedLoopCount, ResultIterSpace.CounterVar); 5450 } 5451 } 5452 for (auto &Pair : DSA.getDoacrossDependClauses()) { 5453 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 5454 // Erroneous case - clause has some problems. 5455 continue; 5456 } 5457 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 5458 Pair.second.size() <= CurrentNestedLoopCount) { 5459 // Erroneous case - clause has some problems. 5460 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 5461 continue; 5462 } 5463 Expr *CntValue; 5464 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 5465 CntValue = ISC.buildOrderedLoopData( 5466 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 5467 Pair.first->getDependencyLoc()); 5468 else 5469 CntValue = ISC.buildOrderedLoopData( 5470 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 5471 Pair.first->getDependencyLoc(), 5472 Pair.second[CurrentNestedLoopCount].first, 5473 Pair.second[CurrentNestedLoopCount].second); 5474 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 5475 } 5476 } 5477 5478 return HasErrors; 5479 } 5480 5481 /// Build 'VarRef = Start. 5482 static ExprResult 5483 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5484 ExprResult Start, 5485 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5486 // Build 'VarRef = Start. 5487 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 5488 if (!NewStart.isUsable()) 5489 return ExprError(); 5490 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 5491 VarRef.get()->getType())) { 5492 NewStart = SemaRef.PerformImplicitConversion( 5493 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 5494 /*AllowExplicit=*/true); 5495 if (!NewStart.isUsable()) 5496 return ExprError(); 5497 } 5498 5499 ExprResult Init = 5500 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5501 return Init; 5502 } 5503 5504 /// Build 'VarRef = Start + Iter * Step'. 5505 static ExprResult buildCounterUpdate( 5506 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5507 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 5508 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 5509 // Add parentheses (for debugging purposes only). 5510 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 5511 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 5512 !Step.isUsable()) 5513 return ExprError(); 5514 5515 ExprResult NewStep = Step; 5516 if (Captures) 5517 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 5518 if (NewStep.isInvalid()) 5519 return ExprError(); 5520 ExprResult Update = 5521 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 5522 if (!Update.isUsable()) 5523 return ExprError(); 5524 5525 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 5526 // 'VarRef = Start (+|-) Iter * Step'. 5527 ExprResult NewStart = Start; 5528 if (Captures) 5529 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 5530 if (NewStart.isInvalid()) 5531 return ExprError(); 5532 5533 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 5534 ExprResult SavedUpdate = Update; 5535 ExprResult UpdateVal; 5536 if (VarRef.get()->getType()->isOverloadableType() || 5537 NewStart.get()->getType()->isOverloadableType() || 5538 Update.get()->getType()->isOverloadableType()) { 5539 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 5540 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 5541 Update = 5542 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5543 if (Update.isUsable()) { 5544 UpdateVal = 5545 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 5546 VarRef.get(), SavedUpdate.get()); 5547 if (UpdateVal.isUsable()) { 5548 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 5549 UpdateVal.get()); 5550 } 5551 } 5552 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 5553 } 5554 5555 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 5556 if (!Update.isUsable() || !UpdateVal.isUsable()) { 5557 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 5558 NewStart.get(), SavedUpdate.get()); 5559 if (!Update.isUsable()) 5560 return ExprError(); 5561 5562 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 5563 VarRef.get()->getType())) { 5564 Update = SemaRef.PerformImplicitConversion( 5565 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 5566 if (!Update.isUsable()) 5567 return ExprError(); 5568 } 5569 5570 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 5571 } 5572 return Update; 5573 } 5574 5575 /// Convert integer expression \a E to make it have at least \a Bits 5576 /// bits. 5577 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 5578 if (E == nullptr) 5579 return ExprError(); 5580 ASTContext &C = SemaRef.Context; 5581 QualType OldType = E->getType(); 5582 unsigned HasBits = C.getTypeSize(OldType); 5583 if (HasBits >= Bits) 5584 return ExprResult(E); 5585 // OK to convert to signed, because new type has more bits than old. 5586 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 5587 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 5588 true); 5589 } 5590 5591 /// Check if the given expression \a E is a constant integer that fits 5592 /// into \a Bits bits. 5593 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 5594 if (E == nullptr) 5595 return false; 5596 llvm::APSInt Result; 5597 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 5598 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 5599 return false; 5600 } 5601 5602 /// Build preinits statement for the given declarations. 5603 static Stmt *buildPreInits(ASTContext &Context, 5604 MutableArrayRef<Decl *> PreInits) { 5605 if (!PreInits.empty()) { 5606 return new (Context) DeclStmt( 5607 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 5608 SourceLocation(), SourceLocation()); 5609 } 5610 return nullptr; 5611 } 5612 5613 /// Build preinits statement for the given declarations. 5614 static Stmt * 5615 buildPreInits(ASTContext &Context, 5616 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5617 if (!Captures.empty()) { 5618 SmallVector<Decl *, 16> PreInits; 5619 for (const auto &Pair : Captures) 5620 PreInits.push_back(Pair.second->getDecl()); 5621 return buildPreInits(Context, PreInits); 5622 } 5623 return nullptr; 5624 } 5625 5626 /// Build postupdate expression for the given list of postupdates expressions. 5627 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 5628 Expr *PostUpdate = nullptr; 5629 if (!PostUpdates.empty()) { 5630 for (Expr *E : PostUpdates) { 5631 Expr *ConvE = S.BuildCStyleCastExpr( 5632 E->getExprLoc(), 5633 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 5634 E->getExprLoc(), E) 5635 .get(); 5636 PostUpdate = PostUpdate 5637 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 5638 PostUpdate, ConvE) 5639 .get() 5640 : ConvE; 5641 } 5642 } 5643 return PostUpdate; 5644 } 5645 5646 /// Called on a for stmt to check itself and nested loops (if any). 5647 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 5648 /// number of collapsed loops otherwise. 5649 static unsigned 5650 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 5651 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 5652 DSAStackTy &DSA, 5653 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5654 OMPLoopDirective::HelperExprs &Built) { 5655 unsigned NestedLoopCount = 1; 5656 if (CollapseLoopCountExpr) { 5657 // Found 'collapse' clause - calculate collapse number. 5658 Expr::EvalResult Result; 5659 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 5660 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 5661 } 5662 unsigned OrderedLoopCount = 1; 5663 if (OrderedLoopCountExpr) { 5664 // Found 'ordered' clause - calculate collapse number. 5665 Expr::EvalResult EVResult; 5666 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) { 5667 llvm::APSInt Result = EVResult.Val.getInt(); 5668 if (Result.getLimitedValue() < NestedLoopCount) { 5669 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 5670 diag::err_omp_wrong_ordered_loop_count) 5671 << OrderedLoopCountExpr->getSourceRange(); 5672 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 5673 diag::note_collapse_loop_count) 5674 << CollapseLoopCountExpr->getSourceRange(); 5675 } 5676 OrderedLoopCount = Result.getLimitedValue(); 5677 } 5678 } 5679 // This is helper routine for loop directives (e.g., 'for', 'simd', 5680 // 'for simd', etc.). 5681 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 5682 SmallVector<LoopIterationSpace, 4> IterSpaces( 5683 std::max(OrderedLoopCount, NestedLoopCount)); 5684 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 5685 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 5686 if (checkOpenMPIterationSpace( 5687 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5688 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5689 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5690 Captures)) 5691 return 0; 5692 // Move on to the next nested for loop, or to the loop body. 5693 // OpenMP [2.8.1, simd construct, Restrictions] 5694 // All loops associated with the construct must be perfectly nested; that 5695 // is, there must be no intervening code nor any OpenMP directive between 5696 // any two loops. 5697 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5698 } 5699 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 5700 if (checkOpenMPIterationSpace( 5701 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5702 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5703 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5704 Captures)) 5705 return 0; 5706 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 5707 // Handle initialization of captured loop iterator variables. 5708 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 5709 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 5710 Captures[DRE] = DRE; 5711 } 5712 } 5713 // Move on to the next nested for loop, or to the loop body. 5714 // OpenMP [2.8.1, simd construct, Restrictions] 5715 // All loops associated with the construct must be perfectly nested; that 5716 // is, there must be no intervening code nor any OpenMP directive between 5717 // any two loops. 5718 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5719 } 5720 5721 Built.clear(/* size */ NestedLoopCount); 5722 5723 if (SemaRef.CurContext->isDependentContext()) 5724 return NestedLoopCount; 5725 5726 // An example of what is generated for the following code: 5727 // 5728 // #pragma omp simd collapse(2) ordered(2) 5729 // for (i = 0; i < NI; ++i) 5730 // for (k = 0; k < NK; ++k) 5731 // for (j = J0; j < NJ; j+=2) { 5732 // <loop body> 5733 // } 5734 // 5735 // We generate the code below. 5736 // Note: the loop body may be outlined in CodeGen. 5737 // Note: some counters may be C++ classes, operator- is used to find number of 5738 // iterations and operator+= to calculate counter value. 5739 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 5740 // or i64 is currently supported). 5741 // 5742 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 5743 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 5744 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 5745 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 5746 // // similar updates for vars in clauses (e.g. 'linear') 5747 // <loop body (using local i and j)> 5748 // } 5749 // i = NI; // assign final values of counters 5750 // j = NJ; 5751 // 5752 5753 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 5754 // the iteration counts of the collapsed for loops. 5755 // Precondition tests if there is at least one iteration (all conditions are 5756 // true). 5757 auto PreCond = ExprResult(IterSpaces[0].PreCond); 5758 Expr *N0 = IterSpaces[0].NumIterations; 5759 ExprResult LastIteration32 = 5760 widenIterationCount(/*Bits=*/32, 5761 SemaRef 5762 .PerformImplicitConversion( 5763 N0->IgnoreImpCasts(), N0->getType(), 5764 Sema::AA_Converting, /*AllowExplicit=*/true) 5765 .get(), 5766 SemaRef); 5767 ExprResult LastIteration64 = widenIterationCount( 5768 /*Bits=*/64, 5769 SemaRef 5770 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 5771 Sema::AA_Converting, 5772 /*AllowExplicit=*/true) 5773 .get(), 5774 SemaRef); 5775 5776 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 5777 return NestedLoopCount; 5778 5779 ASTContext &C = SemaRef.Context; 5780 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 5781 5782 Scope *CurScope = DSA.getCurScope(); 5783 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 5784 if (PreCond.isUsable()) { 5785 PreCond = 5786 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 5787 PreCond.get(), IterSpaces[Cnt].PreCond); 5788 } 5789 Expr *N = IterSpaces[Cnt].NumIterations; 5790 SourceLocation Loc = N->getExprLoc(); 5791 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 5792 if (LastIteration32.isUsable()) 5793 LastIteration32 = SemaRef.BuildBinOp( 5794 CurScope, Loc, BO_Mul, LastIteration32.get(), 5795 SemaRef 5796 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5797 Sema::AA_Converting, 5798 /*AllowExplicit=*/true) 5799 .get()); 5800 if (LastIteration64.isUsable()) 5801 LastIteration64 = SemaRef.BuildBinOp( 5802 CurScope, Loc, BO_Mul, LastIteration64.get(), 5803 SemaRef 5804 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5805 Sema::AA_Converting, 5806 /*AllowExplicit=*/true) 5807 .get()); 5808 } 5809 5810 // Choose either the 32-bit or 64-bit version. 5811 ExprResult LastIteration = LastIteration64; 5812 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 5813 (LastIteration32.isUsable() && 5814 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 5815 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 5816 fitsInto( 5817 /*Bits=*/32, 5818 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 5819 LastIteration64.get(), SemaRef)))) 5820 LastIteration = LastIteration32; 5821 QualType VType = LastIteration.get()->getType(); 5822 QualType RealVType = VType; 5823 QualType StrideVType = VType; 5824 if (isOpenMPTaskLoopDirective(DKind)) { 5825 VType = 5826 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 5827 StrideVType = 5828 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 5829 } 5830 5831 if (!LastIteration.isUsable()) 5832 return 0; 5833 5834 // Save the number of iterations. 5835 ExprResult NumIterations = LastIteration; 5836 { 5837 LastIteration = SemaRef.BuildBinOp( 5838 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 5839 LastIteration.get(), 5840 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5841 if (!LastIteration.isUsable()) 5842 return 0; 5843 } 5844 5845 // Calculate the last iteration number beforehand instead of doing this on 5846 // each iteration. Do not do this if the number of iterations may be kfold-ed. 5847 llvm::APSInt Result; 5848 bool IsConstant = 5849 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 5850 ExprResult CalcLastIteration; 5851 if (!IsConstant) { 5852 ExprResult SaveRef = 5853 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 5854 LastIteration = SaveRef; 5855 5856 // Prepare SaveRef + 1. 5857 NumIterations = SemaRef.BuildBinOp( 5858 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 5859 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5860 if (!NumIterations.isUsable()) 5861 return 0; 5862 } 5863 5864 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 5865 5866 // Build variables passed into runtime, necessary for worksharing directives. 5867 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 5868 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5869 isOpenMPDistributeDirective(DKind)) { 5870 // Lower bound variable, initialized with zero. 5871 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 5872 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 5873 SemaRef.AddInitializerToDecl(LBDecl, 5874 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5875 /*DirectInit*/ false); 5876 5877 // Upper bound variable, initialized with last iteration number. 5878 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 5879 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 5880 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 5881 /*DirectInit*/ false); 5882 5883 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 5884 // This will be used to implement clause 'lastprivate'. 5885 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 5886 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 5887 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 5888 SemaRef.AddInitializerToDecl(ILDecl, 5889 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5890 /*DirectInit*/ false); 5891 5892 // Stride variable returned by runtime (we initialize it to 1 by default). 5893 VarDecl *STDecl = 5894 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 5895 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 5896 SemaRef.AddInitializerToDecl(STDecl, 5897 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 5898 /*DirectInit*/ false); 5899 5900 // Build expression: UB = min(UB, LastIteration) 5901 // It is necessary for CodeGen of directives with static scheduling. 5902 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 5903 UB.get(), LastIteration.get()); 5904 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5905 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 5906 LastIteration.get(), UB.get()); 5907 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 5908 CondOp.get()); 5909 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 5910 5911 // If we have a combined directive that combines 'distribute', 'for' or 5912 // 'simd' we need to be able to access the bounds of the schedule of the 5913 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 5914 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 5915 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5916 // Lower bound variable, initialized with zero. 5917 VarDecl *CombLBDecl = 5918 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 5919 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 5920 SemaRef.AddInitializerToDecl( 5921 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5922 /*DirectInit*/ false); 5923 5924 // Upper bound variable, initialized with last iteration number. 5925 VarDecl *CombUBDecl = 5926 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 5927 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 5928 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 5929 /*DirectInit*/ false); 5930 5931 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 5932 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 5933 ExprResult CombCondOp = 5934 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 5935 LastIteration.get(), CombUB.get()); 5936 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 5937 CombCondOp.get()); 5938 CombEUB = 5939 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 5940 5941 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 5942 // We expect to have at least 2 more parameters than the 'parallel' 5943 // directive does - the lower and upper bounds of the previous schedule. 5944 assert(CD->getNumParams() >= 4 && 5945 "Unexpected number of parameters in loop combined directive"); 5946 5947 // Set the proper type for the bounds given what we learned from the 5948 // enclosed loops. 5949 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 5950 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 5951 5952 // Previous lower and upper bounds are obtained from the region 5953 // parameters. 5954 PrevLB = 5955 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 5956 PrevUB = 5957 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 5958 } 5959 } 5960 5961 // Build the iteration variable and its initialization before loop. 5962 ExprResult IV; 5963 ExprResult Init, CombInit; 5964 { 5965 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 5966 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 5967 Expr *RHS = 5968 (isOpenMPWorksharingDirective(DKind) || 5969 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5970 ? LB.get() 5971 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5972 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 5973 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 5974 5975 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5976 Expr *CombRHS = 5977 (isOpenMPWorksharingDirective(DKind) || 5978 isOpenMPTaskLoopDirective(DKind) || 5979 isOpenMPDistributeDirective(DKind)) 5980 ? CombLB.get() 5981 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5982 CombInit = 5983 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 5984 CombInit = 5985 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 5986 } 5987 } 5988 5989 bool UseStrictCompare = 5990 RealVType->hasUnsignedIntegerRepresentation() && 5991 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 5992 return LIS.IsStrictCompare; 5993 }); 5994 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 5995 // unsigned IV)) for worksharing loops. 5996 SourceLocation CondLoc = AStmt->getBeginLoc(); 5997 Expr *BoundUB = UB.get(); 5998 if (UseStrictCompare) { 5999 BoundUB = 6000 SemaRef 6001 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 6002 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6003 .get(); 6004 BoundUB = 6005 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 6006 } 6007 ExprResult Cond = 6008 (isOpenMPWorksharingDirective(DKind) || 6009 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 6010 ? SemaRef.BuildBinOp(CurScope, CondLoc, 6011 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 6012 BoundUB) 6013 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 6014 NumIterations.get()); 6015 ExprResult CombDistCond; 6016 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6017 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 6018 NumIterations.get()); 6019 } 6020 6021 ExprResult CombCond; 6022 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6023 Expr *BoundCombUB = CombUB.get(); 6024 if (UseStrictCompare) { 6025 BoundCombUB = 6026 SemaRef 6027 .BuildBinOp( 6028 CurScope, CondLoc, BO_Add, BoundCombUB, 6029 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6030 .get(); 6031 BoundCombUB = 6032 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 6033 .get(); 6034 } 6035 CombCond = 6036 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 6037 IV.get(), BoundCombUB); 6038 } 6039 // Loop increment (IV = IV + 1) 6040 SourceLocation IncLoc = AStmt->getBeginLoc(); 6041 ExprResult Inc = 6042 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 6043 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 6044 if (!Inc.isUsable()) 6045 return 0; 6046 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 6047 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 6048 if (!Inc.isUsable()) 6049 return 0; 6050 6051 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 6052 // Used for directives with static scheduling. 6053 // In combined construct, add combined version that use CombLB and CombUB 6054 // base variables for the update 6055 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 6056 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 6057 isOpenMPDistributeDirective(DKind)) { 6058 // LB + ST 6059 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 6060 if (!NextLB.isUsable()) 6061 return 0; 6062 // LB = LB + ST 6063 NextLB = 6064 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 6065 NextLB = 6066 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 6067 if (!NextLB.isUsable()) 6068 return 0; 6069 // UB + ST 6070 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 6071 if (!NextUB.isUsable()) 6072 return 0; 6073 // UB = UB + ST 6074 NextUB = 6075 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 6076 NextUB = 6077 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 6078 if (!NextUB.isUsable()) 6079 return 0; 6080 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6081 CombNextLB = 6082 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 6083 if (!NextLB.isUsable()) 6084 return 0; 6085 // LB = LB + ST 6086 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 6087 CombNextLB.get()); 6088 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 6089 /*DiscardedValue*/ false); 6090 if (!CombNextLB.isUsable()) 6091 return 0; 6092 // UB + ST 6093 CombNextUB = 6094 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 6095 if (!CombNextUB.isUsable()) 6096 return 0; 6097 // UB = UB + ST 6098 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 6099 CombNextUB.get()); 6100 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 6101 /*DiscardedValue*/ false); 6102 if (!CombNextUB.isUsable()) 6103 return 0; 6104 } 6105 } 6106 6107 // Create increment expression for distribute loop when combined in a same 6108 // directive with for as IV = IV + ST; ensure upper bound expression based 6109 // on PrevUB instead of NumIterations - used to implement 'for' when found 6110 // in combination with 'distribute', like in 'distribute parallel for' 6111 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 6112 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 6113 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6114 DistCond = SemaRef.BuildBinOp( 6115 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 6116 assert(DistCond.isUsable() && "distribute cond expr was not built"); 6117 6118 DistInc = 6119 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 6120 assert(DistInc.isUsable() && "distribute inc expr was not built"); 6121 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 6122 DistInc.get()); 6123 DistInc = 6124 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 6125 assert(DistInc.isUsable() && "distribute inc expr was not built"); 6126 6127 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 6128 // construct 6129 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 6130 ExprResult IsUBGreater = 6131 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 6132 ExprResult CondOp = SemaRef.ActOnConditionalOp( 6133 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 6134 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 6135 CondOp.get()); 6136 PrevEUB = 6137 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 6138 6139 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 6140 // parallel for is in combination with a distribute directive with 6141 // schedule(static, 1) 6142 Expr *BoundPrevUB = PrevUB.get(); 6143 if (UseStrictCompare) { 6144 BoundPrevUB = 6145 SemaRef 6146 .BuildBinOp( 6147 CurScope, CondLoc, BO_Add, BoundPrevUB, 6148 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6149 .get(); 6150 BoundPrevUB = 6151 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 6152 .get(); 6153 } 6154 ParForInDistCond = 6155 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 6156 IV.get(), BoundPrevUB); 6157 } 6158 6159 // Build updates and final values of the loop counters. 6160 bool HasErrors = false; 6161 Built.Counters.resize(NestedLoopCount); 6162 Built.Inits.resize(NestedLoopCount); 6163 Built.Updates.resize(NestedLoopCount); 6164 Built.Finals.resize(NestedLoopCount); 6165 { 6166 // We implement the following algorithm for obtaining the 6167 // original loop iteration variable values based on the 6168 // value of the collapsed loop iteration variable IV. 6169 // 6170 // Let n+1 be the number of collapsed loops in the nest. 6171 // Iteration variables (I0, I1, .... In) 6172 // Iteration counts (N0, N1, ... Nn) 6173 // 6174 // Acc = IV; 6175 // 6176 // To compute Ik for loop k, 0 <= k <= n, generate: 6177 // Prod = N(k+1) * N(k+2) * ... * Nn; 6178 // Ik = Acc / Prod; 6179 // Acc -= Ik * Prod; 6180 // 6181 ExprResult Acc = IV; 6182 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 6183 LoopIterationSpace &IS = IterSpaces[Cnt]; 6184 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 6185 ExprResult Iter; 6186 6187 // Compute prod 6188 ExprResult Prod = 6189 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 6190 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 6191 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 6192 IterSpaces[K].NumIterations); 6193 6194 // Iter = Acc / Prod 6195 // If there is at least one more inner loop to avoid 6196 // multiplication by 1. 6197 if (Cnt + 1 < NestedLoopCount) 6198 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 6199 Acc.get(), Prod.get()); 6200 else 6201 Iter = Acc; 6202 if (!Iter.isUsable()) { 6203 HasErrors = true; 6204 break; 6205 } 6206 6207 // Update Acc: 6208 // Acc -= Iter * Prod 6209 // Check if there is at least one more inner loop to avoid 6210 // multiplication by 1. 6211 if (Cnt + 1 < NestedLoopCount) 6212 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 6213 Iter.get(), Prod.get()); 6214 else 6215 Prod = Iter; 6216 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 6217 Acc.get(), Prod.get()); 6218 6219 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 6220 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 6221 DeclRefExpr *CounterVar = buildDeclRefExpr( 6222 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 6223 /*RefersToCapture=*/true); 6224 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 6225 IS.CounterInit, Captures); 6226 if (!Init.isUsable()) { 6227 HasErrors = true; 6228 break; 6229 } 6230 ExprResult Update = buildCounterUpdate( 6231 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 6232 IS.CounterStep, IS.Subtract, &Captures); 6233 if (!Update.isUsable()) { 6234 HasErrors = true; 6235 break; 6236 } 6237 6238 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 6239 ExprResult Final = buildCounterUpdate( 6240 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 6241 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 6242 if (!Final.isUsable()) { 6243 HasErrors = true; 6244 break; 6245 } 6246 6247 if (!Update.isUsable() || !Final.isUsable()) { 6248 HasErrors = true; 6249 break; 6250 } 6251 // Save results 6252 Built.Counters[Cnt] = IS.CounterVar; 6253 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 6254 Built.Inits[Cnt] = Init.get(); 6255 Built.Updates[Cnt] = Update.get(); 6256 Built.Finals[Cnt] = Final.get(); 6257 } 6258 } 6259 6260 if (HasErrors) 6261 return 0; 6262 6263 // Save results 6264 Built.IterationVarRef = IV.get(); 6265 Built.LastIteration = LastIteration.get(); 6266 Built.NumIterations = NumIterations.get(); 6267 Built.CalcLastIteration = SemaRef 6268 .ActOnFinishFullExpr(CalcLastIteration.get(), 6269 /*DiscardedValue*/ false) 6270 .get(); 6271 Built.PreCond = PreCond.get(); 6272 Built.PreInits = buildPreInits(C, Captures); 6273 Built.Cond = Cond.get(); 6274 Built.Init = Init.get(); 6275 Built.Inc = Inc.get(); 6276 Built.LB = LB.get(); 6277 Built.UB = UB.get(); 6278 Built.IL = IL.get(); 6279 Built.ST = ST.get(); 6280 Built.EUB = EUB.get(); 6281 Built.NLB = NextLB.get(); 6282 Built.NUB = NextUB.get(); 6283 Built.PrevLB = PrevLB.get(); 6284 Built.PrevUB = PrevUB.get(); 6285 Built.DistInc = DistInc.get(); 6286 Built.PrevEUB = PrevEUB.get(); 6287 Built.DistCombinedFields.LB = CombLB.get(); 6288 Built.DistCombinedFields.UB = CombUB.get(); 6289 Built.DistCombinedFields.EUB = CombEUB.get(); 6290 Built.DistCombinedFields.Init = CombInit.get(); 6291 Built.DistCombinedFields.Cond = CombCond.get(); 6292 Built.DistCombinedFields.NLB = CombNextLB.get(); 6293 Built.DistCombinedFields.NUB = CombNextUB.get(); 6294 Built.DistCombinedFields.DistCond = CombDistCond.get(); 6295 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 6296 6297 return NestedLoopCount; 6298 } 6299 6300 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 6301 auto CollapseClauses = 6302 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 6303 if (CollapseClauses.begin() != CollapseClauses.end()) 6304 return (*CollapseClauses.begin())->getNumForLoops(); 6305 return nullptr; 6306 } 6307 6308 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 6309 auto OrderedClauses = 6310 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 6311 if (OrderedClauses.begin() != OrderedClauses.end()) 6312 return (*OrderedClauses.begin())->getNumForLoops(); 6313 return nullptr; 6314 } 6315 6316 static bool checkSimdlenSafelenSpecified(Sema &S, 6317 const ArrayRef<OMPClause *> Clauses) { 6318 const OMPSafelenClause *Safelen = nullptr; 6319 const OMPSimdlenClause *Simdlen = nullptr; 6320 6321 for (const OMPClause *Clause : Clauses) { 6322 if (Clause->getClauseKind() == OMPC_safelen) 6323 Safelen = cast<OMPSafelenClause>(Clause); 6324 else if (Clause->getClauseKind() == OMPC_simdlen) 6325 Simdlen = cast<OMPSimdlenClause>(Clause); 6326 if (Safelen && Simdlen) 6327 break; 6328 } 6329 6330 if (Simdlen && Safelen) { 6331 const Expr *SimdlenLength = Simdlen->getSimdlen(); 6332 const Expr *SafelenLength = Safelen->getSafelen(); 6333 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 6334 SimdlenLength->isInstantiationDependent() || 6335 SimdlenLength->containsUnexpandedParameterPack()) 6336 return false; 6337 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 6338 SafelenLength->isInstantiationDependent() || 6339 SafelenLength->containsUnexpandedParameterPack()) 6340 return false; 6341 Expr::EvalResult SimdlenResult, SafelenResult; 6342 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 6343 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 6344 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 6345 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 6346 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 6347 // If both simdlen and safelen clauses are specified, the value of the 6348 // simdlen parameter must be less than or equal to the value of the safelen 6349 // parameter. 6350 if (SimdlenRes > SafelenRes) { 6351 S.Diag(SimdlenLength->getExprLoc(), 6352 diag::err_omp_wrong_simdlen_safelen_values) 6353 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 6354 return true; 6355 } 6356 } 6357 return false; 6358 } 6359 6360 StmtResult 6361 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 6362 SourceLocation StartLoc, SourceLocation EndLoc, 6363 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6364 if (!AStmt) 6365 return StmtError(); 6366 6367 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6368 OMPLoopDirective::HelperExprs B; 6369 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6370 // define the nested loops number. 6371 unsigned NestedLoopCount = checkOpenMPLoop( 6372 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 6373 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 6374 if (NestedLoopCount == 0) 6375 return StmtError(); 6376 6377 assert((CurContext->isDependentContext() || B.builtAll()) && 6378 "omp simd loop exprs were not built"); 6379 6380 if (!CurContext->isDependentContext()) { 6381 // Finalize the clauses that need pre-built expressions for CodeGen. 6382 for (OMPClause *C : Clauses) { 6383 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6384 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6385 B.NumIterations, *this, CurScope, 6386 DSAStack)) 6387 return StmtError(); 6388 } 6389 } 6390 6391 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6392 return StmtError(); 6393 6394 setFunctionHasBranchProtectedScope(); 6395 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6396 Clauses, AStmt, B); 6397 } 6398 6399 StmtResult 6400 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 6401 SourceLocation StartLoc, SourceLocation EndLoc, 6402 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6403 if (!AStmt) 6404 return StmtError(); 6405 6406 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6407 OMPLoopDirective::HelperExprs B; 6408 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6409 // define the nested loops number. 6410 unsigned NestedLoopCount = checkOpenMPLoop( 6411 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 6412 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 6413 if (NestedLoopCount == 0) 6414 return StmtError(); 6415 6416 assert((CurContext->isDependentContext() || B.builtAll()) && 6417 "omp for loop exprs were not built"); 6418 6419 if (!CurContext->isDependentContext()) { 6420 // Finalize the clauses that need pre-built expressions for CodeGen. 6421 for (OMPClause *C : Clauses) { 6422 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6423 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6424 B.NumIterations, *this, CurScope, 6425 DSAStack)) 6426 return StmtError(); 6427 } 6428 } 6429 6430 setFunctionHasBranchProtectedScope(); 6431 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6432 Clauses, AStmt, B, DSAStack->isCancelRegion()); 6433 } 6434 6435 StmtResult Sema::ActOnOpenMPForSimdDirective( 6436 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6437 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6438 if (!AStmt) 6439 return StmtError(); 6440 6441 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6442 OMPLoopDirective::HelperExprs B; 6443 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6444 // define the nested loops number. 6445 unsigned NestedLoopCount = 6446 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 6447 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6448 VarsWithImplicitDSA, B); 6449 if (NestedLoopCount == 0) 6450 return StmtError(); 6451 6452 assert((CurContext->isDependentContext() || B.builtAll()) && 6453 "omp for simd loop exprs were not built"); 6454 6455 if (!CurContext->isDependentContext()) { 6456 // Finalize the clauses that need pre-built expressions for CodeGen. 6457 for (OMPClause *C : Clauses) { 6458 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6459 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6460 B.NumIterations, *this, CurScope, 6461 DSAStack)) 6462 return StmtError(); 6463 } 6464 } 6465 6466 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6467 return StmtError(); 6468 6469 setFunctionHasBranchProtectedScope(); 6470 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6471 Clauses, AStmt, B); 6472 } 6473 6474 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 6475 Stmt *AStmt, 6476 SourceLocation StartLoc, 6477 SourceLocation EndLoc) { 6478 if (!AStmt) 6479 return StmtError(); 6480 6481 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6482 auto BaseStmt = AStmt; 6483 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6484 BaseStmt = CS->getCapturedStmt(); 6485 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6486 auto S = C->children(); 6487 if (S.begin() == S.end()) 6488 return StmtError(); 6489 // All associated statements must be '#pragma omp section' except for 6490 // the first one. 6491 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6492 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6493 if (SectionStmt) 6494 Diag(SectionStmt->getBeginLoc(), 6495 diag::err_omp_sections_substmt_not_section); 6496 return StmtError(); 6497 } 6498 cast<OMPSectionDirective>(SectionStmt) 6499 ->setHasCancel(DSAStack->isCancelRegion()); 6500 } 6501 } else { 6502 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 6503 return StmtError(); 6504 } 6505 6506 setFunctionHasBranchProtectedScope(); 6507 6508 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6509 DSAStack->isCancelRegion()); 6510 } 6511 6512 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 6513 SourceLocation StartLoc, 6514 SourceLocation EndLoc) { 6515 if (!AStmt) 6516 return StmtError(); 6517 6518 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6519 6520 setFunctionHasBranchProtectedScope(); 6521 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 6522 6523 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 6524 DSAStack->isCancelRegion()); 6525 } 6526 6527 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 6528 Stmt *AStmt, 6529 SourceLocation StartLoc, 6530 SourceLocation EndLoc) { 6531 if (!AStmt) 6532 return StmtError(); 6533 6534 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6535 6536 setFunctionHasBranchProtectedScope(); 6537 6538 // OpenMP [2.7.3, single Construct, Restrictions] 6539 // The copyprivate clause must not be used with the nowait clause. 6540 const OMPClause *Nowait = nullptr; 6541 const OMPClause *Copyprivate = nullptr; 6542 for (const OMPClause *Clause : Clauses) { 6543 if (Clause->getClauseKind() == OMPC_nowait) 6544 Nowait = Clause; 6545 else if (Clause->getClauseKind() == OMPC_copyprivate) 6546 Copyprivate = Clause; 6547 if (Copyprivate && Nowait) { 6548 Diag(Copyprivate->getBeginLoc(), 6549 diag::err_omp_single_copyprivate_with_nowait); 6550 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 6551 return StmtError(); 6552 } 6553 } 6554 6555 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6556 } 6557 6558 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 6559 SourceLocation StartLoc, 6560 SourceLocation EndLoc) { 6561 if (!AStmt) 6562 return StmtError(); 6563 6564 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6565 6566 setFunctionHasBranchProtectedScope(); 6567 6568 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 6569 } 6570 6571 StmtResult Sema::ActOnOpenMPCriticalDirective( 6572 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 6573 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 6574 if (!AStmt) 6575 return StmtError(); 6576 6577 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6578 6579 bool ErrorFound = false; 6580 llvm::APSInt Hint; 6581 SourceLocation HintLoc; 6582 bool DependentHint = false; 6583 for (const OMPClause *C : Clauses) { 6584 if (C->getClauseKind() == OMPC_hint) { 6585 if (!DirName.getName()) { 6586 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 6587 ErrorFound = true; 6588 } 6589 Expr *E = cast<OMPHintClause>(C)->getHint(); 6590 if (E->isTypeDependent() || E->isValueDependent() || 6591 E->isInstantiationDependent()) { 6592 DependentHint = true; 6593 } else { 6594 Hint = E->EvaluateKnownConstInt(Context); 6595 HintLoc = C->getBeginLoc(); 6596 } 6597 } 6598 } 6599 if (ErrorFound) 6600 return StmtError(); 6601 const auto Pair = DSAStack->getCriticalWithHint(DirName); 6602 if (Pair.first && DirName.getName() && !DependentHint) { 6603 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 6604 Diag(StartLoc, diag::err_omp_critical_with_hint); 6605 if (HintLoc.isValid()) 6606 Diag(HintLoc, diag::note_omp_critical_hint_here) 6607 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 6608 else 6609 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 6610 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 6611 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 6612 << 1 6613 << C->getHint()->EvaluateKnownConstInt(Context).toString( 6614 /*Radix=*/10, /*Signed=*/false); 6615 } else { 6616 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 6617 } 6618 } 6619 } 6620 6621 setFunctionHasBranchProtectedScope(); 6622 6623 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 6624 Clauses, AStmt); 6625 if (!Pair.first && DirName.getName() && !DependentHint) 6626 DSAStack->addCriticalWithHint(Dir, Hint); 6627 return Dir; 6628 } 6629 6630 StmtResult Sema::ActOnOpenMPParallelForDirective( 6631 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6632 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6633 if (!AStmt) 6634 return StmtError(); 6635 6636 auto *CS = cast<CapturedStmt>(AStmt); 6637 // 1.2.2 OpenMP Language Terminology 6638 // Structured block - An executable statement with a single entry at the 6639 // top and a single exit at the bottom. 6640 // The point of exit cannot be a branch out of the structured block. 6641 // longjmp() and throw() must not violate the entry/exit criteria. 6642 CS->getCapturedDecl()->setNothrow(); 6643 6644 OMPLoopDirective::HelperExprs B; 6645 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6646 // define the nested loops number. 6647 unsigned NestedLoopCount = 6648 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 6649 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6650 VarsWithImplicitDSA, B); 6651 if (NestedLoopCount == 0) 6652 return StmtError(); 6653 6654 assert((CurContext->isDependentContext() || B.builtAll()) && 6655 "omp parallel for loop exprs were not built"); 6656 6657 if (!CurContext->isDependentContext()) { 6658 // Finalize the clauses that need pre-built expressions for CodeGen. 6659 for (OMPClause *C : Clauses) { 6660 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6661 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6662 B.NumIterations, *this, CurScope, 6663 DSAStack)) 6664 return StmtError(); 6665 } 6666 } 6667 6668 setFunctionHasBranchProtectedScope(); 6669 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 6670 NestedLoopCount, Clauses, AStmt, B, 6671 DSAStack->isCancelRegion()); 6672 } 6673 6674 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 6675 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6676 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6677 if (!AStmt) 6678 return StmtError(); 6679 6680 auto *CS = cast<CapturedStmt>(AStmt); 6681 // 1.2.2 OpenMP Language Terminology 6682 // Structured block - An executable statement with a single entry at the 6683 // top and a single exit at the bottom. 6684 // The point of exit cannot be a branch out of the structured block. 6685 // longjmp() and throw() must not violate the entry/exit criteria. 6686 CS->getCapturedDecl()->setNothrow(); 6687 6688 OMPLoopDirective::HelperExprs B; 6689 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6690 // define the nested loops number. 6691 unsigned NestedLoopCount = 6692 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 6693 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6694 VarsWithImplicitDSA, B); 6695 if (NestedLoopCount == 0) 6696 return StmtError(); 6697 6698 if (!CurContext->isDependentContext()) { 6699 // Finalize the clauses that need pre-built expressions for CodeGen. 6700 for (OMPClause *C : Clauses) { 6701 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6702 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6703 B.NumIterations, *this, CurScope, 6704 DSAStack)) 6705 return StmtError(); 6706 } 6707 } 6708 6709 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6710 return StmtError(); 6711 6712 setFunctionHasBranchProtectedScope(); 6713 return OMPParallelForSimdDirective::Create( 6714 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6715 } 6716 6717 StmtResult 6718 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 6719 Stmt *AStmt, SourceLocation StartLoc, 6720 SourceLocation EndLoc) { 6721 if (!AStmt) 6722 return StmtError(); 6723 6724 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6725 auto BaseStmt = AStmt; 6726 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6727 BaseStmt = CS->getCapturedStmt(); 6728 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6729 auto S = C->children(); 6730 if (S.begin() == S.end()) 6731 return StmtError(); 6732 // All associated statements must be '#pragma omp section' except for 6733 // the first one. 6734 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6735 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6736 if (SectionStmt) 6737 Diag(SectionStmt->getBeginLoc(), 6738 diag::err_omp_parallel_sections_substmt_not_section); 6739 return StmtError(); 6740 } 6741 cast<OMPSectionDirective>(SectionStmt) 6742 ->setHasCancel(DSAStack->isCancelRegion()); 6743 } 6744 } else { 6745 Diag(AStmt->getBeginLoc(), 6746 diag::err_omp_parallel_sections_not_compound_stmt); 6747 return StmtError(); 6748 } 6749 6750 setFunctionHasBranchProtectedScope(); 6751 6752 return OMPParallelSectionsDirective::Create( 6753 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 6754 } 6755 6756 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 6757 Stmt *AStmt, SourceLocation StartLoc, 6758 SourceLocation EndLoc) { 6759 if (!AStmt) 6760 return StmtError(); 6761 6762 auto *CS = cast<CapturedStmt>(AStmt); 6763 // 1.2.2 OpenMP Language Terminology 6764 // Structured block - An executable statement with a single entry at the 6765 // top and a single exit at the bottom. 6766 // The point of exit cannot be a branch out of the structured block. 6767 // longjmp() and throw() must not violate the entry/exit criteria. 6768 CS->getCapturedDecl()->setNothrow(); 6769 6770 setFunctionHasBranchProtectedScope(); 6771 6772 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6773 DSAStack->isCancelRegion()); 6774 } 6775 6776 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 6777 SourceLocation EndLoc) { 6778 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 6779 } 6780 6781 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 6782 SourceLocation EndLoc) { 6783 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 6784 } 6785 6786 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 6787 SourceLocation EndLoc) { 6788 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 6789 } 6790 6791 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 6792 Stmt *AStmt, 6793 SourceLocation StartLoc, 6794 SourceLocation EndLoc) { 6795 if (!AStmt) 6796 return StmtError(); 6797 6798 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6799 6800 setFunctionHasBranchProtectedScope(); 6801 6802 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 6803 AStmt, 6804 DSAStack->getTaskgroupReductionRef()); 6805 } 6806 6807 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 6808 SourceLocation StartLoc, 6809 SourceLocation EndLoc) { 6810 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 6811 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 6812 } 6813 6814 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 6815 Stmt *AStmt, 6816 SourceLocation StartLoc, 6817 SourceLocation EndLoc) { 6818 const OMPClause *DependFound = nullptr; 6819 const OMPClause *DependSourceClause = nullptr; 6820 const OMPClause *DependSinkClause = nullptr; 6821 bool ErrorFound = false; 6822 const OMPThreadsClause *TC = nullptr; 6823 const OMPSIMDClause *SC = nullptr; 6824 for (const OMPClause *C : Clauses) { 6825 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 6826 DependFound = C; 6827 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 6828 if (DependSourceClause) { 6829 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 6830 << getOpenMPDirectiveName(OMPD_ordered) 6831 << getOpenMPClauseName(OMPC_depend) << 2; 6832 ErrorFound = true; 6833 } else { 6834 DependSourceClause = C; 6835 } 6836 if (DependSinkClause) { 6837 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6838 << 0; 6839 ErrorFound = true; 6840 } 6841 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 6842 if (DependSourceClause) { 6843 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6844 << 1; 6845 ErrorFound = true; 6846 } 6847 DependSinkClause = C; 6848 } 6849 } else if (C->getClauseKind() == OMPC_threads) { 6850 TC = cast<OMPThreadsClause>(C); 6851 } else if (C->getClauseKind() == OMPC_simd) { 6852 SC = cast<OMPSIMDClause>(C); 6853 } 6854 } 6855 if (!ErrorFound && !SC && 6856 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 6857 // OpenMP [2.8.1,simd Construct, Restrictions] 6858 // An ordered construct with the simd clause is the only OpenMP construct 6859 // that can appear in the simd region. 6860 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 6861 ErrorFound = true; 6862 } else if (DependFound && (TC || SC)) { 6863 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 6864 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 6865 ErrorFound = true; 6866 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 6867 Diag(DependFound->getBeginLoc(), 6868 diag::err_omp_ordered_directive_without_param); 6869 ErrorFound = true; 6870 } else if (TC || Clauses.empty()) { 6871 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 6872 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 6873 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 6874 << (TC != nullptr); 6875 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param); 6876 ErrorFound = true; 6877 } 6878 } 6879 if ((!AStmt && !DependFound) || ErrorFound) 6880 return StmtError(); 6881 6882 if (AStmt) { 6883 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6884 6885 setFunctionHasBranchProtectedScope(); 6886 } 6887 6888 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6889 } 6890 6891 namespace { 6892 /// Helper class for checking expression in 'omp atomic [update]' 6893 /// construct. 6894 class OpenMPAtomicUpdateChecker { 6895 /// Error results for atomic update expressions. 6896 enum ExprAnalysisErrorCode { 6897 /// A statement is not an expression statement. 6898 NotAnExpression, 6899 /// Expression is not builtin binary or unary operation. 6900 NotABinaryOrUnaryExpression, 6901 /// Unary operation is not post-/pre- increment/decrement operation. 6902 NotAnUnaryIncDecExpression, 6903 /// An expression is not of scalar type. 6904 NotAScalarType, 6905 /// A binary operation is not an assignment operation. 6906 NotAnAssignmentOp, 6907 /// RHS part of the binary operation is not a binary expression. 6908 NotABinaryExpression, 6909 /// RHS part is not additive/multiplicative/shift/biwise binary 6910 /// expression. 6911 NotABinaryOperator, 6912 /// RHS binary operation does not have reference to the updated LHS 6913 /// part. 6914 NotAnUpdateExpression, 6915 /// No errors is found. 6916 NoError 6917 }; 6918 /// Reference to Sema. 6919 Sema &SemaRef; 6920 /// A location for note diagnostics (when error is found). 6921 SourceLocation NoteLoc; 6922 /// 'x' lvalue part of the source atomic expression. 6923 Expr *X; 6924 /// 'expr' rvalue part of the source atomic expression. 6925 Expr *E; 6926 /// Helper expression of the form 6927 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6928 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6929 Expr *UpdateExpr; 6930 /// Is 'x' a LHS in a RHS part of full update expression. It is 6931 /// important for non-associative operations. 6932 bool IsXLHSInRHSPart; 6933 BinaryOperatorKind Op; 6934 SourceLocation OpLoc; 6935 /// true if the source expression is a postfix unary operation, false 6936 /// if it is a prefix unary operation. 6937 bool IsPostfixUpdate; 6938 6939 public: 6940 OpenMPAtomicUpdateChecker(Sema &SemaRef) 6941 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 6942 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 6943 /// Check specified statement that it is suitable for 'atomic update' 6944 /// constructs and extract 'x', 'expr' and Operation from the original 6945 /// expression. If DiagId and NoteId == 0, then only check is performed 6946 /// without error notification. 6947 /// \param DiagId Diagnostic which should be emitted if error is found. 6948 /// \param NoteId Diagnostic note for the main error message. 6949 /// \return true if statement is not an update expression, false otherwise. 6950 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 6951 /// Return the 'x' lvalue part of the source atomic expression. 6952 Expr *getX() const { return X; } 6953 /// Return the 'expr' rvalue part of the source atomic expression. 6954 Expr *getExpr() const { return E; } 6955 /// Return the update expression used in calculation of the updated 6956 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6957 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6958 Expr *getUpdateExpr() const { return UpdateExpr; } 6959 /// Return true if 'x' is LHS in RHS part of full update expression, 6960 /// false otherwise. 6961 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 6962 6963 /// true if the source expression is a postfix unary operation, false 6964 /// if it is a prefix unary operation. 6965 bool isPostfixUpdate() const { return IsPostfixUpdate; } 6966 6967 private: 6968 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 6969 unsigned NoteId = 0); 6970 }; 6971 } // namespace 6972 6973 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 6974 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 6975 ExprAnalysisErrorCode ErrorFound = NoError; 6976 SourceLocation ErrorLoc, NoteLoc; 6977 SourceRange ErrorRange, NoteRange; 6978 // Allowed constructs are: 6979 // x = x binop expr; 6980 // x = expr binop x; 6981 if (AtomicBinOp->getOpcode() == BO_Assign) { 6982 X = AtomicBinOp->getLHS(); 6983 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 6984 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 6985 if (AtomicInnerBinOp->isMultiplicativeOp() || 6986 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 6987 AtomicInnerBinOp->isBitwiseOp()) { 6988 Op = AtomicInnerBinOp->getOpcode(); 6989 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 6990 Expr *LHS = AtomicInnerBinOp->getLHS(); 6991 Expr *RHS = AtomicInnerBinOp->getRHS(); 6992 llvm::FoldingSetNodeID XId, LHSId, RHSId; 6993 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 6994 /*Canonical=*/true); 6995 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 6996 /*Canonical=*/true); 6997 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 6998 /*Canonical=*/true); 6999 if (XId == LHSId) { 7000 E = RHS; 7001 IsXLHSInRHSPart = true; 7002 } else if (XId == RHSId) { 7003 E = LHS; 7004 IsXLHSInRHSPart = false; 7005 } else { 7006 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 7007 ErrorRange = AtomicInnerBinOp->getSourceRange(); 7008 NoteLoc = X->getExprLoc(); 7009 NoteRange = X->getSourceRange(); 7010 ErrorFound = NotAnUpdateExpression; 7011 } 7012 } else { 7013 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 7014 ErrorRange = AtomicInnerBinOp->getSourceRange(); 7015 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 7016 NoteRange = SourceRange(NoteLoc, NoteLoc); 7017 ErrorFound = NotABinaryOperator; 7018 } 7019 } else { 7020 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 7021 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 7022 ErrorFound = NotABinaryExpression; 7023 } 7024 } else { 7025 ErrorLoc = AtomicBinOp->getExprLoc(); 7026 ErrorRange = AtomicBinOp->getSourceRange(); 7027 NoteLoc = AtomicBinOp->getOperatorLoc(); 7028 NoteRange = SourceRange(NoteLoc, NoteLoc); 7029 ErrorFound = NotAnAssignmentOp; 7030 } 7031 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 7032 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 7033 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 7034 return true; 7035 } 7036 if (SemaRef.CurContext->isDependentContext()) 7037 E = X = UpdateExpr = nullptr; 7038 return ErrorFound != NoError; 7039 } 7040 7041 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 7042 unsigned NoteId) { 7043 ExprAnalysisErrorCode ErrorFound = NoError; 7044 SourceLocation ErrorLoc, NoteLoc; 7045 SourceRange ErrorRange, NoteRange; 7046 // Allowed constructs are: 7047 // x++; 7048 // x--; 7049 // ++x; 7050 // --x; 7051 // x binop= expr; 7052 // x = x binop expr; 7053 // x = expr binop x; 7054 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 7055 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 7056 if (AtomicBody->getType()->isScalarType() || 7057 AtomicBody->isInstantiationDependent()) { 7058 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 7059 AtomicBody->IgnoreParenImpCasts())) { 7060 // Check for Compound Assignment Operation 7061 Op = BinaryOperator::getOpForCompoundAssignment( 7062 AtomicCompAssignOp->getOpcode()); 7063 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 7064 E = AtomicCompAssignOp->getRHS(); 7065 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 7066 IsXLHSInRHSPart = true; 7067 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 7068 AtomicBody->IgnoreParenImpCasts())) { 7069 // Check for Binary Operation 7070 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 7071 return true; 7072 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 7073 AtomicBody->IgnoreParenImpCasts())) { 7074 // Check for Unary Operation 7075 if (AtomicUnaryOp->isIncrementDecrementOp()) { 7076 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 7077 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 7078 OpLoc = AtomicUnaryOp->getOperatorLoc(); 7079 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 7080 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 7081 IsXLHSInRHSPart = true; 7082 } else { 7083 ErrorFound = NotAnUnaryIncDecExpression; 7084 ErrorLoc = AtomicUnaryOp->getExprLoc(); 7085 ErrorRange = AtomicUnaryOp->getSourceRange(); 7086 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 7087 NoteRange = SourceRange(NoteLoc, NoteLoc); 7088 } 7089 } else if (!AtomicBody->isInstantiationDependent()) { 7090 ErrorFound = NotABinaryOrUnaryExpression; 7091 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 7092 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 7093 } 7094 } else { 7095 ErrorFound = NotAScalarType; 7096 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 7097 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7098 } 7099 } else { 7100 ErrorFound = NotAnExpression; 7101 NoteLoc = ErrorLoc = S->getBeginLoc(); 7102 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7103 } 7104 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 7105 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 7106 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 7107 return true; 7108 } 7109 if (SemaRef.CurContext->isDependentContext()) 7110 E = X = UpdateExpr = nullptr; 7111 if (ErrorFound == NoError && E && X) { 7112 // Build an update expression of form 'OpaqueValueExpr(x) binop 7113 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 7114 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 7115 auto *OVEX = new (SemaRef.getASTContext()) 7116 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 7117 auto *OVEExpr = new (SemaRef.getASTContext()) 7118 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 7119 ExprResult Update = 7120 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 7121 IsXLHSInRHSPart ? OVEExpr : OVEX); 7122 if (Update.isInvalid()) 7123 return true; 7124 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 7125 Sema::AA_Casting); 7126 if (Update.isInvalid()) 7127 return true; 7128 UpdateExpr = Update.get(); 7129 } 7130 return ErrorFound != NoError; 7131 } 7132 7133 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 7134 Stmt *AStmt, 7135 SourceLocation StartLoc, 7136 SourceLocation EndLoc) { 7137 if (!AStmt) 7138 return StmtError(); 7139 7140 auto *CS = cast<CapturedStmt>(AStmt); 7141 // 1.2.2 OpenMP Language Terminology 7142 // Structured block - An executable statement with a single entry at the 7143 // top and a single exit at the bottom. 7144 // The point of exit cannot be a branch out of the structured block. 7145 // longjmp() and throw() must not violate the entry/exit criteria. 7146 OpenMPClauseKind AtomicKind = OMPC_unknown; 7147 SourceLocation AtomicKindLoc; 7148 for (const OMPClause *C : Clauses) { 7149 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 7150 C->getClauseKind() == OMPC_update || 7151 C->getClauseKind() == OMPC_capture) { 7152 if (AtomicKind != OMPC_unknown) { 7153 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 7154 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 7155 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 7156 << getOpenMPClauseName(AtomicKind); 7157 } else { 7158 AtomicKind = C->getClauseKind(); 7159 AtomicKindLoc = C->getBeginLoc(); 7160 } 7161 } 7162 } 7163 7164 Stmt *Body = CS->getCapturedStmt(); 7165 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 7166 Body = EWC->getSubExpr(); 7167 7168 Expr *X = nullptr; 7169 Expr *V = nullptr; 7170 Expr *E = nullptr; 7171 Expr *UE = nullptr; 7172 bool IsXLHSInRHSPart = false; 7173 bool IsPostfixUpdate = false; 7174 // OpenMP [2.12.6, atomic Construct] 7175 // In the next expressions: 7176 // * x and v (as applicable) are both l-value expressions with scalar type. 7177 // * During the execution of an atomic region, multiple syntactic 7178 // occurrences of x must designate the same storage location. 7179 // * Neither of v and expr (as applicable) may access the storage location 7180 // designated by x. 7181 // * Neither of x and expr (as applicable) may access the storage location 7182 // designated by v. 7183 // * expr is an expression with scalar type. 7184 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 7185 // * binop, binop=, ++, and -- are not overloaded operators. 7186 // * The expression x binop expr must be numerically equivalent to x binop 7187 // (expr). This requirement is satisfied if the operators in expr have 7188 // precedence greater than binop, or by using parentheses around expr or 7189 // subexpressions of expr. 7190 // * The expression expr binop x must be numerically equivalent to (expr) 7191 // binop x. This requirement is satisfied if the operators in expr have 7192 // precedence equal to or greater than binop, or by using parentheses around 7193 // expr or subexpressions of expr. 7194 // * For forms that allow multiple occurrences of x, the number of times 7195 // that x is evaluated is unspecified. 7196 if (AtomicKind == OMPC_read) { 7197 enum { 7198 NotAnExpression, 7199 NotAnAssignmentOp, 7200 NotAScalarType, 7201 NotAnLValue, 7202 NoError 7203 } ErrorFound = NoError; 7204 SourceLocation ErrorLoc, NoteLoc; 7205 SourceRange ErrorRange, NoteRange; 7206 // If clause is read: 7207 // v = x; 7208 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7209 const auto *AtomicBinOp = 7210 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7211 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7212 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 7213 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 7214 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 7215 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 7216 if (!X->isLValue() || !V->isLValue()) { 7217 const Expr *NotLValueExpr = X->isLValue() ? V : X; 7218 ErrorFound = NotAnLValue; 7219 ErrorLoc = AtomicBinOp->getExprLoc(); 7220 ErrorRange = AtomicBinOp->getSourceRange(); 7221 NoteLoc = NotLValueExpr->getExprLoc(); 7222 NoteRange = NotLValueExpr->getSourceRange(); 7223 } 7224 } else if (!X->isInstantiationDependent() || 7225 !V->isInstantiationDependent()) { 7226 const Expr *NotScalarExpr = 7227 (X->isInstantiationDependent() || X->getType()->isScalarType()) 7228 ? V 7229 : X; 7230 ErrorFound = NotAScalarType; 7231 ErrorLoc = AtomicBinOp->getExprLoc(); 7232 ErrorRange = AtomicBinOp->getSourceRange(); 7233 NoteLoc = NotScalarExpr->getExprLoc(); 7234 NoteRange = NotScalarExpr->getSourceRange(); 7235 } 7236 } else if (!AtomicBody->isInstantiationDependent()) { 7237 ErrorFound = NotAnAssignmentOp; 7238 ErrorLoc = AtomicBody->getExprLoc(); 7239 ErrorRange = AtomicBody->getSourceRange(); 7240 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7241 : AtomicBody->getExprLoc(); 7242 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7243 : AtomicBody->getSourceRange(); 7244 } 7245 } else { 7246 ErrorFound = NotAnExpression; 7247 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7248 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7249 } 7250 if (ErrorFound != NoError) { 7251 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 7252 << ErrorRange; 7253 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 7254 << NoteRange; 7255 return StmtError(); 7256 } 7257 if (CurContext->isDependentContext()) 7258 V = X = nullptr; 7259 } else if (AtomicKind == OMPC_write) { 7260 enum { 7261 NotAnExpression, 7262 NotAnAssignmentOp, 7263 NotAScalarType, 7264 NotAnLValue, 7265 NoError 7266 } ErrorFound = NoError; 7267 SourceLocation ErrorLoc, NoteLoc; 7268 SourceRange ErrorRange, NoteRange; 7269 // If clause is write: 7270 // x = expr; 7271 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7272 const auto *AtomicBinOp = 7273 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7274 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7275 X = AtomicBinOp->getLHS(); 7276 E = AtomicBinOp->getRHS(); 7277 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 7278 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 7279 if (!X->isLValue()) { 7280 ErrorFound = NotAnLValue; 7281 ErrorLoc = AtomicBinOp->getExprLoc(); 7282 ErrorRange = AtomicBinOp->getSourceRange(); 7283 NoteLoc = X->getExprLoc(); 7284 NoteRange = X->getSourceRange(); 7285 } 7286 } else if (!X->isInstantiationDependent() || 7287 !E->isInstantiationDependent()) { 7288 const Expr *NotScalarExpr = 7289 (X->isInstantiationDependent() || X->getType()->isScalarType()) 7290 ? E 7291 : X; 7292 ErrorFound = NotAScalarType; 7293 ErrorLoc = AtomicBinOp->getExprLoc(); 7294 ErrorRange = AtomicBinOp->getSourceRange(); 7295 NoteLoc = NotScalarExpr->getExprLoc(); 7296 NoteRange = NotScalarExpr->getSourceRange(); 7297 } 7298 } else if (!AtomicBody->isInstantiationDependent()) { 7299 ErrorFound = NotAnAssignmentOp; 7300 ErrorLoc = AtomicBody->getExprLoc(); 7301 ErrorRange = AtomicBody->getSourceRange(); 7302 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7303 : AtomicBody->getExprLoc(); 7304 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7305 : AtomicBody->getSourceRange(); 7306 } 7307 } else { 7308 ErrorFound = NotAnExpression; 7309 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7310 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7311 } 7312 if (ErrorFound != NoError) { 7313 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 7314 << ErrorRange; 7315 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 7316 << NoteRange; 7317 return StmtError(); 7318 } 7319 if (CurContext->isDependentContext()) 7320 E = X = nullptr; 7321 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 7322 // If clause is update: 7323 // x++; 7324 // x--; 7325 // ++x; 7326 // --x; 7327 // x binop= expr; 7328 // x = x binop expr; 7329 // x = expr binop x; 7330 OpenMPAtomicUpdateChecker Checker(*this); 7331 if (Checker.checkStatement( 7332 Body, (AtomicKind == OMPC_update) 7333 ? diag::err_omp_atomic_update_not_expression_statement 7334 : diag::err_omp_atomic_not_expression_statement, 7335 diag::note_omp_atomic_update)) 7336 return StmtError(); 7337 if (!CurContext->isDependentContext()) { 7338 E = Checker.getExpr(); 7339 X = Checker.getX(); 7340 UE = Checker.getUpdateExpr(); 7341 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7342 } 7343 } else if (AtomicKind == OMPC_capture) { 7344 enum { 7345 NotAnAssignmentOp, 7346 NotACompoundStatement, 7347 NotTwoSubstatements, 7348 NotASpecificExpression, 7349 NoError 7350 } ErrorFound = NoError; 7351 SourceLocation ErrorLoc, NoteLoc; 7352 SourceRange ErrorRange, NoteRange; 7353 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7354 // If clause is a capture: 7355 // v = x++; 7356 // v = x--; 7357 // v = ++x; 7358 // v = --x; 7359 // v = x binop= expr; 7360 // v = x = x binop expr; 7361 // v = x = expr binop x; 7362 const auto *AtomicBinOp = 7363 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7364 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7365 V = AtomicBinOp->getLHS(); 7366 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 7367 OpenMPAtomicUpdateChecker Checker(*this); 7368 if (Checker.checkStatement( 7369 Body, diag::err_omp_atomic_capture_not_expression_statement, 7370 diag::note_omp_atomic_update)) 7371 return StmtError(); 7372 E = Checker.getExpr(); 7373 X = Checker.getX(); 7374 UE = Checker.getUpdateExpr(); 7375 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7376 IsPostfixUpdate = Checker.isPostfixUpdate(); 7377 } else if (!AtomicBody->isInstantiationDependent()) { 7378 ErrorLoc = AtomicBody->getExprLoc(); 7379 ErrorRange = AtomicBody->getSourceRange(); 7380 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7381 : AtomicBody->getExprLoc(); 7382 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7383 : AtomicBody->getSourceRange(); 7384 ErrorFound = NotAnAssignmentOp; 7385 } 7386 if (ErrorFound != NoError) { 7387 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 7388 << ErrorRange; 7389 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 7390 return StmtError(); 7391 } 7392 if (CurContext->isDependentContext()) 7393 UE = V = E = X = nullptr; 7394 } else { 7395 // If clause is a capture: 7396 // { v = x; x = expr; } 7397 // { v = x; x++; } 7398 // { v = x; x--; } 7399 // { v = x; ++x; } 7400 // { v = x; --x; } 7401 // { v = x; x binop= expr; } 7402 // { v = x; x = x binop expr; } 7403 // { v = x; x = expr binop x; } 7404 // { x++; v = x; } 7405 // { x--; v = x; } 7406 // { ++x; v = x; } 7407 // { --x; v = x; } 7408 // { x binop= expr; v = x; } 7409 // { x = x binop expr; v = x; } 7410 // { x = expr binop x; v = x; } 7411 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 7412 // Check that this is { expr1; expr2; } 7413 if (CS->size() == 2) { 7414 Stmt *First = CS->body_front(); 7415 Stmt *Second = CS->body_back(); 7416 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 7417 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 7418 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 7419 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 7420 // Need to find what subexpression is 'v' and what is 'x'. 7421 OpenMPAtomicUpdateChecker Checker(*this); 7422 bool IsUpdateExprFound = !Checker.checkStatement(Second); 7423 BinaryOperator *BinOp = nullptr; 7424 if (IsUpdateExprFound) { 7425 BinOp = dyn_cast<BinaryOperator>(First); 7426 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 7427 } 7428 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 7429 // { v = x; x++; } 7430 // { v = x; x--; } 7431 // { v = x; ++x; } 7432 // { v = x; --x; } 7433 // { v = x; x binop= expr; } 7434 // { v = x; x = x binop expr; } 7435 // { v = x; x = expr binop x; } 7436 // Check that the first expression has form v = x. 7437 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 7438 llvm::FoldingSetNodeID XId, PossibleXId; 7439 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 7440 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 7441 IsUpdateExprFound = XId == PossibleXId; 7442 if (IsUpdateExprFound) { 7443 V = BinOp->getLHS(); 7444 X = Checker.getX(); 7445 E = Checker.getExpr(); 7446 UE = Checker.getUpdateExpr(); 7447 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7448 IsPostfixUpdate = true; 7449 } 7450 } 7451 if (!IsUpdateExprFound) { 7452 IsUpdateExprFound = !Checker.checkStatement(First); 7453 BinOp = nullptr; 7454 if (IsUpdateExprFound) { 7455 BinOp = dyn_cast<BinaryOperator>(Second); 7456 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 7457 } 7458 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 7459 // { x++; v = x; } 7460 // { x--; v = x; } 7461 // { ++x; v = x; } 7462 // { --x; v = x; } 7463 // { x binop= expr; v = x; } 7464 // { x = x binop expr; v = x; } 7465 // { x = expr binop x; v = x; } 7466 // Check that the second expression has form v = x. 7467 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 7468 llvm::FoldingSetNodeID XId, PossibleXId; 7469 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 7470 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 7471 IsUpdateExprFound = XId == PossibleXId; 7472 if (IsUpdateExprFound) { 7473 V = BinOp->getLHS(); 7474 X = Checker.getX(); 7475 E = Checker.getExpr(); 7476 UE = Checker.getUpdateExpr(); 7477 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7478 IsPostfixUpdate = false; 7479 } 7480 } 7481 } 7482 if (!IsUpdateExprFound) { 7483 // { v = x; x = expr; } 7484 auto *FirstExpr = dyn_cast<Expr>(First); 7485 auto *SecondExpr = dyn_cast<Expr>(Second); 7486 if (!FirstExpr || !SecondExpr || 7487 !(FirstExpr->isInstantiationDependent() || 7488 SecondExpr->isInstantiationDependent())) { 7489 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 7490 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 7491 ErrorFound = NotAnAssignmentOp; 7492 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 7493 : First->getBeginLoc(); 7494 NoteRange = ErrorRange = FirstBinOp 7495 ? FirstBinOp->getSourceRange() 7496 : SourceRange(ErrorLoc, ErrorLoc); 7497 } else { 7498 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 7499 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 7500 ErrorFound = NotAnAssignmentOp; 7501 NoteLoc = ErrorLoc = SecondBinOp 7502 ? SecondBinOp->getOperatorLoc() 7503 : Second->getBeginLoc(); 7504 NoteRange = ErrorRange = 7505 SecondBinOp ? SecondBinOp->getSourceRange() 7506 : SourceRange(ErrorLoc, ErrorLoc); 7507 } else { 7508 Expr *PossibleXRHSInFirst = 7509 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 7510 Expr *PossibleXLHSInSecond = 7511 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 7512 llvm::FoldingSetNodeID X1Id, X2Id; 7513 PossibleXRHSInFirst->Profile(X1Id, Context, 7514 /*Canonical=*/true); 7515 PossibleXLHSInSecond->Profile(X2Id, Context, 7516 /*Canonical=*/true); 7517 IsUpdateExprFound = X1Id == X2Id; 7518 if (IsUpdateExprFound) { 7519 V = FirstBinOp->getLHS(); 7520 X = SecondBinOp->getLHS(); 7521 E = SecondBinOp->getRHS(); 7522 UE = nullptr; 7523 IsXLHSInRHSPart = false; 7524 IsPostfixUpdate = true; 7525 } else { 7526 ErrorFound = NotASpecificExpression; 7527 ErrorLoc = FirstBinOp->getExprLoc(); 7528 ErrorRange = FirstBinOp->getSourceRange(); 7529 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 7530 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 7531 } 7532 } 7533 } 7534 } 7535 } 7536 } else { 7537 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7538 NoteRange = ErrorRange = 7539 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7540 ErrorFound = NotTwoSubstatements; 7541 } 7542 } else { 7543 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7544 NoteRange = ErrorRange = 7545 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7546 ErrorFound = NotACompoundStatement; 7547 } 7548 if (ErrorFound != NoError) { 7549 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 7550 << ErrorRange; 7551 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 7552 return StmtError(); 7553 } 7554 if (CurContext->isDependentContext()) 7555 UE = V = E = X = nullptr; 7556 } 7557 } 7558 7559 setFunctionHasBranchProtectedScope(); 7560 7561 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7562 X, V, E, UE, IsXLHSInRHSPart, 7563 IsPostfixUpdate); 7564 } 7565 7566 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 7567 Stmt *AStmt, 7568 SourceLocation StartLoc, 7569 SourceLocation EndLoc) { 7570 if (!AStmt) 7571 return StmtError(); 7572 7573 auto *CS = cast<CapturedStmt>(AStmt); 7574 // 1.2.2 OpenMP Language Terminology 7575 // Structured block - An executable statement with a single entry at the 7576 // top and a single exit at the bottom. 7577 // The point of exit cannot be a branch out of the structured block. 7578 // longjmp() and throw() must not violate the entry/exit criteria. 7579 CS->getCapturedDecl()->setNothrow(); 7580 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 7581 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7582 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7583 // 1.2.2 OpenMP Language Terminology 7584 // Structured block - An executable statement with a single entry at the 7585 // top and a single exit at the bottom. 7586 // The point of exit cannot be a branch out of the structured block. 7587 // longjmp() and throw() must not violate the entry/exit criteria. 7588 CS->getCapturedDecl()->setNothrow(); 7589 } 7590 7591 // OpenMP [2.16, Nesting of Regions] 7592 // If specified, a teams construct must be contained within a target 7593 // construct. That target construct must contain no statements or directives 7594 // outside of the teams construct. 7595 if (DSAStack->hasInnerTeamsRegion()) { 7596 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 7597 bool OMPTeamsFound = true; 7598 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 7599 auto I = CS->body_begin(); 7600 while (I != CS->body_end()) { 7601 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 7602 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 7603 OMPTeamsFound) { 7604 7605 OMPTeamsFound = false; 7606 break; 7607 } 7608 ++I; 7609 } 7610 assert(I != CS->body_end() && "Not found statement"); 7611 S = *I; 7612 } else { 7613 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 7614 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 7615 } 7616 if (!OMPTeamsFound) { 7617 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 7618 Diag(DSAStack->getInnerTeamsRegionLoc(), 7619 diag::note_omp_nested_teams_construct_here); 7620 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 7621 << isa<OMPExecutableDirective>(S); 7622 return StmtError(); 7623 } 7624 } 7625 7626 setFunctionHasBranchProtectedScope(); 7627 7628 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7629 } 7630 7631 StmtResult 7632 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 7633 Stmt *AStmt, SourceLocation StartLoc, 7634 SourceLocation EndLoc) { 7635 if (!AStmt) 7636 return StmtError(); 7637 7638 auto *CS = cast<CapturedStmt>(AStmt); 7639 // 1.2.2 OpenMP Language Terminology 7640 // Structured block - An executable statement with a single entry at the 7641 // top and a single exit at the bottom. 7642 // The point of exit cannot be a branch out of the structured block. 7643 // longjmp() and throw() must not violate the entry/exit criteria. 7644 CS->getCapturedDecl()->setNothrow(); 7645 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 7646 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7647 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7648 // 1.2.2 OpenMP Language Terminology 7649 // Structured block - An executable statement with a single entry at the 7650 // top and a single exit at the bottom. 7651 // The point of exit cannot be a branch out of the structured block. 7652 // longjmp() and throw() must not violate the entry/exit criteria. 7653 CS->getCapturedDecl()->setNothrow(); 7654 } 7655 7656 setFunctionHasBranchProtectedScope(); 7657 7658 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7659 AStmt); 7660 } 7661 7662 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 7663 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7664 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7665 if (!AStmt) 7666 return StmtError(); 7667 7668 auto *CS = cast<CapturedStmt>(AStmt); 7669 // 1.2.2 OpenMP Language Terminology 7670 // Structured block - An executable statement with a single entry at the 7671 // top and a single exit at the bottom. 7672 // The point of exit cannot be a branch out of the structured block. 7673 // longjmp() and throw() must not violate the entry/exit criteria. 7674 CS->getCapturedDecl()->setNothrow(); 7675 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7676 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7677 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7678 // 1.2.2 OpenMP Language Terminology 7679 // Structured block - An executable statement with a single entry at the 7680 // top and a single exit at the bottom. 7681 // The point of exit cannot be a branch out of the structured block. 7682 // longjmp() and throw() must not violate the entry/exit criteria. 7683 CS->getCapturedDecl()->setNothrow(); 7684 } 7685 7686 OMPLoopDirective::HelperExprs B; 7687 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7688 // define the nested loops number. 7689 unsigned NestedLoopCount = 7690 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 7691 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7692 VarsWithImplicitDSA, B); 7693 if (NestedLoopCount == 0) 7694 return StmtError(); 7695 7696 assert((CurContext->isDependentContext() || B.builtAll()) && 7697 "omp target parallel for loop exprs were not built"); 7698 7699 if (!CurContext->isDependentContext()) { 7700 // Finalize the clauses that need pre-built expressions for CodeGen. 7701 for (OMPClause *C : Clauses) { 7702 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7703 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7704 B.NumIterations, *this, CurScope, 7705 DSAStack)) 7706 return StmtError(); 7707 } 7708 } 7709 7710 setFunctionHasBranchProtectedScope(); 7711 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 7712 NestedLoopCount, Clauses, AStmt, 7713 B, DSAStack->isCancelRegion()); 7714 } 7715 7716 /// Check for existence of a map clause in the list of clauses. 7717 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 7718 const OpenMPClauseKind K) { 7719 return llvm::any_of( 7720 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 7721 } 7722 7723 template <typename... Params> 7724 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 7725 const Params... ClauseTypes) { 7726 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 7727 } 7728 7729 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 7730 Stmt *AStmt, 7731 SourceLocation StartLoc, 7732 SourceLocation EndLoc) { 7733 if (!AStmt) 7734 return StmtError(); 7735 7736 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7737 7738 // OpenMP [2.10.1, Restrictions, p. 97] 7739 // At least one map clause must appear on the directive. 7740 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 7741 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7742 << "'map' or 'use_device_ptr'" 7743 << getOpenMPDirectiveName(OMPD_target_data); 7744 return StmtError(); 7745 } 7746 7747 setFunctionHasBranchProtectedScope(); 7748 7749 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7750 AStmt); 7751 } 7752 7753 StmtResult 7754 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 7755 SourceLocation StartLoc, 7756 SourceLocation EndLoc, Stmt *AStmt) { 7757 if (!AStmt) 7758 return StmtError(); 7759 7760 auto *CS = cast<CapturedStmt>(AStmt); 7761 // 1.2.2 OpenMP Language Terminology 7762 // Structured block - An executable statement with a single entry at the 7763 // top and a single exit at the bottom. 7764 // The point of exit cannot be a branch out of the structured block. 7765 // longjmp() and throw() must not violate the entry/exit criteria. 7766 CS->getCapturedDecl()->setNothrow(); 7767 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 7768 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7769 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7770 // 1.2.2 OpenMP Language Terminology 7771 // Structured block - An executable statement with a single entry at the 7772 // top and a single exit at the bottom. 7773 // The point of exit cannot be a branch out of the structured block. 7774 // longjmp() and throw() must not violate the entry/exit criteria. 7775 CS->getCapturedDecl()->setNothrow(); 7776 } 7777 7778 // OpenMP [2.10.2, Restrictions, p. 99] 7779 // At least one map clause must appear on the directive. 7780 if (!hasClauses(Clauses, OMPC_map)) { 7781 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7782 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 7783 return StmtError(); 7784 } 7785 7786 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7787 AStmt); 7788 } 7789 7790 StmtResult 7791 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 7792 SourceLocation StartLoc, 7793 SourceLocation EndLoc, Stmt *AStmt) { 7794 if (!AStmt) 7795 return StmtError(); 7796 7797 auto *CS = cast<CapturedStmt>(AStmt); 7798 // 1.2.2 OpenMP Language Terminology 7799 // Structured block - An executable statement with a single entry at the 7800 // top and a single exit at the bottom. 7801 // The point of exit cannot be a branch out of the structured block. 7802 // longjmp() and throw() must not violate the entry/exit criteria. 7803 CS->getCapturedDecl()->setNothrow(); 7804 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 7805 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7806 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7807 // 1.2.2 OpenMP Language Terminology 7808 // Structured block - An executable statement with a single entry at the 7809 // top and a single exit at the bottom. 7810 // The point of exit cannot be a branch out of the structured block. 7811 // longjmp() and throw() must not violate the entry/exit criteria. 7812 CS->getCapturedDecl()->setNothrow(); 7813 } 7814 7815 // OpenMP [2.10.3, Restrictions, p. 102] 7816 // At least one map clause must appear on the directive. 7817 if (!hasClauses(Clauses, OMPC_map)) { 7818 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7819 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 7820 return StmtError(); 7821 } 7822 7823 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7824 AStmt); 7825 } 7826 7827 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 7828 SourceLocation StartLoc, 7829 SourceLocation EndLoc, 7830 Stmt *AStmt) { 7831 if (!AStmt) 7832 return StmtError(); 7833 7834 auto *CS = cast<CapturedStmt>(AStmt); 7835 // 1.2.2 OpenMP Language Terminology 7836 // Structured block - An executable statement with a single entry at the 7837 // top and a single exit at the bottom. 7838 // The point of exit cannot be a branch out of the structured block. 7839 // longjmp() and throw() must not violate the entry/exit criteria. 7840 CS->getCapturedDecl()->setNothrow(); 7841 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 7842 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7843 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7844 // 1.2.2 OpenMP Language Terminology 7845 // Structured block - An executable statement with a single entry at the 7846 // top and a single exit at the bottom. 7847 // The point of exit cannot be a branch out of the structured block. 7848 // longjmp() and throw() must not violate the entry/exit criteria. 7849 CS->getCapturedDecl()->setNothrow(); 7850 } 7851 7852 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 7853 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 7854 return StmtError(); 7855 } 7856 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 7857 AStmt); 7858 } 7859 7860 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 7861 Stmt *AStmt, SourceLocation StartLoc, 7862 SourceLocation EndLoc) { 7863 if (!AStmt) 7864 return StmtError(); 7865 7866 auto *CS = cast<CapturedStmt>(AStmt); 7867 // 1.2.2 OpenMP Language Terminology 7868 // Structured block - An executable statement with a single entry at the 7869 // top and a single exit at the bottom. 7870 // The point of exit cannot be a branch out of the structured block. 7871 // longjmp() and throw() must not violate the entry/exit criteria. 7872 CS->getCapturedDecl()->setNothrow(); 7873 7874 setFunctionHasBranchProtectedScope(); 7875 7876 DSAStack->setParentTeamsRegionLoc(StartLoc); 7877 7878 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7879 } 7880 7881 StmtResult 7882 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 7883 SourceLocation EndLoc, 7884 OpenMPDirectiveKind CancelRegion) { 7885 if (DSAStack->isParentNowaitRegion()) { 7886 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 7887 return StmtError(); 7888 } 7889 if (DSAStack->isParentOrderedRegion()) { 7890 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 7891 return StmtError(); 7892 } 7893 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 7894 CancelRegion); 7895 } 7896 7897 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 7898 SourceLocation StartLoc, 7899 SourceLocation EndLoc, 7900 OpenMPDirectiveKind CancelRegion) { 7901 if (DSAStack->isParentNowaitRegion()) { 7902 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 7903 return StmtError(); 7904 } 7905 if (DSAStack->isParentOrderedRegion()) { 7906 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 7907 return StmtError(); 7908 } 7909 DSAStack->setParentCancelRegion(/*Cancel=*/true); 7910 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7911 CancelRegion); 7912 } 7913 7914 static bool checkGrainsizeNumTasksClauses(Sema &S, 7915 ArrayRef<OMPClause *> Clauses) { 7916 const OMPClause *PrevClause = nullptr; 7917 bool ErrorFound = false; 7918 for (const OMPClause *C : Clauses) { 7919 if (C->getClauseKind() == OMPC_grainsize || 7920 C->getClauseKind() == OMPC_num_tasks) { 7921 if (!PrevClause) 7922 PrevClause = C; 7923 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 7924 S.Diag(C->getBeginLoc(), 7925 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 7926 << getOpenMPClauseName(C->getClauseKind()) 7927 << getOpenMPClauseName(PrevClause->getClauseKind()); 7928 S.Diag(PrevClause->getBeginLoc(), 7929 diag::note_omp_previous_grainsize_num_tasks) 7930 << getOpenMPClauseName(PrevClause->getClauseKind()); 7931 ErrorFound = true; 7932 } 7933 } 7934 } 7935 return ErrorFound; 7936 } 7937 7938 static bool checkReductionClauseWithNogroup(Sema &S, 7939 ArrayRef<OMPClause *> Clauses) { 7940 const OMPClause *ReductionClause = nullptr; 7941 const OMPClause *NogroupClause = nullptr; 7942 for (const OMPClause *C : Clauses) { 7943 if (C->getClauseKind() == OMPC_reduction) { 7944 ReductionClause = C; 7945 if (NogroupClause) 7946 break; 7947 continue; 7948 } 7949 if (C->getClauseKind() == OMPC_nogroup) { 7950 NogroupClause = C; 7951 if (ReductionClause) 7952 break; 7953 continue; 7954 } 7955 } 7956 if (ReductionClause && NogroupClause) { 7957 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 7958 << SourceRange(NogroupClause->getBeginLoc(), 7959 NogroupClause->getEndLoc()); 7960 return true; 7961 } 7962 return false; 7963 } 7964 7965 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 7966 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7967 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7968 if (!AStmt) 7969 return StmtError(); 7970 7971 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7972 OMPLoopDirective::HelperExprs B; 7973 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7974 // define the nested loops number. 7975 unsigned NestedLoopCount = 7976 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 7977 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 7978 VarsWithImplicitDSA, B); 7979 if (NestedLoopCount == 0) 7980 return StmtError(); 7981 7982 assert((CurContext->isDependentContext() || B.builtAll()) && 7983 "omp for loop exprs were not built"); 7984 7985 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7986 // The grainsize clause and num_tasks clause are mutually exclusive and may 7987 // not appear on the same taskloop directive. 7988 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7989 return StmtError(); 7990 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7991 // If a reduction clause is present on the taskloop directive, the nogroup 7992 // clause must not be specified. 7993 if (checkReductionClauseWithNogroup(*this, Clauses)) 7994 return StmtError(); 7995 7996 setFunctionHasBranchProtectedScope(); 7997 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 7998 NestedLoopCount, Clauses, AStmt, B); 7999 } 8000 8001 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 8002 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8003 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8004 if (!AStmt) 8005 return StmtError(); 8006 8007 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8008 OMPLoopDirective::HelperExprs B; 8009 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8010 // define the nested loops number. 8011 unsigned NestedLoopCount = 8012 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 8013 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 8014 VarsWithImplicitDSA, B); 8015 if (NestedLoopCount == 0) 8016 return StmtError(); 8017 8018 assert((CurContext->isDependentContext() || B.builtAll()) && 8019 "omp for loop exprs were not built"); 8020 8021 if (!CurContext->isDependentContext()) { 8022 // Finalize the clauses that need pre-built expressions for CodeGen. 8023 for (OMPClause *C : Clauses) { 8024 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8025 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8026 B.NumIterations, *this, CurScope, 8027 DSAStack)) 8028 return StmtError(); 8029 } 8030 } 8031 8032 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8033 // The grainsize clause and num_tasks clause are mutually exclusive and may 8034 // not appear on the same taskloop directive. 8035 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 8036 return StmtError(); 8037 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8038 // If a reduction clause is present on the taskloop directive, the nogroup 8039 // clause must not be specified. 8040 if (checkReductionClauseWithNogroup(*this, Clauses)) 8041 return StmtError(); 8042 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8043 return StmtError(); 8044 8045 setFunctionHasBranchProtectedScope(); 8046 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 8047 NestedLoopCount, Clauses, AStmt, B); 8048 } 8049 8050 StmtResult Sema::ActOnOpenMPDistributeDirective( 8051 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8052 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8053 if (!AStmt) 8054 return StmtError(); 8055 8056 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8057 OMPLoopDirective::HelperExprs B; 8058 // In presence of clause 'collapse' with number of loops, it will 8059 // define the nested loops number. 8060 unsigned NestedLoopCount = 8061 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 8062 nullptr /*ordered not a clause on distribute*/, AStmt, 8063 *this, *DSAStack, VarsWithImplicitDSA, B); 8064 if (NestedLoopCount == 0) 8065 return StmtError(); 8066 8067 assert((CurContext->isDependentContext() || B.builtAll()) && 8068 "omp for loop exprs were not built"); 8069 8070 setFunctionHasBranchProtectedScope(); 8071 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 8072 NestedLoopCount, Clauses, AStmt, B); 8073 } 8074 8075 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 8076 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8077 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8078 if (!AStmt) 8079 return StmtError(); 8080 8081 auto *CS = cast<CapturedStmt>(AStmt); 8082 // 1.2.2 OpenMP Language Terminology 8083 // Structured block - An executable statement with a single entry at the 8084 // top and a single exit at the bottom. 8085 // The point of exit cannot be a branch out of the structured block. 8086 // longjmp() and throw() must not violate the entry/exit criteria. 8087 CS->getCapturedDecl()->setNothrow(); 8088 for (int ThisCaptureLevel = 8089 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 8090 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8091 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8092 // 1.2.2 OpenMP Language Terminology 8093 // Structured block - An executable statement with a single entry at the 8094 // top and a single exit at the bottom. 8095 // The point of exit cannot be a branch out of the structured block. 8096 // longjmp() and throw() must not violate the entry/exit criteria. 8097 CS->getCapturedDecl()->setNothrow(); 8098 } 8099 8100 OMPLoopDirective::HelperExprs B; 8101 // In presence of clause 'collapse' with number of loops, it will 8102 // define the nested loops number. 8103 unsigned NestedLoopCount = checkOpenMPLoop( 8104 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8105 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8106 VarsWithImplicitDSA, B); 8107 if (NestedLoopCount == 0) 8108 return StmtError(); 8109 8110 assert((CurContext->isDependentContext() || B.builtAll()) && 8111 "omp for loop exprs were not built"); 8112 8113 setFunctionHasBranchProtectedScope(); 8114 return OMPDistributeParallelForDirective::Create( 8115 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8116 DSAStack->isCancelRegion()); 8117 } 8118 8119 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 8120 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8121 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8122 if (!AStmt) 8123 return StmtError(); 8124 8125 auto *CS = cast<CapturedStmt>(AStmt); 8126 // 1.2.2 OpenMP Language Terminology 8127 // Structured block - An executable statement with a single entry at the 8128 // top and a single exit at the bottom. 8129 // The point of exit cannot be a branch out of the structured block. 8130 // longjmp() and throw() must not violate the entry/exit criteria. 8131 CS->getCapturedDecl()->setNothrow(); 8132 for (int ThisCaptureLevel = 8133 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 8134 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8135 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8136 // 1.2.2 OpenMP Language Terminology 8137 // Structured block - An executable statement with a single entry at the 8138 // top and a single exit at the bottom. 8139 // The point of exit cannot be a branch out of the structured block. 8140 // longjmp() and throw() must not violate the entry/exit criteria. 8141 CS->getCapturedDecl()->setNothrow(); 8142 } 8143 8144 OMPLoopDirective::HelperExprs B; 8145 // In presence of clause 'collapse' with number of loops, it will 8146 // define the nested loops number. 8147 unsigned NestedLoopCount = checkOpenMPLoop( 8148 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 8149 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8150 VarsWithImplicitDSA, B); 8151 if (NestedLoopCount == 0) 8152 return StmtError(); 8153 8154 assert((CurContext->isDependentContext() || B.builtAll()) && 8155 "omp for loop exprs were not built"); 8156 8157 if (!CurContext->isDependentContext()) { 8158 // Finalize the clauses that need pre-built expressions for CodeGen. 8159 for (OMPClause *C : Clauses) { 8160 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8161 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8162 B.NumIterations, *this, CurScope, 8163 DSAStack)) 8164 return StmtError(); 8165 } 8166 } 8167 8168 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8169 return StmtError(); 8170 8171 setFunctionHasBranchProtectedScope(); 8172 return OMPDistributeParallelForSimdDirective::Create( 8173 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8174 } 8175 8176 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 8177 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8178 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8179 if (!AStmt) 8180 return StmtError(); 8181 8182 auto *CS = cast<CapturedStmt>(AStmt); 8183 // 1.2.2 OpenMP Language Terminology 8184 // Structured block - An executable statement with a single entry at the 8185 // top and a single exit at the bottom. 8186 // The point of exit cannot be a branch out of the structured block. 8187 // longjmp() and throw() must not violate the entry/exit criteria. 8188 CS->getCapturedDecl()->setNothrow(); 8189 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 8190 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8191 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8192 // 1.2.2 OpenMP Language Terminology 8193 // Structured block - An executable statement with a single entry at the 8194 // top and a single exit at the bottom. 8195 // The point of exit cannot be a branch out of the structured block. 8196 // longjmp() and throw() must not violate the entry/exit criteria. 8197 CS->getCapturedDecl()->setNothrow(); 8198 } 8199 8200 OMPLoopDirective::HelperExprs B; 8201 // In presence of clause 'collapse' with number of loops, it will 8202 // define the nested loops number. 8203 unsigned NestedLoopCount = 8204 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 8205 nullptr /*ordered not a clause on distribute*/, CS, *this, 8206 *DSAStack, VarsWithImplicitDSA, B); 8207 if (NestedLoopCount == 0) 8208 return StmtError(); 8209 8210 assert((CurContext->isDependentContext() || B.builtAll()) && 8211 "omp for loop exprs were not built"); 8212 8213 if (!CurContext->isDependentContext()) { 8214 // Finalize the clauses that need pre-built expressions for CodeGen. 8215 for (OMPClause *C : Clauses) { 8216 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8217 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8218 B.NumIterations, *this, CurScope, 8219 DSAStack)) 8220 return StmtError(); 8221 } 8222 } 8223 8224 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8225 return StmtError(); 8226 8227 setFunctionHasBranchProtectedScope(); 8228 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 8229 NestedLoopCount, Clauses, AStmt, B); 8230 } 8231 8232 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 8233 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8234 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8235 if (!AStmt) 8236 return StmtError(); 8237 8238 auto *CS = cast<CapturedStmt>(AStmt); 8239 // 1.2.2 OpenMP Language Terminology 8240 // Structured block - An executable statement with a single entry at the 8241 // top and a single exit at the bottom. 8242 // The point of exit cannot be a branch out of the structured block. 8243 // longjmp() and throw() must not violate the entry/exit criteria. 8244 CS->getCapturedDecl()->setNothrow(); 8245 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 8246 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8247 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8248 // 1.2.2 OpenMP Language Terminology 8249 // Structured block - An executable statement with a single entry at the 8250 // top and a single exit at the bottom. 8251 // The point of exit cannot be a branch out of the structured block. 8252 // longjmp() and throw() must not violate the entry/exit criteria. 8253 CS->getCapturedDecl()->setNothrow(); 8254 } 8255 8256 OMPLoopDirective::HelperExprs B; 8257 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8258 // define the nested loops number. 8259 unsigned NestedLoopCount = checkOpenMPLoop( 8260 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 8261 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 8262 VarsWithImplicitDSA, B); 8263 if (NestedLoopCount == 0) 8264 return StmtError(); 8265 8266 assert((CurContext->isDependentContext() || B.builtAll()) && 8267 "omp target parallel for simd loop exprs were not built"); 8268 8269 if (!CurContext->isDependentContext()) { 8270 // Finalize the clauses that need pre-built expressions for CodeGen. 8271 for (OMPClause *C : Clauses) { 8272 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8273 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8274 B.NumIterations, *this, CurScope, 8275 DSAStack)) 8276 return StmtError(); 8277 } 8278 } 8279 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8280 return StmtError(); 8281 8282 setFunctionHasBranchProtectedScope(); 8283 return OMPTargetParallelForSimdDirective::Create( 8284 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8285 } 8286 8287 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 8288 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8289 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8290 if (!AStmt) 8291 return StmtError(); 8292 8293 auto *CS = cast<CapturedStmt>(AStmt); 8294 // 1.2.2 OpenMP Language Terminology 8295 // Structured block - An executable statement with a single entry at the 8296 // top and a single exit at the bottom. 8297 // The point of exit cannot be a branch out of the structured block. 8298 // longjmp() and throw() must not violate the entry/exit criteria. 8299 CS->getCapturedDecl()->setNothrow(); 8300 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 8301 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8302 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8303 // 1.2.2 OpenMP Language Terminology 8304 // Structured block - An executable statement with a single entry at the 8305 // top and a single exit at the bottom. 8306 // The point of exit cannot be a branch out of the structured block. 8307 // longjmp() and throw() must not violate the entry/exit criteria. 8308 CS->getCapturedDecl()->setNothrow(); 8309 } 8310 8311 OMPLoopDirective::HelperExprs B; 8312 // In presence of clause 'collapse' with number of loops, it will define the 8313 // nested loops number. 8314 unsigned NestedLoopCount = 8315 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 8316 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 8317 VarsWithImplicitDSA, B); 8318 if (NestedLoopCount == 0) 8319 return StmtError(); 8320 8321 assert((CurContext->isDependentContext() || B.builtAll()) && 8322 "omp target simd loop exprs were not built"); 8323 8324 if (!CurContext->isDependentContext()) { 8325 // Finalize the clauses that need pre-built expressions for CodeGen. 8326 for (OMPClause *C : Clauses) { 8327 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8328 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8329 B.NumIterations, *this, CurScope, 8330 DSAStack)) 8331 return StmtError(); 8332 } 8333 } 8334 8335 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8336 return StmtError(); 8337 8338 setFunctionHasBranchProtectedScope(); 8339 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 8340 NestedLoopCount, Clauses, AStmt, B); 8341 } 8342 8343 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 8344 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8345 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8346 if (!AStmt) 8347 return StmtError(); 8348 8349 auto *CS = cast<CapturedStmt>(AStmt); 8350 // 1.2.2 OpenMP Language Terminology 8351 // Structured block - An executable statement with a single entry at the 8352 // top and a single exit at the bottom. 8353 // The point of exit cannot be a branch out of the structured block. 8354 // longjmp() and throw() must not violate the entry/exit criteria. 8355 CS->getCapturedDecl()->setNothrow(); 8356 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 8357 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8358 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8359 // 1.2.2 OpenMP Language Terminology 8360 // Structured block - An executable statement with a single entry at the 8361 // top and a single exit at the bottom. 8362 // The point of exit cannot be a branch out of the structured block. 8363 // longjmp() and throw() must not violate the entry/exit criteria. 8364 CS->getCapturedDecl()->setNothrow(); 8365 } 8366 8367 OMPLoopDirective::HelperExprs B; 8368 // In presence of clause 'collapse' with number of loops, it will 8369 // define the nested loops number. 8370 unsigned NestedLoopCount = 8371 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 8372 nullptr /*ordered not a clause on distribute*/, CS, *this, 8373 *DSAStack, VarsWithImplicitDSA, B); 8374 if (NestedLoopCount == 0) 8375 return StmtError(); 8376 8377 assert((CurContext->isDependentContext() || B.builtAll()) && 8378 "omp teams distribute loop exprs were not built"); 8379 8380 setFunctionHasBranchProtectedScope(); 8381 8382 DSAStack->setParentTeamsRegionLoc(StartLoc); 8383 8384 return OMPTeamsDistributeDirective::Create( 8385 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8386 } 8387 8388 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 8389 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8390 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8391 if (!AStmt) 8392 return StmtError(); 8393 8394 auto *CS = cast<CapturedStmt>(AStmt); 8395 // 1.2.2 OpenMP Language Terminology 8396 // Structured block - An executable statement with a single entry at the 8397 // top and a single exit at the bottom. 8398 // The point of exit cannot be a branch out of the structured block. 8399 // longjmp() and throw() must not violate the entry/exit criteria. 8400 CS->getCapturedDecl()->setNothrow(); 8401 for (int ThisCaptureLevel = 8402 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 8403 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8404 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8405 // 1.2.2 OpenMP Language Terminology 8406 // Structured block - An executable statement with a single entry at the 8407 // top and a single exit at the bottom. 8408 // The point of exit cannot be a branch out of the structured block. 8409 // longjmp() and throw() must not violate the entry/exit criteria. 8410 CS->getCapturedDecl()->setNothrow(); 8411 } 8412 8413 8414 OMPLoopDirective::HelperExprs B; 8415 // In presence of clause 'collapse' with number of loops, it will 8416 // define the nested loops number. 8417 unsigned NestedLoopCount = checkOpenMPLoop( 8418 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 8419 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8420 VarsWithImplicitDSA, B); 8421 8422 if (NestedLoopCount == 0) 8423 return StmtError(); 8424 8425 assert((CurContext->isDependentContext() || B.builtAll()) && 8426 "omp teams distribute simd loop exprs were not built"); 8427 8428 if (!CurContext->isDependentContext()) { 8429 // Finalize the clauses that need pre-built expressions for CodeGen. 8430 for (OMPClause *C : Clauses) { 8431 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8432 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8433 B.NumIterations, *this, CurScope, 8434 DSAStack)) 8435 return StmtError(); 8436 } 8437 } 8438 8439 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8440 return StmtError(); 8441 8442 setFunctionHasBranchProtectedScope(); 8443 8444 DSAStack->setParentTeamsRegionLoc(StartLoc); 8445 8446 return OMPTeamsDistributeSimdDirective::Create( 8447 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8448 } 8449 8450 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 8451 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8452 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8453 if (!AStmt) 8454 return StmtError(); 8455 8456 auto *CS = cast<CapturedStmt>(AStmt); 8457 // 1.2.2 OpenMP Language Terminology 8458 // Structured block - An executable statement with a single entry at the 8459 // top and a single exit at the bottom. 8460 // The point of exit cannot be a branch out of the structured block. 8461 // longjmp() and throw() must not violate the entry/exit criteria. 8462 CS->getCapturedDecl()->setNothrow(); 8463 8464 for (int ThisCaptureLevel = 8465 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 8466 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8467 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8468 // 1.2.2 OpenMP Language Terminology 8469 // Structured block - An executable statement with a single entry at the 8470 // top and a single exit at the bottom. 8471 // The point of exit cannot be a branch out of the structured block. 8472 // longjmp() and throw() must not violate the entry/exit criteria. 8473 CS->getCapturedDecl()->setNothrow(); 8474 } 8475 8476 OMPLoopDirective::HelperExprs B; 8477 // In presence of clause 'collapse' with number of loops, it will 8478 // define the nested loops number. 8479 unsigned NestedLoopCount = checkOpenMPLoop( 8480 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 8481 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8482 VarsWithImplicitDSA, B); 8483 8484 if (NestedLoopCount == 0) 8485 return StmtError(); 8486 8487 assert((CurContext->isDependentContext() || B.builtAll()) && 8488 "omp for loop exprs were not built"); 8489 8490 if (!CurContext->isDependentContext()) { 8491 // Finalize the clauses that need pre-built expressions for CodeGen. 8492 for (OMPClause *C : Clauses) { 8493 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8494 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8495 B.NumIterations, *this, CurScope, 8496 DSAStack)) 8497 return StmtError(); 8498 } 8499 } 8500 8501 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8502 return StmtError(); 8503 8504 setFunctionHasBranchProtectedScope(); 8505 8506 DSAStack->setParentTeamsRegionLoc(StartLoc); 8507 8508 return OMPTeamsDistributeParallelForSimdDirective::Create( 8509 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8510 } 8511 8512 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 8513 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8514 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8515 if (!AStmt) 8516 return StmtError(); 8517 8518 auto *CS = cast<CapturedStmt>(AStmt); 8519 // 1.2.2 OpenMP Language Terminology 8520 // Structured block - An executable statement with a single entry at the 8521 // top and a single exit at the bottom. 8522 // The point of exit cannot be a branch out of the structured block. 8523 // longjmp() and throw() must not violate the entry/exit criteria. 8524 CS->getCapturedDecl()->setNothrow(); 8525 8526 for (int ThisCaptureLevel = 8527 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 8528 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8529 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8530 // 1.2.2 OpenMP Language Terminology 8531 // Structured block - An executable statement with a single entry at the 8532 // top and a single exit at the bottom. 8533 // The point of exit cannot be a branch out of the structured block. 8534 // longjmp() and throw() must not violate the entry/exit criteria. 8535 CS->getCapturedDecl()->setNothrow(); 8536 } 8537 8538 OMPLoopDirective::HelperExprs B; 8539 // In presence of clause 'collapse' with number of loops, it will 8540 // define the nested loops number. 8541 unsigned NestedLoopCount = checkOpenMPLoop( 8542 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8543 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8544 VarsWithImplicitDSA, B); 8545 8546 if (NestedLoopCount == 0) 8547 return StmtError(); 8548 8549 assert((CurContext->isDependentContext() || B.builtAll()) && 8550 "omp for loop exprs were not built"); 8551 8552 setFunctionHasBranchProtectedScope(); 8553 8554 DSAStack->setParentTeamsRegionLoc(StartLoc); 8555 8556 return OMPTeamsDistributeParallelForDirective::Create( 8557 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8558 DSAStack->isCancelRegion()); 8559 } 8560 8561 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 8562 Stmt *AStmt, 8563 SourceLocation StartLoc, 8564 SourceLocation EndLoc) { 8565 if (!AStmt) 8566 return StmtError(); 8567 8568 auto *CS = cast<CapturedStmt>(AStmt); 8569 // 1.2.2 OpenMP Language Terminology 8570 // Structured block - An executable statement with a single entry at the 8571 // top and a single exit at the bottom. 8572 // The point of exit cannot be a branch out of the structured block. 8573 // longjmp() and throw() must not violate the entry/exit criteria. 8574 CS->getCapturedDecl()->setNothrow(); 8575 8576 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 8577 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8578 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8579 // 1.2.2 OpenMP Language Terminology 8580 // Structured block - An executable statement with a single entry at the 8581 // top and a single exit at the bottom. 8582 // The point of exit cannot be a branch out of the structured block. 8583 // longjmp() and throw() must not violate the entry/exit criteria. 8584 CS->getCapturedDecl()->setNothrow(); 8585 } 8586 setFunctionHasBranchProtectedScope(); 8587 8588 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 8589 AStmt); 8590 } 8591 8592 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 8593 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8594 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8595 if (!AStmt) 8596 return StmtError(); 8597 8598 auto *CS = cast<CapturedStmt>(AStmt); 8599 // 1.2.2 OpenMP Language Terminology 8600 // Structured block - An executable statement with a single entry at the 8601 // top and a single exit at the bottom. 8602 // The point of exit cannot be a branch out of the structured block. 8603 // longjmp() and throw() must not violate the entry/exit criteria. 8604 CS->getCapturedDecl()->setNothrow(); 8605 for (int ThisCaptureLevel = 8606 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 8607 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8608 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8609 // 1.2.2 OpenMP Language Terminology 8610 // Structured block - An executable statement with a single entry at the 8611 // top and a single exit at the bottom. 8612 // The point of exit cannot be a branch out of the structured block. 8613 // longjmp() and throw() must not violate the entry/exit criteria. 8614 CS->getCapturedDecl()->setNothrow(); 8615 } 8616 8617 OMPLoopDirective::HelperExprs B; 8618 // In presence of clause 'collapse' with number of loops, it will 8619 // define the nested loops number. 8620 unsigned NestedLoopCount = checkOpenMPLoop( 8621 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 8622 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8623 VarsWithImplicitDSA, B); 8624 if (NestedLoopCount == 0) 8625 return StmtError(); 8626 8627 assert((CurContext->isDependentContext() || B.builtAll()) && 8628 "omp target teams distribute loop exprs were not built"); 8629 8630 setFunctionHasBranchProtectedScope(); 8631 return OMPTargetTeamsDistributeDirective::Create( 8632 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8633 } 8634 8635 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 8636 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8637 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8638 if (!AStmt) 8639 return StmtError(); 8640 8641 auto *CS = cast<CapturedStmt>(AStmt); 8642 // 1.2.2 OpenMP Language Terminology 8643 // Structured block - An executable statement with a single entry at the 8644 // top and a single exit at the bottom. 8645 // The point of exit cannot be a branch out of the structured block. 8646 // longjmp() and throw() must not violate the entry/exit criteria. 8647 CS->getCapturedDecl()->setNothrow(); 8648 for (int ThisCaptureLevel = 8649 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 8650 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8651 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8652 // 1.2.2 OpenMP Language Terminology 8653 // Structured block - An executable statement with a single entry at the 8654 // top and a single exit at the bottom. 8655 // The point of exit cannot be a branch out of the structured block. 8656 // longjmp() and throw() must not violate the entry/exit criteria. 8657 CS->getCapturedDecl()->setNothrow(); 8658 } 8659 8660 OMPLoopDirective::HelperExprs B; 8661 // In presence of clause 'collapse' with number of loops, it will 8662 // define the nested loops number. 8663 unsigned NestedLoopCount = checkOpenMPLoop( 8664 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8665 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8666 VarsWithImplicitDSA, B); 8667 if (NestedLoopCount == 0) 8668 return StmtError(); 8669 8670 assert((CurContext->isDependentContext() || B.builtAll()) && 8671 "omp target teams distribute parallel for loop exprs were not built"); 8672 8673 if (!CurContext->isDependentContext()) { 8674 // Finalize the clauses that need pre-built expressions for CodeGen. 8675 for (OMPClause *C : Clauses) { 8676 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8677 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8678 B.NumIterations, *this, CurScope, 8679 DSAStack)) 8680 return StmtError(); 8681 } 8682 } 8683 8684 setFunctionHasBranchProtectedScope(); 8685 return OMPTargetTeamsDistributeParallelForDirective::Create( 8686 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8687 DSAStack->isCancelRegion()); 8688 } 8689 8690 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 8691 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8692 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8693 if (!AStmt) 8694 return StmtError(); 8695 8696 auto *CS = cast<CapturedStmt>(AStmt); 8697 // 1.2.2 OpenMP Language Terminology 8698 // Structured block - An executable statement with a single entry at the 8699 // top and a single exit at the bottom. 8700 // The point of exit cannot be a branch out of the structured block. 8701 // longjmp() and throw() must not violate the entry/exit criteria. 8702 CS->getCapturedDecl()->setNothrow(); 8703 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 8704 OMPD_target_teams_distribute_parallel_for_simd); 8705 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8706 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8707 // 1.2.2 OpenMP Language Terminology 8708 // Structured block - An executable statement with a single entry at the 8709 // top and a single exit at the bottom. 8710 // The point of exit cannot be a branch out of the structured block. 8711 // longjmp() and throw() must not violate the entry/exit criteria. 8712 CS->getCapturedDecl()->setNothrow(); 8713 } 8714 8715 OMPLoopDirective::HelperExprs B; 8716 // In presence of clause 'collapse' with number of loops, it will 8717 // define the nested loops number. 8718 unsigned NestedLoopCount = 8719 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 8720 getCollapseNumberExpr(Clauses), 8721 nullptr /*ordered not a clause on distribute*/, CS, *this, 8722 *DSAStack, VarsWithImplicitDSA, B); 8723 if (NestedLoopCount == 0) 8724 return StmtError(); 8725 8726 assert((CurContext->isDependentContext() || B.builtAll()) && 8727 "omp target teams distribute parallel for simd loop exprs were not " 8728 "built"); 8729 8730 if (!CurContext->isDependentContext()) { 8731 // Finalize the clauses that need pre-built expressions for CodeGen. 8732 for (OMPClause *C : Clauses) { 8733 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8734 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8735 B.NumIterations, *this, CurScope, 8736 DSAStack)) 8737 return StmtError(); 8738 } 8739 } 8740 8741 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8742 return StmtError(); 8743 8744 setFunctionHasBranchProtectedScope(); 8745 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 8746 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8747 } 8748 8749 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 8750 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8751 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8752 if (!AStmt) 8753 return StmtError(); 8754 8755 auto *CS = cast<CapturedStmt>(AStmt); 8756 // 1.2.2 OpenMP Language Terminology 8757 // Structured block - An executable statement with a single entry at the 8758 // top and a single exit at the bottom. 8759 // The point of exit cannot be a branch out of the structured block. 8760 // longjmp() and throw() must not violate the entry/exit criteria. 8761 CS->getCapturedDecl()->setNothrow(); 8762 for (int ThisCaptureLevel = 8763 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 8764 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8765 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8766 // 1.2.2 OpenMP Language Terminology 8767 // Structured block - An executable statement with a single entry at the 8768 // top and a single exit at the bottom. 8769 // The point of exit cannot be a branch out of the structured block. 8770 // longjmp() and throw() must not violate the entry/exit criteria. 8771 CS->getCapturedDecl()->setNothrow(); 8772 } 8773 8774 OMPLoopDirective::HelperExprs B; 8775 // In presence of clause 'collapse' with number of loops, it will 8776 // define the nested loops number. 8777 unsigned NestedLoopCount = checkOpenMPLoop( 8778 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 8779 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8780 VarsWithImplicitDSA, B); 8781 if (NestedLoopCount == 0) 8782 return StmtError(); 8783 8784 assert((CurContext->isDependentContext() || B.builtAll()) && 8785 "omp target teams distribute simd loop exprs were not built"); 8786 8787 if (!CurContext->isDependentContext()) { 8788 // Finalize the clauses that need pre-built expressions for CodeGen. 8789 for (OMPClause *C : Clauses) { 8790 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8791 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8792 B.NumIterations, *this, CurScope, 8793 DSAStack)) 8794 return StmtError(); 8795 } 8796 } 8797 8798 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8799 return StmtError(); 8800 8801 setFunctionHasBranchProtectedScope(); 8802 return OMPTargetTeamsDistributeSimdDirective::Create( 8803 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8804 } 8805 8806 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 8807 SourceLocation StartLoc, 8808 SourceLocation LParenLoc, 8809 SourceLocation EndLoc) { 8810 OMPClause *Res = nullptr; 8811 switch (Kind) { 8812 case OMPC_final: 8813 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 8814 break; 8815 case OMPC_num_threads: 8816 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 8817 break; 8818 case OMPC_safelen: 8819 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 8820 break; 8821 case OMPC_simdlen: 8822 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 8823 break; 8824 case OMPC_allocator: 8825 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 8826 break; 8827 case OMPC_collapse: 8828 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 8829 break; 8830 case OMPC_ordered: 8831 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 8832 break; 8833 case OMPC_device: 8834 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 8835 break; 8836 case OMPC_num_teams: 8837 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 8838 break; 8839 case OMPC_thread_limit: 8840 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 8841 break; 8842 case OMPC_priority: 8843 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 8844 break; 8845 case OMPC_grainsize: 8846 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 8847 break; 8848 case OMPC_num_tasks: 8849 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 8850 break; 8851 case OMPC_hint: 8852 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 8853 break; 8854 case OMPC_if: 8855 case OMPC_default: 8856 case OMPC_proc_bind: 8857 case OMPC_schedule: 8858 case OMPC_private: 8859 case OMPC_firstprivate: 8860 case OMPC_lastprivate: 8861 case OMPC_shared: 8862 case OMPC_reduction: 8863 case OMPC_task_reduction: 8864 case OMPC_in_reduction: 8865 case OMPC_linear: 8866 case OMPC_aligned: 8867 case OMPC_copyin: 8868 case OMPC_copyprivate: 8869 case OMPC_nowait: 8870 case OMPC_untied: 8871 case OMPC_mergeable: 8872 case OMPC_threadprivate: 8873 case OMPC_allocate: 8874 case OMPC_flush: 8875 case OMPC_read: 8876 case OMPC_write: 8877 case OMPC_update: 8878 case OMPC_capture: 8879 case OMPC_seq_cst: 8880 case OMPC_depend: 8881 case OMPC_threads: 8882 case OMPC_simd: 8883 case OMPC_map: 8884 case OMPC_nogroup: 8885 case OMPC_dist_schedule: 8886 case OMPC_defaultmap: 8887 case OMPC_unknown: 8888 case OMPC_uniform: 8889 case OMPC_to: 8890 case OMPC_from: 8891 case OMPC_use_device_ptr: 8892 case OMPC_is_device_ptr: 8893 case OMPC_unified_address: 8894 case OMPC_unified_shared_memory: 8895 case OMPC_reverse_offload: 8896 case OMPC_dynamic_allocators: 8897 case OMPC_atomic_default_mem_order: 8898 llvm_unreachable("Clause is not allowed."); 8899 } 8900 return Res; 8901 } 8902 8903 // An OpenMP directive such as 'target parallel' has two captured regions: 8904 // for the 'target' and 'parallel' respectively. This function returns 8905 // the region in which to capture expressions associated with a clause. 8906 // A return value of OMPD_unknown signifies that the expression should not 8907 // be captured. 8908 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 8909 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 8910 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 8911 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8912 switch (CKind) { 8913 case OMPC_if: 8914 switch (DKind) { 8915 case OMPD_target_parallel: 8916 case OMPD_target_parallel_for: 8917 case OMPD_target_parallel_for_simd: 8918 // If this clause applies to the nested 'parallel' region, capture within 8919 // the 'target' region, otherwise do not capture. 8920 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8921 CaptureRegion = OMPD_target; 8922 break; 8923 case OMPD_target_teams_distribute_parallel_for: 8924 case OMPD_target_teams_distribute_parallel_for_simd: 8925 // If this clause applies to the nested 'parallel' region, capture within 8926 // the 'teams' region, otherwise do not capture. 8927 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8928 CaptureRegion = OMPD_teams; 8929 break; 8930 case OMPD_teams_distribute_parallel_for: 8931 case OMPD_teams_distribute_parallel_for_simd: 8932 CaptureRegion = OMPD_teams; 8933 break; 8934 case OMPD_target_update: 8935 case OMPD_target_enter_data: 8936 case OMPD_target_exit_data: 8937 CaptureRegion = OMPD_task; 8938 break; 8939 case OMPD_cancel: 8940 case OMPD_parallel: 8941 case OMPD_parallel_sections: 8942 case OMPD_parallel_for: 8943 case OMPD_parallel_for_simd: 8944 case OMPD_target: 8945 case OMPD_target_simd: 8946 case OMPD_target_teams: 8947 case OMPD_target_teams_distribute: 8948 case OMPD_target_teams_distribute_simd: 8949 case OMPD_distribute_parallel_for: 8950 case OMPD_distribute_parallel_for_simd: 8951 case OMPD_task: 8952 case OMPD_taskloop: 8953 case OMPD_taskloop_simd: 8954 case OMPD_target_data: 8955 // Do not capture if-clause expressions. 8956 break; 8957 case OMPD_threadprivate: 8958 case OMPD_allocate: 8959 case OMPD_taskyield: 8960 case OMPD_barrier: 8961 case OMPD_taskwait: 8962 case OMPD_cancellation_point: 8963 case OMPD_flush: 8964 case OMPD_declare_reduction: 8965 case OMPD_declare_mapper: 8966 case OMPD_declare_simd: 8967 case OMPD_declare_target: 8968 case OMPD_end_declare_target: 8969 case OMPD_teams: 8970 case OMPD_simd: 8971 case OMPD_for: 8972 case OMPD_for_simd: 8973 case OMPD_sections: 8974 case OMPD_section: 8975 case OMPD_single: 8976 case OMPD_master: 8977 case OMPD_critical: 8978 case OMPD_taskgroup: 8979 case OMPD_distribute: 8980 case OMPD_ordered: 8981 case OMPD_atomic: 8982 case OMPD_distribute_simd: 8983 case OMPD_teams_distribute: 8984 case OMPD_teams_distribute_simd: 8985 case OMPD_requires: 8986 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 8987 case OMPD_unknown: 8988 llvm_unreachable("Unknown OpenMP directive"); 8989 } 8990 break; 8991 case OMPC_num_threads: 8992 switch (DKind) { 8993 case OMPD_target_parallel: 8994 case OMPD_target_parallel_for: 8995 case OMPD_target_parallel_for_simd: 8996 CaptureRegion = OMPD_target; 8997 break; 8998 case OMPD_teams_distribute_parallel_for: 8999 case OMPD_teams_distribute_parallel_for_simd: 9000 case OMPD_target_teams_distribute_parallel_for: 9001 case OMPD_target_teams_distribute_parallel_for_simd: 9002 CaptureRegion = OMPD_teams; 9003 break; 9004 case OMPD_parallel: 9005 case OMPD_parallel_sections: 9006 case OMPD_parallel_for: 9007 case OMPD_parallel_for_simd: 9008 case OMPD_distribute_parallel_for: 9009 case OMPD_distribute_parallel_for_simd: 9010 // Do not capture num_threads-clause expressions. 9011 break; 9012 case OMPD_target_data: 9013 case OMPD_target_enter_data: 9014 case OMPD_target_exit_data: 9015 case OMPD_target_update: 9016 case OMPD_target: 9017 case OMPD_target_simd: 9018 case OMPD_target_teams: 9019 case OMPD_target_teams_distribute: 9020 case OMPD_target_teams_distribute_simd: 9021 case OMPD_cancel: 9022 case OMPD_task: 9023 case OMPD_taskloop: 9024 case OMPD_taskloop_simd: 9025 case OMPD_threadprivate: 9026 case OMPD_allocate: 9027 case OMPD_taskyield: 9028 case OMPD_barrier: 9029 case OMPD_taskwait: 9030 case OMPD_cancellation_point: 9031 case OMPD_flush: 9032 case OMPD_declare_reduction: 9033 case OMPD_declare_mapper: 9034 case OMPD_declare_simd: 9035 case OMPD_declare_target: 9036 case OMPD_end_declare_target: 9037 case OMPD_teams: 9038 case OMPD_simd: 9039 case OMPD_for: 9040 case OMPD_for_simd: 9041 case OMPD_sections: 9042 case OMPD_section: 9043 case OMPD_single: 9044 case OMPD_master: 9045 case OMPD_critical: 9046 case OMPD_taskgroup: 9047 case OMPD_distribute: 9048 case OMPD_ordered: 9049 case OMPD_atomic: 9050 case OMPD_distribute_simd: 9051 case OMPD_teams_distribute: 9052 case OMPD_teams_distribute_simd: 9053 case OMPD_requires: 9054 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 9055 case OMPD_unknown: 9056 llvm_unreachable("Unknown OpenMP directive"); 9057 } 9058 break; 9059 case OMPC_num_teams: 9060 switch (DKind) { 9061 case OMPD_target_teams: 9062 case OMPD_target_teams_distribute: 9063 case OMPD_target_teams_distribute_simd: 9064 case OMPD_target_teams_distribute_parallel_for: 9065 case OMPD_target_teams_distribute_parallel_for_simd: 9066 CaptureRegion = OMPD_target; 9067 break; 9068 case OMPD_teams_distribute_parallel_for: 9069 case OMPD_teams_distribute_parallel_for_simd: 9070 case OMPD_teams: 9071 case OMPD_teams_distribute: 9072 case OMPD_teams_distribute_simd: 9073 // Do not capture num_teams-clause expressions. 9074 break; 9075 case OMPD_distribute_parallel_for: 9076 case OMPD_distribute_parallel_for_simd: 9077 case OMPD_task: 9078 case OMPD_taskloop: 9079 case OMPD_taskloop_simd: 9080 case OMPD_target_data: 9081 case OMPD_target_enter_data: 9082 case OMPD_target_exit_data: 9083 case OMPD_target_update: 9084 case OMPD_cancel: 9085 case OMPD_parallel: 9086 case OMPD_parallel_sections: 9087 case OMPD_parallel_for: 9088 case OMPD_parallel_for_simd: 9089 case OMPD_target: 9090 case OMPD_target_simd: 9091 case OMPD_target_parallel: 9092 case OMPD_target_parallel_for: 9093 case OMPD_target_parallel_for_simd: 9094 case OMPD_threadprivate: 9095 case OMPD_allocate: 9096 case OMPD_taskyield: 9097 case OMPD_barrier: 9098 case OMPD_taskwait: 9099 case OMPD_cancellation_point: 9100 case OMPD_flush: 9101 case OMPD_declare_reduction: 9102 case OMPD_declare_mapper: 9103 case OMPD_declare_simd: 9104 case OMPD_declare_target: 9105 case OMPD_end_declare_target: 9106 case OMPD_simd: 9107 case OMPD_for: 9108 case OMPD_for_simd: 9109 case OMPD_sections: 9110 case OMPD_section: 9111 case OMPD_single: 9112 case OMPD_master: 9113 case OMPD_critical: 9114 case OMPD_taskgroup: 9115 case OMPD_distribute: 9116 case OMPD_ordered: 9117 case OMPD_atomic: 9118 case OMPD_distribute_simd: 9119 case OMPD_requires: 9120 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 9121 case OMPD_unknown: 9122 llvm_unreachable("Unknown OpenMP directive"); 9123 } 9124 break; 9125 case OMPC_thread_limit: 9126 switch (DKind) { 9127 case OMPD_target_teams: 9128 case OMPD_target_teams_distribute: 9129 case OMPD_target_teams_distribute_simd: 9130 case OMPD_target_teams_distribute_parallel_for: 9131 case OMPD_target_teams_distribute_parallel_for_simd: 9132 CaptureRegion = OMPD_target; 9133 break; 9134 case OMPD_teams_distribute_parallel_for: 9135 case OMPD_teams_distribute_parallel_for_simd: 9136 case OMPD_teams: 9137 case OMPD_teams_distribute: 9138 case OMPD_teams_distribute_simd: 9139 // Do not capture thread_limit-clause expressions. 9140 break; 9141 case OMPD_distribute_parallel_for: 9142 case OMPD_distribute_parallel_for_simd: 9143 case OMPD_task: 9144 case OMPD_taskloop: 9145 case OMPD_taskloop_simd: 9146 case OMPD_target_data: 9147 case OMPD_target_enter_data: 9148 case OMPD_target_exit_data: 9149 case OMPD_target_update: 9150 case OMPD_cancel: 9151 case OMPD_parallel: 9152 case OMPD_parallel_sections: 9153 case OMPD_parallel_for: 9154 case OMPD_parallel_for_simd: 9155 case OMPD_target: 9156 case OMPD_target_simd: 9157 case OMPD_target_parallel: 9158 case OMPD_target_parallel_for: 9159 case OMPD_target_parallel_for_simd: 9160 case OMPD_threadprivate: 9161 case OMPD_allocate: 9162 case OMPD_taskyield: 9163 case OMPD_barrier: 9164 case OMPD_taskwait: 9165 case OMPD_cancellation_point: 9166 case OMPD_flush: 9167 case OMPD_declare_reduction: 9168 case OMPD_declare_mapper: 9169 case OMPD_declare_simd: 9170 case OMPD_declare_target: 9171 case OMPD_end_declare_target: 9172 case OMPD_simd: 9173 case OMPD_for: 9174 case OMPD_for_simd: 9175 case OMPD_sections: 9176 case OMPD_section: 9177 case OMPD_single: 9178 case OMPD_master: 9179 case OMPD_critical: 9180 case OMPD_taskgroup: 9181 case OMPD_distribute: 9182 case OMPD_ordered: 9183 case OMPD_atomic: 9184 case OMPD_distribute_simd: 9185 case OMPD_requires: 9186 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 9187 case OMPD_unknown: 9188 llvm_unreachable("Unknown OpenMP directive"); 9189 } 9190 break; 9191 case OMPC_schedule: 9192 switch (DKind) { 9193 case OMPD_parallel_for: 9194 case OMPD_parallel_for_simd: 9195 case OMPD_distribute_parallel_for: 9196 case OMPD_distribute_parallel_for_simd: 9197 case OMPD_teams_distribute_parallel_for: 9198 case OMPD_teams_distribute_parallel_for_simd: 9199 case OMPD_target_parallel_for: 9200 case OMPD_target_parallel_for_simd: 9201 case OMPD_target_teams_distribute_parallel_for: 9202 case OMPD_target_teams_distribute_parallel_for_simd: 9203 CaptureRegion = OMPD_parallel; 9204 break; 9205 case OMPD_for: 9206 case OMPD_for_simd: 9207 // Do not capture schedule-clause expressions. 9208 break; 9209 case OMPD_task: 9210 case OMPD_taskloop: 9211 case OMPD_taskloop_simd: 9212 case OMPD_target_data: 9213 case OMPD_target_enter_data: 9214 case OMPD_target_exit_data: 9215 case OMPD_target_update: 9216 case OMPD_teams: 9217 case OMPD_teams_distribute: 9218 case OMPD_teams_distribute_simd: 9219 case OMPD_target_teams_distribute: 9220 case OMPD_target_teams_distribute_simd: 9221 case OMPD_target: 9222 case OMPD_target_simd: 9223 case OMPD_target_parallel: 9224 case OMPD_cancel: 9225 case OMPD_parallel: 9226 case OMPD_parallel_sections: 9227 case OMPD_threadprivate: 9228 case OMPD_allocate: 9229 case OMPD_taskyield: 9230 case OMPD_barrier: 9231 case OMPD_taskwait: 9232 case OMPD_cancellation_point: 9233 case OMPD_flush: 9234 case OMPD_declare_reduction: 9235 case OMPD_declare_mapper: 9236 case OMPD_declare_simd: 9237 case OMPD_declare_target: 9238 case OMPD_end_declare_target: 9239 case OMPD_simd: 9240 case OMPD_sections: 9241 case OMPD_section: 9242 case OMPD_single: 9243 case OMPD_master: 9244 case OMPD_critical: 9245 case OMPD_taskgroup: 9246 case OMPD_distribute: 9247 case OMPD_ordered: 9248 case OMPD_atomic: 9249 case OMPD_distribute_simd: 9250 case OMPD_target_teams: 9251 case OMPD_requires: 9252 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 9253 case OMPD_unknown: 9254 llvm_unreachable("Unknown OpenMP directive"); 9255 } 9256 break; 9257 case OMPC_dist_schedule: 9258 switch (DKind) { 9259 case OMPD_teams_distribute_parallel_for: 9260 case OMPD_teams_distribute_parallel_for_simd: 9261 case OMPD_teams_distribute: 9262 case OMPD_teams_distribute_simd: 9263 case OMPD_target_teams_distribute_parallel_for: 9264 case OMPD_target_teams_distribute_parallel_for_simd: 9265 case OMPD_target_teams_distribute: 9266 case OMPD_target_teams_distribute_simd: 9267 CaptureRegion = OMPD_teams; 9268 break; 9269 case OMPD_distribute_parallel_for: 9270 case OMPD_distribute_parallel_for_simd: 9271 case OMPD_distribute: 9272 case OMPD_distribute_simd: 9273 // Do not capture thread_limit-clause expressions. 9274 break; 9275 case OMPD_parallel_for: 9276 case OMPD_parallel_for_simd: 9277 case OMPD_target_parallel_for_simd: 9278 case OMPD_target_parallel_for: 9279 case OMPD_task: 9280 case OMPD_taskloop: 9281 case OMPD_taskloop_simd: 9282 case OMPD_target_data: 9283 case OMPD_target_enter_data: 9284 case OMPD_target_exit_data: 9285 case OMPD_target_update: 9286 case OMPD_teams: 9287 case OMPD_target: 9288 case OMPD_target_simd: 9289 case OMPD_target_parallel: 9290 case OMPD_cancel: 9291 case OMPD_parallel: 9292 case OMPD_parallel_sections: 9293 case OMPD_threadprivate: 9294 case OMPD_allocate: 9295 case OMPD_taskyield: 9296 case OMPD_barrier: 9297 case OMPD_taskwait: 9298 case OMPD_cancellation_point: 9299 case OMPD_flush: 9300 case OMPD_declare_reduction: 9301 case OMPD_declare_mapper: 9302 case OMPD_declare_simd: 9303 case OMPD_declare_target: 9304 case OMPD_end_declare_target: 9305 case OMPD_simd: 9306 case OMPD_for: 9307 case OMPD_for_simd: 9308 case OMPD_sections: 9309 case OMPD_section: 9310 case OMPD_single: 9311 case OMPD_master: 9312 case OMPD_critical: 9313 case OMPD_taskgroup: 9314 case OMPD_ordered: 9315 case OMPD_atomic: 9316 case OMPD_target_teams: 9317 case OMPD_requires: 9318 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 9319 case OMPD_unknown: 9320 llvm_unreachable("Unknown OpenMP directive"); 9321 } 9322 break; 9323 case OMPC_device: 9324 switch (DKind) { 9325 case OMPD_target_update: 9326 case OMPD_target_enter_data: 9327 case OMPD_target_exit_data: 9328 case OMPD_target: 9329 case OMPD_target_simd: 9330 case OMPD_target_teams: 9331 case OMPD_target_parallel: 9332 case OMPD_target_teams_distribute: 9333 case OMPD_target_teams_distribute_simd: 9334 case OMPD_target_parallel_for: 9335 case OMPD_target_parallel_for_simd: 9336 case OMPD_target_teams_distribute_parallel_for: 9337 case OMPD_target_teams_distribute_parallel_for_simd: 9338 CaptureRegion = OMPD_task; 9339 break; 9340 case OMPD_target_data: 9341 // Do not capture device-clause expressions. 9342 break; 9343 case OMPD_teams_distribute_parallel_for: 9344 case OMPD_teams_distribute_parallel_for_simd: 9345 case OMPD_teams: 9346 case OMPD_teams_distribute: 9347 case OMPD_teams_distribute_simd: 9348 case OMPD_distribute_parallel_for: 9349 case OMPD_distribute_parallel_for_simd: 9350 case OMPD_task: 9351 case OMPD_taskloop: 9352 case OMPD_taskloop_simd: 9353 case OMPD_cancel: 9354 case OMPD_parallel: 9355 case OMPD_parallel_sections: 9356 case OMPD_parallel_for: 9357 case OMPD_parallel_for_simd: 9358 case OMPD_threadprivate: 9359 case OMPD_allocate: 9360 case OMPD_taskyield: 9361 case OMPD_barrier: 9362 case OMPD_taskwait: 9363 case OMPD_cancellation_point: 9364 case OMPD_flush: 9365 case OMPD_declare_reduction: 9366 case OMPD_declare_mapper: 9367 case OMPD_declare_simd: 9368 case OMPD_declare_target: 9369 case OMPD_end_declare_target: 9370 case OMPD_simd: 9371 case OMPD_for: 9372 case OMPD_for_simd: 9373 case OMPD_sections: 9374 case OMPD_section: 9375 case OMPD_single: 9376 case OMPD_master: 9377 case OMPD_critical: 9378 case OMPD_taskgroup: 9379 case OMPD_distribute: 9380 case OMPD_ordered: 9381 case OMPD_atomic: 9382 case OMPD_distribute_simd: 9383 case OMPD_requires: 9384 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 9385 case OMPD_unknown: 9386 llvm_unreachable("Unknown OpenMP directive"); 9387 } 9388 break; 9389 case OMPC_firstprivate: 9390 case OMPC_lastprivate: 9391 case OMPC_reduction: 9392 case OMPC_task_reduction: 9393 case OMPC_in_reduction: 9394 case OMPC_linear: 9395 case OMPC_default: 9396 case OMPC_proc_bind: 9397 case OMPC_final: 9398 case OMPC_safelen: 9399 case OMPC_simdlen: 9400 case OMPC_allocator: 9401 case OMPC_collapse: 9402 case OMPC_private: 9403 case OMPC_shared: 9404 case OMPC_aligned: 9405 case OMPC_copyin: 9406 case OMPC_copyprivate: 9407 case OMPC_ordered: 9408 case OMPC_nowait: 9409 case OMPC_untied: 9410 case OMPC_mergeable: 9411 case OMPC_threadprivate: 9412 case OMPC_allocate: 9413 case OMPC_flush: 9414 case OMPC_read: 9415 case OMPC_write: 9416 case OMPC_update: 9417 case OMPC_capture: 9418 case OMPC_seq_cst: 9419 case OMPC_depend: 9420 case OMPC_threads: 9421 case OMPC_simd: 9422 case OMPC_map: 9423 case OMPC_priority: 9424 case OMPC_grainsize: 9425 case OMPC_nogroup: 9426 case OMPC_num_tasks: 9427 case OMPC_hint: 9428 case OMPC_defaultmap: 9429 case OMPC_unknown: 9430 case OMPC_uniform: 9431 case OMPC_to: 9432 case OMPC_from: 9433 case OMPC_use_device_ptr: 9434 case OMPC_is_device_ptr: 9435 case OMPC_unified_address: 9436 case OMPC_unified_shared_memory: 9437 case OMPC_reverse_offload: 9438 case OMPC_dynamic_allocators: 9439 case OMPC_atomic_default_mem_order: 9440 llvm_unreachable("Unexpected OpenMP clause."); 9441 } 9442 return CaptureRegion; 9443 } 9444 9445 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 9446 Expr *Condition, SourceLocation StartLoc, 9447 SourceLocation LParenLoc, 9448 SourceLocation NameModifierLoc, 9449 SourceLocation ColonLoc, 9450 SourceLocation EndLoc) { 9451 Expr *ValExpr = Condition; 9452 Stmt *HelperValStmt = nullptr; 9453 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 9454 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 9455 !Condition->isInstantiationDependent() && 9456 !Condition->containsUnexpandedParameterPack()) { 9457 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 9458 if (Val.isInvalid()) 9459 return nullptr; 9460 9461 ValExpr = Val.get(); 9462 9463 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 9464 CaptureRegion = 9465 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 9466 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 9467 ValExpr = MakeFullExpr(ValExpr).get(); 9468 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9469 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9470 HelperValStmt = buildPreInits(Context, Captures); 9471 } 9472 } 9473 9474 return new (Context) 9475 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 9476 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 9477 } 9478 9479 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 9480 SourceLocation StartLoc, 9481 SourceLocation LParenLoc, 9482 SourceLocation EndLoc) { 9483 Expr *ValExpr = Condition; 9484 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 9485 !Condition->isInstantiationDependent() && 9486 !Condition->containsUnexpandedParameterPack()) { 9487 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 9488 if (Val.isInvalid()) 9489 return nullptr; 9490 9491 ValExpr = MakeFullExpr(Val.get()).get(); 9492 } 9493 9494 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 9495 } 9496 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 9497 Expr *Op) { 9498 if (!Op) 9499 return ExprError(); 9500 9501 class IntConvertDiagnoser : public ICEConvertDiagnoser { 9502 public: 9503 IntConvertDiagnoser() 9504 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 9505 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 9506 QualType T) override { 9507 return S.Diag(Loc, diag::err_omp_not_integral) << T; 9508 } 9509 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 9510 QualType T) override { 9511 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 9512 } 9513 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 9514 QualType T, 9515 QualType ConvTy) override { 9516 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 9517 } 9518 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 9519 QualType ConvTy) override { 9520 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9521 << ConvTy->isEnumeralType() << ConvTy; 9522 } 9523 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 9524 QualType T) override { 9525 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 9526 } 9527 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 9528 QualType ConvTy) override { 9529 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9530 << ConvTy->isEnumeralType() << ConvTy; 9531 } 9532 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 9533 QualType) override { 9534 llvm_unreachable("conversion functions are permitted"); 9535 } 9536 } ConvertDiagnoser; 9537 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 9538 } 9539 9540 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 9541 OpenMPClauseKind CKind, 9542 bool StrictlyPositive) { 9543 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 9544 !ValExpr->isInstantiationDependent()) { 9545 SourceLocation Loc = ValExpr->getExprLoc(); 9546 ExprResult Value = 9547 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 9548 if (Value.isInvalid()) 9549 return false; 9550 9551 ValExpr = Value.get(); 9552 // The expression must evaluate to a non-negative integer value. 9553 llvm::APSInt Result; 9554 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 9555 Result.isSigned() && 9556 !((!StrictlyPositive && Result.isNonNegative()) || 9557 (StrictlyPositive && Result.isStrictlyPositive()))) { 9558 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 9559 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9560 << ValExpr->getSourceRange(); 9561 return false; 9562 } 9563 } 9564 return true; 9565 } 9566 9567 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 9568 SourceLocation StartLoc, 9569 SourceLocation LParenLoc, 9570 SourceLocation EndLoc) { 9571 Expr *ValExpr = NumThreads; 9572 Stmt *HelperValStmt = nullptr; 9573 9574 // OpenMP [2.5, Restrictions] 9575 // The num_threads expression must evaluate to a positive integer value. 9576 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 9577 /*StrictlyPositive=*/true)) 9578 return nullptr; 9579 9580 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 9581 OpenMPDirectiveKind CaptureRegion = 9582 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 9583 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 9584 ValExpr = MakeFullExpr(ValExpr).get(); 9585 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9586 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9587 HelperValStmt = buildPreInits(Context, Captures); 9588 } 9589 9590 return new (Context) OMPNumThreadsClause( 9591 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 9592 } 9593 9594 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 9595 OpenMPClauseKind CKind, 9596 bool StrictlyPositive) { 9597 if (!E) 9598 return ExprError(); 9599 if (E->isValueDependent() || E->isTypeDependent() || 9600 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 9601 return E; 9602 llvm::APSInt Result; 9603 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 9604 if (ICE.isInvalid()) 9605 return ExprError(); 9606 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 9607 (!StrictlyPositive && !Result.isNonNegative())) { 9608 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 9609 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9610 << E->getSourceRange(); 9611 return ExprError(); 9612 } 9613 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 9614 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 9615 << E->getSourceRange(); 9616 return ExprError(); 9617 } 9618 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 9619 DSAStack->setAssociatedLoops(Result.getExtValue()); 9620 else if (CKind == OMPC_ordered) 9621 DSAStack->setAssociatedLoops(Result.getExtValue()); 9622 return ICE; 9623 } 9624 9625 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 9626 SourceLocation LParenLoc, 9627 SourceLocation EndLoc) { 9628 // OpenMP [2.8.1, simd construct, Description] 9629 // The parameter of the safelen clause must be a constant 9630 // positive integer expression. 9631 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 9632 if (Safelen.isInvalid()) 9633 return nullptr; 9634 return new (Context) 9635 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 9636 } 9637 9638 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 9639 SourceLocation LParenLoc, 9640 SourceLocation EndLoc) { 9641 // OpenMP [2.8.1, simd construct, Description] 9642 // The parameter of the simdlen clause must be a constant 9643 // positive integer expression. 9644 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 9645 if (Simdlen.isInvalid()) 9646 return nullptr; 9647 return new (Context) 9648 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 9649 } 9650 9651 /// Tries to find omp_allocator_handle_t type. 9652 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 9653 DSAStackTy *Stack) { 9654 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 9655 if (!OMPAllocatorHandleT.isNull()) 9656 return true; 9657 // Build the predefined allocator expressions. 9658 bool ErrorFound = false; 9659 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 9660 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 9661 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 9662 StringRef Allocator = 9663 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 9664 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 9665 auto *VD = dyn_cast_or_null<ValueDecl>( 9666 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 9667 if (!VD) { 9668 ErrorFound = true; 9669 break; 9670 } 9671 QualType AllocatorType = 9672 VD->getType().getNonLValueExprType(S.getASTContext()); 9673 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 9674 if (!Res.isUsable()) { 9675 ErrorFound = true; 9676 break; 9677 } 9678 if (OMPAllocatorHandleT.isNull()) 9679 OMPAllocatorHandleT = AllocatorType; 9680 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 9681 ErrorFound = true; 9682 break; 9683 } 9684 Stack->setAllocator(AllocatorKind, Res.get()); 9685 } 9686 if (ErrorFound) { 9687 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found); 9688 return false; 9689 } 9690 OMPAllocatorHandleT.addConst(); 9691 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 9692 return true; 9693 } 9694 9695 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 9696 SourceLocation LParenLoc, 9697 SourceLocation EndLoc) { 9698 // OpenMP [2.11.3, allocate Directive, Description] 9699 // allocator is an expression of omp_allocator_handle_t type. 9700 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 9701 return nullptr; 9702 9703 ExprResult Allocator = DefaultLvalueConversion(A); 9704 if (Allocator.isInvalid()) 9705 return nullptr; 9706 Allocator = PerformImplicitConversion(Allocator.get(), 9707 DSAStack->getOMPAllocatorHandleT(), 9708 Sema::AA_Initializing, 9709 /*AllowExplicit=*/true); 9710 if (Allocator.isInvalid()) 9711 return nullptr; 9712 return new (Context) 9713 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 9714 } 9715 9716 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 9717 SourceLocation StartLoc, 9718 SourceLocation LParenLoc, 9719 SourceLocation EndLoc) { 9720 // OpenMP [2.7.1, loop construct, Description] 9721 // OpenMP [2.8.1, simd construct, Description] 9722 // OpenMP [2.9.6, distribute construct, Description] 9723 // The parameter of the collapse clause must be a constant 9724 // positive integer expression. 9725 ExprResult NumForLoopsResult = 9726 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 9727 if (NumForLoopsResult.isInvalid()) 9728 return nullptr; 9729 return new (Context) 9730 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 9731 } 9732 9733 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 9734 SourceLocation EndLoc, 9735 SourceLocation LParenLoc, 9736 Expr *NumForLoops) { 9737 // OpenMP [2.7.1, loop construct, Description] 9738 // OpenMP [2.8.1, simd construct, Description] 9739 // OpenMP [2.9.6, distribute construct, Description] 9740 // The parameter of the ordered clause must be a constant 9741 // positive integer expression if any. 9742 if (NumForLoops && LParenLoc.isValid()) { 9743 ExprResult NumForLoopsResult = 9744 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 9745 if (NumForLoopsResult.isInvalid()) 9746 return nullptr; 9747 NumForLoops = NumForLoopsResult.get(); 9748 } else { 9749 NumForLoops = nullptr; 9750 } 9751 auto *Clause = OMPOrderedClause::Create( 9752 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 9753 StartLoc, LParenLoc, EndLoc); 9754 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 9755 return Clause; 9756 } 9757 9758 OMPClause *Sema::ActOnOpenMPSimpleClause( 9759 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 9760 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 9761 OMPClause *Res = nullptr; 9762 switch (Kind) { 9763 case OMPC_default: 9764 Res = 9765 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 9766 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 9767 break; 9768 case OMPC_proc_bind: 9769 Res = ActOnOpenMPProcBindClause( 9770 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 9771 LParenLoc, EndLoc); 9772 break; 9773 case OMPC_atomic_default_mem_order: 9774 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 9775 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 9776 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 9777 break; 9778 case OMPC_if: 9779 case OMPC_final: 9780 case OMPC_num_threads: 9781 case OMPC_safelen: 9782 case OMPC_simdlen: 9783 case OMPC_allocator: 9784 case OMPC_collapse: 9785 case OMPC_schedule: 9786 case OMPC_private: 9787 case OMPC_firstprivate: 9788 case OMPC_lastprivate: 9789 case OMPC_shared: 9790 case OMPC_reduction: 9791 case OMPC_task_reduction: 9792 case OMPC_in_reduction: 9793 case OMPC_linear: 9794 case OMPC_aligned: 9795 case OMPC_copyin: 9796 case OMPC_copyprivate: 9797 case OMPC_ordered: 9798 case OMPC_nowait: 9799 case OMPC_untied: 9800 case OMPC_mergeable: 9801 case OMPC_threadprivate: 9802 case OMPC_allocate: 9803 case OMPC_flush: 9804 case OMPC_read: 9805 case OMPC_write: 9806 case OMPC_update: 9807 case OMPC_capture: 9808 case OMPC_seq_cst: 9809 case OMPC_depend: 9810 case OMPC_device: 9811 case OMPC_threads: 9812 case OMPC_simd: 9813 case OMPC_map: 9814 case OMPC_num_teams: 9815 case OMPC_thread_limit: 9816 case OMPC_priority: 9817 case OMPC_grainsize: 9818 case OMPC_nogroup: 9819 case OMPC_num_tasks: 9820 case OMPC_hint: 9821 case OMPC_dist_schedule: 9822 case OMPC_defaultmap: 9823 case OMPC_unknown: 9824 case OMPC_uniform: 9825 case OMPC_to: 9826 case OMPC_from: 9827 case OMPC_use_device_ptr: 9828 case OMPC_is_device_ptr: 9829 case OMPC_unified_address: 9830 case OMPC_unified_shared_memory: 9831 case OMPC_reverse_offload: 9832 case OMPC_dynamic_allocators: 9833 llvm_unreachable("Clause is not allowed."); 9834 } 9835 return Res; 9836 } 9837 9838 static std::string 9839 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 9840 ArrayRef<unsigned> Exclude = llvm::None) { 9841 SmallString<256> Buffer; 9842 llvm::raw_svector_ostream Out(Buffer); 9843 unsigned Bound = Last >= 2 ? Last - 2 : 0; 9844 unsigned Skipped = Exclude.size(); 9845 auto S = Exclude.begin(), E = Exclude.end(); 9846 for (unsigned I = First; I < Last; ++I) { 9847 if (std::find(S, E, I) != E) { 9848 --Skipped; 9849 continue; 9850 } 9851 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 9852 if (I == Bound - Skipped) 9853 Out << " or "; 9854 else if (I != Bound + 1 - Skipped) 9855 Out << ", "; 9856 } 9857 return Out.str(); 9858 } 9859 9860 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 9861 SourceLocation KindKwLoc, 9862 SourceLocation StartLoc, 9863 SourceLocation LParenLoc, 9864 SourceLocation EndLoc) { 9865 if (Kind == OMPC_DEFAULT_unknown) { 9866 static_assert(OMPC_DEFAULT_unknown > 0, 9867 "OMPC_DEFAULT_unknown not greater than 0"); 9868 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9869 << getListOfPossibleValues(OMPC_default, /*First=*/0, 9870 /*Last=*/OMPC_DEFAULT_unknown) 9871 << getOpenMPClauseName(OMPC_default); 9872 return nullptr; 9873 } 9874 switch (Kind) { 9875 case OMPC_DEFAULT_none: 9876 DSAStack->setDefaultDSANone(KindKwLoc); 9877 break; 9878 case OMPC_DEFAULT_shared: 9879 DSAStack->setDefaultDSAShared(KindKwLoc); 9880 break; 9881 case OMPC_DEFAULT_unknown: 9882 llvm_unreachable("Clause kind is not allowed."); 9883 break; 9884 } 9885 return new (Context) 9886 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9887 } 9888 9889 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 9890 SourceLocation KindKwLoc, 9891 SourceLocation StartLoc, 9892 SourceLocation LParenLoc, 9893 SourceLocation EndLoc) { 9894 if (Kind == OMPC_PROC_BIND_unknown) { 9895 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9896 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 9897 /*Last=*/OMPC_PROC_BIND_unknown) 9898 << getOpenMPClauseName(OMPC_proc_bind); 9899 return nullptr; 9900 } 9901 return new (Context) 9902 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9903 } 9904 9905 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 9906 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 9907 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 9908 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 9909 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9910 << getListOfPossibleValues( 9911 OMPC_atomic_default_mem_order, /*First=*/0, 9912 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 9913 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 9914 return nullptr; 9915 } 9916 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 9917 LParenLoc, EndLoc); 9918 } 9919 9920 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 9921 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 9922 SourceLocation StartLoc, SourceLocation LParenLoc, 9923 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 9924 SourceLocation EndLoc) { 9925 OMPClause *Res = nullptr; 9926 switch (Kind) { 9927 case OMPC_schedule: 9928 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 9929 assert(Argument.size() == NumberOfElements && 9930 ArgumentLoc.size() == NumberOfElements); 9931 Res = ActOnOpenMPScheduleClause( 9932 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 9933 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 9934 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 9935 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 9936 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 9937 break; 9938 case OMPC_if: 9939 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 9940 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 9941 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 9942 DelimLoc, EndLoc); 9943 break; 9944 case OMPC_dist_schedule: 9945 Res = ActOnOpenMPDistScheduleClause( 9946 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 9947 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 9948 break; 9949 case OMPC_defaultmap: 9950 enum { Modifier, DefaultmapKind }; 9951 Res = ActOnOpenMPDefaultmapClause( 9952 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 9953 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 9954 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 9955 EndLoc); 9956 break; 9957 case OMPC_final: 9958 case OMPC_num_threads: 9959 case OMPC_safelen: 9960 case OMPC_simdlen: 9961 case OMPC_allocator: 9962 case OMPC_collapse: 9963 case OMPC_default: 9964 case OMPC_proc_bind: 9965 case OMPC_private: 9966 case OMPC_firstprivate: 9967 case OMPC_lastprivate: 9968 case OMPC_shared: 9969 case OMPC_reduction: 9970 case OMPC_task_reduction: 9971 case OMPC_in_reduction: 9972 case OMPC_linear: 9973 case OMPC_aligned: 9974 case OMPC_copyin: 9975 case OMPC_copyprivate: 9976 case OMPC_ordered: 9977 case OMPC_nowait: 9978 case OMPC_untied: 9979 case OMPC_mergeable: 9980 case OMPC_threadprivate: 9981 case OMPC_allocate: 9982 case OMPC_flush: 9983 case OMPC_read: 9984 case OMPC_write: 9985 case OMPC_update: 9986 case OMPC_capture: 9987 case OMPC_seq_cst: 9988 case OMPC_depend: 9989 case OMPC_device: 9990 case OMPC_threads: 9991 case OMPC_simd: 9992 case OMPC_map: 9993 case OMPC_num_teams: 9994 case OMPC_thread_limit: 9995 case OMPC_priority: 9996 case OMPC_grainsize: 9997 case OMPC_nogroup: 9998 case OMPC_num_tasks: 9999 case OMPC_hint: 10000 case OMPC_unknown: 10001 case OMPC_uniform: 10002 case OMPC_to: 10003 case OMPC_from: 10004 case OMPC_use_device_ptr: 10005 case OMPC_is_device_ptr: 10006 case OMPC_unified_address: 10007 case OMPC_unified_shared_memory: 10008 case OMPC_reverse_offload: 10009 case OMPC_dynamic_allocators: 10010 case OMPC_atomic_default_mem_order: 10011 llvm_unreachable("Clause is not allowed."); 10012 } 10013 return Res; 10014 } 10015 10016 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 10017 OpenMPScheduleClauseModifier M2, 10018 SourceLocation M1Loc, SourceLocation M2Loc) { 10019 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 10020 SmallVector<unsigned, 2> Excluded; 10021 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 10022 Excluded.push_back(M2); 10023 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 10024 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 10025 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 10026 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 10027 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 10028 << getListOfPossibleValues(OMPC_schedule, 10029 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 10030 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 10031 Excluded) 10032 << getOpenMPClauseName(OMPC_schedule); 10033 return true; 10034 } 10035 return false; 10036 } 10037 10038 OMPClause *Sema::ActOnOpenMPScheduleClause( 10039 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 10040 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 10041 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 10042 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 10043 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 10044 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 10045 return nullptr; 10046 // OpenMP, 2.7.1, Loop Construct, Restrictions 10047 // Either the monotonic modifier or the nonmonotonic modifier can be specified 10048 // but not both. 10049 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 10050 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 10051 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 10052 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 10053 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 10054 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 10055 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 10056 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 10057 return nullptr; 10058 } 10059 if (Kind == OMPC_SCHEDULE_unknown) { 10060 std::string Values; 10061 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 10062 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 10063 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 10064 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 10065 Exclude); 10066 } else { 10067 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 10068 /*Last=*/OMPC_SCHEDULE_unknown); 10069 } 10070 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 10071 << Values << getOpenMPClauseName(OMPC_schedule); 10072 return nullptr; 10073 } 10074 // OpenMP, 2.7.1, Loop Construct, Restrictions 10075 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 10076 // schedule(guided). 10077 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 10078 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 10079 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 10080 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 10081 diag::err_omp_schedule_nonmonotonic_static); 10082 return nullptr; 10083 } 10084 Expr *ValExpr = ChunkSize; 10085 Stmt *HelperValStmt = nullptr; 10086 if (ChunkSize) { 10087 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 10088 !ChunkSize->isInstantiationDependent() && 10089 !ChunkSize->containsUnexpandedParameterPack()) { 10090 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 10091 ExprResult Val = 10092 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 10093 if (Val.isInvalid()) 10094 return nullptr; 10095 10096 ValExpr = Val.get(); 10097 10098 // OpenMP [2.7.1, Restrictions] 10099 // chunk_size must be a loop invariant integer expression with a positive 10100 // value. 10101 llvm::APSInt Result; 10102 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 10103 if (Result.isSigned() && !Result.isStrictlyPositive()) { 10104 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 10105 << "schedule" << 1 << ChunkSize->getSourceRange(); 10106 return nullptr; 10107 } 10108 } else if (getOpenMPCaptureRegionForClause( 10109 DSAStack->getCurrentDirective(), OMPC_schedule) != 10110 OMPD_unknown && 10111 !CurContext->isDependentContext()) { 10112 ValExpr = MakeFullExpr(ValExpr).get(); 10113 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 10114 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 10115 HelperValStmt = buildPreInits(Context, Captures); 10116 } 10117 } 10118 } 10119 10120 return new (Context) 10121 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 10122 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 10123 } 10124 10125 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 10126 SourceLocation StartLoc, 10127 SourceLocation EndLoc) { 10128 OMPClause *Res = nullptr; 10129 switch (Kind) { 10130 case OMPC_ordered: 10131 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 10132 break; 10133 case OMPC_nowait: 10134 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 10135 break; 10136 case OMPC_untied: 10137 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 10138 break; 10139 case OMPC_mergeable: 10140 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 10141 break; 10142 case OMPC_read: 10143 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 10144 break; 10145 case OMPC_write: 10146 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 10147 break; 10148 case OMPC_update: 10149 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 10150 break; 10151 case OMPC_capture: 10152 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 10153 break; 10154 case OMPC_seq_cst: 10155 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 10156 break; 10157 case OMPC_threads: 10158 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 10159 break; 10160 case OMPC_simd: 10161 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 10162 break; 10163 case OMPC_nogroup: 10164 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 10165 break; 10166 case OMPC_unified_address: 10167 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 10168 break; 10169 case OMPC_unified_shared_memory: 10170 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 10171 break; 10172 case OMPC_reverse_offload: 10173 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 10174 break; 10175 case OMPC_dynamic_allocators: 10176 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 10177 break; 10178 case OMPC_if: 10179 case OMPC_final: 10180 case OMPC_num_threads: 10181 case OMPC_safelen: 10182 case OMPC_simdlen: 10183 case OMPC_allocator: 10184 case OMPC_collapse: 10185 case OMPC_schedule: 10186 case OMPC_private: 10187 case OMPC_firstprivate: 10188 case OMPC_lastprivate: 10189 case OMPC_shared: 10190 case OMPC_reduction: 10191 case OMPC_task_reduction: 10192 case OMPC_in_reduction: 10193 case OMPC_linear: 10194 case OMPC_aligned: 10195 case OMPC_copyin: 10196 case OMPC_copyprivate: 10197 case OMPC_default: 10198 case OMPC_proc_bind: 10199 case OMPC_threadprivate: 10200 case OMPC_allocate: 10201 case OMPC_flush: 10202 case OMPC_depend: 10203 case OMPC_device: 10204 case OMPC_map: 10205 case OMPC_num_teams: 10206 case OMPC_thread_limit: 10207 case OMPC_priority: 10208 case OMPC_grainsize: 10209 case OMPC_num_tasks: 10210 case OMPC_hint: 10211 case OMPC_dist_schedule: 10212 case OMPC_defaultmap: 10213 case OMPC_unknown: 10214 case OMPC_uniform: 10215 case OMPC_to: 10216 case OMPC_from: 10217 case OMPC_use_device_ptr: 10218 case OMPC_is_device_ptr: 10219 case OMPC_atomic_default_mem_order: 10220 llvm_unreachable("Clause is not allowed."); 10221 } 10222 return Res; 10223 } 10224 10225 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 10226 SourceLocation EndLoc) { 10227 DSAStack->setNowaitRegion(); 10228 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 10229 } 10230 10231 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 10232 SourceLocation EndLoc) { 10233 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 10234 } 10235 10236 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 10237 SourceLocation EndLoc) { 10238 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 10239 } 10240 10241 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 10242 SourceLocation EndLoc) { 10243 return new (Context) OMPReadClause(StartLoc, EndLoc); 10244 } 10245 10246 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 10247 SourceLocation EndLoc) { 10248 return new (Context) OMPWriteClause(StartLoc, EndLoc); 10249 } 10250 10251 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 10252 SourceLocation EndLoc) { 10253 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 10254 } 10255 10256 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 10257 SourceLocation EndLoc) { 10258 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 10259 } 10260 10261 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 10262 SourceLocation EndLoc) { 10263 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 10264 } 10265 10266 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 10267 SourceLocation EndLoc) { 10268 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 10269 } 10270 10271 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 10272 SourceLocation EndLoc) { 10273 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 10274 } 10275 10276 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 10277 SourceLocation EndLoc) { 10278 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 10279 } 10280 10281 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 10282 SourceLocation EndLoc) { 10283 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 10284 } 10285 10286 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 10287 SourceLocation EndLoc) { 10288 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 10289 } 10290 10291 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 10292 SourceLocation EndLoc) { 10293 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 10294 } 10295 10296 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 10297 SourceLocation EndLoc) { 10298 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 10299 } 10300 10301 OMPClause *Sema::ActOnOpenMPVarListClause( 10302 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 10303 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 10304 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 10305 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind, 10306 OpenMPLinearClauseKind LinKind, 10307 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 10308 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType, 10309 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) { 10310 SourceLocation StartLoc = Locs.StartLoc; 10311 SourceLocation LParenLoc = Locs.LParenLoc; 10312 SourceLocation EndLoc = Locs.EndLoc; 10313 OMPClause *Res = nullptr; 10314 switch (Kind) { 10315 case OMPC_private: 10316 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10317 break; 10318 case OMPC_firstprivate: 10319 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10320 break; 10321 case OMPC_lastprivate: 10322 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10323 break; 10324 case OMPC_shared: 10325 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 10326 break; 10327 case OMPC_reduction: 10328 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10329 EndLoc, ReductionOrMapperIdScopeSpec, 10330 ReductionOrMapperId); 10331 break; 10332 case OMPC_task_reduction: 10333 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10334 EndLoc, ReductionOrMapperIdScopeSpec, 10335 ReductionOrMapperId); 10336 break; 10337 case OMPC_in_reduction: 10338 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10339 EndLoc, ReductionOrMapperIdScopeSpec, 10340 ReductionOrMapperId); 10341 break; 10342 case OMPC_linear: 10343 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 10344 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 10345 break; 10346 case OMPC_aligned: 10347 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 10348 ColonLoc, EndLoc); 10349 break; 10350 case OMPC_copyin: 10351 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 10352 break; 10353 case OMPC_copyprivate: 10354 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10355 break; 10356 case OMPC_flush: 10357 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 10358 break; 10359 case OMPC_depend: 10360 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 10361 StartLoc, LParenLoc, EndLoc); 10362 break; 10363 case OMPC_map: 10364 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, 10365 ReductionOrMapperIdScopeSpec, 10366 ReductionOrMapperId, MapType, IsMapTypeImplicit, 10367 DepLinMapLoc, ColonLoc, VarList, Locs); 10368 break; 10369 case OMPC_to: 10370 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec, 10371 ReductionOrMapperId, Locs); 10372 break; 10373 case OMPC_from: 10374 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec, 10375 ReductionOrMapperId, Locs); 10376 break; 10377 case OMPC_use_device_ptr: 10378 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 10379 break; 10380 case OMPC_is_device_ptr: 10381 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 10382 break; 10383 case OMPC_allocate: 10384 Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc, 10385 ColonLoc, EndLoc); 10386 break; 10387 case OMPC_if: 10388 case OMPC_final: 10389 case OMPC_num_threads: 10390 case OMPC_safelen: 10391 case OMPC_simdlen: 10392 case OMPC_allocator: 10393 case OMPC_collapse: 10394 case OMPC_default: 10395 case OMPC_proc_bind: 10396 case OMPC_schedule: 10397 case OMPC_ordered: 10398 case OMPC_nowait: 10399 case OMPC_untied: 10400 case OMPC_mergeable: 10401 case OMPC_threadprivate: 10402 case OMPC_read: 10403 case OMPC_write: 10404 case OMPC_update: 10405 case OMPC_capture: 10406 case OMPC_seq_cst: 10407 case OMPC_device: 10408 case OMPC_threads: 10409 case OMPC_simd: 10410 case OMPC_num_teams: 10411 case OMPC_thread_limit: 10412 case OMPC_priority: 10413 case OMPC_grainsize: 10414 case OMPC_nogroup: 10415 case OMPC_num_tasks: 10416 case OMPC_hint: 10417 case OMPC_dist_schedule: 10418 case OMPC_defaultmap: 10419 case OMPC_unknown: 10420 case OMPC_uniform: 10421 case OMPC_unified_address: 10422 case OMPC_unified_shared_memory: 10423 case OMPC_reverse_offload: 10424 case OMPC_dynamic_allocators: 10425 case OMPC_atomic_default_mem_order: 10426 llvm_unreachable("Clause is not allowed."); 10427 } 10428 return Res; 10429 } 10430 10431 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 10432 ExprObjectKind OK, SourceLocation Loc) { 10433 ExprResult Res = BuildDeclRefExpr( 10434 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 10435 if (!Res.isUsable()) 10436 return ExprError(); 10437 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 10438 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 10439 if (!Res.isUsable()) 10440 return ExprError(); 10441 } 10442 if (VK != VK_LValue && Res.get()->isGLValue()) { 10443 Res = DefaultLvalueConversion(Res.get()); 10444 if (!Res.isUsable()) 10445 return ExprError(); 10446 } 10447 return Res; 10448 } 10449 10450 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 10451 SourceLocation StartLoc, 10452 SourceLocation LParenLoc, 10453 SourceLocation EndLoc) { 10454 SmallVector<Expr *, 8> Vars; 10455 SmallVector<Expr *, 8> PrivateCopies; 10456 for (Expr *RefExpr : VarList) { 10457 assert(RefExpr && "NULL expr in OpenMP private clause."); 10458 SourceLocation ELoc; 10459 SourceRange ERange; 10460 Expr *SimpleRefExpr = RefExpr; 10461 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10462 if (Res.second) { 10463 // It will be analyzed later. 10464 Vars.push_back(RefExpr); 10465 PrivateCopies.push_back(nullptr); 10466 } 10467 ValueDecl *D = Res.first; 10468 if (!D) 10469 continue; 10470 10471 QualType Type = D->getType(); 10472 auto *VD = dyn_cast<VarDecl>(D); 10473 10474 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10475 // A variable that appears in a private clause must not have an incomplete 10476 // type or a reference type. 10477 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 10478 continue; 10479 Type = Type.getNonReferenceType(); 10480 10481 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 10482 // A variable that is privatized must not have a const-qualified type 10483 // unless it is of class type with a mutable member. This restriction does 10484 // not apply to the firstprivate clause. 10485 // 10486 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 10487 // A variable that appears in a private clause must not have a 10488 // const-qualified type unless it is of class type with a mutable member. 10489 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 10490 continue; 10491 10492 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10493 // in a Construct] 10494 // Variables with the predetermined data-sharing attributes may not be 10495 // listed in data-sharing attributes clauses, except for the cases 10496 // listed below. For these exceptions only, listing a predetermined 10497 // variable in a data-sharing attribute clause is allowed and overrides 10498 // the variable's predetermined data-sharing attributes. 10499 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10500 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 10501 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10502 << getOpenMPClauseName(OMPC_private); 10503 reportOriginalDsa(*this, DSAStack, D, DVar); 10504 continue; 10505 } 10506 10507 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10508 // Variably modified types are not supported for tasks. 10509 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10510 isOpenMPTaskingDirective(CurrDir)) { 10511 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10512 << getOpenMPClauseName(OMPC_private) << Type 10513 << getOpenMPDirectiveName(CurrDir); 10514 bool IsDecl = 10515 !VD || 10516 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10517 Diag(D->getLocation(), 10518 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10519 << D; 10520 continue; 10521 } 10522 10523 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10524 // A list item cannot appear in both a map clause and a data-sharing 10525 // attribute clause on the same construct 10526 if (isOpenMPTargetExecutionDirective(CurrDir)) { 10527 OpenMPClauseKind ConflictKind; 10528 if (DSAStack->checkMappableExprComponentListsForDecl( 10529 VD, /*CurrentRegionOnly=*/true, 10530 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 10531 OpenMPClauseKind WhereFoundClauseKind) -> bool { 10532 ConflictKind = WhereFoundClauseKind; 10533 return true; 10534 })) { 10535 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10536 << getOpenMPClauseName(OMPC_private) 10537 << getOpenMPClauseName(ConflictKind) 10538 << getOpenMPDirectiveName(CurrDir); 10539 reportOriginalDsa(*this, DSAStack, D, DVar); 10540 continue; 10541 } 10542 } 10543 10544 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 10545 // A variable of class type (or array thereof) that appears in a private 10546 // clause requires an accessible, unambiguous default constructor for the 10547 // class type. 10548 // Generate helper private variable and initialize it with the default 10549 // value. The address of the original variable is replaced by the address of 10550 // the new private variable in CodeGen. This new variable is not added to 10551 // IdResolver, so the code in the OpenMP region uses original variable for 10552 // proper diagnostics. 10553 Type = Type.getUnqualifiedType(); 10554 VarDecl *VDPrivate = 10555 buildVarDecl(*this, ELoc, Type, D->getName(), 10556 D->hasAttrs() ? &D->getAttrs() : nullptr, 10557 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10558 ActOnUninitializedDecl(VDPrivate); 10559 if (VDPrivate->isInvalidDecl()) 10560 continue; 10561 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10562 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 10563 10564 DeclRefExpr *Ref = nullptr; 10565 if (!VD && !CurContext->isDependentContext()) 10566 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10567 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 10568 Vars.push_back((VD || CurContext->isDependentContext()) 10569 ? RefExpr->IgnoreParens() 10570 : Ref); 10571 PrivateCopies.push_back(VDPrivateRefExpr); 10572 } 10573 10574 if (Vars.empty()) 10575 return nullptr; 10576 10577 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 10578 PrivateCopies); 10579 } 10580 10581 namespace { 10582 class DiagsUninitializedSeveretyRAII { 10583 private: 10584 DiagnosticsEngine &Diags; 10585 SourceLocation SavedLoc; 10586 bool IsIgnored = false; 10587 10588 public: 10589 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 10590 bool IsIgnored) 10591 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 10592 if (!IsIgnored) { 10593 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 10594 /*Map*/ diag::Severity::Ignored, Loc); 10595 } 10596 } 10597 ~DiagsUninitializedSeveretyRAII() { 10598 if (!IsIgnored) 10599 Diags.popMappings(SavedLoc); 10600 } 10601 }; 10602 } 10603 10604 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 10605 SourceLocation StartLoc, 10606 SourceLocation LParenLoc, 10607 SourceLocation EndLoc) { 10608 SmallVector<Expr *, 8> Vars; 10609 SmallVector<Expr *, 8> PrivateCopies; 10610 SmallVector<Expr *, 8> Inits; 10611 SmallVector<Decl *, 4> ExprCaptures; 10612 bool IsImplicitClause = 10613 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 10614 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 10615 10616 for (Expr *RefExpr : VarList) { 10617 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 10618 SourceLocation ELoc; 10619 SourceRange ERange; 10620 Expr *SimpleRefExpr = RefExpr; 10621 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10622 if (Res.second) { 10623 // It will be analyzed later. 10624 Vars.push_back(RefExpr); 10625 PrivateCopies.push_back(nullptr); 10626 Inits.push_back(nullptr); 10627 } 10628 ValueDecl *D = Res.first; 10629 if (!D) 10630 continue; 10631 10632 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 10633 QualType Type = D->getType(); 10634 auto *VD = dyn_cast<VarDecl>(D); 10635 10636 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10637 // A variable that appears in a private clause must not have an incomplete 10638 // type or a reference type. 10639 if (RequireCompleteType(ELoc, Type, 10640 diag::err_omp_firstprivate_incomplete_type)) 10641 continue; 10642 Type = Type.getNonReferenceType(); 10643 10644 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 10645 // A variable of class type (or array thereof) that appears in a private 10646 // clause requires an accessible, unambiguous copy constructor for the 10647 // class type. 10648 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 10649 10650 // If an implicit firstprivate variable found it was checked already. 10651 DSAStackTy::DSAVarData TopDVar; 10652 if (!IsImplicitClause) { 10653 DSAStackTy::DSAVarData DVar = 10654 DSAStack->getTopDSA(D, /*FromParent=*/false); 10655 TopDVar = DVar; 10656 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10657 bool IsConstant = ElemType.isConstant(Context); 10658 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 10659 // A list item that specifies a given variable may not appear in more 10660 // than one clause on the same directive, except that a variable may be 10661 // specified in both firstprivate and lastprivate clauses. 10662 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 10663 // A list item may appear in a firstprivate or lastprivate clause but not 10664 // both. 10665 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 10666 (isOpenMPDistributeDirective(CurrDir) || 10667 DVar.CKind != OMPC_lastprivate) && 10668 DVar.RefExpr) { 10669 Diag(ELoc, diag::err_omp_wrong_dsa) 10670 << getOpenMPClauseName(DVar.CKind) 10671 << getOpenMPClauseName(OMPC_firstprivate); 10672 reportOriginalDsa(*this, DSAStack, D, DVar); 10673 continue; 10674 } 10675 10676 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10677 // in a Construct] 10678 // Variables with the predetermined data-sharing attributes may not be 10679 // listed in data-sharing attributes clauses, except for the cases 10680 // listed below. For these exceptions only, listing a predetermined 10681 // variable in a data-sharing attribute clause is allowed and overrides 10682 // the variable's predetermined data-sharing attributes. 10683 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10684 // in a Construct, C/C++, p.2] 10685 // Variables with const-qualified type having no mutable member may be 10686 // listed in a firstprivate clause, even if they are static data members. 10687 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 10688 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 10689 Diag(ELoc, diag::err_omp_wrong_dsa) 10690 << getOpenMPClauseName(DVar.CKind) 10691 << getOpenMPClauseName(OMPC_firstprivate); 10692 reportOriginalDsa(*this, DSAStack, D, DVar); 10693 continue; 10694 } 10695 10696 // OpenMP [2.9.3.4, Restrictions, p.2] 10697 // A list item that is private within a parallel region must not appear 10698 // in a firstprivate clause on a worksharing construct if any of the 10699 // worksharing regions arising from the worksharing construct ever bind 10700 // to any of the parallel regions arising from the parallel construct. 10701 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 10702 // A list item that is private within a teams region must not appear in a 10703 // firstprivate clause on a distribute construct if any of the distribute 10704 // regions arising from the distribute construct ever bind to any of the 10705 // teams regions arising from the teams construct. 10706 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 10707 // A list item that appears in a reduction clause of a teams construct 10708 // must not appear in a firstprivate clause on a distribute construct if 10709 // any of the distribute regions arising from the distribute construct 10710 // ever bind to any of the teams regions arising from the teams construct. 10711 if ((isOpenMPWorksharingDirective(CurrDir) || 10712 isOpenMPDistributeDirective(CurrDir)) && 10713 !isOpenMPParallelDirective(CurrDir) && 10714 !isOpenMPTeamsDirective(CurrDir)) { 10715 DVar = DSAStack->getImplicitDSA(D, true); 10716 if (DVar.CKind != OMPC_shared && 10717 (isOpenMPParallelDirective(DVar.DKind) || 10718 isOpenMPTeamsDirective(DVar.DKind) || 10719 DVar.DKind == OMPD_unknown)) { 10720 Diag(ELoc, diag::err_omp_required_access) 10721 << getOpenMPClauseName(OMPC_firstprivate) 10722 << getOpenMPClauseName(OMPC_shared); 10723 reportOriginalDsa(*this, DSAStack, D, DVar); 10724 continue; 10725 } 10726 } 10727 // OpenMP [2.9.3.4, Restrictions, p.3] 10728 // A list item that appears in a reduction clause of a parallel construct 10729 // must not appear in a firstprivate clause on a worksharing or task 10730 // construct if any of the worksharing or task regions arising from the 10731 // worksharing or task construct ever bind to any of the parallel regions 10732 // arising from the parallel construct. 10733 // OpenMP [2.9.3.4, Restrictions, p.4] 10734 // A list item that appears in a reduction clause in worksharing 10735 // construct must not appear in a firstprivate clause in a task construct 10736 // encountered during execution of any of the worksharing regions arising 10737 // from the worksharing construct. 10738 if (isOpenMPTaskingDirective(CurrDir)) { 10739 DVar = DSAStack->hasInnermostDSA( 10740 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 10741 [](OpenMPDirectiveKind K) { 10742 return isOpenMPParallelDirective(K) || 10743 isOpenMPWorksharingDirective(K) || 10744 isOpenMPTeamsDirective(K); 10745 }, 10746 /*FromParent=*/true); 10747 if (DVar.CKind == OMPC_reduction && 10748 (isOpenMPParallelDirective(DVar.DKind) || 10749 isOpenMPWorksharingDirective(DVar.DKind) || 10750 isOpenMPTeamsDirective(DVar.DKind))) { 10751 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 10752 << getOpenMPDirectiveName(DVar.DKind); 10753 reportOriginalDsa(*this, DSAStack, D, DVar); 10754 continue; 10755 } 10756 } 10757 10758 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10759 // A list item cannot appear in both a map clause and a data-sharing 10760 // attribute clause on the same construct 10761 if (isOpenMPTargetExecutionDirective(CurrDir)) { 10762 OpenMPClauseKind ConflictKind; 10763 if (DSAStack->checkMappableExprComponentListsForDecl( 10764 VD, /*CurrentRegionOnly=*/true, 10765 [&ConflictKind]( 10766 OMPClauseMappableExprCommon::MappableExprComponentListRef, 10767 OpenMPClauseKind WhereFoundClauseKind) { 10768 ConflictKind = WhereFoundClauseKind; 10769 return true; 10770 })) { 10771 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10772 << getOpenMPClauseName(OMPC_firstprivate) 10773 << getOpenMPClauseName(ConflictKind) 10774 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10775 reportOriginalDsa(*this, DSAStack, D, DVar); 10776 continue; 10777 } 10778 } 10779 } 10780 10781 // Variably modified types are not supported for tasks. 10782 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10783 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 10784 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10785 << getOpenMPClauseName(OMPC_firstprivate) << Type 10786 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10787 bool IsDecl = 10788 !VD || 10789 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10790 Diag(D->getLocation(), 10791 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10792 << D; 10793 continue; 10794 } 10795 10796 Type = Type.getUnqualifiedType(); 10797 VarDecl *VDPrivate = 10798 buildVarDecl(*this, ELoc, Type, D->getName(), 10799 D->hasAttrs() ? &D->getAttrs() : nullptr, 10800 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10801 // Generate helper private variable and initialize it with the value of the 10802 // original variable. The address of the original variable is replaced by 10803 // the address of the new private variable in the CodeGen. This new variable 10804 // is not added to IdResolver, so the code in the OpenMP region uses 10805 // original variable for proper diagnostics and variable capturing. 10806 Expr *VDInitRefExpr = nullptr; 10807 // For arrays generate initializer for single element and replace it by the 10808 // original array element in CodeGen. 10809 if (Type->isArrayType()) { 10810 VarDecl *VDInit = 10811 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 10812 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 10813 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 10814 ElemType = ElemType.getUnqualifiedType(); 10815 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 10816 ".firstprivate.temp"); 10817 InitializedEntity Entity = 10818 InitializedEntity::InitializeVariable(VDInitTemp); 10819 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 10820 10821 InitializationSequence InitSeq(*this, Entity, Kind, Init); 10822 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 10823 if (Result.isInvalid()) 10824 VDPrivate->setInvalidDecl(); 10825 else 10826 VDPrivate->setInit(Result.getAs<Expr>()); 10827 // Remove temp variable declaration. 10828 Context.Deallocate(VDInitTemp); 10829 } else { 10830 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 10831 ".firstprivate.temp"); 10832 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 10833 RefExpr->getExprLoc()); 10834 AddInitializerToDecl(VDPrivate, 10835 DefaultLvalueConversion(VDInitRefExpr).get(), 10836 /*DirectInit=*/false); 10837 } 10838 if (VDPrivate->isInvalidDecl()) { 10839 if (IsImplicitClause) { 10840 Diag(RefExpr->getExprLoc(), 10841 diag::note_omp_task_predetermined_firstprivate_here); 10842 } 10843 continue; 10844 } 10845 CurContext->addDecl(VDPrivate); 10846 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10847 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 10848 RefExpr->getExprLoc()); 10849 DeclRefExpr *Ref = nullptr; 10850 if (!VD && !CurContext->isDependentContext()) { 10851 if (TopDVar.CKind == OMPC_lastprivate) { 10852 Ref = TopDVar.PrivateCopy; 10853 } else { 10854 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10855 if (!isOpenMPCapturedDecl(D)) 10856 ExprCaptures.push_back(Ref->getDecl()); 10857 } 10858 } 10859 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 10860 Vars.push_back((VD || CurContext->isDependentContext()) 10861 ? RefExpr->IgnoreParens() 10862 : Ref); 10863 PrivateCopies.push_back(VDPrivateRefExpr); 10864 Inits.push_back(VDInitRefExpr); 10865 } 10866 10867 if (Vars.empty()) 10868 return nullptr; 10869 10870 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10871 Vars, PrivateCopies, Inits, 10872 buildPreInits(Context, ExprCaptures)); 10873 } 10874 10875 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 10876 SourceLocation StartLoc, 10877 SourceLocation LParenLoc, 10878 SourceLocation EndLoc) { 10879 SmallVector<Expr *, 8> Vars; 10880 SmallVector<Expr *, 8> SrcExprs; 10881 SmallVector<Expr *, 8> DstExprs; 10882 SmallVector<Expr *, 8> AssignmentOps; 10883 SmallVector<Decl *, 4> ExprCaptures; 10884 SmallVector<Expr *, 4> ExprPostUpdates; 10885 for (Expr *RefExpr : VarList) { 10886 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 10887 SourceLocation ELoc; 10888 SourceRange ERange; 10889 Expr *SimpleRefExpr = RefExpr; 10890 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10891 if (Res.second) { 10892 // It will be analyzed later. 10893 Vars.push_back(RefExpr); 10894 SrcExprs.push_back(nullptr); 10895 DstExprs.push_back(nullptr); 10896 AssignmentOps.push_back(nullptr); 10897 } 10898 ValueDecl *D = Res.first; 10899 if (!D) 10900 continue; 10901 10902 QualType Type = D->getType(); 10903 auto *VD = dyn_cast<VarDecl>(D); 10904 10905 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 10906 // A variable that appears in a lastprivate clause must not have an 10907 // incomplete type or a reference type. 10908 if (RequireCompleteType(ELoc, Type, 10909 diag::err_omp_lastprivate_incomplete_type)) 10910 continue; 10911 Type = Type.getNonReferenceType(); 10912 10913 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 10914 // A variable that is privatized must not have a const-qualified type 10915 // unless it is of class type with a mutable member. This restriction does 10916 // not apply to the firstprivate clause. 10917 // 10918 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 10919 // A variable that appears in a lastprivate clause must not have a 10920 // const-qualified type unless it is of class type with a mutable member. 10921 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 10922 continue; 10923 10924 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10925 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 10926 // in a Construct] 10927 // Variables with the predetermined data-sharing attributes may not be 10928 // listed in data-sharing attributes clauses, except for the cases 10929 // listed below. 10930 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 10931 // A list item may appear in a firstprivate or lastprivate clause but not 10932 // both. 10933 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10934 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 10935 (isOpenMPDistributeDirective(CurrDir) || 10936 DVar.CKind != OMPC_firstprivate) && 10937 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 10938 Diag(ELoc, diag::err_omp_wrong_dsa) 10939 << getOpenMPClauseName(DVar.CKind) 10940 << getOpenMPClauseName(OMPC_lastprivate); 10941 reportOriginalDsa(*this, DSAStack, D, DVar); 10942 continue; 10943 } 10944 10945 // OpenMP [2.14.3.5, Restrictions, p.2] 10946 // A list item that is private within a parallel region, or that appears in 10947 // the reduction clause of a parallel construct, must not appear in a 10948 // lastprivate clause on a worksharing construct if any of the corresponding 10949 // worksharing regions ever binds to any of the corresponding parallel 10950 // regions. 10951 DSAStackTy::DSAVarData TopDVar = DVar; 10952 if (isOpenMPWorksharingDirective(CurrDir) && 10953 !isOpenMPParallelDirective(CurrDir) && 10954 !isOpenMPTeamsDirective(CurrDir)) { 10955 DVar = DSAStack->getImplicitDSA(D, true); 10956 if (DVar.CKind != OMPC_shared) { 10957 Diag(ELoc, diag::err_omp_required_access) 10958 << getOpenMPClauseName(OMPC_lastprivate) 10959 << getOpenMPClauseName(OMPC_shared); 10960 reportOriginalDsa(*this, DSAStack, D, DVar); 10961 continue; 10962 } 10963 } 10964 10965 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 10966 // A variable of class type (or array thereof) that appears in a 10967 // lastprivate clause requires an accessible, unambiguous default 10968 // constructor for the class type, unless the list item is also specified 10969 // in a firstprivate clause. 10970 // A variable of class type (or array thereof) that appears in a 10971 // lastprivate clause requires an accessible, unambiguous copy assignment 10972 // operator for the class type. 10973 Type = Context.getBaseElementType(Type).getNonReferenceType(); 10974 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 10975 Type.getUnqualifiedType(), ".lastprivate.src", 10976 D->hasAttrs() ? &D->getAttrs() : nullptr); 10977 DeclRefExpr *PseudoSrcExpr = 10978 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 10979 VarDecl *DstVD = 10980 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 10981 D->hasAttrs() ? &D->getAttrs() : nullptr); 10982 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 10983 // For arrays generate assignment operation for single element and replace 10984 // it by the original array element in CodeGen. 10985 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 10986 PseudoDstExpr, PseudoSrcExpr); 10987 if (AssignmentOp.isInvalid()) 10988 continue; 10989 AssignmentOp = 10990 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 10991 if (AssignmentOp.isInvalid()) 10992 continue; 10993 10994 DeclRefExpr *Ref = nullptr; 10995 if (!VD && !CurContext->isDependentContext()) { 10996 if (TopDVar.CKind == OMPC_firstprivate) { 10997 Ref = TopDVar.PrivateCopy; 10998 } else { 10999 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 11000 if (!isOpenMPCapturedDecl(D)) 11001 ExprCaptures.push_back(Ref->getDecl()); 11002 } 11003 if (TopDVar.CKind == OMPC_firstprivate || 11004 (!isOpenMPCapturedDecl(D) && 11005 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 11006 ExprResult RefRes = DefaultLvalueConversion(Ref); 11007 if (!RefRes.isUsable()) 11008 continue; 11009 ExprResult PostUpdateRes = 11010 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 11011 RefRes.get()); 11012 if (!PostUpdateRes.isUsable()) 11013 continue; 11014 ExprPostUpdates.push_back( 11015 IgnoredValueConversions(PostUpdateRes.get()).get()); 11016 } 11017 } 11018 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 11019 Vars.push_back((VD || CurContext->isDependentContext()) 11020 ? RefExpr->IgnoreParens() 11021 : Ref); 11022 SrcExprs.push_back(PseudoSrcExpr); 11023 DstExprs.push_back(PseudoDstExpr); 11024 AssignmentOps.push_back(AssignmentOp.get()); 11025 } 11026 11027 if (Vars.empty()) 11028 return nullptr; 11029 11030 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11031 Vars, SrcExprs, DstExprs, AssignmentOps, 11032 buildPreInits(Context, ExprCaptures), 11033 buildPostUpdate(*this, ExprPostUpdates)); 11034 } 11035 11036 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 11037 SourceLocation StartLoc, 11038 SourceLocation LParenLoc, 11039 SourceLocation EndLoc) { 11040 SmallVector<Expr *, 8> Vars; 11041 for (Expr *RefExpr : VarList) { 11042 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 11043 SourceLocation ELoc; 11044 SourceRange ERange; 11045 Expr *SimpleRefExpr = RefExpr; 11046 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11047 if (Res.second) { 11048 // It will be analyzed later. 11049 Vars.push_back(RefExpr); 11050 } 11051 ValueDecl *D = Res.first; 11052 if (!D) 11053 continue; 11054 11055 auto *VD = dyn_cast<VarDecl>(D); 11056 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 11057 // in a Construct] 11058 // Variables with the predetermined data-sharing attributes may not be 11059 // listed in data-sharing attributes clauses, except for the cases 11060 // listed below. For these exceptions only, listing a predetermined 11061 // variable in a data-sharing attribute clause is allowed and overrides 11062 // the variable's predetermined data-sharing attributes. 11063 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11064 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 11065 DVar.RefExpr) { 11066 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 11067 << getOpenMPClauseName(OMPC_shared); 11068 reportOriginalDsa(*this, DSAStack, D, DVar); 11069 continue; 11070 } 11071 11072 DeclRefExpr *Ref = nullptr; 11073 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 11074 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11075 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 11076 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 11077 ? RefExpr->IgnoreParens() 11078 : Ref); 11079 } 11080 11081 if (Vars.empty()) 11082 return nullptr; 11083 11084 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 11085 } 11086 11087 namespace { 11088 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 11089 DSAStackTy *Stack; 11090 11091 public: 11092 bool VisitDeclRefExpr(DeclRefExpr *E) { 11093 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 11094 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 11095 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 11096 return false; 11097 if (DVar.CKind != OMPC_unknown) 11098 return true; 11099 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 11100 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 11101 /*FromParent=*/true); 11102 return DVarPrivate.CKind != OMPC_unknown; 11103 } 11104 return false; 11105 } 11106 bool VisitStmt(Stmt *S) { 11107 for (Stmt *Child : S->children()) { 11108 if (Child && Visit(Child)) 11109 return true; 11110 } 11111 return false; 11112 } 11113 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 11114 }; 11115 } // namespace 11116 11117 namespace { 11118 // Transform MemberExpression for specified FieldDecl of current class to 11119 // DeclRefExpr to specified OMPCapturedExprDecl. 11120 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 11121 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 11122 ValueDecl *Field = nullptr; 11123 DeclRefExpr *CapturedExpr = nullptr; 11124 11125 public: 11126 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 11127 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 11128 11129 ExprResult TransformMemberExpr(MemberExpr *E) { 11130 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 11131 E->getMemberDecl() == Field) { 11132 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 11133 return CapturedExpr; 11134 } 11135 return BaseTransform::TransformMemberExpr(E); 11136 } 11137 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 11138 }; 11139 } // namespace 11140 11141 template <typename T, typename U> 11142 static T filterLookupForUDReductionAndMapper( 11143 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 11144 for (U &Set : Lookups) { 11145 for (auto *D : Set) { 11146 if (T Res = Gen(cast<ValueDecl>(D))) 11147 return Res; 11148 } 11149 } 11150 return T(); 11151 } 11152 11153 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 11154 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 11155 11156 for (auto RD : D->redecls()) { 11157 // Don't bother with extra checks if we already know this one isn't visible. 11158 if (RD == D) 11159 continue; 11160 11161 auto ND = cast<NamedDecl>(RD); 11162 if (LookupResult::isVisible(SemaRef, ND)) 11163 return ND; 11164 } 11165 11166 return nullptr; 11167 } 11168 11169 static void 11170 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 11171 SourceLocation Loc, QualType Ty, 11172 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 11173 // Find all of the associated namespaces and classes based on the 11174 // arguments we have. 11175 Sema::AssociatedNamespaceSet AssociatedNamespaces; 11176 Sema::AssociatedClassSet AssociatedClasses; 11177 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 11178 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 11179 AssociatedClasses); 11180 11181 // C++ [basic.lookup.argdep]p3: 11182 // Let X be the lookup set produced by unqualified lookup (3.4.1) 11183 // and let Y be the lookup set produced by argument dependent 11184 // lookup (defined as follows). If X contains [...] then Y is 11185 // empty. Otherwise Y is the set of declarations found in the 11186 // namespaces associated with the argument types as described 11187 // below. The set of declarations found by the lookup of the name 11188 // is the union of X and Y. 11189 // 11190 // Here, we compute Y and add its members to the overloaded 11191 // candidate set. 11192 for (auto *NS : AssociatedNamespaces) { 11193 // When considering an associated namespace, the lookup is the 11194 // same as the lookup performed when the associated namespace is 11195 // used as a qualifier (3.4.3.2) except that: 11196 // 11197 // -- Any using-directives in the associated namespace are 11198 // ignored. 11199 // 11200 // -- Any namespace-scope friend functions declared in 11201 // associated classes are visible within their respective 11202 // namespaces even if they are not visible during an ordinary 11203 // lookup (11.4). 11204 DeclContext::lookup_result R = NS->lookup(Id.getName()); 11205 for (auto *D : R) { 11206 auto *Underlying = D; 11207 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 11208 Underlying = USD->getTargetDecl(); 11209 11210 if (!isa<OMPDeclareReductionDecl>(Underlying) && 11211 !isa<OMPDeclareMapperDecl>(Underlying)) 11212 continue; 11213 11214 if (!SemaRef.isVisible(D)) { 11215 D = findAcceptableDecl(SemaRef, D); 11216 if (!D) 11217 continue; 11218 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 11219 Underlying = USD->getTargetDecl(); 11220 } 11221 Lookups.emplace_back(); 11222 Lookups.back().addDecl(Underlying); 11223 } 11224 } 11225 } 11226 11227 static ExprResult 11228 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 11229 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 11230 const DeclarationNameInfo &ReductionId, QualType Ty, 11231 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 11232 if (ReductionIdScopeSpec.isInvalid()) 11233 return ExprError(); 11234 SmallVector<UnresolvedSet<8>, 4> Lookups; 11235 if (S) { 11236 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 11237 Lookup.suppressDiagnostics(); 11238 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 11239 NamedDecl *D = Lookup.getRepresentativeDecl(); 11240 do { 11241 S = S->getParent(); 11242 } while (S && !S->isDeclScope(D)); 11243 if (S) 11244 S = S->getParent(); 11245 Lookups.emplace_back(); 11246 Lookups.back().append(Lookup.begin(), Lookup.end()); 11247 Lookup.clear(); 11248 } 11249 } else if (auto *ULE = 11250 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 11251 Lookups.push_back(UnresolvedSet<8>()); 11252 Decl *PrevD = nullptr; 11253 for (NamedDecl *D : ULE->decls()) { 11254 if (D == PrevD) 11255 Lookups.push_back(UnresolvedSet<8>()); 11256 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 11257 Lookups.back().addDecl(DRD); 11258 PrevD = D; 11259 } 11260 } 11261 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 11262 Ty->isInstantiationDependentType() || 11263 Ty->containsUnexpandedParameterPack() || 11264 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 11265 return !D->isInvalidDecl() && 11266 (D->getType()->isDependentType() || 11267 D->getType()->isInstantiationDependentType() || 11268 D->getType()->containsUnexpandedParameterPack()); 11269 })) { 11270 UnresolvedSet<8> ResSet; 11271 for (const UnresolvedSet<8> &Set : Lookups) { 11272 if (Set.empty()) 11273 continue; 11274 ResSet.append(Set.begin(), Set.end()); 11275 // The last item marks the end of all declarations at the specified scope. 11276 ResSet.addDecl(Set[Set.size() - 1]); 11277 } 11278 return UnresolvedLookupExpr::Create( 11279 SemaRef.Context, /*NamingClass=*/nullptr, 11280 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 11281 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 11282 } 11283 // Lookup inside the classes. 11284 // C++ [over.match.oper]p3: 11285 // For a unary operator @ with an operand of a type whose 11286 // cv-unqualified version is T1, and for a binary operator @ with 11287 // a left operand of a type whose cv-unqualified version is T1 and 11288 // a right operand of a type whose cv-unqualified version is T2, 11289 // three sets of candidate functions, designated member 11290 // candidates, non-member candidates and built-in candidates, are 11291 // constructed as follows: 11292 // -- If T1 is a complete class type or a class currently being 11293 // defined, the set of member candidates is the result of the 11294 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 11295 // the set of member candidates is empty. 11296 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 11297 Lookup.suppressDiagnostics(); 11298 if (const auto *TyRec = Ty->getAs<RecordType>()) { 11299 // Complete the type if it can be completed. 11300 // If the type is neither complete nor being defined, bail out now. 11301 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 11302 TyRec->getDecl()->getDefinition()) { 11303 Lookup.clear(); 11304 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 11305 if (Lookup.empty()) { 11306 Lookups.emplace_back(); 11307 Lookups.back().append(Lookup.begin(), Lookup.end()); 11308 } 11309 } 11310 } 11311 // Perform ADL. 11312 if (SemaRef.getLangOpts().CPlusPlus) 11313 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 11314 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 11315 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 11316 if (!D->isInvalidDecl() && 11317 SemaRef.Context.hasSameType(D->getType(), Ty)) 11318 return D; 11319 return nullptr; 11320 })) 11321 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 11322 VK_LValue, Loc); 11323 if (SemaRef.getLangOpts().CPlusPlus) { 11324 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 11325 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 11326 if (!D->isInvalidDecl() && 11327 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 11328 !Ty.isMoreQualifiedThan(D->getType())) 11329 return D; 11330 return nullptr; 11331 })) { 11332 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 11333 /*DetectVirtual=*/false); 11334 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 11335 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 11336 VD->getType().getUnqualifiedType()))) { 11337 if (SemaRef.CheckBaseClassAccess( 11338 Loc, VD->getType(), Ty, Paths.front(), 11339 /*DiagID=*/0) != Sema::AR_inaccessible) { 11340 SemaRef.BuildBasePathArray(Paths, BasePath); 11341 return SemaRef.BuildDeclRefExpr( 11342 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 11343 } 11344 } 11345 } 11346 } 11347 } 11348 if (ReductionIdScopeSpec.isSet()) { 11349 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 11350 return ExprError(); 11351 } 11352 return ExprEmpty(); 11353 } 11354 11355 namespace { 11356 /// Data for the reduction-based clauses. 11357 struct ReductionData { 11358 /// List of original reduction items. 11359 SmallVector<Expr *, 8> Vars; 11360 /// List of private copies of the reduction items. 11361 SmallVector<Expr *, 8> Privates; 11362 /// LHS expressions for the reduction_op expressions. 11363 SmallVector<Expr *, 8> LHSs; 11364 /// RHS expressions for the reduction_op expressions. 11365 SmallVector<Expr *, 8> RHSs; 11366 /// Reduction operation expression. 11367 SmallVector<Expr *, 8> ReductionOps; 11368 /// Taskgroup descriptors for the corresponding reduction items in 11369 /// in_reduction clauses. 11370 SmallVector<Expr *, 8> TaskgroupDescriptors; 11371 /// List of captures for clause. 11372 SmallVector<Decl *, 4> ExprCaptures; 11373 /// List of postupdate expressions. 11374 SmallVector<Expr *, 4> ExprPostUpdates; 11375 ReductionData() = delete; 11376 /// Reserves required memory for the reduction data. 11377 ReductionData(unsigned Size) { 11378 Vars.reserve(Size); 11379 Privates.reserve(Size); 11380 LHSs.reserve(Size); 11381 RHSs.reserve(Size); 11382 ReductionOps.reserve(Size); 11383 TaskgroupDescriptors.reserve(Size); 11384 ExprCaptures.reserve(Size); 11385 ExprPostUpdates.reserve(Size); 11386 } 11387 /// Stores reduction item and reduction operation only (required for dependent 11388 /// reduction item). 11389 void push(Expr *Item, Expr *ReductionOp) { 11390 Vars.emplace_back(Item); 11391 Privates.emplace_back(nullptr); 11392 LHSs.emplace_back(nullptr); 11393 RHSs.emplace_back(nullptr); 11394 ReductionOps.emplace_back(ReductionOp); 11395 TaskgroupDescriptors.emplace_back(nullptr); 11396 } 11397 /// Stores reduction data. 11398 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 11399 Expr *TaskgroupDescriptor) { 11400 Vars.emplace_back(Item); 11401 Privates.emplace_back(Private); 11402 LHSs.emplace_back(LHS); 11403 RHSs.emplace_back(RHS); 11404 ReductionOps.emplace_back(ReductionOp); 11405 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 11406 } 11407 }; 11408 } // namespace 11409 11410 static bool checkOMPArraySectionConstantForReduction( 11411 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 11412 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 11413 const Expr *Length = OASE->getLength(); 11414 if (Length == nullptr) { 11415 // For array sections of the form [1:] or [:], we would need to analyze 11416 // the lower bound... 11417 if (OASE->getColonLoc().isValid()) 11418 return false; 11419 11420 // This is an array subscript which has implicit length 1! 11421 SingleElement = true; 11422 ArraySizes.push_back(llvm::APSInt::get(1)); 11423 } else { 11424 Expr::EvalResult Result; 11425 if (!Length->EvaluateAsInt(Result, Context)) 11426 return false; 11427 11428 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 11429 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 11430 ArraySizes.push_back(ConstantLengthValue); 11431 } 11432 11433 // Get the base of this array section and walk up from there. 11434 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 11435 11436 // We require length = 1 for all array sections except the right-most to 11437 // guarantee that the memory region is contiguous and has no holes in it. 11438 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 11439 Length = TempOASE->getLength(); 11440 if (Length == nullptr) { 11441 // For array sections of the form [1:] or [:], we would need to analyze 11442 // the lower bound... 11443 if (OASE->getColonLoc().isValid()) 11444 return false; 11445 11446 // This is an array subscript which has implicit length 1! 11447 ArraySizes.push_back(llvm::APSInt::get(1)); 11448 } else { 11449 Expr::EvalResult Result; 11450 if (!Length->EvaluateAsInt(Result, Context)) 11451 return false; 11452 11453 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 11454 if (ConstantLengthValue.getSExtValue() != 1) 11455 return false; 11456 11457 ArraySizes.push_back(ConstantLengthValue); 11458 } 11459 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 11460 } 11461 11462 // If we have a single element, we don't need to add the implicit lengths. 11463 if (!SingleElement) { 11464 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 11465 // Has implicit length 1! 11466 ArraySizes.push_back(llvm::APSInt::get(1)); 11467 Base = TempASE->getBase()->IgnoreParenImpCasts(); 11468 } 11469 } 11470 11471 // This array section can be privatized as a single value or as a constant 11472 // sized array. 11473 return true; 11474 } 11475 11476 static bool actOnOMPReductionKindClause( 11477 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 11478 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11479 SourceLocation ColonLoc, SourceLocation EndLoc, 11480 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11481 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 11482 DeclarationName DN = ReductionId.getName(); 11483 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 11484 BinaryOperatorKind BOK = BO_Comma; 11485 11486 ASTContext &Context = S.Context; 11487 // OpenMP [2.14.3.6, reduction clause] 11488 // C 11489 // reduction-identifier is either an identifier or one of the following 11490 // operators: +, -, *, &, |, ^, && and || 11491 // C++ 11492 // reduction-identifier is either an id-expression or one of the following 11493 // operators: +, -, *, &, |, ^, && and || 11494 switch (OOK) { 11495 case OO_Plus: 11496 case OO_Minus: 11497 BOK = BO_Add; 11498 break; 11499 case OO_Star: 11500 BOK = BO_Mul; 11501 break; 11502 case OO_Amp: 11503 BOK = BO_And; 11504 break; 11505 case OO_Pipe: 11506 BOK = BO_Or; 11507 break; 11508 case OO_Caret: 11509 BOK = BO_Xor; 11510 break; 11511 case OO_AmpAmp: 11512 BOK = BO_LAnd; 11513 break; 11514 case OO_PipePipe: 11515 BOK = BO_LOr; 11516 break; 11517 case OO_New: 11518 case OO_Delete: 11519 case OO_Array_New: 11520 case OO_Array_Delete: 11521 case OO_Slash: 11522 case OO_Percent: 11523 case OO_Tilde: 11524 case OO_Exclaim: 11525 case OO_Equal: 11526 case OO_Less: 11527 case OO_Greater: 11528 case OO_LessEqual: 11529 case OO_GreaterEqual: 11530 case OO_PlusEqual: 11531 case OO_MinusEqual: 11532 case OO_StarEqual: 11533 case OO_SlashEqual: 11534 case OO_PercentEqual: 11535 case OO_CaretEqual: 11536 case OO_AmpEqual: 11537 case OO_PipeEqual: 11538 case OO_LessLess: 11539 case OO_GreaterGreater: 11540 case OO_LessLessEqual: 11541 case OO_GreaterGreaterEqual: 11542 case OO_EqualEqual: 11543 case OO_ExclaimEqual: 11544 case OO_Spaceship: 11545 case OO_PlusPlus: 11546 case OO_MinusMinus: 11547 case OO_Comma: 11548 case OO_ArrowStar: 11549 case OO_Arrow: 11550 case OO_Call: 11551 case OO_Subscript: 11552 case OO_Conditional: 11553 case OO_Coawait: 11554 case NUM_OVERLOADED_OPERATORS: 11555 llvm_unreachable("Unexpected reduction identifier"); 11556 case OO_None: 11557 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 11558 if (II->isStr("max")) 11559 BOK = BO_GT; 11560 else if (II->isStr("min")) 11561 BOK = BO_LT; 11562 } 11563 break; 11564 } 11565 SourceRange ReductionIdRange; 11566 if (ReductionIdScopeSpec.isValid()) 11567 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 11568 else 11569 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 11570 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 11571 11572 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 11573 bool FirstIter = true; 11574 for (Expr *RefExpr : VarList) { 11575 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 11576 // OpenMP [2.1, C/C++] 11577 // A list item is a variable or array section, subject to the restrictions 11578 // specified in Section 2.4 on page 42 and in each of the sections 11579 // describing clauses and directives for which a list appears. 11580 // OpenMP [2.14.3.3, Restrictions, p.1] 11581 // A variable that is part of another variable (as an array or 11582 // structure element) cannot appear in a private clause. 11583 if (!FirstIter && IR != ER) 11584 ++IR; 11585 FirstIter = false; 11586 SourceLocation ELoc; 11587 SourceRange ERange; 11588 Expr *SimpleRefExpr = RefExpr; 11589 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 11590 /*AllowArraySection=*/true); 11591 if (Res.second) { 11592 // Try to find 'declare reduction' corresponding construct before using 11593 // builtin/overloaded operators. 11594 QualType Type = Context.DependentTy; 11595 CXXCastPath BasePath; 11596 ExprResult DeclareReductionRef = buildDeclareReductionRef( 11597 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 11598 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 11599 Expr *ReductionOp = nullptr; 11600 if (S.CurContext->isDependentContext() && 11601 (DeclareReductionRef.isUnset() || 11602 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 11603 ReductionOp = DeclareReductionRef.get(); 11604 // It will be analyzed later. 11605 RD.push(RefExpr, ReductionOp); 11606 } 11607 ValueDecl *D = Res.first; 11608 if (!D) 11609 continue; 11610 11611 Expr *TaskgroupDescriptor = nullptr; 11612 QualType Type; 11613 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 11614 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 11615 if (ASE) { 11616 Type = ASE->getType().getNonReferenceType(); 11617 } else if (OASE) { 11618 QualType BaseType = 11619 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 11620 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 11621 Type = ATy->getElementType(); 11622 else 11623 Type = BaseType->getPointeeType(); 11624 Type = Type.getNonReferenceType(); 11625 } else { 11626 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 11627 } 11628 auto *VD = dyn_cast<VarDecl>(D); 11629 11630 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 11631 // A variable that appears in a private clause must not have an incomplete 11632 // type or a reference type. 11633 if (S.RequireCompleteType(ELoc, D->getType(), 11634 diag::err_omp_reduction_incomplete_type)) 11635 continue; 11636 // OpenMP [2.14.3.6, reduction clause, Restrictions] 11637 // A list item that appears in a reduction clause must not be 11638 // const-qualified. 11639 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 11640 /*AcceptIfMutable*/ false, ASE || OASE)) 11641 continue; 11642 11643 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 11644 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 11645 // If a list-item is a reference type then it must bind to the same object 11646 // for all threads of the team. 11647 if (!ASE && !OASE) { 11648 if (VD) { 11649 VarDecl *VDDef = VD->getDefinition(); 11650 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 11651 DSARefChecker Check(Stack); 11652 if (Check.Visit(VDDef->getInit())) { 11653 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 11654 << getOpenMPClauseName(ClauseKind) << ERange; 11655 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 11656 continue; 11657 } 11658 } 11659 } 11660 11661 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 11662 // in a Construct] 11663 // Variables with the predetermined data-sharing attributes may not be 11664 // listed in data-sharing attributes clauses, except for the cases 11665 // listed below. For these exceptions only, listing a predetermined 11666 // variable in a data-sharing attribute clause is allowed and overrides 11667 // the variable's predetermined data-sharing attributes. 11668 // OpenMP [2.14.3.6, Restrictions, p.3] 11669 // Any number of reduction clauses can be specified on the directive, 11670 // but a list item can appear only once in the reduction clauses for that 11671 // directive. 11672 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 11673 if (DVar.CKind == OMPC_reduction) { 11674 S.Diag(ELoc, diag::err_omp_once_referenced) 11675 << getOpenMPClauseName(ClauseKind); 11676 if (DVar.RefExpr) 11677 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 11678 continue; 11679 } 11680 if (DVar.CKind != OMPC_unknown) { 11681 S.Diag(ELoc, diag::err_omp_wrong_dsa) 11682 << getOpenMPClauseName(DVar.CKind) 11683 << getOpenMPClauseName(OMPC_reduction); 11684 reportOriginalDsa(S, Stack, D, DVar); 11685 continue; 11686 } 11687 11688 // OpenMP [2.14.3.6, Restrictions, p.1] 11689 // A list item that appears in a reduction clause of a worksharing 11690 // construct must be shared in the parallel regions to which any of the 11691 // worksharing regions arising from the worksharing construct bind. 11692 if (isOpenMPWorksharingDirective(CurrDir) && 11693 !isOpenMPParallelDirective(CurrDir) && 11694 !isOpenMPTeamsDirective(CurrDir)) { 11695 DVar = Stack->getImplicitDSA(D, true); 11696 if (DVar.CKind != OMPC_shared) { 11697 S.Diag(ELoc, diag::err_omp_required_access) 11698 << getOpenMPClauseName(OMPC_reduction) 11699 << getOpenMPClauseName(OMPC_shared); 11700 reportOriginalDsa(S, Stack, D, DVar); 11701 continue; 11702 } 11703 } 11704 } 11705 11706 // Try to find 'declare reduction' corresponding construct before using 11707 // builtin/overloaded operators. 11708 CXXCastPath BasePath; 11709 ExprResult DeclareReductionRef = buildDeclareReductionRef( 11710 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 11711 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 11712 if (DeclareReductionRef.isInvalid()) 11713 continue; 11714 if (S.CurContext->isDependentContext() && 11715 (DeclareReductionRef.isUnset() || 11716 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 11717 RD.push(RefExpr, DeclareReductionRef.get()); 11718 continue; 11719 } 11720 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 11721 // Not allowed reduction identifier is found. 11722 S.Diag(ReductionId.getBeginLoc(), 11723 diag::err_omp_unknown_reduction_identifier) 11724 << Type << ReductionIdRange; 11725 continue; 11726 } 11727 11728 // OpenMP [2.14.3.6, reduction clause, Restrictions] 11729 // The type of a list item that appears in a reduction clause must be valid 11730 // for the reduction-identifier. For a max or min reduction in C, the type 11731 // of the list item must be an allowed arithmetic data type: char, int, 11732 // float, double, or _Bool, possibly modified with long, short, signed, or 11733 // unsigned. For a max or min reduction in C++, the type of the list item 11734 // must be an allowed arithmetic data type: char, wchar_t, int, float, 11735 // double, or bool, possibly modified with long, short, signed, or unsigned. 11736 if (DeclareReductionRef.isUnset()) { 11737 if ((BOK == BO_GT || BOK == BO_LT) && 11738 !(Type->isScalarType() || 11739 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 11740 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 11741 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 11742 if (!ASE && !OASE) { 11743 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11744 VarDecl::DeclarationOnly; 11745 S.Diag(D->getLocation(), 11746 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11747 << D; 11748 } 11749 continue; 11750 } 11751 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 11752 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 11753 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 11754 << getOpenMPClauseName(ClauseKind); 11755 if (!ASE && !OASE) { 11756 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11757 VarDecl::DeclarationOnly; 11758 S.Diag(D->getLocation(), 11759 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11760 << D; 11761 } 11762 continue; 11763 } 11764 } 11765 11766 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 11767 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 11768 D->hasAttrs() ? &D->getAttrs() : nullptr); 11769 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 11770 D->hasAttrs() ? &D->getAttrs() : nullptr); 11771 QualType PrivateTy = Type; 11772 11773 // Try if we can determine constant lengths for all array sections and avoid 11774 // the VLA. 11775 bool ConstantLengthOASE = false; 11776 if (OASE) { 11777 bool SingleElement; 11778 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 11779 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 11780 Context, OASE, SingleElement, ArraySizes); 11781 11782 // If we don't have a single element, we must emit a constant array type. 11783 if (ConstantLengthOASE && !SingleElement) { 11784 for (llvm::APSInt &Size : ArraySizes) 11785 PrivateTy = Context.getConstantArrayType( 11786 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 11787 } 11788 } 11789 11790 if ((OASE && !ConstantLengthOASE) || 11791 (!OASE && !ASE && 11792 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 11793 if (!Context.getTargetInfo().isVLASupported() && 11794 S.shouldDiagnoseTargetSupportFromOpenMP()) { 11795 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 11796 S.Diag(ELoc, diag::note_vla_unsupported); 11797 continue; 11798 } 11799 // For arrays/array sections only: 11800 // Create pseudo array type for private copy. The size for this array will 11801 // be generated during codegen. 11802 // For array subscripts or single variables Private Ty is the same as Type 11803 // (type of the variable or single array element). 11804 PrivateTy = Context.getVariableArrayType( 11805 Type, 11806 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 11807 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 11808 } else if (!ASE && !OASE && 11809 Context.getAsArrayType(D->getType().getNonReferenceType())) { 11810 PrivateTy = D->getType().getNonReferenceType(); 11811 } 11812 // Private copy. 11813 VarDecl *PrivateVD = 11814 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 11815 D->hasAttrs() ? &D->getAttrs() : nullptr, 11816 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11817 // Add initializer for private variable. 11818 Expr *Init = nullptr; 11819 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 11820 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 11821 if (DeclareReductionRef.isUsable()) { 11822 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 11823 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 11824 if (DRD->getInitializer()) { 11825 Init = DRDRef; 11826 RHSVD->setInit(DRDRef); 11827 RHSVD->setInitStyle(VarDecl::CallInit); 11828 } 11829 } else { 11830 switch (BOK) { 11831 case BO_Add: 11832 case BO_Xor: 11833 case BO_Or: 11834 case BO_LOr: 11835 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 11836 if (Type->isScalarType() || Type->isAnyComplexType()) 11837 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 11838 break; 11839 case BO_Mul: 11840 case BO_LAnd: 11841 if (Type->isScalarType() || Type->isAnyComplexType()) { 11842 // '*' and '&&' reduction ops - initializer is '1'. 11843 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 11844 } 11845 break; 11846 case BO_And: { 11847 // '&' reduction op - initializer is '~0'. 11848 QualType OrigType = Type; 11849 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 11850 Type = ComplexTy->getElementType(); 11851 if (Type->isRealFloatingType()) { 11852 llvm::APFloat InitValue = 11853 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 11854 /*isIEEE=*/true); 11855 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11856 Type, ELoc); 11857 } else if (Type->isScalarType()) { 11858 uint64_t Size = Context.getTypeSize(Type); 11859 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 11860 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 11861 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11862 } 11863 if (Init && OrigType->isAnyComplexType()) { 11864 // Init = 0xFFFF + 0xFFFFi; 11865 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 11866 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 11867 } 11868 Type = OrigType; 11869 break; 11870 } 11871 case BO_LT: 11872 case BO_GT: { 11873 // 'min' reduction op - initializer is 'Largest representable number in 11874 // the reduction list item type'. 11875 // 'max' reduction op - initializer is 'Least representable number in 11876 // the reduction list item type'. 11877 if (Type->isIntegerType() || Type->isPointerType()) { 11878 bool IsSigned = Type->hasSignedIntegerRepresentation(); 11879 uint64_t Size = Context.getTypeSize(Type); 11880 QualType IntTy = 11881 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 11882 llvm::APInt InitValue = 11883 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 11884 : llvm::APInt::getMinValue(Size) 11885 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 11886 : llvm::APInt::getMaxValue(Size); 11887 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11888 if (Type->isPointerType()) { 11889 // Cast to pointer type. 11890 ExprResult CastExpr = S.BuildCStyleCastExpr( 11891 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 11892 if (CastExpr.isInvalid()) 11893 continue; 11894 Init = CastExpr.get(); 11895 } 11896 } else if (Type->isRealFloatingType()) { 11897 llvm::APFloat InitValue = llvm::APFloat::getLargest( 11898 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 11899 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11900 Type, ELoc); 11901 } 11902 break; 11903 } 11904 case BO_PtrMemD: 11905 case BO_PtrMemI: 11906 case BO_MulAssign: 11907 case BO_Div: 11908 case BO_Rem: 11909 case BO_Sub: 11910 case BO_Shl: 11911 case BO_Shr: 11912 case BO_LE: 11913 case BO_GE: 11914 case BO_EQ: 11915 case BO_NE: 11916 case BO_Cmp: 11917 case BO_AndAssign: 11918 case BO_XorAssign: 11919 case BO_OrAssign: 11920 case BO_Assign: 11921 case BO_AddAssign: 11922 case BO_SubAssign: 11923 case BO_DivAssign: 11924 case BO_RemAssign: 11925 case BO_ShlAssign: 11926 case BO_ShrAssign: 11927 case BO_Comma: 11928 llvm_unreachable("Unexpected reduction operation"); 11929 } 11930 } 11931 if (Init && DeclareReductionRef.isUnset()) 11932 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 11933 else if (!Init) 11934 S.ActOnUninitializedDecl(RHSVD); 11935 if (RHSVD->isInvalidDecl()) 11936 continue; 11937 if (!RHSVD->hasInit() && 11938 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 11939 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 11940 << Type << ReductionIdRange; 11941 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11942 VarDecl::DeclarationOnly; 11943 S.Diag(D->getLocation(), 11944 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11945 << D; 11946 continue; 11947 } 11948 // Store initializer for single element in private copy. Will be used during 11949 // codegen. 11950 PrivateVD->setInit(RHSVD->getInit()); 11951 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 11952 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 11953 ExprResult ReductionOp; 11954 if (DeclareReductionRef.isUsable()) { 11955 QualType RedTy = DeclareReductionRef.get()->getType(); 11956 QualType PtrRedTy = Context.getPointerType(RedTy); 11957 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 11958 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 11959 if (!BasePath.empty()) { 11960 LHS = S.DefaultLvalueConversion(LHS.get()); 11961 RHS = S.DefaultLvalueConversion(RHS.get()); 11962 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 11963 CK_UncheckedDerivedToBase, LHS.get(), 11964 &BasePath, LHS.get()->getValueKind()); 11965 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 11966 CK_UncheckedDerivedToBase, RHS.get(), 11967 &BasePath, RHS.get()->getValueKind()); 11968 } 11969 FunctionProtoType::ExtProtoInfo EPI; 11970 QualType Params[] = {PtrRedTy, PtrRedTy}; 11971 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 11972 auto *OVE = new (Context) OpaqueValueExpr( 11973 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 11974 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 11975 Expr *Args[] = {LHS.get(), RHS.get()}; 11976 ReductionOp = 11977 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 11978 } else { 11979 ReductionOp = S.BuildBinOp( 11980 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 11981 if (ReductionOp.isUsable()) { 11982 if (BOK != BO_LT && BOK != BO_GT) { 11983 ReductionOp = 11984 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11985 BO_Assign, LHSDRE, ReductionOp.get()); 11986 } else { 11987 auto *ConditionalOp = new (Context) 11988 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 11989 Type, VK_LValue, OK_Ordinary); 11990 ReductionOp = 11991 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11992 BO_Assign, LHSDRE, ConditionalOp); 11993 } 11994 if (ReductionOp.isUsable()) 11995 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 11996 /*DiscardedValue*/ false); 11997 } 11998 if (!ReductionOp.isUsable()) 11999 continue; 12000 } 12001 12002 // OpenMP [2.15.4.6, Restrictions, p.2] 12003 // A list item that appears in an in_reduction clause of a task construct 12004 // must appear in a task_reduction clause of a construct associated with a 12005 // taskgroup region that includes the participating task in its taskgroup 12006 // set. The construct associated with the innermost region that meets this 12007 // condition must specify the same reduction-identifier as the in_reduction 12008 // clause. 12009 if (ClauseKind == OMPC_in_reduction) { 12010 SourceRange ParentSR; 12011 BinaryOperatorKind ParentBOK; 12012 const Expr *ParentReductionOp; 12013 Expr *ParentBOKTD, *ParentReductionOpTD; 12014 DSAStackTy::DSAVarData ParentBOKDSA = 12015 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 12016 ParentBOKTD); 12017 DSAStackTy::DSAVarData ParentReductionOpDSA = 12018 Stack->getTopMostTaskgroupReductionData( 12019 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 12020 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 12021 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 12022 if (!IsParentBOK && !IsParentReductionOp) { 12023 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 12024 continue; 12025 } 12026 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 12027 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 12028 IsParentReductionOp) { 12029 bool EmitError = true; 12030 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 12031 llvm::FoldingSetNodeID RedId, ParentRedId; 12032 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 12033 DeclareReductionRef.get()->Profile(RedId, Context, 12034 /*Canonical=*/true); 12035 EmitError = RedId != ParentRedId; 12036 } 12037 if (EmitError) { 12038 S.Diag(ReductionId.getBeginLoc(), 12039 diag::err_omp_reduction_identifier_mismatch) 12040 << ReductionIdRange << RefExpr->getSourceRange(); 12041 S.Diag(ParentSR.getBegin(), 12042 diag::note_omp_previous_reduction_identifier) 12043 << ParentSR 12044 << (IsParentBOK ? ParentBOKDSA.RefExpr 12045 : ParentReductionOpDSA.RefExpr) 12046 ->getSourceRange(); 12047 continue; 12048 } 12049 } 12050 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 12051 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 12052 } 12053 12054 DeclRefExpr *Ref = nullptr; 12055 Expr *VarsExpr = RefExpr->IgnoreParens(); 12056 if (!VD && !S.CurContext->isDependentContext()) { 12057 if (ASE || OASE) { 12058 TransformExprToCaptures RebuildToCapture(S, D); 12059 VarsExpr = 12060 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 12061 Ref = RebuildToCapture.getCapturedExpr(); 12062 } else { 12063 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 12064 } 12065 if (!S.isOpenMPCapturedDecl(D)) { 12066 RD.ExprCaptures.emplace_back(Ref->getDecl()); 12067 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 12068 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 12069 if (!RefRes.isUsable()) 12070 continue; 12071 ExprResult PostUpdateRes = 12072 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 12073 RefRes.get()); 12074 if (!PostUpdateRes.isUsable()) 12075 continue; 12076 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 12077 Stack->getCurrentDirective() == OMPD_taskgroup) { 12078 S.Diag(RefExpr->getExprLoc(), 12079 diag::err_omp_reduction_non_addressable_expression) 12080 << RefExpr->getSourceRange(); 12081 continue; 12082 } 12083 RD.ExprPostUpdates.emplace_back( 12084 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 12085 } 12086 } 12087 } 12088 // All reduction items are still marked as reduction (to do not increase 12089 // code base size). 12090 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 12091 if (CurrDir == OMPD_taskgroup) { 12092 if (DeclareReductionRef.isUsable()) 12093 Stack->addTaskgroupReductionData(D, ReductionIdRange, 12094 DeclareReductionRef.get()); 12095 else 12096 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 12097 } 12098 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 12099 TaskgroupDescriptor); 12100 } 12101 return RD.Vars.empty(); 12102 } 12103 12104 OMPClause *Sema::ActOnOpenMPReductionClause( 12105 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12106 SourceLocation ColonLoc, SourceLocation EndLoc, 12107 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12108 ArrayRef<Expr *> UnresolvedReductions) { 12109 ReductionData RD(VarList.size()); 12110 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 12111 StartLoc, LParenLoc, ColonLoc, EndLoc, 12112 ReductionIdScopeSpec, ReductionId, 12113 UnresolvedReductions, RD)) 12114 return nullptr; 12115 12116 return OMPReductionClause::Create( 12117 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12118 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12119 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 12120 buildPreInits(Context, RD.ExprCaptures), 12121 buildPostUpdate(*this, RD.ExprPostUpdates)); 12122 } 12123 12124 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 12125 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12126 SourceLocation ColonLoc, SourceLocation EndLoc, 12127 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12128 ArrayRef<Expr *> UnresolvedReductions) { 12129 ReductionData RD(VarList.size()); 12130 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 12131 StartLoc, LParenLoc, ColonLoc, EndLoc, 12132 ReductionIdScopeSpec, ReductionId, 12133 UnresolvedReductions, RD)) 12134 return nullptr; 12135 12136 return OMPTaskReductionClause::Create( 12137 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12138 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12139 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 12140 buildPreInits(Context, RD.ExprCaptures), 12141 buildPostUpdate(*this, RD.ExprPostUpdates)); 12142 } 12143 12144 OMPClause *Sema::ActOnOpenMPInReductionClause( 12145 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12146 SourceLocation ColonLoc, SourceLocation EndLoc, 12147 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12148 ArrayRef<Expr *> UnresolvedReductions) { 12149 ReductionData RD(VarList.size()); 12150 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 12151 StartLoc, LParenLoc, ColonLoc, EndLoc, 12152 ReductionIdScopeSpec, ReductionId, 12153 UnresolvedReductions, RD)) 12154 return nullptr; 12155 12156 return OMPInReductionClause::Create( 12157 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12158 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12159 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 12160 buildPreInits(Context, RD.ExprCaptures), 12161 buildPostUpdate(*this, RD.ExprPostUpdates)); 12162 } 12163 12164 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 12165 SourceLocation LinLoc) { 12166 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 12167 LinKind == OMPC_LINEAR_unknown) { 12168 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 12169 return true; 12170 } 12171 return false; 12172 } 12173 12174 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 12175 OpenMPLinearClauseKind LinKind, 12176 QualType Type) { 12177 const auto *VD = dyn_cast_or_null<VarDecl>(D); 12178 // A variable must not have an incomplete type or a reference type. 12179 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 12180 return true; 12181 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 12182 !Type->isReferenceType()) { 12183 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 12184 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 12185 return true; 12186 } 12187 Type = Type.getNonReferenceType(); 12188 12189 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 12190 // A variable that is privatized must not have a const-qualified type 12191 // unless it is of class type with a mutable member. This restriction does 12192 // not apply to the firstprivate clause. 12193 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 12194 return true; 12195 12196 // A list item must be of integral or pointer type. 12197 Type = Type.getUnqualifiedType().getCanonicalType(); 12198 const auto *Ty = Type.getTypePtrOrNull(); 12199 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 12200 !Ty->isPointerType())) { 12201 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 12202 if (D) { 12203 bool IsDecl = 12204 !VD || 12205 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12206 Diag(D->getLocation(), 12207 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12208 << D; 12209 } 12210 return true; 12211 } 12212 return false; 12213 } 12214 12215 OMPClause *Sema::ActOnOpenMPLinearClause( 12216 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 12217 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 12218 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 12219 SmallVector<Expr *, 8> Vars; 12220 SmallVector<Expr *, 8> Privates; 12221 SmallVector<Expr *, 8> Inits; 12222 SmallVector<Decl *, 4> ExprCaptures; 12223 SmallVector<Expr *, 4> ExprPostUpdates; 12224 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 12225 LinKind = OMPC_LINEAR_val; 12226 for (Expr *RefExpr : VarList) { 12227 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12228 SourceLocation ELoc; 12229 SourceRange ERange; 12230 Expr *SimpleRefExpr = RefExpr; 12231 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12232 if (Res.second) { 12233 // It will be analyzed later. 12234 Vars.push_back(RefExpr); 12235 Privates.push_back(nullptr); 12236 Inits.push_back(nullptr); 12237 } 12238 ValueDecl *D = Res.first; 12239 if (!D) 12240 continue; 12241 12242 QualType Type = D->getType(); 12243 auto *VD = dyn_cast<VarDecl>(D); 12244 12245 // OpenMP [2.14.3.7, linear clause] 12246 // A list-item cannot appear in more than one linear clause. 12247 // A list-item that appears in a linear clause cannot appear in any 12248 // other data-sharing attribute clause. 12249 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 12250 if (DVar.RefExpr) { 12251 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 12252 << getOpenMPClauseName(OMPC_linear); 12253 reportOriginalDsa(*this, DSAStack, D, DVar); 12254 continue; 12255 } 12256 12257 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 12258 continue; 12259 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 12260 12261 // Build private copy of original var. 12262 VarDecl *Private = 12263 buildVarDecl(*this, ELoc, Type, D->getName(), 12264 D->hasAttrs() ? &D->getAttrs() : nullptr, 12265 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 12266 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 12267 // Build var to save initial value. 12268 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 12269 Expr *InitExpr; 12270 DeclRefExpr *Ref = nullptr; 12271 if (!VD && !CurContext->isDependentContext()) { 12272 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 12273 if (!isOpenMPCapturedDecl(D)) { 12274 ExprCaptures.push_back(Ref->getDecl()); 12275 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 12276 ExprResult RefRes = DefaultLvalueConversion(Ref); 12277 if (!RefRes.isUsable()) 12278 continue; 12279 ExprResult PostUpdateRes = 12280 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 12281 SimpleRefExpr, RefRes.get()); 12282 if (!PostUpdateRes.isUsable()) 12283 continue; 12284 ExprPostUpdates.push_back( 12285 IgnoredValueConversions(PostUpdateRes.get()).get()); 12286 } 12287 } 12288 } 12289 if (LinKind == OMPC_LINEAR_uval) 12290 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 12291 else 12292 InitExpr = VD ? SimpleRefExpr : Ref; 12293 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 12294 /*DirectInit=*/false); 12295 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 12296 12297 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 12298 Vars.push_back((VD || CurContext->isDependentContext()) 12299 ? RefExpr->IgnoreParens() 12300 : Ref); 12301 Privates.push_back(PrivateRef); 12302 Inits.push_back(InitRef); 12303 } 12304 12305 if (Vars.empty()) 12306 return nullptr; 12307 12308 Expr *StepExpr = Step; 12309 Expr *CalcStepExpr = nullptr; 12310 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 12311 !Step->isInstantiationDependent() && 12312 !Step->containsUnexpandedParameterPack()) { 12313 SourceLocation StepLoc = Step->getBeginLoc(); 12314 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 12315 if (Val.isInvalid()) 12316 return nullptr; 12317 StepExpr = Val.get(); 12318 12319 // Build var to save the step value. 12320 VarDecl *SaveVar = 12321 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 12322 ExprResult SaveRef = 12323 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 12324 ExprResult CalcStep = 12325 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 12326 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 12327 12328 // Warn about zero linear step (it would be probably better specified as 12329 // making corresponding variables 'const'). 12330 llvm::APSInt Result; 12331 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 12332 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 12333 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 12334 << (Vars.size() > 1); 12335 if (!IsConstant && CalcStep.isUsable()) { 12336 // Calculate the step beforehand instead of doing this on each iteration. 12337 // (This is not used if the number of iterations may be kfold-ed). 12338 CalcStepExpr = CalcStep.get(); 12339 } 12340 } 12341 12342 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 12343 ColonLoc, EndLoc, Vars, Privates, Inits, 12344 StepExpr, CalcStepExpr, 12345 buildPreInits(Context, ExprCaptures), 12346 buildPostUpdate(*this, ExprPostUpdates)); 12347 } 12348 12349 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 12350 Expr *NumIterations, Sema &SemaRef, 12351 Scope *S, DSAStackTy *Stack) { 12352 // Walk the vars and build update/final expressions for the CodeGen. 12353 SmallVector<Expr *, 8> Updates; 12354 SmallVector<Expr *, 8> Finals; 12355 Expr *Step = Clause.getStep(); 12356 Expr *CalcStep = Clause.getCalcStep(); 12357 // OpenMP [2.14.3.7, linear clause] 12358 // If linear-step is not specified it is assumed to be 1. 12359 if (!Step) 12360 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 12361 else if (CalcStep) 12362 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 12363 bool HasErrors = false; 12364 auto CurInit = Clause.inits().begin(); 12365 auto CurPrivate = Clause.privates().begin(); 12366 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 12367 for (Expr *RefExpr : Clause.varlists()) { 12368 SourceLocation ELoc; 12369 SourceRange ERange; 12370 Expr *SimpleRefExpr = RefExpr; 12371 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 12372 ValueDecl *D = Res.first; 12373 if (Res.second || !D) { 12374 Updates.push_back(nullptr); 12375 Finals.push_back(nullptr); 12376 HasErrors = true; 12377 continue; 12378 } 12379 auto &&Info = Stack->isLoopControlVariable(D); 12380 // OpenMP [2.15.11, distribute simd Construct] 12381 // A list item may not appear in a linear clause, unless it is the loop 12382 // iteration variable. 12383 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 12384 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 12385 SemaRef.Diag(ELoc, 12386 diag::err_omp_linear_distribute_var_non_loop_iteration); 12387 Updates.push_back(nullptr); 12388 Finals.push_back(nullptr); 12389 HasErrors = true; 12390 continue; 12391 } 12392 Expr *InitExpr = *CurInit; 12393 12394 // Build privatized reference to the current linear var. 12395 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 12396 Expr *CapturedRef; 12397 if (LinKind == OMPC_LINEAR_uval) 12398 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 12399 else 12400 CapturedRef = 12401 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 12402 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 12403 /*RefersToCapture=*/true); 12404 12405 // Build update: Var = InitExpr + IV * Step 12406 ExprResult Update; 12407 if (!Info.first) 12408 Update = 12409 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 12410 InitExpr, IV, Step, /* Subtract */ false); 12411 else 12412 Update = *CurPrivate; 12413 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 12414 /*DiscardedValue*/ false); 12415 12416 // Build final: Var = InitExpr + NumIterations * Step 12417 ExprResult Final; 12418 if (!Info.first) 12419 Final = 12420 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 12421 InitExpr, NumIterations, Step, /*Subtract=*/false); 12422 else 12423 Final = *CurPrivate; 12424 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 12425 /*DiscardedValue*/ false); 12426 12427 if (!Update.isUsable() || !Final.isUsable()) { 12428 Updates.push_back(nullptr); 12429 Finals.push_back(nullptr); 12430 HasErrors = true; 12431 } else { 12432 Updates.push_back(Update.get()); 12433 Finals.push_back(Final.get()); 12434 } 12435 ++CurInit; 12436 ++CurPrivate; 12437 } 12438 Clause.setUpdates(Updates); 12439 Clause.setFinals(Finals); 12440 return HasErrors; 12441 } 12442 12443 OMPClause *Sema::ActOnOpenMPAlignedClause( 12444 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 12445 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 12446 SmallVector<Expr *, 8> Vars; 12447 for (Expr *RefExpr : VarList) { 12448 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12449 SourceLocation ELoc; 12450 SourceRange ERange; 12451 Expr *SimpleRefExpr = RefExpr; 12452 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12453 if (Res.second) { 12454 // It will be analyzed later. 12455 Vars.push_back(RefExpr); 12456 } 12457 ValueDecl *D = Res.first; 12458 if (!D) 12459 continue; 12460 12461 QualType QType = D->getType(); 12462 auto *VD = dyn_cast<VarDecl>(D); 12463 12464 // OpenMP [2.8.1, simd construct, Restrictions] 12465 // The type of list items appearing in the aligned clause must be 12466 // array, pointer, reference to array, or reference to pointer. 12467 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 12468 const Type *Ty = QType.getTypePtrOrNull(); 12469 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 12470 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 12471 << QType << getLangOpts().CPlusPlus << ERange; 12472 bool IsDecl = 12473 !VD || 12474 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12475 Diag(D->getLocation(), 12476 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12477 << D; 12478 continue; 12479 } 12480 12481 // OpenMP [2.8.1, simd construct, Restrictions] 12482 // A list-item cannot appear in more than one aligned clause. 12483 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 12484 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 12485 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 12486 << getOpenMPClauseName(OMPC_aligned); 12487 continue; 12488 } 12489 12490 DeclRefExpr *Ref = nullptr; 12491 if (!VD && isOpenMPCapturedDecl(D)) 12492 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 12493 Vars.push_back(DefaultFunctionArrayConversion( 12494 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 12495 .get()); 12496 } 12497 12498 // OpenMP [2.8.1, simd construct, Description] 12499 // The parameter of the aligned clause, alignment, must be a constant 12500 // positive integer expression. 12501 // If no optional parameter is specified, implementation-defined default 12502 // alignments for SIMD instructions on the target platforms are assumed. 12503 if (Alignment != nullptr) { 12504 ExprResult AlignResult = 12505 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 12506 if (AlignResult.isInvalid()) 12507 return nullptr; 12508 Alignment = AlignResult.get(); 12509 } 12510 if (Vars.empty()) 12511 return nullptr; 12512 12513 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 12514 EndLoc, Vars, Alignment); 12515 } 12516 12517 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 12518 SourceLocation StartLoc, 12519 SourceLocation LParenLoc, 12520 SourceLocation EndLoc) { 12521 SmallVector<Expr *, 8> Vars; 12522 SmallVector<Expr *, 8> SrcExprs; 12523 SmallVector<Expr *, 8> DstExprs; 12524 SmallVector<Expr *, 8> AssignmentOps; 12525 for (Expr *RefExpr : VarList) { 12526 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 12527 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 12528 // It will be analyzed later. 12529 Vars.push_back(RefExpr); 12530 SrcExprs.push_back(nullptr); 12531 DstExprs.push_back(nullptr); 12532 AssignmentOps.push_back(nullptr); 12533 continue; 12534 } 12535 12536 SourceLocation ELoc = RefExpr->getExprLoc(); 12537 // OpenMP [2.1, C/C++] 12538 // A list item is a variable name. 12539 // OpenMP [2.14.4.1, Restrictions, p.1] 12540 // A list item that appears in a copyin clause must be threadprivate. 12541 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 12542 if (!DE || !isa<VarDecl>(DE->getDecl())) { 12543 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 12544 << 0 << RefExpr->getSourceRange(); 12545 continue; 12546 } 12547 12548 Decl *D = DE->getDecl(); 12549 auto *VD = cast<VarDecl>(D); 12550 12551 QualType Type = VD->getType(); 12552 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 12553 // It will be analyzed later. 12554 Vars.push_back(DE); 12555 SrcExprs.push_back(nullptr); 12556 DstExprs.push_back(nullptr); 12557 AssignmentOps.push_back(nullptr); 12558 continue; 12559 } 12560 12561 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 12562 // A list item that appears in a copyin clause must be threadprivate. 12563 if (!DSAStack->isThreadPrivate(VD)) { 12564 Diag(ELoc, diag::err_omp_required_access) 12565 << getOpenMPClauseName(OMPC_copyin) 12566 << getOpenMPDirectiveName(OMPD_threadprivate); 12567 continue; 12568 } 12569 12570 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 12571 // A variable of class type (or array thereof) that appears in a 12572 // copyin clause requires an accessible, unambiguous copy assignment 12573 // operator for the class type. 12574 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 12575 VarDecl *SrcVD = 12576 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 12577 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12578 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 12579 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 12580 VarDecl *DstVD = 12581 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 12582 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12583 DeclRefExpr *PseudoDstExpr = 12584 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 12585 // For arrays generate assignment operation for single element and replace 12586 // it by the original array element in CodeGen. 12587 ExprResult AssignmentOp = 12588 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 12589 PseudoSrcExpr); 12590 if (AssignmentOp.isInvalid()) 12591 continue; 12592 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 12593 /*DiscardedValue*/ false); 12594 if (AssignmentOp.isInvalid()) 12595 continue; 12596 12597 DSAStack->addDSA(VD, DE, OMPC_copyin); 12598 Vars.push_back(DE); 12599 SrcExprs.push_back(PseudoSrcExpr); 12600 DstExprs.push_back(PseudoDstExpr); 12601 AssignmentOps.push_back(AssignmentOp.get()); 12602 } 12603 12604 if (Vars.empty()) 12605 return nullptr; 12606 12607 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 12608 SrcExprs, DstExprs, AssignmentOps); 12609 } 12610 12611 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 12612 SourceLocation StartLoc, 12613 SourceLocation LParenLoc, 12614 SourceLocation EndLoc) { 12615 SmallVector<Expr *, 8> Vars; 12616 SmallVector<Expr *, 8> SrcExprs; 12617 SmallVector<Expr *, 8> DstExprs; 12618 SmallVector<Expr *, 8> AssignmentOps; 12619 for (Expr *RefExpr : VarList) { 12620 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12621 SourceLocation ELoc; 12622 SourceRange ERange; 12623 Expr *SimpleRefExpr = RefExpr; 12624 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12625 if (Res.second) { 12626 // It will be analyzed later. 12627 Vars.push_back(RefExpr); 12628 SrcExprs.push_back(nullptr); 12629 DstExprs.push_back(nullptr); 12630 AssignmentOps.push_back(nullptr); 12631 } 12632 ValueDecl *D = Res.first; 12633 if (!D) 12634 continue; 12635 12636 QualType Type = D->getType(); 12637 auto *VD = dyn_cast<VarDecl>(D); 12638 12639 // OpenMP [2.14.4.2, Restrictions, p.2] 12640 // A list item that appears in a copyprivate clause may not appear in a 12641 // private or firstprivate clause on the single construct. 12642 if (!VD || !DSAStack->isThreadPrivate(VD)) { 12643 DSAStackTy::DSAVarData DVar = 12644 DSAStack->getTopDSA(D, /*FromParent=*/false); 12645 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 12646 DVar.RefExpr) { 12647 Diag(ELoc, diag::err_omp_wrong_dsa) 12648 << getOpenMPClauseName(DVar.CKind) 12649 << getOpenMPClauseName(OMPC_copyprivate); 12650 reportOriginalDsa(*this, DSAStack, D, DVar); 12651 continue; 12652 } 12653 12654 // OpenMP [2.11.4.2, Restrictions, p.1] 12655 // All list items that appear in a copyprivate clause must be either 12656 // threadprivate or private in the enclosing context. 12657 if (DVar.CKind == OMPC_unknown) { 12658 DVar = DSAStack->getImplicitDSA(D, false); 12659 if (DVar.CKind == OMPC_shared) { 12660 Diag(ELoc, diag::err_omp_required_access) 12661 << getOpenMPClauseName(OMPC_copyprivate) 12662 << "threadprivate or private in the enclosing context"; 12663 reportOriginalDsa(*this, DSAStack, D, DVar); 12664 continue; 12665 } 12666 } 12667 } 12668 12669 // Variably modified types are not supported. 12670 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 12671 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 12672 << getOpenMPClauseName(OMPC_copyprivate) << Type 12673 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 12674 bool IsDecl = 12675 !VD || 12676 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12677 Diag(D->getLocation(), 12678 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12679 << D; 12680 continue; 12681 } 12682 12683 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 12684 // A variable of class type (or array thereof) that appears in a 12685 // copyin clause requires an accessible, unambiguous copy assignment 12686 // operator for the class type. 12687 Type = Context.getBaseElementType(Type.getNonReferenceType()) 12688 .getUnqualifiedType(); 12689 VarDecl *SrcVD = 12690 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 12691 D->hasAttrs() ? &D->getAttrs() : nullptr); 12692 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 12693 VarDecl *DstVD = 12694 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 12695 D->hasAttrs() ? &D->getAttrs() : nullptr); 12696 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 12697 ExprResult AssignmentOp = BuildBinOp( 12698 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 12699 if (AssignmentOp.isInvalid()) 12700 continue; 12701 AssignmentOp = 12702 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 12703 if (AssignmentOp.isInvalid()) 12704 continue; 12705 12706 // No need to mark vars as copyprivate, they are already threadprivate or 12707 // implicitly private. 12708 assert(VD || isOpenMPCapturedDecl(D)); 12709 Vars.push_back( 12710 VD ? RefExpr->IgnoreParens() 12711 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 12712 SrcExprs.push_back(PseudoSrcExpr); 12713 DstExprs.push_back(PseudoDstExpr); 12714 AssignmentOps.push_back(AssignmentOp.get()); 12715 } 12716 12717 if (Vars.empty()) 12718 return nullptr; 12719 12720 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12721 Vars, SrcExprs, DstExprs, AssignmentOps); 12722 } 12723 12724 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 12725 SourceLocation StartLoc, 12726 SourceLocation LParenLoc, 12727 SourceLocation EndLoc) { 12728 if (VarList.empty()) 12729 return nullptr; 12730 12731 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 12732 } 12733 12734 OMPClause * 12735 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 12736 SourceLocation DepLoc, SourceLocation ColonLoc, 12737 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 12738 SourceLocation LParenLoc, SourceLocation EndLoc) { 12739 if (DSAStack->getCurrentDirective() == OMPD_ordered && 12740 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 12741 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 12742 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 12743 return nullptr; 12744 } 12745 if (DSAStack->getCurrentDirective() != OMPD_ordered && 12746 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 12747 DepKind == OMPC_DEPEND_sink)) { 12748 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 12749 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 12750 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 12751 /*Last=*/OMPC_DEPEND_unknown, Except) 12752 << getOpenMPClauseName(OMPC_depend); 12753 return nullptr; 12754 } 12755 SmallVector<Expr *, 8> Vars; 12756 DSAStackTy::OperatorOffsetTy OpsOffs; 12757 llvm::APSInt DepCounter(/*BitWidth=*/32); 12758 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 12759 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 12760 if (const Expr *OrderedCountExpr = 12761 DSAStack->getParentOrderedRegionParam().first) { 12762 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 12763 TotalDepCount.setIsUnsigned(/*Val=*/true); 12764 } 12765 } 12766 for (Expr *RefExpr : VarList) { 12767 assert(RefExpr && "NULL expr in OpenMP shared clause."); 12768 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 12769 // It will be analyzed later. 12770 Vars.push_back(RefExpr); 12771 continue; 12772 } 12773 12774 SourceLocation ELoc = RefExpr->getExprLoc(); 12775 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 12776 if (DepKind == OMPC_DEPEND_sink) { 12777 if (DSAStack->getParentOrderedRegionParam().first && 12778 DepCounter >= TotalDepCount) { 12779 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 12780 continue; 12781 } 12782 ++DepCounter; 12783 // OpenMP [2.13.9, Summary] 12784 // depend(dependence-type : vec), where dependence-type is: 12785 // 'sink' and where vec is the iteration vector, which has the form: 12786 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 12787 // where n is the value specified by the ordered clause in the loop 12788 // directive, xi denotes the loop iteration variable of the i-th nested 12789 // loop associated with the loop directive, and di is a constant 12790 // non-negative integer. 12791 if (CurContext->isDependentContext()) { 12792 // It will be analyzed later. 12793 Vars.push_back(RefExpr); 12794 continue; 12795 } 12796 SimpleExpr = SimpleExpr->IgnoreImplicit(); 12797 OverloadedOperatorKind OOK = OO_None; 12798 SourceLocation OOLoc; 12799 Expr *LHS = SimpleExpr; 12800 Expr *RHS = nullptr; 12801 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 12802 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 12803 OOLoc = BO->getOperatorLoc(); 12804 LHS = BO->getLHS()->IgnoreParenImpCasts(); 12805 RHS = BO->getRHS()->IgnoreParenImpCasts(); 12806 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 12807 OOK = OCE->getOperator(); 12808 OOLoc = OCE->getOperatorLoc(); 12809 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12810 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 12811 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 12812 OOK = MCE->getMethodDecl() 12813 ->getNameInfo() 12814 .getName() 12815 .getCXXOverloadedOperator(); 12816 OOLoc = MCE->getCallee()->getExprLoc(); 12817 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 12818 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12819 } 12820 SourceLocation ELoc; 12821 SourceRange ERange; 12822 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 12823 if (Res.second) { 12824 // It will be analyzed later. 12825 Vars.push_back(RefExpr); 12826 } 12827 ValueDecl *D = Res.first; 12828 if (!D) 12829 continue; 12830 12831 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 12832 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 12833 continue; 12834 } 12835 if (RHS) { 12836 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 12837 RHS, OMPC_depend, /*StrictlyPositive=*/false); 12838 if (RHSRes.isInvalid()) 12839 continue; 12840 } 12841 if (!CurContext->isDependentContext() && 12842 DSAStack->getParentOrderedRegionParam().first && 12843 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 12844 const ValueDecl *VD = 12845 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 12846 if (VD) 12847 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 12848 << 1 << VD; 12849 else 12850 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 12851 continue; 12852 } 12853 OpsOffs.emplace_back(RHS, OOK); 12854 } else { 12855 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 12856 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 12857 (ASE && 12858 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 12859 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 12860 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12861 << RefExpr->getSourceRange(); 12862 continue; 12863 } 12864 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 12865 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 12866 ExprResult Res = 12867 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 12868 getDiagnostics().setSuppressAllDiagnostics(Suppress); 12869 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 12870 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12871 << RefExpr->getSourceRange(); 12872 continue; 12873 } 12874 } 12875 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 12876 } 12877 12878 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 12879 TotalDepCount > VarList.size() && 12880 DSAStack->getParentOrderedRegionParam().first && 12881 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 12882 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 12883 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 12884 } 12885 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 12886 Vars.empty()) 12887 return nullptr; 12888 12889 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12890 DepKind, DepLoc, ColonLoc, Vars, 12891 TotalDepCount.getZExtValue()); 12892 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 12893 DSAStack->isParentOrderedRegion()) 12894 DSAStack->addDoacrossDependClause(C, OpsOffs); 12895 return C; 12896 } 12897 12898 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 12899 SourceLocation LParenLoc, 12900 SourceLocation EndLoc) { 12901 Expr *ValExpr = Device; 12902 Stmt *HelperValStmt = nullptr; 12903 12904 // OpenMP [2.9.1, Restrictions] 12905 // The device expression must evaluate to a non-negative integer value. 12906 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 12907 /*StrictlyPositive=*/false)) 12908 return nullptr; 12909 12910 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12911 OpenMPDirectiveKind CaptureRegion = 12912 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 12913 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12914 ValExpr = MakeFullExpr(ValExpr).get(); 12915 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12916 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12917 HelperValStmt = buildPreInits(Context, Captures); 12918 } 12919 12920 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 12921 StartLoc, LParenLoc, EndLoc); 12922 } 12923 12924 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 12925 DSAStackTy *Stack, QualType QTy, 12926 bool FullCheck = true) { 12927 NamedDecl *ND; 12928 if (QTy->isIncompleteType(&ND)) { 12929 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 12930 return false; 12931 } 12932 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 12933 !QTy.isTrivialType(SemaRef.Context)) 12934 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 12935 return true; 12936 } 12937 12938 /// Return true if it can be proven that the provided array expression 12939 /// (array section or array subscript) does NOT specify the whole size of the 12940 /// array whose base type is \a BaseQTy. 12941 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 12942 const Expr *E, 12943 QualType BaseQTy) { 12944 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 12945 12946 // If this is an array subscript, it refers to the whole size if the size of 12947 // the dimension is constant and equals 1. Also, an array section assumes the 12948 // format of an array subscript if no colon is used. 12949 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 12950 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 12951 return ATy->getSize().getSExtValue() != 1; 12952 // Size can't be evaluated statically. 12953 return false; 12954 } 12955 12956 assert(OASE && "Expecting array section if not an array subscript."); 12957 const Expr *LowerBound = OASE->getLowerBound(); 12958 const Expr *Length = OASE->getLength(); 12959 12960 // If there is a lower bound that does not evaluates to zero, we are not 12961 // covering the whole dimension. 12962 if (LowerBound) { 12963 Expr::EvalResult Result; 12964 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 12965 return false; // Can't get the integer value as a constant. 12966 12967 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 12968 if (ConstLowerBound.getSExtValue()) 12969 return true; 12970 } 12971 12972 // If we don't have a length we covering the whole dimension. 12973 if (!Length) 12974 return false; 12975 12976 // If the base is a pointer, we don't have a way to get the size of the 12977 // pointee. 12978 if (BaseQTy->isPointerType()) 12979 return false; 12980 12981 // We can only check if the length is the same as the size of the dimension 12982 // if we have a constant array. 12983 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 12984 if (!CATy) 12985 return false; 12986 12987 Expr::EvalResult Result; 12988 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 12989 return false; // Can't get the integer value as a constant. 12990 12991 llvm::APSInt ConstLength = Result.Val.getInt(); 12992 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 12993 } 12994 12995 // Return true if it can be proven that the provided array expression (array 12996 // section or array subscript) does NOT specify a single element of the array 12997 // whose base type is \a BaseQTy. 12998 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 12999 const Expr *E, 13000 QualType BaseQTy) { 13001 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 13002 13003 // An array subscript always refer to a single element. Also, an array section 13004 // assumes the format of an array subscript if no colon is used. 13005 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 13006 return false; 13007 13008 assert(OASE && "Expecting array section if not an array subscript."); 13009 const Expr *Length = OASE->getLength(); 13010 13011 // If we don't have a length we have to check if the array has unitary size 13012 // for this dimension. Also, we should always expect a length if the base type 13013 // is pointer. 13014 if (!Length) { 13015 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 13016 return ATy->getSize().getSExtValue() != 1; 13017 // We cannot assume anything. 13018 return false; 13019 } 13020 13021 // Check if the length evaluates to 1. 13022 Expr::EvalResult Result; 13023 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 13024 return false; // Can't get the integer value as a constant. 13025 13026 llvm::APSInt ConstLength = Result.Val.getInt(); 13027 return ConstLength.getSExtValue() != 1; 13028 } 13029 13030 // Return the expression of the base of the mappable expression or null if it 13031 // cannot be determined and do all the necessary checks to see if the expression 13032 // is valid as a standalone mappable expression. In the process, record all the 13033 // components of the expression. 13034 static const Expr *checkMapClauseExpressionBase( 13035 Sema &SemaRef, Expr *E, 13036 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 13037 OpenMPClauseKind CKind, bool NoDiagnose) { 13038 SourceLocation ELoc = E->getExprLoc(); 13039 SourceRange ERange = E->getSourceRange(); 13040 13041 // The base of elements of list in a map clause have to be either: 13042 // - a reference to variable or field. 13043 // - a member expression. 13044 // - an array expression. 13045 // 13046 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 13047 // reference to 'r'. 13048 // 13049 // If we have: 13050 // 13051 // struct SS { 13052 // Bla S; 13053 // foo() { 13054 // #pragma omp target map (S.Arr[:12]); 13055 // } 13056 // } 13057 // 13058 // We want to retrieve the member expression 'this->S'; 13059 13060 const Expr *RelevantExpr = nullptr; 13061 13062 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 13063 // If a list item is an array section, it must specify contiguous storage. 13064 // 13065 // For this restriction it is sufficient that we make sure only references 13066 // to variables or fields and array expressions, and that no array sections 13067 // exist except in the rightmost expression (unless they cover the whole 13068 // dimension of the array). E.g. these would be invalid: 13069 // 13070 // r.ArrS[3:5].Arr[6:7] 13071 // 13072 // r.ArrS[3:5].x 13073 // 13074 // but these would be valid: 13075 // r.ArrS[3].Arr[6:7] 13076 // 13077 // r.ArrS[3].x 13078 13079 bool AllowUnitySizeArraySection = true; 13080 bool AllowWholeSizeArraySection = true; 13081 13082 while (!RelevantExpr) { 13083 E = E->IgnoreParenImpCasts(); 13084 13085 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 13086 if (!isa<VarDecl>(CurE->getDecl())) 13087 return nullptr; 13088 13089 RelevantExpr = CurE; 13090 13091 // If we got a reference to a declaration, we should not expect any array 13092 // section before that. 13093 AllowUnitySizeArraySection = false; 13094 AllowWholeSizeArraySection = false; 13095 13096 // Record the component. 13097 CurComponents.emplace_back(CurE, CurE->getDecl()); 13098 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 13099 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 13100 13101 if (isa<CXXThisExpr>(BaseE)) 13102 // We found a base expression: this->Val. 13103 RelevantExpr = CurE; 13104 else 13105 E = BaseE; 13106 13107 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 13108 if (!NoDiagnose) { 13109 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 13110 << CurE->getSourceRange(); 13111 return nullptr; 13112 } 13113 if (RelevantExpr) 13114 return nullptr; 13115 continue; 13116 } 13117 13118 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 13119 13120 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 13121 // A bit-field cannot appear in a map clause. 13122 // 13123 if (FD->isBitField()) { 13124 if (!NoDiagnose) { 13125 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 13126 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 13127 return nullptr; 13128 } 13129 if (RelevantExpr) 13130 return nullptr; 13131 continue; 13132 } 13133 13134 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13135 // If the type of a list item is a reference to a type T then the type 13136 // will be considered to be T for all purposes of this clause. 13137 QualType CurType = BaseE->getType().getNonReferenceType(); 13138 13139 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 13140 // A list item cannot be a variable that is a member of a structure with 13141 // a union type. 13142 // 13143 if (CurType->isUnionType()) { 13144 if (!NoDiagnose) { 13145 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 13146 << CurE->getSourceRange(); 13147 return nullptr; 13148 } 13149 continue; 13150 } 13151 13152 // If we got a member expression, we should not expect any array section 13153 // before that: 13154 // 13155 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 13156 // If a list item is an element of a structure, only the rightmost symbol 13157 // of the variable reference can be an array section. 13158 // 13159 AllowUnitySizeArraySection = false; 13160 AllowWholeSizeArraySection = false; 13161 13162 // Record the component. 13163 CurComponents.emplace_back(CurE, FD); 13164 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 13165 E = CurE->getBase()->IgnoreParenImpCasts(); 13166 13167 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 13168 if (!NoDiagnose) { 13169 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 13170 << 0 << CurE->getSourceRange(); 13171 return nullptr; 13172 } 13173 continue; 13174 } 13175 13176 // If we got an array subscript that express the whole dimension we 13177 // can have any array expressions before. If it only expressing part of 13178 // the dimension, we can only have unitary-size array expressions. 13179 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 13180 E->getType())) 13181 AllowWholeSizeArraySection = false; 13182 13183 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 13184 Expr::EvalResult Result; 13185 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) { 13186 if (!Result.Val.getInt().isNullValue()) { 13187 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 13188 diag::err_omp_invalid_map_this_expr); 13189 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 13190 diag::note_omp_invalid_subscript_on_this_ptr_map); 13191 } 13192 } 13193 RelevantExpr = TE; 13194 } 13195 13196 // Record the component - we don't have any declaration associated. 13197 CurComponents.emplace_back(CurE, nullptr); 13198 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 13199 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 13200 E = CurE->getBase()->IgnoreParenImpCasts(); 13201 13202 QualType CurType = 13203 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 13204 13205 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13206 // If the type of a list item is a reference to a type T then the type 13207 // will be considered to be T for all purposes of this clause. 13208 if (CurType->isReferenceType()) 13209 CurType = CurType->getPointeeType(); 13210 13211 bool IsPointer = CurType->isAnyPointerType(); 13212 13213 if (!IsPointer && !CurType->isArrayType()) { 13214 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 13215 << 0 << CurE->getSourceRange(); 13216 return nullptr; 13217 } 13218 13219 bool NotWhole = 13220 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 13221 bool NotUnity = 13222 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 13223 13224 if (AllowWholeSizeArraySection) { 13225 // Any array section is currently allowed. Allowing a whole size array 13226 // section implies allowing a unity array section as well. 13227 // 13228 // If this array section refers to the whole dimension we can still 13229 // accept other array sections before this one, except if the base is a 13230 // pointer. Otherwise, only unitary sections are accepted. 13231 if (NotWhole || IsPointer) 13232 AllowWholeSizeArraySection = false; 13233 } else if (AllowUnitySizeArraySection && NotUnity) { 13234 // A unity or whole array section is not allowed and that is not 13235 // compatible with the properties of the current array section. 13236 SemaRef.Diag( 13237 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 13238 << CurE->getSourceRange(); 13239 return nullptr; 13240 } 13241 13242 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 13243 Expr::EvalResult ResultR; 13244 Expr::EvalResult ResultL; 13245 if (CurE->getLength()->EvaluateAsInt(ResultR, 13246 SemaRef.getASTContext())) { 13247 if (!ResultR.Val.getInt().isOneValue()) { 13248 SemaRef.Diag(CurE->getLength()->getExprLoc(), 13249 diag::err_omp_invalid_map_this_expr); 13250 SemaRef.Diag(CurE->getLength()->getExprLoc(), 13251 diag::note_omp_invalid_length_on_this_ptr_mapping); 13252 } 13253 } 13254 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt( 13255 ResultL, SemaRef.getASTContext())) { 13256 if (!ResultL.Val.getInt().isNullValue()) { 13257 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 13258 diag::err_omp_invalid_map_this_expr); 13259 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 13260 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 13261 } 13262 } 13263 RelevantExpr = TE; 13264 } 13265 13266 // Record the component - we don't have any declaration associated. 13267 CurComponents.emplace_back(CurE, nullptr); 13268 } else { 13269 if (!NoDiagnose) { 13270 // If nothing else worked, this is not a valid map clause expression. 13271 SemaRef.Diag( 13272 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 13273 << ERange; 13274 } 13275 return nullptr; 13276 } 13277 } 13278 13279 return RelevantExpr; 13280 } 13281 13282 // Return true if expression E associated with value VD has conflicts with other 13283 // map information. 13284 static bool checkMapConflicts( 13285 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 13286 bool CurrentRegionOnly, 13287 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 13288 OpenMPClauseKind CKind) { 13289 assert(VD && E); 13290 SourceLocation ELoc = E->getExprLoc(); 13291 SourceRange ERange = E->getSourceRange(); 13292 13293 // In order to easily check the conflicts we need to match each component of 13294 // the expression under test with the components of the expressions that are 13295 // already in the stack. 13296 13297 assert(!CurComponents.empty() && "Map clause expression with no components!"); 13298 assert(CurComponents.back().getAssociatedDeclaration() == VD && 13299 "Map clause expression with unexpected base!"); 13300 13301 // Variables to help detecting enclosing problems in data environment nests. 13302 bool IsEnclosedByDataEnvironmentExpr = false; 13303 const Expr *EnclosingExpr = nullptr; 13304 13305 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 13306 VD, CurrentRegionOnly, 13307 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 13308 ERange, CKind, &EnclosingExpr, 13309 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 13310 StackComponents, 13311 OpenMPClauseKind) { 13312 assert(!StackComponents.empty() && 13313 "Map clause expression with no components!"); 13314 assert(StackComponents.back().getAssociatedDeclaration() == VD && 13315 "Map clause expression with unexpected base!"); 13316 (void)VD; 13317 13318 // The whole expression in the stack. 13319 const Expr *RE = StackComponents.front().getAssociatedExpression(); 13320 13321 // Expressions must start from the same base. Here we detect at which 13322 // point both expressions diverge from each other and see if we can 13323 // detect if the memory referred to both expressions is contiguous and 13324 // do not overlap. 13325 auto CI = CurComponents.rbegin(); 13326 auto CE = CurComponents.rend(); 13327 auto SI = StackComponents.rbegin(); 13328 auto SE = StackComponents.rend(); 13329 for (; CI != CE && SI != SE; ++CI, ++SI) { 13330 13331 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 13332 // At most one list item can be an array item derived from a given 13333 // variable in map clauses of the same construct. 13334 if (CurrentRegionOnly && 13335 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 13336 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 13337 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 13338 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 13339 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 13340 diag::err_omp_multiple_array_items_in_map_clause) 13341 << CI->getAssociatedExpression()->getSourceRange(); 13342 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 13343 diag::note_used_here) 13344 << SI->getAssociatedExpression()->getSourceRange(); 13345 return true; 13346 } 13347 13348 // Do both expressions have the same kind? 13349 if (CI->getAssociatedExpression()->getStmtClass() != 13350 SI->getAssociatedExpression()->getStmtClass()) 13351 break; 13352 13353 // Are we dealing with different variables/fields? 13354 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 13355 break; 13356 } 13357 // Check if the extra components of the expressions in the enclosing 13358 // data environment are redundant for the current base declaration. 13359 // If they are, the maps completely overlap, which is legal. 13360 for (; SI != SE; ++SI) { 13361 QualType Type; 13362 if (const auto *ASE = 13363 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 13364 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 13365 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 13366 SI->getAssociatedExpression())) { 13367 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 13368 Type = 13369 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 13370 } 13371 if (Type.isNull() || Type->isAnyPointerType() || 13372 checkArrayExpressionDoesNotReferToWholeSize( 13373 SemaRef, SI->getAssociatedExpression(), Type)) 13374 break; 13375 } 13376 13377 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 13378 // List items of map clauses in the same construct must not share 13379 // original storage. 13380 // 13381 // If the expressions are exactly the same or one is a subset of the 13382 // other, it means they are sharing storage. 13383 if (CI == CE && SI == SE) { 13384 if (CurrentRegionOnly) { 13385 if (CKind == OMPC_map) { 13386 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 13387 } else { 13388 assert(CKind == OMPC_to || CKind == OMPC_from); 13389 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 13390 << ERange; 13391 } 13392 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13393 << RE->getSourceRange(); 13394 return true; 13395 } 13396 // If we find the same expression in the enclosing data environment, 13397 // that is legal. 13398 IsEnclosedByDataEnvironmentExpr = true; 13399 return false; 13400 } 13401 13402 QualType DerivedType = 13403 std::prev(CI)->getAssociatedDeclaration()->getType(); 13404 SourceLocation DerivedLoc = 13405 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 13406 13407 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13408 // If the type of a list item is a reference to a type T then the type 13409 // will be considered to be T for all purposes of this clause. 13410 DerivedType = DerivedType.getNonReferenceType(); 13411 13412 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 13413 // A variable for which the type is pointer and an array section 13414 // derived from that variable must not appear as list items of map 13415 // clauses of the same construct. 13416 // 13417 // Also, cover one of the cases in: 13418 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 13419 // If any part of the original storage of a list item has corresponding 13420 // storage in the device data environment, all of the original storage 13421 // must have corresponding storage in the device data environment. 13422 // 13423 if (DerivedType->isAnyPointerType()) { 13424 if (CI == CE || SI == SE) { 13425 SemaRef.Diag( 13426 DerivedLoc, 13427 diag::err_omp_pointer_mapped_along_with_derived_section) 13428 << DerivedLoc; 13429 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13430 << RE->getSourceRange(); 13431 return true; 13432 } 13433 if (CI->getAssociatedExpression()->getStmtClass() != 13434 SI->getAssociatedExpression()->getStmtClass() || 13435 CI->getAssociatedDeclaration()->getCanonicalDecl() == 13436 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 13437 assert(CI != CE && SI != SE); 13438 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 13439 << DerivedLoc; 13440 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13441 << RE->getSourceRange(); 13442 return true; 13443 } 13444 } 13445 13446 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 13447 // List items of map clauses in the same construct must not share 13448 // original storage. 13449 // 13450 // An expression is a subset of the other. 13451 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 13452 if (CKind == OMPC_map) { 13453 if (CI != CE || SI != SE) { 13454 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 13455 // a pointer. 13456 auto Begin = 13457 CI != CE ? CurComponents.begin() : StackComponents.begin(); 13458 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 13459 auto It = Begin; 13460 while (It != End && !It->getAssociatedDeclaration()) 13461 std::advance(It, 1); 13462 assert(It != End && 13463 "Expected at least one component with the declaration."); 13464 if (It != Begin && It->getAssociatedDeclaration() 13465 ->getType() 13466 .getCanonicalType() 13467 ->isAnyPointerType()) { 13468 IsEnclosedByDataEnvironmentExpr = false; 13469 EnclosingExpr = nullptr; 13470 return false; 13471 } 13472 } 13473 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 13474 } else { 13475 assert(CKind == OMPC_to || CKind == OMPC_from); 13476 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 13477 << ERange; 13478 } 13479 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13480 << RE->getSourceRange(); 13481 return true; 13482 } 13483 13484 // The current expression uses the same base as other expression in the 13485 // data environment but does not contain it completely. 13486 if (!CurrentRegionOnly && SI != SE) 13487 EnclosingExpr = RE; 13488 13489 // The current expression is a subset of the expression in the data 13490 // environment. 13491 IsEnclosedByDataEnvironmentExpr |= 13492 (!CurrentRegionOnly && CI != CE && SI == SE); 13493 13494 return false; 13495 }); 13496 13497 if (CurrentRegionOnly) 13498 return FoundError; 13499 13500 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 13501 // If any part of the original storage of a list item has corresponding 13502 // storage in the device data environment, all of the original storage must 13503 // have corresponding storage in the device data environment. 13504 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 13505 // If a list item is an element of a structure, and a different element of 13506 // the structure has a corresponding list item in the device data environment 13507 // prior to a task encountering the construct associated with the map clause, 13508 // then the list item must also have a corresponding list item in the device 13509 // data environment prior to the task encountering the construct. 13510 // 13511 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 13512 SemaRef.Diag(ELoc, 13513 diag::err_omp_original_storage_is_shared_and_does_not_contain) 13514 << ERange; 13515 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 13516 << EnclosingExpr->getSourceRange(); 13517 return true; 13518 } 13519 13520 return FoundError; 13521 } 13522 13523 // Look up the user-defined mapper given the mapper name and mapped type, and 13524 // build a reference to it. 13525 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 13526 CXXScopeSpec &MapperIdScopeSpec, 13527 const DeclarationNameInfo &MapperId, 13528 QualType Type, 13529 Expr *UnresolvedMapper) { 13530 if (MapperIdScopeSpec.isInvalid()) 13531 return ExprError(); 13532 // Find all user-defined mappers with the given MapperId. 13533 SmallVector<UnresolvedSet<8>, 4> Lookups; 13534 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 13535 Lookup.suppressDiagnostics(); 13536 if (S) { 13537 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 13538 NamedDecl *D = Lookup.getRepresentativeDecl(); 13539 while (S && !S->isDeclScope(D)) 13540 S = S->getParent(); 13541 if (S) 13542 S = S->getParent(); 13543 Lookups.emplace_back(); 13544 Lookups.back().append(Lookup.begin(), Lookup.end()); 13545 Lookup.clear(); 13546 } 13547 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 13548 // Extract the user-defined mappers with the given MapperId. 13549 Lookups.push_back(UnresolvedSet<8>()); 13550 for (NamedDecl *D : ULE->decls()) { 13551 auto *DMD = cast<OMPDeclareMapperDecl>(D); 13552 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 13553 Lookups.back().addDecl(DMD); 13554 } 13555 } 13556 // Defer the lookup for dependent types. The results will be passed through 13557 // UnresolvedMapper on instantiation. 13558 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 13559 Type->isInstantiationDependentType() || 13560 Type->containsUnexpandedParameterPack() || 13561 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 13562 return !D->isInvalidDecl() && 13563 (D->getType()->isDependentType() || 13564 D->getType()->isInstantiationDependentType() || 13565 D->getType()->containsUnexpandedParameterPack()); 13566 })) { 13567 UnresolvedSet<8> URS; 13568 for (const UnresolvedSet<8> &Set : Lookups) { 13569 if (Set.empty()) 13570 continue; 13571 URS.append(Set.begin(), Set.end()); 13572 } 13573 return UnresolvedLookupExpr::Create( 13574 SemaRef.Context, /*NamingClass=*/nullptr, 13575 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 13576 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 13577 } 13578 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 13579 // The type must be of struct, union or class type in C and C++ 13580 if (!Type->isStructureOrClassType() && !Type->isUnionType()) 13581 return ExprEmpty(); 13582 SourceLocation Loc = MapperId.getLoc(); 13583 // Perform argument dependent lookup. 13584 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 13585 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 13586 // Return the first user-defined mapper with the desired type. 13587 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13588 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 13589 if (!D->isInvalidDecl() && 13590 SemaRef.Context.hasSameType(D->getType(), Type)) 13591 return D; 13592 return nullptr; 13593 })) 13594 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13595 // Find the first user-defined mapper with a type derived from the desired 13596 // type. 13597 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13598 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 13599 if (!D->isInvalidDecl() && 13600 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 13601 !Type.isMoreQualifiedThan(D->getType())) 13602 return D; 13603 return nullptr; 13604 })) { 13605 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 13606 /*DetectVirtual=*/false); 13607 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 13608 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 13609 VD->getType().getUnqualifiedType()))) { 13610 if (SemaRef.CheckBaseClassAccess( 13611 Loc, VD->getType(), Type, Paths.front(), 13612 /*DiagID=*/0) != Sema::AR_inaccessible) { 13613 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13614 } 13615 } 13616 } 13617 } 13618 // Report error if a mapper is specified, but cannot be found. 13619 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 13620 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 13621 << Type << MapperId.getName(); 13622 return ExprError(); 13623 } 13624 return ExprEmpty(); 13625 } 13626 13627 namespace { 13628 // Utility struct that gathers all the related lists associated with a mappable 13629 // expression. 13630 struct MappableVarListInfo { 13631 // The list of expressions. 13632 ArrayRef<Expr *> VarList; 13633 // The list of processed expressions. 13634 SmallVector<Expr *, 16> ProcessedVarList; 13635 // The mappble components for each expression. 13636 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 13637 // The base declaration of the variable. 13638 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 13639 // The reference to the user-defined mapper associated with every expression. 13640 SmallVector<Expr *, 16> UDMapperList; 13641 13642 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 13643 // We have a list of components and base declarations for each entry in the 13644 // variable list. 13645 VarComponents.reserve(VarList.size()); 13646 VarBaseDeclarations.reserve(VarList.size()); 13647 } 13648 }; 13649 } 13650 13651 // Check the validity of the provided variable list for the provided clause kind 13652 // \a CKind. In the check process the valid expressions, mappable expression 13653 // components, variables, and user-defined mappers are extracted and used to 13654 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 13655 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 13656 // and \a MapperId are expected to be valid if the clause kind is 'map'. 13657 static void checkMappableExpressionList( 13658 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 13659 MappableVarListInfo &MVLI, SourceLocation StartLoc, 13660 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 13661 ArrayRef<Expr *> UnresolvedMappers, 13662 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 13663 bool IsMapTypeImplicit = false) { 13664 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 13665 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 13666 "Unexpected clause kind with mappable expressions!"); 13667 13668 // If the identifier of user-defined mapper is not specified, it is "default". 13669 // We do not change the actual name in this clause to distinguish whether a 13670 // mapper is specified explicitly, i.e., it is not explicitly specified when 13671 // MapperId.getName() is empty. 13672 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 13673 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 13674 MapperId.setName(DeclNames.getIdentifier( 13675 &SemaRef.getASTContext().Idents.get("default"))); 13676 } 13677 13678 // Iterators to find the current unresolved mapper expression. 13679 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 13680 bool UpdateUMIt = false; 13681 Expr *UnresolvedMapper = nullptr; 13682 13683 // Keep track of the mappable components and base declarations in this clause. 13684 // Each entry in the list is going to have a list of components associated. We 13685 // record each set of the components so that we can build the clause later on. 13686 // In the end we should have the same amount of declarations and component 13687 // lists. 13688 13689 for (Expr *RE : MVLI.VarList) { 13690 assert(RE && "Null expr in omp to/from/map clause"); 13691 SourceLocation ELoc = RE->getExprLoc(); 13692 13693 // Find the current unresolved mapper expression. 13694 if (UpdateUMIt && UMIt != UMEnd) { 13695 UMIt++; 13696 assert( 13697 UMIt != UMEnd && 13698 "Expect the size of UnresolvedMappers to match with that of VarList"); 13699 } 13700 UpdateUMIt = true; 13701 if (UMIt != UMEnd) 13702 UnresolvedMapper = *UMIt; 13703 13704 const Expr *VE = RE->IgnoreParenLValueCasts(); 13705 13706 if (VE->isValueDependent() || VE->isTypeDependent() || 13707 VE->isInstantiationDependent() || 13708 VE->containsUnexpandedParameterPack()) { 13709 // Try to find the associated user-defined mapper. 13710 ExprResult ER = buildUserDefinedMapperRef( 13711 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13712 VE->getType().getCanonicalType(), UnresolvedMapper); 13713 if (ER.isInvalid()) 13714 continue; 13715 MVLI.UDMapperList.push_back(ER.get()); 13716 // We can only analyze this information once the missing information is 13717 // resolved. 13718 MVLI.ProcessedVarList.push_back(RE); 13719 continue; 13720 } 13721 13722 Expr *SimpleExpr = RE->IgnoreParenCasts(); 13723 13724 if (!RE->IgnoreParenImpCasts()->isLValue()) { 13725 SemaRef.Diag(ELoc, 13726 diag::err_omp_expected_named_var_member_or_array_expression) 13727 << RE->getSourceRange(); 13728 continue; 13729 } 13730 13731 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 13732 ValueDecl *CurDeclaration = nullptr; 13733 13734 // Obtain the array or member expression bases if required. Also, fill the 13735 // components array with all the components identified in the process. 13736 const Expr *BE = checkMapClauseExpressionBase( 13737 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 13738 if (!BE) 13739 continue; 13740 13741 assert(!CurComponents.empty() && 13742 "Invalid mappable expression information."); 13743 13744 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 13745 // Add store "this" pointer to class in DSAStackTy for future checking 13746 DSAS->addMappedClassesQualTypes(TE->getType()); 13747 // Try to find the associated user-defined mapper. 13748 ExprResult ER = buildUserDefinedMapperRef( 13749 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13750 VE->getType().getCanonicalType(), UnresolvedMapper); 13751 if (ER.isInvalid()) 13752 continue; 13753 MVLI.UDMapperList.push_back(ER.get()); 13754 // Skip restriction checking for variable or field declarations 13755 MVLI.ProcessedVarList.push_back(RE); 13756 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13757 MVLI.VarComponents.back().append(CurComponents.begin(), 13758 CurComponents.end()); 13759 MVLI.VarBaseDeclarations.push_back(nullptr); 13760 continue; 13761 } 13762 13763 // For the following checks, we rely on the base declaration which is 13764 // expected to be associated with the last component. The declaration is 13765 // expected to be a variable or a field (if 'this' is being mapped). 13766 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 13767 assert(CurDeclaration && "Null decl on map clause."); 13768 assert( 13769 CurDeclaration->isCanonicalDecl() && 13770 "Expecting components to have associated only canonical declarations."); 13771 13772 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 13773 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 13774 13775 assert((VD || FD) && "Only variables or fields are expected here!"); 13776 (void)FD; 13777 13778 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 13779 // threadprivate variables cannot appear in a map clause. 13780 // OpenMP 4.5 [2.10.5, target update Construct] 13781 // threadprivate variables cannot appear in a from clause. 13782 if (VD && DSAS->isThreadPrivate(VD)) { 13783 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 13784 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 13785 << getOpenMPClauseName(CKind); 13786 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 13787 continue; 13788 } 13789 13790 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 13791 // A list item cannot appear in both a map clause and a data-sharing 13792 // attribute clause on the same construct. 13793 13794 // Check conflicts with other map clause expressions. We check the conflicts 13795 // with the current construct separately from the enclosing data 13796 // environment, because the restrictions are different. We only have to 13797 // check conflicts across regions for the map clauses. 13798 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 13799 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 13800 break; 13801 if (CKind == OMPC_map && 13802 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 13803 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 13804 break; 13805 13806 // OpenMP 4.5 [2.10.5, target update Construct] 13807 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13808 // If the type of a list item is a reference to a type T then the type will 13809 // be considered to be T for all purposes of this clause. 13810 auto I = llvm::find_if( 13811 CurComponents, 13812 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 13813 return MC.getAssociatedDeclaration(); 13814 }); 13815 assert(I != CurComponents.end() && "Null decl on map clause."); 13816 QualType Type = 13817 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 13818 13819 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 13820 // A list item in a to or from clause must have a mappable type. 13821 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 13822 // A list item must have a mappable type. 13823 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 13824 DSAS, Type)) 13825 continue; 13826 13827 if (CKind == OMPC_map) { 13828 // target enter data 13829 // OpenMP [2.10.2, Restrictions, p. 99] 13830 // A map-type must be specified in all map clauses and must be either 13831 // to or alloc. 13832 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 13833 if (DKind == OMPD_target_enter_data && 13834 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 13835 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 13836 << (IsMapTypeImplicit ? 1 : 0) 13837 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 13838 << getOpenMPDirectiveName(DKind); 13839 continue; 13840 } 13841 13842 // target exit_data 13843 // OpenMP [2.10.3, Restrictions, p. 102] 13844 // A map-type must be specified in all map clauses and must be either 13845 // from, release, or delete. 13846 if (DKind == OMPD_target_exit_data && 13847 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 13848 MapType == OMPC_MAP_delete)) { 13849 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 13850 << (IsMapTypeImplicit ? 1 : 0) 13851 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 13852 << getOpenMPDirectiveName(DKind); 13853 continue; 13854 } 13855 13856 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13857 // A list item cannot appear in both a map clause and a data-sharing 13858 // attribute clause on the same construct 13859 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 13860 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 13861 if (isOpenMPPrivate(DVar.CKind)) { 13862 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13863 << getOpenMPClauseName(DVar.CKind) 13864 << getOpenMPClauseName(OMPC_map) 13865 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 13866 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 13867 continue; 13868 } 13869 } 13870 } 13871 13872 // Try to find the associated user-defined mapper. 13873 ExprResult ER = buildUserDefinedMapperRef( 13874 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13875 Type.getCanonicalType(), UnresolvedMapper); 13876 if (ER.isInvalid()) 13877 continue; 13878 MVLI.UDMapperList.push_back(ER.get()); 13879 13880 // Save the current expression. 13881 MVLI.ProcessedVarList.push_back(RE); 13882 13883 // Store the components in the stack so that they can be used to check 13884 // against other clauses later on. 13885 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 13886 /*WhereFoundClauseKind=*/OMPC_map); 13887 13888 // Save the components and declaration to create the clause. For purposes of 13889 // the clause creation, any component list that has has base 'this' uses 13890 // null as base declaration. 13891 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13892 MVLI.VarComponents.back().append(CurComponents.begin(), 13893 CurComponents.end()); 13894 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 13895 : CurDeclaration); 13896 } 13897 } 13898 13899 OMPClause *Sema::ActOnOpenMPMapClause( 13900 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 13901 ArrayRef<SourceLocation> MapTypeModifiersLoc, 13902 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 13903 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 13904 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 13905 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 13906 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown, 13907 OMPC_MAP_MODIFIER_unknown, 13908 OMPC_MAP_MODIFIER_unknown}; 13909 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers]; 13910 13911 // Process map-type-modifiers, flag errors for duplicate modifiers. 13912 unsigned Count = 0; 13913 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 13914 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 13915 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 13916 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 13917 continue; 13918 } 13919 assert(Count < OMPMapClause::NumberOfModifiers && 13920 "Modifiers exceed the allowed number of map type modifiers"); 13921 Modifiers[Count] = MapTypeModifiers[I]; 13922 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 13923 ++Count; 13924 } 13925 13926 MappableVarListInfo MVLI(VarList); 13927 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 13928 MapperIdScopeSpec, MapperId, UnresolvedMappers, 13929 MapType, IsMapTypeImplicit); 13930 13931 // We need to produce a map clause even if we don't have variables so that 13932 // other diagnostics related with non-existing map clauses are accurate. 13933 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 13934 MVLI.VarBaseDeclarations, MVLI.VarComponents, 13935 MVLI.UDMapperList, Modifiers, ModifiersLoc, 13936 MapperIdScopeSpec.getWithLocInContext(Context), 13937 MapperId, MapType, IsMapTypeImplicit, MapLoc); 13938 } 13939 13940 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 13941 TypeResult ParsedType) { 13942 assert(ParsedType.isUsable()); 13943 13944 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 13945 if (ReductionType.isNull()) 13946 return QualType(); 13947 13948 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 13949 // A type name in a declare reduction directive cannot be a function type, an 13950 // array type, a reference type, or a type qualified with const, volatile or 13951 // restrict. 13952 if (ReductionType.hasQualifiers()) { 13953 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 13954 return QualType(); 13955 } 13956 13957 if (ReductionType->isFunctionType()) { 13958 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 13959 return QualType(); 13960 } 13961 if (ReductionType->isReferenceType()) { 13962 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 13963 return QualType(); 13964 } 13965 if (ReductionType->isArrayType()) { 13966 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 13967 return QualType(); 13968 } 13969 return ReductionType; 13970 } 13971 13972 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 13973 Scope *S, DeclContext *DC, DeclarationName Name, 13974 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 13975 AccessSpecifier AS, Decl *PrevDeclInScope) { 13976 SmallVector<Decl *, 8> Decls; 13977 Decls.reserve(ReductionTypes.size()); 13978 13979 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 13980 forRedeclarationInCurContext()); 13981 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 13982 // A reduction-identifier may not be re-declared in the current scope for the 13983 // same type or for a type that is compatible according to the base language 13984 // rules. 13985 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 13986 OMPDeclareReductionDecl *PrevDRD = nullptr; 13987 bool InCompoundScope = true; 13988 if (S != nullptr) { 13989 // Find previous declaration with the same name not referenced in other 13990 // declarations. 13991 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 13992 InCompoundScope = 13993 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 13994 LookupName(Lookup, S); 13995 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 13996 /*AllowInlineNamespace=*/false); 13997 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 13998 LookupResult::Filter Filter = Lookup.makeFilter(); 13999 while (Filter.hasNext()) { 14000 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 14001 if (InCompoundScope) { 14002 auto I = UsedAsPrevious.find(PrevDecl); 14003 if (I == UsedAsPrevious.end()) 14004 UsedAsPrevious[PrevDecl] = false; 14005 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 14006 UsedAsPrevious[D] = true; 14007 } 14008 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 14009 PrevDecl->getLocation(); 14010 } 14011 Filter.done(); 14012 if (InCompoundScope) { 14013 for (const auto &PrevData : UsedAsPrevious) { 14014 if (!PrevData.second) { 14015 PrevDRD = PrevData.first; 14016 break; 14017 } 14018 } 14019 } 14020 } else if (PrevDeclInScope != nullptr) { 14021 auto *PrevDRDInScope = PrevDRD = 14022 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 14023 do { 14024 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 14025 PrevDRDInScope->getLocation(); 14026 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 14027 } while (PrevDRDInScope != nullptr); 14028 } 14029 for (const auto &TyData : ReductionTypes) { 14030 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 14031 bool Invalid = false; 14032 if (I != PreviousRedeclTypes.end()) { 14033 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 14034 << TyData.first; 14035 Diag(I->second, diag::note_previous_definition); 14036 Invalid = true; 14037 } 14038 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 14039 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 14040 Name, TyData.first, PrevDRD); 14041 DC->addDecl(DRD); 14042 DRD->setAccess(AS); 14043 Decls.push_back(DRD); 14044 if (Invalid) 14045 DRD->setInvalidDecl(); 14046 else 14047 PrevDRD = DRD; 14048 } 14049 14050 return DeclGroupPtrTy::make( 14051 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 14052 } 14053 14054 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 14055 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14056 14057 // Enter new function scope. 14058 PushFunctionScope(); 14059 setFunctionHasBranchProtectedScope(); 14060 getCurFunction()->setHasOMPDeclareReductionCombiner(); 14061 14062 if (S != nullptr) 14063 PushDeclContext(S, DRD); 14064 else 14065 CurContext = DRD; 14066 14067 PushExpressionEvaluationContext( 14068 ExpressionEvaluationContext::PotentiallyEvaluated); 14069 14070 QualType ReductionType = DRD->getType(); 14071 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 14072 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 14073 // uses semantics of argument handles by value, but it should be passed by 14074 // reference. C lang does not support references, so pass all parameters as 14075 // pointers. 14076 // Create 'T omp_in;' variable. 14077 VarDecl *OmpInParm = 14078 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 14079 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 14080 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 14081 // uses semantics of argument handles by value, but it should be passed by 14082 // reference. C lang does not support references, so pass all parameters as 14083 // pointers. 14084 // Create 'T omp_out;' variable. 14085 VarDecl *OmpOutParm = 14086 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 14087 if (S != nullptr) { 14088 PushOnScopeChains(OmpInParm, S); 14089 PushOnScopeChains(OmpOutParm, S); 14090 } else { 14091 DRD->addDecl(OmpInParm); 14092 DRD->addDecl(OmpOutParm); 14093 } 14094 Expr *InE = 14095 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 14096 Expr *OutE = 14097 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 14098 DRD->setCombinerData(InE, OutE); 14099 } 14100 14101 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 14102 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14103 DiscardCleanupsInEvaluationContext(); 14104 PopExpressionEvaluationContext(); 14105 14106 PopDeclContext(); 14107 PopFunctionScopeInfo(); 14108 14109 if (Combiner != nullptr) 14110 DRD->setCombiner(Combiner); 14111 else 14112 DRD->setInvalidDecl(); 14113 } 14114 14115 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 14116 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14117 14118 // Enter new function scope. 14119 PushFunctionScope(); 14120 setFunctionHasBranchProtectedScope(); 14121 14122 if (S != nullptr) 14123 PushDeclContext(S, DRD); 14124 else 14125 CurContext = DRD; 14126 14127 PushExpressionEvaluationContext( 14128 ExpressionEvaluationContext::PotentiallyEvaluated); 14129 14130 QualType ReductionType = DRD->getType(); 14131 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 14132 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 14133 // uses semantics of argument handles by value, but it should be passed by 14134 // reference. C lang does not support references, so pass all parameters as 14135 // pointers. 14136 // Create 'T omp_priv;' variable. 14137 VarDecl *OmpPrivParm = 14138 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 14139 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 14140 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 14141 // uses semantics of argument handles by value, but it should be passed by 14142 // reference. C lang does not support references, so pass all parameters as 14143 // pointers. 14144 // Create 'T omp_orig;' variable. 14145 VarDecl *OmpOrigParm = 14146 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 14147 if (S != nullptr) { 14148 PushOnScopeChains(OmpPrivParm, S); 14149 PushOnScopeChains(OmpOrigParm, S); 14150 } else { 14151 DRD->addDecl(OmpPrivParm); 14152 DRD->addDecl(OmpOrigParm); 14153 } 14154 Expr *OrigE = 14155 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 14156 Expr *PrivE = 14157 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 14158 DRD->setInitializerData(OrigE, PrivE); 14159 return OmpPrivParm; 14160 } 14161 14162 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 14163 VarDecl *OmpPrivParm) { 14164 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14165 DiscardCleanupsInEvaluationContext(); 14166 PopExpressionEvaluationContext(); 14167 14168 PopDeclContext(); 14169 PopFunctionScopeInfo(); 14170 14171 if (Initializer != nullptr) { 14172 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 14173 } else if (OmpPrivParm->hasInit()) { 14174 DRD->setInitializer(OmpPrivParm->getInit(), 14175 OmpPrivParm->isDirectInit() 14176 ? OMPDeclareReductionDecl::DirectInit 14177 : OMPDeclareReductionDecl::CopyInit); 14178 } else { 14179 DRD->setInvalidDecl(); 14180 } 14181 } 14182 14183 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 14184 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 14185 for (Decl *D : DeclReductions.get()) { 14186 if (IsValid) { 14187 if (S) 14188 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 14189 /*AddToContext=*/false); 14190 } else { 14191 D->setInvalidDecl(); 14192 } 14193 } 14194 return DeclReductions; 14195 } 14196 14197 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 14198 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 14199 QualType T = TInfo->getType(); 14200 if (D.isInvalidType()) 14201 return true; 14202 14203 if (getLangOpts().CPlusPlus) { 14204 // Check that there are no default arguments (C++ only). 14205 CheckExtraCXXDefaultArguments(D); 14206 } 14207 14208 return CreateParsedType(T, TInfo); 14209 } 14210 14211 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 14212 TypeResult ParsedType) { 14213 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 14214 14215 QualType MapperType = GetTypeFromParser(ParsedType.get()); 14216 assert(!MapperType.isNull() && "Expect valid mapper type"); 14217 14218 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 14219 // The type must be of struct, union or class type in C and C++ 14220 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 14221 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 14222 return QualType(); 14223 } 14224 return MapperType; 14225 } 14226 14227 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart( 14228 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 14229 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 14230 Decl *PrevDeclInScope) { 14231 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 14232 forRedeclarationInCurContext()); 14233 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 14234 // A mapper-identifier may not be redeclared in the current scope for the 14235 // same type or for a type that is compatible according to the base language 14236 // rules. 14237 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 14238 OMPDeclareMapperDecl *PrevDMD = nullptr; 14239 bool InCompoundScope = true; 14240 if (S != nullptr) { 14241 // Find previous declaration with the same name not referenced in other 14242 // declarations. 14243 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 14244 InCompoundScope = 14245 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 14246 LookupName(Lookup, S); 14247 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 14248 /*AllowInlineNamespace=*/false); 14249 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 14250 LookupResult::Filter Filter = Lookup.makeFilter(); 14251 while (Filter.hasNext()) { 14252 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 14253 if (InCompoundScope) { 14254 auto I = UsedAsPrevious.find(PrevDecl); 14255 if (I == UsedAsPrevious.end()) 14256 UsedAsPrevious[PrevDecl] = false; 14257 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 14258 UsedAsPrevious[D] = true; 14259 } 14260 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 14261 PrevDecl->getLocation(); 14262 } 14263 Filter.done(); 14264 if (InCompoundScope) { 14265 for (const auto &PrevData : UsedAsPrevious) { 14266 if (!PrevData.second) { 14267 PrevDMD = PrevData.first; 14268 break; 14269 } 14270 } 14271 } 14272 } else if (PrevDeclInScope) { 14273 auto *PrevDMDInScope = PrevDMD = 14274 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 14275 do { 14276 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 14277 PrevDMDInScope->getLocation(); 14278 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 14279 } while (PrevDMDInScope != nullptr); 14280 } 14281 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 14282 bool Invalid = false; 14283 if (I != PreviousRedeclTypes.end()) { 14284 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 14285 << MapperType << Name; 14286 Diag(I->second, diag::note_previous_definition); 14287 Invalid = true; 14288 } 14289 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 14290 MapperType, VN, PrevDMD); 14291 DC->addDecl(DMD); 14292 DMD->setAccess(AS); 14293 if (Invalid) 14294 DMD->setInvalidDecl(); 14295 14296 // Enter new function scope. 14297 PushFunctionScope(); 14298 setFunctionHasBranchProtectedScope(); 14299 14300 CurContext = DMD; 14301 14302 return DMD; 14303 } 14304 14305 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD, 14306 Scope *S, 14307 QualType MapperType, 14308 SourceLocation StartLoc, 14309 DeclarationName VN) { 14310 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString()); 14311 if (S) 14312 PushOnScopeChains(VD, S); 14313 else 14314 DMD->addDecl(VD); 14315 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 14316 DMD->setMapperVarRef(MapperVarRefExpr); 14317 } 14318 14319 Sema::DeclGroupPtrTy 14320 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S, 14321 ArrayRef<OMPClause *> ClauseList) { 14322 PopDeclContext(); 14323 PopFunctionScopeInfo(); 14324 14325 if (D) { 14326 if (S) 14327 PushOnScopeChains(D, S, /*AddToContext=*/false); 14328 D->CreateClauses(Context, ClauseList); 14329 } 14330 14331 return DeclGroupPtrTy::make(DeclGroupRef(D)); 14332 } 14333 14334 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 14335 SourceLocation StartLoc, 14336 SourceLocation LParenLoc, 14337 SourceLocation EndLoc) { 14338 Expr *ValExpr = NumTeams; 14339 Stmt *HelperValStmt = nullptr; 14340 14341 // OpenMP [teams Constrcut, Restrictions] 14342 // The num_teams expression must evaluate to a positive integer value. 14343 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 14344 /*StrictlyPositive=*/true)) 14345 return nullptr; 14346 14347 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14348 OpenMPDirectiveKind CaptureRegion = 14349 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 14350 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14351 ValExpr = MakeFullExpr(ValExpr).get(); 14352 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14353 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14354 HelperValStmt = buildPreInits(Context, Captures); 14355 } 14356 14357 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 14358 StartLoc, LParenLoc, EndLoc); 14359 } 14360 14361 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 14362 SourceLocation StartLoc, 14363 SourceLocation LParenLoc, 14364 SourceLocation EndLoc) { 14365 Expr *ValExpr = ThreadLimit; 14366 Stmt *HelperValStmt = nullptr; 14367 14368 // OpenMP [teams Constrcut, Restrictions] 14369 // The thread_limit expression must evaluate to a positive integer value. 14370 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 14371 /*StrictlyPositive=*/true)) 14372 return nullptr; 14373 14374 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14375 OpenMPDirectiveKind CaptureRegion = 14376 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 14377 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14378 ValExpr = MakeFullExpr(ValExpr).get(); 14379 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14380 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14381 HelperValStmt = buildPreInits(Context, Captures); 14382 } 14383 14384 return new (Context) OMPThreadLimitClause( 14385 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14386 } 14387 14388 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 14389 SourceLocation StartLoc, 14390 SourceLocation LParenLoc, 14391 SourceLocation EndLoc) { 14392 Expr *ValExpr = Priority; 14393 14394 // OpenMP [2.9.1, task Constrcut] 14395 // The priority-value is a non-negative numerical scalar expression. 14396 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 14397 /*StrictlyPositive=*/false)) 14398 return nullptr; 14399 14400 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14401 } 14402 14403 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 14404 SourceLocation StartLoc, 14405 SourceLocation LParenLoc, 14406 SourceLocation EndLoc) { 14407 Expr *ValExpr = Grainsize; 14408 14409 // OpenMP [2.9.2, taskloop Constrcut] 14410 // The parameter of the grainsize clause must be a positive integer 14411 // expression. 14412 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 14413 /*StrictlyPositive=*/true)) 14414 return nullptr; 14415 14416 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14417 } 14418 14419 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 14420 SourceLocation StartLoc, 14421 SourceLocation LParenLoc, 14422 SourceLocation EndLoc) { 14423 Expr *ValExpr = NumTasks; 14424 14425 // OpenMP [2.9.2, taskloop Constrcut] 14426 // The parameter of the num_tasks clause must be a positive integer 14427 // expression. 14428 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 14429 /*StrictlyPositive=*/true)) 14430 return nullptr; 14431 14432 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14433 } 14434 14435 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 14436 SourceLocation LParenLoc, 14437 SourceLocation EndLoc) { 14438 // OpenMP [2.13.2, critical construct, Description] 14439 // ... where hint-expression is an integer constant expression that evaluates 14440 // to a valid lock hint. 14441 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 14442 if (HintExpr.isInvalid()) 14443 return nullptr; 14444 return new (Context) 14445 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 14446 } 14447 14448 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 14449 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14450 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 14451 SourceLocation EndLoc) { 14452 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 14453 std::string Values; 14454 Values += "'"; 14455 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 14456 Values += "'"; 14457 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14458 << Values << getOpenMPClauseName(OMPC_dist_schedule); 14459 return nullptr; 14460 } 14461 Expr *ValExpr = ChunkSize; 14462 Stmt *HelperValStmt = nullptr; 14463 if (ChunkSize) { 14464 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 14465 !ChunkSize->isInstantiationDependent() && 14466 !ChunkSize->containsUnexpandedParameterPack()) { 14467 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 14468 ExprResult Val = 14469 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 14470 if (Val.isInvalid()) 14471 return nullptr; 14472 14473 ValExpr = Val.get(); 14474 14475 // OpenMP [2.7.1, Restrictions] 14476 // chunk_size must be a loop invariant integer expression with a positive 14477 // value. 14478 llvm::APSInt Result; 14479 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 14480 if (Result.isSigned() && !Result.isStrictlyPositive()) { 14481 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 14482 << "dist_schedule" << ChunkSize->getSourceRange(); 14483 return nullptr; 14484 } 14485 } else if (getOpenMPCaptureRegionForClause( 14486 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 14487 OMPD_unknown && 14488 !CurContext->isDependentContext()) { 14489 ValExpr = MakeFullExpr(ValExpr).get(); 14490 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14491 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14492 HelperValStmt = buildPreInits(Context, Captures); 14493 } 14494 } 14495 } 14496 14497 return new (Context) 14498 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 14499 Kind, ValExpr, HelperValStmt); 14500 } 14501 14502 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 14503 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 14504 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 14505 SourceLocation KindLoc, SourceLocation EndLoc) { 14506 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 14507 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 14508 std::string Value; 14509 SourceLocation Loc; 14510 Value += "'"; 14511 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 14512 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14513 OMPC_DEFAULTMAP_MODIFIER_tofrom); 14514 Loc = MLoc; 14515 } else { 14516 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14517 OMPC_DEFAULTMAP_scalar); 14518 Loc = KindLoc; 14519 } 14520 Value += "'"; 14521 Diag(Loc, diag::err_omp_unexpected_clause_value) 14522 << Value << getOpenMPClauseName(OMPC_defaultmap); 14523 return nullptr; 14524 } 14525 DSAStack->setDefaultDMAToFromScalar(StartLoc); 14526 14527 return new (Context) 14528 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 14529 } 14530 14531 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 14532 DeclContext *CurLexicalContext = getCurLexicalContext(); 14533 if (!CurLexicalContext->isFileContext() && 14534 !CurLexicalContext->isExternCContext() && 14535 !CurLexicalContext->isExternCXXContext() && 14536 !isa<CXXRecordDecl>(CurLexicalContext) && 14537 !isa<ClassTemplateDecl>(CurLexicalContext) && 14538 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 14539 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 14540 Diag(Loc, diag::err_omp_region_not_file_context); 14541 return false; 14542 } 14543 ++DeclareTargetNestingLevel; 14544 return true; 14545 } 14546 14547 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 14548 assert(DeclareTargetNestingLevel > 0 && 14549 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 14550 --DeclareTargetNestingLevel; 14551 } 14552 14553 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 14554 CXXScopeSpec &ScopeSpec, 14555 const DeclarationNameInfo &Id, 14556 OMPDeclareTargetDeclAttr::MapTypeTy MT, 14557 NamedDeclSetType &SameDirectiveDecls) { 14558 LookupResult Lookup(*this, Id, LookupOrdinaryName); 14559 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 14560 14561 if (Lookup.isAmbiguous()) 14562 return; 14563 Lookup.suppressDiagnostics(); 14564 14565 if (!Lookup.isSingleResult()) { 14566 VarOrFuncDeclFilterCCC CCC(*this); 14567 if (TypoCorrection Corrected = 14568 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 14569 CTK_ErrorRecovery)) { 14570 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 14571 << Id.getName()); 14572 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 14573 return; 14574 } 14575 14576 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 14577 return; 14578 } 14579 14580 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 14581 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 14582 isa<FunctionTemplateDecl>(ND)) { 14583 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 14584 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 14585 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14586 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 14587 cast<ValueDecl>(ND)); 14588 if (!Res) { 14589 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 14590 ND->addAttr(A); 14591 if (ASTMutationListener *ML = Context.getASTMutationListener()) 14592 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 14593 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 14594 } else if (*Res != MT) { 14595 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 14596 << Id.getName(); 14597 } 14598 } else { 14599 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 14600 } 14601 } 14602 14603 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 14604 Sema &SemaRef, Decl *D) { 14605 if (!D || !isa<VarDecl>(D)) 14606 return; 14607 auto *VD = cast<VarDecl>(D); 14608 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 14609 return; 14610 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 14611 SemaRef.Diag(SL, diag::note_used_here) << SR; 14612 } 14613 14614 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 14615 Sema &SemaRef, DSAStackTy *Stack, 14616 ValueDecl *VD) { 14617 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 14618 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 14619 /*FullCheck=*/false); 14620 } 14621 14622 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 14623 SourceLocation IdLoc) { 14624 if (!D || D->isInvalidDecl()) 14625 return; 14626 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 14627 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 14628 if (auto *VD = dyn_cast<VarDecl>(D)) { 14629 // Only global variables can be marked as declare target. 14630 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 14631 !VD->isStaticDataMember()) 14632 return; 14633 // 2.10.6: threadprivate variable cannot appear in a declare target 14634 // directive. 14635 if (DSAStack->isThreadPrivate(VD)) { 14636 Diag(SL, diag::err_omp_threadprivate_in_target); 14637 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 14638 return; 14639 } 14640 } 14641 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 14642 D = FTD->getTemplatedDecl(); 14643 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 14644 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14645 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 14646 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 14647 assert(IdLoc.isValid() && "Source location is expected"); 14648 Diag(IdLoc, diag::err_omp_function_in_link_clause); 14649 Diag(FD->getLocation(), diag::note_defined_here) << FD; 14650 return; 14651 } 14652 } 14653 if (auto *VD = dyn_cast<ValueDecl>(D)) { 14654 // Problem if any with var declared with incomplete type will be reported 14655 // as normal, so no need to check it here. 14656 if ((E || !VD->getType()->isIncompleteType()) && 14657 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 14658 return; 14659 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 14660 // Checking declaration inside declare target region. 14661 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 14662 isa<FunctionTemplateDecl>(D)) { 14663 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 14664 Context, OMPDeclareTargetDeclAttr::MT_To); 14665 D->addAttr(A); 14666 if (ASTMutationListener *ML = Context.getASTMutationListener()) 14667 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 14668 } 14669 return; 14670 } 14671 } 14672 if (!E) 14673 return; 14674 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 14675 } 14676 14677 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 14678 CXXScopeSpec &MapperIdScopeSpec, 14679 DeclarationNameInfo &MapperId, 14680 const OMPVarListLocTy &Locs, 14681 ArrayRef<Expr *> UnresolvedMappers) { 14682 MappableVarListInfo MVLI(VarList); 14683 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 14684 MapperIdScopeSpec, MapperId, UnresolvedMappers); 14685 if (MVLI.ProcessedVarList.empty()) 14686 return nullptr; 14687 14688 return OMPToClause::Create( 14689 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 14690 MVLI.VarComponents, MVLI.UDMapperList, 14691 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 14692 } 14693 14694 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 14695 CXXScopeSpec &MapperIdScopeSpec, 14696 DeclarationNameInfo &MapperId, 14697 const OMPVarListLocTy &Locs, 14698 ArrayRef<Expr *> UnresolvedMappers) { 14699 MappableVarListInfo MVLI(VarList); 14700 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 14701 MapperIdScopeSpec, MapperId, UnresolvedMappers); 14702 if (MVLI.ProcessedVarList.empty()) 14703 return nullptr; 14704 14705 return OMPFromClause::Create( 14706 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 14707 MVLI.VarComponents, MVLI.UDMapperList, 14708 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 14709 } 14710 14711 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 14712 const OMPVarListLocTy &Locs) { 14713 MappableVarListInfo MVLI(VarList); 14714 SmallVector<Expr *, 8> PrivateCopies; 14715 SmallVector<Expr *, 8> Inits; 14716 14717 for (Expr *RefExpr : VarList) { 14718 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 14719 SourceLocation ELoc; 14720 SourceRange ERange; 14721 Expr *SimpleRefExpr = RefExpr; 14722 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14723 if (Res.second) { 14724 // It will be analyzed later. 14725 MVLI.ProcessedVarList.push_back(RefExpr); 14726 PrivateCopies.push_back(nullptr); 14727 Inits.push_back(nullptr); 14728 } 14729 ValueDecl *D = Res.first; 14730 if (!D) 14731 continue; 14732 14733 QualType Type = D->getType(); 14734 Type = Type.getNonReferenceType().getUnqualifiedType(); 14735 14736 auto *VD = dyn_cast<VarDecl>(D); 14737 14738 // Item should be a pointer or reference to pointer. 14739 if (!Type->isPointerType()) { 14740 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 14741 << 0 << RefExpr->getSourceRange(); 14742 continue; 14743 } 14744 14745 // Build the private variable and the expression that refers to it. 14746 auto VDPrivate = 14747 buildVarDecl(*this, ELoc, Type, D->getName(), 14748 D->hasAttrs() ? &D->getAttrs() : nullptr, 14749 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14750 if (VDPrivate->isInvalidDecl()) 14751 continue; 14752 14753 CurContext->addDecl(VDPrivate); 14754 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14755 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 14756 14757 // Add temporary variable to initialize the private copy of the pointer. 14758 VarDecl *VDInit = 14759 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 14760 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 14761 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 14762 AddInitializerToDecl(VDPrivate, 14763 DefaultLvalueConversion(VDInitRefExpr).get(), 14764 /*DirectInit=*/false); 14765 14766 // If required, build a capture to implement the privatization initialized 14767 // with the current list item value. 14768 DeclRefExpr *Ref = nullptr; 14769 if (!VD) 14770 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14771 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 14772 PrivateCopies.push_back(VDPrivateRefExpr); 14773 Inits.push_back(VDInitRefExpr); 14774 14775 // We need to add a data sharing attribute for this variable to make sure it 14776 // is correctly captured. A variable that shows up in a use_device_ptr has 14777 // similar properties of a first private variable. 14778 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 14779 14780 // Create a mappable component for the list item. List items in this clause 14781 // only need a component. 14782 MVLI.VarBaseDeclarations.push_back(D); 14783 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14784 MVLI.VarComponents.back().push_back( 14785 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 14786 } 14787 14788 if (MVLI.ProcessedVarList.empty()) 14789 return nullptr; 14790 14791 return OMPUseDevicePtrClause::Create( 14792 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 14793 MVLI.VarBaseDeclarations, MVLI.VarComponents); 14794 } 14795 14796 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 14797 const OMPVarListLocTy &Locs) { 14798 MappableVarListInfo MVLI(VarList); 14799 for (Expr *RefExpr : VarList) { 14800 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 14801 SourceLocation ELoc; 14802 SourceRange ERange; 14803 Expr *SimpleRefExpr = RefExpr; 14804 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14805 if (Res.second) { 14806 // It will be analyzed later. 14807 MVLI.ProcessedVarList.push_back(RefExpr); 14808 } 14809 ValueDecl *D = Res.first; 14810 if (!D) 14811 continue; 14812 14813 QualType Type = D->getType(); 14814 // item should be a pointer or array or reference to pointer or array 14815 if (!Type.getNonReferenceType()->isPointerType() && 14816 !Type.getNonReferenceType()->isArrayType()) { 14817 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 14818 << 0 << RefExpr->getSourceRange(); 14819 continue; 14820 } 14821 14822 // Check if the declaration in the clause does not show up in any data 14823 // sharing attribute. 14824 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14825 if (isOpenMPPrivate(DVar.CKind)) { 14826 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14827 << getOpenMPClauseName(DVar.CKind) 14828 << getOpenMPClauseName(OMPC_is_device_ptr) 14829 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14830 reportOriginalDsa(*this, DSAStack, D, DVar); 14831 continue; 14832 } 14833 14834 const Expr *ConflictExpr; 14835 if (DSAStack->checkMappableExprComponentListsForDecl( 14836 D, /*CurrentRegionOnly=*/true, 14837 [&ConflictExpr]( 14838 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 14839 OpenMPClauseKind) -> bool { 14840 ConflictExpr = R.front().getAssociatedExpression(); 14841 return true; 14842 })) { 14843 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 14844 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 14845 << ConflictExpr->getSourceRange(); 14846 continue; 14847 } 14848 14849 // Store the components in the stack so that they can be used to check 14850 // against other clauses later on. 14851 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 14852 DSAStack->addMappableExpressionComponents( 14853 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 14854 14855 // Record the expression we've just processed. 14856 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 14857 14858 // Create a mappable component for the list item. List items in this clause 14859 // only need a component. We use a null declaration to signal fields in 14860 // 'this'. 14861 assert((isa<DeclRefExpr>(SimpleRefExpr) || 14862 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 14863 "Unexpected device pointer expression!"); 14864 MVLI.VarBaseDeclarations.push_back( 14865 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 14866 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14867 MVLI.VarComponents.back().push_back(MC); 14868 } 14869 14870 if (MVLI.ProcessedVarList.empty()) 14871 return nullptr; 14872 14873 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 14874 MVLI.VarBaseDeclarations, 14875 MVLI.VarComponents); 14876 } 14877 14878 OMPClause *Sema::ActOnOpenMPAllocateClause( 14879 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 14880 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14881 if (Allocator) { 14882 // OpenMP [2.11.4 allocate Clause, Description] 14883 // allocator is an expression of omp_allocator_handle_t type. 14884 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 14885 return nullptr; 14886 14887 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 14888 if (AllocatorRes.isInvalid()) 14889 return nullptr; 14890 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 14891 DSAStack->getOMPAllocatorHandleT(), 14892 Sema::AA_Initializing, 14893 /*AllowExplicit=*/true); 14894 if (AllocatorRes.isInvalid()) 14895 return nullptr; 14896 Allocator = AllocatorRes.get(); 14897 } else { 14898 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 14899 // allocate clauses that appear on a target construct or on constructs in a 14900 // target region must specify an allocator expression unless a requires 14901 // directive with the dynamic_allocators clause is present in the same 14902 // compilation unit. 14903 if (LangOpts.OpenMPIsDevice && 14904 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 14905 targetDiag(StartLoc, diag::err_expected_allocator_expression); 14906 } 14907 // Analyze and build list of variables. 14908 SmallVector<Expr *, 8> Vars; 14909 for (Expr *RefExpr : VarList) { 14910 assert(RefExpr && "NULL expr in OpenMP private clause."); 14911 SourceLocation ELoc; 14912 SourceRange ERange; 14913 Expr *SimpleRefExpr = RefExpr; 14914 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14915 if (Res.second) { 14916 // It will be analyzed later. 14917 Vars.push_back(RefExpr); 14918 } 14919 ValueDecl *D = Res.first; 14920 if (!D) 14921 continue; 14922 14923 auto *VD = dyn_cast<VarDecl>(D); 14924 DeclRefExpr *Ref = nullptr; 14925 if (!VD && !CurContext->isDependentContext()) 14926 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 14927 Vars.push_back((VD || CurContext->isDependentContext()) 14928 ? RefExpr->IgnoreParens() 14929 : Ref); 14930 } 14931 14932 if (Vars.empty()) 14933 return nullptr; 14934 14935 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 14936 ColonLoc, EndLoc, Vars); 14937 } 14938