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 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 151 Scope *CurScope, SourceLocation Loc) 152 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 153 ConstructLoc(Loc) {} 154 SharingMapTy() = default; 155 }; 156 157 using StackTy = SmallVector<SharingMapTy, 4>; 158 159 /// Stack of used declaration and their data-sharing attributes. 160 DeclSAMapTy Threadprivates; 161 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 162 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 163 /// true, if check for DSA must be from parent directive, false, if 164 /// from current directive. 165 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 166 Sema &SemaRef; 167 bool ForceCapturing = false; 168 /// true if all the vaiables in the target executable directives must be 169 /// captured by reference. 170 bool ForceCaptureByReferenceInTargetExecutable = false; 171 CriticalsWithHintsTy Criticals; 172 173 using iterator = StackTy::const_reverse_iterator; 174 175 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const; 176 177 /// Checks if the variable is a local for OpenMP region. 178 bool isOpenMPLocal(VarDecl *D, iterator Iter) const; 179 180 bool isStackEmpty() const { 181 return Stack.empty() || 182 Stack.back().second != CurrentNonCapturingFunctionScope || 183 Stack.back().first.empty(); 184 } 185 186 /// Vector of previously declared requires directives 187 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 188 189 public: 190 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 191 192 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 193 OpenMPClauseKind getClauseParsingMode() const { 194 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 195 return ClauseKindMode; 196 } 197 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 198 199 bool isForceVarCapturing() const { return ForceCapturing; } 200 void setForceVarCapturing(bool V) { ForceCapturing = V; } 201 202 void setForceCaptureByReferenceInTargetExecutable(bool V) { 203 ForceCaptureByReferenceInTargetExecutable = V; 204 } 205 bool isForceCaptureByReferenceInTargetExecutable() const { 206 return ForceCaptureByReferenceInTargetExecutable; 207 } 208 209 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 210 Scope *CurScope, SourceLocation Loc) { 211 if (Stack.empty() || 212 Stack.back().second != CurrentNonCapturingFunctionScope) 213 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 214 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 215 Stack.back().first.back().DefaultAttrLoc = Loc; 216 } 217 218 void pop() { 219 assert(!Stack.back().first.empty() && 220 "Data-sharing attributes stack is empty!"); 221 Stack.back().first.pop_back(); 222 } 223 224 /// Marks that we're started loop parsing. 225 void loopInit() { 226 assert(isOpenMPLoopDirective(getCurrentDirective()) && 227 "Expected loop-based directive."); 228 Stack.back().first.back().LoopStart = true; 229 } 230 /// Start capturing of the variables in the loop context. 231 void loopStart() { 232 assert(isOpenMPLoopDirective(getCurrentDirective()) && 233 "Expected loop-based directive."); 234 Stack.back().first.back().LoopStart = false; 235 } 236 /// true, if variables are captured, false otherwise. 237 bool isLoopStarted() const { 238 assert(isOpenMPLoopDirective(getCurrentDirective()) && 239 "Expected loop-based directive."); 240 return !Stack.back().first.back().LoopStart; 241 } 242 /// Marks (or clears) declaration as possibly loop counter. 243 void resetPossibleLoopCounter(const Decl *D = nullptr) { 244 Stack.back().first.back().PossiblyLoopCounter = 245 D ? D->getCanonicalDecl() : D; 246 } 247 /// Gets the possible loop counter decl. 248 const Decl *getPossiblyLoopCunter() const { 249 return Stack.back().first.back().PossiblyLoopCounter; 250 } 251 /// Start new OpenMP region stack in new non-capturing function. 252 void pushFunction() { 253 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 254 assert(!isa<CapturingScopeInfo>(CurFnScope)); 255 CurrentNonCapturingFunctionScope = CurFnScope; 256 } 257 /// Pop region stack for non-capturing function. 258 void popFunction(const FunctionScopeInfo *OldFSI) { 259 if (!Stack.empty() && Stack.back().second == OldFSI) { 260 assert(Stack.back().first.empty()); 261 Stack.pop_back(); 262 } 263 CurrentNonCapturingFunctionScope = nullptr; 264 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 265 if (!isa<CapturingScopeInfo>(FSI)) { 266 CurrentNonCapturingFunctionScope = FSI; 267 break; 268 } 269 } 270 } 271 272 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 273 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 274 } 275 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 276 getCriticalWithHint(const DeclarationNameInfo &Name) const { 277 auto I = Criticals.find(Name.getAsString()); 278 if (I != Criticals.end()) 279 return I->second; 280 return std::make_pair(nullptr, llvm::APSInt()); 281 } 282 /// If 'aligned' declaration for given variable \a D was not seen yet, 283 /// add it and return NULL; otherwise return previous occurrence's expression 284 /// for diagnostics. 285 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 286 287 /// Register specified variable as loop control variable. 288 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 289 /// Check if the specified variable is a loop control variable for 290 /// current region. 291 /// \return The index of the loop control variable in the list of associated 292 /// for-loops (from outer to inner). 293 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 294 /// Check if the specified variable is a loop control variable for 295 /// parent region. 296 /// \return The index of the loop control variable in the list of associated 297 /// for-loops (from outer to inner). 298 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 299 /// Get the loop control variable for the I-th loop (or nullptr) in 300 /// parent directive. 301 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 302 303 /// Adds explicit data sharing attribute to the specified declaration. 304 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 305 DeclRefExpr *PrivateCopy = nullptr); 306 307 /// Adds additional information for the reduction items with the reduction id 308 /// represented as an operator. 309 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 310 BinaryOperatorKind BOK); 311 /// Adds additional information for the reduction items with the reduction id 312 /// represented as reduction identifier. 313 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 314 const Expr *ReductionRef); 315 /// Returns the location and reduction operation from the innermost parent 316 /// region for the given \p D. 317 const DSAVarData 318 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 319 BinaryOperatorKind &BOK, 320 Expr *&TaskgroupDescriptor) const; 321 /// Returns the location and reduction operation from the innermost parent 322 /// region for the given \p D. 323 const DSAVarData 324 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 325 const Expr *&ReductionRef, 326 Expr *&TaskgroupDescriptor) const; 327 /// Return reduction reference expression for the current taskgroup. 328 Expr *getTaskgroupReductionRef() const { 329 assert(Stack.back().first.back().Directive == OMPD_taskgroup && 330 "taskgroup reference expression requested for non taskgroup " 331 "directive."); 332 return Stack.back().first.back().TaskgroupReductionRef; 333 } 334 /// Checks if the given \p VD declaration is actually a taskgroup reduction 335 /// descriptor variable at the \p Level of OpenMP regions. 336 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 337 return Stack.back().first[Level].TaskgroupReductionRef && 338 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef) 339 ->getDecl() == VD; 340 } 341 342 /// Returns data sharing attributes from top of the stack for the 343 /// specified declaration. 344 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 345 /// Returns data-sharing attributes for the specified declaration. 346 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 347 /// Checks if the specified variables has data-sharing attributes which 348 /// match specified \a CPred predicate in any directive which matches \a DPred 349 /// predicate. 350 const DSAVarData 351 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 352 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 353 bool FromParent) const; 354 /// Checks if the specified variables has data-sharing attributes which 355 /// match specified \a CPred predicate in any innermost directive which 356 /// matches \a DPred predicate. 357 const DSAVarData 358 hasInnermostDSA(ValueDecl *D, 359 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 360 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 361 bool FromParent) const; 362 /// Checks if the specified variables has explicit data-sharing 363 /// attributes which match specified \a CPred predicate at the specified 364 /// OpenMP region. 365 bool hasExplicitDSA(const ValueDecl *D, 366 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 367 unsigned Level, bool NotLastprivate = false) const; 368 369 /// Returns true if the directive at level \Level matches in the 370 /// specified \a DPred predicate. 371 bool hasExplicitDirective( 372 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 373 unsigned Level) const; 374 375 /// Finds a directive which matches specified \a DPred predicate. 376 bool hasDirective( 377 const llvm::function_ref<bool( 378 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 379 DPred, 380 bool FromParent) const; 381 382 /// Returns currently analyzed directive. 383 OpenMPDirectiveKind getCurrentDirective() const { 384 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; 385 } 386 /// Returns directive kind at specified level. 387 OpenMPDirectiveKind getDirective(unsigned Level) const { 388 assert(!isStackEmpty() && "No directive at specified level."); 389 return Stack.back().first[Level].Directive; 390 } 391 /// Returns parent directive. 392 OpenMPDirectiveKind getParentDirective() const { 393 if (isStackEmpty() || Stack.back().first.size() == 1) 394 return OMPD_unknown; 395 return std::next(Stack.back().first.rbegin())->Directive; 396 } 397 398 /// Add requires decl to internal vector 399 void addRequiresDecl(OMPRequiresDecl *RD) { 400 RequiresDecls.push_back(RD); 401 } 402 403 /// Checks for a duplicate clause amongst previously declared requires 404 /// directives 405 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 406 bool IsDuplicate = false; 407 for (OMPClause *CNew : ClauseList) { 408 for (const OMPRequiresDecl *D : RequiresDecls) { 409 for (const OMPClause *CPrev : D->clauselists()) { 410 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 411 SemaRef.Diag(CNew->getBeginLoc(), 412 diag::err_omp_requires_clause_redeclaration) 413 << getOpenMPClauseName(CNew->getClauseKind()); 414 SemaRef.Diag(CPrev->getBeginLoc(), 415 diag::note_omp_requires_previous_clause) 416 << getOpenMPClauseName(CPrev->getClauseKind()); 417 IsDuplicate = true; 418 } 419 } 420 } 421 } 422 return IsDuplicate; 423 } 424 425 /// Set default data sharing attribute to none. 426 void setDefaultDSANone(SourceLocation Loc) { 427 assert(!isStackEmpty()); 428 Stack.back().first.back().DefaultAttr = DSA_none; 429 Stack.back().first.back().DefaultAttrLoc = Loc; 430 } 431 /// Set default data sharing attribute to shared. 432 void setDefaultDSAShared(SourceLocation Loc) { 433 assert(!isStackEmpty()); 434 Stack.back().first.back().DefaultAttr = DSA_shared; 435 Stack.back().first.back().DefaultAttrLoc = Loc; 436 } 437 /// Set default data mapping attribute to 'tofrom:scalar'. 438 void setDefaultDMAToFromScalar(SourceLocation Loc) { 439 assert(!isStackEmpty()); 440 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar; 441 Stack.back().first.back().DefaultMapAttrLoc = Loc; 442 } 443 444 DefaultDataSharingAttributes getDefaultDSA() const { 445 return isStackEmpty() ? DSA_unspecified 446 : Stack.back().first.back().DefaultAttr; 447 } 448 SourceLocation getDefaultDSALocation() const { 449 return isStackEmpty() ? SourceLocation() 450 : Stack.back().first.back().DefaultAttrLoc; 451 } 452 DefaultMapAttributes getDefaultDMA() const { 453 return isStackEmpty() ? DMA_unspecified 454 : Stack.back().first.back().DefaultMapAttr; 455 } 456 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const { 457 return Stack.back().first[Level].DefaultMapAttr; 458 } 459 SourceLocation getDefaultDMALocation() const { 460 return isStackEmpty() ? SourceLocation() 461 : Stack.back().first.back().DefaultMapAttrLoc; 462 } 463 464 /// Checks if the specified variable is a threadprivate. 465 bool isThreadPrivate(VarDecl *D) { 466 const DSAVarData DVar = getTopDSA(D, false); 467 return isOpenMPThreadPrivate(DVar.CKind); 468 } 469 470 /// Marks current region as ordered (it has an 'ordered' clause). 471 void setOrderedRegion(bool IsOrdered, const Expr *Param, 472 OMPOrderedClause *Clause) { 473 assert(!isStackEmpty()); 474 if (IsOrdered) 475 Stack.back().first.back().OrderedRegion.emplace(Param, Clause); 476 else 477 Stack.back().first.back().OrderedRegion.reset(); 478 } 479 /// Returns true, if region is ordered (has associated 'ordered' clause), 480 /// false - otherwise. 481 bool isOrderedRegion() const { 482 if (isStackEmpty()) 483 return false; 484 return Stack.back().first.rbegin()->OrderedRegion.hasValue(); 485 } 486 /// Returns optional parameter for the ordered region. 487 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 488 if (isStackEmpty() || 489 !Stack.back().first.rbegin()->OrderedRegion.hasValue()) 490 return std::make_pair(nullptr, nullptr); 491 return Stack.back().first.rbegin()->OrderedRegion.getValue(); 492 } 493 /// Returns true, if parent region is ordered (has associated 494 /// 'ordered' clause), false - otherwise. 495 bool isParentOrderedRegion() const { 496 if (isStackEmpty() || Stack.back().first.size() == 1) 497 return false; 498 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue(); 499 } 500 /// Returns optional parameter for the ordered region. 501 std::pair<const Expr *, OMPOrderedClause *> 502 getParentOrderedRegionParam() const { 503 if (isStackEmpty() || Stack.back().first.size() == 1 || 504 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue()) 505 return std::make_pair(nullptr, nullptr); 506 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue(); 507 } 508 /// Marks current region as nowait (it has a 'nowait' clause). 509 void setNowaitRegion(bool IsNowait = true) { 510 assert(!isStackEmpty()); 511 Stack.back().first.back().NowaitRegion = IsNowait; 512 } 513 /// Returns true, if parent region is nowait (has associated 514 /// 'nowait' clause), false - otherwise. 515 bool isParentNowaitRegion() const { 516 if (isStackEmpty() || Stack.back().first.size() == 1) 517 return false; 518 return std::next(Stack.back().first.rbegin())->NowaitRegion; 519 } 520 /// Marks parent region as cancel region. 521 void setParentCancelRegion(bool Cancel = true) { 522 if (!isStackEmpty() && Stack.back().first.size() > 1) { 523 auto &StackElemRef = *std::next(Stack.back().first.rbegin()); 524 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; 525 } 526 } 527 /// Return true if current region has inner cancel construct. 528 bool isCancelRegion() const { 529 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; 530 } 531 532 /// Set collapse value for the region. 533 void setAssociatedLoops(unsigned Val) { 534 assert(!isStackEmpty()); 535 Stack.back().first.back().AssociatedLoops = Val; 536 } 537 /// Return collapse value for region. 538 unsigned getAssociatedLoops() const { 539 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; 540 } 541 542 /// Marks current target region as one with closely nested teams 543 /// region. 544 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 545 if (!isStackEmpty() && Stack.back().first.size() > 1) { 546 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = 547 TeamsRegionLoc; 548 } 549 } 550 /// Returns true, if current region has closely nested teams region. 551 bool hasInnerTeamsRegion() const { 552 return getInnerTeamsRegionLoc().isValid(); 553 } 554 /// Returns location of the nested teams region (if any). 555 SourceLocation getInnerTeamsRegionLoc() const { 556 return isStackEmpty() ? SourceLocation() 557 : Stack.back().first.back().InnerTeamsRegionLoc; 558 } 559 560 Scope *getCurScope() const { 561 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 562 } 563 SourceLocation getConstructLoc() const { 564 return isStackEmpty() ? SourceLocation() 565 : Stack.back().first.back().ConstructLoc; 566 } 567 568 /// Do the check specified in \a Check to all component lists and return true 569 /// if any issue is found. 570 bool checkMappableExprComponentListsForDecl( 571 const ValueDecl *VD, bool CurrentRegionOnly, 572 const llvm::function_ref< 573 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 574 OpenMPClauseKind)> 575 Check) const { 576 if (isStackEmpty()) 577 return false; 578 auto SI = Stack.back().first.rbegin(); 579 auto SE = Stack.back().first.rend(); 580 581 if (SI == SE) 582 return false; 583 584 if (CurrentRegionOnly) 585 SE = std::next(SI); 586 else 587 std::advance(SI, 1); 588 589 for (; SI != SE; ++SI) { 590 auto MI = SI->MappedExprComponents.find(VD); 591 if (MI != SI->MappedExprComponents.end()) 592 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 593 MI->second.Components) 594 if (Check(L, MI->second.Kind)) 595 return true; 596 } 597 return false; 598 } 599 600 /// Do the check specified in \a Check to all component lists at a given level 601 /// and return true if any issue is found. 602 bool checkMappableExprComponentListsForDeclAtLevel( 603 const ValueDecl *VD, unsigned Level, 604 const llvm::function_ref< 605 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 606 OpenMPClauseKind)> 607 Check) const { 608 if (isStackEmpty()) 609 return false; 610 611 auto StartI = Stack.back().first.begin(); 612 auto EndI = Stack.back().first.end(); 613 if (std::distance(StartI, EndI) <= (int)Level) 614 return false; 615 std::advance(StartI, Level); 616 617 auto MI = StartI->MappedExprComponents.find(VD); 618 if (MI != StartI->MappedExprComponents.end()) 619 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 620 MI->second.Components) 621 if (Check(L, MI->second.Kind)) 622 return true; 623 return false; 624 } 625 626 /// Create a new mappable expression component list associated with a given 627 /// declaration and initialize it with the provided list of components. 628 void addMappableExpressionComponents( 629 const ValueDecl *VD, 630 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 631 OpenMPClauseKind WhereFoundClauseKind) { 632 assert(!isStackEmpty() && 633 "Not expecting to retrieve components from a empty stack!"); 634 MappedExprComponentTy &MEC = 635 Stack.back().first.back().MappedExprComponents[VD]; 636 // Create new entry and append the new components there. 637 MEC.Components.resize(MEC.Components.size() + 1); 638 MEC.Components.back().append(Components.begin(), Components.end()); 639 MEC.Kind = WhereFoundClauseKind; 640 } 641 642 unsigned getNestingLevel() const { 643 assert(!isStackEmpty()); 644 return Stack.back().first.size() - 1; 645 } 646 void addDoacrossDependClause(OMPDependClause *C, 647 const OperatorOffsetTy &OpsOffs) { 648 assert(!isStackEmpty() && Stack.back().first.size() > 1); 649 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 650 assert(isOpenMPWorksharingDirective(StackElem.Directive)); 651 StackElem.DoacrossDepends.try_emplace(C, OpsOffs); 652 } 653 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 654 getDoacrossDependClauses() const { 655 assert(!isStackEmpty()); 656 const SharingMapTy &StackElem = Stack.back().first.back(); 657 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 658 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 659 return llvm::make_range(Ref.begin(), Ref.end()); 660 } 661 return llvm::make_range(StackElem.DoacrossDepends.end(), 662 StackElem.DoacrossDepends.end()); 663 } 664 665 // Store types of classes which have been explicitly mapped 666 void addMappedClassesQualTypes(QualType QT) { 667 SharingMapTy &StackElem = Stack.back().first.back(); 668 StackElem.MappedClassesQualTypes.insert(QT); 669 } 670 671 // Return set of mapped classes types 672 bool isClassPreviouslyMapped(QualType QT) const { 673 const SharingMapTy &StackElem = Stack.back().first.back(); 674 return StackElem.MappedClassesQualTypes.count(QT) != 0; 675 } 676 677 }; 678 679 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 680 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 681 } 682 683 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 684 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown; 685 } 686 687 } // namespace 688 689 static const Expr *getExprAsWritten(const Expr *E) { 690 if (const auto *FE = dyn_cast<FullExpr>(E)) 691 E = FE->getSubExpr(); 692 693 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 694 E = MTE->GetTemporaryExpr(); 695 696 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 697 E = Binder->getSubExpr(); 698 699 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 700 E = ICE->getSubExprAsWritten(); 701 return E->IgnoreParens(); 702 } 703 704 static Expr *getExprAsWritten(Expr *E) { 705 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 706 } 707 708 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 709 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 710 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 711 D = ME->getMemberDecl(); 712 const auto *VD = dyn_cast<VarDecl>(D); 713 const auto *FD = dyn_cast<FieldDecl>(D); 714 if (VD != nullptr) { 715 VD = VD->getCanonicalDecl(); 716 D = VD; 717 } else { 718 assert(FD); 719 FD = FD->getCanonicalDecl(); 720 D = FD; 721 } 722 return D; 723 } 724 725 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 726 return const_cast<ValueDecl *>( 727 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 728 } 729 730 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter, 731 ValueDecl *D) const { 732 D = getCanonicalDecl(D); 733 auto *VD = dyn_cast<VarDecl>(D); 734 const auto *FD = dyn_cast<FieldDecl>(D); 735 DSAVarData DVar; 736 if (isStackEmpty() || Iter == Stack.back().first.rend()) { 737 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 738 // in a region but not in construct] 739 // File-scope or namespace-scope variables referenced in called routines 740 // in the region are shared unless they appear in a threadprivate 741 // directive. 742 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 743 DVar.CKind = OMPC_shared; 744 745 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 746 // in a region but not in construct] 747 // Variables with static storage duration that are declared in called 748 // routines in the region are shared. 749 if (VD && VD->hasGlobalStorage()) 750 DVar.CKind = OMPC_shared; 751 752 // Non-static data members are shared by default. 753 if (FD) 754 DVar.CKind = OMPC_shared; 755 756 return DVar; 757 } 758 759 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 760 // in a Construct, C/C++, predetermined, p.1] 761 // Variables with automatic storage duration that are declared in a scope 762 // inside the construct are private. 763 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 764 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 765 DVar.CKind = OMPC_private; 766 return DVar; 767 } 768 769 DVar.DKind = Iter->Directive; 770 // Explicitly specified attributes and local variables with predetermined 771 // attributes. 772 if (Iter->SharingMap.count(D)) { 773 const DSAInfo &Data = Iter->SharingMap.lookup(D); 774 DVar.RefExpr = Data.RefExpr.getPointer(); 775 DVar.PrivateCopy = Data.PrivateCopy; 776 DVar.CKind = Data.Attributes; 777 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 778 return DVar; 779 } 780 781 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 782 // in a Construct, C/C++, implicitly determined, p.1] 783 // In a parallel or task construct, the data-sharing attributes of these 784 // variables are determined by the default clause, if present. 785 switch (Iter->DefaultAttr) { 786 case DSA_shared: 787 DVar.CKind = OMPC_shared; 788 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 789 return DVar; 790 case DSA_none: 791 return DVar; 792 case DSA_unspecified: 793 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 794 // in a Construct, implicitly determined, p.2] 795 // In a parallel construct, if no default clause is present, these 796 // variables are shared. 797 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 798 if (isOpenMPParallelDirective(DVar.DKind) || 799 isOpenMPTeamsDirective(DVar.DKind)) { 800 DVar.CKind = OMPC_shared; 801 return DVar; 802 } 803 804 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 805 // in a Construct, implicitly determined, p.4] 806 // In a task construct, if no default clause is present, a variable that in 807 // the enclosing context is determined to be shared by all implicit tasks 808 // bound to the current team is shared. 809 if (isOpenMPTaskingDirective(DVar.DKind)) { 810 DSAVarData DVarTemp; 811 iterator I = Iter, E = Stack.back().first.rend(); 812 do { 813 ++I; 814 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 815 // Referenced in a Construct, implicitly determined, p.6] 816 // In a task construct, if no default clause is present, a variable 817 // whose data-sharing attribute is not determined by the rules above is 818 // firstprivate. 819 DVarTemp = getDSA(I, D); 820 if (DVarTemp.CKind != OMPC_shared) { 821 DVar.RefExpr = nullptr; 822 DVar.CKind = OMPC_firstprivate; 823 return DVar; 824 } 825 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 826 DVar.CKind = 827 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 828 return DVar; 829 } 830 } 831 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 832 // in a Construct, implicitly determined, p.3] 833 // For constructs other than task, if no default clause is present, these 834 // variables inherit their data-sharing attributes from the enclosing 835 // context. 836 return getDSA(++Iter, D); 837 } 838 839 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 840 const Expr *NewDE) { 841 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 842 D = getCanonicalDecl(D); 843 SharingMapTy &StackElem = Stack.back().first.back(); 844 auto It = StackElem.AlignedMap.find(D); 845 if (It == StackElem.AlignedMap.end()) { 846 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 847 StackElem.AlignedMap[D] = NewDE; 848 return nullptr; 849 } 850 assert(It->second && "Unexpected nullptr expr in the aligned map"); 851 return It->second; 852 } 853 854 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 855 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 856 D = getCanonicalDecl(D); 857 SharingMapTy &StackElem = Stack.back().first.back(); 858 StackElem.LCVMap.try_emplace( 859 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 860 } 861 862 const DSAStackTy::LCDeclInfo 863 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 864 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 865 D = getCanonicalDecl(D); 866 const SharingMapTy &StackElem = Stack.back().first.back(); 867 auto It = StackElem.LCVMap.find(D); 868 if (It != StackElem.LCVMap.end()) 869 return It->second; 870 return {0, nullptr}; 871 } 872 873 const DSAStackTy::LCDeclInfo 874 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 875 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 876 "Data-sharing attributes stack is empty"); 877 D = getCanonicalDecl(D); 878 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 879 auto It = StackElem.LCVMap.find(D); 880 if (It != StackElem.LCVMap.end()) 881 return It->second; 882 return {0, nullptr}; 883 } 884 885 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 886 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 887 "Data-sharing attributes stack is empty"); 888 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 889 if (StackElem.LCVMap.size() < I) 890 return nullptr; 891 for (const auto &Pair : StackElem.LCVMap) 892 if (Pair.second.first == I) 893 return Pair.first; 894 return nullptr; 895 } 896 897 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 898 DeclRefExpr *PrivateCopy) { 899 D = getCanonicalDecl(D); 900 if (A == OMPC_threadprivate) { 901 DSAInfo &Data = Threadprivates[D]; 902 Data.Attributes = A; 903 Data.RefExpr.setPointer(E); 904 Data.PrivateCopy = nullptr; 905 } else { 906 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 907 DSAInfo &Data = Stack.back().first.back().SharingMap[D]; 908 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 909 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 910 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 911 (isLoopControlVariable(D).first && A == OMPC_private)); 912 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 913 Data.RefExpr.setInt(/*IntVal=*/true); 914 return; 915 } 916 const bool IsLastprivate = 917 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 918 Data.Attributes = A; 919 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 920 Data.PrivateCopy = PrivateCopy; 921 if (PrivateCopy) { 922 DSAInfo &Data = 923 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; 924 Data.Attributes = A; 925 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 926 Data.PrivateCopy = nullptr; 927 } 928 } 929 } 930 931 /// Build a variable declaration for OpenMP loop iteration variable. 932 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 933 StringRef Name, const AttrVec *Attrs = nullptr, 934 DeclRefExpr *OrigRef = nullptr) { 935 DeclContext *DC = SemaRef.CurContext; 936 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 937 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 938 auto *Decl = 939 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 940 if (Attrs) { 941 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 942 I != E; ++I) 943 Decl->addAttr(*I); 944 } 945 Decl->setImplicit(); 946 if (OrigRef) { 947 Decl->addAttr( 948 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 949 } 950 return Decl; 951 } 952 953 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 954 SourceLocation Loc, 955 bool RefersToCapture = false) { 956 D->setReferenced(); 957 D->markUsed(S.Context); 958 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 959 SourceLocation(), D, RefersToCapture, Loc, Ty, 960 VK_LValue); 961 } 962 963 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 964 BinaryOperatorKind BOK) { 965 D = getCanonicalDecl(D); 966 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 967 assert( 968 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 969 "Additional reduction info may be specified only for reduction items."); 970 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 971 assert(ReductionData.ReductionRange.isInvalid() && 972 Stack.back().first.back().Directive == OMPD_taskgroup && 973 "Additional reduction info may be specified only once for reduction " 974 "items."); 975 ReductionData.set(BOK, SR); 976 Expr *&TaskgroupReductionRef = 977 Stack.back().first.back().TaskgroupReductionRef; 978 if (!TaskgroupReductionRef) { 979 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 980 SemaRef.Context.VoidPtrTy, ".task_red."); 981 TaskgroupReductionRef = 982 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 983 } 984 } 985 986 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 987 const Expr *ReductionRef) { 988 D = getCanonicalDecl(D); 989 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 990 assert( 991 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 992 "Additional reduction info may be specified only for reduction items."); 993 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 994 assert(ReductionData.ReductionRange.isInvalid() && 995 Stack.back().first.back().Directive == OMPD_taskgroup && 996 "Additional reduction info may be specified only once for reduction " 997 "items."); 998 ReductionData.set(ReductionRef, SR); 999 Expr *&TaskgroupReductionRef = 1000 Stack.back().first.back().TaskgroupReductionRef; 1001 if (!TaskgroupReductionRef) { 1002 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1003 SemaRef.Context.VoidPtrTy, ".task_red."); 1004 TaskgroupReductionRef = 1005 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1006 } 1007 } 1008 1009 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1010 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1011 Expr *&TaskgroupDescriptor) const { 1012 D = getCanonicalDecl(D); 1013 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1014 if (Stack.back().first.empty()) 1015 return DSAVarData(); 1016 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 1017 E = Stack.back().first.rend(); 1018 I != E; std::advance(I, 1)) { 1019 const DSAInfo &Data = I->SharingMap.lookup(D); 1020 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1021 continue; 1022 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1023 if (!ReductionData.ReductionOp || 1024 ReductionData.ReductionOp.is<const Expr *>()) 1025 return DSAVarData(); 1026 SR = ReductionData.ReductionRange; 1027 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1028 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1029 "expression for the descriptor is not " 1030 "set."); 1031 TaskgroupDescriptor = I->TaskgroupReductionRef; 1032 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1033 Data.PrivateCopy, I->DefaultAttrLoc); 1034 } 1035 return DSAVarData(); 1036 } 1037 1038 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1039 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1040 Expr *&TaskgroupDescriptor) const { 1041 D = getCanonicalDecl(D); 1042 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1043 if (Stack.back().first.empty()) 1044 return DSAVarData(); 1045 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 1046 E = Stack.back().first.rend(); 1047 I != E; std::advance(I, 1)) { 1048 const DSAInfo &Data = I->SharingMap.lookup(D); 1049 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1050 continue; 1051 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1052 if (!ReductionData.ReductionOp || 1053 !ReductionData.ReductionOp.is<const Expr *>()) 1054 return DSAVarData(); 1055 SR = ReductionData.ReductionRange; 1056 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1057 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1058 "expression for the descriptor is not " 1059 "set."); 1060 TaskgroupDescriptor = I->TaskgroupReductionRef; 1061 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1062 Data.PrivateCopy, I->DefaultAttrLoc); 1063 } 1064 return DSAVarData(); 1065 } 1066 1067 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const { 1068 D = D->getCanonicalDecl(); 1069 if (!isStackEmpty()) { 1070 iterator I = Iter, E = Stack.back().first.rend(); 1071 Scope *TopScope = nullptr; 1072 while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) && 1073 !isOpenMPTargetExecutionDirective(I->Directive)) 1074 ++I; 1075 if (I == E) 1076 return false; 1077 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1078 Scope *CurScope = getCurScope(); 1079 while (CurScope != TopScope && !CurScope->isDeclScope(D)) 1080 CurScope = CurScope->getParent(); 1081 return CurScope != TopScope; 1082 } 1083 return false; 1084 } 1085 1086 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1087 bool AcceptIfMutable = true, 1088 bool *IsClassType = nullptr) { 1089 ASTContext &Context = SemaRef.getASTContext(); 1090 Type = Type.getNonReferenceType().getCanonicalType(); 1091 bool IsConstant = Type.isConstant(Context); 1092 Type = Context.getBaseElementType(Type); 1093 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1094 ? Type->getAsCXXRecordDecl() 1095 : nullptr; 1096 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1097 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1098 RD = CTD->getTemplatedDecl(); 1099 if (IsClassType) 1100 *IsClassType = RD; 1101 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1102 RD->hasDefinition() && RD->hasMutableFields()); 1103 } 1104 1105 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1106 QualType Type, OpenMPClauseKind CKind, 1107 SourceLocation ELoc, 1108 bool AcceptIfMutable = true, 1109 bool ListItemNotVar = false) { 1110 ASTContext &Context = SemaRef.getASTContext(); 1111 bool IsClassType; 1112 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1113 unsigned Diag = ListItemNotVar 1114 ? diag::err_omp_const_list_item 1115 : IsClassType ? diag::err_omp_const_not_mutable_variable 1116 : diag::err_omp_const_variable; 1117 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1118 if (!ListItemNotVar && D) { 1119 const VarDecl *VD = dyn_cast<VarDecl>(D); 1120 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1121 VarDecl::DeclarationOnly; 1122 SemaRef.Diag(D->getLocation(), 1123 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1124 << D; 1125 } 1126 return true; 1127 } 1128 return false; 1129 } 1130 1131 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1132 bool FromParent) { 1133 D = getCanonicalDecl(D); 1134 DSAVarData DVar; 1135 1136 auto *VD = dyn_cast<VarDecl>(D); 1137 auto TI = Threadprivates.find(D); 1138 if (TI != Threadprivates.end()) { 1139 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1140 DVar.CKind = OMPC_threadprivate; 1141 return DVar; 1142 } 1143 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1144 DVar.RefExpr = buildDeclRefExpr( 1145 SemaRef, VD, D->getType().getNonReferenceType(), 1146 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1147 DVar.CKind = OMPC_threadprivate; 1148 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1149 return DVar; 1150 } 1151 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1152 // in a Construct, C/C++, predetermined, p.1] 1153 // Variables appearing in threadprivate directives are threadprivate. 1154 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1155 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1156 SemaRef.getLangOpts().OpenMPUseTLS && 1157 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1158 (VD && VD->getStorageClass() == SC_Register && 1159 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1160 DVar.RefExpr = buildDeclRefExpr( 1161 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1162 DVar.CKind = OMPC_threadprivate; 1163 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1164 return DVar; 1165 } 1166 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1167 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1168 !isLoopControlVariable(D).first) { 1169 iterator IterTarget = 1170 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(), 1171 [](const SharingMapTy &Data) { 1172 return isOpenMPTargetExecutionDirective(Data.Directive); 1173 }); 1174 if (IterTarget != Stack.back().first.rend()) { 1175 iterator ParentIterTarget = std::next(IterTarget, 1); 1176 for (iterator Iter = Stack.back().first.rbegin(); 1177 Iter != ParentIterTarget; std::advance(Iter, 1)) { 1178 if (isOpenMPLocal(VD, Iter)) { 1179 DVar.RefExpr = 1180 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1181 D->getLocation()); 1182 DVar.CKind = OMPC_threadprivate; 1183 return DVar; 1184 } 1185 } 1186 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) { 1187 auto DSAIter = IterTarget->SharingMap.find(D); 1188 if (DSAIter != IterTarget->SharingMap.end() && 1189 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1190 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1191 DVar.CKind = OMPC_threadprivate; 1192 return DVar; 1193 } 1194 iterator End = Stack.back().first.rend(); 1195 if (!SemaRef.isOpenMPCapturedByRef( 1196 D, std::distance(ParentIterTarget, End))) { 1197 DVar.RefExpr = 1198 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1199 IterTarget->ConstructLoc); 1200 DVar.CKind = OMPC_threadprivate; 1201 return DVar; 1202 } 1203 } 1204 } 1205 } 1206 1207 if (isStackEmpty()) 1208 // Not in OpenMP execution region and top scope was already checked. 1209 return DVar; 1210 1211 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1212 // in a Construct, C/C++, predetermined, p.4] 1213 // Static data members are shared. 1214 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1215 // in a Construct, C/C++, predetermined, p.7] 1216 // Variables with static storage duration that are declared in a scope 1217 // inside the construct are shared. 1218 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1219 if (VD && VD->isStaticDataMember()) { 1220 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 1221 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1222 return DVar; 1223 1224 DVar.CKind = OMPC_shared; 1225 return DVar; 1226 } 1227 1228 // The predetermined shared attribute for const-qualified types having no 1229 // mutable members was removed after OpenMP 3.1. 1230 if (SemaRef.LangOpts.OpenMP <= 31) { 1231 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1232 // in a Construct, C/C++, predetermined, p.6] 1233 // Variables with const qualified type having no mutable member are 1234 // shared. 1235 if (isConstNotMutableType(SemaRef, D->getType())) { 1236 // Variables with const-qualified type having no mutable member may be 1237 // listed in a firstprivate clause, even if they are static data members. 1238 DSAVarData DVarTemp = hasInnermostDSA( 1239 D, 1240 [](OpenMPClauseKind C) { 1241 return C == OMPC_firstprivate || C == OMPC_shared; 1242 }, 1243 MatchesAlways, FromParent); 1244 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1245 return DVarTemp; 1246 1247 DVar.CKind = OMPC_shared; 1248 return DVar; 1249 } 1250 } 1251 1252 // Explicitly specified attributes and local variables with predetermined 1253 // attributes. 1254 iterator I = Stack.back().first.rbegin(); 1255 iterator EndI = Stack.back().first.rend(); 1256 if (FromParent && I != EndI) 1257 std::advance(I, 1); 1258 auto It = I->SharingMap.find(D); 1259 if (It != I->SharingMap.end()) { 1260 const DSAInfo &Data = It->getSecond(); 1261 DVar.RefExpr = Data.RefExpr.getPointer(); 1262 DVar.PrivateCopy = Data.PrivateCopy; 1263 DVar.CKind = Data.Attributes; 1264 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1265 DVar.DKind = I->Directive; 1266 } 1267 1268 return DVar; 1269 } 1270 1271 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1272 bool FromParent) const { 1273 if (isStackEmpty()) { 1274 iterator I; 1275 return getDSA(I, D); 1276 } 1277 D = getCanonicalDecl(D); 1278 iterator StartI = Stack.back().first.rbegin(); 1279 iterator EndI = Stack.back().first.rend(); 1280 if (FromParent && StartI != EndI) 1281 std::advance(StartI, 1); 1282 return getDSA(StartI, D); 1283 } 1284 1285 const DSAStackTy::DSAVarData 1286 DSAStackTy::hasDSA(ValueDecl *D, 1287 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1288 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1289 bool FromParent) const { 1290 if (isStackEmpty()) 1291 return {}; 1292 D = getCanonicalDecl(D); 1293 iterator I = Stack.back().first.rbegin(); 1294 iterator EndI = Stack.back().first.rend(); 1295 if (FromParent && I != EndI) 1296 std::advance(I, 1); 1297 for (; I != EndI; std::advance(I, 1)) { 1298 if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive)) 1299 continue; 1300 iterator NewI = I; 1301 DSAVarData DVar = getDSA(NewI, D); 1302 if (I == NewI && CPred(DVar.CKind)) 1303 return DVar; 1304 } 1305 return {}; 1306 } 1307 1308 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1309 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1310 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1311 bool FromParent) const { 1312 if (isStackEmpty()) 1313 return {}; 1314 D = getCanonicalDecl(D); 1315 iterator StartI = Stack.back().first.rbegin(); 1316 iterator EndI = Stack.back().first.rend(); 1317 if (FromParent && StartI != EndI) 1318 std::advance(StartI, 1); 1319 if (StartI == EndI || !DPred(StartI->Directive)) 1320 return {}; 1321 iterator NewI = StartI; 1322 DSAVarData DVar = getDSA(NewI, D); 1323 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1324 } 1325 1326 bool DSAStackTy::hasExplicitDSA( 1327 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1328 unsigned Level, bool NotLastprivate) const { 1329 if (isStackEmpty()) 1330 return false; 1331 D = getCanonicalDecl(D); 1332 auto StartI = Stack.back().first.begin(); 1333 auto EndI = Stack.back().first.end(); 1334 if (std::distance(StartI, EndI) <= (int)Level) 1335 return false; 1336 std::advance(StartI, Level); 1337 auto I = StartI->SharingMap.find(D); 1338 if ((I != StartI->SharingMap.end()) && 1339 I->getSecond().RefExpr.getPointer() && 1340 CPred(I->getSecond().Attributes) && 1341 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1342 return true; 1343 // Check predetermined rules for the loop control variables. 1344 auto LI = StartI->LCVMap.find(D); 1345 if (LI != StartI->LCVMap.end()) 1346 return CPred(OMPC_private); 1347 return false; 1348 } 1349 1350 bool DSAStackTy::hasExplicitDirective( 1351 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1352 unsigned Level) const { 1353 if (isStackEmpty()) 1354 return false; 1355 auto StartI = Stack.back().first.begin(); 1356 auto EndI = Stack.back().first.end(); 1357 if (std::distance(StartI, EndI) <= (int)Level) 1358 return false; 1359 std::advance(StartI, Level); 1360 return DPred(StartI->Directive); 1361 } 1362 1363 bool DSAStackTy::hasDirective( 1364 const llvm::function_ref<bool(OpenMPDirectiveKind, 1365 const DeclarationNameInfo &, SourceLocation)> 1366 DPred, 1367 bool FromParent) const { 1368 // We look only in the enclosing region. 1369 if (isStackEmpty()) 1370 return false; 1371 auto StartI = std::next(Stack.back().first.rbegin()); 1372 auto EndI = Stack.back().first.rend(); 1373 if (FromParent && StartI != EndI) 1374 StartI = std::next(StartI); 1375 for (auto I = StartI, EE = EndI; I != EE; ++I) { 1376 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1377 return true; 1378 } 1379 return false; 1380 } 1381 1382 void Sema::InitDataSharingAttributesStack() { 1383 VarDataSharingAttributesStack = new DSAStackTy(*this); 1384 } 1385 1386 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1387 1388 void Sema::pushOpenMPFunctionRegion() { 1389 DSAStack->pushFunction(); 1390 } 1391 1392 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1393 DSAStack->popFunction(OldFSI); 1394 } 1395 1396 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1397 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1398 "Expected OpenMP device compilation."); 1399 return !S.isInOpenMPTargetExecutionDirective() && 1400 !S.isInOpenMPDeclareTargetContext(); 1401 } 1402 1403 /// Do we know that we will eventually codegen the given function? 1404 static bool isKnownEmitted(Sema &S, FunctionDecl *FD) { 1405 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1406 "Expected OpenMP device compilation."); 1407 // Templates are emitted when they're instantiated. 1408 if (FD->isDependentContext()) 1409 return false; 1410 1411 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1412 FD->getCanonicalDecl())) 1413 return true; 1414 1415 // Otherwise, the function is known-emitted if it's in our set of 1416 // known-emitted functions. 1417 return S.DeviceKnownEmittedFns.count(FD) > 0; 1418 } 1419 1420 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1421 unsigned DiagID) { 1422 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1423 "Expected OpenMP device compilation."); 1424 return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) && 1425 !isKnownEmitted(*this, getCurFunctionDecl())) 1426 ? DeviceDiagBuilder::K_Deferred 1427 : DeviceDiagBuilder::K_Immediate, 1428 Loc, DiagID, getCurFunctionDecl(), *this); 1429 } 1430 1431 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) { 1432 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1433 "Expected OpenMP device compilation."); 1434 assert(Callee && "Callee may not be null."); 1435 FunctionDecl *Caller = getCurFunctionDecl(); 1436 1437 // If the caller is known-emitted, mark the callee as known-emitted. 1438 // Otherwise, mark the call in our call graph so we can traverse it later. 1439 if (!isOpenMPDeviceDelayedContext(*this) || 1440 (Caller && isKnownEmitted(*this, Caller))) 1441 markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted); 1442 else if (Caller) 1443 DeviceCallGraph[Caller].insert({Callee, Loc}); 1444 } 1445 1446 void Sema::checkOpenMPDeviceExpr(const Expr *E) { 1447 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && 1448 "OpenMP device compilation mode is expected."); 1449 QualType Ty = E->getType(); 1450 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) || 1451 (Ty->isFloat128Type() && !Context.getTargetInfo().hasFloat128Type()) || 1452 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 && 1453 !Context.getTargetInfo().hasInt128Type())) 1454 targetDiag(E->getExprLoc(), diag::err_type_unsupported) 1455 << Ty << E->getSourceRange(); 1456 } 1457 1458 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { 1459 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1460 1461 ASTContext &Ctx = getASTContext(); 1462 bool IsByRef = true; 1463 1464 // Find the directive that is associated with the provided scope. 1465 D = cast<ValueDecl>(D->getCanonicalDecl()); 1466 QualType Ty = D->getType(); 1467 1468 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1469 // This table summarizes how a given variable should be passed to the device 1470 // given its type and the clauses where it appears. This table is based on 1471 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1472 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1473 // 1474 // ========================================================================= 1475 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1476 // | |(tofrom:scalar)| | pvt | | | | 1477 // ========================================================================= 1478 // | scl | | | | - | | bycopy| 1479 // | scl | | - | x | - | - | bycopy| 1480 // | scl | | x | - | - | - | null | 1481 // | scl | x | | | - | | byref | 1482 // | scl | x | - | x | - | - | bycopy| 1483 // | scl | x | x | - | - | - | null | 1484 // | scl | | - | - | - | x | byref | 1485 // | scl | x | - | - | - | x | byref | 1486 // 1487 // | agg | n.a. | | | - | | byref | 1488 // | agg | n.a. | - | x | - | - | byref | 1489 // | agg | n.a. | x | - | - | - | null | 1490 // | agg | n.a. | - | - | - | x | byref | 1491 // | agg | n.a. | - | - | - | x[] | byref | 1492 // 1493 // | ptr | n.a. | | | - | | bycopy| 1494 // | ptr | n.a. | - | x | - | - | bycopy| 1495 // | ptr | n.a. | x | - | - | - | null | 1496 // | ptr | n.a. | - | - | - | x | byref | 1497 // | ptr | n.a. | - | - | - | x[] | bycopy| 1498 // | ptr | n.a. | - | - | x | | bycopy| 1499 // | ptr | n.a. | - | - | x | x | bycopy| 1500 // | ptr | n.a. | - | - | x | x[] | bycopy| 1501 // ========================================================================= 1502 // Legend: 1503 // scl - scalar 1504 // ptr - pointer 1505 // agg - aggregate 1506 // x - applies 1507 // - - invalid in this combination 1508 // [] - mapped with an array section 1509 // byref - should be mapped by reference 1510 // byval - should be mapped by value 1511 // null - initialize a local variable to null on the device 1512 // 1513 // Observations: 1514 // - All scalar declarations that show up in a map clause have to be passed 1515 // by reference, because they may have been mapped in the enclosing data 1516 // environment. 1517 // - If the scalar value does not fit the size of uintptr, it has to be 1518 // passed by reference, regardless the result in the table above. 1519 // - For pointers mapped by value that have either an implicit map or an 1520 // array section, the runtime library may pass the NULL value to the 1521 // device instead of the value passed to it by the compiler. 1522 1523 if (Ty->isReferenceType()) 1524 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1525 1526 // Locate map clauses and see if the variable being captured is referred to 1527 // in any of those clauses. Here we only care about variables, not fields, 1528 // because fields are part of aggregates. 1529 bool IsVariableUsedInMapClause = false; 1530 bool IsVariableAssociatedWithSection = false; 1531 1532 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1533 D, Level, 1534 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1535 OMPClauseMappableExprCommon::MappableExprComponentListRef 1536 MapExprComponents, 1537 OpenMPClauseKind WhereFoundClauseKind) { 1538 // Only the map clause information influences how a variable is 1539 // captured. E.g. is_device_ptr does not require changing the default 1540 // behavior. 1541 if (WhereFoundClauseKind != OMPC_map) 1542 return false; 1543 1544 auto EI = MapExprComponents.rbegin(); 1545 auto EE = MapExprComponents.rend(); 1546 1547 assert(EI != EE && "Invalid map expression!"); 1548 1549 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1550 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1551 1552 ++EI; 1553 if (EI == EE) 1554 return false; 1555 1556 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1557 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1558 isa<MemberExpr>(EI->getAssociatedExpression())) { 1559 IsVariableAssociatedWithSection = true; 1560 // There is nothing more we need to know about this variable. 1561 return true; 1562 } 1563 1564 // Keep looking for more map info. 1565 return false; 1566 }); 1567 1568 if (IsVariableUsedInMapClause) { 1569 // If variable is identified in a map clause it is always captured by 1570 // reference except if it is a pointer that is dereferenced somehow. 1571 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1572 } else { 1573 // By default, all the data that has a scalar type is mapped by copy 1574 // (except for reduction variables). 1575 IsByRef = 1576 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1577 !Ty->isAnyPointerType()) || 1578 !Ty->isScalarType() || 1579 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar || 1580 DSAStack->hasExplicitDSA( 1581 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 1582 } 1583 } 1584 1585 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1586 IsByRef = 1587 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1588 !Ty->isAnyPointerType()) || 1589 !DSAStack->hasExplicitDSA( 1590 D, 1591 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1592 Level, /*NotLastprivate=*/true)) && 1593 // If the variable is artificial and must be captured by value - try to 1594 // capture by value. 1595 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 1596 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 1597 } 1598 1599 // When passing data by copy, we need to make sure it fits the uintptr size 1600 // and alignment, because the runtime library only deals with uintptr types. 1601 // If it does not fit the uintptr size, we need to pass the data by reference 1602 // instead. 1603 if (!IsByRef && 1604 (Ctx.getTypeSizeInChars(Ty) > 1605 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1606 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1607 IsByRef = true; 1608 } 1609 1610 return IsByRef; 1611 } 1612 1613 unsigned Sema::getOpenMPNestingLevel() const { 1614 assert(getLangOpts().OpenMP); 1615 return DSAStack->getNestingLevel(); 1616 } 1617 1618 bool Sema::isInOpenMPTargetExecutionDirective() const { 1619 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 1620 !DSAStack->isClauseParsingMode()) || 1621 DSAStack->hasDirective( 1622 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1623 SourceLocation) -> bool { 1624 return isOpenMPTargetExecutionDirective(K); 1625 }, 1626 false); 1627 } 1628 1629 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) { 1630 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1631 D = getCanonicalDecl(D); 1632 1633 // If we are attempting to capture a global variable in a directive with 1634 // 'target' we return true so that this global is also mapped to the device. 1635 // 1636 auto *VD = dyn_cast<VarDecl>(D); 1637 if (VD && !VD->hasLocalStorage()) { 1638 if (isInOpenMPDeclareTargetContext() && 1639 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 1640 // Try to mark variable as declare target if it is used in capturing 1641 // regions. 1642 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1643 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 1644 return nullptr; 1645 } else if (isInOpenMPTargetExecutionDirective()) { 1646 // If the declaration is enclosed in a 'declare target' directive, 1647 // then it should not be captured. 1648 // 1649 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1650 return nullptr; 1651 return VD; 1652 } 1653 } 1654 // Capture variables captured by reference in lambdas for target-based 1655 // directives. 1656 if (VD && !DSAStack->isClauseParsingMode()) { 1657 if (const auto *RD = VD->getType() 1658 .getCanonicalType() 1659 .getNonReferenceType() 1660 ->getAsCXXRecordDecl()) { 1661 bool SavedForceCaptureByReferenceInTargetExecutable = 1662 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 1663 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true); 1664 if (RD->isLambda()) { 1665 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 1666 FieldDecl *ThisCapture; 1667 RD->getCaptureFields(Captures, ThisCapture); 1668 for (const LambdaCapture &LC : RD->captures()) { 1669 if (LC.getCaptureKind() == LCK_ByRef) { 1670 VarDecl *VD = LC.getCapturedVar(); 1671 DeclContext *VDC = VD->getDeclContext(); 1672 if (!VDC->Encloses(CurContext)) 1673 continue; 1674 DSAStackTy::DSAVarData DVarPrivate = 1675 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1676 // Do not capture already captured variables. 1677 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && 1678 DVarPrivate.CKind == OMPC_unknown && 1679 !DSAStack->checkMappableExprComponentListsForDecl( 1680 D, /*CurrentRegionOnly=*/true, 1681 [](OMPClauseMappableExprCommon:: 1682 MappableExprComponentListRef, 1683 OpenMPClauseKind) { return true; })) 1684 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar()); 1685 } else if (LC.getCaptureKind() == LCK_This) { 1686 QualType ThisTy = getCurrentThisType(); 1687 if (!ThisTy.isNull() && 1688 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 1689 CheckCXXThisCapture(LC.getLocation()); 1690 } 1691 } 1692 } 1693 DSAStack->setForceCaptureByReferenceInTargetExecutable( 1694 SavedForceCaptureByReferenceInTargetExecutable); 1695 } 1696 } 1697 1698 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1699 (!DSAStack->isClauseParsingMode() || 1700 DSAStack->getParentDirective() != OMPD_unknown)) { 1701 auto &&Info = DSAStack->isLoopControlVariable(D); 1702 if (Info.first || 1703 (VD && VD->hasLocalStorage() && 1704 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 1705 (VD && DSAStack->isForceVarCapturing())) 1706 return VD ? VD : Info.second; 1707 DSAStackTy::DSAVarData DVarPrivate = 1708 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1709 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1710 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1711 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1712 [](OpenMPDirectiveKind) { return true; }, 1713 DSAStack->isClauseParsingMode()); 1714 if (DVarPrivate.CKind != OMPC_unknown) 1715 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1716 } 1717 return nullptr; 1718 } 1719 1720 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1721 unsigned Level) const { 1722 SmallVector<OpenMPDirectiveKind, 4> Regions; 1723 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1724 FunctionScopesIndex -= Regions.size(); 1725 } 1726 1727 void Sema::startOpenMPLoop() { 1728 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 1729 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 1730 DSAStack->loopInit(); 1731 } 1732 1733 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1734 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1735 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 1736 if (DSAStack->getAssociatedLoops() > 0 && 1737 !DSAStack->isLoopStarted()) { 1738 DSAStack->resetPossibleLoopCounter(D); 1739 DSAStack->loopStart(); 1740 return true; 1741 } 1742 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 1743 DSAStack->isLoopControlVariable(D).first) && 1744 !DSAStack->hasExplicitDSA( 1745 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 1746 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 1747 return true; 1748 } 1749 return DSAStack->hasExplicitDSA( 1750 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1751 (DSAStack->isClauseParsingMode() && 1752 DSAStack->getClauseParsingMode() == OMPC_private) || 1753 // Consider taskgroup reduction descriptor variable a private to avoid 1754 // possible capture in the region. 1755 (DSAStack->hasExplicitDirective( 1756 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1757 Level) && 1758 DSAStack->isTaskgroupReductionRef(D, Level)); 1759 } 1760 1761 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1762 unsigned Level) { 1763 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1764 D = getCanonicalDecl(D); 1765 OpenMPClauseKind OMPC = OMPC_unknown; 1766 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1767 const unsigned NewLevel = I - 1; 1768 if (DSAStack->hasExplicitDSA(D, 1769 [&OMPC](const OpenMPClauseKind K) { 1770 if (isOpenMPPrivate(K)) { 1771 OMPC = K; 1772 return true; 1773 } 1774 return false; 1775 }, 1776 NewLevel)) 1777 break; 1778 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1779 D, NewLevel, 1780 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1781 OpenMPClauseKind) { return true; })) { 1782 OMPC = OMPC_map; 1783 break; 1784 } 1785 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1786 NewLevel)) { 1787 OMPC = OMPC_map; 1788 if (D->getType()->isScalarType() && 1789 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1790 DefaultMapAttributes::DMA_tofrom_scalar) 1791 OMPC = OMPC_firstprivate; 1792 break; 1793 } 1794 } 1795 if (OMPC != OMPC_unknown) 1796 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1797 } 1798 1799 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1800 unsigned Level) const { 1801 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1802 // Return true if the current level is no longer enclosed in a target region. 1803 1804 const auto *VD = dyn_cast<VarDecl>(D); 1805 return VD && !VD->hasLocalStorage() && 1806 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1807 Level); 1808 } 1809 1810 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1811 1812 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1813 const DeclarationNameInfo &DirName, 1814 Scope *CurScope, SourceLocation Loc) { 1815 DSAStack->push(DKind, DirName, CurScope, Loc); 1816 PushExpressionEvaluationContext( 1817 ExpressionEvaluationContext::PotentiallyEvaluated); 1818 } 1819 1820 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1821 DSAStack->setClauseParsingMode(K); 1822 } 1823 1824 void Sema::EndOpenMPClause() { 1825 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1826 } 1827 1828 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1829 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1830 // A variable of class type (or array thereof) that appears in a lastprivate 1831 // clause requires an accessible, unambiguous default constructor for the 1832 // class type, unless the list item is also specified in a firstprivate 1833 // clause. 1834 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1835 for (OMPClause *C : D->clauses()) { 1836 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1837 SmallVector<Expr *, 8> PrivateCopies; 1838 for (Expr *DE : Clause->varlists()) { 1839 if (DE->isValueDependent() || DE->isTypeDependent()) { 1840 PrivateCopies.push_back(nullptr); 1841 continue; 1842 } 1843 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1844 auto *VD = cast<VarDecl>(DRE->getDecl()); 1845 QualType Type = VD->getType().getNonReferenceType(); 1846 const DSAStackTy::DSAVarData DVar = 1847 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1848 if (DVar.CKind == OMPC_lastprivate) { 1849 // Generate helper private variable and initialize it with the 1850 // default value. The address of the original variable is replaced 1851 // by the address of the new private variable in CodeGen. This new 1852 // variable is not added to IdResolver, so the code in the OpenMP 1853 // region uses original variable for proper diagnostics. 1854 VarDecl *VDPrivate = buildVarDecl( 1855 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1856 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1857 ActOnUninitializedDecl(VDPrivate); 1858 if (VDPrivate->isInvalidDecl()) 1859 continue; 1860 PrivateCopies.push_back(buildDeclRefExpr( 1861 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1862 } else { 1863 // The variable is also a firstprivate, so initialization sequence 1864 // for private copy is generated already. 1865 PrivateCopies.push_back(nullptr); 1866 } 1867 } 1868 // Set initializers to private copies if no errors were found. 1869 if (PrivateCopies.size() == Clause->varlist_size()) 1870 Clause->setPrivateCopies(PrivateCopies); 1871 } 1872 } 1873 } 1874 1875 DSAStack->pop(); 1876 DiscardCleanupsInEvaluationContext(); 1877 PopExpressionEvaluationContext(); 1878 } 1879 1880 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1881 Expr *NumIterations, Sema &SemaRef, 1882 Scope *S, DSAStackTy *Stack); 1883 1884 namespace { 1885 1886 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 1887 private: 1888 Sema &SemaRef; 1889 1890 public: 1891 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1892 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1893 NamedDecl *ND = Candidate.getCorrectionDecl(); 1894 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1895 return VD->hasGlobalStorage() && 1896 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1897 SemaRef.getCurScope()); 1898 } 1899 return false; 1900 } 1901 }; 1902 1903 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 1904 private: 1905 Sema &SemaRef; 1906 1907 public: 1908 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1909 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1910 NamedDecl *ND = Candidate.getCorrectionDecl(); 1911 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) { 1912 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1913 SemaRef.getCurScope()); 1914 } 1915 return false; 1916 } 1917 }; 1918 1919 } // namespace 1920 1921 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1922 CXXScopeSpec &ScopeSpec, 1923 const DeclarationNameInfo &Id) { 1924 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1925 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1926 1927 if (Lookup.isAmbiguous()) 1928 return ExprError(); 1929 1930 VarDecl *VD; 1931 if (!Lookup.isSingleResult()) { 1932 if (TypoCorrection Corrected = CorrectTypo( 1933 Id, LookupOrdinaryName, CurScope, nullptr, 1934 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1935 diagnoseTypo(Corrected, 1936 PDiag(Lookup.empty() 1937 ? diag::err_undeclared_var_use_suggest 1938 : diag::err_omp_expected_var_arg_suggest) 1939 << Id.getName()); 1940 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1941 } else { 1942 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1943 : diag::err_omp_expected_var_arg) 1944 << Id.getName(); 1945 return ExprError(); 1946 } 1947 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1948 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1949 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1950 return ExprError(); 1951 } 1952 Lookup.suppressDiagnostics(); 1953 1954 // OpenMP [2.9.2, Syntax, C/C++] 1955 // Variables must be file-scope, namespace-scope, or static block-scope. 1956 if (!VD->hasGlobalStorage()) { 1957 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1958 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1959 bool IsDecl = 1960 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1961 Diag(VD->getLocation(), 1962 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1963 << VD; 1964 return ExprError(); 1965 } 1966 1967 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1968 NamedDecl *ND = CanonicalVD; 1969 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1970 // A threadprivate directive for file-scope variables must appear outside 1971 // any definition or declaration. 1972 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1973 !getCurLexicalContext()->isTranslationUnit()) { 1974 Diag(Id.getLoc(), diag::err_omp_var_scope) 1975 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1976 bool IsDecl = 1977 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1978 Diag(VD->getLocation(), 1979 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1980 << VD; 1981 return ExprError(); 1982 } 1983 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1984 // A threadprivate directive for static class member variables must appear 1985 // in the class definition, in the same scope in which the member 1986 // variables are declared. 1987 if (CanonicalVD->isStaticDataMember() && 1988 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1989 Diag(Id.getLoc(), diag::err_omp_var_scope) 1990 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1991 bool IsDecl = 1992 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1993 Diag(VD->getLocation(), 1994 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1995 << VD; 1996 return ExprError(); 1997 } 1998 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1999 // A threadprivate directive for namespace-scope variables must appear 2000 // outside any definition or declaration other than the namespace 2001 // definition itself. 2002 if (CanonicalVD->getDeclContext()->isNamespace() && 2003 (!getCurLexicalContext()->isFileContext() || 2004 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2005 Diag(Id.getLoc(), diag::err_omp_var_scope) 2006 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 2007 bool IsDecl = 2008 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2009 Diag(VD->getLocation(), 2010 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2011 << VD; 2012 return ExprError(); 2013 } 2014 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2015 // A threadprivate directive for static block-scope variables must appear 2016 // in the scope of the variable and not in a nested scope. 2017 if (CanonicalVD->isStaticLocal() && CurScope && 2018 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2019 Diag(Id.getLoc(), diag::err_omp_var_scope) 2020 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 2021 bool IsDecl = 2022 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2023 Diag(VD->getLocation(), 2024 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2025 << VD; 2026 return ExprError(); 2027 } 2028 2029 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2030 // A threadprivate directive must lexically precede all references to any 2031 // of the variables in its list. 2032 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 2033 Diag(Id.getLoc(), diag::err_omp_var_used) 2034 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 2035 return ExprError(); 2036 } 2037 2038 QualType ExprType = VD->getType().getNonReferenceType(); 2039 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2040 SourceLocation(), VD, 2041 /*RefersToEnclosingVariableOrCapture=*/false, 2042 Id.getLoc(), ExprType, VK_LValue); 2043 } 2044 2045 Sema::DeclGroupPtrTy 2046 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2047 ArrayRef<Expr *> VarList) { 2048 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2049 CurContext->addDecl(D); 2050 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2051 } 2052 return nullptr; 2053 } 2054 2055 namespace { 2056 class LocalVarRefChecker final 2057 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2058 Sema &SemaRef; 2059 2060 public: 2061 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2062 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2063 if (VD->hasLocalStorage()) { 2064 SemaRef.Diag(E->getBeginLoc(), 2065 diag::err_omp_local_var_in_threadprivate_init) 2066 << E->getSourceRange(); 2067 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2068 << VD << VD->getSourceRange(); 2069 return true; 2070 } 2071 } 2072 return false; 2073 } 2074 bool VisitStmt(const Stmt *S) { 2075 for (const Stmt *Child : S->children()) { 2076 if (Child && Visit(Child)) 2077 return true; 2078 } 2079 return false; 2080 } 2081 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2082 }; 2083 } // namespace 2084 2085 OMPThreadPrivateDecl * 2086 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2087 SmallVector<Expr *, 8> Vars; 2088 for (Expr *RefExpr : VarList) { 2089 auto *DE = cast<DeclRefExpr>(RefExpr); 2090 auto *VD = cast<VarDecl>(DE->getDecl()); 2091 SourceLocation ILoc = DE->getExprLoc(); 2092 2093 // Mark variable as used. 2094 VD->setReferenced(); 2095 VD->markUsed(Context); 2096 2097 QualType QType = VD->getType(); 2098 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2099 // It will be analyzed later. 2100 Vars.push_back(DE); 2101 continue; 2102 } 2103 2104 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2105 // A threadprivate variable must not have an incomplete type. 2106 if (RequireCompleteType(ILoc, VD->getType(), 2107 diag::err_omp_threadprivate_incomplete_type)) { 2108 continue; 2109 } 2110 2111 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2112 // A threadprivate variable must not have a reference type. 2113 if (VD->getType()->isReferenceType()) { 2114 Diag(ILoc, diag::err_omp_ref_type_arg) 2115 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2116 bool IsDecl = 2117 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2118 Diag(VD->getLocation(), 2119 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2120 << VD; 2121 continue; 2122 } 2123 2124 // Check if this is a TLS variable. If TLS is not being supported, produce 2125 // the corresponding diagnostic. 2126 if ((VD->getTLSKind() != VarDecl::TLS_None && 2127 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2128 getLangOpts().OpenMPUseTLS && 2129 getASTContext().getTargetInfo().isTLSSupported())) || 2130 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2131 !VD->isLocalVarDecl())) { 2132 Diag(ILoc, diag::err_omp_var_thread_local) 2133 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2134 bool IsDecl = 2135 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2136 Diag(VD->getLocation(), 2137 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2138 << VD; 2139 continue; 2140 } 2141 2142 // Check if initial value of threadprivate variable reference variable with 2143 // local storage (it is not supported by runtime). 2144 if (const Expr *Init = VD->getAnyInitializer()) { 2145 LocalVarRefChecker Checker(*this); 2146 if (Checker.Visit(Init)) 2147 continue; 2148 } 2149 2150 Vars.push_back(RefExpr); 2151 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2152 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2153 Context, SourceRange(Loc, Loc))); 2154 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2155 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2156 } 2157 OMPThreadPrivateDecl *D = nullptr; 2158 if (!Vars.empty()) { 2159 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2160 Vars); 2161 D->setAccess(AS_public); 2162 } 2163 return D; 2164 } 2165 2166 Sema::DeclGroupPtrTy 2167 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 2168 ArrayRef<OMPClause *> ClauseList) { 2169 OMPRequiresDecl *D = nullptr; 2170 if (!CurContext->isFileContext()) { 2171 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 2172 } else { 2173 D = CheckOMPRequiresDecl(Loc, ClauseList); 2174 if (D) { 2175 CurContext->addDecl(D); 2176 DSAStack->addRequiresDecl(D); 2177 } 2178 } 2179 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2180 } 2181 2182 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 2183 ArrayRef<OMPClause *> ClauseList) { 2184 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 2185 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 2186 ClauseList); 2187 return nullptr; 2188 } 2189 2190 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2191 const ValueDecl *D, 2192 const DSAStackTy::DSAVarData &DVar, 2193 bool IsLoopIterVar = false) { 2194 if (DVar.RefExpr) { 2195 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 2196 << getOpenMPClauseName(DVar.CKind); 2197 return; 2198 } 2199 enum { 2200 PDSA_StaticMemberShared, 2201 PDSA_StaticLocalVarShared, 2202 PDSA_LoopIterVarPrivate, 2203 PDSA_LoopIterVarLinear, 2204 PDSA_LoopIterVarLastprivate, 2205 PDSA_ConstVarShared, 2206 PDSA_GlobalVarShared, 2207 PDSA_TaskVarFirstprivate, 2208 PDSA_LocalVarPrivate, 2209 PDSA_Implicit 2210 } Reason = PDSA_Implicit; 2211 bool ReportHint = false; 2212 auto ReportLoc = D->getLocation(); 2213 auto *VD = dyn_cast<VarDecl>(D); 2214 if (IsLoopIterVar) { 2215 if (DVar.CKind == OMPC_private) 2216 Reason = PDSA_LoopIterVarPrivate; 2217 else if (DVar.CKind == OMPC_lastprivate) 2218 Reason = PDSA_LoopIterVarLastprivate; 2219 else 2220 Reason = PDSA_LoopIterVarLinear; 2221 } else if (isOpenMPTaskingDirective(DVar.DKind) && 2222 DVar.CKind == OMPC_firstprivate) { 2223 Reason = PDSA_TaskVarFirstprivate; 2224 ReportLoc = DVar.ImplicitDSALoc; 2225 } else if (VD && VD->isStaticLocal()) 2226 Reason = PDSA_StaticLocalVarShared; 2227 else if (VD && VD->isStaticDataMember()) 2228 Reason = PDSA_StaticMemberShared; 2229 else if (VD && VD->isFileVarDecl()) 2230 Reason = PDSA_GlobalVarShared; 2231 else if (D->getType().isConstant(SemaRef.getASTContext())) 2232 Reason = PDSA_ConstVarShared; 2233 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 2234 ReportHint = true; 2235 Reason = PDSA_LocalVarPrivate; 2236 } 2237 if (Reason != PDSA_Implicit) { 2238 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 2239 << Reason << ReportHint 2240 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 2241 } else if (DVar.ImplicitDSALoc.isValid()) { 2242 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 2243 << getOpenMPClauseName(DVar.CKind); 2244 } 2245 } 2246 2247 namespace { 2248 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 2249 DSAStackTy *Stack; 2250 Sema &SemaRef; 2251 bool ErrorFound = false; 2252 CapturedStmt *CS = nullptr; 2253 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 2254 llvm::SmallVector<Expr *, 4> ImplicitMap; 2255 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 2256 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 2257 2258 void VisitSubCaptures(OMPExecutableDirective *S) { 2259 // Check implicitly captured variables. 2260 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 2261 return; 2262 for (const CapturedStmt::Capture &Cap : 2263 S->getInnermostCapturedStmt()->captures()) { 2264 if (!Cap.capturesVariable()) 2265 continue; 2266 VarDecl *VD = Cap.getCapturedVar(); 2267 // Do not try to map the variable if it or its sub-component was mapped 2268 // already. 2269 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 2270 Stack->checkMappableExprComponentListsForDecl( 2271 VD, /*CurrentRegionOnly=*/true, 2272 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2273 OpenMPClauseKind) { return true; })) 2274 continue; 2275 DeclRefExpr *DRE = buildDeclRefExpr( 2276 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 2277 Cap.getLocation(), /*RefersToCapture=*/true); 2278 Visit(DRE); 2279 } 2280 } 2281 2282 public: 2283 void VisitDeclRefExpr(DeclRefExpr *E) { 2284 if (E->isTypeDependent() || E->isValueDependent() || 2285 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2286 return; 2287 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2288 VD = VD->getCanonicalDecl(); 2289 // Skip internally declared variables. 2290 if (VD->hasLocalStorage() && !CS->capturesVariable(VD)) 2291 return; 2292 2293 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 2294 // Check if the variable has explicit DSA set and stop analysis if it so. 2295 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 2296 return; 2297 2298 // Skip internally declared static variables. 2299 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 2300 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 2301 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) && 2302 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 2303 return; 2304 2305 SourceLocation ELoc = E->getExprLoc(); 2306 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2307 // The default(none) clause requires that each variable that is referenced 2308 // in the construct, and does not have a predetermined data-sharing 2309 // attribute, must have its data-sharing attribute explicitly determined 2310 // by being listed in a data-sharing attribute clause. 2311 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 2312 isImplicitOrExplicitTaskingRegion(DKind) && 2313 VarsWithInheritedDSA.count(VD) == 0) { 2314 VarsWithInheritedDSA[VD] = E; 2315 return; 2316 } 2317 2318 if (isOpenMPTargetExecutionDirective(DKind) && 2319 !Stack->isLoopControlVariable(VD).first) { 2320 if (!Stack->checkMappableExprComponentListsForDecl( 2321 VD, /*CurrentRegionOnly=*/true, 2322 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2323 StackComponents, 2324 OpenMPClauseKind) { 2325 // Variable is used if it has been marked as an array, array 2326 // section or the variable iself. 2327 return StackComponents.size() == 1 || 2328 std::all_of( 2329 std::next(StackComponents.rbegin()), 2330 StackComponents.rend(), 2331 [](const OMPClauseMappableExprCommon:: 2332 MappableComponent &MC) { 2333 return MC.getAssociatedDeclaration() == 2334 nullptr && 2335 (isa<OMPArraySectionExpr>( 2336 MC.getAssociatedExpression()) || 2337 isa<ArraySubscriptExpr>( 2338 MC.getAssociatedExpression())); 2339 }); 2340 })) { 2341 bool IsFirstprivate = false; 2342 // By default lambdas are captured as firstprivates. 2343 if (const auto *RD = 2344 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2345 IsFirstprivate = RD->isLambda(); 2346 IsFirstprivate = 2347 IsFirstprivate || 2348 (VD->getType().getNonReferenceType()->isScalarType() && 2349 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2350 if (IsFirstprivate) 2351 ImplicitFirstprivate.emplace_back(E); 2352 else 2353 ImplicitMap.emplace_back(E); 2354 return; 2355 } 2356 } 2357 2358 // OpenMP [2.9.3.6, Restrictions, p.2] 2359 // A list item that appears in a reduction clause of the innermost 2360 // enclosing worksharing or parallel construct may not be accessed in an 2361 // explicit task. 2362 DVar = Stack->hasInnermostDSA( 2363 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2364 [](OpenMPDirectiveKind K) { 2365 return isOpenMPParallelDirective(K) || 2366 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2367 }, 2368 /*FromParent=*/true); 2369 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2370 ErrorFound = true; 2371 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2372 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2373 return; 2374 } 2375 2376 // Define implicit data-sharing attributes for task. 2377 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2378 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2379 !Stack->isLoopControlVariable(VD).first) 2380 ImplicitFirstprivate.push_back(E); 2381 } 2382 } 2383 void VisitMemberExpr(MemberExpr *E) { 2384 if (E->isTypeDependent() || E->isValueDependent() || 2385 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2386 return; 2387 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2388 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2389 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2390 if (!FD) 2391 return; 2392 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2393 // Check if the variable has explicit DSA set and stop analysis if it 2394 // so. 2395 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2396 return; 2397 2398 if (isOpenMPTargetExecutionDirective(DKind) && 2399 !Stack->isLoopControlVariable(FD).first && 2400 !Stack->checkMappableExprComponentListsForDecl( 2401 FD, /*CurrentRegionOnly=*/true, 2402 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2403 StackComponents, 2404 OpenMPClauseKind) { 2405 return isa<CXXThisExpr>( 2406 cast<MemberExpr>( 2407 StackComponents.back().getAssociatedExpression()) 2408 ->getBase() 2409 ->IgnoreParens()); 2410 })) { 2411 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2412 // A bit-field cannot appear in a map clause. 2413 // 2414 if (FD->isBitField()) 2415 return; 2416 2417 // Check to see if the member expression is referencing a class that 2418 // has already been explicitly mapped 2419 if (Stack->isClassPreviouslyMapped(TE->getType())) 2420 return; 2421 2422 ImplicitMap.emplace_back(E); 2423 return; 2424 } 2425 2426 SourceLocation ELoc = E->getExprLoc(); 2427 // OpenMP [2.9.3.6, Restrictions, p.2] 2428 // A list item that appears in a reduction clause of the innermost 2429 // enclosing worksharing or parallel construct may not be accessed in 2430 // an explicit task. 2431 DVar = Stack->hasInnermostDSA( 2432 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2433 [](OpenMPDirectiveKind K) { 2434 return isOpenMPParallelDirective(K) || 2435 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2436 }, 2437 /*FromParent=*/true); 2438 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2439 ErrorFound = true; 2440 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2441 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2442 return; 2443 } 2444 2445 // Define implicit data-sharing attributes for task. 2446 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2447 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2448 !Stack->isLoopControlVariable(FD).first) { 2449 // Check if there is a captured expression for the current field in the 2450 // region. Do not mark it as firstprivate unless there is no captured 2451 // expression. 2452 // TODO: try to make it firstprivate. 2453 if (DVar.CKind != OMPC_unknown) 2454 ImplicitFirstprivate.push_back(E); 2455 } 2456 return; 2457 } 2458 if (isOpenMPTargetExecutionDirective(DKind)) { 2459 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2460 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2461 /*NoDiagnose=*/true)) 2462 return; 2463 const auto *VD = cast<ValueDecl>( 2464 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2465 if (!Stack->checkMappableExprComponentListsForDecl( 2466 VD, /*CurrentRegionOnly=*/true, 2467 [&CurComponents]( 2468 OMPClauseMappableExprCommon::MappableExprComponentListRef 2469 StackComponents, 2470 OpenMPClauseKind) { 2471 auto CCI = CurComponents.rbegin(); 2472 auto CCE = CurComponents.rend(); 2473 for (const auto &SC : llvm::reverse(StackComponents)) { 2474 // Do both expressions have the same kind? 2475 if (CCI->getAssociatedExpression()->getStmtClass() != 2476 SC.getAssociatedExpression()->getStmtClass()) 2477 if (!(isa<OMPArraySectionExpr>( 2478 SC.getAssociatedExpression()) && 2479 isa<ArraySubscriptExpr>( 2480 CCI->getAssociatedExpression()))) 2481 return false; 2482 2483 const Decl *CCD = CCI->getAssociatedDeclaration(); 2484 const Decl *SCD = SC.getAssociatedDeclaration(); 2485 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2486 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2487 if (SCD != CCD) 2488 return false; 2489 std::advance(CCI, 1); 2490 if (CCI == CCE) 2491 break; 2492 } 2493 return true; 2494 })) { 2495 Visit(E->getBase()); 2496 } 2497 } else { 2498 Visit(E->getBase()); 2499 } 2500 } 2501 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2502 for (OMPClause *C : S->clauses()) { 2503 // Skip analysis of arguments of implicitly defined firstprivate clause 2504 // for task|target directives. 2505 // Skip analysis of arguments of implicitly defined map clause for target 2506 // directives. 2507 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2508 C->isImplicit())) { 2509 for (Stmt *CC : C->children()) { 2510 if (CC) 2511 Visit(CC); 2512 } 2513 } 2514 } 2515 // Check implicitly captured variables. 2516 VisitSubCaptures(S); 2517 } 2518 void VisitStmt(Stmt *S) { 2519 for (Stmt *C : S->children()) { 2520 if (C) { 2521 // Check implicitly captured variables in the task-based directives to 2522 // check if they must be firstprivatized. 2523 Visit(C); 2524 } 2525 } 2526 } 2527 2528 bool isErrorFound() const { return ErrorFound; } 2529 ArrayRef<Expr *> getImplicitFirstprivate() const { 2530 return ImplicitFirstprivate; 2531 } 2532 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2533 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2534 return VarsWithInheritedDSA; 2535 } 2536 2537 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2538 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 2539 }; 2540 } // namespace 2541 2542 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2543 switch (DKind) { 2544 case OMPD_parallel: 2545 case OMPD_parallel_for: 2546 case OMPD_parallel_for_simd: 2547 case OMPD_parallel_sections: 2548 case OMPD_teams: 2549 case OMPD_teams_distribute: 2550 case OMPD_teams_distribute_simd: { 2551 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2552 QualType KmpInt32PtrTy = 2553 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2554 Sema::CapturedParamNameType Params[] = { 2555 std::make_pair(".global_tid.", KmpInt32PtrTy), 2556 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2557 std::make_pair(StringRef(), QualType()) // __context with shared vars 2558 }; 2559 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2560 Params); 2561 break; 2562 } 2563 case OMPD_target_teams: 2564 case OMPD_target_parallel: 2565 case OMPD_target_parallel_for: 2566 case OMPD_target_parallel_for_simd: 2567 case OMPD_target_teams_distribute: 2568 case OMPD_target_teams_distribute_simd: { 2569 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2570 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2571 QualType KmpInt32PtrTy = 2572 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2573 QualType Args[] = {VoidPtrTy}; 2574 FunctionProtoType::ExtProtoInfo EPI; 2575 EPI.Variadic = true; 2576 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2577 Sema::CapturedParamNameType Params[] = { 2578 std::make_pair(".global_tid.", KmpInt32Ty), 2579 std::make_pair(".part_id.", KmpInt32PtrTy), 2580 std::make_pair(".privates.", VoidPtrTy), 2581 std::make_pair( 2582 ".copy_fn.", 2583 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2584 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2585 std::make_pair(StringRef(), QualType()) // __context with shared vars 2586 }; 2587 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2588 Params); 2589 // Mark this captured region as inlined, because we don't use outlined 2590 // function directly. 2591 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2592 AlwaysInlineAttr::CreateImplicit( 2593 Context, AlwaysInlineAttr::Keyword_forceinline)); 2594 Sema::CapturedParamNameType ParamsTarget[] = { 2595 std::make_pair(StringRef(), QualType()) // __context with shared vars 2596 }; 2597 // Start a captured region for 'target' with no implicit parameters. 2598 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2599 ParamsTarget); 2600 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2601 std::make_pair(".global_tid.", KmpInt32PtrTy), 2602 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2603 std::make_pair(StringRef(), QualType()) // __context with shared vars 2604 }; 2605 // Start a captured region for 'teams' or 'parallel'. Both regions have 2606 // the same implicit parameters. 2607 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2608 ParamsTeamsOrParallel); 2609 break; 2610 } 2611 case OMPD_target: 2612 case OMPD_target_simd: { 2613 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2614 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2615 QualType KmpInt32PtrTy = 2616 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2617 QualType Args[] = {VoidPtrTy}; 2618 FunctionProtoType::ExtProtoInfo EPI; 2619 EPI.Variadic = true; 2620 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2621 Sema::CapturedParamNameType Params[] = { 2622 std::make_pair(".global_tid.", KmpInt32Ty), 2623 std::make_pair(".part_id.", KmpInt32PtrTy), 2624 std::make_pair(".privates.", VoidPtrTy), 2625 std::make_pair( 2626 ".copy_fn.", 2627 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2628 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2629 std::make_pair(StringRef(), QualType()) // __context with shared vars 2630 }; 2631 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2632 Params); 2633 // Mark this captured region as inlined, because we don't use outlined 2634 // function directly. 2635 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2636 AlwaysInlineAttr::CreateImplicit( 2637 Context, AlwaysInlineAttr::Keyword_forceinline)); 2638 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2639 std::make_pair(StringRef(), QualType())); 2640 break; 2641 } 2642 case OMPD_simd: 2643 case OMPD_for: 2644 case OMPD_for_simd: 2645 case OMPD_sections: 2646 case OMPD_section: 2647 case OMPD_single: 2648 case OMPD_master: 2649 case OMPD_critical: 2650 case OMPD_taskgroup: 2651 case OMPD_distribute: 2652 case OMPD_distribute_simd: 2653 case OMPD_ordered: 2654 case OMPD_atomic: 2655 case OMPD_target_data: { 2656 Sema::CapturedParamNameType Params[] = { 2657 std::make_pair(StringRef(), QualType()) // __context with shared vars 2658 }; 2659 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2660 Params); 2661 break; 2662 } 2663 case OMPD_task: { 2664 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2665 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2666 QualType KmpInt32PtrTy = 2667 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2668 QualType Args[] = {VoidPtrTy}; 2669 FunctionProtoType::ExtProtoInfo EPI; 2670 EPI.Variadic = true; 2671 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2672 Sema::CapturedParamNameType Params[] = { 2673 std::make_pair(".global_tid.", KmpInt32Ty), 2674 std::make_pair(".part_id.", KmpInt32PtrTy), 2675 std::make_pair(".privates.", VoidPtrTy), 2676 std::make_pair( 2677 ".copy_fn.", 2678 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2679 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2680 std::make_pair(StringRef(), QualType()) // __context with shared vars 2681 }; 2682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2683 Params); 2684 // Mark this captured region as inlined, because we don't use outlined 2685 // function directly. 2686 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2687 AlwaysInlineAttr::CreateImplicit( 2688 Context, AlwaysInlineAttr::Keyword_forceinline)); 2689 break; 2690 } 2691 case OMPD_taskloop: 2692 case OMPD_taskloop_simd: { 2693 QualType KmpInt32Ty = 2694 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 2695 .withConst(); 2696 QualType KmpUInt64Ty = 2697 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 2698 .withConst(); 2699 QualType KmpInt64Ty = 2700 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 2701 .withConst(); 2702 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2703 QualType KmpInt32PtrTy = 2704 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2705 QualType Args[] = {VoidPtrTy}; 2706 FunctionProtoType::ExtProtoInfo EPI; 2707 EPI.Variadic = true; 2708 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2709 Sema::CapturedParamNameType Params[] = { 2710 std::make_pair(".global_tid.", KmpInt32Ty), 2711 std::make_pair(".part_id.", KmpInt32PtrTy), 2712 std::make_pair(".privates.", VoidPtrTy), 2713 std::make_pair( 2714 ".copy_fn.", 2715 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2716 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2717 std::make_pair(".lb.", KmpUInt64Ty), 2718 std::make_pair(".ub.", KmpUInt64Ty), 2719 std::make_pair(".st.", KmpInt64Ty), 2720 std::make_pair(".liter.", KmpInt32Ty), 2721 std::make_pair(".reductions.", VoidPtrTy), 2722 std::make_pair(StringRef(), QualType()) // __context with shared vars 2723 }; 2724 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2725 Params); 2726 // Mark this captured region as inlined, because we don't use outlined 2727 // function directly. 2728 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2729 AlwaysInlineAttr::CreateImplicit( 2730 Context, AlwaysInlineAttr::Keyword_forceinline)); 2731 break; 2732 } 2733 case OMPD_distribute_parallel_for_simd: 2734 case OMPD_distribute_parallel_for: { 2735 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2736 QualType KmpInt32PtrTy = 2737 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2738 Sema::CapturedParamNameType Params[] = { 2739 std::make_pair(".global_tid.", KmpInt32PtrTy), 2740 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2741 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2742 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2743 std::make_pair(StringRef(), QualType()) // __context with shared vars 2744 }; 2745 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2746 Params); 2747 break; 2748 } 2749 case OMPD_target_teams_distribute_parallel_for: 2750 case OMPD_target_teams_distribute_parallel_for_simd: { 2751 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2752 QualType KmpInt32PtrTy = 2753 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2754 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2755 2756 QualType Args[] = {VoidPtrTy}; 2757 FunctionProtoType::ExtProtoInfo EPI; 2758 EPI.Variadic = true; 2759 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2760 Sema::CapturedParamNameType Params[] = { 2761 std::make_pair(".global_tid.", KmpInt32Ty), 2762 std::make_pair(".part_id.", KmpInt32PtrTy), 2763 std::make_pair(".privates.", VoidPtrTy), 2764 std::make_pair( 2765 ".copy_fn.", 2766 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2767 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2768 std::make_pair(StringRef(), QualType()) // __context with shared vars 2769 }; 2770 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2771 Params); 2772 // Mark this captured region as inlined, because we don't use outlined 2773 // function directly. 2774 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2775 AlwaysInlineAttr::CreateImplicit( 2776 Context, AlwaysInlineAttr::Keyword_forceinline)); 2777 Sema::CapturedParamNameType ParamsTarget[] = { 2778 std::make_pair(StringRef(), QualType()) // __context with shared vars 2779 }; 2780 // Start a captured region for 'target' with no implicit parameters. 2781 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2782 ParamsTarget); 2783 2784 Sema::CapturedParamNameType ParamsTeams[] = { 2785 std::make_pair(".global_tid.", KmpInt32PtrTy), 2786 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2787 std::make_pair(StringRef(), QualType()) // __context with shared vars 2788 }; 2789 // Start a captured region for 'target' with no implicit parameters. 2790 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2791 ParamsTeams); 2792 2793 Sema::CapturedParamNameType ParamsParallel[] = { 2794 std::make_pair(".global_tid.", KmpInt32PtrTy), 2795 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2796 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2797 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2798 std::make_pair(StringRef(), QualType()) // __context with shared vars 2799 }; 2800 // Start a captured region for 'teams' or 'parallel'. Both regions have 2801 // the same implicit parameters. 2802 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2803 ParamsParallel); 2804 break; 2805 } 2806 2807 case OMPD_teams_distribute_parallel_for: 2808 case OMPD_teams_distribute_parallel_for_simd: { 2809 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2810 QualType KmpInt32PtrTy = 2811 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2812 2813 Sema::CapturedParamNameType ParamsTeams[] = { 2814 std::make_pair(".global_tid.", KmpInt32PtrTy), 2815 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2816 std::make_pair(StringRef(), QualType()) // __context with shared vars 2817 }; 2818 // Start a captured region for 'target' with no implicit parameters. 2819 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2820 ParamsTeams); 2821 2822 Sema::CapturedParamNameType ParamsParallel[] = { 2823 std::make_pair(".global_tid.", KmpInt32PtrTy), 2824 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2825 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2826 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2827 std::make_pair(StringRef(), QualType()) // __context with shared vars 2828 }; 2829 // Start a captured region for 'teams' or 'parallel'. Both regions have 2830 // the same implicit parameters. 2831 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2832 ParamsParallel); 2833 break; 2834 } 2835 case OMPD_target_update: 2836 case OMPD_target_enter_data: 2837 case OMPD_target_exit_data: { 2838 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2839 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2840 QualType KmpInt32PtrTy = 2841 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2842 QualType Args[] = {VoidPtrTy}; 2843 FunctionProtoType::ExtProtoInfo EPI; 2844 EPI.Variadic = true; 2845 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2846 Sema::CapturedParamNameType Params[] = { 2847 std::make_pair(".global_tid.", KmpInt32Ty), 2848 std::make_pair(".part_id.", KmpInt32PtrTy), 2849 std::make_pair(".privates.", VoidPtrTy), 2850 std::make_pair( 2851 ".copy_fn.", 2852 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2853 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2854 std::make_pair(StringRef(), QualType()) // __context with shared vars 2855 }; 2856 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2857 Params); 2858 // Mark this captured region as inlined, because we don't use outlined 2859 // function directly. 2860 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2861 AlwaysInlineAttr::CreateImplicit( 2862 Context, AlwaysInlineAttr::Keyword_forceinline)); 2863 break; 2864 } 2865 case OMPD_threadprivate: 2866 case OMPD_taskyield: 2867 case OMPD_barrier: 2868 case OMPD_taskwait: 2869 case OMPD_cancellation_point: 2870 case OMPD_cancel: 2871 case OMPD_flush: 2872 case OMPD_declare_reduction: 2873 case OMPD_declare_mapper: 2874 case OMPD_declare_simd: 2875 case OMPD_declare_target: 2876 case OMPD_end_declare_target: 2877 case OMPD_requires: 2878 llvm_unreachable("OpenMP Directive is not allowed"); 2879 case OMPD_unknown: 2880 llvm_unreachable("Unknown OpenMP directive"); 2881 } 2882 } 2883 2884 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 2885 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2886 getOpenMPCaptureRegions(CaptureRegions, DKind); 2887 return CaptureRegions.size(); 2888 } 2889 2890 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 2891 Expr *CaptureExpr, bool WithInit, 2892 bool AsExpression) { 2893 assert(CaptureExpr); 2894 ASTContext &C = S.getASTContext(); 2895 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 2896 QualType Ty = Init->getType(); 2897 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 2898 if (S.getLangOpts().CPlusPlus) { 2899 Ty = C.getLValueReferenceType(Ty); 2900 } else { 2901 Ty = C.getPointerType(Ty); 2902 ExprResult Res = 2903 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 2904 if (!Res.isUsable()) 2905 return nullptr; 2906 Init = Res.get(); 2907 } 2908 WithInit = true; 2909 } 2910 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 2911 CaptureExpr->getBeginLoc()); 2912 if (!WithInit) 2913 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 2914 S.CurContext->addHiddenDecl(CED); 2915 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 2916 return CED; 2917 } 2918 2919 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2920 bool WithInit) { 2921 OMPCapturedExprDecl *CD; 2922 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 2923 CD = cast<OMPCapturedExprDecl>(VD); 2924 else 2925 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 2926 /*AsExpression=*/false); 2927 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2928 CaptureExpr->getExprLoc()); 2929 } 2930 2931 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 2932 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 2933 if (!Ref) { 2934 OMPCapturedExprDecl *CD = buildCaptureDecl( 2935 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 2936 /*WithInit=*/true, /*AsExpression=*/true); 2937 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2938 CaptureExpr->getExprLoc()); 2939 } 2940 ExprResult Res = Ref; 2941 if (!S.getLangOpts().CPlusPlus && 2942 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 2943 Ref->getType()->isPointerType()) { 2944 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 2945 if (!Res.isUsable()) 2946 return ExprError(); 2947 } 2948 return S.DefaultLvalueConversion(Res.get()); 2949 } 2950 2951 namespace { 2952 // OpenMP directives parsed in this section are represented as a 2953 // CapturedStatement with an associated statement. If a syntax error 2954 // is detected during the parsing of the associated statement, the 2955 // compiler must abort processing and close the CapturedStatement. 2956 // 2957 // Combined directives such as 'target parallel' have more than one 2958 // nested CapturedStatements. This RAII ensures that we unwind out 2959 // of all the nested CapturedStatements when an error is found. 2960 class CaptureRegionUnwinderRAII { 2961 private: 2962 Sema &S; 2963 bool &ErrorFound; 2964 OpenMPDirectiveKind DKind = OMPD_unknown; 2965 2966 public: 2967 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 2968 OpenMPDirectiveKind DKind) 2969 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 2970 ~CaptureRegionUnwinderRAII() { 2971 if (ErrorFound) { 2972 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 2973 while (--ThisCaptureLevel >= 0) 2974 S.ActOnCapturedRegionError(); 2975 } 2976 } 2977 }; 2978 } // namespace 2979 2980 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 2981 ArrayRef<OMPClause *> Clauses) { 2982 bool ErrorFound = false; 2983 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 2984 *this, ErrorFound, DSAStack->getCurrentDirective()); 2985 if (!S.isUsable()) { 2986 ErrorFound = true; 2987 return StmtError(); 2988 } 2989 2990 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2991 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 2992 OMPOrderedClause *OC = nullptr; 2993 OMPScheduleClause *SC = nullptr; 2994 SmallVector<const OMPLinearClause *, 4> LCs; 2995 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 2996 // This is required for proper codegen. 2997 for (OMPClause *Clause : Clauses) { 2998 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 2999 Clause->getClauseKind() == OMPC_in_reduction) { 3000 // Capture taskgroup task_reduction descriptors inside the tasking regions 3001 // with the corresponding in_reduction items. 3002 auto *IRC = cast<OMPInReductionClause>(Clause); 3003 for (Expr *E : IRC->taskgroup_descriptors()) 3004 if (E) 3005 MarkDeclarationsReferencedInExpr(E); 3006 } 3007 if (isOpenMPPrivate(Clause->getClauseKind()) || 3008 Clause->getClauseKind() == OMPC_copyprivate || 3009 (getLangOpts().OpenMPUseTLS && 3010 getASTContext().getTargetInfo().isTLSSupported() && 3011 Clause->getClauseKind() == OMPC_copyin)) { 3012 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 3013 // Mark all variables in private list clauses as used in inner region. 3014 for (Stmt *VarRef : Clause->children()) { 3015 if (auto *E = cast_or_null<Expr>(VarRef)) { 3016 MarkDeclarationsReferencedInExpr(E); 3017 } 3018 } 3019 DSAStack->setForceVarCapturing(/*V=*/false); 3020 } else if (CaptureRegions.size() > 1 || 3021 CaptureRegions.back() != OMPD_unknown) { 3022 if (auto *C = OMPClauseWithPreInit::get(Clause)) 3023 PICs.push_back(C); 3024 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 3025 if (Expr *E = C->getPostUpdateExpr()) 3026 MarkDeclarationsReferencedInExpr(E); 3027 } 3028 } 3029 if (Clause->getClauseKind() == OMPC_schedule) 3030 SC = cast<OMPScheduleClause>(Clause); 3031 else if (Clause->getClauseKind() == OMPC_ordered) 3032 OC = cast<OMPOrderedClause>(Clause); 3033 else if (Clause->getClauseKind() == OMPC_linear) 3034 LCs.push_back(cast<OMPLinearClause>(Clause)); 3035 } 3036 // OpenMP, 2.7.1 Loop Construct, Restrictions 3037 // The nonmonotonic modifier cannot be specified if an ordered clause is 3038 // specified. 3039 if (SC && 3040 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 3041 SC->getSecondScheduleModifier() == 3042 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 3043 OC) { 3044 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 3045 ? SC->getFirstScheduleModifierLoc() 3046 : SC->getSecondScheduleModifierLoc(), 3047 diag::err_omp_schedule_nonmonotonic_ordered) 3048 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3049 ErrorFound = true; 3050 } 3051 if (!LCs.empty() && OC && OC->getNumForLoops()) { 3052 for (const OMPLinearClause *C : LCs) { 3053 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 3054 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3055 } 3056 ErrorFound = true; 3057 } 3058 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 3059 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 3060 OC->getNumForLoops()) { 3061 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 3062 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 3063 ErrorFound = true; 3064 } 3065 if (ErrorFound) { 3066 return StmtError(); 3067 } 3068 StmtResult SR = S; 3069 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 3070 // Mark all variables in private list clauses as used in inner region. 3071 // Required for proper codegen of combined directives. 3072 // TODO: add processing for other clauses. 3073 if (ThisCaptureRegion != OMPD_unknown) { 3074 for (const clang::OMPClauseWithPreInit *C : PICs) { 3075 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 3076 // Find the particular capture region for the clause if the 3077 // directive is a combined one with multiple capture regions. 3078 // If the directive is not a combined one, the capture region 3079 // associated with the clause is OMPD_unknown and is generated 3080 // only once. 3081 if (CaptureRegion == ThisCaptureRegion || 3082 CaptureRegion == OMPD_unknown) { 3083 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 3084 for (Decl *D : DS->decls()) 3085 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 3086 } 3087 } 3088 } 3089 } 3090 SR = ActOnCapturedRegionEnd(SR.get()); 3091 } 3092 return SR; 3093 } 3094 3095 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 3096 OpenMPDirectiveKind CancelRegion, 3097 SourceLocation StartLoc) { 3098 // CancelRegion is only needed for cancel and cancellation_point. 3099 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 3100 return false; 3101 3102 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 3103 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 3104 return false; 3105 3106 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 3107 << getOpenMPDirectiveName(CancelRegion); 3108 return true; 3109 } 3110 3111 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 3112 OpenMPDirectiveKind CurrentRegion, 3113 const DeclarationNameInfo &CurrentName, 3114 OpenMPDirectiveKind CancelRegion, 3115 SourceLocation StartLoc) { 3116 if (Stack->getCurScope()) { 3117 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 3118 OpenMPDirectiveKind OffendingRegion = ParentRegion; 3119 bool NestingProhibited = false; 3120 bool CloseNesting = true; 3121 bool OrphanSeen = false; 3122 enum { 3123 NoRecommend, 3124 ShouldBeInParallelRegion, 3125 ShouldBeInOrderedRegion, 3126 ShouldBeInTargetRegion, 3127 ShouldBeInTeamsRegion 3128 } Recommend = NoRecommend; 3129 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 3130 // OpenMP [2.16, Nesting of Regions] 3131 // OpenMP constructs may not be nested inside a simd region. 3132 // OpenMP [2.8.1,simd Construct, Restrictions] 3133 // An ordered construct with the simd clause is the only OpenMP 3134 // construct that can appear in the simd region. 3135 // Allowing a SIMD construct nested in another SIMD construct is an 3136 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 3137 // message. 3138 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 3139 ? diag::err_omp_prohibited_region_simd 3140 : diag::warn_omp_nesting_simd); 3141 return CurrentRegion != OMPD_simd; 3142 } 3143 if (ParentRegion == OMPD_atomic) { 3144 // OpenMP [2.16, Nesting of Regions] 3145 // OpenMP constructs may not be nested inside an atomic region. 3146 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 3147 return true; 3148 } 3149 if (CurrentRegion == OMPD_section) { 3150 // OpenMP [2.7.2, sections Construct, Restrictions] 3151 // Orphaned section directives are prohibited. That is, the section 3152 // directives must appear within the sections construct and must not be 3153 // encountered elsewhere in the sections region. 3154 if (ParentRegion != OMPD_sections && 3155 ParentRegion != OMPD_parallel_sections) { 3156 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 3157 << (ParentRegion != OMPD_unknown) 3158 << getOpenMPDirectiveName(ParentRegion); 3159 return true; 3160 } 3161 return false; 3162 } 3163 // Allow some constructs (except teams and cancellation constructs) to be 3164 // orphaned (they could be used in functions, called from OpenMP regions 3165 // with the required preconditions). 3166 if (ParentRegion == OMPD_unknown && 3167 !isOpenMPNestingTeamsDirective(CurrentRegion) && 3168 CurrentRegion != OMPD_cancellation_point && 3169 CurrentRegion != OMPD_cancel) 3170 return false; 3171 if (CurrentRegion == OMPD_cancellation_point || 3172 CurrentRegion == OMPD_cancel) { 3173 // OpenMP [2.16, Nesting of Regions] 3174 // A cancellation point construct for which construct-type-clause is 3175 // taskgroup must be nested inside a task construct. A cancellation 3176 // point construct for which construct-type-clause is not taskgroup must 3177 // be closely nested inside an OpenMP construct that matches the type 3178 // specified in construct-type-clause. 3179 // A cancel construct for which construct-type-clause is taskgroup must be 3180 // nested inside a task construct. A cancel construct for which 3181 // construct-type-clause is not taskgroup must be closely nested inside an 3182 // OpenMP construct that matches the type specified in 3183 // construct-type-clause. 3184 NestingProhibited = 3185 !((CancelRegion == OMPD_parallel && 3186 (ParentRegion == OMPD_parallel || 3187 ParentRegion == OMPD_target_parallel)) || 3188 (CancelRegion == OMPD_for && 3189 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 3190 ParentRegion == OMPD_target_parallel_for || 3191 ParentRegion == OMPD_distribute_parallel_for || 3192 ParentRegion == OMPD_teams_distribute_parallel_for || 3193 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 3194 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 3195 (CancelRegion == OMPD_sections && 3196 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 3197 ParentRegion == OMPD_parallel_sections))); 3198 OrphanSeen = ParentRegion == OMPD_unknown; 3199 } else if (CurrentRegion == OMPD_master) { 3200 // OpenMP [2.16, Nesting of Regions] 3201 // A master region may not be closely nested inside a worksharing, 3202 // atomic, or explicit task region. 3203 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3204 isOpenMPTaskingDirective(ParentRegion); 3205 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 3206 // OpenMP [2.16, Nesting of Regions] 3207 // A critical region may not be nested (closely or otherwise) inside a 3208 // critical region with the same name. Note that this restriction is not 3209 // sufficient to prevent deadlock. 3210 SourceLocation PreviousCriticalLoc; 3211 bool DeadLock = Stack->hasDirective( 3212 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 3213 const DeclarationNameInfo &DNI, 3214 SourceLocation Loc) { 3215 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 3216 PreviousCriticalLoc = Loc; 3217 return true; 3218 } 3219 return false; 3220 }, 3221 false /* skip top directive */); 3222 if (DeadLock) { 3223 SemaRef.Diag(StartLoc, 3224 diag::err_omp_prohibited_region_critical_same_name) 3225 << CurrentName.getName(); 3226 if (PreviousCriticalLoc.isValid()) 3227 SemaRef.Diag(PreviousCriticalLoc, 3228 diag::note_omp_previous_critical_region); 3229 return true; 3230 } 3231 } else if (CurrentRegion == OMPD_barrier) { 3232 // OpenMP [2.16, Nesting of Regions] 3233 // A barrier region may not be closely nested inside a worksharing, 3234 // explicit task, critical, ordered, atomic, or master region. 3235 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3236 isOpenMPTaskingDirective(ParentRegion) || 3237 ParentRegion == OMPD_master || 3238 ParentRegion == OMPD_critical || 3239 ParentRegion == OMPD_ordered; 3240 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 3241 !isOpenMPParallelDirective(CurrentRegion) && 3242 !isOpenMPTeamsDirective(CurrentRegion)) { 3243 // OpenMP [2.16, Nesting of Regions] 3244 // A worksharing region may not be closely nested inside a worksharing, 3245 // explicit task, critical, ordered, atomic, or master region. 3246 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3247 isOpenMPTaskingDirective(ParentRegion) || 3248 ParentRegion == OMPD_master || 3249 ParentRegion == OMPD_critical || 3250 ParentRegion == OMPD_ordered; 3251 Recommend = ShouldBeInParallelRegion; 3252 } else if (CurrentRegion == OMPD_ordered) { 3253 // OpenMP [2.16, Nesting of Regions] 3254 // An ordered region may not be closely nested inside a critical, 3255 // atomic, or explicit task region. 3256 // An ordered region must be closely nested inside a loop region (or 3257 // parallel loop region) with an ordered clause. 3258 // OpenMP [2.8.1,simd Construct, Restrictions] 3259 // An ordered construct with the simd clause is the only OpenMP construct 3260 // that can appear in the simd region. 3261 NestingProhibited = ParentRegion == OMPD_critical || 3262 isOpenMPTaskingDirective(ParentRegion) || 3263 !(isOpenMPSimdDirective(ParentRegion) || 3264 Stack->isParentOrderedRegion()); 3265 Recommend = ShouldBeInOrderedRegion; 3266 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 3267 // OpenMP [2.16, Nesting of Regions] 3268 // If specified, a teams construct must be contained within a target 3269 // construct. 3270 NestingProhibited = ParentRegion != OMPD_target; 3271 OrphanSeen = ParentRegion == OMPD_unknown; 3272 Recommend = ShouldBeInTargetRegion; 3273 } 3274 if (!NestingProhibited && 3275 !isOpenMPTargetExecutionDirective(CurrentRegion) && 3276 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 3277 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 3278 // OpenMP [2.16, Nesting of Regions] 3279 // distribute, parallel, parallel sections, parallel workshare, and the 3280 // parallel loop and parallel loop SIMD constructs are the only OpenMP 3281 // constructs that can be closely nested in the teams region. 3282 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 3283 !isOpenMPDistributeDirective(CurrentRegion); 3284 Recommend = ShouldBeInParallelRegion; 3285 } 3286 if (!NestingProhibited && 3287 isOpenMPNestingDistributeDirective(CurrentRegion)) { 3288 // OpenMP 4.5 [2.17 Nesting of Regions] 3289 // The region associated with the distribute construct must be strictly 3290 // nested inside a teams region 3291 NestingProhibited = 3292 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 3293 Recommend = ShouldBeInTeamsRegion; 3294 } 3295 if (!NestingProhibited && 3296 (isOpenMPTargetExecutionDirective(CurrentRegion) || 3297 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 3298 // OpenMP 4.5 [2.17 Nesting of Regions] 3299 // If a target, target update, target data, target enter data, or 3300 // target exit data construct is encountered during execution of a 3301 // target region, the behavior is unspecified. 3302 NestingProhibited = Stack->hasDirective( 3303 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 3304 SourceLocation) { 3305 if (isOpenMPTargetExecutionDirective(K)) { 3306 OffendingRegion = K; 3307 return true; 3308 } 3309 return false; 3310 }, 3311 false /* don't skip top directive */); 3312 CloseNesting = false; 3313 } 3314 if (NestingProhibited) { 3315 if (OrphanSeen) { 3316 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 3317 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 3318 } else { 3319 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 3320 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 3321 << Recommend << getOpenMPDirectiveName(CurrentRegion); 3322 } 3323 return true; 3324 } 3325 } 3326 return false; 3327 } 3328 3329 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 3330 ArrayRef<OMPClause *> Clauses, 3331 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 3332 bool ErrorFound = false; 3333 unsigned NamedModifiersNumber = 0; 3334 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 3335 OMPD_unknown + 1); 3336 SmallVector<SourceLocation, 4> NameModifierLoc; 3337 for (const OMPClause *C : Clauses) { 3338 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 3339 // At most one if clause without a directive-name-modifier can appear on 3340 // the directive. 3341 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 3342 if (FoundNameModifiers[CurNM]) { 3343 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 3344 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 3345 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 3346 ErrorFound = true; 3347 } else if (CurNM != OMPD_unknown) { 3348 NameModifierLoc.push_back(IC->getNameModifierLoc()); 3349 ++NamedModifiersNumber; 3350 } 3351 FoundNameModifiers[CurNM] = IC; 3352 if (CurNM == OMPD_unknown) 3353 continue; 3354 // Check if the specified name modifier is allowed for the current 3355 // directive. 3356 // At most one if clause with the particular directive-name-modifier can 3357 // appear on the directive. 3358 bool MatchFound = false; 3359 for (auto NM : AllowedNameModifiers) { 3360 if (CurNM == NM) { 3361 MatchFound = true; 3362 break; 3363 } 3364 } 3365 if (!MatchFound) { 3366 S.Diag(IC->getNameModifierLoc(), 3367 diag::err_omp_wrong_if_directive_name_modifier) 3368 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3369 ErrorFound = true; 3370 } 3371 } 3372 } 3373 // If any if clause on the directive includes a directive-name-modifier then 3374 // all if clauses on the directive must include a directive-name-modifier. 3375 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3376 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3377 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 3378 diag::err_omp_no_more_if_clause); 3379 } else { 3380 std::string Values; 3381 std::string Sep(", "); 3382 unsigned AllowedCnt = 0; 3383 unsigned TotalAllowedNum = 3384 AllowedNameModifiers.size() - NamedModifiersNumber; 3385 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3386 ++Cnt) { 3387 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3388 if (!FoundNameModifiers[NM]) { 3389 Values += "'"; 3390 Values += getOpenMPDirectiveName(NM); 3391 Values += "'"; 3392 if (AllowedCnt + 2 == TotalAllowedNum) 3393 Values += " or "; 3394 else if (AllowedCnt + 1 != TotalAllowedNum) 3395 Values += Sep; 3396 ++AllowedCnt; 3397 } 3398 } 3399 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 3400 diag::err_omp_unnamed_if_clause) 3401 << (TotalAllowedNum > 1) << Values; 3402 } 3403 for (SourceLocation Loc : NameModifierLoc) { 3404 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3405 } 3406 ErrorFound = true; 3407 } 3408 return ErrorFound; 3409 } 3410 3411 StmtResult Sema::ActOnOpenMPExecutableDirective( 3412 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3413 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3414 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3415 StmtResult Res = StmtError(); 3416 // First check CancelRegion which is then used in checkNestingOfRegions. 3417 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3418 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3419 StartLoc)) 3420 return StmtError(); 3421 3422 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3423 VarsWithInheritedDSAType VarsWithInheritedDSA; 3424 bool ErrorFound = false; 3425 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3426 if (AStmt && !CurContext->isDependentContext()) { 3427 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3428 3429 // Check default data sharing attributes for referenced variables. 3430 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3431 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3432 Stmt *S = AStmt; 3433 while (--ThisCaptureLevel >= 0) 3434 S = cast<CapturedStmt>(S)->getCapturedStmt(); 3435 DSAChecker.Visit(S); 3436 if (DSAChecker.isErrorFound()) 3437 return StmtError(); 3438 // Generate list of implicitly defined firstprivate variables. 3439 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3440 3441 SmallVector<Expr *, 4> ImplicitFirstprivates( 3442 DSAChecker.getImplicitFirstprivate().begin(), 3443 DSAChecker.getImplicitFirstprivate().end()); 3444 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 3445 DSAChecker.getImplicitMap().end()); 3446 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 3447 for (OMPClause *C : Clauses) { 3448 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 3449 for (Expr *E : IRC->taskgroup_descriptors()) 3450 if (E) 3451 ImplicitFirstprivates.emplace_back(E); 3452 } 3453 } 3454 if (!ImplicitFirstprivates.empty()) { 3455 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3456 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 3457 SourceLocation())) { 3458 ClausesWithImplicit.push_back(Implicit); 3459 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3460 ImplicitFirstprivates.size(); 3461 } else { 3462 ErrorFound = true; 3463 } 3464 } 3465 if (!ImplicitMaps.empty()) { 3466 CXXScopeSpec MapperIdScopeSpec; 3467 DeclarationNameInfo MapperId; 3468 if (OMPClause *Implicit = ActOnOpenMPMapClause( 3469 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, 3470 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(), 3471 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) { 3472 ClausesWithImplicit.emplace_back(Implicit); 3473 ErrorFound |= 3474 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 3475 } else { 3476 ErrorFound = true; 3477 } 3478 } 3479 } 3480 3481 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3482 switch (Kind) { 3483 case OMPD_parallel: 3484 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3485 EndLoc); 3486 AllowedNameModifiers.push_back(OMPD_parallel); 3487 break; 3488 case OMPD_simd: 3489 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3490 VarsWithInheritedDSA); 3491 break; 3492 case OMPD_for: 3493 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3494 VarsWithInheritedDSA); 3495 break; 3496 case OMPD_for_simd: 3497 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3498 EndLoc, VarsWithInheritedDSA); 3499 break; 3500 case OMPD_sections: 3501 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3502 EndLoc); 3503 break; 3504 case OMPD_section: 3505 assert(ClausesWithImplicit.empty() && 3506 "No clauses are allowed for 'omp section' directive"); 3507 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3508 break; 3509 case OMPD_single: 3510 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3511 EndLoc); 3512 break; 3513 case OMPD_master: 3514 assert(ClausesWithImplicit.empty() && 3515 "No clauses are allowed for 'omp master' directive"); 3516 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3517 break; 3518 case OMPD_critical: 3519 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3520 StartLoc, EndLoc); 3521 break; 3522 case OMPD_parallel_for: 3523 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3524 EndLoc, VarsWithInheritedDSA); 3525 AllowedNameModifiers.push_back(OMPD_parallel); 3526 break; 3527 case OMPD_parallel_for_simd: 3528 Res = ActOnOpenMPParallelForSimdDirective( 3529 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3530 AllowedNameModifiers.push_back(OMPD_parallel); 3531 break; 3532 case OMPD_parallel_sections: 3533 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3534 StartLoc, EndLoc); 3535 AllowedNameModifiers.push_back(OMPD_parallel); 3536 break; 3537 case OMPD_task: 3538 Res = 3539 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3540 AllowedNameModifiers.push_back(OMPD_task); 3541 break; 3542 case OMPD_taskyield: 3543 assert(ClausesWithImplicit.empty() && 3544 "No clauses are allowed for 'omp taskyield' directive"); 3545 assert(AStmt == nullptr && 3546 "No associated statement allowed for 'omp taskyield' directive"); 3547 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3548 break; 3549 case OMPD_barrier: 3550 assert(ClausesWithImplicit.empty() && 3551 "No clauses are allowed for 'omp barrier' directive"); 3552 assert(AStmt == nullptr && 3553 "No associated statement allowed for 'omp barrier' directive"); 3554 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 3555 break; 3556 case OMPD_taskwait: 3557 assert(ClausesWithImplicit.empty() && 3558 "No clauses are allowed for 'omp taskwait' directive"); 3559 assert(AStmt == nullptr && 3560 "No associated statement allowed for 'omp taskwait' directive"); 3561 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 3562 break; 3563 case OMPD_taskgroup: 3564 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 3565 EndLoc); 3566 break; 3567 case OMPD_flush: 3568 assert(AStmt == nullptr && 3569 "No associated statement allowed for 'omp flush' directive"); 3570 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 3571 break; 3572 case OMPD_ordered: 3573 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 3574 EndLoc); 3575 break; 3576 case OMPD_atomic: 3577 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 3578 EndLoc); 3579 break; 3580 case OMPD_teams: 3581 Res = 3582 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3583 break; 3584 case OMPD_target: 3585 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 3586 EndLoc); 3587 AllowedNameModifiers.push_back(OMPD_target); 3588 break; 3589 case OMPD_target_parallel: 3590 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 3591 StartLoc, EndLoc); 3592 AllowedNameModifiers.push_back(OMPD_target); 3593 AllowedNameModifiers.push_back(OMPD_parallel); 3594 break; 3595 case OMPD_target_parallel_for: 3596 Res = ActOnOpenMPTargetParallelForDirective( 3597 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3598 AllowedNameModifiers.push_back(OMPD_target); 3599 AllowedNameModifiers.push_back(OMPD_parallel); 3600 break; 3601 case OMPD_cancellation_point: 3602 assert(ClausesWithImplicit.empty() && 3603 "No clauses are allowed for 'omp cancellation point' directive"); 3604 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 3605 "cancellation point' directive"); 3606 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 3607 break; 3608 case OMPD_cancel: 3609 assert(AStmt == nullptr && 3610 "No associated statement allowed for 'omp cancel' directive"); 3611 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 3612 CancelRegion); 3613 AllowedNameModifiers.push_back(OMPD_cancel); 3614 break; 3615 case OMPD_target_data: 3616 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 3617 EndLoc); 3618 AllowedNameModifiers.push_back(OMPD_target_data); 3619 break; 3620 case OMPD_target_enter_data: 3621 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 3622 EndLoc, AStmt); 3623 AllowedNameModifiers.push_back(OMPD_target_enter_data); 3624 break; 3625 case OMPD_target_exit_data: 3626 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 3627 EndLoc, AStmt); 3628 AllowedNameModifiers.push_back(OMPD_target_exit_data); 3629 break; 3630 case OMPD_taskloop: 3631 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 3632 EndLoc, VarsWithInheritedDSA); 3633 AllowedNameModifiers.push_back(OMPD_taskloop); 3634 break; 3635 case OMPD_taskloop_simd: 3636 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3637 EndLoc, VarsWithInheritedDSA); 3638 AllowedNameModifiers.push_back(OMPD_taskloop); 3639 break; 3640 case OMPD_distribute: 3641 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 3642 EndLoc, VarsWithInheritedDSA); 3643 break; 3644 case OMPD_target_update: 3645 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 3646 EndLoc, AStmt); 3647 AllowedNameModifiers.push_back(OMPD_target_update); 3648 break; 3649 case OMPD_distribute_parallel_for: 3650 Res = ActOnOpenMPDistributeParallelForDirective( 3651 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3652 AllowedNameModifiers.push_back(OMPD_parallel); 3653 break; 3654 case OMPD_distribute_parallel_for_simd: 3655 Res = ActOnOpenMPDistributeParallelForSimdDirective( 3656 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3657 AllowedNameModifiers.push_back(OMPD_parallel); 3658 break; 3659 case OMPD_distribute_simd: 3660 Res = ActOnOpenMPDistributeSimdDirective( 3661 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3662 break; 3663 case OMPD_target_parallel_for_simd: 3664 Res = ActOnOpenMPTargetParallelForSimdDirective( 3665 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3666 AllowedNameModifiers.push_back(OMPD_target); 3667 AllowedNameModifiers.push_back(OMPD_parallel); 3668 break; 3669 case OMPD_target_simd: 3670 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3671 EndLoc, VarsWithInheritedDSA); 3672 AllowedNameModifiers.push_back(OMPD_target); 3673 break; 3674 case OMPD_teams_distribute: 3675 Res = ActOnOpenMPTeamsDistributeDirective( 3676 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3677 break; 3678 case OMPD_teams_distribute_simd: 3679 Res = ActOnOpenMPTeamsDistributeSimdDirective( 3680 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3681 break; 3682 case OMPD_teams_distribute_parallel_for_simd: 3683 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 3684 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3685 AllowedNameModifiers.push_back(OMPD_parallel); 3686 break; 3687 case OMPD_teams_distribute_parallel_for: 3688 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 3689 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3690 AllowedNameModifiers.push_back(OMPD_parallel); 3691 break; 3692 case OMPD_target_teams: 3693 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 3694 EndLoc); 3695 AllowedNameModifiers.push_back(OMPD_target); 3696 break; 3697 case OMPD_target_teams_distribute: 3698 Res = ActOnOpenMPTargetTeamsDistributeDirective( 3699 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3700 AllowedNameModifiers.push_back(OMPD_target); 3701 break; 3702 case OMPD_target_teams_distribute_parallel_for: 3703 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 3704 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3705 AllowedNameModifiers.push_back(OMPD_target); 3706 AllowedNameModifiers.push_back(OMPD_parallel); 3707 break; 3708 case OMPD_target_teams_distribute_parallel_for_simd: 3709 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 3710 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3711 AllowedNameModifiers.push_back(OMPD_target); 3712 AllowedNameModifiers.push_back(OMPD_parallel); 3713 break; 3714 case OMPD_target_teams_distribute_simd: 3715 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 3716 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3717 AllowedNameModifiers.push_back(OMPD_target); 3718 break; 3719 case OMPD_declare_target: 3720 case OMPD_end_declare_target: 3721 case OMPD_threadprivate: 3722 case OMPD_declare_reduction: 3723 case OMPD_declare_mapper: 3724 case OMPD_declare_simd: 3725 case OMPD_requires: 3726 llvm_unreachable("OpenMP Directive is not allowed"); 3727 case OMPD_unknown: 3728 llvm_unreachable("Unknown OpenMP directive"); 3729 } 3730 3731 for (const auto &P : VarsWithInheritedDSA) { 3732 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 3733 << P.first << P.second->getSourceRange(); 3734 } 3735 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 3736 3737 if (!AllowedNameModifiers.empty()) 3738 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 3739 ErrorFound; 3740 3741 if (ErrorFound) 3742 return StmtError(); 3743 return Res; 3744 } 3745 3746 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 3747 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 3748 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 3749 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 3750 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 3751 assert(Aligneds.size() == Alignments.size()); 3752 assert(Linears.size() == LinModifiers.size()); 3753 assert(Linears.size() == Steps.size()); 3754 if (!DG || DG.get().isNull()) 3755 return DeclGroupPtrTy(); 3756 3757 if (!DG.get().isSingleDecl()) { 3758 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 3759 return DG; 3760 } 3761 Decl *ADecl = DG.get().getSingleDecl(); 3762 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 3763 ADecl = FTD->getTemplatedDecl(); 3764 3765 auto *FD = dyn_cast<FunctionDecl>(ADecl); 3766 if (!FD) { 3767 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 3768 return DeclGroupPtrTy(); 3769 } 3770 3771 // OpenMP [2.8.2, declare simd construct, Description] 3772 // The parameter of the simdlen clause must be a constant positive integer 3773 // expression. 3774 ExprResult SL; 3775 if (Simdlen) 3776 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 3777 // OpenMP [2.8.2, declare simd construct, Description] 3778 // The special this pointer can be used as if was one of the arguments to the 3779 // function in any of the linear, aligned, or uniform clauses. 3780 // The uniform clause declares one or more arguments to have an invariant 3781 // value for all concurrent invocations of the function in the execution of a 3782 // single SIMD loop. 3783 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 3784 const Expr *UniformedLinearThis = nullptr; 3785 for (const Expr *E : Uniforms) { 3786 E = E->IgnoreParenImpCasts(); 3787 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3788 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 3789 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3790 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3791 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 3792 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 3793 continue; 3794 } 3795 if (isa<CXXThisExpr>(E)) { 3796 UniformedLinearThis = E; 3797 continue; 3798 } 3799 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3800 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3801 } 3802 // OpenMP [2.8.2, declare simd construct, Description] 3803 // The aligned clause declares that the object to which each list item points 3804 // is aligned to the number of bytes expressed in the optional parameter of 3805 // the aligned clause. 3806 // The special this pointer can be used as if was one of the arguments to the 3807 // function in any of the linear, aligned, or uniform clauses. 3808 // The type of list items appearing in the aligned clause must be array, 3809 // pointer, reference to array, or reference to pointer. 3810 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 3811 const Expr *AlignedThis = nullptr; 3812 for (const Expr *E : Aligneds) { 3813 E = E->IgnoreParenImpCasts(); 3814 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3815 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3816 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3817 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3818 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3819 ->getCanonicalDecl() == CanonPVD) { 3820 // OpenMP [2.8.1, simd construct, Restrictions] 3821 // A list-item cannot appear in more than one aligned clause. 3822 if (AlignedArgs.count(CanonPVD) > 0) { 3823 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3824 << 1 << E->getSourceRange(); 3825 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 3826 diag::note_omp_explicit_dsa) 3827 << getOpenMPClauseName(OMPC_aligned); 3828 continue; 3829 } 3830 AlignedArgs[CanonPVD] = E; 3831 QualType QTy = PVD->getType() 3832 .getNonReferenceType() 3833 .getUnqualifiedType() 3834 .getCanonicalType(); 3835 const Type *Ty = QTy.getTypePtrOrNull(); 3836 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 3837 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 3838 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 3839 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 3840 } 3841 continue; 3842 } 3843 } 3844 if (isa<CXXThisExpr>(E)) { 3845 if (AlignedThis) { 3846 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3847 << 2 << E->getSourceRange(); 3848 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 3849 << getOpenMPClauseName(OMPC_aligned); 3850 } 3851 AlignedThis = E; 3852 continue; 3853 } 3854 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3855 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3856 } 3857 // The optional parameter of the aligned clause, alignment, must be a constant 3858 // positive integer expression. If no optional parameter is specified, 3859 // implementation-defined default alignments for SIMD instructions on the 3860 // target platforms are assumed. 3861 SmallVector<const Expr *, 4> NewAligns; 3862 for (Expr *E : Alignments) { 3863 ExprResult Align; 3864 if (E) 3865 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 3866 NewAligns.push_back(Align.get()); 3867 } 3868 // OpenMP [2.8.2, declare simd construct, Description] 3869 // The linear clause declares one or more list items to be private to a SIMD 3870 // lane and to have a linear relationship with respect to the iteration space 3871 // of a loop. 3872 // The special this pointer can be used as if was one of the arguments to the 3873 // function in any of the linear, aligned, or uniform clauses. 3874 // When a linear-step expression is specified in a linear clause it must be 3875 // either a constant integer expression or an integer-typed parameter that is 3876 // specified in a uniform clause on the directive. 3877 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 3878 const bool IsUniformedThis = UniformedLinearThis != nullptr; 3879 auto MI = LinModifiers.begin(); 3880 for (const Expr *E : Linears) { 3881 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 3882 ++MI; 3883 E = E->IgnoreParenImpCasts(); 3884 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3885 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3886 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3887 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3888 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3889 ->getCanonicalDecl() == CanonPVD) { 3890 // OpenMP [2.15.3.7, linear Clause, Restrictions] 3891 // A list-item cannot appear in more than one linear clause. 3892 if (LinearArgs.count(CanonPVD) > 0) { 3893 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3894 << getOpenMPClauseName(OMPC_linear) 3895 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 3896 Diag(LinearArgs[CanonPVD]->getExprLoc(), 3897 diag::note_omp_explicit_dsa) 3898 << getOpenMPClauseName(OMPC_linear); 3899 continue; 3900 } 3901 // Each argument can appear in at most one uniform or linear clause. 3902 if (UniformedArgs.count(CanonPVD) > 0) { 3903 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3904 << getOpenMPClauseName(OMPC_linear) 3905 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 3906 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 3907 diag::note_omp_explicit_dsa) 3908 << getOpenMPClauseName(OMPC_uniform); 3909 continue; 3910 } 3911 LinearArgs[CanonPVD] = E; 3912 if (E->isValueDependent() || E->isTypeDependent() || 3913 E->isInstantiationDependent() || 3914 E->containsUnexpandedParameterPack()) 3915 continue; 3916 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 3917 PVD->getOriginalType()); 3918 continue; 3919 } 3920 } 3921 if (isa<CXXThisExpr>(E)) { 3922 if (UniformedLinearThis) { 3923 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3924 << getOpenMPClauseName(OMPC_linear) 3925 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 3926 << E->getSourceRange(); 3927 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 3928 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 3929 : OMPC_linear); 3930 continue; 3931 } 3932 UniformedLinearThis = E; 3933 if (E->isValueDependent() || E->isTypeDependent() || 3934 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3935 continue; 3936 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 3937 E->getType()); 3938 continue; 3939 } 3940 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3941 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3942 } 3943 Expr *Step = nullptr; 3944 Expr *NewStep = nullptr; 3945 SmallVector<Expr *, 4> NewSteps; 3946 for (Expr *E : Steps) { 3947 // Skip the same step expression, it was checked already. 3948 if (Step == E || !E) { 3949 NewSteps.push_back(E ? NewStep : nullptr); 3950 continue; 3951 } 3952 Step = E; 3953 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 3954 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3955 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3956 if (UniformedArgs.count(CanonPVD) == 0) { 3957 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 3958 << Step->getSourceRange(); 3959 } else if (E->isValueDependent() || E->isTypeDependent() || 3960 E->isInstantiationDependent() || 3961 E->containsUnexpandedParameterPack() || 3962 CanonPVD->getType()->hasIntegerRepresentation()) { 3963 NewSteps.push_back(Step); 3964 } else { 3965 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 3966 << Step->getSourceRange(); 3967 } 3968 continue; 3969 } 3970 NewStep = Step; 3971 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3972 !Step->isInstantiationDependent() && 3973 !Step->containsUnexpandedParameterPack()) { 3974 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 3975 .get(); 3976 if (NewStep) 3977 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 3978 } 3979 NewSteps.push_back(NewStep); 3980 } 3981 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 3982 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 3983 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 3984 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 3985 const_cast<Expr **>(Linears.data()), Linears.size(), 3986 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 3987 NewSteps.data(), NewSteps.size(), SR); 3988 ADecl->addAttr(NewAttr); 3989 return ConvertDeclToDeclGroup(ADecl); 3990 } 3991 3992 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 3993 Stmt *AStmt, 3994 SourceLocation StartLoc, 3995 SourceLocation EndLoc) { 3996 if (!AStmt) 3997 return StmtError(); 3998 3999 auto *CS = cast<CapturedStmt>(AStmt); 4000 // 1.2.2 OpenMP Language Terminology 4001 // Structured block - An executable statement with a single entry at the 4002 // top and a single exit at the bottom. 4003 // The point of exit cannot be a branch out of the structured block. 4004 // longjmp() and throw() must not violate the entry/exit criteria. 4005 CS->getCapturedDecl()->setNothrow(); 4006 4007 setFunctionHasBranchProtectedScope(); 4008 4009 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4010 DSAStack->isCancelRegion()); 4011 } 4012 4013 namespace { 4014 /// Helper class for checking canonical form of the OpenMP loops and 4015 /// extracting iteration space of each loop in the loop nest, that will be used 4016 /// for IR generation. 4017 class OpenMPIterationSpaceChecker { 4018 /// Reference to Sema. 4019 Sema &SemaRef; 4020 /// A location for diagnostics (when there is no some better location). 4021 SourceLocation DefaultLoc; 4022 /// A location for diagnostics (when increment is not compatible). 4023 SourceLocation ConditionLoc; 4024 /// A source location for referring to loop init later. 4025 SourceRange InitSrcRange; 4026 /// A source location for referring to condition later. 4027 SourceRange ConditionSrcRange; 4028 /// A source location for referring to increment later. 4029 SourceRange IncrementSrcRange; 4030 /// Loop variable. 4031 ValueDecl *LCDecl = nullptr; 4032 /// Reference to loop variable. 4033 Expr *LCRef = nullptr; 4034 /// Lower bound (initializer for the var). 4035 Expr *LB = nullptr; 4036 /// Upper bound. 4037 Expr *UB = nullptr; 4038 /// Loop step (increment). 4039 Expr *Step = nullptr; 4040 /// This flag is true when condition is one of: 4041 /// Var < UB 4042 /// Var <= UB 4043 /// UB > Var 4044 /// UB >= Var 4045 /// This will have no value when the condition is != 4046 llvm::Optional<bool> TestIsLessOp; 4047 /// This flag is true when condition is strict ( < or > ). 4048 bool TestIsStrictOp = false; 4049 /// This flag is true when step is subtracted on each iteration. 4050 bool SubtractStep = false; 4051 4052 public: 4053 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 4054 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 4055 /// Check init-expr for canonical loop form and save loop counter 4056 /// variable - #Var and its initialization value - #LB. 4057 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 4058 /// Check test-expr for canonical form, save upper-bound (#UB), flags 4059 /// for less/greater and for strict/non-strict comparison. 4060 bool checkAndSetCond(Expr *S); 4061 /// Check incr-expr for canonical loop form and return true if it 4062 /// does not conform, otherwise save loop step (#Step). 4063 bool checkAndSetInc(Expr *S); 4064 /// Return the loop counter variable. 4065 ValueDecl *getLoopDecl() const { return LCDecl; } 4066 /// Return the reference expression to loop counter variable. 4067 Expr *getLoopDeclRefExpr() const { return LCRef; } 4068 /// Source range of the loop init. 4069 SourceRange getInitSrcRange() const { return InitSrcRange; } 4070 /// Source range of the loop condition. 4071 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 4072 /// Source range of the loop increment. 4073 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 4074 /// True if the step should be subtracted. 4075 bool shouldSubtractStep() const { return SubtractStep; } 4076 /// True, if the compare operator is strict (<, > or !=). 4077 bool isStrictTestOp() const { return TestIsStrictOp; } 4078 /// Build the expression to calculate the number of iterations. 4079 Expr *buildNumIterations( 4080 Scope *S, const bool LimitedType, 4081 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4082 /// Build the precondition expression for the loops. 4083 Expr * 4084 buildPreCond(Scope *S, Expr *Cond, 4085 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4086 /// Build reference expression to the counter be used for codegen. 4087 DeclRefExpr * 4088 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4089 DSAStackTy &DSA) const; 4090 /// Build reference expression to the private counter be used for 4091 /// codegen. 4092 Expr *buildPrivateCounterVar() const; 4093 /// Build initialization of the counter be used for codegen. 4094 Expr *buildCounterInit() const; 4095 /// Build step of the counter be used for codegen. 4096 Expr *buildCounterStep() const; 4097 /// Build loop data with counter value for depend clauses in ordered 4098 /// directives. 4099 Expr * 4100 buildOrderedLoopData(Scope *S, Expr *Counter, 4101 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4102 SourceLocation Loc, Expr *Inc = nullptr, 4103 OverloadedOperatorKind OOK = OO_Amp); 4104 /// Return true if any expression is dependent. 4105 bool dependent() const; 4106 4107 private: 4108 /// Check the right-hand side of an assignment in the increment 4109 /// expression. 4110 bool checkAndSetIncRHS(Expr *RHS); 4111 /// Helper to set loop counter variable and its initializer. 4112 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 4113 /// Helper to set upper bound. 4114 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 4115 SourceRange SR, SourceLocation SL); 4116 /// Helper to set loop increment. 4117 bool setStep(Expr *NewStep, bool Subtract); 4118 }; 4119 4120 bool OpenMPIterationSpaceChecker::dependent() const { 4121 if (!LCDecl) { 4122 assert(!LB && !UB && !Step); 4123 return false; 4124 } 4125 return LCDecl->getType()->isDependentType() || 4126 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 4127 (Step && Step->isValueDependent()); 4128 } 4129 4130 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 4131 Expr *NewLCRefExpr, 4132 Expr *NewLB) { 4133 // State consistency checking to ensure correct usage. 4134 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 4135 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4136 if (!NewLCDecl || !NewLB) 4137 return true; 4138 LCDecl = getCanonicalDecl(NewLCDecl); 4139 LCRef = NewLCRefExpr; 4140 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 4141 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4142 if ((Ctor->isCopyOrMoveConstructor() || 4143 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4144 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4145 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 4146 LB = NewLB; 4147 return false; 4148 } 4149 4150 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 4151 llvm::Optional<bool> LessOp, 4152 bool StrictOp, SourceRange SR, 4153 SourceLocation SL) { 4154 // State consistency checking to ensure correct usage. 4155 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 4156 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4157 if (!NewUB) 4158 return true; 4159 UB = NewUB; 4160 if (LessOp) 4161 TestIsLessOp = LessOp; 4162 TestIsStrictOp = StrictOp; 4163 ConditionSrcRange = SR; 4164 ConditionLoc = SL; 4165 return false; 4166 } 4167 4168 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 4169 // State consistency checking to ensure correct usage. 4170 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 4171 if (!NewStep) 4172 return true; 4173 if (!NewStep->isValueDependent()) { 4174 // Check that the step is integer expression. 4175 SourceLocation StepLoc = NewStep->getBeginLoc(); 4176 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 4177 StepLoc, getExprAsWritten(NewStep)); 4178 if (Val.isInvalid()) 4179 return true; 4180 NewStep = Val.get(); 4181 4182 // OpenMP [2.6, Canonical Loop Form, Restrictions] 4183 // If test-expr is of form var relational-op b and relational-op is < or 4184 // <= then incr-expr must cause var to increase on each iteration of the 4185 // loop. If test-expr is of form var relational-op b and relational-op is 4186 // > or >= then incr-expr must cause var to decrease on each iteration of 4187 // the loop. 4188 // If test-expr is of form b relational-op var and relational-op is < or 4189 // <= then incr-expr must cause var to decrease on each iteration of the 4190 // loop. If test-expr is of form b relational-op var and relational-op is 4191 // > or >= then incr-expr must cause var to increase on each iteration of 4192 // the loop. 4193 llvm::APSInt Result; 4194 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 4195 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 4196 bool IsConstNeg = 4197 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 4198 bool IsConstPos = 4199 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 4200 bool IsConstZero = IsConstant && !Result.getBoolValue(); 4201 4202 // != with increment is treated as <; != with decrement is treated as > 4203 if (!TestIsLessOp.hasValue()) 4204 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 4205 if (UB && (IsConstZero || 4206 (TestIsLessOp.getValue() ? 4207 (IsConstNeg || (IsUnsigned && Subtract)) : 4208 (IsConstPos || (IsUnsigned && !Subtract))))) { 4209 SemaRef.Diag(NewStep->getExprLoc(), 4210 diag::err_omp_loop_incr_not_compatible) 4211 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 4212 SemaRef.Diag(ConditionLoc, 4213 diag::note_omp_loop_cond_requres_compatible_incr) 4214 << TestIsLessOp.getValue() << ConditionSrcRange; 4215 return true; 4216 } 4217 if (TestIsLessOp.getValue() == Subtract) { 4218 NewStep = 4219 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 4220 .get(); 4221 Subtract = !Subtract; 4222 } 4223 } 4224 4225 Step = NewStep; 4226 SubtractStep = Subtract; 4227 return false; 4228 } 4229 4230 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 4231 // Check init-expr for canonical loop form and save loop counter 4232 // variable - #Var and its initialization value - #LB. 4233 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 4234 // var = lb 4235 // integer-type var = lb 4236 // random-access-iterator-type var = lb 4237 // pointer-type var = lb 4238 // 4239 if (!S) { 4240 if (EmitDiags) { 4241 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 4242 } 4243 return true; 4244 } 4245 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4246 if (!ExprTemp->cleanupsHaveSideEffects()) 4247 S = ExprTemp->getSubExpr(); 4248 4249 InitSrcRange = S->getSourceRange(); 4250 if (Expr *E = dyn_cast<Expr>(S)) 4251 S = E->IgnoreParens(); 4252 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4253 if (BO->getOpcode() == BO_Assign) { 4254 Expr *LHS = BO->getLHS()->IgnoreParens(); 4255 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4256 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4257 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4258 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4259 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 4260 } 4261 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4262 if (ME->isArrow() && 4263 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4264 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4265 } 4266 } 4267 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 4268 if (DS->isSingleDecl()) { 4269 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 4270 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 4271 // Accept non-canonical init form here but emit ext. warning. 4272 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 4273 SemaRef.Diag(S->getBeginLoc(), 4274 diag::ext_omp_loop_not_canonical_init) 4275 << S->getSourceRange(); 4276 return setLCDeclAndLB( 4277 Var, 4278 buildDeclRefExpr(SemaRef, Var, 4279 Var->getType().getNonReferenceType(), 4280 DS->getBeginLoc()), 4281 Var->getInit()); 4282 } 4283 } 4284 } 4285 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4286 if (CE->getOperator() == OO_Equal) { 4287 Expr *LHS = CE->getArg(0); 4288 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4289 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4290 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4291 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4292 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 4293 } 4294 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4295 if (ME->isArrow() && 4296 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4297 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4298 } 4299 } 4300 } 4301 4302 if (dependent() || SemaRef.CurContext->isDependentContext()) 4303 return false; 4304 if (EmitDiags) { 4305 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 4306 << S->getSourceRange(); 4307 } 4308 return true; 4309 } 4310 4311 /// Ignore parenthesizes, implicit casts, copy constructor and return the 4312 /// variable (which may be the loop variable) if possible. 4313 static const ValueDecl *getInitLCDecl(const Expr *E) { 4314 if (!E) 4315 return nullptr; 4316 E = getExprAsWritten(E); 4317 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 4318 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4319 if ((Ctor->isCopyOrMoveConstructor() || 4320 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4321 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4322 E = CE->getArg(0)->IgnoreParenImpCasts(); 4323 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 4324 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 4325 return getCanonicalDecl(VD); 4326 } 4327 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 4328 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4329 return getCanonicalDecl(ME->getMemberDecl()); 4330 return nullptr; 4331 } 4332 4333 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 4334 // Check test-expr for canonical form, save upper-bound UB, flags for 4335 // less/greater and for strict/non-strict comparison. 4336 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4337 // var relational-op b 4338 // b relational-op var 4339 // 4340 if (!S) { 4341 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 4342 return true; 4343 } 4344 S = getExprAsWritten(S); 4345 SourceLocation CondLoc = S->getBeginLoc(); 4346 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4347 if (BO->isRelationalOp()) { 4348 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4349 return setUB(BO->getRHS(), 4350 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 4351 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4352 BO->getSourceRange(), BO->getOperatorLoc()); 4353 if (getInitLCDecl(BO->getRHS()) == LCDecl) 4354 return setUB(BO->getLHS(), 4355 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 4356 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4357 BO->getSourceRange(), BO->getOperatorLoc()); 4358 } else if (BO->getOpcode() == BO_NE) 4359 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ? 4360 BO->getRHS() : BO->getLHS(), 4361 /*LessOp=*/llvm::None, 4362 /*StrictOp=*/true, 4363 BO->getSourceRange(), BO->getOperatorLoc()); 4364 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4365 if (CE->getNumArgs() == 2) { 4366 auto Op = CE->getOperator(); 4367 switch (Op) { 4368 case OO_Greater: 4369 case OO_GreaterEqual: 4370 case OO_Less: 4371 case OO_LessEqual: 4372 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4373 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 4374 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4375 CE->getOperatorLoc()); 4376 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 4377 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 4378 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4379 CE->getOperatorLoc()); 4380 break; 4381 case OO_ExclaimEqual: 4382 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? 4383 CE->getArg(1) : CE->getArg(0), 4384 /*LessOp=*/llvm::None, 4385 /*StrictOp=*/true, 4386 CE->getSourceRange(), 4387 CE->getOperatorLoc()); 4388 break; 4389 default: 4390 break; 4391 } 4392 } 4393 } 4394 if (dependent() || SemaRef.CurContext->isDependentContext()) 4395 return false; 4396 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 4397 << S->getSourceRange() << LCDecl; 4398 return true; 4399 } 4400 4401 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 4402 // RHS of canonical loop form increment can be: 4403 // var + incr 4404 // incr + var 4405 // var - incr 4406 // 4407 RHS = RHS->IgnoreParenImpCasts(); 4408 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 4409 if (BO->isAdditiveOp()) { 4410 bool IsAdd = BO->getOpcode() == BO_Add; 4411 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4412 return setStep(BO->getRHS(), !IsAdd); 4413 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 4414 return setStep(BO->getLHS(), /*Subtract=*/false); 4415 } 4416 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 4417 bool IsAdd = CE->getOperator() == OO_Plus; 4418 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 4419 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4420 return setStep(CE->getArg(1), !IsAdd); 4421 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 4422 return setStep(CE->getArg(0), /*Subtract=*/false); 4423 } 4424 } 4425 if (dependent() || SemaRef.CurContext->isDependentContext()) 4426 return false; 4427 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4428 << RHS->getSourceRange() << LCDecl; 4429 return true; 4430 } 4431 4432 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 4433 // Check incr-expr for canonical loop form and return true if it 4434 // does not conform. 4435 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4436 // ++var 4437 // var++ 4438 // --var 4439 // var-- 4440 // var += incr 4441 // var -= incr 4442 // var = var + incr 4443 // var = incr + var 4444 // var = var - incr 4445 // 4446 if (!S) { 4447 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 4448 return true; 4449 } 4450 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4451 if (!ExprTemp->cleanupsHaveSideEffects()) 4452 S = ExprTemp->getSubExpr(); 4453 4454 IncrementSrcRange = S->getSourceRange(); 4455 S = S->IgnoreParens(); 4456 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 4457 if (UO->isIncrementDecrementOp() && 4458 getInitLCDecl(UO->getSubExpr()) == LCDecl) 4459 return setStep(SemaRef 4460 .ActOnIntegerConstant(UO->getBeginLoc(), 4461 (UO->isDecrementOp() ? -1 : 1)) 4462 .get(), 4463 /*Subtract=*/false); 4464 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4465 switch (BO->getOpcode()) { 4466 case BO_AddAssign: 4467 case BO_SubAssign: 4468 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4469 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 4470 break; 4471 case BO_Assign: 4472 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4473 return checkAndSetIncRHS(BO->getRHS()); 4474 break; 4475 default: 4476 break; 4477 } 4478 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4479 switch (CE->getOperator()) { 4480 case OO_PlusPlus: 4481 case OO_MinusMinus: 4482 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4483 return setStep(SemaRef 4484 .ActOnIntegerConstant( 4485 CE->getBeginLoc(), 4486 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 4487 .get(), 4488 /*Subtract=*/false); 4489 break; 4490 case OO_PlusEqual: 4491 case OO_MinusEqual: 4492 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4493 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 4494 break; 4495 case OO_Equal: 4496 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4497 return checkAndSetIncRHS(CE->getArg(1)); 4498 break; 4499 default: 4500 break; 4501 } 4502 } 4503 if (dependent() || SemaRef.CurContext->isDependentContext()) 4504 return false; 4505 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4506 << S->getSourceRange() << LCDecl; 4507 return true; 4508 } 4509 4510 static ExprResult 4511 tryBuildCapture(Sema &SemaRef, Expr *Capture, 4512 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4513 if (SemaRef.CurContext->isDependentContext()) 4514 return ExprResult(Capture); 4515 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 4516 return SemaRef.PerformImplicitConversion( 4517 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 4518 /*AllowExplicit=*/true); 4519 auto I = Captures.find(Capture); 4520 if (I != Captures.end()) 4521 return buildCapture(SemaRef, Capture, I->second); 4522 DeclRefExpr *Ref = nullptr; 4523 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 4524 Captures[Capture] = Ref; 4525 return Res; 4526 } 4527 4528 /// Build the expression to calculate the number of iterations. 4529 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 4530 Scope *S, const bool LimitedType, 4531 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4532 ExprResult Diff; 4533 QualType VarType = LCDecl->getType().getNonReferenceType(); 4534 if (VarType->isIntegerType() || VarType->isPointerType() || 4535 SemaRef.getLangOpts().CPlusPlus) { 4536 // Upper - Lower 4537 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 4538 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 4539 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 4540 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 4541 if (!Upper || !Lower) 4542 return nullptr; 4543 4544 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4545 4546 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4547 // BuildBinOp already emitted error, this one is to point user to upper 4548 // and lower bound, and to tell what is passed to 'operator-'. 4549 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 4550 << Upper->getSourceRange() << Lower->getSourceRange(); 4551 return nullptr; 4552 } 4553 } 4554 4555 if (!Diff.isUsable()) 4556 return nullptr; 4557 4558 // Upper - Lower [- 1] 4559 if (TestIsStrictOp) 4560 Diff = SemaRef.BuildBinOp( 4561 S, DefaultLoc, BO_Sub, Diff.get(), 4562 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4563 if (!Diff.isUsable()) 4564 return nullptr; 4565 4566 // Upper - Lower [- 1] + Step 4567 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4568 if (!NewStep.isUsable()) 4569 return nullptr; 4570 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 4571 if (!Diff.isUsable()) 4572 return nullptr; 4573 4574 // Parentheses (for dumping/debugging purposes only). 4575 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4576 if (!Diff.isUsable()) 4577 return nullptr; 4578 4579 // (Upper - Lower [- 1] + Step) / Step 4580 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4581 if (!Diff.isUsable()) 4582 return nullptr; 4583 4584 // OpenMP runtime requires 32-bit or 64-bit loop variables. 4585 QualType Type = Diff.get()->getType(); 4586 ASTContext &C = SemaRef.Context; 4587 bool UseVarType = VarType->hasIntegerRepresentation() && 4588 C.getTypeSize(Type) > C.getTypeSize(VarType); 4589 if (!Type->isIntegerType() || UseVarType) { 4590 unsigned NewSize = 4591 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 4592 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 4593 : Type->hasSignedIntegerRepresentation(); 4594 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 4595 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 4596 Diff = SemaRef.PerformImplicitConversion( 4597 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 4598 if (!Diff.isUsable()) 4599 return nullptr; 4600 } 4601 } 4602 if (LimitedType) { 4603 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 4604 if (NewSize != C.getTypeSize(Type)) { 4605 if (NewSize < C.getTypeSize(Type)) { 4606 assert(NewSize == 64 && "incorrect loop var size"); 4607 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 4608 << InitSrcRange << ConditionSrcRange; 4609 } 4610 QualType NewType = C.getIntTypeForBitwidth( 4611 NewSize, Type->hasSignedIntegerRepresentation() || 4612 C.getTypeSize(Type) < NewSize); 4613 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 4614 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 4615 Sema::AA_Converting, true); 4616 if (!Diff.isUsable()) 4617 return nullptr; 4618 } 4619 } 4620 } 4621 4622 return Diff.get(); 4623 } 4624 4625 Expr *OpenMPIterationSpaceChecker::buildPreCond( 4626 Scope *S, Expr *Cond, 4627 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4628 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 4629 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4630 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4631 4632 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 4633 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 4634 if (!NewLB.isUsable() || !NewUB.isUsable()) 4635 return nullptr; 4636 4637 ExprResult CondExpr = 4638 SemaRef.BuildBinOp(S, DefaultLoc, 4639 TestIsLessOp.getValue() ? 4640 (TestIsStrictOp ? BO_LT : BO_LE) : 4641 (TestIsStrictOp ? BO_GT : BO_GE), 4642 NewLB.get(), NewUB.get()); 4643 if (CondExpr.isUsable()) { 4644 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 4645 SemaRef.Context.BoolTy)) 4646 CondExpr = SemaRef.PerformImplicitConversion( 4647 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 4648 /*AllowExplicit=*/true); 4649 } 4650 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4651 // Otherwise use original loop condition and evaluate it in runtime. 4652 return CondExpr.isUsable() ? CondExpr.get() : Cond; 4653 } 4654 4655 /// Build reference expression to the counter be used for codegen. 4656 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 4657 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4658 DSAStackTy &DSA) const { 4659 auto *VD = dyn_cast<VarDecl>(LCDecl); 4660 if (!VD) { 4661 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 4662 DeclRefExpr *Ref = buildDeclRefExpr( 4663 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 4664 const DSAStackTy::DSAVarData Data = 4665 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 4666 // If the loop control decl is explicitly marked as private, do not mark it 4667 // as captured again. 4668 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 4669 Captures.insert(std::make_pair(LCRef, Ref)); 4670 return Ref; 4671 } 4672 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 4673 DefaultLoc); 4674 } 4675 4676 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 4677 if (LCDecl && !LCDecl->isInvalidDecl()) { 4678 QualType Type = LCDecl->getType().getNonReferenceType(); 4679 VarDecl *PrivateVar = buildVarDecl( 4680 SemaRef, DefaultLoc, Type, LCDecl->getName(), 4681 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 4682 isa<VarDecl>(LCDecl) 4683 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 4684 : nullptr); 4685 if (PrivateVar->isInvalidDecl()) 4686 return nullptr; 4687 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 4688 } 4689 return nullptr; 4690 } 4691 4692 /// Build initialization of the counter to be used for codegen. 4693 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 4694 4695 /// Build step of the counter be used for codegen. 4696 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 4697 4698 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 4699 Scope *S, Expr *Counter, 4700 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 4701 Expr *Inc, OverloadedOperatorKind OOK) { 4702 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 4703 if (!Cnt) 4704 return nullptr; 4705 if (Inc) { 4706 assert((OOK == OO_Plus || OOK == OO_Minus) && 4707 "Expected only + or - operations for depend clauses."); 4708 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 4709 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 4710 if (!Cnt) 4711 return nullptr; 4712 } 4713 ExprResult Diff; 4714 QualType VarType = LCDecl->getType().getNonReferenceType(); 4715 if (VarType->isIntegerType() || VarType->isPointerType() || 4716 SemaRef.getLangOpts().CPlusPlus) { 4717 // Upper - Lower 4718 Expr *Upper = TestIsLessOp.getValue() 4719 ? Cnt 4720 : tryBuildCapture(SemaRef, UB, Captures).get(); 4721 Expr *Lower = TestIsLessOp.getValue() 4722 ? tryBuildCapture(SemaRef, LB, Captures).get() 4723 : Cnt; 4724 if (!Upper || !Lower) 4725 return nullptr; 4726 4727 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4728 4729 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4730 // BuildBinOp already emitted error, this one is to point user to upper 4731 // and lower bound, and to tell what is passed to 'operator-'. 4732 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 4733 << Upper->getSourceRange() << Lower->getSourceRange(); 4734 return nullptr; 4735 } 4736 } 4737 4738 if (!Diff.isUsable()) 4739 return nullptr; 4740 4741 // Parentheses (for dumping/debugging purposes only). 4742 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4743 if (!Diff.isUsable()) 4744 return nullptr; 4745 4746 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4747 if (!NewStep.isUsable()) 4748 return nullptr; 4749 // (Upper - Lower) / Step 4750 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4751 if (!Diff.isUsable()) 4752 return nullptr; 4753 4754 return Diff.get(); 4755 } 4756 4757 /// Iteration space of a single for loop. 4758 struct LoopIterationSpace final { 4759 /// True if the condition operator is the strict compare operator (<, > or 4760 /// !=). 4761 bool IsStrictCompare = false; 4762 /// Condition of the loop. 4763 Expr *PreCond = nullptr; 4764 /// This expression calculates the number of iterations in the loop. 4765 /// It is always possible to calculate it before starting the loop. 4766 Expr *NumIterations = nullptr; 4767 /// The loop counter variable. 4768 Expr *CounterVar = nullptr; 4769 /// Private loop counter variable. 4770 Expr *PrivateCounterVar = nullptr; 4771 /// This is initializer for the initial value of #CounterVar. 4772 Expr *CounterInit = nullptr; 4773 /// This is step for the #CounterVar used to generate its update: 4774 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 4775 Expr *CounterStep = nullptr; 4776 /// Should step be subtracted? 4777 bool Subtract = false; 4778 /// Source range of the loop init. 4779 SourceRange InitSrcRange; 4780 /// Source range of the loop condition. 4781 SourceRange CondSrcRange; 4782 /// Source range of the loop increment. 4783 SourceRange IncSrcRange; 4784 }; 4785 4786 } // namespace 4787 4788 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 4789 assert(getLangOpts().OpenMP && "OpenMP is not active."); 4790 assert(Init && "Expected loop in canonical form."); 4791 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 4792 if (AssociatedLoops > 0 && 4793 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 4794 DSAStack->loopStart(); 4795 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 4796 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 4797 if (ValueDecl *D = ISC.getLoopDecl()) { 4798 auto *VD = dyn_cast<VarDecl>(D); 4799 if (!VD) { 4800 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 4801 VD = Private; 4802 } else { 4803 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 4804 /*WithInit=*/false); 4805 VD = cast<VarDecl>(Ref->getDecl()); 4806 } 4807 } 4808 DSAStack->addLoopControlVariable(D, VD); 4809 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 4810 if (LD != D->getCanonicalDecl()) { 4811 DSAStack->resetPossibleLoopCounter(); 4812 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 4813 MarkDeclarationsReferencedInExpr( 4814 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 4815 Var->getType().getNonLValueExprType(Context), 4816 ForLoc, /*RefersToCapture=*/true)); 4817 } 4818 } 4819 } 4820 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 4821 } 4822 } 4823 4824 /// Called on a for stmt to check and extract its iteration space 4825 /// for further processing (such as collapsing). 4826 static bool checkOpenMPIterationSpace( 4827 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 4828 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 4829 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 4830 Expr *OrderedLoopCountExpr, 4831 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4832 LoopIterationSpace &ResultIterSpace, 4833 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4834 // OpenMP [2.6, Canonical Loop Form] 4835 // for (init-expr; test-expr; incr-expr) structured-block 4836 auto *For = dyn_cast_or_null<ForStmt>(S); 4837 if (!For) { 4838 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 4839 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 4840 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 4841 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 4842 if (TotalNestedLoopCount > 1) { 4843 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 4844 SemaRef.Diag(DSA.getConstructLoc(), 4845 diag::note_omp_collapse_ordered_expr) 4846 << 2 << CollapseLoopCountExpr->getSourceRange() 4847 << OrderedLoopCountExpr->getSourceRange(); 4848 else if (CollapseLoopCountExpr) 4849 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4850 diag::note_omp_collapse_ordered_expr) 4851 << 0 << CollapseLoopCountExpr->getSourceRange(); 4852 else 4853 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4854 diag::note_omp_collapse_ordered_expr) 4855 << 1 << OrderedLoopCountExpr->getSourceRange(); 4856 } 4857 return true; 4858 } 4859 assert(For->getBody()); 4860 4861 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 4862 4863 // Check init. 4864 Stmt *Init = For->getInit(); 4865 if (ISC.checkAndSetInit(Init)) 4866 return true; 4867 4868 bool HasErrors = false; 4869 4870 // Check loop variable's type. 4871 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 4872 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 4873 4874 // OpenMP [2.6, Canonical Loop Form] 4875 // Var is one of the following: 4876 // A variable of signed or unsigned integer type. 4877 // For C++, a variable of a random access iterator type. 4878 // For C, a variable of a pointer type. 4879 QualType VarType = LCDecl->getType().getNonReferenceType(); 4880 if (!VarType->isDependentType() && !VarType->isIntegerType() && 4881 !VarType->isPointerType() && 4882 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 4883 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 4884 << SemaRef.getLangOpts().CPlusPlus; 4885 HasErrors = true; 4886 } 4887 4888 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 4889 // a Construct 4890 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4891 // parallel for construct is (are) private. 4892 // The loop iteration variable in the associated for-loop of a simd 4893 // construct with just one associated for-loop is linear with a 4894 // constant-linear-step that is the increment of the associated for-loop. 4895 // Exclude loop var from the list of variables with implicitly defined data 4896 // sharing attributes. 4897 VarsWithImplicitDSA.erase(LCDecl); 4898 4899 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 4900 // in a Construct, C/C++]. 4901 // The loop iteration variable in the associated for-loop of a simd 4902 // construct with just one associated for-loop may be listed in a linear 4903 // clause with a constant-linear-step that is the increment of the 4904 // associated for-loop. 4905 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4906 // parallel for construct may be listed in a private or lastprivate clause. 4907 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 4908 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 4909 // declared in the loop and it is predetermined as a private. 4910 OpenMPClauseKind PredeterminedCKind = 4911 isOpenMPSimdDirective(DKind) 4912 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 4913 : OMPC_private; 4914 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4915 DVar.CKind != PredeterminedCKind) || 4916 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 4917 isOpenMPDistributeDirective(DKind)) && 4918 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4919 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 4920 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 4921 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 4922 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 4923 << getOpenMPClauseName(PredeterminedCKind); 4924 if (DVar.RefExpr == nullptr) 4925 DVar.CKind = PredeterminedCKind; 4926 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 4927 HasErrors = true; 4928 } else if (LoopDeclRefExpr != nullptr) { 4929 // Make the loop iteration variable private (for worksharing constructs), 4930 // linear (for simd directives with the only one associated loop) or 4931 // lastprivate (for simd directives with several collapsed or ordered 4932 // loops). 4933 if (DVar.CKind == OMPC_unknown) 4934 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 4935 } 4936 4937 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 4938 4939 // Check test-expr. 4940 HasErrors |= ISC.checkAndSetCond(For->getCond()); 4941 4942 // Check incr-expr. 4943 HasErrors |= ISC.checkAndSetInc(For->getInc()); 4944 } 4945 4946 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 4947 return HasErrors; 4948 4949 // Build the loop's iteration space representation. 4950 ResultIterSpace.PreCond = 4951 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 4952 ResultIterSpace.NumIterations = ISC.buildNumIterations( 4953 DSA.getCurScope(), 4954 (isOpenMPWorksharingDirective(DKind) || 4955 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 4956 Captures); 4957 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 4958 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 4959 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 4960 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 4961 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 4962 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 4963 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 4964 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 4965 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp(); 4966 4967 HasErrors |= (ResultIterSpace.PreCond == nullptr || 4968 ResultIterSpace.NumIterations == nullptr || 4969 ResultIterSpace.CounterVar == nullptr || 4970 ResultIterSpace.PrivateCounterVar == nullptr || 4971 ResultIterSpace.CounterInit == nullptr || 4972 ResultIterSpace.CounterStep == nullptr); 4973 if (!HasErrors && DSA.isOrderedRegion()) { 4974 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 4975 if (CurrentNestedLoopCount < 4976 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 4977 DSA.getOrderedRegionParam().second->setLoopNumIterations( 4978 CurrentNestedLoopCount, ResultIterSpace.NumIterations); 4979 DSA.getOrderedRegionParam().second->setLoopCounter( 4980 CurrentNestedLoopCount, ResultIterSpace.CounterVar); 4981 } 4982 } 4983 for (auto &Pair : DSA.getDoacrossDependClauses()) { 4984 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 4985 // Erroneous case - clause has some problems. 4986 continue; 4987 } 4988 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 4989 Pair.second.size() <= CurrentNestedLoopCount) { 4990 // Erroneous case - clause has some problems. 4991 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 4992 continue; 4993 } 4994 Expr *CntValue; 4995 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 4996 CntValue = ISC.buildOrderedLoopData( 4997 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 4998 Pair.first->getDependencyLoc()); 4999 else 5000 CntValue = ISC.buildOrderedLoopData( 5001 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 5002 Pair.first->getDependencyLoc(), 5003 Pair.second[CurrentNestedLoopCount].first, 5004 Pair.second[CurrentNestedLoopCount].second); 5005 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 5006 } 5007 } 5008 5009 return HasErrors; 5010 } 5011 5012 /// Build 'VarRef = Start. 5013 static ExprResult 5014 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5015 ExprResult Start, 5016 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5017 // Build 'VarRef = Start. 5018 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 5019 if (!NewStart.isUsable()) 5020 return ExprError(); 5021 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 5022 VarRef.get()->getType())) { 5023 NewStart = SemaRef.PerformImplicitConversion( 5024 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 5025 /*AllowExplicit=*/true); 5026 if (!NewStart.isUsable()) 5027 return ExprError(); 5028 } 5029 5030 ExprResult Init = 5031 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5032 return Init; 5033 } 5034 5035 /// Build 'VarRef = Start + Iter * Step'. 5036 static ExprResult buildCounterUpdate( 5037 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5038 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 5039 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 5040 // Add parentheses (for debugging purposes only). 5041 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 5042 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 5043 !Step.isUsable()) 5044 return ExprError(); 5045 5046 ExprResult NewStep = Step; 5047 if (Captures) 5048 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 5049 if (NewStep.isInvalid()) 5050 return ExprError(); 5051 ExprResult Update = 5052 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 5053 if (!Update.isUsable()) 5054 return ExprError(); 5055 5056 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 5057 // 'VarRef = Start (+|-) Iter * Step'. 5058 ExprResult NewStart = Start; 5059 if (Captures) 5060 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 5061 if (NewStart.isInvalid()) 5062 return ExprError(); 5063 5064 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 5065 ExprResult SavedUpdate = Update; 5066 ExprResult UpdateVal; 5067 if (VarRef.get()->getType()->isOverloadableType() || 5068 NewStart.get()->getType()->isOverloadableType() || 5069 Update.get()->getType()->isOverloadableType()) { 5070 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 5071 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 5072 Update = 5073 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5074 if (Update.isUsable()) { 5075 UpdateVal = 5076 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 5077 VarRef.get(), SavedUpdate.get()); 5078 if (UpdateVal.isUsable()) { 5079 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 5080 UpdateVal.get()); 5081 } 5082 } 5083 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 5084 } 5085 5086 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 5087 if (!Update.isUsable() || !UpdateVal.isUsable()) { 5088 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 5089 NewStart.get(), SavedUpdate.get()); 5090 if (!Update.isUsable()) 5091 return ExprError(); 5092 5093 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 5094 VarRef.get()->getType())) { 5095 Update = SemaRef.PerformImplicitConversion( 5096 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 5097 if (!Update.isUsable()) 5098 return ExprError(); 5099 } 5100 5101 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 5102 } 5103 return Update; 5104 } 5105 5106 /// Convert integer expression \a E to make it have at least \a Bits 5107 /// bits. 5108 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 5109 if (E == nullptr) 5110 return ExprError(); 5111 ASTContext &C = SemaRef.Context; 5112 QualType OldType = E->getType(); 5113 unsigned HasBits = C.getTypeSize(OldType); 5114 if (HasBits >= Bits) 5115 return ExprResult(E); 5116 // OK to convert to signed, because new type has more bits than old. 5117 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 5118 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 5119 true); 5120 } 5121 5122 /// Check if the given expression \a E is a constant integer that fits 5123 /// into \a Bits bits. 5124 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 5125 if (E == nullptr) 5126 return false; 5127 llvm::APSInt Result; 5128 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 5129 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 5130 return false; 5131 } 5132 5133 /// Build preinits statement for the given declarations. 5134 static Stmt *buildPreInits(ASTContext &Context, 5135 MutableArrayRef<Decl *> PreInits) { 5136 if (!PreInits.empty()) { 5137 return new (Context) DeclStmt( 5138 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 5139 SourceLocation(), SourceLocation()); 5140 } 5141 return nullptr; 5142 } 5143 5144 /// Build preinits statement for the given declarations. 5145 static Stmt * 5146 buildPreInits(ASTContext &Context, 5147 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5148 if (!Captures.empty()) { 5149 SmallVector<Decl *, 16> PreInits; 5150 for (const auto &Pair : Captures) 5151 PreInits.push_back(Pair.second->getDecl()); 5152 return buildPreInits(Context, PreInits); 5153 } 5154 return nullptr; 5155 } 5156 5157 /// Build postupdate expression for the given list of postupdates expressions. 5158 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 5159 Expr *PostUpdate = nullptr; 5160 if (!PostUpdates.empty()) { 5161 for (Expr *E : PostUpdates) { 5162 Expr *ConvE = S.BuildCStyleCastExpr( 5163 E->getExprLoc(), 5164 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 5165 E->getExprLoc(), E) 5166 .get(); 5167 PostUpdate = PostUpdate 5168 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 5169 PostUpdate, ConvE) 5170 .get() 5171 : ConvE; 5172 } 5173 } 5174 return PostUpdate; 5175 } 5176 5177 /// Called on a for stmt to check itself and nested loops (if any). 5178 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 5179 /// number of collapsed loops otherwise. 5180 static unsigned 5181 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 5182 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 5183 DSAStackTy &DSA, 5184 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5185 OMPLoopDirective::HelperExprs &Built) { 5186 unsigned NestedLoopCount = 1; 5187 if (CollapseLoopCountExpr) { 5188 // Found 'collapse' clause - calculate collapse number. 5189 Expr::EvalResult Result; 5190 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 5191 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 5192 } 5193 unsigned OrderedLoopCount = 1; 5194 if (OrderedLoopCountExpr) { 5195 // Found 'ordered' clause - calculate collapse number. 5196 Expr::EvalResult EVResult; 5197 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) { 5198 llvm::APSInt Result = EVResult.Val.getInt(); 5199 if (Result.getLimitedValue() < NestedLoopCount) { 5200 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 5201 diag::err_omp_wrong_ordered_loop_count) 5202 << OrderedLoopCountExpr->getSourceRange(); 5203 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 5204 diag::note_collapse_loop_count) 5205 << CollapseLoopCountExpr->getSourceRange(); 5206 } 5207 OrderedLoopCount = Result.getLimitedValue(); 5208 } 5209 } 5210 // This is helper routine for loop directives (e.g., 'for', 'simd', 5211 // 'for simd', etc.). 5212 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 5213 SmallVector<LoopIterationSpace, 4> IterSpaces( 5214 std::max(OrderedLoopCount, NestedLoopCount)); 5215 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 5216 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 5217 if (checkOpenMPIterationSpace( 5218 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5219 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5220 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5221 Captures)) 5222 return 0; 5223 // Move on to the next nested for loop, or to the loop body. 5224 // OpenMP [2.8.1, simd construct, Restrictions] 5225 // All loops associated with the construct must be perfectly nested; that 5226 // is, there must be no intervening code nor any OpenMP directive between 5227 // any two loops. 5228 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5229 } 5230 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 5231 if (checkOpenMPIterationSpace( 5232 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5233 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5234 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5235 Captures)) 5236 return 0; 5237 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 5238 // Handle initialization of captured loop iterator variables. 5239 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 5240 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 5241 Captures[DRE] = DRE; 5242 } 5243 } 5244 // Move on to the next nested for loop, or to the loop body. 5245 // OpenMP [2.8.1, simd construct, Restrictions] 5246 // All loops associated with the construct must be perfectly nested; that 5247 // is, there must be no intervening code nor any OpenMP directive between 5248 // any two loops. 5249 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5250 } 5251 5252 Built.clear(/* size */ NestedLoopCount); 5253 5254 if (SemaRef.CurContext->isDependentContext()) 5255 return NestedLoopCount; 5256 5257 // An example of what is generated for the following code: 5258 // 5259 // #pragma omp simd collapse(2) ordered(2) 5260 // for (i = 0; i < NI; ++i) 5261 // for (k = 0; k < NK; ++k) 5262 // for (j = J0; j < NJ; j+=2) { 5263 // <loop body> 5264 // } 5265 // 5266 // We generate the code below. 5267 // Note: the loop body may be outlined in CodeGen. 5268 // Note: some counters may be C++ classes, operator- is used to find number of 5269 // iterations and operator+= to calculate counter value. 5270 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 5271 // or i64 is currently supported). 5272 // 5273 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 5274 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 5275 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 5276 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 5277 // // similar updates for vars in clauses (e.g. 'linear') 5278 // <loop body (using local i and j)> 5279 // } 5280 // i = NI; // assign final values of counters 5281 // j = NJ; 5282 // 5283 5284 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 5285 // the iteration counts of the collapsed for loops. 5286 // Precondition tests if there is at least one iteration (all conditions are 5287 // true). 5288 auto PreCond = ExprResult(IterSpaces[0].PreCond); 5289 Expr *N0 = IterSpaces[0].NumIterations; 5290 ExprResult LastIteration32 = 5291 widenIterationCount(/*Bits=*/32, 5292 SemaRef 5293 .PerformImplicitConversion( 5294 N0->IgnoreImpCasts(), N0->getType(), 5295 Sema::AA_Converting, /*AllowExplicit=*/true) 5296 .get(), 5297 SemaRef); 5298 ExprResult LastIteration64 = widenIterationCount( 5299 /*Bits=*/64, 5300 SemaRef 5301 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 5302 Sema::AA_Converting, 5303 /*AllowExplicit=*/true) 5304 .get(), 5305 SemaRef); 5306 5307 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 5308 return NestedLoopCount; 5309 5310 ASTContext &C = SemaRef.Context; 5311 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 5312 5313 Scope *CurScope = DSA.getCurScope(); 5314 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 5315 if (PreCond.isUsable()) { 5316 PreCond = 5317 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 5318 PreCond.get(), IterSpaces[Cnt].PreCond); 5319 } 5320 Expr *N = IterSpaces[Cnt].NumIterations; 5321 SourceLocation Loc = N->getExprLoc(); 5322 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 5323 if (LastIteration32.isUsable()) 5324 LastIteration32 = SemaRef.BuildBinOp( 5325 CurScope, Loc, BO_Mul, LastIteration32.get(), 5326 SemaRef 5327 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5328 Sema::AA_Converting, 5329 /*AllowExplicit=*/true) 5330 .get()); 5331 if (LastIteration64.isUsable()) 5332 LastIteration64 = SemaRef.BuildBinOp( 5333 CurScope, Loc, BO_Mul, LastIteration64.get(), 5334 SemaRef 5335 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5336 Sema::AA_Converting, 5337 /*AllowExplicit=*/true) 5338 .get()); 5339 } 5340 5341 // Choose either the 32-bit or 64-bit version. 5342 ExprResult LastIteration = LastIteration64; 5343 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 5344 (LastIteration32.isUsable() && 5345 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 5346 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 5347 fitsInto( 5348 /*Bits=*/32, 5349 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 5350 LastIteration64.get(), SemaRef)))) 5351 LastIteration = LastIteration32; 5352 QualType VType = LastIteration.get()->getType(); 5353 QualType RealVType = VType; 5354 QualType StrideVType = VType; 5355 if (isOpenMPTaskLoopDirective(DKind)) { 5356 VType = 5357 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 5358 StrideVType = 5359 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 5360 } 5361 5362 if (!LastIteration.isUsable()) 5363 return 0; 5364 5365 // Save the number of iterations. 5366 ExprResult NumIterations = LastIteration; 5367 { 5368 LastIteration = SemaRef.BuildBinOp( 5369 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 5370 LastIteration.get(), 5371 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5372 if (!LastIteration.isUsable()) 5373 return 0; 5374 } 5375 5376 // Calculate the last iteration number beforehand instead of doing this on 5377 // each iteration. Do not do this if the number of iterations may be kfold-ed. 5378 llvm::APSInt Result; 5379 bool IsConstant = 5380 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 5381 ExprResult CalcLastIteration; 5382 if (!IsConstant) { 5383 ExprResult SaveRef = 5384 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 5385 LastIteration = SaveRef; 5386 5387 // Prepare SaveRef + 1. 5388 NumIterations = SemaRef.BuildBinOp( 5389 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 5390 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5391 if (!NumIterations.isUsable()) 5392 return 0; 5393 } 5394 5395 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 5396 5397 // Build variables passed into runtime, necessary for worksharing directives. 5398 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 5399 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5400 isOpenMPDistributeDirective(DKind)) { 5401 // Lower bound variable, initialized with zero. 5402 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 5403 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 5404 SemaRef.AddInitializerToDecl(LBDecl, 5405 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5406 /*DirectInit*/ false); 5407 5408 // Upper bound variable, initialized with last iteration number. 5409 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 5410 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 5411 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 5412 /*DirectInit*/ false); 5413 5414 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 5415 // This will be used to implement clause 'lastprivate'. 5416 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 5417 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 5418 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 5419 SemaRef.AddInitializerToDecl(ILDecl, 5420 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5421 /*DirectInit*/ false); 5422 5423 // Stride variable returned by runtime (we initialize it to 1 by default). 5424 VarDecl *STDecl = 5425 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 5426 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 5427 SemaRef.AddInitializerToDecl(STDecl, 5428 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 5429 /*DirectInit*/ false); 5430 5431 // Build expression: UB = min(UB, LastIteration) 5432 // It is necessary for CodeGen of directives with static scheduling. 5433 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 5434 UB.get(), LastIteration.get()); 5435 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5436 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 5437 LastIteration.get(), UB.get()); 5438 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 5439 CondOp.get()); 5440 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 5441 5442 // If we have a combined directive that combines 'distribute', 'for' or 5443 // 'simd' we need to be able to access the bounds of the schedule of the 5444 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 5445 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 5446 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5447 // Lower bound variable, initialized with zero. 5448 VarDecl *CombLBDecl = 5449 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 5450 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 5451 SemaRef.AddInitializerToDecl( 5452 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5453 /*DirectInit*/ false); 5454 5455 // Upper bound variable, initialized with last iteration number. 5456 VarDecl *CombUBDecl = 5457 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 5458 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 5459 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 5460 /*DirectInit*/ false); 5461 5462 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 5463 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 5464 ExprResult CombCondOp = 5465 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 5466 LastIteration.get(), CombUB.get()); 5467 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 5468 CombCondOp.get()); 5469 CombEUB = 5470 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 5471 5472 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 5473 // We expect to have at least 2 more parameters than the 'parallel' 5474 // directive does - the lower and upper bounds of the previous schedule. 5475 assert(CD->getNumParams() >= 4 && 5476 "Unexpected number of parameters in loop combined directive"); 5477 5478 // Set the proper type for the bounds given what we learned from the 5479 // enclosed loops. 5480 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 5481 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 5482 5483 // Previous lower and upper bounds are obtained from the region 5484 // parameters. 5485 PrevLB = 5486 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 5487 PrevUB = 5488 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 5489 } 5490 } 5491 5492 // Build the iteration variable and its initialization before loop. 5493 ExprResult IV; 5494 ExprResult Init, CombInit; 5495 { 5496 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 5497 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 5498 Expr *RHS = 5499 (isOpenMPWorksharingDirective(DKind) || 5500 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5501 ? LB.get() 5502 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5503 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 5504 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 5505 5506 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5507 Expr *CombRHS = 5508 (isOpenMPWorksharingDirective(DKind) || 5509 isOpenMPTaskLoopDirective(DKind) || 5510 isOpenMPDistributeDirective(DKind)) 5511 ? CombLB.get() 5512 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5513 CombInit = 5514 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 5515 CombInit = 5516 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 5517 } 5518 } 5519 5520 bool UseStrictCompare = 5521 RealVType->hasUnsignedIntegerRepresentation() && 5522 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 5523 return LIS.IsStrictCompare; 5524 }); 5525 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 5526 // unsigned IV)) for worksharing loops. 5527 SourceLocation CondLoc = AStmt->getBeginLoc(); 5528 Expr *BoundUB = UB.get(); 5529 if (UseStrictCompare) { 5530 BoundUB = 5531 SemaRef 5532 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 5533 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 5534 .get(); 5535 BoundUB = 5536 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 5537 } 5538 ExprResult Cond = 5539 (isOpenMPWorksharingDirective(DKind) || 5540 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5541 ? SemaRef.BuildBinOp(CurScope, CondLoc, 5542 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 5543 BoundUB) 5544 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 5545 NumIterations.get()); 5546 ExprResult CombDistCond; 5547 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5548 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 5549 NumIterations.get()); 5550 } 5551 5552 ExprResult CombCond; 5553 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5554 Expr *BoundCombUB = CombUB.get(); 5555 if (UseStrictCompare) { 5556 BoundCombUB = 5557 SemaRef 5558 .BuildBinOp( 5559 CurScope, CondLoc, BO_Add, BoundCombUB, 5560 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 5561 .get(); 5562 BoundCombUB = 5563 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 5564 .get(); 5565 } 5566 CombCond = 5567 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 5568 IV.get(), BoundCombUB); 5569 } 5570 // Loop increment (IV = IV + 1) 5571 SourceLocation IncLoc = AStmt->getBeginLoc(); 5572 ExprResult Inc = 5573 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 5574 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 5575 if (!Inc.isUsable()) 5576 return 0; 5577 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 5578 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 5579 if (!Inc.isUsable()) 5580 return 0; 5581 5582 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 5583 // Used for directives with static scheduling. 5584 // In combined construct, add combined version that use CombLB and CombUB 5585 // base variables for the update 5586 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 5587 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5588 isOpenMPDistributeDirective(DKind)) { 5589 // LB + ST 5590 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 5591 if (!NextLB.isUsable()) 5592 return 0; 5593 // LB = LB + ST 5594 NextLB = 5595 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 5596 NextLB = 5597 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 5598 if (!NextLB.isUsable()) 5599 return 0; 5600 // UB + ST 5601 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 5602 if (!NextUB.isUsable()) 5603 return 0; 5604 // UB = UB + ST 5605 NextUB = 5606 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 5607 NextUB = 5608 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 5609 if (!NextUB.isUsable()) 5610 return 0; 5611 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5612 CombNextLB = 5613 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 5614 if (!NextLB.isUsable()) 5615 return 0; 5616 // LB = LB + ST 5617 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 5618 CombNextLB.get()); 5619 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 5620 /*DiscardedValue*/ false); 5621 if (!CombNextLB.isUsable()) 5622 return 0; 5623 // UB + ST 5624 CombNextUB = 5625 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 5626 if (!CombNextUB.isUsable()) 5627 return 0; 5628 // UB = UB + ST 5629 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 5630 CombNextUB.get()); 5631 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 5632 /*DiscardedValue*/ false); 5633 if (!CombNextUB.isUsable()) 5634 return 0; 5635 } 5636 } 5637 5638 // Create increment expression for distribute loop when combined in a same 5639 // directive with for as IV = IV + ST; ensure upper bound expression based 5640 // on PrevUB instead of NumIterations - used to implement 'for' when found 5641 // in combination with 'distribute', like in 'distribute parallel for' 5642 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 5643 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 5644 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5645 DistCond = SemaRef.BuildBinOp( 5646 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 5647 assert(DistCond.isUsable() && "distribute cond expr was not built"); 5648 5649 DistInc = 5650 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 5651 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5652 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 5653 DistInc.get()); 5654 DistInc = 5655 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 5656 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5657 5658 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 5659 // construct 5660 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 5661 ExprResult IsUBGreater = 5662 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 5663 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5664 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 5665 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 5666 CondOp.get()); 5667 PrevEUB = 5668 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 5669 5670 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 5671 // parallel for is in combination with a distribute directive with 5672 // schedule(static, 1) 5673 Expr *BoundPrevUB = PrevUB.get(); 5674 if (UseStrictCompare) { 5675 BoundPrevUB = 5676 SemaRef 5677 .BuildBinOp( 5678 CurScope, CondLoc, BO_Add, BoundPrevUB, 5679 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 5680 .get(); 5681 BoundPrevUB = 5682 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 5683 .get(); 5684 } 5685 ParForInDistCond = 5686 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 5687 IV.get(), BoundPrevUB); 5688 } 5689 5690 // Build updates and final values of the loop counters. 5691 bool HasErrors = false; 5692 Built.Counters.resize(NestedLoopCount); 5693 Built.Inits.resize(NestedLoopCount); 5694 Built.Updates.resize(NestedLoopCount); 5695 Built.Finals.resize(NestedLoopCount); 5696 { 5697 // We implement the following algorithm for obtaining the 5698 // original loop iteration variable values based on the 5699 // value of the collapsed loop iteration variable IV. 5700 // 5701 // Let n+1 be the number of collapsed loops in the nest. 5702 // Iteration variables (I0, I1, .... In) 5703 // Iteration counts (N0, N1, ... Nn) 5704 // 5705 // Acc = IV; 5706 // 5707 // To compute Ik for loop k, 0 <= k <= n, generate: 5708 // Prod = N(k+1) * N(k+2) * ... * Nn; 5709 // Ik = Acc / Prod; 5710 // Acc -= Ik * Prod; 5711 // 5712 ExprResult Acc = IV; 5713 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 5714 LoopIterationSpace &IS = IterSpaces[Cnt]; 5715 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 5716 ExprResult Iter; 5717 5718 // Compute prod 5719 ExprResult Prod = 5720 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 5721 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 5722 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 5723 IterSpaces[K].NumIterations); 5724 5725 // Iter = Acc / Prod 5726 // If there is at least one more inner loop to avoid 5727 // multiplication by 1. 5728 if (Cnt + 1 < NestedLoopCount) 5729 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 5730 Acc.get(), Prod.get()); 5731 else 5732 Iter = Acc; 5733 if (!Iter.isUsable()) { 5734 HasErrors = true; 5735 break; 5736 } 5737 5738 // Update Acc: 5739 // Acc -= Iter * Prod 5740 // Check if there is at least one more inner loop to avoid 5741 // multiplication by 1. 5742 if (Cnt + 1 < NestedLoopCount) 5743 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 5744 Iter.get(), Prod.get()); 5745 else 5746 Prod = Iter; 5747 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 5748 Acc.get(), Prod.get()); 5749 5750 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 5751 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 5752 DeclRefExpr *CounterVar = buildDeclRefExpr( 5753 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 5754 /*RefersToCapture=*/true); 5755 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 5756 IS.CounterInit, Captures); 5757 if (!Init.isUsable()) { 5758 HasErrors = true; 5759 break; 5760 } 5761 ExprResult Update = buildCounterUpdate( 5762 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 5763 IS.CounterStep, IS.Subtract, &Captures); 5764 if (!Update.isUsable()) { 5765 HasErrors = true; 5766 break; 5767 } 5768 5769 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 5770 ExprResult Final = buildCounterUpdate( 5771 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 5772 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 5773 if (!Final.isUsable()) { 5774 HasErrors = true; 5775 break; 5776 } 5777 5778 if (!Update.isUsable() || !Final.isUsable()) { 5779 HasErrors = true; 5780 break; 5781 } 5782 // Save results 5783 Built.Counters[Cnt] = IS.CounterVar; 5784 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 5785 Built.Inits[Cnt] = Init.get(); 5786 Built.Updates[Cnt] = Update.get(); 5787 Built.Finals[Cnt] = Final.get(); 5788 } 5789 } 5790 5791 if (HasErrors) 5792 return 0; 5793 5794 // Save results 5795 Built.IterationVarRef = IV.get(); 5796 Built.LastIteration = LastIteration.get(); 5797 Built.NumIterations = NumIterations.get(); 5798 Built.CalcLastIteration = SemaRef 5799 .ActOnFinishFullExpr(CalcLastIteration.get(), 5800 /*DiscardedValue*/ false) 5801 .get(); 5802 Built.PreCond = PreCond.get(); 5803 Built.PreInits = buildPreInits(C, Captures); 5804 Built.Cond = Cond.get(); 5805 Built.Init = Init.get(); 5806 Built.Inc = Inc.get(); 5807 Built.LB = LB.get(); 5808 Built.UB = UB.get(); 5809 Built.IL = IL.get(); 5810 Built.ST = ST.get(); 5811 Built.EUB = EUB.get(); 5812 Built.NLB = NextLB.get(); 5813 Built.NUB = NextUB.get(); 5814 Built.PrevLB = PrevLB.get(); 5815 Built.PrevUB = PrevUB.get(); 5816 Built.DistInc = DistInc.get(); 5817 Built.PrevEUB = PrevEUB.get(); 5818 Built.DistCombinedFields.LB = CombLB.get(); 5819 Built.DistCombinedFields.UB = CombUB.get(); 5820 Built.DistCombinedFields.EUB = CombEUB.get(); 5821 Built.DistCombinedFields.Init = CombInit.get(); 5822 Built.DistCombinedFields.Cond = CombCond.get(); 5823 Built.DistCombinedFields.NLB = CombNextLB.get(); 5824 Built.DistCombinedFields.NUB = CombNextUB.get(); 5825 Built.DistCombinedFields.DistCond = CombDistCond.get(); 5826 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 5827 5828 return NestedLoopCount; 5829 } 5830 5831 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 5832 auto CollapseClauses = 5833 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 5834 if (CollapseClauses.begin() != CollapseClauses.end()) 5835 return (*CollapseClauses.begin())->getNumForLoops(); 5836 return nullptr; 5837 } 5838 5839 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 5840 auto OrderedClauses = 5841 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 5842 if (OrderedClauses.begin() != OrderedClauses.end()) 5843 return (*OrderedClauses.begin())->getNumForLoops(); 5844 return nullptr; 5845 } 5846 5847 static bool checkSimdlenSafelenSpecified(Sema &S, 5848 const ArrayRef<OMPClause *> Clauses) { 5849 const OMPSafelenClause *Safelen = nullptr; 5850 const OMPSimdlenClause *Simdlen = nullptr; 5851 5852 for (const OMPClause *Clause : Clauses) { 5853 if (Clause->getClauseKind() == OMPC_safelen) 5854 Safelen = cast<OMPSafelenClause>(Clause); 5855 else if (Clause->getClauseKind() == OMPC_simdlen) 5856 Simdlen = cast<OMPSimdlenClause>(Clause); 5857 if (Safelen && Simdlen) 5858 break; 5859 } 5860 5861 if (Simdlen && Safelen) { 5862 const Expr *SimdlenLength = Simdlen->getSimdlen(); 5863 const Expr *SafelenLength = Safelen->getSafelen(); 5864 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 5865 SimdlenLength->isInstantiationDependent() || 5866 SimdlenLength->containsUnexpandedParameterPack()) 5867 return false; 5868 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 5869 SafelenLength->isInstantiationDependent() || 5870 SafelenLength->containsUnexpandedParameterPack()) 5871 return false; 5872 Expr::EvalResult SimdlenResult, SafelenResult; 5873 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 5874 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 5875 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 5876 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 5877 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 5878 // If both simdlen and safelen clauses are specified, the value of the 5879 // simdlen parameter must be less than or equal to the value of the safelen 5880 // parameter. 5881 if (SimdlenRes > SafelenRes) { 5882 S.Diag(SimdlenLength->getExprLoc(), 5883 diag::err_omp_wrong_simdlen_safelen_values) 5884 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 5885 return true; 5886 } 5887 } 5888 return false; 5889 } 5890 5891 StmtResult 5892 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5893 SourceLocation StartLoc, SourceLocation EndLoc, 5894 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5895 if (!AStmt) 5896 return StmtError(); 5897 5898 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5899 OMPLoopDirective::HelperExprs B; 5900 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5901 // define the nested loops number. 5902 unsigned NestedLoopCount = checkOpenMPLoop( 5903 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5904 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5905 if (NestedLoopCount == 0) 5906 return StmtError(); 5907 5908 assert((CurContext->isDependentContext() || B.builtAll()) && 5909 "omp simd loop exprs were not built"); 5910 5911 if (!CurContext->isDependentContext()) { 5912 // Finalize the clauses that need pre-built expressions for CodeGen. 5913 for (OMPClause *C : Clauses) { 5914 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5915 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5916 B.NumIterations, *this, CurScope, 5917 DSAStack)) 5918 return StmtError(); 5919 } 5920 } 5921 5922 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5923 return StmtError(); 5924 5925 setFunctionHasBranchProtectedScope(); 5926 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5927 Clauses, AStmt, B); 5928 } 5929 5930 StmtResult 5931 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5932 SourceLocation StartLoc, SourceLocation EndLoc, 5933 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5934 if (!AStmt) 5935 return StmtError(); 5936 5937 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5938 OMPLoopDirective::HelperExprs B; 5939 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5940 // define the nested loops number. 5941 unsigned NestedLoopCount = checkOpenMPLoop( 5942 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5943 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5944 if (NestedLoopCount == 0) 5945 return StmtError(); 5946 5947 assert((CurContext->isDependentContext() || B.builtAll()) && 5948 "omp for loop exprs were not built"); 5949 5950 if (!CurContext->isDependentContext()) { 5951 // Finalize the clauses that need pre-built expressions for CodeGen. 5952 for (OMPClause *C : Clauses) { 5953 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5954 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5955 B.NumIterations, *this, CurScope, 5956 DSAStack)) 5957 return StmtError(); 5958 } 5959 } 5960 5961 setFunctionHasBranchProtectedScope(); 5962 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5963 Clauses, AStmt, B, DSAStack->isCancelRegion()); 5964 } 5965 5966 StmtResult Sema::ActOnOpenMPForSimdDirective( 5967 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5968 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5969 if (!AStmt) 5970 return StmtError(); 5971 5972 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5973 OMPLoopDirective::HelperExprs B; 5974 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5975 // define the nested loops number. 5976 unsigned NestedLoopCount = 5977 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 5978 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5979 VarsWithImplicitDSA, B); 5980 if (NestedLoopCount == 0) 5981 return StmtError(); 5982 5983 assert((CurContext->isDependentContext() || B.builtAll()) && 5984 "omp for simd loop exprs were not built"); 5985 5986 if (!CurContext->isDependentContext()) { 5987 // Finalize the clauses that need pre-built expressions for CodeGen. 5988 for (OMPClause *C : Clauses) { 5989 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5990 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5991 B.NumIterations, *this, CurScope, 5992 DSAStack)) 5993 return StmtError(); 5994 } 5995 } 5996 5997 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5998 return StmtError(); 5999 6000 setFunctionHasBranchProtectedScope(); 6001 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6002 Clauses, AStmt, B); 6003 } 6004 6005 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 6006 Stmt *AStmt, 6007 SourceLocation StartLoc, 6008 SourceLocation EndLoc) { 6009 if (!AStmt) 6010 return StmtError(); 6011 6012 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6013 auto BaseStmt = AStmt; 6014 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6015 BaseStmt = CS->getCapturedStmt(); 6016 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6017 auto S = C->children(); 6018 if (S.begin() == S.end()) 6019 return StmtError(); 6020 // All associated statements must be '#pragma omp section' except for 6021 // the first one. 6022 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6023 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6024 if (SectionStmt) 6025 Diag(SectionStmt->getBeginLoc(), 6026 diag::err_omp_sections_substmt_not_section); 6027 return StmtError(); 6028 } 6029 cast<OMPSectionDirective>(SectionStmt) 6030 ->setHasCancel(DSAStack->isCancelRegion()); 6031 } 6032 } else { 6033 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 6034 return StmtError(); 6035 } 6036 6037 setFunctionHasBranchProtectedScope(); 6038 6039 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6040 DSAStack->isCancelRegion()); 6041 } 6042 6043 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 6044 SourceLocation StartLoc, 6045 SourceLocation EndLoc) { 6046 if (!AStmt) 6047 return StmtError(); 6048 6049 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6050 6051 setFunctionHasBranchProtectedScope(); 6052 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 6053 6054 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 6055 DSAStack->isCancelRegion()); 6056 } 6057 6058 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 6059 Stmt *AStmt, 6060 SourceLocation StartLoc, 6061 SourceLocation EndLoc) { 6062 if (!AStmt) 6063 return StmtError(); 6064 6065 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6066 6067 setFunctionHasBranchProtectedScope(); 6068 6069 // OpenMP [2.7.3, single Construct, Restrictions] 6070 // The copyprivate clause must not be used with the nowait clause. 6071 const OMPClause *Nowait = nullptr; 6072 const OMPClause *Copyprivate = nullptr; 6073 for (const OMPClause *Clause : Clauses) { 6074 if (Clause->getClauseKind() == OMPC_nowait) 6075 Nowait = Clause; 6076 else if (Clause->getClauseKind() == OMPC_copyprivate) 6077 Copyprivate = Clause; 6078 if (Copyprivate && Nowait) { 6079 Diag(Copyprivate->getBeginLoc(), 6080 diag::err_omp_single_copyprivate_with_nowait); 6081 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 6082 return StmtError(); 6083 } 6084 } 6085 6086 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6087 } 6088 6089 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 6090 SourceLocation StartLoc, 6091 SourceLocation EndLoc) { 6092 if (!AStmt) 6093 return StmtError(); 6094 6095 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6096 6097 setFunctionHasBranchProtectedScope(); 6098 6099 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 6100 } 6101 6102 StmtResult Sema::ActOnOpenMPCriticalDirective( 6103 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 6104 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 6105 if (!AStmt) 6106 return StmtError(); 6107 6108 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6109 6110 bool ErrorFound = false; 6111 llvm::APSInt Hint; 6112 SourceLocation HintLoc; 6113 bool DependentHint = false; 6114 for (const OMPClause *C : Clauses) { 6115 if (C->getClauseKind() == OMPC_hint) { 6116 if (!DirName.getName()) { 6117 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 6118 ErrorFound = true; 6119 } 6120 Expr *E = cast<OMPHintClause>(C)->getHint(); 6121 if (E->isTypeDependent() || E->isValueDependent() || 6122 E->isInstantiationDependent()) { 6123 DependentHint = true; 6124 } else { 6125 Hint = E->EvaluateKnownConstInt(Context); 6126 HintLoc = C->getBeginLoc(); 6127 } 6128 } 6129 } 6130 if (ErrorFound) 6131 return StmtError(); 6132 const auto Pair = DSAStack->getCriticalWithHint(DirName); 6133 if (Pair.first && DirName.getName() && !DependentHint) { 6134 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 6135 Diag(StartLoc, diag::err_omp_critical_with_hint); 6136 if (HintLoc.isValid()) 6137 Diag(HintLoc, diag::note_omp_critical_hint_here) 6138 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 6139 else 6140 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 6141 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 6142 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 6143 << 1 6144 << C->getHint()->EvaluateKnownConstInt(Context).toString( 6145 /*Radix=*/10, /*Signed=*/false); 6146 } else { 6147 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 6148 } 6149 } 6150 } 6151 6152 setFunctionHasBranchProtectedScope(); 6153 6154 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 6155 Clauses, AStmt); 6156 if (!Pair.first && DirName.getName() && !DependentHint) 6157 DSAStack->addCriticalWithHint(Dir, Hint); 6158 return Dir; 6159 } 6160 6161 StmtResult Sema::ActOnOpenMPParallelForDirective( 6162 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6163 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6164 if (!AStmt) 6165 return StmtError(); 6166 6167 auto *CS = cast<CapturedStmt>(AStmt); 6168 // 1.2.2 OpenMP Language Terminology 6169 // Structured block - An executable statement with a single entry at the 6170 // top and a single exit at the bottom. 6171 // The point of exit cannot be a branch out of the structured block. 6172 // longjmp() and throw() must not violate the entry/exit criteria. 6173 CS->getCapturedDecl()->setNothrow(); 6174 6175 OMPLoopDirective::HelperExprs B; 6176 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6177 // define the nested loops number. 6178 unsigned NestedLoopCount = 6179 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 6180 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6181 VarsWithImplicitDSA, B); 6182 if (NestedLoopCount == 0) 6183 return StmtError(); 6184 6185 assert((CurContext->isDependentContext() || B.builtAll()) && 6186 "omp parallel for loop exprs were not built"); 6187 6188 if (!CurContext->isDependentContext()) { 6189 // Finalize the clauses that need pre-built expressions for CodeGen. 6190 for (OMPClause *C : Clauses) { 6191 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6192 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6193 B.NumIterations, *this, CurScope, 6194 DSAStack)) 6195 return StmtError(); 6196 } 6197 } 6198 6199 setFunctionHasBranchProtectedScope(); 6200 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 6201 NestedLoopCount, Clauses, AStmt, B, 6202 DSAStack->isCancelRegion()); 6203 } 6204 6205 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 6206 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6207 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6208 if (!AStmt) 6209 return StmtError(); 6210 6211 auto *CS = cast<CapturedStmt>(AStmt); 6212 // 1.2.2 OpenMP Language Terminology 6213 // Structured block - An executable statement with a single entry at the 6214 // top and a single exit at the bottom. 6215 // The point of exit cannot be a branch out of the structured block. 6216 // longjmp() and throw() must not violate the entry/exit criteria. 6217 CS->getCapturedDecl()->setNothrow(); 6218 6219 OMPLoopDirective::HelperExprs B; 6220 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6221 // define the nested loops number. 6222 unsigned NestedLoopCount = 6223 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 6224 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6225 VarsWithImplicitDSA, B); 6226 if (NestedLoopCount == 0) 6227 return StmtError(); 6228 6229 if (!CurContext->isDependentContext()) { 6230 // Finalize the clauses that need pre-built expressions for CodeGen. 6231 for (OMPClause *C : Clauses) { 6232 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6233 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6234 B.NumIterations, *this, CurScope, 6235 DSAStack)) 6236 return StmtError(); 6237 } 6238 } 6239 6240 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6241 return StmtError(); 6242 6243 setFunctionHasBranchProtectedScope(); 6244 return OMPParallelForSimdDirective::Create( 6245 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6246 } 6247 6248 StmtResult 6249 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 6250 Stmt *AStmt, SourceLocation StartLoc, 6251 SourceLocation EndLoc) { 6252 if (!AStmt) 6253 return StmtError(); 6254 6255 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6256 auto BaseStmt = AStmt; 6257 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6258 BaseStmt = CS->getCapturedStmt(); 6259 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6260 auto S = C->children(); 6261 if (S.begin() == S.end()) 6262 return StmtError(); 6263 // All associated statements must be '#pragma omp section' except for 6264 // the first one. 6265 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6266 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6267 if (SectionStmt) 6268 Diag(SectionStmt->getBeginLoc(), 6269 diag::err_omp_parallel_sections_substmt_not_section); 6270 return StmtError(); 6271 } 6272 cast<OMPSectionDirective>(SectionStmt) 6273 ->setHasCancel(DSAStack->isCancelRegion()); 6274 } 6275 } else { 6276 Diag(AStmt->getBeginLoc(), 6277 diag::err_omp_parallel_sections_not_compound_stmt); 6278 return StmtError(); 6279 } 6280 6281 setFunctionHasBranchProtectedScope(); 6282 6283 return OMPParallelSectionsDirective::Create( 6284 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 6285 } 6286 6287 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 6288 Stmt *AStmt, SourceLocation StartLoc, 6289 SourceLocation EndLoc) { 6290 if (!AStmt) 6291 return StmtError(); 6292 6293 auto *CS = cast<CapturedStmt>(AStmt); 6294 // 1.2.2 OpenMP Language Terminology 6295 // Structured block - An executable statement with a single entry at the 6296 // top and a single exit at the bottom. 6297 // The point of exit cannot be a branch out of the structured block. 6298 // longjmp() and throw() must not violate the entry/exit criteria. 6299 CS->getCapturedDecl()->setNothrow(); 6300 6301 setFunctionHasBranchProtectedScope(); 6302 6303 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6304 DSAStack->isCancelRegion()); 6305 } 6306 6307 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 6308 SourceLocation EndLoc) { 6309 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 6310 } 6311 6312 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 6313 SourceLocation EndLoc) { 6314 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 6315 } 6316 6317 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 6318 SourceLocation EndLoc) { 6319 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 6320 } 6321 6322 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 6323 Stmt *AStmt, 6324 SourceLocation StartLoc, 6325 SourceLocation EndLoc) { 6326 if (!AStmt) 6327 return StmtError(); 6328 6329 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6330 6331 setFunctionHasBranchProtectedScope(); 6332 6333 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 6334 AStmt, 6335 DSAStack->getTaskgroupReductionRef()); 6336 } 6337 6338 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 6339 SourceLocation StartLoc, 6340 SourceLocation EndLoc) { 6341 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 6342 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 6343 } 6344 6345 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 6346 Stmt *AStmt, 6347 SourceLocation StartLoc, 6348 SourceLocation EndLoc) { 6349 const OMPClause *DependFound = nullptr; 6350 const OMPClause *DependSourceClause = nullptr; 6351 const OMPClause *DependSinkClause = nullptr; 6352 bool ErrorFound = false; 6353 const OMPThreadsClause *TC = nullptr; 6354 const OMPSIMDClause *SC = nullptr; 6355 for (const OMPClause *C : Clauses) { 6356 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 6357 DependFound = C; 6358 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 6359 if (DependSourceClause) { 6360 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 6361 << getOpenMPDirectiveName(OMPD_ordered) 6362 << getOpenMPClauseName(OMPC_depend) << 2; 6363 ErrorFound = true; 6364 } else { 6365 DependSourceClause = C; 6366 } 6367 if (DependSinkClause) { 6368 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6369 << 0; 6370 ErrorFound = true; 6371 } 6372 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 6373 if (DependSourceClause) { 6374 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6375 << 1; 6376 ErrorFound = true; 6377 } 6378 DependSinkClause = C; 6379 } 6380 } else if (C->getClauseKind() == OMPC_threads) { 6381 TC = cast<OMPThreadsClause>(C); 6382 } else if (C->getClauseKind() == OMPC_simd) { 6383 SC = cast<OMPSIMDClause>(C); 6384 } 6385 } 6386 if (!ErrorFound && !SC && 6387 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 6388 // OpenMP [2.8.1,simd Construct, Restrictions] 6389 // An ordered construct with the simd clause is the only OpenMP construct 6390 // that can appear in the simd region. 6391 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 6392 ErrorFound = true; 6393 } else if (DependFound && (TC || SC)) { 6394 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 6395 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 6396 ErrorFound = true; 6397 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 6398 Diag(DependFound->getBeginLoc(), 6399 diag::err_omp_ordered_directive_without_param); 6400 ErrorFound = true; 6401 } else if (TC || Clauses.empty()) { 6402 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 6403 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 6404 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 6405 << (TC != nullptr); 6406 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param); 6407 ErrorFound = true; 6408 } 6409 } 6410 if ((!AStmt && !DependFound) || ErrorFound) 6411 return StmtError(); 6412 6413 if (AStmt) { 6414 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6415 6416 setFunctionHasBranchProtectedScope(); 6417 } 6418 6419 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6420 } 6421 6422 namespace { 6423 /// Helper class for checking expression in 'omp atomic [update]' 6424 /// construct. 6425 class OpenMPAtomicUpdateChecker { 6426 /// Error results for atomic update expressions. 6427 enum ExprAnalysisErrorCode { 6428 /// A statement is not an expression statement. 6429 NotAnExpression, 6430 /// Expression is not builtin binary or unary operation. 6431 NotABinaryOrUnaryExpression, 6432 /// Unary operation is not post-/pre- increment/decrement operation. 6433 NotAnUnaryIncDecExpression, 6434 /// An expression is not of scalar type. 6435 NotAScalarType, 6436 /// A binary operation is not an assignment operation. 6437 NotAnAssignmentOp, 6438 /// RHS part of the binary operation is not a binary expression. 6439 NotABinaryExpression, 6440 /// RHS part is not additive/multiplicative/shift/biwise binary 6441 /// expression. 6442 NotABinaryOperator, 6443 /// RHS binary operation does not have reference to the updated LHS 6444 /// part. 6445 NotAnUpdateExpression, 6446 /// No errors is found. 6447 NoError 6448 }; 6449 /// Reference to Sema. 6450 Sema &SemaRef; 6451 /// A location for note diagnostics (when error is found). 6452 SourceLocation NoteLoc; 6453 /// 'x' lvalue part of the source atomic expression. 6454 Expr *X; 6455 /// 'expr' rvalue part of the source atomic expression. 6456 Expr *E; 6457 /// Helper expression of the form 6458 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6459 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6460 Expr *UpdateExpr; 6461 /// Is 'x' a LHS in a RHS part of full update expression. It is 6462 /// important for non-associative operations. 6463 bool IsXLHSInRHSPart; 6464 BinaryOperatorKind Op; 6465 SourceLocation OpLoc; 6466 /// true if the source expression is a postfix unary operation, false 6467 /// if it is a prefix unary operation. 6468 bool IsPostfixUpdate; 6469 6470 public: 6471 OpenMPAtomicUpdateChecker(Sema &SemaRef) 6472 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 6473 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 6474 /// Check specified statement that it is suitable for 'atomic update' 6475 /// constructs and extract 'x', 'expr' and Operation from the original 6476 /// expression. If DiagId and NoteId == 0, then only check is performed 6477 /// without error notification. 6478 /// \param DiagId Diagnostic which should be emitted if error is found. 6479 /// \param NoteId Diagnostic note for the main error message. 6480 /// \return true if statement is not an update expression, false otherwise. 6481 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 6482 /// Return the 'x' lvalue part of the source atomic expression. 6483 Expr *getX() const { return X; } 6484 /// Return the 'expr' rvalue part of the source atomic expression. 6485 Expr *getExpr() const { return E; } 6486 /// Return the update expression used in calculation of the updated 6487 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6488 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6489 Expr *getUpdateExpr() const { return UpdateExpr; } 6490 /// Return true if 'x' is LHS in RHS part of full update expression, 6491 /// false otherwise. 6492 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 6493 6494 /// true if the source expression is a postfix unary operation, false 6495 /// if it is a prefix unary operation. 6496 bool isPostfixUpdate() const { return IsPostfixUpdate; } 6497 6498 private: 6499 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 6500 unsigned NoteId = 0); 6501 }; 6502 } // namespace 6503 6504 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 6505 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 6506 ExprAnalysisErrorCode ErrorFound = NoError; 6507 SourceLocation ErrorLoc, NoteLoc; 6508 SourceRange ErrorRange, NoteRange; 6509 // Allowed constructs are: 6510 // x = x binop expr; 6511 // x = expr binop x; 6512 if (AtomicBinOp->getOpcode() == BO_Assign) { 6513 X = AtomicBinOp->getLHS(); 6514 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 6515 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 6516 if (AtomicInnerBinOp->isMultiplicativeOp() || 6517 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 6518 AtomicInnerBinOp->isBitwiseOp()) { 6519 Op = AtomicInnerBinOp->getOpcode(); 6520 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 6521 Expr *LHS = AtomicInnerBinOp->getLHS(); 6522 Expr *RHS = AtomicInnerBinOp->getRHS(); 6523 llvm::FoldingSetNodeID XId, LHSId, RHSId; 6524 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 6525 /*Canonical=*/true); 6526 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 6527 /*Canonical=*/true); 6528 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 6529 /*Canonical=*/true); 6530 if (XId == LHSId) { 6531 E = RHS; 6532 IsXLHSInRHSPart = true; 6533 } else if (XId == RHSId) { 6534 E = LHS; 6535 IsXLHSInRHSPart = false; 6536 } else { 6537 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 6538 ErrorRange = AtomicInnerBinOp->getSourceRange(); 6539 NoteLoc = X->getExprLoc(); 6540 NoteRange = X->getSourceRange(); 6541 ErrorFound = NotAnUpdateExpression; 6542 } 6543 } else { 6544 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 6545 ErrorRange = AtomicInnerBinOp->getSourceRange(); 6546 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 6547 NoteRange = SourceRange(NoteLoc, NoteLoc); 6548 ErrorFound = NotABinaryOperator; 6549 } 6550 } else { 6551 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 6552 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 6553 ErrorFound = NotABinaryExpression; 6554 } 6555 } else { 6556 ErrorLoc = AtomicBinOp->getExprLoc(); 6557 ErrorRange = AtomicBinOp->getSourceRange(); 6558 NoteLoc = AtomicBinOp->getOperatorLoc(); 6559 NoteRange = SourceRange(NoteLoc, NoteLoc); 6560 ErrorFound = NotAnAssignmentOp; 6561 } 6562 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6563 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6564 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6565 return true; 6566 } 6567 if (SemaRef.CurContext->isDependentContext()) 6568 E = X = UpdateExpr = nullptr; 6569 return ErrorFound != NoError; 6570 } 6571 6572 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 6573 unsigned NoteId) { 6574 ExprAnalysisErrorCode ErrorFound = NoError; 6575 SourceLocation ErrorLoc, NoteLoc; 6576 SourceRange ErrorRange, NoteRange; 6577 // Allowed constructs are: 6578 // x++; 6579 // x--; 6580 // ++x; 6581 // --x; 6582 // x binop= expr; 6583 // x = x binop expr; 6584 // x = expr binop x; 6585 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 6586 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 6587 if (AtomicBody->getType()->isScalarType() || 6588 AtomicBody->isInstantiationDependent()) { 6589 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 6590 AtomicBody->IgnoreParenImpCasts())) { 6591 // Check for Compound Assignment Operation 6592 Op = BinaryOperator::getOpForCompoundAssignment( 6593 AtomicCompAssignOp->getOpcode()); 6594 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 6595 E = AtomicCompAssignOp->getRHS(); 6596 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 6597 IsXLHSInRHSPart = true; 6598 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 6599 AtomicBody->IgnoreParenImpCasts())) { 6600 // Check for Binary Operation 6601 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 6602 return true; 6603 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 6604 AtomicBody->IgnoreParenImpCasts())) { 6605 // Check for Unary Operation 6606 if (AtomicUnaryOp->isIncrementDecrementOp()) { 6607 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 6608 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 6609 OpLoc = AtomicUnaryOp->getOperatorLoc(); 6610 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 6611 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 6612 IsXLHSInRHSPart = true; 6613 } else { 6614 ErrorFound = NotAnUnaryIncDecExpression; 6615 ErrorLoc = AtomicUnaryOp->getExprLoc(); 6616 ErrorRange = AtomicUnaryOp->getSourceRange(); 6617 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 6618 NoteRange = SourceRange(NoteLoc, NoteLoc); 6619 } 6620 } else if (!AtomicBody->isInstantiationDependent()) { 6621 ErrorFound = NotABinaryOrUnaryExpression; 6622 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 6623 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 6624 } 6625 } else { 6626 ErrorFound = NotAScalarType; 6627 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 6628 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6629 } 6630 } else { 6631 ErrorFound = NotAnExpression; 6632 NoteLoc = ErrorLoc = S->getBeginLoc(); 6633 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6634 } 6635 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6636 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6637 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6638 return true; 6639 } 6640 if (SemaRef.CurContext->isDependentContext()) 6641 E = X = UpdateExpr = nullptr; 6642 if (ErrorFound == NoError && E && X) { 6643 // Build an update expression of form 'OpaqueValueExpr(x) binop 6644 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 6645 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 6646 auto *OVEX = new (SemaRef.getASTContext()) 6647 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 6648 auto *OVEExpr = new (SemaRef.getASTContext()) 6649 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 6650 ExprResult Update = 6651 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 6652 IsXLHSInRHSPart ? OVEExpr : OVEX); 6653 if (Update.isInvalid()) 6654 return true; 6655 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 6656 Sema::AA_Casting); 6657 if (Update.isInvalid()) 6658 return true; 6659 UpdateExpr = Update.get(); 6660 } 6661 return ErrorFound != NoError; 6662 } 6663 6664 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 6665 Stmt *AStmt, 6666 SourceLocation StartLoc, 6667 SourceLocation EndLoc) { 6668 if (!AStmt) 6669 return StmtError(); 6670 6671 auto *CS = cast<CapturedStmt>(AStmt); 6672 // 1.2.2 OpenMP Language Terminology 6673 // Structured block - An executable statement with a single entry at the 6674 // top and a single exit at the bottom. 6675 // The point of exit cannot be a branch out of the structured block. 6676 // longjmp() and throw() must not violate the entry/exit criteria. 6677 OpenMPClauseKind AtomicKind = OMPC_unknown; 6678 SourceLocation AtomicKindLoc; 6679 for (const OMPClause *C : Clauses) { 6680 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 6681 C->getClauseKind() == OMPC_update || 6682 C->getClauseKind() == OMPC_capture) { 6683 if (AtomicKind != OMPC_unknown) { 6684 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 6685 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 6686 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 6687 << getOpenMPClauseName(AtomicKind); 6688 } else { 6689 AtomicKind = C->getClauseKind(); 6690 AtomicKindLoc = C->getBeginLoc(); 6691 } 6692 } 6693 } 6694 6695 Stmt *Body = CS->getCapturedStmt(); 6696 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 6697 Body = EWC->getSubExpr(); 6698 6699 Expr *X = nullptr; 6700 Expr *V = nullptr; 6701 Expr *E = nullptr; 6702 Expr *UE = nullptr; 6703 bool IsXLHSInRHSPart = false; 6704 bool IsPostfixUpdate = false; 6705 // OpenMP [2.12.6, atomic Construct] 6706 // In the next expressions: 6707 // * x and v (as applicable) are both l-value expressions with scalar type. 6708 // * During the execution of an atomic region, multiple syntactic 6709 // occurrences of x must designate the same storage location. 6710 // * Neither of v and expr (as applicable) may access the storage location 6711 // designated by x. 6712 // * Neither of x and expr (as applicable) may access the storage location 6713 // designated by v. 6714 // * expr is an expression with scalar type. 6715 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 6716 // * binop, binop=, ++, and -- are not overloaded operators. 6717 // * The expression x binop expr must be numerically equivalent to x binop 6718 // (expr). This requirement is satisfied if the operators in expr have 6719 // precedence greater than binop, or by using parentheses around expr or 6720 // subexpressions of expr. 6721 // * The expression expr binop x must be numerically equivalent to (expr) 6722 // binop x. This requirement is satisfied if the operators in expr have 6723 // precedence equal to or greater than binop, or by using parentheses around 6724 // expr or subexpressions of expr. 6725 // * For forms that allow multiple occurrences of x, the number of times 6726 // that x is evaluated is unspecified. 6727 if (AtomicKind == OMPC_read) { 6728 enum { 6729 NotAnExpression, 6730 NotAnAssignmentOp, 6731 NotAScalarType, 6732 NotAnLValue, 6733 NoError 6734 } ErrorFound = NoError; 6735 SourceLocation ErrorLoc, NoteLoc; 6736 SourceRange ErrorRange, NoteRange; 6737 // If clause is read: 6738 // v = x; 6739 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6740 const auto *AtomicBinOp = 6741 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6742 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6743 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6744 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 6745 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6746 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 6747 if (!X->isLValue() || !V->isLValue()) { 6748 const Expr *NotLValueExpr = X->isLValue() ? V : X; 6749 ErrorFound = NotAnLValue; 6750 ErrorLoc = AtomicBinOp->getExprLoc(); 6751 ErrorRange = AtomicBinOp->getSourceRange(); 6752 NoteLoc = NotLValueExpr->getExprLoc(); 6753 NoteRange = NotLValueExpr->getSourceRange(); 6754 } 6755 } else if (!X->isInstantiationDependent() || 6756 !V->isInstantiationDependent()) { 6757 const Expr *NotScalarExpr = 6758 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6759 ? V 6760 : X; 6761 ErrorFound = NotAScalarType; 6762 ErrorLoc = AtomicBinOp->getExprLoc(); 6763 ErrorRange = AtomicBinOp->getSourceRange(); 6764 NoteLoc = NotScalarExpr->getExprLoc(); 6765 NoteRange = NotScalarExpr->getSourceRange(); 6766 } 6767 } else if (!AtomicBody->isInstantiationDependent()) { 6768 ErrorFound = NotAnAssignmentOp; 6769 ErrorLoc = AtomicBody->getExprLoc(); 6770 ErrorRange = AtomicBody->getSourceRange(); 6771 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6772 : AtomicBody->getExprLoc(); 6773 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6774 : AtomicBody->getSourceRange(); 6775 } 6776 } else { 6777 ErrorFound = NotAnExpression; 6778 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6779 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6780 } 6781 if (ErrorFound != NoError) { 6782 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 6783 << ErrorRange; 6784 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6785 << NoteRange; 6786 return StmtError(); 6787 } 6788 if (CurContext->isDependentContext()) 6789 V = X = nullptr; 6790 } else if (AtomicKind == OMPC_write) { 6791 enum { 6792 NotAnExpression, 6793 NotAnAssignmentOp, 6794 NotAScalarType, 6795 NotAnLValue, 6796 NoError 6797 } ErrorFound = NoError; 6798 SourceLocation ErrorLoc, NoteLoc; 6799 SourceRange ErrorRange, NoteRange; 6800 // If clause is write: 6801 // x = expr; 6802 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6803 const auto *AtomicBinOp = 6804 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6805 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6806 X = AtomicBinOp->getLHS(); 6807 E = AtomicBinOp->getRHS(); 6808 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6809 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 6810 if (!X->isLValue()) { 6811 ErrorFound = NotAnLValue; 6812 ErrorLoc = AtomicBinOp->getExprLoc(); 6813 ErrorRange = AtomicBinOp->getSourceRange(); 6814 NoteLoc = X->getExprLoc(); 6815 NoteRange = X->getSourceRange(); 6816 } 6817 } else if (!X->isInstantiationDependent() || 6818 !E->isInstantiationDependent()) { 6819 const Expr *NotScalarExpr = 6820 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6821 ? E 6822 : X; 6823 ErrorFound = NotAScalarType; 6824 ErrorLoc = AtomicBinOp->getExprLoc(); 6825 ErrorRange = AtomicBinOp->getSourceRange(); 6826 NoteLoc = NotScalarExpr->getExprLoc(); 6827 NoteRange = NotScalarExpr->getSourceRange(); 6828 } 6829 } else if (!AtomicBody->isInstantiationDependent()) { 6830 ErrorFound = NotAnAssignmentOp; 6831 ErrorLoc = AtomicBody->getExprLoc(); 6832 ErrorRange = AtomicBody->getSourceRange(); 6833 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6834 : AtomicBody->getExprLoc(); 6835 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6836 : AtomicBody->getSourceRange(); 6837 } 6838 } else { 6839 ErrorFound = NotAnExpression; 6840 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6841 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6842 } 6843 if (ErrorFound != NoError) { 6844 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 6845 << ErrorRange; 6846 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6847 << NoteRange; 6848 return StmtError(); 6849 } 6850 if (CurContext->isDependentContext()) 6851 E = X = nullptr; 6852 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 6853 // If clause is update: 6854 // x++; 6855 // x--; 6856 // ++x; 6857 // --x; 6858 // x binop= expr; 6859 // x = x binop expr; 6860 // x = expr binop x; 6861 OpenMPAtomicUpdateChecker Checker(*this); 6862 if (Checker.checkStatement( 6863 Body, (AtomicKind == OMPC_update) 6864 ? diag::err_omp_atomic_update_not_expression_statement 6865 : diag::err_omp_atomic_not_expression_statement, 6866 diag::note_omp_atomic_update)) 6867 return StmtError(); 6868 if (!CurContext->isDependentContext()) { 6869 E = Checker.getExpr(); 6870 X = Checker.getX(); 6871 UE = Checker.getUpdateExpr(); 6872 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6873 } 6874 } else if (AtomicKind == OMPC_capture) { 6875 enum { 6876 NotAnAssignmentOp, 6877 NotACompoundStatement, 6878 NotTwoSubstatements, 6879 NotASpecificExpression, 6880 NoError 6881 } ErrorFound = NoError; 6882 SourceLocation ErrorLoc, NoteLoc; 6883 SourceRange ErrorRange, NoteRange; 6884 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6885 // If clause is a capture: 6886 // v = x++; 6887 // v = x--; 6888 // v = ++x; 6889 // v = --x; 6890 // v = x binop= expr; 6891 // v = x = x binop expr; 6892 // v = x = expr binop x; 6893 const auto *AtomicBinOp = 6894 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6895 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6896 V = AtomicBinOp->getLHS(); 6897 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6898 OpenMPAtomicUpdateChecker Checker(*this); 6899 if (Checker.checkStatement( 6900 Body, diag::err_omp_atomic_capture_not_expression_statement, 6901 diag::note_omp_atomic_update)) 6902 return StmtError(); 6903 E = Checker.getExpr(); 6904 X = Checker.getX(); 6905 UE = Checker.getUpdateExpr(); 6906 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6907 IsPostfixUpdate = Checker.isPostfixUpdate(); 6908 } else if (!AtomicBody->isInstantiationDependent()) { 6909 ErrorLoc = AtomicBody->getExprLoc(); 6910 ErrorRange = AtomicBody->getSourceRange(); 6911 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6912 : AtomicBody->getExprLoc(); 6913 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6914 : AtomicBody->getSourceRange(); 6915 ErrorFound = NotAnAssignmentOp; 6916 } 6917 if (ErrorFound != NoError) { 6918 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 6919 << ErrorRange; 6920 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6921 return StmtError(); 6922 } 6923 if (CurContext->isDependentContext()) 6924 UE = V = E = X = nullptr; 6925 } else { 6926 // If clause is a capture: 6927 // { v = x; x = expr; } 6928 // { v = x; x++; } 6929 // { v = x; x--; } 6930 // { v = x; ++x; } 6931 // { v = x; --x; } 6932 // { v = x; x binop= expr; } 6933 // { v = x; x = x binop expr; } 6934 // { v = x; x = expr binop x; } 6935 // { x++; v = x; } 6936 // { x--; v = x; } 6937 // { ++x; v = x; } 6938 // { --x; v = x; } 6939 // { x binop= expr; v = x; } 6940 // { x = x binop expr; v = x; } 6941 // { x = expr binop x; v = x; } 6942 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 6943 // Check that this is { expr1; expr2; } 6944 if (CS->size() == 2) { 6945 Stmt *First = CS->body_front(); 6946 Stmt *Second = CS->body_back(); 6947 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 6948 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 6949 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 6950 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 6951 // Need to find what subexpression is 'v' and what is 'x'. 6952 OpenMPAtomicUpdateChecker Checker(*this); 6953 bool IsUpdateExprFound = !Checker.checkStatement(Second); 6954 BinaryOperator *BinOp = nullptr; 6955 if (IsUpdateExprFound) { 6956 BinOp = dyn_cast<BinaryOperator>(First); 6957 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6958 } 6959 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6960 // { v = x; x++; } 6961 // { v = x; x--; } 6962 // { v = x; ++x; } 6963 // { v = x; --x; } 6964 // { v = x; x binop= expr; } 6965 // { v = x; x = x binop expr; } 6966 // { v = x; x = expr binop x; } 6967 // Check that the first expression has form v = x. 6968 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6969 llvm::FoldingSetNodeID XId, PossibleXId; 6970 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6971 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6972 IsUpdateExprFound = XId == PossibleXId; 6973 if (IsUpdateExprFound) { 6974 V = BinOp->getLHS(); 6975 X = Checker.getX(); 6976 E = Checker.getExpr(); 6977 UE = Checker.getUpdateExpr(); 6978 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6979 IsPostfixUpdate = true; 6980 } 6981 } 6982 if (!IsUpdateExprFound) { 6983 IsUpdateExprFound = !Checker.checkStatement(First); 6984 BinOp = nullptr; 6985 if (IsUpdateExprFound) { 6986 BinOp = dyn_cast<BinaryOperator>(Second); 6987 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6988 } 6989 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6990 // { x++; v = x; } 6991 // { x--; v = x; } 6992 // { ++x; v = x; } 6993 // { --x; v = x; } 6994 // { x binop= expr; v = x; } 6995 // { x = x binop expr; v = x; } 6996 // { x = expr binop x; v = x; } 6997 // Check that the second expression has form v = x. 6998 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6999 llvm::FoldingSetNodeID XId, PossibleXId; 7000 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 7001 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 7002 IsUpdateExprFound = XId == PossibleXId; 7003 if (IsUpdateExprFound) { 7004 V = BinOp->getLHS(); 7005 X = Checker.getX(); 7006 E = Checker.getExpr(); 7007 UE = Checker.getUpdateExpr(); 7008 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7009 IsPostfixUpdate = false; 7010 } 7011 } 7012 } 7013 if (!IsUpdateExprFound) { 7014 // { v = x; x = expr; } 7015 auto *FirstExpr = dyn_cast<Expr>(First); 7016 auto *SecondExpr = dyn_cast<Expr>(Second); 7017 if (!FirstExpr || !SecondExpr || 7018 !(FirstExpr->isInstantiationDependent() || 7019 SecondExpr->isInstantiationDependent())) { 7020 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 7021 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 7022 ErrorFound = NotAnAssignmentOp; 7023 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 7024 : First->getBeginLoc(); 7025 NoteRange = ErrorRange = FirstBinOp 7026 ? FirstBinOp->getSourceRange() 7027 : SourceRange(ErrorLoc, ErrorLoc); 7028 } else { 7029 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 7030 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 7031 ErrorFound = NotAnAssignmentOp; 7032 NoteLoc = ErrorLoc = SecondBinOp 7033 ? SecondBinOp->getOperatorLoc() 7034 : Second->getBeginLoc(); 7035 NoteRange = ErrorRange = 7036 SecondBinOp ? SecondBinOp->getSourceRange() 7037 : SourceRange(ErrorLoc, ErrorLoc); 7038 } else { 7039 Expr *PossibleXRHSInFirst = 7040 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 7041 Expr *PossibleXLHSInSecond = 7042 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 7043 llvm::FoldingSetNodeID X1Id, X2Id; 7044 PossibleXRHSInFirst->Profile(X1Id, Context, 7045 /*Canonical=*/true); 7046 PossibleXLHSInSecond->Profile(X2Id, Context, 7047 /*Canonical=*/true); 7048 IsUpdateExprFound = X1Id == X2Id; 7049 if (IsUpdateExprFound) { 7050 V = FirstBinOp->getLHS(); 7051 X = SecondBinOp->getLHS(); 7052 E = SecondBinOp->getRHS(); 7053 UE = nullptr; 7054 IsXLHSInRHSPart = false; 7055 IsPostfixUpdate = true; 7056 } else { 7057 ErrorFound = NotASpecificExpression; 7058 ErrorLoc = FirstBinOp->getExprLoc(); 7059 ErrorRange = FirstBinOp->getSourceRange(); 7060 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 7061 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 7062 } 7063 } 7064 } 7065 } 7066 } 7067 } else { 7068 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7069 NoteRange = ErrorRange = 7070 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7071 ErrorFound = NotTwoSubstatements; 7072 } 7073 } else { 7074 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7075 NoteRange = ErrorRange = 7076 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7077 ErrorFound = NotACompoundStatement; 7078 } 7079 if (ErrorFound != NoError) { 7080 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 7081 << ErrorRange; 7082 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 7083 return StmtError(); 7084 } 7085 if (CurContext->isDependentContext()) 7086 UE = V = E = X = nullptr; 7087 } 7088 } 7089 7090 setFunctionHasBranchProtectedScope(); 7091 7092 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7093 X, V, E, UE, IsXLHSInRHSPart, 7094 IsPostfixUpdate); 7095 } 7096 7097 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 7098 Stmt *AStmt, 7099 SourceLocation StartLoc, 7100 SourceLocation EndLoc) { 7101 if (!AStmt) 7102 return StmtError(); 7103 7104 auto *CS = cast<CapturedStmt>(AStmt); 7105 // 1.2.2 OpenMP Language Terminology 7106 // Structured block - An executable statement with a single entry at the 7107 // top and a single exit at the bottom. 7108 // The point of exit cannot be a branch out of the structured block. 7109 // longjmp() and throw() must not violate the entry/exit criteria. 7110 CS->getCapturedDecl()->setNothrow(); 7111 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 7112 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7113 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7114 // 1.2.2 OpenMP Language Terminology 7115 // Structured block - An executable statement with a single entry at the 7116 // top and a single exit at the bottom. 7117 // The point of exit cannot be a branch out of the structured block. 7118 // longjmp() and throw() must not violate the entry/exit criteria. 7119 CS->getCapturedDecl()->setNothrow(); 7120 } 7121 7122 // OpenMP [2.16, Nesting of Regions] 7123 // If specified, a teams construct must be contained within a target 7124 // construct. That target construct must contain no statements or directives 7125 // outside of the teams construct. 7126 if (DSAStack->hasInnerTeamsRegion()) { 7127 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 7128 bool OMPTeamsFound = true; 7129 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 7130 auto I = CS->body_begin(); 7131 while (I != CS->body_end()) { 7132 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 7133 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 7134 OMPTeamsFound) { 7135 7136 OMPTeamsFound = false; 7137 break; 7138 } 7139 ++I; 7140 } 7141 assert(I != CS->body_end() && "Not found statement"); 7142 S = *I; 7143 } else { 7144 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 7145 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 7146 } 7147 if (!OMPTeamsFound) { 7148 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 7149 Diag(DSAStack->getInnerTeamsRegionLoc(), 7150 diag::note_omp_nested_teams_construct_here); 7151 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 7152 << isa<OMPExecutableDirective>(S); 7153 return StmtError(); 7154 } 7155 } 7156 7157 setFunctionHasBranchProtectedScope(); 7158 7159 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7160 } 7161 7162 StmtResult 7163 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 7164 Stmt *AStmt, SourceLocation StartLoc, 7165 SourceLocation EndLoc) { 7166 if (!AStmt) 7167 return StmtError(); 7168 7169 auto *CS = cast<CapturedStmt>(AStmt); 7170 // 1.2.2 OpenMP Language Terminology 7171 // Structured block - An executable statement with a single entry at the 7172 // top and a single exit at the bottom. 7173 // The point of exit cannot be a branch out of the structured block. 7174 // longjmp() and throw() must not violate the entry/exit criteria. 7175 CS->getCapturedDecl()->setNothrow(); 7176 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 7177 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7178 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7179 // 1.2.2 OpenMP Language Terminology 7180 // Structured block - An executable statement with a single entry at the 7181 // top and a single exit at the bottom. 7182 // The point of exit cannot be a branch out of the structured block. 7183 // longjmp() and throw() must not violate the entry/exit criteria. 7184 CS->getCapturedDecl()->setNothrow(); 7185 } 7186 7187 setFunctionHasBranchProtectedScope(); 7188 7189 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7190 AStmt); 7191 } 7192 7193 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 7194 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7195 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7196 if (!AStmt) 7197 return StmtError(); 7198 7199 auto *CS = cast<CapturedStmt>(AStmt); 7200 // 1.2.2 OpenMP Language Terminology 7201 // Structured block - An executable statement with a single entry at the 7202 // top and a single exit at the bottom. 7203 // The point of exit cannot be a branch out of the structured block. 7204 // longjmp() and throw() must not violate the entry/exit criteria. 7205 CS->getCapturedDecl()->setNothrow(); 7206 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7207 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7208 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7209 // 1.2.2 OpenMP Language Terminology 7210 // Structured block - An executable statement with a single entry at the 7211 // top and a single exit at the bottom. 7212 // The point of exit cannot be a branch out of the structured block. 7213 // longjmp() and throw() must not violate the entry/exit criteria. 7214 CS->getCapturedDecl()->setNothrow(); 7215 } 7216 7217 OMPLoopDirective::HelperExprs B; 7218 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7219 // define the nested loops number. 7220 unsigned NestedLoopCount = 7221 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 7222 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7223 VarsWithImplicitDSA, B); 7224 if (NestedLoopCount == 0) 7225 return StmtError(); 7226 7227 assert((CurContext->isDependentContext() || B.builtAll()) && 7228 "omp target parallel for loop exprs were not built"); 7229 7230 if (!CurContext->isDependentContext()) { 7231 // Finalize the clauses that need pre-built expressions for CodeGen. 7232 for (OMPClause *C : Clauses) { 7233 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7234 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7235 B.NumIterations, *this, CurScope, 7236 DSAStack)) 7237 return StmtError(); 7238 } 7239 } 7240 7241 setFunctionHasBranchProtectedScope(); 7242 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 7243 NestedLoopCount, Clauses, AStmt, 7244 B, DSAStack->isCancelRegion()); 7245 } 7246 7247 /// Check for existence of a map clause in the list of clauses. 7248 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 7249 const OpenMPClauseKind K) { 7250 return llvm::any_of( 7251 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 7252 } 7253 7254 template <typename... Params> 7255 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 7256 const Params... ClauseTypes) { 7257 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 7258 } 7259 7260 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 7261 Stmt *AStmt, 7262 SourceLocation StartLoc, 7263 SourceLocation EndLoc) { 7264 if (!AStmt) 7265 return StmtError(); 7266 7267 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7268 7269 // OpenMP [2.10.1, Restrictions, p. 97] 7270 // At least one map clause must appear on the directive. 7271 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 7272 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7273 << "'map' or 'use_device_ptr'" 7274 << getOpenMPDirectiveName(OMPD_target_data); 7275 return StmtError(); 7276 } 7277 7278 setFunctionHasBranchProtectedScope(); 7279 7280 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7281 AStmt); 7282 } 7283 7284 StmtResult 7285 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 7286 SourceLocation StartLoc, 7287 SourceLocation EndLoc, Stmt *AStmt) { 7288 if (!AStmt) 7289 return StmtError(); 7290 7291 auto *CS = cast<CapturedStmt>(AStmt); 7292 // 1.2.2 OpenMP Language Terminology 7293 // Structured block - An executable statement with a single entry at the 7294 // top and a single exit at the bottom. 7295 // The point of exit cannot be a branch out of the structured block. 7296 // longjmp() and throw() must not violate the entry/exit criteria. 7297 CS->getCapturedDecl()->setNothrow(); 7298 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 7299 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7300 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7301 // 1.2.2 OpenMP Language Terminology 7302 // Structured block - An executable statement with a single entry at the 7303 // top and a single exit at the bottom. 7304 // The point of exit cannot be a branch out of the structured block. 7305 // longjmp() and throw() must not violate the entry/exit criteria. 7306 CS->getCapturedDecl()->setNothrow(); 7307 } 7308 7309 // OpenMP [2.10.2, Restrictions, p. 99] 7310 // At least one map clause must appear on the directive. 7311 if (!hasClauses(Clauses, OMPC_map)) { 7312 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7313 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 7314 return StmtError(); 7315 } 7316 7317 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7318 AStmt); 7319 } 7320 7321 StmtResult 7322 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 7323 SourceLocation StartLoc, 7324 SourceLocation EndLoc, Stmt *AStmt) { 7325 if (!AStmt) 7326 return StmtError(); 7327 7328 auto *CS = cast<CapturedStmt>(AStmt); 7329 // 1.2.2 OpenMP Language Terminology 7330 // Structured block - An executable statement with a single entry at the 7331 // top and a single exit at the bottom. 7332 // The point of exit cannot be a branch out of the structured block. 7333 // longjmp() and throw() must not violate the entry/exit criteria. 7334 CS->getCapturedDecl()->setNothrow(); 7335 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 7336 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7337 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7338 // 1.2.2 OpenMP Language Terminology 7339 // Structured block - An executable statement with a single entry at the 7340 // top and a single exit at the bottom. 7341 // The point of exit cannot be a branch out of the structured block. 7342 // longjmp() and throw() must not violate the entry/exit criteria. 7343 CS->getCapturedDecl()->setNothrow(); 7344 } 7345 7346 // OpenMP [2.10.3, Restrictions, p. 102] 7347 // At least one map clause must appear on the directive. 7348 if (!hasClauses(Clauses, OMPC_map)) { 7349 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7350 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 7351 return StmtError(); 7352 } 7353 7354 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7355 AStmt); 7356 } 7357 7358 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 7359 SourceLocation StartLoc, 7360 SourceLocation EndLoc, 7361 Stmt *AStmt) { 7362 if (!AStmt) 7363 return StmtError(); 7364 7365 auto *CS = cast<CapturedStmt>(AStmt); 7366 // 1.2.2 OpenMP Language Terminology 7367 // Structured block - An executable statement with a single entry at the 7368 // top and a single exit at the bottom. 7369 // The point of exit cannot be a branch out of the structured block. 7370 // longjmp() and throw() must not violate the entry/exit criteria. 7371 CS->getCapturedDecl()->setNothrow(); 7372 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 7373 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7374 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7375 // 1.2.2 OpenMP Language Terminology 7376 // Structured block - An executable statement with a single entry at the 7377 // top and a single exit at the bottom. 7378 // The point of exit cannot be a branch out of the structured block. 7379 // longjmp() and throw() must not violate the entry/exit criteria. 7380 CS->getCapturedDecl()->setNothrow(); 7381 } 7382 7383 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 7384 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 7385 return StmtError(); 7386 } 7387 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 7388 AStmt); 7389 } 7390 7391 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 7392 Stmt *AStmt, SourceLocation StartLoc, 7393 SourceLocation EndLoc) { 7394 if (!AStmt) 7395 return StmtError(); 7396 7397 auto *CS = cast<CapturedStmt>(AStmt); 7398 // 1.2.2 OpenMP Language Terminology 7399 // Structured block - An executable statement with a single entry at the 7400 // top and a single exit at the bottom. 7401 // The point of exit cannot be a branch out of the structured block. 7402 // longjmp() and throw() must not violate the entry/exit criteria. 7403 CS->getCapturedDecl()->setNothrow(); 7404 7405 setFunctionHasBranchProtectedScope(); 7406 7407 DSAStack->setParentTeamsRegionLoc(StartLoc); 7408 7409 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7410 } 7411 7412 StmtResult 7413 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 7414 SourceLocation EndLoc, 7415 OpenMPDirectiveKind CancelRegion) { 7416 if (DSAStack->isParentNowaitRegion()) { 7417 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 7418 return StmtError(); 7419 } 7420 if (DSAStack->isParentOrderedRegion()) { 7421 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 7422 return StmtError(); 7423 } 7424 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 7425 CancelRegion); 7426 } 7427 7428 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 7429 SourceLocation StartLoc, 7430 SourceLocation EndLoc, 7431 OpenMPDirectiveKind CancelRegion) { 7432 if (DSAStack->isParentNowaitRegion()) { 7433 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 7434 return StmtError(); 7435 } 7436 if (DSAStack->isParentOrderedRegion()) { 7437 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 7438 return StmtError(); 7439 } 7440 DSAStack->setParentCancelRegion(/*Cancel=*/true); 7441 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7442 CancelRegion); 7443 } 7444 7445 static bool checkGrainsizeNumTasksClauses(Sema &S, 7446 ArrayRef<OMPClause *> Clauses) { 7447 const OMPClause *PrevClause = nullptr; 7448 bool ErrorFound = false; 7449 for (const OMPClause *C : Clauses) { 7450 if (C->getClauseKind() == OMPC_grainsize || 7451 C->getClauseKind() == OMPC_num_tasks) { 7452 if (!PrevClause) 7453 PrevClause = C; 7454 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 7455 S.Diag(C->getBeginLoc(), 7456 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 7457 << getOpenMPClauseName(C->getClauseKind()) 7458 << getOpenMPClauseName(PrevClause->getClauseKind()); 7459 S.Diag(PrevClause->getBeginLoc(), 7460 diag::note_omp_previous_grainsize_num_tasks) 7461 << getOpenMPClauseName(PrevClause->getClauseKind()); 7462 ErrorFound = true; 7463 } 7464 } 7465 } 7466 return ErrorFound; 7467 } 7468 7469 static bool checkReductionClauseWithNogroup(Sema &S, 7470 ArrayRef<OMPClause *> Clauses) { 7471 const OMPClause *ReductionClause = nullptr; 7472 const OMPClause *NogroupClause = nullptr; 7473 for (const OMPClause *C : Clauses) { 7474 if (C->getClauseKind() == OMPC_reduction) { 7475 ReductionClause = C; 7476 if (NogroupClause) 7477 break; 7478 continue; 7479 } 7480 if (C->getClauseKind() == OMPC_nogroup) { 7481 NogroupClause = C; 7482 if (ReductionClause) 7483 break; 7484 continue; 7485 } 7486 } 7487 if (ReductionClause && NogroupClause) { 7488 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 7489 << SourceRange(NogroupClause->getBeginLoc(), 7490 NogroupClause->getEndLoc()); 7491 return true; 7492 } 7493 return false; 7494 } 7495 7496 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 7497 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7498 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7499 if (!AStmt) 7500 return StmtError(); 7501 7502 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7503 OMPLoopDirective::HelperExprs B; 7504 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7505 // define the nested loops number. 7506 unsigned NestedLoopCount = 7507 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 7508 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 7509 VarsWithImplicitDSA, B); 7510 if (NestedLoopCount == 0) 7511 return StmtError(); 7512 7513 assert((CurContext->isDependentContext() || B.builtAll()) && 7514 "omp for loop exprs were not built"); 7515 7516 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7517 // The grainsize clause and num_tasks clause are mutually exclusive and may 7518 // not appear on the same taskloop directive. 7519 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7520 return StmtError(); 7521 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7522 // If a reduction clause is present on the taskloop directive, the nogroup 7523 // clause must not be specified. 7524 if (checkReductionClauseWithNogroup(*this, Clauses)) 7525 return StmtError(); 7526 7527 setFunctionHasBranchProtectedScope(); 7528 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 7529 NestedLoopCount, Clauses, AStmt, B); 7530 } 7531 7532 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 7533 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7534 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7535 if (!AStmt) 7536 return StmtError(); 7537 7538 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7539 OMPLoopDirective::HelperExprs B; 7540 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7541 // define the nested loops number. 7542 unsigned NestedLoopCount = 7543 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 7544 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 7545 VarsWithImplicitDSA, B); 7546 if (NestedLoopCount == 0) 7547 return StmtError(); 7548 7549 assert((CurContext->isDependentContext() || B.builtAll()) && 7550 "omp for loop exprs were not built"); 7551 7552 if (!CurContext->isDependentContext()) { 7553 // Finalize the clauses that need pre-built expressions for CodeGen. 7554 for (OMPClause *C : Clauses) { 7555 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7556 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7557 B.NumIterations, *this, CurScope, 7558 DSAStack)) 7559 return StmtError(); 7560 } 7561 } 7562 7563 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7564 // The grainsize clause and num_tasks clause are mutually exclusive and may 7565 // not appear on the same taskloop directive. 7566 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7567 return StmtError(); 7568 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7569 // If a reduction clause is present on the taskloop directive, the nogroup 7570 // clause must not be specified. 7571 if (checkReductionClauseWithNogroup(*this, Clauses)) 7572 return StmtError(); 7573 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7574 return StmtError(); 7575 7576 setFunctionHasBranchProtectedScope(); 7577 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 7578 NestedLoopCount, Clauses, AStmt, B); 7579 } 7580 7581 StmtResult Sema::ActOnOpenMPDistributeDirective( 7582 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7583 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7584 if (!AStmt) 7585 return StmtError(); 7586 7587 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7588 OMPLoopDirective::HelperExprs B; 7589 // In presence of clause 'collapse' with number of loops, it will 7590 // define the nested loops number. 7591 unsigned NestedLoopCount = 7592 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 7593 nullptr /*ordered not a clause on distribute*/, AStmt, 7594 *this, *DSAStack, VarsWithImplicitDSA, B); 7595 if (NestedLoopCount == 0) 7596 return StmtError(); 7597 7598 assert((CurContext->isDependentContext() || B.builtAll()) && 7599 "omp for loop exprs were not built"); 7600 7601 setFunctionHasBranchProtectedScope(); 7602 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 7603 NestedLoopCount, Clauses, AStmt, B); 7604 } 7605 7606 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 7607 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7608 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7609 if (!AStmt) 7610 return StmtError(); 7611 7612 auto *CS = cast<CapturedStmt>(AStmt); 7613 // 1.2.2 OpenMP Language Terminology 7614 // Structured block - An executable statement with a single entry at the 7615 // top and a single exit at the bottom. 7616 // The point of exit cannot be a branch out of the structured block. 7617 // longjmp() and throw() must not violate the entry/exit criteria. 7618 CS->getCapturedDecl()->setNothrow(); 7619 for (int ThisCaptureLevel = 7620 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 7621 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7622 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7623 // 1.2.2 OpenMP Language Terminology 7624 // Structured block - An executable statement with a single entry at the 7625 // top and a single exit at the bottom. 7626 // The point of exit cannot be a branch out of the structured block. 7627 // longjmp() and throw() must not violate the entry/exit criteria. 7628 CS->getCapturedDecl()->setNothrow(); 7629 } 7630 7631 OMPLoopDirective::HelperExprs B; 7632 // In presence of clause 'collapse' with number of loops, it will 7633 // define the nested loops number. 7634 unsigned NestedLoopCount = checkOpenMPLoop( 7635 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7636 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7637 VarsWithImplicitDSA, B); 7638 if (NestedLoopCount == 0) 7639 return StmtError(); 7640 7641 assert((CurContext->isDependentContext() || B.builtAll()) && 7642 "omp for loop exprs were not built"); 7643 7644 setFunctionHasBranchProtectedScope(); 7645 return OMPDistributeParallelForDirective::Create( 7646 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7647 DSAStack->isCancelRegion()); 7648 } 7649 7650 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 7651 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7652 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7653 if (!AStmt) 7654 return StmtError(); 7655 7656 auto *CS = cast<CapturedStmt>(AStmt); 7657 // 1.2.2 OpenMP Language Terminology 7658 // Structured block - An executable statement with a single entry at the 7659 // top and a single exit at the bottom. 7660 // The point of exit cannot be a branch out of the structured block. 7661 // longjmp() and throw() must not violate the entry/exit criteria. 7662 CS->getCapturedDecl()->setNothrow(); 7663 for (int ThisCaptureLevel = 7664 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 7665 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7666 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7667 // 1.2.2 OpenMP Language Terminology 7668 // Structured block - An executable statement with a single entry at the 7669 // top and a single exit at the bottom. 7670 // The point of exit cannot be a branch out of the structured block. 7671 // longjmp() and throw() must not violate the entry/exit criteria. 7672 CS->getCapturedDecl()->setNothrow(); 7673 } 7674 7675 OMPLoopDirective::HelperExprs B; 7676 // In presence of clause 'collapse' with number of loops, it will 7677 // define the nested loops number. 7678 unsigned NestedLoopCount = checkOpenMPLoop( 7679 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7680 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7681 VarsWithImplicitDSA, B); 7682 if (NestedLoopCount == 0) 7683 return StmtError(); 7684 7685 assert((CurContext->isDependentContext() || B.builtAll()) && 7686 "omp for loop exprs were not built"); 7687 7688 if (!CurContext->isDependentContext()) { 7689 // Finalize the clauses that need pre-built expressions for CodeGen. 7690 for (OMPClause *C : Clauses) { 7691 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7692 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7693 B.NumIterations, *this, CurScope, 7694 DSAStack)) 7695 return StmtError(); 7696 } 7697 } 7698 7699 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7700 return StmtError(); 7701 7702 setFunctionHasBranchProtectedScope(); 7703 return OMPDistributeParallelForSimdDirective::Create( 7704 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7705 } 7706 7707 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 7708 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7709 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7710 if (!AStmt) 7711 return StmtError(); 7712 7713 auto *CS = cast<CapturedStmt>(AStmt); 7714 // 1.2.2 OpenMP Language Terminology 7715 // Structured block - An executable statement with a single entry at the 7716 // top and a single exit at the bottom. 7717 // The point of exit cannot be a branch out of the structured block. 7718 // longjmp() and throw() must not violate the entry/exit criteria. 7719 CS->getCapturedDecl()->setNothrow(); 7720 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 7721 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7722 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7723 // 1.2.2 OpenMP Language Terminology 7724 // Structured block - An executable statement with a single entry at the 7725 // top and a single exit at the bottom. 7726 // The point of exit cannot be a branch out of the structured block. 7727 // longjmp() and throw() must not violate the entry/exit criteria. 7728 CS->getCapturedDecl()->setNothrow(); 7729 } 7730 7731 OMPLoopDirective::HelperExprs B; 7732 // In presence of clause 'collapse' with number of loops, it will 7733 // define the nested loops number. 7734 unsigned NestedLoopCount = 7735 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 7736 nullptr /*ordered not a clause on distribute*/, CS, *this, 7737 *DSAStack, VarsWithImplicitDSA, B); 7738 if (NestedLoopCount == 0) 7739 return StmtError(); 7740 7741 assert((CurContext->isDependentContext() || B.builtAll()) && 7742 "omp for loop exprs were not built"); 7743 7744 if (!CurContext->isDependentContext()) { 7745 // Finalize the clauses that need pre-built expressions for CodeGen. 7746 for (OMPClause *C : Clauses) { 7747 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7748 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7749 B.NumIterations, *this, CurScope, 7750 DSAStack)) 7751 return StmtError(); 7752 } 7753 } 7754 7755 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7756 return StmtError(); 7757 7758 setFunctionHasBranchProtectedScope(); 7759 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 7760 NestedLoopCount, Clauses, AStmt, B); 7761 } 7762 7763 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 7764 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7765 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7766 if (!AStmt) 7767 return StmtError(); 7768 7769 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7777 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7778 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7779 // 1.2.2 OpenMP Language Terminology 7780 // Structured block - An executable statement with a single entry at the 7781 // top and a single exit at the bottom. 7782 // The point of exit cannot be a branch out of the structured block. 7783 // longjmp() and throw() must not violate the entry/exit criteria. 7784 CS->getCapturedDecl()->setNothrow(); 7785 } 7786 7787 OMPLoopDirective::HelperExprs B; 7788 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7789 // define the nested loops number. 7790 unsigned NestedLoopCount = checkOpenMPLoop( 7791 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 7792 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7793 VarsWithImplicitDSA, B); 7794 if (NestedLoopCount == 0) 7795 return StmtError(); 7796 7797 assert((CurContext->isDependentContext() || B.builtAll()) && 7798 "omp target parallel for simd loop exprs were not built"); 7799 7800 if (!CurContext->isDependentContext()) { 7801 // Finalize the clauses that need pre-built expressions for CodeGen. 7802 for (OMPClause *C : Clauses) { 7803 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7804 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7805 B.NumIterations, *this, CurScope, 7806 DSAStack)) 7807 return StmtError(); 7808 } 7809 } 7810 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7811 return StmtError(); 7812 7813 setFunctionHasBranchProtectedScope(); 7814 return OMPTargetParallelForSimdDirective::Create( 7815 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7816 } 7817 7818 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 7819 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7820 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7821 if (!AStmt) 7822 return StmtError(); 7823 7824 auto *CS = cast<CapturedStmt>(AStmt); 7825 // 1.2.2 OpenMP Language Terminology 7826 // Structured block - An executable statement with a single entry at the 7827 // top and a single exit at the bottom. 7828 // The point of exit cannot be a branch out of the structured block. 7829 // longjmp() and throw() must not violate the entry/exit criteria. 7830 CS->getCapturedDecl()->setNothrow(); 7831 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 7832 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7833 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7834 // 1.2.2 OpenMP Language Terminology 7835 // Structured block - An executable statement with a single entry at the 7836 // top and a single exit at the bottom. 7837 // The point of exit cannot be a branch out of the structured block. 7838 // longjmp() and throw() must not violate the entry/exit criteria. 7839 CS->getCapturedDecl()->setNothrow(); 7840 } 7841 7842 OMPLoopDirective::HelperExprs B; 7843 // In presence of clause 'collapse' with number of loops, it will define the 7844 // nested loops number. 7845 unsigned NestedLoopCount = 7846 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 7847 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7848 VarsWithImplicitDSA, B); 7849 if (NestedLoopCount == 0) 7850 return StmtError(); 7851 7852 assert((CurContext->isDependentContext() || B.builtAll()) && 7853 "omp target simd loop exprs were not built"); 7854 7855 if (!CurContext->isDependentContext()) { 7856 // Finalize the clauses that need pre-built expressions for CodeGen. 7857 for (OMPClause *C : Clauses) { 7858 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7859 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7860 B.NumIterations, *this, CurScope, 7861 DSAStack)) 7862 return StmtError(); 7863 } 7864 } 7865 7866 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7867 return StmtError(); 7868 7869 setFunctionHasBranchProtectedScope(); 7870 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 7871 NestedLoopCount, Clauses, AStmt, B); 7872 } 7873 7874 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 7875 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7876 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7877 if (!AStmt) 7878 return StmtError(); 7879 7880 auto *CS = cast<CapturedStmt>(AStmt); 7881 // 1.2.2 OpenMP Language Terminology 7882 // Structured block - An executable statement with a single entry at the 7883 // top and a single exit at the bottom. 7884 // The point of exit cannot be a branch out of the structured block. 7885 // longjmp() and throw() must not violate the entry/exit criteria. 7886 CS->getCapturedDecl()->setNothrow(); 7887 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 7888 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7889 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7890 // 1.2.2 OpenMP Language Terminology 7891 // Structured block - An executable statement with a single entry at the 7892 // top and a single exit at the bottom. 7893 // The point of exit cannot be a branch out of the structured block. 7894 // longjmp() and throw() must not violate the entry/exit criteria. 7895 CS->getCapturedDecl()->setNothrow(); 7896 } 7897 7898 OMPLoopDirective::HelperExprs B; 7899 // In presence of clause 'collapse' with number of loops, it will 7900 // define the nested loops number. 7901 unsigned NestedLoopCount = 7902 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 7903 nullptr /*ordered not a clause on distribute*/, CS, *this, 7904 *DSAStack, VarsWithImplicitDSA, B); 7905 if (NestedLoopCount == 0) 7906 return StmtError(); 7907 7908 assert((CurContext->isDependentContext() || B.builtAll()) && 7909 "omp teams distribute loop exprs were not built"); 7910 7911 setFunctionHasBranchProtectedScope(); 7912 7913 DSAStack->setParentTeamsRegionLoc(StartLoc); 7914 7915 return OMPTeamsDistributeDirective::Create( 7916 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7917 } 7918 7919 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 7920 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7921 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7922 if (!AStmt) 7923 return StmtError(); 7924 7925 auto *CS = cast<CapturedStmt>(AStmt); 7926 // 1.2.2 OpenMP Language Terminology 7927 // Structured block - An executable statement with a single entry at the 7928 // top and a single exit at the bottom. 7929 // The point of exit cannot be a branch out of the structured block. 7930 // longjmp() and throw() must not violate the entry/exit criteria. 7931 CS->getCapturedDecl()->setNothrow(); 7932 for (int ThisCaptureLevel = 7933 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 7934 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7935 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7936 // 1.2.2 OpenMP Language Terminology 7937 // Structured block - An executable statement with a single entry at the 7938 // top and a single exit at the bottom. 7939 // The point of exit cannot be a branch out of the structured block. 7940 // longjmp() and throw() must not violate the entry/exit criteria. 7941 CS->getCapturedDecl()->setNothrow(); 7942 } 7943 7944 7945 OMPLoopDirective::HelperExprs B; 7946 // In presence of clause 'collapse' with number of loops, it will 7947 // define the nested loops number. 7948 unsigned NestedLoopCount = checkOpenMPLoop( 7949 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7950 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7951 VarsWithImplicitDSA, B); 7952 7953 if (NestedLoopCount == 0) 7954 return StmtError(); 7955 7956 assert((CurContext->isDependentContext() || B.builtAll()) && 7957 "omp teams distribute simd loop exprs were not built"); 7958 7959 if (!CurContext->isDependentContext()) { 7960 // Finalize the clauses that need pre-built expressions for CodeGen. 7961 for (OMPClause *C : Clauses) { 7962 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7963 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7964 B.NumIterations, *this, CurScope, 7965 DSAStack)) 7966 return StmtError(); 7967 } 7968 } 7969 7970 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7971 return StmtError(); 7972 7973 setFunctionHasBranchProtectedScope(); 7974 7975 DSAStack->setParentTeamsRegionLoc(StartLoc); 7976 7977 return OMPTeamsDistributeSimdDirective::Create( 7978 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7979 } 7980 7981 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 7982 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7983 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7984 if (!AStmt) 7985 return StmtError(); 7986 7987 auto *CS = cast<CapturedStmt>(AStmt); 7988 // 1.2.2 OpenMP Language Terminology 7989 // Structured block - An executable statement with a single entry at the 7990 // top and a single exit at the bottom. 7991 // The point of exit cannot be a branch out of the structured block. 7992 // longjmp() and throw() must not violate the entry/exit criteria. 7993 CS->getCapturedDecl()->setNothrow(); 7994 7995 for (int ThisCaptureLevel = 7996 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 7997 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7998 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7999 // 1.2.2 OpenMP Language Terminology 8000 // Structured block - An executable statement with a single entry at the 8001 // top and a single exit at the bottom. 8002 // The point of exit cannot be a branch out of the structured block. 8003 // longjmp() and throw() must not violate the entry/exit criteria. 8004 CS->getCapturedDecl()->setNothrow(); 8005 } 8006 8007 OMPLoopDirective::HelperExprs B; 8008 // In presence of clause 'collapse' with number of loops, it will 8009 // define the nested loops number. 8010 unsigned NestedLoopCount = checkOpenMPLoop( 8011 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 8012 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8013 VarsWithImplicitDSA, B); 8014 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 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8033 return StmtError(); 8034 8035 setFunctionHasBranchProtectedScope(); 8036 8037 DSAStack->setParentTeamsRegionLoc(StartLoc); 8038 8039 return OMPTeamsDistributeParallelForSimdDirective::Create( 8040 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8041 } 8042 8043 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 8044 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8045 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8046 if (!AStmt) 8047 return StmtError(); 8048 8049 auto *CS = cast<CapturedStmt>(AStmt); 8050 // 1.2.2 OpenMP Language Terminology 8051 // Structured block - An executable statement with a single entry at the 8052 // top and a single exit at the bottom. 8053 // The point of exit cannot be a branch out of the structured block. 8054 // longjmp() and throw() must not violate the entry/exit criteria. 8055 CS->getCapturedDecl()->setNothrow(); 8056 8057 for (int ThisCaptureLevel = 8058 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 8059 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8060 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8061 // 1.2.2 OpenMP Language Terminology 8062 // Structured block - An executable statement with a single entry at the 8063 // top and a single exit at the bottom. 8064 // The point of exit cannot be a branch out of the structured block. 8065 // longjmp() and throw() must not violate the entry/exit criteria. 8066 CS->getCapturedDecl()->setNothrow(); 8067 } 8068 8069 OMPLoopDirective::HelperExprs B; 8070 // In presence of clause 'collapse' with number of loops, it will 8071 // define the nested loops number. 8072 unsigned NestedLoopCount = checkOpenMPLoop( 8073 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8074 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8075 VarsWithImplicitDSA, B); 8076 8077 if (NestedLoopCount == 0) 8078 return StmtError(); 8079 8080 assert((CurContext->isDependentContext() || B.builtAll()) && 8081 "omp for loop exprs were not built"); 8082 8083 setFunctionHasBranchProtectedScope(); 8084 8085 DSAStack->setParentTeamsRegionLoc(StartLoc); 8086 8087 return OMPTeamsDistributeParallelForDirective::Create( 8088 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8089 DSAStack->isCancelRegion()); 8090 } 8091 8092 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 8093 Stmt *AStmt, 8094 SourceLocation StartLoc, 8095 SourceLocation EndLoc) { 8096 if (!AStmt) 8097 return StmtError(); 8098 8099 auto *CS = cast<CapturedStmt>(AStmt); 8100 // 1.2.2 OpenMP Language Terminology 8101 // Structured block - An executable statement with a single entry at the 8102 // top and a single exit at the bottom. 8103 // The point of exit cannot be a branch out of the structured block. 8104 // longjmp() and throw() must not violate the entry/exit criteria. 8105 CS->getCapturedDecl()->setNothrow(); 8106 8107 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 8108 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8109 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8110 // 1.2.2 OpenMP Language Terminology 8111 // Structured block - An executable statement with a single entry at the 8112 // top and a single exit at the bottom. 8113 // The point of exit cannot be a branch out of the structured block. 8114 // longjmp() and throw() must not violate the entry/exit criteria. 8115 CS->getCapturedDecl()->setNothrow(); 8116 } 8117 setFunctionHasBranchProtectedScope(); 8118 8119 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 8120 AStmt); 8121 } 8122 8123 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 8124 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8125 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8126 if (!AStmt) 8127 return StmtError(); 8128 8129 auto *CS = cast<CapturedStmt>(AStmt); 8130 // 1.2.2 OpenMP Language Terminology 8131 // Structured block - An executable statement with a single entry at the 8132 // top and a single exit at the bottom. 8133 // The point of exit cannot be a branch out of the structured block. 8134 // longjmp() and throw() must not violate the entry/exit criteria. 8135 CS->getCapturedDecl()->setNothrow(); 8136 for (int ThisCaptureLevel = 8137 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 8138 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8139 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8140 // 1.2.2 OpenMP Language Terminology 8141 // Structured block - An executable statement with a single entry at the 8142 // top and a single exit at the bottom. 8143 // The point of exit cannot be a branch out of the structured block. 8144 // longjmp() and throw() must not violate the entry/exit criteria. 8145 CS->getCapturedDecl()->setNothrow(); 8146 } 8147 8148 OMPLoopDirective::HelperExprs B; 8149 // In presence of clause 'collapse' with number of loops, it will 8150 // define the nested loops number. 8151 unsigned NestedLoopCount = checkOpenMPLoop( 8152 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 8153 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8154 VarsWithImplicitDSA, B); 8155 if (NestedLoopCount == 0) 8156 return StmtError(); 8157 8158 assert((CurContext->isDependentContext() || B.builtAll()) && 8159 "omp target teams distribute loop exprs were not built"); 8160 8161 setFunctionHasBranchProtectedScope(); 8162 return OMPTargetTeamsDistributeDirective::Create( 8163 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8164 } 8165 8166 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 8167 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8168 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8169 if (!AStmt) 8170 return StmtError(); 8171 8172 auto *CS = cast<CapturedStmt>(AStmt); 8173 // 1.2.2 OpenMP Language Terminology 8174 // Structured block - An executable statement with a single entry at the 8175 // top and a single exit at the bottom. 8176 // The point of exit cannot be a branch out of the structured block. 8177 // longjmp() and throw() must not violate the entry/exit criteria. 8178 CS->getCapturedDecl()->setNothrow(); 8179 for (int ThisCaptureLevel = 8180 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 8181 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8182 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 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 } 8190 8191 OMPLoopDirective::HelperExprs B; 8192 // In presence of clause 'collapse' with number of loops, it will 8193 // define the nested loops number. 8194 unsigned NestedLoopCount = checkOpenMPLoop( 8195 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8196 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8197 VarsWithImplicitDSA, B); 8198 if (NestedLoopCount == 0) 8199 return StmtError(); 8200 8201 assert((CurContext->isDependentContext() || B.builtAll()) && 8202 "omp target teams distribute parallel for loop exprs were not built"); 8203 8204 if (!CurContext->isDependentContext()) { 8205 // Finalize the clauses that need pre-built expressions for CodeGen. 8206 for (OMPClause *C : Clauses) { 8207 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8208 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8209 B.NumIterations, *this, CurScope, 8210 DSAStack)) 8211 return StmtError(); 8212 } 8213 } 8214 8215 setFunctionHasBranchProtectedScope(); 8216 return OMPTargetTeamsDistributeParallelForDirective::Create( 8217 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8218 DSAStack->isCancelRegion()); 8219 } 8220 8221 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 8222 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8223 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8224 if (!AStmt) 8225 return StmtError(); 8226 8227 auto *CS = cast<CapturedStmt>(AStmt); 8228 // 1.2.2 OpenMP Language Terminology 8229 // Structured block - An executable statement with a single entry at the 8230 // top and a single exit at the bottom. 8231 // The point of exit cannot be a branch out of the structured block. 8232 // longjmp() and throw() must not violate the entry/exit criteria. 8233 CS->getCapturedDecl()->setNothrow(); 8234 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 8235 OMPD_target_teams_distribute_parallel_for_simd); 8236 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8237 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8238 // 1.2.2 OpenMP Language Terminology 8239 // Structured block - An executable statement with a single entry at the 8240 // top and a single exit at the bottom. 8241 // The point of exit cannot be a branch out of the structured block. 8242 // longjmp() and throw() must not violate the entry/exit criteria. 8243 CS->getCapturedDecl()->setNothrow(); 8244 } 8245 8246 OMPLoopDirective::HelperExprs B; 8247 // In presence of clause 'collapse' with number of loops, it will 8248 // define the nested loops number. 8249 unsigned NestedLoopCount = 8250 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 8251 getCollapseNumberExpr(Clauses), 8252 nullptr /*ordered not a clause on distribute*/, CS, *this, 8253 *DSAStack, VarsWithImplicitDSA, B); 8254 if (NestedLoopCount == 0) 8255 return StmtError(); 8256 8257 assert((CurContext->isDependentContext() || B.builtAll()) && 8258 "omp target teams distribute parallel for simd loop exprs were not " 8259 "built"); 8260 8261 if (!CurContext->isDependentContext()) { 8262 // Finalize the clauses that need pre-built expressions for CodeGen. 8263 for (OMPClause *C : Clauses) { 8264 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8265 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8266 B.NumIterations, *this, CurScope, 8267 DSAStack)) 8268 return StmtError(); 8269 } 8270 } 8271 8272 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8273 return StmtError(); 8274 8275 setFunctionHasBranchProtectedScope(); 8276 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 8277 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8278 } 8279 8280 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 8281 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8282 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8283 if (!AStmt) 8284 return StmtError(); 8285 8286 auto *CS = cast<CapturedStmt>(AStmt); 8287 // 1.2.2 OpenMP Language Terminology 8288 // Structured block - An executable statement with a single entry at the 8289 // top and a single exit at the bottom. 8290 // The point of exit cannot be a branch out of the structured block. 8291 // longjmp() and throw() must not violate the entry/exit criteria. 8292 CS->getCapturedDecl()->setNothrow(); 8293 for (int ThisCaptureLevel = 8294 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 8295 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8296 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8297 // 1.2.2 OpenMP Language Terminology 8298 // Structured block - An executable statement with a single entry at the 8299 // top and a single exit at the bottom. 8300 // The point of exit cannot be a branch out of the structured block. 8301 // longjmp() and throw() must not violate the entry/exit criteria. 8302 CS->getCapturedDecl()->setNothrow(); 8303 } 8304 8305 OMPLoopDirective::HelperExprs B; 8306 // In presence of clause 'collapse' with number of loops, it will 8307 // define the nested loops number. 8308 unsigned NestedLoopCount = checkOpenMPLoop( 8309 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 8310 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8311 VarsWithImplicitDSA, B); 8312 if (NestedLoopCount == 0) 8313 return StmtError(); 8314 8315 assert((CurContext->isDependentContext() || B.builtAll()) && 8316 "omp target teams distribute simd loop exprs were not built"); 8317 8318 if (!CurContext->isDependentContext()) { 8319 // Finalize the clauses that need pre-built expressions for CodeGen. 8320 for (OMPClause *C : Clauses) { 8321 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8322 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8323 B.NumIterations, *this, CurScope, 8324 DSAStack)) 8325 return StmtError(); 8326 } 8327 } 8328 8329 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8330 return StmtError(); 8331 8332 setFunctionHasBranchProtectedScope(); 8333 return OMPTargetTeamsDistributeSimdDirective::Create( 8334 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8335 } 8336 8337 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 8338 SourceLocation StartLoc, 8339 SourceLocation LParenLoc, 8340 SourceLocation EndLoc) { 8341 OMPClause *Res = nullptr; 8342 switch (Kind) { 8343 case OMPC_final: 8344 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 8345 break; 8346 case OMPC_num_threads: 8347 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 8348 break; 8349 case OMPC_safelen: 8350 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 8351 break; 8352 case OMPC_simdlen: 8353 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 8354 break; 8355 case OMPC_collapse: 8356 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 8357 break; 8358 case OMPC_ordered: 8359 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 8360 break; 8361 case OMPC_device: 8362 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 8363 break; 8364 case OMPC_num_teams: 8365 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 8366 break; 8367 case OMPC_thread_limit: 8368 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 8369 break; 8370 case OMPC_priority: 8371 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 8372 break; 8373 case OMPC_grainsize: 8374 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 8375 break; 8376 case OMPC_num_tasks: 8377 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 8378 break; 8379 case OMPC_hint: 8380 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 8381 break; 8382 case OMPC_if: 8383 case OMPC_default: 8384 case OMPC_proc_bind: 8385 case OMPC_schedule: 8386 case OMPC_private: 8387 case OMPC_firstprivate: 8388 case OMPC_lastprivate: 8389 case OMPC_shared: 8390 case OMPC_reduction: 8391 case OMPC_task_reduction: 8392 case OMPC_in_reduction: 8393 case OMPC_linear: 8394 case OMPC_aligned: 8395 case OMPC_copyin: 8396 case OMPC_copyprivate: 8397 case OMPC_nowait: 8398 case OMPC_untied: 8399 case OMPC_mergeable: 8400 case OMPC_threadprivate: 8401 case OMPC_flush: 8402 case OMPC_read: 8403 case OMPC_write: 8404 case OMPC_update: 8405 case OMPC_capture: 8406 case OMPC_seq_cst: 8407 case OMPC_depend: 8408 case OMPC_threads: 8409 case OMPC_simd: 8410 case OMPC_map: 8411 case OMPC_nogroup: 8412 case OMPC_dist_schedule: 8413 case OMPC_defaultmap: 8414 case OMPC_unknown: 8415 case OMPC_uniform: 8416 case OMPC_to: 8417 case OMPC_from: 8418 case OMPC_use_device_ptr: 8419 case OMPC_is_device_ptr: 8420 case OMPC_unified_address: 8421 case OMPC_unified_shared_memory: 8422 case OMPC_reverse_offload: 8423 case OMPC_dynamic_allocators: 8424 case OMPC_atomic_default_mem_order: 8425 llvm_unreachable("Clause is not allowed."); 8426 } 8427 return Res; 8428 } 8429 8430 // An OpenMP directive such as 'target parallel' has two captured regions: 8431 // for the 'target' and 'parallel' respectively. This function returns 8432 // the region in which to capture expressions associated with a clause. 8433 // A return value of OMPD_unknown signifies that the expression should not 8434 // be captured. 8435 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 8436 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 8437 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 8438 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8439 switch (CKind) { 8440 case OMPC_if: 8441 switch (DKind) { 8442 case OMPD_target_parallel: 8443 case OMPD_target_parallel_for: 8444 case OMPD_target_parallel_for_simd: 8445 // If this clause applies to the nested 'parallel' region, capture within 8446 // the 'target' region, otherwise do not capture. 8447 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8448 CaptureRegion = OMPD_target; 8449 break; 8450 case OMPD_target_teams_distribute_parallel_for: 8451 case OMPD_target_teams_distribute_parallel_for_simd: 8452 // If this clause applies to the nested 'parallel' region, capture within 8453 // the 'teams' region, otherwise do not capture. 8454 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8455 CaptureRegion = OMPD_teams; 8456 break; 8457 case OMPD_teams_distribute_parallel_for: 8458 case OMPD_teams_distribute_parallel_for_simd: 8459 CaptureRegion = OMPD_teams; 8460 break; 8461 case OMPD_target_update: 8462 case OMPD_target_enter_data: 8463 case OMPD_target_exit_data: 8464 CaptureRegion = OMPD_task; 8465 break; 8466 case OMPD_cancel: 8467 case OMPD_parallel: 8468 case OMPD_parallel_sections: 8469 case OMPD_parallel_for: 8470 case OMPD_parallel_for_simd: 8471 case OMPD_target: 8472 case OMPD_target_simd: 8473 case OMPD_target_teams: 8474 case OMPD_target_teams_distribute: 8475 case OMPD_target_teams_distribute_simd: 8476 case OMPD_distribute_parallel_for: 8477 case OMPD_distribute_parallel_for_simd: 8478 case OMPD_task: 8479 case OMPD_taskloop: 8480 case OMPD_taskloop_simd: 8481 case OMPD_target_data: 8482 // Do not capture if-clause expressions. 8483 break; 8484 case OMPD_threadprivate: 8485 case OMPD_taskyield: 8486 case OMPD_barrier: 8487 case OMPD_taskwait: 8488 case OMPD_cancellation_point: 8489 case OMPD_flush: 8490 case OMPD_declare_reduction: 8491 case OMPD_declare_mapper: 8492 case OMPD_declare_simd: 8493 case OMPD_declare_target: 8494 case OMPD_end_declare_target: 8495 case OMPD_teams: 8496 case OMPD_simd: 8497 case OMPD_for: 8498 case OMPD_for_simd: 8499 case OMPD_sections: 8500 case OMPD_section: 8501 case OMPD_single: 8502 case OMPD_master: 8503 case OMPD_critical: 8504 case OMPD_taskgroup: 8505 case OMPD_distribute: 8506 case OMPD_ordered: 8507 case OMPD_atomic: 8508 case OMPD_distribute_simd: 8509 case OMPD_teams_distribute: 8510 case OMPD_teams_distribute_simd: 8511 case OMPD_requires: 8512 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 8513 case OMPD_unknown: 8514 llvm_unreachable("Unknown OpenMP directive"); 8515 } 8516 break; 8517 case OMPC_num_threads: 8518 switch (DKind) { 8519 case OMPD_target_parallel: 8520 case OMPD_target_parallel_for: 8521 case OMPD_target_parallel_for_simd: 8522 CaptureRegion = OMPD_target; 8523 break; 8524 case OMPD_teams_distribute_parallel_for: 8525 case OMPD_teams_distribute_parallel_for_simd: 8526 case OMPD_target_teams_distribute_parallel_for: 8527 case OMPD_target_teams_distribute_parallel_for_simd: 8528 CaptureRegion = OMPD_teams; 8529 break; 8530 case OMPD_parallel: 8531 case OMPD_parallel_sections: 8532 case OMPD_parallel_for: 8533 case OMPD_parallel_for_simd: 8534 case OMPD_distribute_parallel_for: 8535 case OMPD_distribute_parallel_for_simd: 8536 // Do not capture num_threads-clause expressions. 8537 break; 8538 case OMPD_target_data: 8539 case OMPD_target_enter_data: 8540 case OMPD_target_exit_data: 8541 case OMPD_target_update: 8542 case OMPD_target: 8543 case OMPD_target_simd: 8544 case OMPD_target_teams: 8545 case OMPD_target_teams_distribute: 8546 case OMPD_target_teams_distribute_simd: 8547 case OMPD_cancel: 8548 case OMPD_task: 8549 case OMPD_taskloop: 8550 case OMPD_taskloop_simd: 8551 case OMPD_threadprivate: 8552 case OMPD_taskyield: 8553 case OMPD_barrier: 8554 case OMPD_taskwait: 8555 case OMPD_cancellation_point: 8556 case OMPD_flush: 8557 case OMPD_declare_reduction: 8558 case OMPD_declare_mapper: 8559 case OMPD_declare_simd: 8560 case OMPD_declare_target: 8561 case OMPD_end_declare_target: 8562 case OMPD_teams: 8563 case OMPD_simd: 8564 case OMPD_for: 8565 case OMPD_for_simd: 8566 case OMPD_sections: 8567 case OMPD_section: 8568 case OMPD_single: 8569 case OMPD_master: 8570 case OMPD_critical: 8571 case OMPD_taskgroup: 8572 case OMPD_distribute: 8573 case OMPD_ordered: 8574 case OMPD_atomic: 8575 case OMPD_distribute_simd: 8576 case OMPD_teams_distribute: 8577 case OMPD_teams_distribute_simd: 8578 case OMPD_requires: 8579 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 8580 case OMPD_unknown: 8581 llvm_unreachable("Unknown OpenMP directive"); 8582 } 8583 break; 8584 case OMPC_num_teams: 8585 switch (DKind) { 8586 case OMPD_target_teams: 8587 case OMPD_target_teams_distribute: 8588 case OMPD_target_teams_distribute_simd: 8589 case OMPD_target_teams_distribute_parallel_for: 8590 case OMPD_target_teams_distribute_parallel_for_simd: 8591 CaptureRegion = OMPD_target; 8592 break; 8593 case OMPD_teams_distribute_parallel_for: 8594 case OMPD_teams_distribute_parallel_for_simd: 8595 case OMPD_teams: 8596 case OMPD_teams_distribute: 8597 case OMPD_teams_distribute_simd: 8598 // Do not capture num_teams-clause expressions. 8599 break; 8600 case OMPD_distribute_parallel_for: 8601 case OMPD_distribute_parallel_for_simd: 8602 case OMPD_task: 8603 case OMPD_taskloop: 8604 case OMPD_taskloop_simd: 8605 case OMPD_target_data: 8606 case OMPD_target_enter_data: 8607 case OMPD_target_exit_data: 8608 case OMPD_target_update: 8609 case OMPD_cancel: 8610 case OMPD_parallel: 8611 case OMPD_parallel_sections: 8612 case OMPD_parallel_for: 8613 case OMPD_parallel_for_simd: 8614 case OMPD_target: 8615 case OMPD_target_simd: 8616 case OMPD_target_parallel: 8617 case OMPD_target_parallel_for: 8618 case OMPD_target_parallel_for_simd: 8619 case OMPD_threadprivate: 8620 case OMPD_taskyield: 8621 case OMPD_barrier: 8622 case OMPD_taskwait: 8623 case OMPD_cancellation_point: 8624 case OMPD_flush: 8625 case OMPD_declare_reduction: 8626 case OMPD_declare_mapper: 8627 case OMPD_declare_simd: 8628 case OMPD_declare_target: 8629 case OMPD_end_declare_target: 8630 case OMPD_simd: 8631 case OMPD_for: 8632 case OMPD_for_simd: 8633 case OMPD_sections: 8634 case OMPD_section: 8635 case OMPD_single: 8636 case OMPD_master: 8637 case OMPD_critical: 8638 case OMPD_taskgroup: 8639 case OMPD_distribute: 8640 case OMPD_ordered: 8641 case OMPD_atomic: 8642 case OMPD_distribute_simd: 8643 case OMPD_requires: 8644 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8645 case OMPD_unknown: 8646 llvm_unreachable("Unknown OpenMP directive"); 8647 } 8648 break; 8649 case OMPC_thread_limit: 8650 switch (DKind) { 8651 case OMPD_target_teams: 8652 case OMPD_target_teams_distribute: 8653 case OMPD_target_teams_distribute_simd: 8654 case OMPD_target_teams_distribute_parallel_for: 8655 case OMPD_target_teams_distribute_parallel_for_simd: 8656 CaptureRegion = OMPD_target; 8657 break; 8658 case OMPD_teams_distribute_parallel_for: 8659 case OMPD_teams_distribute_parallel_for_simd: 8660 case OMPD_teams: 8661 case OMPD_teams_distribute: 8662 case OMPD_teams_distribute_simd: 8663 // Do not capture thread_limit-clause expressions. 8664 break; 8665 case OMPD_distribute_parallel_for: 8666 case OMPD_distribute_parallel_for_simd: 8667 case OMPD_task: 8668 case OMPD_taskloop: 8669 case OMPD_taskloop_simd: 8670 case OMPD_target_data: 8671 case OMPD_target_enter_data: 8672 case OMPD_target_exit_data: 8673 case OMPD_target_update: 8674 case OMPD_cancel: 8675 case OMPD_parallel: 8676 case OMPD_parallel_sections: 8677 case OMPD_parallel_for: 8678 case OMPD_parallel_for_simd: 8679 case OMPD_target: 8680 case OMPD_target_simd: 8681 case OMPD_target_parallel: 8682 case OMPD_target_parallel_for: 8683 case OMPD_target_parallel_for_simd: 8684 case OMPD_threadprivate: 8685 case OMPD_taskyield: 8686 case OMPD_barrier: 8687 case OMPD_taskwait: 8688 case OMPD_cancellation_point: 8689 case OMPD_flush: 8690 case OMPD_declare_reduction: 8691 case OMPD_declare_mapper: 8692 case OMPD_declare_simd: 8693 case OMPD_declare_target: 8694 case OMPD_end_declare_target: 8695 case OMPD_simd: 8696 case OMPD_for: 8697 case OMPD_for_simd: 8698 case OMPD_sections: 8699 case OMPD_section: 8700 case OMPD_single: 8701 case OMPD_master: 8702 case OMPD_critical: 8703 case OMPD_taskgroup: 8704 case OMPD_distribute: 8705 case OMPD_ordered: 8706 case OMPD_atomic: 8707 case OMPD_distribute_simd: 8708 case OMPD_requires: 8709 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 8710 case OMPD_unknown: 8711 llvm_unreachable("Unknown OpenMP directive"); 8712 } 8713 break; 8714 case OMPC_schedule: 8715 switch (DKind) { 8716 case OMPD_parallel_for: 8717 case OMPD_parallel_for_simd: 8718 case OMPD_distribute_parallel_for: 8719 case OMPD_distribute_parallel_for_simd: 8720 case OMPD_teams_distribute_parallel_for: 8721 case OMPD_teams_distribute_parallel_for_simd: 8722 case OMPD_target_parallel_for: 8723 case OMPD_target_parallel_for_simd: 8724 case OMPD_target_teams_distribute_parallel_for: 8725 case OMPD_target_teams_distribute_parallel_for_simd: 8726 CaptureRegion = OMPD_parallel; 8727 break; 8728 case OMPD_for: 8729 case OMPD_for_simd: 8730 // Do not capture schedule-clause expressions. 8731 break; 8732 case OMPD_task: 8733 case OMPD_taskloop: 8734 case OMPD_taskloop_simd: 8735 case OMPD_target_data: 8736 case OMPD_target_enter_data: 8737 case OMPD_target_exit_data: 8738 case OMPD_target_update: 8739 case OMPD_teams: 8740 case OMPD_teams_distribute: 8741 case OMPD_teams_distribute_simd: 8742 case OMPD_target_teams_distribute: 8743 case OMPD_target_teams_distribute_simd: 8744 case OMPD_target: 8745 case OMPD_target_simd: 8746 case OMPD_target_parallel: 8747 case OMPD_cancel: 8748 case OMPD_parallel: 8749 case OMPD_parallel_sections: 8750 case OMPD_threadprivate: 8751 case OMPD_taskyield: 8752 case OMPD_barrier: 8753 case OMPD_taskwait: 8754 case OMPD_cancellation_point: 8755 case OMPD_flush: 8756 case OMPD_declare_reduction: 8757 case OMPD_declare_mapper: 8758 case OMPD_declare_simd: 8759 case OMPD_declare_target: 8760 case OMPD_end_declare_target: 8761 case OMPD_simd: 8762 case OMPD_sections: 8763 case OMPD_section: 8764 case OMPD_single: 8765 case OMPD_master: 8766 case OMPD_critical: 8767 case OMPD_taskgroup: 8768 case OMPD_distribute: 8769 case OMPD_ordered: 8770 case OMPD_atomic: 8771 case OMPD_distribute_simd: 8772 case OMPD_target_teams: 8773 case OMPD_requires: 8774 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8775 case OMPD_unknown: 8776 llvm_unreachable("Unknown OpenMP directive"); 8777 } 8778 break; 8779 case OMPC_dist_schedule: 8780 switch (DKind) { 8781 case OMPD_teams_distribute_parallel_for: 8782 case OMPD_teams_distribute_parallel_for_simd: 8783 case OMPD_teams_distribute: 8784 case OMPD_teams_distribute_simd: 8785 case OMPD_target_teams_distribute_parallel_for: 8786 case OMPD_target_teams_distribute_parallel_for_simd: 8787 case OMPD_target_teams_distribute: 8788 case OMPD_target_teams_distribute_simd: 8789 CaptureRegion = OMPD_teams; 8790 break; 8791 case OMPD_distribute_parallel_for: 8792 case OMPD_distribute_parallel_for_simd: 8793 case OMPD_distribute: 8794 case OMPD_distribute_simd: 8795 // Do not capture thread_limit-clause expressions. 8796 break; 8797 case OMPD_parallel_for: 8798 case OMPD_parallel_for_simd: 8799 case OMPD_target_parallel_for_simd: 8800 case OMPD_target_parallel_for: 8801 case OMPD_task: 8802 case OMPD_taskloop: 8803 case OMPD_taskloop_simd: 8804 case OMPD_target_data: 8805 case OMPD_target_enter_data: 8806 case OMPD_target_exit_data: 8807 case OMPD_target_update: 8808 case OMPD_teams: 8809 case OMPD_target: 8810 case OMPD_target_simd: 8811 case OMPD_target_parallel: 8812 case OMPD_cancel: 8813 case OMPD_parallel: 8814 case OMPD_parallel_sections: 8815 case OMPD_threadprivate: 8816 case OMPD_taskyield: 8817 case OMPD_barrier: 8818 case OMPD_taskwait: 8819 case OMPD_cancellation_point: 8820 case OMPD_flush: 8821 case OMPD_declare_reduction: 8822 case OMPD_declare_mapper: 8823 case OMPD_declare_simd: 8824 case OMPD_declare_target: 8825 case OMPD_end_declare_target: 8826 case OMPD_simd: 8827 case OMPD_for: 8828 case OMPD_for_simd: 8829 case OMPD_sections: 8830 case OMPD_section: 8831 case OMPD_single: 8832 case OMPD_master: 8833 case OMPD_critical: 8834 case OMPD_taskgroup: 8835 case OMPD_ordered: 8836 case OMPD_atomic: 8837 case OMPD_target_teams: 8838 case OMPD_requires: 8839 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8840 case OMPD_unknown: 8841 llvm_unreachable("Unknown OpenMP directive"); 8842 } 8843 break; 8844 case OMPC_device: 8845 switch (DKind) { 8846 case OMPD_target_update: 8847 case OMPD_target_enter_data: 8848 case OMPD_target_exit_data: 8849 case OMPD_target: 8850 case OMPD_target_simd: 8851 case OMPD_target_teams: 8852 case OMPD_target_parallel: 8853 case OMPD_target_teams_distribute: 8854 case OMPD_target_teams_distribute_simd: 8855 case OMPD_target_parallel_for: 8856 case OMPD_target_parallel_for_simd: 8857 case OMPD_target_teams_distribute_parallel_for: 8858 case OMPD_target_teams_distribute_parallel_for_simd: 8859 CaptureRegion = OMPD_task; 8860 break; 8861 case OMPD_target_data: 8862 // Do not capture device-clause expressions. 8863 break; 8864 case OMPD_teams_distribute_parallel_for: 8865 case OMPD_teams_distribute_parallel_for_simd: 8866 case OMPD_teams: 8867 case OMPD_teams_distribute: 8868 case OMPD_teams_distribute_simd: 8869 case OMPD_distribute_parallel_for: 8870 case OMPD_distribute_parallel_for_simd: 8871 case OMPD_task: 8872 case OMPD_taskloop: 8873 case OMPD_taskloop_simd: 8874 case OMPD_cancel: 8875 case OMPD_parallel: 8876 case OMPD_parallel_sections: 8877 case OMPD_parallel_for: 8878 case OMPD_parallel_for_simd: 8879 case OMPD_threadprivate: 8880 case OMPD_taskyield: 8881 case OMPD_barrier: 8882 case OMPD_taskwait: 8883 case OMPD_cancellation_point: 8884 case OMPD_flush: 8885 case OMPD_declare_reduction: 8886 case OMPD_declare_mapper: 8887 case OMPD_declare_simd: 8888 case OMPD_declare_target: 8889 case OMPD_end_declare_target: 8890 case OMPD_simd: 8891 case OMPD_for: 8892 case OMPD_for_simd: 8893 case OMPD_sections: 8894 case OMPD_section: 8895 case OMPD_single: 8896 case OMPD_master: 8897 case OMPD_critical: 8898 case OMPD_taskgroup: 8899 case OMPD_distribute: 8900 case OMPD_ordered: 8901 case OMPD_atomic: 8902 case OMPD_distribute_simd: 8903 case OMPD_requires: 8904 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8905 case OMPD_unknown: 8906 llvm_unreachable("Unknown OpenMP directive"); 8907 } 8908 break; 8909 case OMPC_firstprivate: 8910 case OMPC_lastprivate: 8911 case OMPC_reduction: 8912 case OMPC_task_reduction: 8913 case OMPC_in_reduction: 8914 case OMPC_linear: 8915 case OMPC_default: 8916 case OMPC_proc_bind: 8917 case OMPC_final: 8918 case OMPC_safelen: 8919 case OMPC_simdlen: 8920 case OMPC_collapse: 8921 case OMPC_private: 8922 case OMPC_shared: 8923 case OMPC_aligned: 8924 case OMPC_copyin: 8925 case OMPC_copyprivate: 8926 case OMPC_ordered: 8927 case OMPC_nowait: 8928 case OMPC_untied: 8929 case OMPC_mergeable: 8930 case OMPC_threadprivate: 8931 case OMPC_flush: 8932 case OMPC_read: 8933 case OMPC_write: 8934 case OMPC_update: 8935 case OMPC_capture: 8936 case OMPC_seq_cst: 8937 case OMPC_depend: 8938 case OMPC_threads: 8939 case OMPC_simd: 8940 case OMPC_map: 8941 case OMPC_priority: 8942 case OMPC_grainsize: 8943 case OMPC_nogroup: 8944 case OMPC_num_tasks: 8945 case OMPC_hint: 8946 case OMPC_defaultmap: 8947 case OMPC_unknown: 8948 case OMPC_uniform: 8949 case OMPC_to: 8950 case OMPC_from: 8951 case OMPC_use_device_ptr: 8952 case OMPC_is_device_ptr: 8953 case OMPC_unified_address: 8954 case OMPC_unified_shared_memory: 8955 case OMPC_reverse_offload: 8956 case OMPC_dynamic_allocators: 8957 case OMPC_atomic_default_mem_order: 8958 llvm_unreachable("Unexpected OpenMP clause."); 8959 } 8960 return CaptureRegion; 8961 } 8962 8963 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 8964 Expr *Condition, SourceLocation StartLoc, 8965 SourceLocation LParenLoc, 8966 SourceLocation NameModifierLoc, 8967 SourceLocation ColonLoc, 8968 SourceLocation EndLoc) { 8969 Expr *ValExpr = Condition; 8970 Stmt *HelperValStmt = nullptr; 8971 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8972 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8973 !Condition->isInstantiationDependent() && 8974 !Condition->containsUnexpandedParameterPack()) { 8975 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8976 if (Val.isInvalid()) 8977 return nullptr; 8978 8979 ValExpr = Val.get(); 8980 8981 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8982 CaptureRegion = 8983 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 8984 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8985 ValExpr = MakeFullExpr(ValExpr).get(); 8986 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8987 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8988 HelperValStmt = buildPreInits(Context, Captures); 8989 } 8990 } 8991 8992 return new (Context) 8993 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 8994 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 8995 } 8996 8997 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 8998 SourceLocation StartLoc, 8999 SourceLocation LParenLoc, 9000 SourceLocation EndLoc) { 9001 Expr *ValExpr = Condition; 9002 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 9003 !Condition->isInstantiationDependent() && 9004 !Condition->containsUnexpandedParameterPack()) { 9005 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 9006 if (Val.isInvalid()) 9007 return nullptr; 9008 9009 ValExpr = MakeFullExpr(Val.get()).get(); 9010 } 9011 9012 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 9013 } 9014 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 9015 Expr *Op) { 9016 if (!Op) 9017 return ExprError(); 9018 9019 class IntConvertDiagnoser : public ICEConvertDiagnoser { 9020 public: 9021 IntConvertDiagnoser() 9022 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 9023 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 9024 QualType T) override { 9025 return S.Diag(Loc, diag::err_omp_not_integral) << T; 9026 } 9027 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 9028 QualType T) override { 9029 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 9030 } 9031 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 9032 QualType T, 9033 QualType ConvTy) override { 9034 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 9035 } 9036 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 9037 QualType ConvTy) override { 9038 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9039 << ConvTy->isEnumeralType() << ConvTy; 9040 } 9041 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 9042 QualType T) override { 9043 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 9044 } 9045 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 9046 QualType ConvTy) override { 9047 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9048 << ConvTy->isEnumeralType() << ConvTy; 9049 } 9050 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 9051 QualType) override { 9052 llvm_unreachable("conversion functions are permitted"); 9053 } 9054 } ConvertDiagnoser; 9055 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 9056 } 9057 9058 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 9059 OpenMPClauseKind CKind, 9060 bool StrictlyPositive) { 9061 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 9062 !ValExpr->isInstantiationDependent()) { 9063 SourceLocation Loc = ValExpr->getExprLoc(); 9064 ExprResult Value = 9065 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 9066 if (Value.isInvalid()) 9067 return false; 9068 9069 ValExpr = Value.get(); 9070 // The expression must evaluate to a non-negative integer value. 9071 llvm::APSInt Result; 9072 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 9073 Result.isSigned() && 9074 !((!StrictlyPositive && Result.isNonNegative()) || 9075 (StrictlyPositive && Result.isStrictlyPositive()))) { 9076 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 9077 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9078 << ValExpr->getSourceRange(); 9079 return false; 9080 } 9081 } 9082 return true; 9083 } 9084 9085 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 9086 SourceLocation StartLoc, 9087 SourceLocation LParenLoc, 9088 SourceLocation EndLoc) { 9089 Expr *ValExpr = NumThreads; 9090 Stmt *HelperValStmt = nullptr; 9091 9092 // OpenMP [2.5, Restrictions] 9093 // The num_threads expression must evaluate to a positive integer value. 9094 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 9095 /*StrictlyPositive=*/true)) 9096 return nullptr; 9097 9098 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 9099 OpenMPDirectiveKind CaptureRegion = 9100 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 9101 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 9102 ValExpr = MakeFullExpr(ValExpr).get(); 9103 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9104 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9105 HelperValStmt = buildPreInits(Context, Captures); 9106 } 9107 9108 return new (Context) OMPNumThreadsClause( 9109 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 9110 } 9111 9112 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 9113 OpenMPClauseKind CKind, 9114 bool StrictlyPositive) { 9115 if (!E) 9116 return ExprError(); 9117 if (E->isValueDependent() || E->isTypeDependent() || 9118 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 9119 return E; 9120 llvm::APSInt Result; 9121 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 9122 if (ICE.isInvalid()) 9123 return ExprError(); 9124 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 9125 (!StrictlyPositive && !Result.isNonNegative())) { 9126 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 9127 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9128 << E->getSourceRange(); 9129 return ExprError(); 9130 } 9131 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 9132 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 9133 << E->getSourceRange(); 9134 return ExprError(); 9135 } 9136 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 9137 DSAStack->setAssociatedLoops(Result.getExtValue()); 9138 else if (CKind == OMPC_ordered) 9139 DSAStack->setAssociatedLoops(Result.getExtValue()); 9140 return ICE; 9141 } 9142 9143 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 9144 SourceLocation LParenLoc, 9145 SourceLocation EndLoc) { 9146 // OpenMP [2.8.1, simd construct, Description] 9147 // The parameter of the safelen clause must be a constant 9148 // positive integer expression. 9149 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 9150 if (Safelen.isInvalid()) 9151 return nullptr; 9152 return new (Context) 9153 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 9154 } 9155 9156 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 9157 SourceLocation LParenLoc, 9158 SourceLocation EndLoc) { 9159 // OpenMP [2.8.1, simd construct, Description] 9160 // The parameter of the simdlen clause must be a constant 9161 // positive integer expression. 9162 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 9163 if (Simdlen.isInvalid()) 9164 return nullptr; 9165 return new (Context) 9166 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 9167 } 9168 9169 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 9170 SourceLocation StartLoc, 9171 SourceLocation LParenLoc, 9172 SourceLocation EndLoc) { 9173 // OpenMP [2.7.1, loop construct, Description] 9174 // OpenMP [2.8.1, simd construct, Description] 9175 // OpenMP [2.9.6, distribute construct, Description] 9176 // The parameter of the collapse clause must be a constant 9177 // positive integer expression. 9178 ExprResult NumForLoopsResult = 9179 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 9180 if (NumForLoopsResult.isInvalid()) 9181 return nullptr; 9182 return new (Context) 9183 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 9184 } 9185 9186 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 9187 SourceLocation EndLoc, 9188 SourceLocation LParenLoc, 9189 Expr *NumForLoops) { 9190 // OpenMP [2.7.1, loop construct, Description] 9191 // OpenMP [2.8.1, simd construct, Description] 9192 // OpenMP [2.9.6, distribute construct, Description] 9193 // The parameter of the ordered clause must be a constant 9194 // positive integer expression if any. 9195 if (NumForLoops && LParenLoc.isValid()) { 9196 ExprResult NumForLoopsResult = 9197 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 9198 if (NumForLoopsResult.isInvalid()) 9199 return nullptr; 9200 NumForLoops = NumForLoopsResult.get(); 9201 } else { 9202 NumForLoops = nullptr; 9203 } 9204 auto *Clause = OMPOrderedClause::Create( 9205 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 9206 StartLoc, LParenLoc, EndLoc); 9207 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 9208 return Clause; 9209 } 9210 9211 OMPClause *Sema::ActOnOpenMPSimpleClause( 9212 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 9213 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 9214 OMPClause *Res = nullptr; 9215 switch (Kind) { 9216 case OMPC_default: 9217 Res = 9218 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 9219 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 9220 break; 9221 case OMPC_proc_bind: 9222 Res = ActOnOpenMPProcBindClause( 9223 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 9224 LParenLoc, EndLoc); 9225 break; 9226 case OMPC_atomic_default_mem_order: 9227 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 9228 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 9229 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 9230 break; 9231 case OMPC_if: 9232 case OMPC_final: 9233 case OMPC_num_threads: 9234 case OMPC_safelen: 9235 case OMPC_simdlen: 9236 case OMPC_collapse: 9237 case OMPC_schedule: 9238 case OMPC_private: 9239 case OMPC_firstprivate: 9240 case OMPC_lastprivate: 9241 case OMPC_shared: 9242 case OMPC_reduction: 9243 case OMPC_task_reduction: 9244 case OMPC_in_reduction: 9245 case OMPC_linear: 9246 case OMPC_aligned: 9247 case OMPC_copyin: 9248 case OMPC_copyprivate: 9249 case OMPC_ordered: 9250 case OMPC_nowait: 9251 case OMPC_untied: 9252 case OMPC_mergeable: 9253 case OMPC_threadprivate: 9254 case OMPC_flush: 9255 case OMPC_read: 9256 case OMPC_write: 9257 case OMPC_update: 9258 case OMPC_capture: 9259 case OMPC_seq_cst: 9260 case OMPC_depend: 9261 case OMPC_device: 9262 case OMPC_threads: 9263 case OMPC_simd: 9264 case OMPC_map: 9265 case OMPC_num_teams: 9266 case OMPC_thread_limit: 9267 case OMPC_priority: 9268 case OMPC_grainsize: 9269 case OMPC_nogroup: 9270 case OMPC_num_tasks: 9271 case OMPC_hint: 9272 case OMPC_dist_schedule: 9273 case OMPC_defaultmap: 9274 case OMPC_unknown: 9275 case OMPC_uniform: 9276 case OMPC_to: 9277 case OMPC_from: 9278 case OMPC_use_device_ptr: 9279 case OMPC_is_device_ptr: 9280 case OMPC_unified_address: 9281 case OMPC_unified_shared_memory: 9282 case OMPC_reverse_offload: 9283 case OMPC_dynamic_allocators: 9284 llvm_unreachable("Clause is not allowed."); 9285 } 9286 return Res; 9287 } 9288 9289 static std::string 9290 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 9291 ArrayRef<unsigned> Exclude = llvm::None) { 9292 SmallString<256> Buffer; 9293 llvm::raw_svector_ostream Out(Buffer); 9294 unsigned Bound = Last >= 2 ? Last - 2 : 0; 9295 unsigned Skipped = Exclude.size(); 9296 auto S = Exclude.begin(), E = Exclude.end(); 9297 for (unsigned I = First; I < Last; ++I) { 9298 if (std::find(S, E, I) != E) { 9299 --Skipped; 9300 continue; 9301 } 9302 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 9303 if (I == Bound - Skipped) 9304 Out << " or "; 9305 else if (I != Bound + 1 - Skipped) 9306 Out << ", "; 9307 } 9308 return Out.str(); 9309 } 9310 9311 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 9312 SourceLocation KindKwLoc, 9313 SourceLocation StartLoc, 9314 SourceLocation LParenLoc, 9315 SourceLocation EndLoc) { 9316 if (Kind == OMPC_DEFAULT_unknown) { 9317 static_assert(OMPC_DEFAULT_unknown > 0, 9318 "OMPC_DEFAULT_unknown not greater than 0"); 9319 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9320 << getListOfPossibleValues(OMPC_default, /*First=*/0, 9321 /*Last=*/OMPC_DEFAULT_unknown) 9322 << getOpenMPClauseName(OMPC_default); 9323 return nullptr; 9324 } 9325 switch (Kind) { 9326 case OMPC_DEFAULT_none: 9327 DSAStack->setDefaultDSANone(KindKwLoc); 9328 break; 9329 case OMPC_DEFAULT_shared: 9330 DSAStack->setDefaultDSAShared(KindKwLoc); 9331 break; 9332 case OMPC_DEFAULT_unknown: 9333 llvm_unreachable("Clause kind is not allowed."); 9334 break; 9335 } 9336 return new (Context) 9337 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9338 } 9339 9340 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 9341 SourceLocation KindKwLoc, 9342 SourceLocation StartLoc, 9343 SourceLocation LParenLoc, 9344 SourceLocation EndLoc) { 9345 if (Kind == OMPC_PROC_BIND_unknown) { 9346 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9347 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 9348 /*Last=*/OMPC_PROC_BIND_unknown) 9349 << getOpenMPClauseName(OMPC_proc_bind); 9350 return nullptr; 9351 } 9352 return new (Context) 9353 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9354 } 9355 9356 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 9357 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 9358 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 9359 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 9360 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9361 << getListOfPossibleValues( 9362 OMPC_atomic_default_mem_order, /*First=*/0, 9363 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 9364 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 9365 return nullptr; 9366 } 9367 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 9368 LParenLoc, EndLoc); 9369 } 9370 9371 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 9372 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 9373 SourceLocation StartLoc, SourceLocation LParenLoc, 9374 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 9375 SourceLocation EndLoc) { 9376 OMPClause *Res = nullptr; 9377 switch (Kind) { 9378 case OMPC_schedule: 9379 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 9380 assert(Argument.size() == NumberOfElements && 9381 ArgumentLoc.size() == NumberOfElements); 9382 Res = ActOnOpenMPScheduleClause( 9383 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 9384 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 9385 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 9386 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 9387 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 9388 break; 9389 case OMPC_if: 9390 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 9391 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 9392 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 9393 DelimLoc, EndLoc); 9394 break; 9395 case OMPC_dist_schedule: 9396 Res = ActOnOpenMPDistScheduleClause( 9397 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 9398 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 9399 break; 9400 case OMPC_defaultmap: 9401 enum { Modifier, DefaultmapKind }; 9402 Res = ActOnOpenMPDefaultmapClause( 9403 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 9404 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 9405 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 9406 EndLoc); 9407 break; 9408 case OMPC_final: 9409 case OMPC_num_threads: 9410 case OMPC_safelen: 9411 case OMPC_simdlen: 9412 case OMPC_collapse: 9413 case OMPC_default: 9414 case OMPC_proc_bind: 9415 case OMPC_private: 9416 case OMPC_firstprivate: 9417 case OMPC_lastprivate: 9418 case OMPC_shared: 9419 case OMPC_reduction: 9420 case OMPC_task_reduction: 9421 case OMPC_in_reduction: 9422 case OMPC_linear: 9423 case OMPC_aligned: 9424 case OMPC_copyin: 9425 case OMPC_copyprivate: 9426 case OMPC_ordered: 9427 case OMPC_nowait: 9428 case OMPC_untied: 9429 case OMPC_mergeable: 9430 case OMPC_threadprivate: 9431 case OMPC_flush: 9432 case OMPC_read: 9433 case OMPC_write: 9434 case OMPC_update: 9435 case OMPC_capture: 9436 case OMPC_seq_cst: 9437 case OMPC_depend: 9438 case OMPC_device: 9439 case OMPC_threads: 9440 case OMPC_simd: 9441 case OMPC_map: 9442 case OMPC_num_teams: 9443 case OMPC_thread_limit: 9444 case OMPC_priority: 9445 case OMPC_grainsize: 9446 case OMPC_nogroup: 9447 case OMPC_num_tasks: 9448 case OMPC_hint: 9449 case OMPC_unknown: 9450 case OMPC_uniform: 9451 case OMPC_to: 9452 case OMPC_from: 9453 case OMPC_use_device_ptr: 9454 case OMPC_is_device_ptr: 9455 case OMPC_unified_address: 9456 case OMPC_unified_shared_memory: 9457 case OMPC_reverse_offload: 9458 case OMPC_dynamic_allocators: 9459 case OMPC_atomic_default_mem_order: 9460 llvm_unreachable("Clause is not allowed."); 9461 } 9462 return Res; 9463 } 9464 9465 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 9466 OpenMPScheduleClauseModifier M2, 9467 SourceLocation M1Loc, SourceLocation M2Loc) { 9468 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 9469 SmallVector<unsigned, 2> Excluded; 9470 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 9471 Excluded.push_back(M2); 9472 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 9473 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 9474 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 9475 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 9476 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 9477 << getListOfPossibleValues(OMPC_schedule, 9478 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 9479 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 9480 Excluded) 9481 << getOpenMPClauseName(OMPC_schedule); 9482 return true; 9483 } 9484 return false; 9485 } 9486 9487 OMPClause *Sema::ActOnOpenMPScheduleClause( 9488 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 9489 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 9490 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 9491 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 9492 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 9493 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 9494 return nullptr; 9495 // OpenMP, 2.7.1, Loop Construct, Restrictions 9496 // Either the monotonic modifier or the nonmonotonic modifier can be specified 9497 // but not both. 9498 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 9499 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 9500 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 9501 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 9502 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 9503 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 9504 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 9505 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 9506 return nullptr; 9507 } 9508 if (Kind == OMPC_SCHEDULE_unknown) { 9509 std::string Values; 9510 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 9511 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 9512 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 9513 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 9514 Exclude); 9515 } else { 9516 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 9517 /*Last=*/OMPC_SCHEDULE_unknown); 9518 } 9519 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 9520 << Values << getOpenMPClauseName(OMPC_schedule); 9521 return nullptr; 9522 } 9523 // OpenMP, 2.7.1, Loop Construct, Restrictions 9524 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 9525 // schedule(guided). 9526 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 9527 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 9528 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 9529 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 9530 diag::err_omp_schedule_nonmonotonic_static); 9531 return nullptr; 9532 } 9533 Expr *ValExpr = ChunkSize; 9534 Stmt *HelperValStmt = nullptr; 9535 if (ChunkSize) { 9536 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 9537 !ChunkSize->isInstantiationDependent() && 9538 !ChunkSize->containsUnexpandedParameterPack()) { 9539 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 9540 ExprResult Val = 9541 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 9542 if (Val.isInvalid()) 9543 return nullptr; 9544 9545 ValExpr = Val.get(); 9546 9547 // OpenMP [2.7.1, Restrictions] 9548 // chunk_size must be a loop invariant integer expression with a positive 9549 // value. 9550 llvm::APSInt Result; 9551 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 9552 if (Result.isSigned() && !Result.isStrictlyPositive()) { 9553 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 9554 << "schedule" << 1 << ChunkSize->getSourceRange(); 9555 return nullptr; 9556 } 9557 } else if (getOpenMPCaptureRegionForClause( 9558 DSAStack->getCurrentDirective(), OMPC_schedule) != 9559 OMPD_unknown && 9560 !CurContext->isDependentContext()) { 9561 ValExpr = MakeFullExpr(ValExpr).get(); 9562 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9563 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9564 HelperValStmt = buildPreInits(Context, Captures); 9565 } 9566 } 9567 } 9568 9569 return new (Context) 9570 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 9571 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 9572 } 9573 9574 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 9575 SourceLocation StartLoc, 9576 SourceLocation EndLoc) { 9577 OMPClause *Res = nullptr; 9578 switch (Kind) { 9579 case OMPC_ordered: 9580 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 9581 break; 9582 case OMPC_nowait: 9583 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 9584 break; 9585 case OMPC_untied: 9586 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 9587 break; 9588 case OMPC_mergeable: 9589 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 9590 break; 9591 case OMPC_read: 9592 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 9593 break; 9594 case OMPC_write: 9595 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 9596 break; 9597 case OMPC_update: 9598 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 9599 break; 9600 case OMPC_capture: 9601 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 9602 break; 9603 case OMPC_seq_cst: 9604 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 9605 break; 9606 case OMPC_threads: 9607 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 9608 break; 9609 case OMPC_simd: 9610 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 9611 break; 9612 case OMPC_nogroup: 9613 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 9614 break; 9615 case OMPC_unified_address: 9616 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 9617 break; 9618 case OMPC_unified_shared_memory: 9619 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 9620 break; 9621 case OMPC_reverse_offload: 9622 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 9623 break; 9624 case OMPC_dynamic_allocators: 9625 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 9626 break; 9627 case OMPC_if: 9628 case OMPC_final: 9629 case OMPC_num_threads: 9630 case OMPC_safelen: 9631 case OMPC_simdlen: 9632 case OMPC_collapse: 9633 case OMPC_schedule: 9634 case OMPC_private: 9635 case OMPC_firstprivate: 9636 case OMPC_lastprivate: 9637 case OMPC_shared: 9638 case OMPC_reduction: 9639 case OMPC_task_reduction: 9640 case OMPC_in_reduction: 9641 case OMPC_linear: 9642 case OMPC_aligned: 9643 case OMPC_copyin: 9644 case OMPC_copyprivate: 9645 case OMPC_default: 9646 case OMPC_proc_bind: 9647 case OMPC_threadprivate: 9648 case OMPC_flush: 9649 case OMPC_depend: 9650 case OMPC_device: 9651 case OMPC_map: 9652 case OMPC_num_teams: 9653 case OMPC_thread_limit: 9654 case OMPC_priority: 9655 case OMPC_grainsize: 9656 case OMPC_num_tasks: 9657 case OMPC_hint: 9658 case OMPC_dist_schedule: 9659 case OMPC_defaultmap: 9660 case OMPC_unknown: 9661 case OMPC_uniform: 9662 case OMPC_to: 9663 case OMPC_from: 9664 case OMPC_use_device_ptr: 9665 case OMPC_is_device_ptr: 9666 case OMPC_atomic_default_mem_order: 9667 llvm_unreachable("Clause is not allowed."); 9668 } 9669 return Res; 9670 } 9671 9672 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 9673 SourceLocation EndLoc) { 9674 DSAStack->setNowaitRegion(); 9675 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 9676 } 9677 9678 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 9679 SourceLocation EndLoc) { 9680 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 9681 } 9682 9683 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 9684 SourceLocation EndLoc) { 9685 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 9686 } 9687 9688 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 9689 SourceLocation EndLoc) { 9690 return new (Context) OMPReadClause(StartLoc, EndLoc); 9691 } 9692 9693 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 9694 SourceLocation EndLoc) { 9695 return new (Context) OMPWriteClause(StartLoc, EndLoc); 9696 } 9697 9698 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 9699 SourceLocation EndLoc) { 9700 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 9701 } 9702 9703 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 9704 SourceLocation EndLoc) { 9705 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 9706 } 9707 9708 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 9709 SourceLocation EndLoc) { 9710 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 9711 } 9712 9713 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 9714 SourceLocation EndLoc) { 9715 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 9716 } 9717 9718 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 9719 SourceLocation EndLoc) { 9720 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 9721 } 9722 9723 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 9724 SourceLocation EndLoc) { 9725 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 9726 } 9727 9728 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 9729 SourceLocation EndLoc) { 9730 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 9731 } 9732 9733 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 9734 SourceLocation EndLoc) { 9735 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 9736 } 9737 9738 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 9739 SourceLocation EndLoc) { 9740 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 9741 } 9742 9743 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 9744 SourceLocation EndLoc) { 9745 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 9746 } 9747 9748 OMPClause *Sema::ActOnOpenMPVarListClause( 9749 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 9750 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 9751 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 9752 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind, 9753 OpenMPLinearClauseKind LinKind, 9754 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 9755 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType, 9756 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) { 9757 SourceLocation StartLoc = Locs.StartLoc; 9758 SourceLocation LParenLoc = Locs.LParenLoc; 9759 SourceLocation EndLoc = Locs.EndLoc; 9760 OMPClause *Res = nullptr; 9761 switch (Kind) { 9762 case OMPC_private: 9763 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9764 break; 9765 case OMPC_firstprivate: 9766 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9767 break; 9768 case OMPC_lastprivate: 9769 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9770 break; 9771 case OMPC_shared: 9772 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 9773 break; 9774 case OMPC_reduction: 9775 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9776 EndLoc, ReductionOrMapperIdScopeSpec, 9777 ReductionOrMapperId); 9778 break; 9779 case OMPC_task_reduction: 9780 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9781 EndLoc, ReductionOrMapperIdScopeSpec, 9782 ReductionOrMapperId); 9783 break; 9784 case OMPC_in_reduction: 9785 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9786 EndLoc, ReductionOrMapperIdScopeSpec, 9787 ReductionOrMapperId); 9788 break; 9789 case OMPC_linear: 9790 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 9791 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 9792 break; 9793 case OMPC_aligned: 9794 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 9795 ColonLoc, EndLoc); 9796 break; 9797 case OMPC_copyin: 9798 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 9799 break; 9800 case OMPC_copyprivate: 9801 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9802 break; 9803 case OMPC_flush: 9804 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 9805 break; 9806 case OMPC_depend: 9807 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 9808 StartLoc, LParenLoc, EndLoc); 9809 break; 9810 case OMPC_map: 9811 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, 9812 ReductionOrMapperIdScopeSpec, 9813 ReductionOrMapperId, MapType, IsMapTypeImplicit, 9814 DepLinMapLoc, ColonLoc, VarList, Locs); 9815 break; 9816 case OMPC_to: 9817 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec, 9818 ReductionOrMapperId, Locs); 9819 break; 9820 case OMPC_from: 9821 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec, 9822 ReductionOrMapperId, Locs); 9823 break; 9824 case OMPC_use_device_ptr: 9825 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 9826 break; 9827 case OMPC_is_device_ptr: 9828 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 9829 break; 9830 case OMPC_if: 9831 case OMPC_final: 9832 case OMPC_num_threads: 9833 case OMPC_safelen: 9834 case OMPC_simdlen: 9835 case OMPC_collapse: 9836 case OMPC_default: 9837 case OMPC_proc_bind: 9838 case OMPC_schedule: 9839 case OMPC_ordered: 9840 case OMPC_nowait: 9841 case OMPC_untied: 9842 case OMPC_mergeable: 9843 case OMPC_threadprivate: 9844 case OMPC_read: 9845 case OMPC_write: 9846 case OMPC_update: 9847 case OMPC_capture: 9848 case OMPC_seq_cst: 9849 case OMPC_device: 9850 case OMPC_threads: 9851 case OMPC_simd: 9852 case OMPC_num_teams: 9853 case OMPC_thread_limit: 9854 case OMPC_priority: 9855 case OMPC_grainsize: 9856 case OMPC_nogroup: 9857 case OMPC_num_tasks: 9858 case OMPC_hint: 9859 case OMPC_dist_schedule: 9860 case OMPC_defaultmap: 9861 case OMPC_unknown: 9862 case OMPC_uniform: 9863 case OMPC_unified_address: 9864 case OMPC_unified_shared_memory: 9865 case OMPC_reverse_offload: 9866 case OMPC_dynamic_allocators: 9867 case OMPC_atomic_default_mem_order: 9868 llvm_unreachable("Clause is not allowed."); 9869 } 9870 return Res; 9871 } 9872 9873 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 9874 ExprObjectKind OK, SourceLocation Loc) { 9875 ExprResult Res = BuildDeclRefExpr( 9876 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 9877 if (!Res.isUsable()) 9878 return ExprError(); 9879 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 9880 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 9881 if (!Res.isUsable()) 9882 return ExprError(); 9883 } 9884 if (VK != VK_LValue && Res.get()->isGLValue()) { 9885 Res = DefaultLvalueConversion(Res.get()); 9886 if (!Res.isUsable()) 9887 return ExprError(); 9888 } 9889 return Res; 9890 } 9891 9892 static std::pair<ValueDecl *, bool> 9893 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 9894 SourceRange &ERange, bool AllowArraySection = false) { 9895 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 9896 RefExpr->containsUnexpandedParameterPack()) 9897 return std::make_pair(nullptr, true); 9898 9899 // OpenMP [3.1, C/C++] 9900 // A list item is a variable name. 9901 // OpenMP [2.9.3.3, Restrictions, p.1] 9902 // A variable that is part of another variable (as an array or 9903 // structure element) cannot appear in a private clause. 9904 RefExpr = RefExpr->IgnoreParens(); 9905 enum { 9906 NoArrayExpr = -1, 9907 ArraySubscript = 0, 9908 OMPArraySection = 1 9909 } IsArrayExpr = NoArrayExpr; 9910 if (AllowArraySection) { 9911 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 9912 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 9913 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9914 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9915 RefExpr = Base; 9916 IsArrayExpr = ArraySubscript; 9917 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 9918 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 9919 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 9920 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 9921 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9922 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9923 RefExpr = Base; 9924 IsArrayExpr = OMPArraySection; 9925 } 9926 } 9927 ELoc = RefExpr->getExprLoc(); 9928 ERange = RefExpr->getSourceRange(); 9929 RefExpr = RefExpr->IgnoreParenImpCasts(); 9930 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 9931 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 9932 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 9933 (S.getCurrentThisType().isNull() || !ME || 9934 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 9935 !isa<FieldDecl>(ME->getMemberDecl()))) { 9936 if (IsArrayExpr != NoArrayExpr) { 9937 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 9938 << ERange; 9939 } else { 9940 S.Diag(ELoc, 9941 AllowArraySection 9942 ? diag::err_omp_expected_var_name_member_expr_or_array_item 9943 : diag::err_omp_expected_var_name_member_expr) 9944 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 9945 } 9946 return std::make_pair(nullptr, false); 9947 } 9948 return std::make_pair( 9949 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 9950 } 9951 9952 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 9953 SourceLocation StartLoc, 9954 SourceLocation LParenLoc, 9955 SourceLocation EndLoc) { 9956 SmallVector<Expr *, 8> Vars; 9957 SmallVector<Expr *, 8> PrivateCopies; 9958 for (Expr *RefExpr : VarList) { 9959 assert(RefExpr && "NULL expr in OpenMP private clause."); 9960 SourceLocation ELoc; 9961 SourceRange ERange; 9962 Expr *SimpleRefExpr = RefExpr; 9963 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9964 if (Res.second) { 9965 // It will be analyzed later. 9966 Vars.push_back(RefExpr); 9967 PrivateCopies.push_back(nullptr); 9968 } 9969 ValueDecl *D = Res.first; 9970 if (!D) 9971 continue; 9972 9973 QualType Type = D->getType(); 9974 auto *VD = dyn_cast<VarDecl>(D); 9975 9976 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9977 // A variable that appears in a private clause must not have an incomplete 9978 // type or a reference type. 9979 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 9980 continue; 9981 Type = Type.getNonReferenceType(); 9982 9983 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 9984 // A variable that is privatized must not have a const-qualified type 9985 // unless it is of class type with a mutable member. This restriction does 9986 // not apply to the firstprivate clause. 9987 // 9988 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 9989 // A variable that appears in a private clause must not have a 9990 // const-qualified type unless it is of class type with a mutable member. 9991 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 9992 continue; 9993 9994 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9995 // in a Construct] 9996 // Variables with the predetermined data-sharing attributes may not be 9997 // listed in data-sharing attributes clauses, except for the cases 9998 // listed below. For these exceptions only, listing a predetermined 9999 // variable in a data-sharing attribute clause is allowed and overrides 10000 // the variable's predetermined data-sharing attributes. 10001 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10002 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 10003 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10004 << getOpenMPClauseName(OMPC_private); 10005 reportOriginalDsa(*this, DSAStack, D, DVar); 10006 continue; 10007 } 10008 10009 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10010 // Variably modified types are not supported for tasks. 10011 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10012 isOpenMPTaskingDirective(CurrDir)) { 10013 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10014 << getOpenMPClauseName(OMPC_private) << Type 10015 << getOpenMPDirectiveName(CurrDir); 10016 bool IsDecl = 10017 !VD || 10018 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10019 Diag(D->getLocation(), 10020 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10021 << D; 10022 continue; 10023 } 10024 10025 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10026 // A list item cannot appear in both a map clause and a data-sharing 10027 // attribute clause on the same construct 10028 if (isOpenMPTargetExecutionDirective(CurrDir)) { 10029 OpenMPClauseKind ConflictKind; 10030 if (DSAStack->checkMappableExprComponentListsForDecl( 10031 VD, /*CurrentRegionOnly=*/true, 10032 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 10033 OpenMPClauseKind WhereFoundClauseKind) -> bool { 10034 ConflictKind = WhereFoundClauseKind; 10035 return true; 10036 })) { 10037 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10038 << getOpenMPClauseName(OMPC_private) 10039 << getOpenMPClauseName(ConflictKind) 10040 << getOpenMPDirectiveName(CurrDir); 10041 reportOriginalDsa(*this, DSAStack, D, DVar); 10042 continue; 10043 } 10044 } 10045 10046 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 10047 // A variable of class type (or array thereof) that appears in a private 10048 // clause requires an accessible, unambiguous default constructor for the 10049 // class type. 10050 // Generate helper private variable and initialize it with the default 10051 // value. The address of the original variable is replaced by the address of 10052 // the new private variable in CodeGen. This new variable is not added to 10053 // IdResolver, so the code in the OpenMP region uses original variable for 10054 // proper diagnostics. 10055 Type = Type.getUnqualifiedType(); 10056 VarDecl *VDPrivate = 10057 buildVarDecl(*this, ELoc, Type, D->getName(), 10058 D->hasAttrs() ? &D->getAttrs() : nullptr, 10059 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10060 ActOnUninitializedDecl(VDPrivate); 10061 if (VDPrivate->isInvalidDecl()) 10062 continue; 10063 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10064 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 10065 10066 DeclRefExpr *Ref = nullptr; 10067 if (!VD && !CurContext->isDependentContext()) 10068 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10069 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 10070 Vars.push_back((VD || CurContext->isDependentContext()) 10071 ? RefExpr->IgnoreParens() 10072 : Ref); 10073 PrivateCopies.push_back(VDPrivateRefExpr); 10074 } 10075 10076 if (Vars.empty()) 10077 return nullptr; 10078 10079 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 10080 PrivateCopies); 10081 } 10082 10083 namespace { 10084 class DiagsUninitializedSeveretyRAII { 10085 private: 10086 DiagnosticsEngine &Diags; 10087 SourceLocation SavedLoc; 10088 bool IsIgnored = false; 10089 10090 public: 10091 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 10092 bool IsIgnored) 10093 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 10094 if (!IsIgnored) { 10095 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 10096 /*Map*/ diag::Severity::Ignored, Loc); 10097 } 10098 } 10099 ~DiagsUninitializedSeveretyRAII() { 10100 if (!IsIgnored) 10101 Diags.popMappings(SavedLoc); 10102 } 10103 }; 10104 } 10105 10106 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 10107 SourceLocation StartLoc, 10108 SourceLocation LParenLoc, 10109 SourceLocation EndLoc) { 10110 SmallVector<Expr *, 8> Vars; 10111 SmallVector<Expr *, 8> PrivateCopies; 10112 SmallVector<Expr *, 8> Inits; 10113 SmallVector<Decl *, 4> ExprCaptures; 10114 bool IsImplicitClause = 10115 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 10116 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 10117 10118 for (Expr *RefExpr : VarList) { 10119 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 10120 SourceLocation ELoc; 10121 SourceRange ERange; 10122 Expr *SimpleRefExpr = RefExpr; 10123 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10124 if (Res.second) { 10125 // It will be analyzed later. 10126 Vars.push_back(RefExpr); 10127 PrivateCopies.push_back(nullptr); 10128 Inits.push_back(nullptr); 10129 } 10130 ValueDecl *D = Res.first; 10131 if (!D) 10132 continue; 10133 10134 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 10135 QualType Type = D->getType(); 10136 auto *VD = dyn_cast<VarDecl>(D); 10137 10138 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10139 // A variable that appears in a private clause must not have an incomplete 10140 // type or a reference type. 10141 if (RequireCompleteType(ELoc, Type, 10142 diag::err_omp_firstprivate_incomplete_type)) 10143 continue; 10144 Type = Type.getNonReferenceType(); 10145 10146 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 10147 // A variable of class type (or array thereof) that appears in a private 10148 // clause requires an accessible, unambiguous copy constructor for the 10149 // class type. 10150 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 10151 10152 // If an implicit firstprivate variable found it was checked already. 10153 DSAStackTy::DSAVarData TopDVar; 10154 if (!IsImplicitClause) { 10155 DSAStackTy::DSAVarData DVar = 10156 DSAStack->getTopDSA(D, /*FromParent=*/false); 10157 TopDVar = DVar; 10158 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10159 bool IsConstant = ElemType.isConstant(Context); 10160 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 10161 // A list item that specifies a given variable may not appear in more 10162 // than one clause on the same directive, except that a variable may be 10163 // specified in both firstprivate and lastprivate clauses. 10164 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 10165 // A list item may appear in a firstprivate or lastprivate clause but not 10166 // both. 10167 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 10168 (isOpenMPDistributeDirective(CurrDir) || 10169 DVar.CKind != OMPC_lastprivate) && 10170 DVar.RefExpr) { 10171 Diag(ELoc, diag::err_omp_wrong_dsa) 10172 << getOpenMPClauseName(DVar.CKind) 10173 << getOpenMPClauseName(OMPC_firstprivate); 10174 reportOriginalDsa(*this, DSAStack, D, DVar); 10175 continue; 10176 } 10177 10178 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10179 // in a Construct] 10180 // Variables with the predetermined data-sharing attributes may not be 10181 // listed in data-sharing attributes clauses, except for the cases 10182 // listed below. For these exceptions only, listing a predetermined 10183 // variable in a data-sharing attribute clause is allowed and overrides 10184 // the variable's predetermined data-sharing attributes. 10185 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10186 // in a Construct, C/C++, p.2] 10187 // Variables with const-qualified type having no mutable member may be 10188 // listed in a firstprivate clause, even if they are static data members. 10189 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 10190 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 10191 Diag(ELoc, diag::err_omp_wrong_dsa) 10192 << getOpenMPClauseName(DVar.CKind) 10193 << getOpenMPClauseName(OMPC_firstprivate); 10194 reportOriginalDsa(*this, DSAStack, D, DVar); 10195 continue; 10196 } 10197 10198 // OpenMP [2.9.3.4, Restrictions, p.2] 10199 // A list item that is private within a parallel region must not appear 10200 // in a firstprivate clause on a worksharing construct if any of the 10201 // worksharing regions arising from the worksharing construct ever bind 10202 // to any of the parallel regions arising from the parallel construct. 10203 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 10204 // A list item that is private within a teams region must not appear in a 10205 // firstprivate clause on a distribute construct if any of the distribute 10206 // regions arising from the distribute construct ever bind to any of the 10207 // teams regions arising from the teams construct. 10208 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 10209 // A list item that appears in a reduction clause of a teams construct 10210 // must not appear in a firstprivate clause on a distribute construct if 10211 // any of the distribute regions arising from the distribute construct 10212 // ever bind to any of the teams regions arising from the teams construct. 10213 if ((isOpenMPWorksharingDirective(CurrDir) || 10214 isOpenMPDistributeDirective(CurrDir)) && 10215 !isOpenMPParallelDirective(CurrDir) && 10216 !isOpenMPTeamsDirective(CurrDir)) { 10217 DVar = DSAStack->getImplicitDSA(D, true); 10218 if (DVar.CKind != OMPC_shared && 10219 (isOpenMPParallelDirective(DVar.DKind) || 10220 isOpenMPTeamsDirective(DVar.DKind) || 10221 DVar.DKind == OMPD_unknown)) { 10222 Diag(ELoc, diag::err_omp_required_access) 10223 << getOpenMPClauseName(OMPC_firstprivate) 10224 << getOpenMPClauseName(OMPC_shared); 10225 reportOriginalDsa(*this, DSAStack, D, DVar); 10226 continue; 10227 } 10228 } 10229 // OpenMP [2.9.3.4, Restrictions, p.3] 10230 // A list item that appears in a reduction clause of a parallel construct 10231 // must not appear in a firstprivate clause on a worksharing or task 10232 // construct if any of the worksharing or task regions arising from the 10233 // worksharing or task construct ever bind to any of the parallel regions 10234 // arising from the parallel construct. 10235 // OpenMP [2.9.3.4, Restrictions, p.4] 10236 // A list item that appears in a reduction clause in worksharing 10237 // construct must not appear in a firstprivate clause in a task construct 10238 // encountered during execution of any of the worksharing regions arising 10239 // from the worksharing construct. 10240 if (isOpenMPTaskingDirective(CurrDir)) { 10241 DVar = DSAStack->hasInnermostDSA( 10242 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 10243 [](OpenMPDirectiveKind K) { 10244 return isOpenMPParallelDirective(K) || 10245 isOpenMPWorksharingDirective(K) || 10246 isOpenMPTeamsDirective(K); 10247 }, 10248 /*FromParent=*/true); 10249 if (DVar.CKind == OMPC_reduction && 10250 (isOpenMPParallelDirective(DVar.DKind) || 10251 isOpenMPWorksharingDirective(DVar.DKind) || 10252 isOpenMPTeamsDirective(DVar.DKind))) { 10253 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 10254 << getOpenMPDirectiveName(DVar.DKind); 10255 reportOriginalDsa(*this, DSAStack, D, DVar); 10256 continue; 10257 } 10258 } 10259 10260 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10261 // A list item cannot appear in both a map clause and a data-sharing 10262 // attribute clause on the same construct 10263 if (isOpenMPTargetExecutionDirective(CurrDir)) { 10264 OpenMPClauseKind ConflictKind; 10265 if (DSAStack->checkMappableExprComponentListsForDecl( 10266 VD, /*CurrentRegionOnly=*/true, 10267 [&ConflictKind]( 10268 OMPClauseMappableExprCommon::MappableExprComponentListRef, 10269 OpenMPClauseKind WhereFoundClauseKind) { 10270 ConflictKind = WhereFoundClauseKind; 10271 return true; 10272 })) { 10273 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10274 << getOpenMPClauseName(OMPC_firstprivate) 10275 << getOpenMPClauseName(ConflictKind) 10276 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10277 reportOriginalDsa(*this, DSAStack, D, DVar); 10278 continue; 10279 } 10280 } 10281 } 10282 10283 // Variably modified types are not supported for tasks. 10284 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10285 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 10286 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10287 << getOpenMPClauseName(OMPC_firstprivate) << Type 10288 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10289 bool IsDecl = 10290 !VD || 10291 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10292 Diag(D->getLocation(), 10293 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10294 << D; 10295 continue; 10296 } 10297 10298 Type = Type.getUnqualifiedType(); 10299 VarDecl *VDPrivate = 10300 buildVarDecl(*this, ELoc, Type, D->getName(), 10301 D->hasAttrs() ? &D->getAttrs() : nullptr, 10302 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10303 // Generate helper private variable and initialize it with the value of the 10304 // original variable. The address of the original variable is replaced by 10305 // the address of the new private variable in the CodeGen. This new variable 10306 // is not added to IdResolver, so the code in the OpenMP region uses 10307 // original variable for proper diagnostics and variable capturing. 10308 Expr *VDInitRefExpr = nullptr; 10309 // For arrays generate initializer for single element and replace it by the 10310 // original array element in CodeGen. 10311 if (Type->isArrayType()) { 10312 VarDecl *VDInit = 10313 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 10314 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 10315 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 10316 ElemType = ElemType.getUnqualifiedType(); 10317 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 10318 ".firstprivate.temp"); 10319 InitializedEntity Entity = 10320 InitializedEntity::InitializeVariable(VDInitTemp); 10321 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 10322 10323 InitializationSequence InitSeq(*this, Entity, Kind, Init); 10324 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 10325 if (Result.isInvalid()) 10326 VDPrivate->setInvalidDecl(); 10327 else 10328 VDPrivate->setInit(Result.getAs<Expr>()); 10329 // Remove temp variable declaration. 10330 Context.Deallocate(VDInitTemp); 10331 } else { 10332 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 10333 ".firstprivate.temp"); 10334 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 10335 RefExpr->getExprLoc()); 10336 AddInitializerToDecl(VDPrivate, 10337 DefaultLvalueConversion(VDInitRefExpr).get(), 10338 /*DirectInit=*/false); 10339 } 10340 if (VDPrivate->isInvalidDecl()) { 10341 if (IsImplicitClause) { 10342 Diag(RefExpr->getExprLoc(), 10343 diag::note_omp_task_predetermined_firstprivate_here); 10344 } 10345 continue; 10346 } 10347 CurContext->addDecl(VDPrivate); 10348 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10349 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 10350 RefExpr->getExprLoc()); 10351 DeclRefExpr *Ref = nullptr; 10352 if (!VD && !CurContext->isDependentContext()) { 10353 if (TopDVar.CKind == OMPC_lastprivate) { 10354 Ref = TopDVar.PrivateCopy; 10355 } else { 10356 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10357 if (!isOpenMPCapturedDecl(D)) 10358 ExprCaptures.push_back(Ref->getDecl()); 10359 } 10360 } 10361 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 10362 Vars.push_back((VD || CurContext->isDependentContext()) 10363 ? RefExpr->IgnoreParens() 10364 : Ref); 10365 PrivateCopies.push_back(VDPrivateRefExpr); 10366 Inits.push_back(VDInitRefExpr); 10367 } 10368 10369 if (Vars.empty()) 10370 return nullptr; 10371 10372 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10373 Vars, PrivateCopies, Inits, 10374 buildPreInits(Context, ExprCaptures)); 10375 } 10376 10377 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 10378 SourceLocation StartLoc, 10379 SourceLocation LParenLoc, 10380 SourceLocation EndLoc) { 10381 SmallVector<Expr *, 8> Vars; 10382 SmallVector<Expr *, 8> SrcExprs; 10383 SmallVector<Expr *, 8> DstExprs; 10384 SmallVector<Expr *, 8> AssignmentOps; 10385 SmallVector<Decl *, 4> ExprCaptures; 10386 SmallVector<Expr *, 4> ExprPostUpdates; 10387 for (Expr *RefExpr : VarList) { 10388 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 10389 SourceLocation ELoc; 10390 SourceRange ERange; 10391 Expr *SimpleRefExpr = RefExpr; 10392 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10393 if (Res.second) { 10394 // It will be analyzed later. 10395 Vars.push_back(RefExpr); 10396 SrcExprs.push_back(nullptr); 10397 DstExprs.push_back(nullptr); 10398 AssignmentOps.push_back(nullptr); 10399 } 10400 ValueDecl *D = Res.first; 10401 if (!D) 10402 continue; 10403 10404 QualType Type = D->getType(); 10405 auto *VD = dyn_cast<VarDecl>(D); 10406 10407 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 10408 // A variable that appears in a lastprivate clause must not have an 10409 // incomplete type or a reference type. 10410 if (RequireCompleteType(ELoc, Type, 10411 diag::err_omp_lastprivate_incomplete_type)) 10412 continue; 10413 Type = Type.getNonReferenceType(); 10414 10415 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 10416 // A variable that is privatized must not have a const-qualified type 10417 // unless it is of class type with a mutable member. This restriction does 10418 // not apply to the firstprivate clause. 10419 // 10420 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 10421 // A variable that appears in a lastprivate clause must not have a 10422 // const-qualified type unless it is of class type with a mutable member. 10423 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 10424 continue; 10425 10426 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10427 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 10428 // in a Construct] 10429 // Variables with the predetermined data-sharing attributes may not be 10430 // listed in data-sharing attributes clauses, except for the cases 10431 // listed below. 10432 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 10433 // A list item may appear in a firstprivate or lastprivate clause but not 10434 // both. 10435 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10436 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 10437 (isOpenMPDistributeDirective(CurrDir) || 10438 DVar.CKind != OMPC_firstprivate) && 10439 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 10440 Diag(ELoc, diag::err_omp_wrong_dsa) 10441 << getOpenMPClauseName(DVar.CKind) 10442 << getOpenMPClauseName(OMPC_lastprivate); 10443 reportOriginalDsa(*this, DSAStack, D, DVar); 10444 continue; 10445 } 10446 10447 // OpenMP [2.14.3.5, Restrictions, p.2] 10448 // A list item that is private within a parallel region, or that appears in 10449 // the reduction clause of a parallel construct, must not appear in a 10450 // lastprivate clause on a worksharing construct if any of the corresponding 10451 // worksharing regions ever binds to any of the corresponding parallel 10452 // regions. 10453 DSAStackTy::DSAVarData TopDVar = DVar; 10454 if (isOpenMPWorksharingDirective(CurrDir) && 10455 !isOpenMPParallelDirective(CurrDir) && 10456 !isOpenMPTeamsDirective(CurrDir)) { 10457 DVar = DSAStack->getImplicitDSA(D, true); 10458 if (DVar.CKind != OMPC_shared) { 10459 Diag(ELoc, diag::err_omp_required_access) 10460 << getOpenMPClauseName(OMPC_lastprivate) 10461 << getOpenMPClauseName(OMPC_shared); 10462 reportOriginalDsa(*this, DSAStack, D, DVar); 10463 continue; 10464 } 10465 } 10466 10467 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 10468 // A variable of class type (or array thereof) that appears in a 10469 // lastprivate clause requires an accessible, unambiguous default 10470 // constructor for the class type, unless the list item is also specified 10471 // in a firstprivate clause. 10472 // A variable of class type (or array thereof) that appears in a 10473 // lastprivate clause requires an accessible, unambiguous copy assignment 10474 // operator for the class type. 10475 Type = Context.getBaseElementType(Type).getNonReferenceType(); 10476 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 10477 Type.getUnqualifiedType(), ".lastprivate.src", 10478 D->hasAttrs() ? &D->getAttrs() : nullptr); 10479 DeclRefExpr *PseudoSrcExpr = 10480 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 10481 VarDecl *DstVD = 10482 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 10483 D->hasAttrs() ? &D->getAttrs() : nullptr); 10484 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 10485 // For arrays generate assignment operation for single element and replace 10486 // it by the original array element in CodeGen. 10487 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 10488 PseudoDstExpr, PseudoSrcExpr); 10489 if (AssignmentOp.isInvalid()) 10490 continue; 10491 AssignmentOp = 10492 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 10493 if (AssignmentOp.isInvalid()) 10494 continue; 10495 10496 DeclRefExpr *Ref = nullptr; 10497 if (!VD && !CurContext->isDependentContext()) { 10498 if (TopDVar.CKind == OMPC_firstprivate) { 10499 Ref = TopDVar.PrivateCopy; 10500 } else { 10501 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10502 if (!isOpenMPCapturedDecl(D)) 10503 ExprCaptures.push_back(Ref->getDecl()); 10504 } 10505 if (TopDVar.CKind == OMPC_firstprivate || 10506 (!isOpenMPCapturedDecl(D) && 10507 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 10508 ExprResult RefRes = DefaultLvalueConversion(Ref); 10509 if (!RefRes.isUsable()) 10510 continue; 10511 ExprResult PostUpdateRes = 10512 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 10513 RefRes.get()); 10514 if (!PostUpdateRes.isUsable()) 10515 continue; 10516 ExprPostUpdates.push_back( 10517 IgnoredValueConversions(PostUpdateRes.get()).get()); 10518 } 10519 } 10520 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 10521 Vars.push_back((VD || CurContext->isDependentContext()) 10522 ? RefExpr->IgnoreParens() 10523 : Ref); 10524 SrcExprs.push_back(PseudoSrcExpr); 10525 DstExprs.push_back(PseudoDstExpr); 10526 AssignmentOps.push_back(AssignmentOp.get()); 10527 } 10528 10529 if (Vars.empty()) 10530 return nullptr; 10531 10532 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10533 Vars, SrcExprs, DstExprs, AssignmentOps, 10534 buildPreInits(Context, ExprCaptures), 10535 buildPostUpdate(*this, ExprPostUpdates)); 10536 } 10537 10538 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 10539 SourceLocation StartLoc, 10540 SourceLocation LParenLoc, 10541 SourceLocation EndLoc) { 10542 SmallVector<Expr *, 8> Vars; 10543 for (Expr *RefExpr : VarList) { 10544 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 10545 SourceLocation ELoc; 10546 SourceRange ERange; 10547 Expr *SimpleRefExpr = RefExpr; 10548 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10549 if (Res.second) { 10550 // It will be analyzed later. 10551 Vars.push_back(RefExpr); 10552 } 10553 ValueDecl *D = Res.first; 10554 if (!D) 10555 continue; 10556 10557 auto *VD = dyn_cast<VarDecl>(D); 10558 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10559 // in a Construct] 10560 // Variables with the predetermined data-sharing attributes may not be 10561 // listed in data-sharing attributes clauses, except for the cases 10562 // listed below. For these exceptions only, listing a predetermined 10563 // variable in a data-sharing attribute clause is allowed and overrides 10564 // the variable's predetermined data-sharing attributes. 10565 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10566 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 10567 DVar.RefExpr) { 10568 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10569 << getOpenMPClauseName(OMPC_shared); 10570 reportOriginalDsa(*this, DSAStack, D, DVar); 10571 continue; 10572 } 10573 10574 DeclRefExpr *Ref = nullptr; 10575 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 10576 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10577 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 10578 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 10579 ? RefExpr->IgnoreParens() 10580 : Ref); 10581 } 10582 10583 if (Vars.empty()) 10584 return nullptr; 10585 10586 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 10587 } 10588 10589 namespace { 10590 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 10591 DSAStackTy *Stack; 10592 10593 public: 10594 bool VisitDeclRefExpr(DeclRefExpr *E) { 10595 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 10596 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 10597 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 10598 return false; 10599 if (DVar.CKind != OMPC_unknown) 10600 return true; 10601 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 10602 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 10603 /*FromParent=*/true); 10604 return DVarPrivate.CKind != OMPC_unknown; 10605 } 10606 return false; 10607 } 10608 bool VisitStmt(Stmt *S) { 10609 for (Stmt *Child : S->children()) { 10610 if (Child && Visit(Child)) 10611 return true; 10612 } 10613 return false; 10614 } 10615 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 10616 }; 10617 } // namespace 10618 10619 namespace { 10620 // Transform MemberExpression for specified FieldDecl of current class to 10621 // DeclRefExpr to specified OMPCapturedExprDecl. 10622 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 10623 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 10624 ValueDecl *Field = nullptr; 10625 DeclRefExpr *CapturedExpr = nullptr; 10626 10627 public: 10628 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 10629 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 10630 10631 ExprResult TransformMemberExpr(MemberExpr *E) { 10632 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 10633 E->getMemberDecl() == Field) { 10634 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 10635 return CapturedExpr; 10636 } 10637 return BaseTransform::TransformMemberExpr(E); 10638 } 10639 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 10640 }; 10641 } // namespace 10642 10643 template <typename T, typename U> 10644 static T filterLookupForUDReductionAndMapper( 10645 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 10646 for (U &Set : Lookups) { 10647 for (auto *D : Set) { 10648 if (T Res = Gen(cast<ValueDecl>(D))) 10649 return Res; 10650 } 10651 } 10652 return T(); 10653 } 10654 10655 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 10656 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 10657 10658 for (auto RD : D->redecls()) { 10659 // Don't bother with extra checks if we already know this one isn't visible. 10660 if (RD == D) 10661 continue; 10662 10663 auto ND = cast<NamedDecl>(RD); 10664 if (LookupResult::isVisible(SemaRef, ND)) 10665 return ND; 10666 } 10667 10668 return nullptr; 10669 } 10670 10671 static void 10672 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 10673 SourceLocation Loc, QualType Ty, 10674 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 10675 // Find all of the associated namespaces and classes based on the 10676 // arguments we have. 10677 Sema::AssociatedNamespaceSet AssociatedNamespaces; 10678 Sema::AssociatedClassSet AssociatedClasses; 10679 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 10680 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 10681 AssociatedClasses); 10682 10683 // C++ [basic.lookup.argdep]p3: 10684 // Let X be the lookup set produced by unqualified lookup (3.4.1) 10685 // and let Y be the lookup set produced by argument dependent 10686 // lookup (defined as follows). If X contains [...] then Y is 10687 // empty. Otherwise Y is the set of declarations found in the 10688 // namespaces associated with the argument types as described 10689 // below. The set of declarations found by the lookup of the name 10690 // is the union of X and Y. 10691 // 10692 // Here, we compute Y and add its members to the overloaded 10693 // candidate set. 10694 for (auto *NS : AssociatedNamespaces) { 10695 // When considering an associated namespace, the lookup is the 10696 // same as the lookup performed when the associated namespace is 10697 // used as a qualifier (3.4.3.2) except that: 10698 // 10699 // -- Any using-directives in the associated namespace are 10700 // ignored. 10701 // 10702 // -- Any namespace-scope friend functions declared in 10703 // associated classes are visible within their respective 10704 // namespaces even if they are not visible during an ordinary 10705 // lookup (11.4). 10706 DeclContext::lookup_result R = NS->lookup(Id.getName()); 10707 for (auto *D : R) { 10708 auto *Underlying = D; 10709 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 10710 Underlying = USD->getTargetDecl(); 10711 10712 if (!isa<OMPDeclareReductionDecl>(Underlying) && 10713 !isa<OMPDeclareMapperDecl>(Underlying)) 10714 continue; 10715 10716 if (!SemaRef.isVisible(D)) { 10717 D = findAcceptableDecl(SemaRef, D); 10718 if (!D) 10719 continue; 10720 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 10721 Underlying = USD->getTargetDecl(); 10722 } 10723 Lookups.emplace_back(); 10724 Lookups.back().addDecl(Underlying); 10725 } 10726 } 10727 } 10728 10729 static ExprResult 10730 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 10731 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 10732 const DeclarationNameInfo &ReductionId, QualType Ty, 10733 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 10734 if (ReductionIdScopeSpec.isInvalid()) 10735 return ExprError(); 10736 SmallVector<UnresolvedSet<8>, 4> Lookups; 10737 if (S) { 10738 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 10739 Lookup.suppressDiagnostics(); 10740 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 10741 NamedDecl *D = Lookup.getRepresentativeDecl(); 10742 do { 10743 S = S->getParent(); 10744 } while (S && !S->isDeclScope(D)); 10745 if (S) 10746 S = S->getParent(); 10747 Lookups.emplace_back(); 10748 Lookups.back().append(Lookup.begin(), Lookup.end()); 10749 Lookup.clear(); 10750 } 10751 } else if (auto *ULE = 10752 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 10753 Lookups.push_back(UnresolvedSet<8>()); 10754 Decl *PrevD = nullptr; 10755 for (NamedDecl *D : ULE->decls()) { 10756 if (D == PrevD) 10757 Lookups.push_back(UnresolvedSet<8>()); 10758 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 10759 Lookups.back().addDecl(DRD); 10760 PrevD = D; 10761 } 10762 } 10763 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 10764 Ty->isInstantiationDependentType() || 10765 Ty->containsUnexpandedParameterPack() || 10766 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 10767 return !D->isInvalidDecl() && 10768 (D->getType()->isDependentType() || 10769 D->getType()->isInstantiationDependentType() || 10770 D->getType()->containsUnexpandedParameterPack()); 10771 })) { 10772 UnresolvedSet<8> ResSet; 10773 for (const UnresolvedSet<8> &Set : Lookups) { 10774 if (Set.empty()) 10775 continue; 10776 ResSet.append(Set.begin(), Set.end()); 10777 // The last item marks the end of all declarations at the specified scope. 10778 ResSet.addDecl(Set[Set.size() - 1]); 10779 } 10780 return UnresolvedLookupExpr::Create( 10781 SemaRef.Context, /*NamingClass=*/nullptr, 10782 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 10783 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 10784 } 10785 // Lookup inside the classes. 10786 // C++ [over.match.oper]p3: 10787 // For a unary operator @ with an operand of a type whose 10788 // cv-unqualified version is T1, and for a binary operator @ with 10789 // a left operand of a type whose cv-unqualified version is T1 and 10790 // a right operand of a type whose cv-unqualified version is T2, 10791 // three sets of candidate functions, designated member 10792 // candidates, non-member candidates and built-in candidates, are 10793 // constructed as follows: 10794 // -- If T1 is a complete class type or a class currently being 10795 // defined, the set of member candidates is the result of the 10796 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 10797 // the set of member candidates is empty. 10798 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 10799 Lookup.suppressDiagnostics(); 10800 if (const auto *TyRec = Ty->getAs<RecordType>()) { 10801 // Complete the type if it can be completed. 10802 // If the type is neither complete nor being defined, bail out now. 10803 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 10804 TyRec->getDecl()->getDefinition()) { 10805 Lookup.clear(); 10806 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 10807 if (Lookup.empty()) { 10808 Lookups.emplace_back(); 10809 Lookups.back().append(Lookup.begin(), Lookup.end()); 10810 } 10811 } 10812 } 10813 // Perform ADL. 10814 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 10815 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 10816 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 10817 if (!D->isInvalidDecl() && 10818 SemaRef.Context.hasSameType(D->getType(), Ty)) 10819 return D; 10820 return nullptr; 10821 })) 10822 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 10823 VK_LValue, Loc); 10824 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 10825 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 10826 if (!D->isInvalidDecl() && 10827 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 10828 !Ty.isMoreQualifiedThan(D->getType())) 10829 return D; 10830 return nullptr; 10831 })) { 10832 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 10833 /*DetectVirtual=*/false); 10834 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 10835 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 10836 VD->getType().getUnqualifiedType()))) { 10837 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 10838 /*DiagID=*/0) != 10839 Sema::AR_inaccessible) { 10840 SemaRef.BuildBasePathArray(Paths, BasePath); 10841 return SemaRef.BuildDeclRefExpr( 10842 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 10843 } 10844 } 10845 } 10846 } 10847 if (ReductionIdScopeSpec.isSet()) { 10848 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 10849 return ExprError(); 10850 } 10851 return ExprEmpty(); 10852 } 10853 10854 namespace { 10855 /// Data for the reduction-based clauses. 10856 struct ReductionData { 10857 /// List of original reduction items. 10858 SmallVector<Expr *, 8> Vars; 10859 /// List of private copies of the reduction items. 10860 SmallVector<Expr *, 8> Privates; 10861 /// LHS expressions for the reduction_op expressions. 10862 SmallVector<Expr *, 8> LHSs; 10863 /// RHS expressions for the reduction_op expressions. 10864 SmallVector<Expr *, 8> RHSs; 10865 /// Reduction operation expression. 10866 SmallVector<Expr *, 8> ReductionOps; 10867 /// Taskgroup descriptors for the corresponding reduction items in 10868 /// in_reduction clauses. 10869 SmallVector<Expr *, 8> TaskgroupDescriptors; 10870 /// List of captures for clause. 10871 SmallVector<Decl *, 4> ExprCaptures; 10872 /// List of postupdate expressions. 10873 SmallVector<Expr *, 4> ExprPostUpdates; 10874 ReductionData() = delete; 10875 /// Reserves required memory for the reduction data. 10876 ReductionData(unsigned Size) { 10877 Vars.reserve(Size); 10878 Privates.reserve(Size); 10879 LHSs.reserve(Size); 10880 RHSs.reserve(Size); 10881 ReductionOps.reserve(Size); 10882 TaskgroupDescriptors.reserve(Size); 10883 ExprCaptures.reserve(Size); 10884 ExprPostUpdates.reserve(Size); 10885 } 10886 /// Stores reduction item and reduction operation only (required for dependent 10887 /// reduction item). 10888 void push(Expr *Item, Expr *ReductionOp) { 10889 Vars.emplace_back(Item); 10890 Privates.emplace_back(nullptr); 10891 LHSs.emplace_back(nullptr); 10892 RHSs.emplace_back(nullptr); 10893 ReductionOps.emplace_back(ReductionOp); 10894 TaskgroupDescriptors.emplace_back(nullptr); 10895 } 10896 /// Stores reduction data. 10897 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 10898 Expr *TaskgroupDescriptor) { 10899 Vars.emplace_back(Item); 10900 Privates.emplace_back(Private); 10901 LHSs.emplace_back(LHS); 10902 RHSs.emplace_back(RHS); 10903 ReductionOps.emplace_back(ReductionOp); 10904 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 10905 } 10906 }; 10907 } // namespace 10908 10909 static bool checkOMPArraySectionConstantForReduction( 10910 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 10911 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 10912 const Expr *Length = OASE->getLength(); 10913 if (Length == nullptr) { 10914 // For array sections of the form [1:] or [:], we would need to analyze 10915 // the lower bound... 10916 if (OASE->getColonLoc().isValid()) 10917 return false; 10918 10919 // This is an array subscript which has implicit length 1! 10920 SingleElement = true; 10921 ArraySizes.push_back(llvm::APSInt::get(1)); 10922 } else { 10923 Expr::EvalResult Result; 10924 if (!Length->EvaluateAsInt(Result, Context)) 10925 return false; 10926 10927 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 10928 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 10929 ArraySizes.push_back(ConstantLengthValue); 10930 } 10931 10932 // Get the base of this array section and walk up from there. 10933 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 10934 10935 // We require length = 1 for all array sections except the right-most to 10936 // guarantee that the memory region is contiguous and has no holes in it. 10937 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 10938 Length = TempOASE->getLength(); 10939 if (Length == nullptr) { 10940 // For array sections of the form [1:] or [:], we would need to analyze 10941 // the lower bound... 10942 if (OASE->getColonLoc().isValid()) 10943 return false; 10944 10945 // This is an array subscript which has implicit length 1! 10946 ArraySizes.push_back(llvm::APSInt::get(1)); 10947 } else { 10948 Expr::EvalResult Result; 10949 if (!Length->EvaluateAsInt(Result, Context)) 10950 return false; 10951 10952 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 10953 if (ConstantLengthValue.getSExtValue() != 1) 10954 return false; 10955 10956 ArraySizes.push_back(ConstantLengthValue); 10957 } 10958 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 10959 } 10960 10961 // If we have a single element, we don't need to add the implicit lengths. 10962 if (!SingleElement) { 10963 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 10964 // Has implicit length 1! 10965 ArraySizes.push_back(llvm::APSInt::get(1)); 10966 Base = TempASE->getBase()->IgnoreParenImpCasts(); 10967 } 10968 } 10969 10970 // This array section can be privatized as a single value or as a constant 10971 // sized array. 10972 return true; 10973 } 10974 10975 static bool actOnOMPReductionKindClause( 10976 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 10977 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10978 SourceLocation ColonLoc, SourceLocation EndLoc, 10979 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10980 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 10981 DeclarationName DN = ReductionId.getName(); 10982 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 10983 BinaryOperatorKind BOK = BO_Comma; 10984 10985 ASTContext &Context = S.Context; 10986 // OpenMP [2.14.3.6, reduction clause] 10987 // C 10988 // reduction-identifier is either an identifier or one of the following 10989 // operators: +, -, *, &, |, ^, && and || 10990 // C++ 10991 // reduction-identifier is either an id-expression or one of the following 10992 // operators: +, -, *, &, |, ^, && and || 10993 switch (OOK) { 10994 case OO_Plus: 10995 case OO_Minus: 10996 BOK = BO_Add; 10997 break; 10998 case OO_Star: 10999 BOK = BO_Mul; 11000 break; 11001 case OO_Amp: 11002 BOK = BO_And; 11003 break; 11004 case OO_Pipe: 11005 BOK = BO_Or; 11006 break; 11007 case OO_Caret: 11008 BOK = BO_Xor; 11009 break; 11010 case OO_AmpAmp: 11011 BOK = BO_LAnd; 11012 break; 11013 case OO_PipePipe: 11014 BOK = BO_LOr; 11015 break; 11016 case OO_New: 11017 case OO_Delete: 11018 case OO_Array_New: 11019 case OO_Array_Delete: 11020 case OO_Slash: 11021 case OO_Percent: 11022 case OO_Tilde: 11023 case OO_Exclaim: 11024 case OO_Equal: 11025 case OO_Less: 11026 case OO_Greater: 11027 case OO_LessEqual: 11028 case OO_GreaterEqual: 11029 case OO_PlusEqual: 11030 case OO_MinusEqual: 11031 case OO_StarEqual: 11032 case OO_SlashEqual: 11033 case OO_PercentEqual: 11034 case OO_CaretEqual: 11035 case OO_AmpEqual: 11036 case OO_PipeEqual: 11037 case OO_LessLess: 11038 case OO_GreaterGreater: 11039 case OO_LessLessEqual: 11040 case OO_GreaterGreaterEqual: 11041 case OO_EqualEqual: 11042 case OO_ExclaimEqual: 11043 case OO_Spaceship: 11044 case OO_PlusPlus: 11045 case OO_MinusMinus: 11046 case OO_Comma: 11047 case OO_ArrowStar: 11048 case OO_Arrow: 11049 case OO_Call: 11050 case OO_Subscript: 11051 case OO_Conditional: 11052 case OO_Coawait: 11053 case NUM_OVERLOADED_OPERATORS: 11054 llvm_unreachable("Unexpected reduction identifier"); 11055 case OO_None: 11056 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 11057 if (II->isStr("max")) 11058 BOK = BO_GT; 11059 else if (II->isStr("min")) 11060 BOK = BO_LT; 11061 } 11062 break; 11063 } 11064 SourceRange ReductionIdRange; 11065 if (ReductionIdScopeSpec.isValid()) 11066 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 11067 else 11068 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 11069 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 11070 11071 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 11072 bool FirstIter = true; 11073 for (Expr *RefExpr : VarList) { 11074 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 11075 // OpenMP [2.1, C/C++] 11076 // A list item is a variable or array section, subject to the restrictions 11077 // specified in Section 2.4 on page 42 and in each of the sections 11078 // describing clauses and directives for which a list appears. 11079 // OpenMP [2.14.3.3, Restrictions, p.1] 11080 // A variable that is part of another variable (as an array or 11081 // structure element) cannot appear in a private clause. 11082 if (!FirstIter && IR != ER) 11083 ++IR; 11084 FirstIter = false; 11085 SourceLocation ELoc; 11086 SourceRange ERange; 11087 Expr *SimpleRefExpr = RefExpr; 11088 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 11089 /*AllowArraySection=*/true); 11090 if (Res.second) { 11091 // Try to find 'declare reduction' corresponding construct before using 11092 // builtin/overloaded operators. 11093 QualType Type = Context.DependentTy; 11094 CXXCastPath BasePath; 11095 ExprResult DeclareReductionRef = buildDeclareReductionRef( 11096 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 11097 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 11098 Expr *ReductionOp = nullptr; 11099 if (S.CurContext->isDependentContext() && 11100 (DeclareReductionRef.isUnset() || 11101 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 11102 ReductionOp = DeclareReductionRef.get(); 11103 // It will be analyzed later. 11104 RD.push(RefExpr, ReductionOp); 11105 } 11106 ValueDecl *D = Res.first; 11107 if (!D) 11108 continue; 11109 11110 Expr *TaskgroupDescriptor = nullptr; 11111 QualType Type; 11112 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 11113 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 11114 if (ASE) { 11115 Type = ASE->getType().getNonReferenceType(); 11116 } else if (OASE) { 11117 QualType BaseType = 11118 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 11119 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 11120 Type = ATy->getElementType(); 11121 else 11122 Type = BaseType->getPointeeType(); 11123 Type = Type.getNonReferenceType(); 11124 } else { 11125 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 11126 } 11127 auto *VD = dyn_cast<VarDecl>(D); 11128 11129 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 11130 // A variable that appears in a private clause must not have an incomplete 11131 // type or a reference type. 11132 if (S.RequireCompleteType(ELoc, D->getType(), 11133 diag::err_omp_reduction_incomplete_type)) 11134 continue; 11135 // OpenMP [2.14.3.6, reduction clause, Restrictions] 11136 // A list item that appears in a reduction clause must not be 11137 // const-qualified. 11138 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 11139 /*AcceptIfMutable*/ false, ASE || OASE)) 11140 continue; 11141 11142 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 11143 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 11144 // If a list-item is a reference type then it must bind to the same object 11145 // for all threads of the team. 11146 if (!ASE && !OASE) { 11147 if (VD) { 11148 VarDecl *VDDef = VD->getDefinition(); 11149 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 11150 DSARefChecker Check(Stack); 11151 if (Check.Visit(VDDef->getInit())) { 11152 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 11153 << getOpenMPClauseName(ClauseKind) << ERange; 11154 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 11155 continue; 11156 } 11157 } 11158 } 11159 11160 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 11161 // in a Construct] 11162 // Variables with the predetermined data-sharing attributes may not be 11163 // listed in data-sharing attributes clauses, except for the cases 11164 // listed below. For these exceptions only, listing a predetermined 11165 // variable in a data-sharing attribute clause is allowed and overrides 11166 // the variable's predetermined data-sharing attributes. 11167 // OpenMP [2.14.3.6, Restrictions, p.3] 11168 // Any number of reduction clauses can be specified on the directive, 11169 // but a list item can appear only once in the reduction clauses for that 11170 // directive. 11171 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 11172 if (DVar.CKind == OMPC_reduction) { 11173 S.Diag(ELoc, diag::err_omp_once_referenced) 11174 << getOpenMPClauseName(ClauseKind); 11175 if (DVar.RefExpr) 11176 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 11177 continue; 11178 } 11179 if (DVar.CKind != OMPC_unknown) { 11180 S.Diag(ELoc, diag::err_omp_wrong_dsa) 11181 << getOpenMPClauseName(DVar.CKind) 11182 << getOpenMPClauseName(OMPC_reduction); 11183 reportOriginalDsa(S, Stack, D, DVar); 11184 continue; 11185 } 11186 11187 // OpenMP [2.14.3.6, Restrictions, p.1] 11188 // A list item that appears in a reduction clause of a worksharing 11189 // construct must be shared in the parallel regions to which any of the 11190 // worksharing regions arising from the worksharing construct bind. 11191 if (isOpenMPWorksharingDirective(CurrDir) && 11192 !isOpenMPParallelDirective(CurrDir) && 11193 !isOpenMPTeamsDirective(CurrDir)) { 11194 DVar = Stack->getImplicitDSA(D, true); 11195 if (DVar.CKind != OMPC_shared) { 11196 S.Diag(ELoc, diag::err_omp_required_access) 11197 << getOpenMPClauseName(OMPC_reduction) 11198 << getOpenMPClauseName(OMPC_shared); 11199 reportOriginalDsa(S, Stack, D, DVar); 11200 continue; 11201 } 11202 } 11203 } 11204 11205 // Try to find 'declare reduction' corresponding construct before using 11206 // builtin/overloaded operators. 11207 CXXCastPath BasePath; 11208 ExprResult DeclareReductionRef = buildDeclareReductionRef( 11209 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 11210 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 11211 if (DeclareReductionRef.isInvalid()) 11212 continue; 11213 if (S.CurContext->isDependentContext() && 11214 (DeclareReductionRef.isUnset() || 11215 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 11216 RD.push(RefExpr, DeclareReductionRef.get()); 11217 continue; 11218 } 11219 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 11220 // Not allowed reduction identifier is found. 11221 S.Diag(ReductionId.getBeginLoc(), 11222 diag::err_omp_unknown_reduction_identifier) 11223 << Type << ReductionIdRange; 11224 continue; 11225 } 11226 11227 // OpenMP [2.14.3.6, reduction clause, Restrictions] 11228 // The type of a list item that appears in a reduction clause must be valid 11229 // for the reduction-identifier. For a max or min reduction in C, the type 11230 // of the list item must be an allowed arithmetic data type: char, int, 11231 // float, double, or _Bool, possibly modified with long, short, signed, or 11232 // unsigned. For a max or min reduction in C++, the type of the list item 11233 // must be an allowed arithmetic data type: char, wchar_t, int, float, 11234 // double, or bool, possibly modified with long, short, signed, or unsigned. 11235 if (DeclareReductionRef.isUnset()) { 11236 if ((BOK == BO_GT || BOK == BO_LT) && 11237 !(Type->isScalarType() || 11238 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 11239 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 11240 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 11241 if (!ASE && !OASE) { 11242 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11243 VarDecl::DeclarationOnly; 11244 S.Diag(D->getLocation(), 11245 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11246 << D; 11247 } 11248 continue; 11249 } 11250 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 11251 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 11252 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 11253 << getOpenMPClauseName(ClauseKind); 11254 if (!ASE && !OASE) { 11255 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11256 VarDecl::DeclarationOnly; 11257 S.Diag(D->getLocation(), 11258 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11259 << D; 11260 } 11261 continue; 11262 } 11263 } 11264 11265 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 11266 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 11267 D->hasAttrs() ? &D->getAttrs() : nullptr); 11268 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 11269 D->hasAttrs() ? &D->getAttrs() : nullptr); 11270 QualType PrivateTy = Type; 11271 11272 // Try if we can determine constant lengths for all array sections and avoid 11273 // the VLA. 11274 bool ConstantLengthOASE = false; 11275 if (OASE) { 11276 bool SingleElement; 11277 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 11278 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 11279 Context, OASE, SingleElement, ArraySizes); 11280 11281 // If we don't have a single element, we must emit a constant array type. 11282 if (ConstantLengthOASE && !SingleElement) { 11283 for (llvm::APSInt &Size : ArraySizes) 11284 PrivateTy = Context.getConstantArrayType( 11285 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 11286 } 11287 } 11288 11289 if ((OASE && !ConstantLengthOASE) || 11290 (!OASE && !ASE && 11291 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 11292 if (!Context.getTargetInfo().isVLASupported() && 11293 S.shouldDiagnoseTargetSupportFromOpenMP()) { 11294 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 11295 S.Diag(ELoc, diag::note_vla_unsupported); 11296 continue; 11297 } 11298 // For arrays/array sections only: 11299 // Create pseudo array type for private copy. The size for this array will 11300 // be generated during codegen. 11301 // For array subscripts or single variables Private Ty is the same as Type 11302 // (type of the variable or single array element). 11303 PrivateTy = Context.getVariableArrayType( 11304 Type, 11305 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 11306 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 11307 } else if (!ASE && !OASE && 11308 Context.getAsArrayType(D->getType().getNonReferenceType())) { 11309 PrivateTy = D->getType().getNonReferenceType(); 11310 } 11311 // Private copy. 11312 VarDecl *PrivateVD = 11313 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 11314 D->hasAttrs() ? &D->getAttrs() : nullptr, 11315 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11316 // Add initializer for private variable. 11317 Expr *Init = nullptr; 11318 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 11319 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 11320 if (DeclareReductionRef.isUsable()) { 11321 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 11322 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 11323 if (DRD->getInitializer()) { 11324 Init = DRDRef; 11325 RHSVD->setInit(DRDRef); 11326 RHSVD->setInitStyle(VarDecl::CallInit); 11327 } 11328 } else { 11329 switch (BOK) { 11330 case BO_Add: 11331 case BO_Xor: 11332 case BO_Or: 11333 case BO_LOr: 11334 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 11335 if (Type->isScalarType() || Type->isAnyComplexType()) 11336 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 11337 break; 11338 case BO_Mul: 11339 case BO_LAnd: 11340 if (Type->isScalarType() || Type->isAnyComplexType()) { 11341 // '*' and '&&' reduction ops - initializer is '1'. 11342 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 11343 } 11344 break; 11345 case BO_And: { 11346 // '&' reduction op - initializer is '~0'. 11347 QualType OrigType = Type; 11348 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 11349 Type = ComplexTy->getElementType(); 11350 if (Type->isRealFloatingType()) { 11351 llvm::APFloat InitValue = 11352 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 11353 /*isIEEE=*/true); 11354 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11355 Type, ELoc); 11356 } else if (Type->isScalarType()) { 11357 uint64_t Size = Context.getTypeSize(Type); 11358 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 11359 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 11360 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11361 } 11362 if (Init && OrigType->isAnyComplexType()) { 11363 // Init = 0xFFFF + 0xFFFFi; 11364 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 11365 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 11366 } 11367 Type = OrigType; 11368 break; 11369 } 11370 case BO_LT: 11371 case BO_GT: { 11372 // 'min' reduction op - initializer is 'Largest representable number in 11373 // the reduction list item type'. 11374 // 'max' reduction op - initializer is 'Least representable number in 11375 // the reduction list item type'. 11376 if (Type->isIntegerType() || Type->isPointerType()) { 11377 bool IsSigned = Type->hasSignedIntegerRepresentation(); 11378 uint64_t Size = Context.getTypeSize(Type); 11379 QualType IntTy = 11380 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 11381 llvm::APInt InitValue = 11382 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 11383 : llvm::APInt::getMinValue(Size) 11384 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 11385 : llvm::APInt::getMaxValue(Size); 11386 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11387 if (Type->isPointerType()) { 11388 // Cast to pointer type. 11389 ExprResult CastExpr = S.BuildCStyleCastExpr( 11390 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 11391 if (CastExpr.isInvalid()) 11392 continue; 11393 Init = CastExpr.get(); 11394 } 11395 } else if (Type->isRealFloatingType()) { 11396 llvm::APFloat InitValue = llvm::APFloat::getLargest( 11397 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 11398 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11399 Type, ELoc); 11400 } 11401 break; 11402 } 11403 case BO_PtrMemD: 11404 case BO_PtrMemI: 11405 case BO_MulAssign: 11406 case BO_Div: 11407 case BO_Rem: 11408 case BO_Sub: 11409 case BO_Shl: 11410 case BO_Shr: 11411 case BO_LE: 11412 case BO_GE: 11413 case BO_EQ: 11414 case BO_NE: 11415 case BO_Cmp: 11416 case BO_AndAssign: 11417 case BO_XorAssign: 11418 case BO_OrAssign: 11419 case BO_Assign: 11420 case BO_AddAssign: 11421 case BO_SubAssign: 11422 case BO_DivAssign: 11423 case BO_RemAssign: 11424 case BO_ShlAssign: 11425 case BO_ShrAssign: 11426 case BO_Comma: 11427 llvm_unreachable("Unexpected reduction operation"); 11428 } 11429 } 11430 if (Init && DeclareReductionRef.isUnset()) 11431 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 11432 else if (!Init) 11433 S.ActOnUninitializedDecl(RHSVD); 11434 if (RHSVD->isInvalidDecl()) 11435 continue; 11436 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 11437 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 11438 << Type << ReductionIdRange; 11439 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11440 VarDecl::DeclarationOnly; 11441 S.Diag(D->getLocation(), 11442 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11443 << D; 11444 continue; 11445 } 11446 // Store initializer for single element in private copy. Will be used during 11447 // codegen. 11448 PrivateVD->setInit(RHSVD->getInit()); 11449 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 11450 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 11451 ExprResult ReductionOp; 11452 if (DeclareReductionRef.isUsable()) { 11453 QualType RedTy = DeclareReductionRef.get()->getType(); 11454 QualType PtrRedTy = Context.getPointerType(RedTy); 11455 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 11456 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 11457 if (!BasePath.empty()) { 11458 LHS = S.DefaultLvalueConversion(LHS.get()); 11459 RHS = S.DefaultLvalueConversion(RHS.get()); 11460 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 11461 CK_UncheckedDerivedToBase, LHS.get(), 11462 &BasePath, LHS.get()->getValueKind()); 11463 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 11464 CK_UncheckedDerivedToBase, RHS.get(), 11465 &BasePath, RHS.get()->getValueKind()); 11466 } 11467 FunctionProtoType::ExtProtoInfo EPI; 11468 QualType Params[] = {PtrRedTy, PtrRedTy}; 11469 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 11470 auto *OVE = new (Context) OpaqueValueExpr( 11471 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 11472 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 11473 Expr *Args[] = {LHS.get(), RHS.get()}; 11474 ReductionOp = 11475 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 11476 } else { 11477 ReductionOp = S.BuildBinOp( 11478 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 11479 if (ReductionOp.isUsable()) { 11480 if (BOK != BO_LT && BOK != BO_GT) { 11481 ReductionOp = 11482 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11483 BO_Assign, LHSDRE, ReductionOp.get()); 11484 } else { 11485 auto *ConditionalOp = new (Context) 11486 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 11487 Type, VK_LValue, OK_Ordinary); 11488 ReductionOp = 11489 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11490 BO_Assign, LHSDRE, ConditionalOp); 11491 } 11492 if (ReductionOp.isUsable()) 11493 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 11494 /*DiscardedValue*/ false); 11495 } 11496 if (!ReductionOp.isUsable()) 11497 continue; 11498 } 11499 11500 // OpenMP [2.15.4.6, Restrictions, p.2] 11501 // A list item that appears in an in_reduction clause of a task construct 11502 // must appear in a task_reduction clause of a construct associated with a 11503 // taskgroup region that includes the participating task in its taskgroup 11504 // set. The construct associated with the innermost region that meets this 11505 // condition must specify the same reduction-identifier as the in_reduction 11506 // clause. 11507 if (ClauseKind == OMPC_in_reduction) { 11508 SourceRange ParentSR; 11509 BinaryOperatorKind ParentBOK; 11510 const Expr *ParentReductionOp; 11511 Expr *ParentBOKTD, *ParentReductionOpTD; 11512 DSAStackTy::DSAVarData ParentBOKDSA = 11513 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 11514 ParentBOKTD); 11515 DSAStackTy::DSAVarData ParentReductionOpDSA = 11516 Stack->getTopMostTaskgroupReductionData( 11517 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 11518 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 11519 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 11520 if (!IsParentBOK && !IsParentReductionOp) { 11521 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 11522 continue; 11523 } 11524 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 11525 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 11526 IsParentReductionOp) { 11527 bool EmitError = true; 11528 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 11529 llvm::FoldingSetNodeID RedId, ParentRedId; 11530 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 11531 DeclareReductionRef.get()->Profile(RedId, Context, 11532 /*Canonical=*/true); 11533 EmitError = RedId != ParentRedId; 11534 } 11535 if (EmitError) { 11536 S.Diag(ReductionId.getBeginLoc(), 11537 diag::err_omp_reduction_identifier_mismatch) 11538 << ReductionIdRange << RefExpr->getSourceRange(); 11539 S.Diag(ParentSR.getBegin(), 11540 diag::note_omp_previous_reduction_identifier) 11541 << ParentSR 11542 << (IsParentBOK ? ParentBOKDSA.RefExpr 11543 : ParentReductionOpDSA.RefExpr) 11544 ->getSourceRange(); 11545 continue; 11546 } 11547 } 11548 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 11549 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 11550 } 11551 11552 DeclRefExpr *Ref = nullptr; 11553 Expr *VarsExpr = RefExpr->IgnoreParens(); 11554 if (!VD && !S.CurContext->isDependentContext()) { 11555 if (ASE || OASE) { 11556 TransformExprToCaptures RebuildToCapture(S, D); 11557 VarsExpr = 11558 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 11559 Ref = RebuildToCapture.getCapturedExpr(); 11560 } else { 11561 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 11562 } 11563 if (!S.isOpenMPCapturedDecl(D)) { 11564 RD.ExprCaptures.emplace_back(Ref->getDecl()); 11565 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 11566 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 11567 if (!RefRes.isUsable()) 11568 continue; 11569 ExprResult PostUpdateRes = 11570 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 11571 RefRes.get()); 11572 if (!PostUpdateRes.isUsable()) 11573 continue; 11574 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 11575 Stack->getCurrentDirective() == OMPD_taskgroup) { 11576 S.Diag(RefExpr->getExprLoc(), 11577 diag::err_omp_reduction_non_addressable_expression) 11578 << RefExpr->getSourceRange(); 11579 continue; 11580 } 11581 RD.ExprPostUpdates.emplace_back( 11582 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 11583 } 11584 } 11585 } 11586 // All reduction items are still marked as reduction (to do not increase 11587 // code base size). 11588 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 11589 if (CurrDir == OMPD_taskgroup) { 11590 if (DeclareReductionRef.isUsable()) 11591 Stack->addTaskgroupReductionData(D, ReductionIdRange, 11592 DeclareReductionRef.get()); 11593 else 11594 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 11595 } 11596 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 11597 TaskgroupDescriptor); 11598 } 11599 return RD.Vars.empty(); 11600 } 11601 11602 OMPClause *Sema::ActOnOpenMPReductionClause( 11603 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11604 SourceLocation ColonLoc, SourceLocation EndLoc, 11605 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11606 ArrayRef<Expr *> UnresolvedReductions) { 11607 ReductionData RD(VarList.size()); 11608 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 11609 StartLoc, LParenLoc, ColonLoc, EndLoc, 11610 ReductionIdScopeSpec, ReductionId, 11611 UnresolvedReductions, RD)) 11612 return nullptr; 11613 11614 return OMPReductionClause::Create( 11615 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11616 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11617 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 11618 buildPreInits(Context, RD.ExprCaptures), 11619 buildPostUpdate(*this, RD.ExprPostUpdates)); 11620 } 11621 11622 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 11623 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11624 SourceLocation ColonLoc, SourceLocation EndLoc, 11625 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11626 ArrayRef<Expr *> UnresolvedReductions) { 11627 ReductionData RD(VarList.size()); 11628 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 11629 StartLoc, LParenLoc, ColonLoc, EndLoc, 11630 ReductionIdScopeSpec, ReductionId, 11631 UnresolvedReductions, RD)) 11632 return nullptr; 11633 11634 return OMPTaskReductionClause::Create( 11635 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11636 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11637 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 11638 buildPreInits(Context, RD.ExprCaptures), 11639 buildPostUpdate(*this, RD.ExprPostUpdates)); 11640 } 11641 11642 OMPClause *Sema::ActOnOpenMPInReductionClause( 11643 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11644 SourceLocation ColonLoc, SourceLocation EndLoc, 11645 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11646 ArrayRef<Expr *> UnresolvedReductions) { 11647 ReductionData RD(VarList.size()); 11648 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 11649 StartLoc, LParenLoc, ColonLoc, EndLoc, 11650 ReductionIdScopeSpec, ReductionId, 11651 UnresolvedReductions, RD)) 11652 return nullptr; 11653 11654 return OMPInReductionClause::Create( 11655 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11656 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11657 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 11658 buildPreInits(Context, RD.ExprCaptures), 11659 buildPostUpdate(*this, RD.ExprPostUpdates)); 11660 } 11661 11662 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 11663 SourceLocation LinLoc) { 11664 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 11665 LinKind == OMPC_LINEAR_unknown) { 11666 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 11667 return true; 11668 } 11669 return false; 11670 } 11671 11672 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 11673 OpenMPLinearClauseKind LinKind, 11674 QualType Type) { 11675 const auto *VD = dyn_cast_or_null<VarDecl>(D); 11676 // A variable must not have an incomplete type or a reference type. 11677 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 11678 return true; 11679 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 11680 !Type->isReferenceType()) { 11681 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 11682 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 11683 return true; 11684 } 11685 Type = Type.getNonReferenceType(); 11686 11687 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 11688 // A variable that is privatized must not have a const-qualified type 11689 // unless it is of class type with a mutable member. This restriction does 11690 // not apply to the firstprivate clause. 11691 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 11692 return true; 11693 11694 // A list item must be of integral or pointer type. 11695 Type = Type.getUnqualifiedType().getCanonicalType(); 11696 const auto *Ty = Type.getTypePtrOrNull(); 11697 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 11698 !Ty->isPointerType())) { 11699 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 11700 if (D) { 11701 bool IsDecl = 11702 !VD || 11703 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11704 Diag(D->getLocation(), 11705 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11706 << D; 11707 } 11708 return true; 11709 } 11710 return false; 11711 } 11712 11713 OMPClause *Sema::ActOnOpenMPLinearClause( 11714 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 11715 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 11716 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11717 SmallVector<Expr *, 8> Vars; 11718 SmallVector<Expr *, 8> Privates; 11719 SmallVector<Expr *, 8> Inits; 11720 SmallVector<Decl *, 4> ExprCaptures; 11721 SmallVector<Expr *, 4> ExprPostUpdates; 11722 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 11723 LinKind = OMPC_LINEAR_val; 11724 for (Expr *RefExpr : VarList) { 11725 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11726 SourceLocation ELoc; 11727 SourceRange ERange; 11728 Expr *SimpleRefExpr = RefExpr; 11729 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11730 if (Res.second) { 11731 // It will be analyzed later. 11732 Vars.push_back(RefExpr); 11733 Privates.push_back(nullptr); 11734 Inits.push_back(nullptr); 11735 } 11736 ValueDecl *D = Res.first; 11737 if (!D) 11738 continue; 11739 11740 QualType Type = D->getType(); 11741 auto *VD = dyn_cast<VarDecl>(D); 11742 11743 // OpenMP [2.14.3.7, linear clause] 11744 // A list-item cannot appear in more than one linear clause. 11745 // A list-item that appears in a linear clause cannot appear in any 11746 // other data-sharing attribute clause. 11747 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11748 if (DVar.RefExpr) { 11749 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 11750 << getOpenMPClauseName(OMPC_linear); 11751 reportOriginalDsa(*this, DSAStack, D, DVar); 11752 continue; 11753 } 11754 11755 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 11756 continue; 11757 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11758 11759 // Build private copy of original var. 11760 VarDecl *Private = 11761 buildVarDecl(*this, ELoc, Type, D->getName(), 11762 D->hasAttrs() ? &D->getAttrs() : nullptr, 11763 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11764 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 11765 // Build var to save initial value. 11766 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 11767 Expr *InitExpr; 11768 DeclRefExpr *Ref = nullptr; 11769 if (!VD && !CurContext->isDependentContext()) { 11770 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 11771 if (!isOpenMPCapturedDecl(D)) { 11772 ExprCaptures.push_back(Ref->getDecl()); 11773 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 11774 ExprResult RefRes = DefaultLvalueConversion(Ref); 11775 if (!RefRes.isUsable()) 11776 continue; 11777 ExprResult PostUpdateRes = 11778 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 11779 SimpleRefExpr, RefRes.get()); 11780 if (!PostUpdateRes.isUsable()) 11781 continue; 11782 ExprPostUpdates.push_back( 11783 IgnoredValueConversions(PostUpdateRes.get()).get()); 11784 } 11785 } 11786 } 11787 if (LinKind == OMPC_LINEAR_uval) 11788 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 11789 else 11790 InitExpr = VD ? SimpleRefExpr : Ref; 11791 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 11792 /*DirectInit=*/false); 11793 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 11794 11795 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 11796 Vars.push_back((VD || CurContext->isDependentContext()) 11797 ? RefExpr->IgnoreParens() 11798 : Ref); 11799 Privates.push_back(PrivateRef); 11800 Inits.push_back(InitRef); 11801 } 11802 11803 if (Vars.empty()) 11804 return nullptr; 11805 11806 Expr *StepExpr = Step; 11807 Expr *CalcStepExpr = nullptr; 11808 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 11809 !Step->isInstantiationDependent() && 11810 !Step->containsUnexpandedParameterPack()) { 11811 SourceLocation StepLoc = Step->getBeginLoc(); 11812 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 11813 if (Val.isInvalid()) 11814 return nullptr; 11815 StepExpr = Val.get(); 11816 11817 // Build var to save the step value. 11818 VarDecl *SaveVar = 11819 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 11820 ExprResult SaveRef = 11821 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 11822 ExprResult CalcStep = 11823 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 11824 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 11825 11826 // Warn about zero linear step (it would be probably better specified as 11827 // making corresponding variables 'const'). 11828 llvm::APSInt Result; 11829 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 11830 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 11831 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 11832 << (Vars.size() > 1); 11833 if (!IsConstant && CalcStep.isUsable()) { 11834 // Calculate the step beforehand instead of doing this on each iteration. 11835 // (This is not used if the number of iterations may be kfold-ed). 11836 CalcStepExpr = CalcStep.get(); 11837 } 11838 } 11839 11840 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 11841 ColonLoc, EndLoc, Vars, Privates, Inits, 11842 StepExpr, CalcStepExpr, 11843 buildPreInits(Context, ExprCaptures), 11844 buildPostUpdate(*this, ExprPostUpdates)); 11845 } 11846 11847 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 11848 Expr *NumIterations, Sema &SemaRef, 11849 Scope *S, DSAStackTy *Stack) { 11850 // Walk the vars and build update/final expressions for the CodeGen. 11851 SmallVector<Expr *, 8> Updates; 11852 SmallVector<Expr *, 8> Finals; 11853 Expr *Step = Clause.getStep(); 11854 Expr *CalcStep = Clause.getCalcStep(); 11855 // OpenMP [2.14.3.7, linear clause] 11856 // If linear-step is not specified it is assumed to be 1. 11857 if (!Step) 11858 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 11859 else if (CalcStep) 11860 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 11861 bool HasErrors = false; 11862 auto CurInit = Clause.inits().begin(); 11863 auto CurPrivate = Clause.privates().begin(); 11864 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 11865 for (Expr *RefExpr : Clause.varlists()) { 11866 SourceLocation ELoc; 11867 SourceRange ERange; 11868 Expr *SimpleRefExpr = RefExpr; 11869 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 11870 ValueDecl *D = Res.first; 11871 if (Res.second || !D) { 11872 Updates.push_back(nullptr); 11873 Finals.push_back(nullptr); 11874 HasErrors = true; 11875 continue; 11876 } 11877 auto &&Info = Stack->isLoopControlVariable(D); 11878 // OpenMP [2.15.11, distribute simd Construct] 11879 // A list item may not appear in a linear clause, unless it is the loop 11880 // iteration variable. 11881 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 11882 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 11883 SemaRef.Diag(ELoc, 11884 diag::err_omp_linear_distribute_var_non_loop_iteration); 11885 Updates.push_back(nullptr); 11886 Finals.push_back(nullptr); 11887 HasErrors = true; 11888 continue; 11889 } 11890 Expr *InitExpr = *CurInit; 11891 11892 // Build privatized reference to the current linear var. 11893 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 11894 Expr *CapturedRef; 11895 if (LinKind == OMPC_LINEAR_uval) 11896 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 11897 else 11898 CapturedRef = 11899 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 11900 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 11901 /*RefersToCapture=*/true); 11902 11903 // Build update: Var = InitExpr + IV * Step 11904 ExprResult Update; 11905 if (!Info.first) 11906 Update = 11907 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 11908 InitExpr, IV, Step, /* Subtract */ false); 11909 else 11910 Update = *CurPrivate; 11911 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 11912 /*DiscardedValue*/ false); 11913 11914 // Build final: Var = InitExpr + NumIterations * Step 11915 ExprResult Final; 11916 if (!Info.first) 11917 Final = 11918 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 11919 InitExpr, NumIterations, Step, /*Subtract=*/false); 11920 else 11921 Final = *CurPrivate; 11922 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 11923 /*DiscardedValue*/ false); 11924 11925 if (!Update.isUsable() || !Final.isUsable()) { 11926 Updates.push_back(nullptr); 11927 Finals.push_back(nullptr); 11928 HasErrors = true; 11929 } else { 11930 Updates.push_back(Update.get()); 11931 Finals.push_back(Final.get()); 11932 } 11933 ++CurInit; 11934 ++CurPrivate; 11935 } 11936 Clause.setUpdates(Updates); 11937 Clause.setFinals(Finals); 11938 return HasErrors; 11939 } 11940 11941 OMPClause *Sema::ActOnOpenMPAlignedClause( 11942 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 11943 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11944 SmallVector<Expr *, 8> Vars; 11945 for (Expr *RefExpr : VarList) { 11946 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11947 SourceLocation ELoc; 11948 SourceRange ERange; 11949 Expr *SimpleRefExpr = RefExpr; 11950 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11951 if (Res.second) { 11952 // It will be analyzed later. 11953 Vars.push_back(RefExpr); 11954 } 11955 ValueDecl *D = Res.first; 11956 if (!D) 11957 continue; 11958 11959 QualType QType = D->getType(); 11960 auto *VD = dyn_cast<VarDecl>(D); 11961 11962 // OpenMP [2.8.1, simd construct, Restrictions] 11963 // The type of list items appearing in the aligned clause must be 11964 // array, pointer, reference to array, or reference to pointer. 11965 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11966 const Type *Ty = QType.getTypePtrOrNull(); 11967 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 11968 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 11969 << QType << getLangOpts().CPlusPlus << ERange; 11970 bool IsDecl = 11971 !VD || 11972 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11973 Diag(D->getLocation(), 11974 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11975 << D; 11976 continue; 11977 } 11978 11979 // OpenMP [2.8.1, simd construct, Restrictions] 11980 // A list-item cannot appear in more than one aligned clause. 11981 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 11982 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 11983 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 11984 << getOpenMPClauseName(OMPC_aligned); 11985 continue; 11986 } 11987 11988 DeclRefExpr *Ref = nullptr; 11989 if (!VD && isOpenMPCapturedDecl(D)) 11990 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11991 Vars.push_back(DefaultFunctionArrayConversion( 11992 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 11993 .get()); 11994 } 11995 11996 // OpenMP [2.8.1, simd construct, Description] 11997 // The parameter of the aligned clause, alignment, must be a constant 11998 // positive integer expression. 11999 // If no optional parameter is specified, implementation-defined default 12000 // alignments for SIMD instructions on the target platforms are assumed. 12001 if (Alignment != nullptr) { 12002 ExprResult AlignResult = 12003 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 12004 if (AlignResult.isInvalid()) 12005 return nullptr; 12006 Alignment = AlignResult.get(); 12007 } 12008 if (Vars.empty()) 12009 return nullptr; 12010 12011 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 12012 EndLoc, Vars, Alignment); 12013 } 12014 12015 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 12016 SourceLocation StartLoc, 12017 SourceLocation LParenLoc, 12018 SourceLocation EndLoc) { 12019 SmallVector<Expr *, 8> Vars; 12020 SmallVector<Expr *, 8> SrcExprs; 12021 SmallVector<Expr *, 8> DstExprs; 12022 SmallVector<Expr *, 8> AssignmentOps; 12023 for (Expr *RefExpr : VarList) { 12024 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 12025 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 12026 // It will be analyzed later. 12027 Vars.push_back(RefExpr); 12028 SrcExprs.push_back(nullptr); 12029 DstExprs.push_back(nullptr); 12030 AssignmentOps.push_back(nullptr); 12031 continue; 12032 } 12033 12034 SourceLocation ELoc = RefExpr->getExprLoc(); 12035 // OpenMP [2.1, C/C++] 12036 // A list item is a variable name. 12037 // OpenMP [2.14.4.1, Restrictions, p.1] 12038 // A list item that appears in a copyin clause must be threadprivate. 12039 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 12040 if (!DE || !isa<VarDecl>(DE->getDecl())) { 12041 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 12042 << 0 << RefExpr->getSourceRange(); 12043 continue; 12044 } 12045 12046 Decl *D = DE->getDecl(); 12047 auto *VD = cast<VarDecl>(D); 12048 12049 QualType Type = VD->getType(); 12050 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 12051 // It will be analyzed later. 12052 Vars.push_back(DE); 12053 SrcExprs.push_back(nullptr); 12054 DstExprs.push_back(nullptr); 12055 AssignmentOps.push_back(nullptr); 12056 continue; 12057 } 12058 12059 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 12060 // A list item that appears in a copyin clause must be threadprivate. 12061 if (!DSAStack->isThreadPrivate(VD)) { 12062 Diag(ELoc, diag::err_omp_required_access) 12063 << getOpenMPClauseName(OMPC_copyin) 12064 << getOpenMPDirectiveName(OMPD_threadprivate); 12065 continue; 12066 } 12067 12068 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 12069 // A variable of class type (or array thereof) that appears in a 12070 // copyin clause requires an accessible, unambiguous copy assignment 12071 // operator for the class type. 12072 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 12073 VarDecl *SrcVD = 12074 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 12075 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12076 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 12077 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 12078 VarDecl *DstVD = 12079 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 12080 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12081 DeclRefExpr *PseudoDstExpr = 12082 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 12083 // For arrays generate assignment operation for single element and replace 12084 // it by the original array element in CodeGen. 12085 ExprResult AssignmentOp = 12086 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 12087 PseudoSrcExpr); 12088 if (AssignmentOp.isInvalid()) 12089 continue; 12090 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 12091 /*DiscardedValue*/ false); 12092 if (AssignmentOp.isInvalid()) 12093 continue; 12094 12095 DSAStack->addDSA(VD, DE, OMPC_copyin); 12096 Vars.push_back(DE); 12097 SrcExprs.push_back(PseudoSrcExpr); 12098 DstExprs.push_back(PseudoDstExpr); 12099 AssignmentOps.push_back(AssignmentOp.get()); 12100 } 12101 12102 if (Vars.empty()) 12103 return nullptr; 12104 12105 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 12106 SrcExprs, DstExprs, AssignmentOps); 12107 } 12108 12109 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 12110 SourceLocation StartLoc, 12111 SourceLocation LParenLoc, 12112 SourceLocation EndLoc) { 12113 SmallVector<Expr *, 8> Vars; 12114 SmallVector<Expr *, 8> SrcExprs; 12115 SmallVector<Expr *, 8> DstExprs; 12116 SmallVector<Expr *, 8> AssignmentOps; 12117 for (Expr *RefExpr : VarList) { 12118 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12119 SourceLocation ELoc; 12120 SourceRange ERange; 12121 Expr *SimpleRefExpr = RefExpr; 12122 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12123 if (Res.second) { 12124 // It will be analyzed later. 12125 Vars.push_back(RefExpr); 12126 SrcExprs.push_back(nullptr); 12127 DstExprs.push_back(nullptr); 12128 AssignmentOps.push_back(nullptr); 12129 } 12130 ValueDecl *D = Res.first; 12131 if (!D) 12132 continue; 12133 12134 QualType Type = D->getType(); 12135 auto *VD = dyn_cast<VarDecl>(D); 12136 12137 // OpenMP [2.14.4.2, Restrictions, p.2] 12138 // A list item that appears in a copyprivate clause may not appear in a 12139 // private or firstprivate clause on the single construct. 12140 if (!VD || !DSAStack->isThreadPrivate(VD)) { 12141 DSAStackTy::DSAVarData DVar = 12142 DSAStack->getTopDSA(D, /*FromParent=*/false); 12143 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 12144 DVar.RefExpr) { 12145 Diag(ELoc, diag::err_omp_wrong_dsa) 12146 << getOpenMPClauseName(DVar.CKind) 12147 << getOpenMPClauseName(OMPC_copyprivate); 12148 reportOriginalDsa(*this, DSAStack, D, DVar); 12149 continue; 12150 } 12151 12152 // OpenMP [2.11.4.2, Restrictions, p.1] 12153 // All list items that appear in a copyprivate clause must be either 12154 // threadprivate or private in the enclosing context. 12155 if (DVar.CKind == OMPC_unknown) { 12156 DVar = DSAStack->getImplicitDSA(D, false); 12157 if (DVar.CKind == OMPC_shared) { 12158 Diag(ELoc, diag::err_omp_required_access) 12159 << getOpenMPClauseName(OMPC_copyprivate) 12160 << "threadprivate or private in the enclosing context"; 12161 reportOriginalDsa(*this, DSAStack, D, DVar); 12162 continue; 12163 } 12164 } 12165 } 12166 12167 // Variably modified types are not supported. 12168 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 12169 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 12170 << getOpenMPClauseName(OMPC_copyprivate) << Type 12171 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 12172 bool IsDecl = 12173 !VD || 12174 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12175 Diag(D->getLocation(), 12176 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12177 << D; 12178 continue; 12179 } 12180 12181 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 12182 // A variable of class type (or array thereof) that appears in a 12183 // copyin clause requires an accessible, unambiguous copy assignment 12184 // operator for the class type. 12185 Type = Context.getBaseElementType(Type.getNonReferenceType()) 12186 .getUnqualifiedType(); 12187 VarDecl *SrcVD = 12188 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 12189 D->hasAttrs() ? &D->getAttrs() : nullptr); 12190 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 12191 VarDecl *DstVD = 12192 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 12193 D->hasAttrs() ? &D->getAttrs() : nullptr); 12194 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 12195 ExprResult AssignmentOp = BuildBinOp( 12196 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 12197 if (AssignmentOp.isInvalid()) 12198 continue; 12199 AssignmentOp = 12200 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 12201 if (AssignmentOp.isInvalid()) 12202 continue; 12203 12204 // No need to mark vars as copyprivate, they are already threadprivate or 12205 // implicitly private. 12206 assert(VD || isOpenMPCapturedDecl(D)); 12207 Vars.push_back( 12208 VD ? RefExpr->IgnoreParens() 12209 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 12210 SrcExprs.push_back(PseudoSrcExpr); 12211 DstExprs.push_back(PseudoDstExpr); 12212 AssignmentOps.push_back(AssignmentOp.get()); 12213 } 12214 12215 if (Vars.empty()) 12216 return nullptr; 12217 12218 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12219 Vars, SrcExprs, DstExprs, AssignmentOps); 12220 } 12221 12222 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 12223 SourceLocation StartLoc, 12224 SourceLocation LParenLoc, 12225 SourceLocation EndLoc) { 12226 if (VarList.empty()) 12227 return nullptr; 12228 12229 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 12230 } 12231 12232 OMPClause * 12233 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 12234 SourceLocation DepLoc, SourceLocation ColonLoc, 12235 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 12236 SourceLocation LParenLoc, SourceLocation EndLoc) { 12237 if (DSAStack->getCurrentDirective() == OMPD_ordered && 12238 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 12239 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 12240 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 12241 return nullptr; 12242 } 12243 if (DSAStack->getCurrentDirective() != OMPD_ordered && 12244 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 12245 DepKind == OMPC_DEPEND_sink)) { 12246 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 12247 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 12248 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 12249 /*Last=*/OMPC_DEPEND_unknown, Except) 12250 << getOpenMPClauseName(OMPC_depend); 12251 return nullptr; 12252 } 12253 SmallVector<Expr *, 8> Vars; 12254 DSAStackTy::OperatorOffsetTy OpsOffs; 12255 llvm::APSInt DepCounter(/*BitWidth=*/32); 12256 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 12257 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 12258 if (const Expr *OrderedCountExpr = 12259 DSAStack->getParentOrderedRegionParam().first) { 12260 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 12261 TotalDepCount.setIsUnsigned(/*Val=*/true); 12262 } 12263 } 12264 for (Expr *RefExpr : VarList) { 12265 assert(RefExpr && "NULL expr in OpenMP shared clause."); 12266 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 12267 // It will be analyzed later. 12268 Vars.push_back(RefExpr); 12269 continue; 12270 } 12271 12272 SourceLocation ELoc = RefExpr->getExprLoc(); 12273 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 12274 if (DepKind == OMPC_DEPEND_sink) { 12275 if (DSAStack->getParentOrderedRegionParam().first && 12276 DepCounter >= TotalDepCount) { 12277 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 12278 continue; 12279 } 12280 ++DepCounter; 12281 // OpenMP [2.13.9, Summary] 12282 // depend(dependence-type : vec), where dependence-type is: 12283 // 'sink' and where vec is the iteration vector, which has the form: 12284 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 12285 // where n is the value specified by the ordered clause in the loop 12286 // directive, xi denotes the loop iteration variable of the i-th nested 12287 // loop associated with the loop directive, and di is a constant 12288 // non-negative integer. 12289 if (CurContext->isDependentContext()) { 12290 // It will be analyzed later. 12291 Vars.push_back(RefExpr); 12292 continue; 12293 } 12294 SimpleExpr = SimpleExpr->IgnoreImplicit(); 12295 OverloadedOperatorKind OOK = OO_None; 12296 SourceLocation OOLoc; 12297 Expr *LHS = SimpleExpr; 12298 Expr *RHS = nullptr; 12299 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 12300 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 12301 OOLoc = BO->getOperatorLoc(); 12302 LHS = BO->getLHS()->IgnoreParenImpCasts(); 12303 RHS = BO->getRHS()->IgnoreParenImpCasts(); 12304 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 12305 OOK = OCE->getOperator(); 12306 OOLoc = OCE->getOperatorLoc(); 12307 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12308 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 12309 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 12310 OOK = MCE->getMethodDecl() 12311 ->getNameInfo() 12312 .getName() 12313 .getCXXOverloadedOperator(); 12314 OOLoc = MCE->getCallee()->getExprLoc(); 12315 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 12316 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12317 } 12318 SourceLocation ELoc; 12319 SourceRange ERange; 12320 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 12321 if (Res.second) { 12322 // It will be analyzed later. 12323 Vars.push_back(RefExpr); 12324 } 12325 ValueDecl *D = Res.first; 12326 if (!D) 12327 continue; 12328 12329 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 12330 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 12331 continue; 12332 } 12333 if (RHS) { 12334 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 12335 RHS, OMPC_depend, /*StrictlyPositive=*/false); 12336 if (RHSRes.isInvalid()) 12337 continue; 12338 } 12339 if (!CurContext->isDependentContext() && 12340 DSAStack->getParentOrderedRegionParam().first && 12341 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 12342 const ValueDecl *VD = 12343 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 12344 if (VD) 12345 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 12346 << 1 << VD; 12347 else 12348 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 12349 continue; 12350 } 12351 OpsOffs.emplace_back(RHS, OOK); 12352 } else { 12353 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 12354 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 12355 (ASE && 12356 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 12357 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 12358 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12359 << RefExpr->getSourceRange(); 12360 continue; 12361 } 12362 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 12363 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 12364 ExprResult Res = 12365 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 12366 getDiagnostics().setSuppressAllDiagnostics(Suppress); 12367 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 12368 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12369 << RefExpr->getSourceRange(); 12370 continue; 12371 } 12372 } 12373 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 12374 } 12375 12376 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 12377 TotalDepCount > VarList.size() && 12378 DSAStack->getParentOrderedRegionParam().first && 12379 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 12380 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 12381 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 12382 } 12383 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 12384 Vars.empty()) 12385 return nullptr; 12386 12387 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12388 DepKind, DepLoc, ColonLoc, Vars, 12389 TotalDepCount.getZExtValue()); 12390 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 12391 DSAStack->isParentOrderedRegion()) 12392 DSAStack->addDoacrossDependClause(C, OpsOffs); 12393 return C; 12394 } 12395 12396 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 12397 SourceLocation LParenLoc, 12398 SourceLocation EndLoc) { 12399 Expr *ValExpr = Device; 12400 Stmt *HelperValStmt = nullptr; 12401 12402 // OpenMP [2.9.1, Restrictions] 12403 // The device expression must evaluate to a non-negative integer value. 12404 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 12405 /*StrictlyPositive=*/false)) 12406 return nullptr; 12407 12408 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12409 OpenMPDirectiveKind CaptureRegion = 12410 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 12411 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12412 ValExpr = MakeFullExpr(ValExpr).get(); 12413 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12414 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12415 HelperValStmt = buildPreInits(Context, Captures); 12416 } 12417 12418 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 12419 StartLoc, LParenLoc, EndLoc); 12420 } 12421 12422 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 12423 DSAStackTy *Stack, QualType QTy, 12424 bool FullCheck = true) { 12425 NamedDecl *ND; 12426 if (QTy->isIncompleteType(&ND)) { 12427 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 12428 return false; 12429 } 12430 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 12431 !QTy.isTrivialType(SemaRef.Context)) 12432 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 12433 return true; 12434 } 12435 12436 /// Return true if it can be proven that the provided array expression 12437 /// (array section or array subscript) does NOT specify the whole size of the 12438 /// array whose base type is \a BaseQTy. 12439 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 12440 const Expr *E, 12441 QualType BaseQTy) { 12442 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 12443 12444 // If this is an array subscript, it refers to the whole size if the size of 12445 // the dimension is constant and equals 1. Also, an array section assumes the 12446 // format of an array subscript if no colon is used. 12447 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 12448 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 12449 return ATy->getSize().getSExtValue() != 1; 12450 // Size can't be evaluated statically. 12451 return false; 12452 } 12453 12454 assert(OASE && "Expecting array section if not an array subscript."); 12455 const Expr *LowerBound = OASE->getLowerBound(); 12456 const Expr *Length = OASE->getLength(); 12457 12458 // If there is a lower bound that does not evaluates to zero, we are not 12459 // covering the whole dimension. 12460 if (LowerBound) { 12461 Expr::EvalResult Result; 12462 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 12463 return false; // Can't get the integer value as a constant. 12464 12465 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 12466 if (ConstLowerBound.getSExtValue()) 12467 return true; 12468 } 12469 12470 // If we don't have a length we covering the whole dimension. 12471 if (!Length) 12472 return false; 12473 12474 // If the base is a pointer, we don't have a way to get the size of the 12475 // pointee. 12476 if (BaseQTy->isPointerType()) 12477 return false; 12478 12479 // We can only check if the length is the same as the size of the dimension 12480 // if we have a constant array. 12481 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 12482 if (!CATy) 12483 return false; 12484 12485 Expr::EvalResult Result; 12486 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 12487 return false; // Can't get the integer value as a constant. 12488 12489 llvm::APSInt ConstLength = Result.Val.getInt(); 12490 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 12491 } 12492 12493 // Return true if it can be proven that the provided array expression (array 12494 // section or array subscript) does NOT specify a single element of the array 12495 // whose base type is \a BaseQTy. 12496 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 12497 const Expr *E, 12498 QualType BaseQTy) { 12499 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 12500 12501 // An array subscript always refer to a single element. Also, an array section 12502 // assumes the format of an array subscript if no colon is used. 12503 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 12504 return false; 12505 12506 assert(OASE && "Expecting array section if not an array subscript."); 12507 const Expr *Length = OASE->getLength(); 12508 12509 // If we don't have a length we have to check if the array has unitary size 12510 // for this dimension. Also, we should always expect a length if the base type 12511 // is pointer. 12512 if (!Length) { 12513 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 12514 return ATy->getSize().getSExtValue() != 1; 12515 // We cannot assume anything. 12516 return false; 12517 } 12518 12519 // Check if the length evaluates to 1. 12520 Expr::EvalResult Result; 12521 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 12522 return false; // Can't get the integer value as a constant. 12523 12524 llvm::APSInt ConstLength = Result.Val.getInt(); 12525 return ConstLength.getSExtValue() != 1; 12526 } 12527 12528 // Return the expression of the base of the mappable expression or null if it 12529 // cannot be determined and do all the necessary checks to see if the expression 12530 // is valid as a standalone mappable expression. In the process, record all the 12531 // components of the expression. 12532 static const Expr *checkMapClauseExpressionBase( 12533 Sema &SemaRef, Expr *E, 12534 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 12535 OpenMPClauseKind CKind, bool NoDiagnose) { 12536 SourceLocation ELoc = E->getExprLoc(); 12537 SourceRange ERange = E->getSourceRange(); 12538 12539 // The base of elements of list in a map clause have to be either: 12540 // - a reference to variable or field. 12541 // - a member expression. 12542 // - an array expression. 12543 // 12544 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 12545 // reference to 'r'. 12546 // 12547 // If we have: 12548 // 12549 // struct SS { 12550 // Bla S; 12551 // foo() { 12552 // #pragma omp target map (S.Arr[:12]); 12553 // } 12554 // } 12555 // 12556 // We want to retrieve the member expression 'this->S'; 12557 12558 const Expr *RelevantExpr = nullptr; 12559 12560 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 12561 // If a list item is an array section, it must specify contiguous storage. 12562 // 12563 // For this restriction it is sufficient that we make sure only references 12564 // to variables or fields and array expressions, and that no array sections 12565 // exist except in the rightmost expression (unless they cover the whole 12566 // dimension of the array). E.g. these would be invalid: 12567 // 12568 // r.ArrS[3:5].Arr[6:7] 12569 // 12570 // r.ArrS[3:5].x 12571 // 12572 // but these would be valid: 12573 // r.ArrS[3].Arr[6:7] 12574 // 12575 // r.ArrS[3].x 12576 12577 bool AllowUnitySizeArraySection = true; 12578 bool AllowWholeSizeArraySection = true; 12579 12580 while (!RelevantExpr) { 12581 E = E->IgnoreParenImpCasts(); 12582 12583 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 12584 if (!isa<VarDecl>(CurE->getDecl())) 12585 return nullptr; 12586 12587 RelevantExpr = CurE; 12588 12589 // If we got a reference to a declaration, we should not expect any array 12590 // section before that. 12591 AllowUnitySizeArraySection = false; 12592 AllowWholeSizeArraySection = false; 12593 12594 // Record the component. 12595 CurComponents.emplace_back(CurE, CurE->getDecl()); 12596 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 12597 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 12598 12599 if (isa<CXXThisExpr>(BaseE)) 12600 // We found a base expression: this->Val. 12601 RelevantExpr = CurE; 12602 else 12603 E = BaseE; 12604 12605 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 12606 if (!NoDiagnose) { 12607 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 12608 << CurE->getSourceRange(); 12609 return nullptr; 12610 } 12611 if (RelevantExpr) 12612 return nullptr; 12613 continue; 12614 } 12615 12616 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 12617 12618 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 12619 // A bit-field cannot appear in a map clause. 12620 // 12621 if (FD->isBitField()) { 12622 if (!NoDiagnose) { 12623 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 12624 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 12625 return nullptr; 12626 } 12627 if (RelevantExpr) 12628 return nullptr; 12629 continue; 12630 } 12631 12632 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12633 // If the type of a list item is a reference to a type T then the type 12634 // will be considered to be T for all purposes of this clause. 12635 QualType CurType = BaseE->getType().getNonReferenceType(); 12636 12637 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 12638 // A list item cannot be a variable that is a member of a structure with 12639 // a union type. 12640 // 12641 if (CurType->isUnionType()) { 12642 if (!NoDiagnose) { 12643 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 12644 << CurE->getSourceRange(); 12645 return nullptr; 12646 } 12647 continue; 12648 } 12649 12650 // If we got a member expression, we should not expect any array section 12651 // before that: 12652 // 12653 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 12654 // If a list item is an element of a structure, only the rightmost symbol 12655 // of the variable reference can be an array section. 12656 // 12657 AllowUnitySizeArraySection = false; 12658 AllowWholeSizeArraySection = false; 12659 12660 // Record the component. 12661 CurComponents.emplace_back(CurE, FD); 12662 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 12663 E = CurE->getBase()->IgnoreParenImpCasts(); 12664 12665 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 12666 if (!NoDiagnose) { 12667 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 12668 << 0 << CurE->getSourceRange(); 12669 return nullptr; 12670 } 12671 continue; 12672 } 12673 12674 // If we got an array subscript that express the whole dimension we 12675 // can have any array expressions before. If it only expressing part of 12676 // the dimension, we can only have unitary-size array expressions. 12677 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 12678 E->getType())) 12679 AllowWholeSizeArraySection = false; 12680 12681 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 12682 Expr::EvalResult Result; 12683 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) { 12684 if (!Result.Val.getInt().isNullValue()) { 12685 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 12686 diag::err_omp_invalid_map_this_expr); 12687 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 12688 diag::note_omp_invalid_subscript_on_this_ptr_map); 12689 } 12690 } 12691 RelevantExpr = TE; 12692 } 12693 12694 // Record the component - we don't have any declaration associated. 12695 CurComponents.emplace_back(CurE, nullptr); 12696 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 12697 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 12698 E = CurE->getBase()->IgnoreParenImpCasts(); 12699 12700 QualType CurType = 12701 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12702 12703 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12704 // If the type of a list item is a reference to a type T then the type 12705 // will be considered to be T for all purposes of this clause. 12706 if (CurType->isReferenceType()) 12707 CurType = CurType->getPointeeType(); 12708 12709 bool IsPointer = CurType->isAnyPointerType(); 12710 12711 if (!IsPointer && !CurType->isArrayType()) { 12712 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 12713 << 0 << CurE->getSourceRange(); 12714 return nullptr; 12715 } 12716 12717 bool NotWhole = 12718 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 12719 bool NotUnity = 12720 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 12721 12722 if (AllowWholeSizeArraySection) { 12723 // Any array section is currently allowed. Allowing a whole size array 12724 // section implies allowing a unity array section as well. 12725 // 12726 // If this array section refers to the whole dimension we can still 12727 // accept other array sections before this one, except if the base is a 12728 // pointer. Otherwise, only unitary sections are accepted. 12729 if (NotWhole || IsPointer) 12730 AllowWholeSizeArraySection = false; 12731 } else if (AllowUnitySizeArraySection && NotUnity) { 12732 // A unity or whole array section is not allowed and that is not 12733 // compatible with the properties of the current array section. 12734 SemaRef.Diag( 12735 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 12736 << CurE->getSourceRange(); 12737 return nullptr; 12738 } 12739 12740 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 12741 Expr::EvalResult ResultR; 12742 Expr::EvalResult ResultL; 12743 if (CurE->getLength()->EvaluateAsInt(ResultR, 12744 SemaRef.getASTContext())) { 12745 if (!ResultR.Val.getInt().isOneValue()) { 12746 SemaRef.Diag(CurE->getLength()->getExprLoc(), 12747 diag::err_omp_invalid_map_this_expr); 12748 SemaRef.Diag(CurE->getLength()->getExprLoc(), 12749 diag::note_omp_invalid_length_on_this_ptr_mapping); 12750 } 12751 } 12752 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt( 12753 ResultL, SemaRef.getASTContext())) { 12754 if (!ResultL.Val.getInt().isNullValue()) { 12755 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 12756 diag::err_omp_invalid_map_this_expr); 12757 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 12758 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 12759 } 12760 } 12761 RelevantExpr = TE; 12762 } 12763 12764 // Record the component - we don't have any declaration associated. 12765 CurComponents.emplace_back(CurE, nullptr); 12766 } else { 12767 if (!NoDiagnose) { 12768 // If nothing else worked, this is not a valid map clause expression. 12769 SemaRef.Diag( 12770 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 12771 << ERange; 12772 } 12773 return nullptr; 12774 } 12775 } 12776 12777 return RelevantExpr; 12778 } 12779 12780 // Return true if expression E associated with value VD has conflicts with other 12781 // map information. 12782 static bool checkMapConflicts( 12783 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 12784 bool CurrentRegionOnly, 12785 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 12786 OpenMPClauseKind CKind) { 12787 assert(VD && E); 12788 SourceLocation ELoc = E->getExprLoc(); 12789 SourceRange ERange = E->getSourceRange(); 12790 12791 // In order to easily check the conflicts we need to match each component of 12792 // the expression under test with the components of the expressions that are 12793 // already in the stack. 12794 12795 assert(!CurComponents.empty() && "Map clause expression with no components!"); 12796 assert(CurComponents.back().getAssociatedDeclaration() == VD && 12797 "Map clause expression with unexpected base!"); 12798 12799 // Variables to help detecting enclosing problems in data environment nests. 12800 bool IsEnclosedByDataEnvironmentExpr = false; 12801 const Expr *EnclosingExpr = nullptr; 12802 12803 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 12804 VD, CurrentRegionOnly, 12805 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 12806 ERange, CKind, &EnclosingExpr, 12807 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 12808 StackComponents, 12809 OpenMPClauseKind) { 12810 assert(!StackComponents.empty() && 12811 "Map clause expression with no components!"); 12812 assert(StackComponents.back().getAssociatedDeclaration() == VD && 12813 "Map clause expression with unexpected base!"); 12814 (void)VD; 12815 12816 // The whole expression in the stack. 12817 const Expr *RE = StackComponents.front().getAssociatedExpression(); 12818 12819 // Expressions must start from the same base. Here we detect at which 12820 // point both expressions diverge from each other and see if we can 12821 // detect if the memory referred to both expressions is contiguous and 12822 // do not overlap. 12823 auto CI = CurComponents.rbegin(); 12824 auto CE = CurComponents.rend(); 12825 auto SI = StackComponents.rbegin(); 12826 auto SE = StackComponents.rend(); 12827 for (; CI != CE && SI != SE; ++CI, ++SI) { 12828 12829 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 12830 // At most one list item can be an array item derived from a given 12831 // variable in map clauses of the same construct. 12832 if (CurrentRegionOnly && 12833 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 12834 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 12835 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 12836 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 12837 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 12838 diag::err_omp_multiple_array_items_in_map_clause) 12839 << CI->getAssociatedExpression()->getSourceRange(); 12840 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 12841 diag::note_used_here) 12842 << SI->getAssociatedExpression()->getSourceRange(); 12843 return true; 12844 } 12845 12846 // Do both expressions have the same kind? 12847 if (CI->getAssociatedExpression()->getStmtClass() != 12848 SI->getAssociatedExpression()->getStmtClass()) 12849 break; 12850 12851 // Are we dealing with different variables/fields? 12852 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 12853 break; 12854 } 12855 // Check if the extra components of the expressions in the enclosing 12856 // data environment are redundant for the current base declaration. 12857 // If they are, the maps completely overlap, which is legal. 12858 for (; SI != SE; ++SI) { 12859 QualType Type; 12860 if (const auto *ASE = 12861 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 12862 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 12863 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 12864 SI->getAssociatedExpression())) { 12865 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 12866 Type = 12867 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12868 } 12869 if (Type.isNull() || Type->isAnyPointerType() || 12870 checkArrayExpressionDoesNotReferToWholeSize( 12871 SemaRef, SI->getAssociatedExpression(), Type)) 12872 break; 12873 } 12874 12875 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12876 // List items of map clauses in the same construct must not share 12877 // original storage. 12878 // 12879 // If the expressions are exactly the same or one is a subset of the 12880 // other, it means they are sharing storage. 12881 if (CI == CE && SI == SE) { 12882 if (CurrentRegionOnly) { 12883 if (CKind == OMPC_map) { 12884 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12885 } else { 12886 assert(CKind == OMPC_to || CKind == OMPC_from); 12887 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12888 << ERange; 12889 } 12890 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12891 << RE->getSourceRange(); 12892 return true; 12893 } 12894 // If we find the same expression in the enclosing data environment, 12895 // that is legal. 12896 IsEnclosedByDataEnvironmentExpr = true; 12897 return false; 12898 } 12899 12900 QualType DerivedType = 12901 std::prev(CI)->getAssociatedDeclaration()->getType(); 12902 SourceLocation DerivedLoc = 12903 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 12904 12905 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12906 // If the type of a list item is a reference to a type T then the type 12907 // will be considered to be T for all purposes of this clause. 12908 DerivedType = DerivedType.getNonReferenceType(); 12909 12910 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 12911 // A variable for which the type is pointer and an array section 12912 // derived from that variable must not appear as list items of map 12913 // clauses of the same construct. 12914 // 12915 // Also, cover one of the cases in: 12916 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12917 // If any part of the original storage of a list item has corresponding 12918 // storage in the device data environment, all of the original storage 12919 // must have corresponding storage in the device data environment. 12920 // 12921 if (DerivedType->isAnyPointerType()) { 12922 if (CI == CE || SI == SE) { 12923 SemaRef.Diag( 12924 DerivedLoc, 12925 diag::err_omp_pointer_mapped_along_with_derived_section) 12926 << DerivedLoc; 12927 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12928 << RE->getSourceRange(); 12929 return true; 12930 } 12931 if (CI->getAssociatedExpression()->getStmtClass() != 12932 SI->getAssociatedExpression()->getStmtClass() || 12933 CI->getAssociatedDeclaration()->getCanonicalDecl() == 12934 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 12935 assert(CI != CE && SI != SE); 12936 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 12937 << DerivedLoc; 12938 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12939 << RE->getSourceRange(); 12940 return true; 12941 } 12942 } 12943 12944 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12945 // List items of map clauses in the same construct must not share 12946 // original storage. 12947 // 12948 // An expression is a subset of the other. 12949 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 12950 if (CKind == OMPC_map) { 12951 if (CI != CE || SI != SE) { 12952 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 12953 // a pointer. 12954 auto Begin = 12955 CI != CE ? CurComponents.begin() : StackComponents.begin(); 12956 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 12957 auto It = Begin; 12958 while (It != End && !It->getAssociatedDeclaration()) 12959 std::advance(It, 1); 12960 assert(It != End && 12961 "Expected at least one component with the declaration."); 12962 if (It != Begin && It->getAssociatedDeclaration() 12963 ->getType() 12964 .getCanonicalType() 12965 ->isAnyPointerType()) { 12966 IsEnclosedByDataEnvironmentExpr = false; 12967 EnclosingExpr = nullptr; 12968 return false; 12969 } 12970 } 12971 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12972 } else { 12973 assert(CKind == OMPC_to || CKind == OMPC_from); 12974 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12975 << ERange; 12976 } 12977 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12978 << RE->getSourceRange(); 12979 return true; 12980 } 12981 12982 // The current expression uses the same base as other expression in the 12983 // data environment but does not contain it completely. 12984 if (!CurrentRegionOnly && SI != SE) 12985 EnclosingExpr = RE; 12986 12987 // The current expression is a subset of the expression in the data 12988 // environment. 12989 IsEnclosedByDataEnvironmentExpr |= 12990 (!CurrentRegionOnly && CI != CE && SI == SE); 12991 12992 return false; 12993 }); 12994 12995 if (CurrentRegionOnly) 12996 return FoundError; 12997 12998 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12999 // If any part of the original storage of a list item has corresponding 13000 // storage in the device data environment, all of the original storage must 13001 // have corresponding storage in the device data environment. 13002 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 13003 // If a list item is an element of a structure, and a different element of 13004 // the structure has a corresponding list item in the device data environment 13005 // prior to a task encountering the construct associated with the map clause, 13006 // then the list item must also have a corresponding list item in the device 13007 // data environment prior to the task encountering the construct. 13008 // 13009 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 13010 SemaRef.Diag(ELoc, 13011 diag::err_omp_original_storage_is_shared_and_does_not_contain) 13012 << ERange; 13013 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 13014 << EnclosingExpr->getSourceRange(); 13015 return true; 13016 } 13017 13018 return FoundError; 13019 } 13020 13021 // Look up the user-defined mapper given the mapper name and mapped type, and 13022 // build a reference to it. 13023 ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 13024 CXXScopeSpec &MapperIdScopeSpec, 13025 const DeclarationNameInfo &MapperId, 13026 QualType Type, Expr *UnresolvedMapper) { 13027 if (MapperIdScopeSpec.isInvalid()) 13028 return ExprError(); 13029 // Find all user-defined mappers with the given MapperId. 13030 SmallVector<UnresolvedSet<8>, 4> Lookups; 13031 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 13032 Lookup.suppressDiagnostics(); 13033 if (S) { 13034 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 13035 NamedDecl *D = Lookup.getRepresentativeDecl(); 13036 while (S && !S->isDeclScope(D)) 13037 S = S->getParent(); 13038 if (S) 13039 S = S->getParent(); 13040 Lookups.emplace_back(); 13041 Lookups.back().append(Lookup.begin(), Lookup.end()); 13042 Lookup.clear(); 13043 } 13044 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 13045 // Extract the user-defined mappers with the given MapperId. 13046 Lookups.push_back(UnresolvedSet<8>()); 13047 for (NamedDecl *D : ULE->decls()) { 13048 auto *DMD = cast<OMPDeclareMapperDecl>(D); 13049 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 13050 Lookups.back().addDecl(DMD); 13051 } 13052 } 13053 // Defer the lookup for dependent types. The results will be passed through 13054 // UnresolvedMapper on instantiation. 13055 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 13056 Type->isInstantiationDependentType() || 13057 Type->containsUnexpandedParameterPack() || 13058 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 13059 return !D->isInvalidDecl() && 13060 (D->getType()->isDependentType() || 13061 D->getType()->isInstantiationDependentType() || 13062 D->getType()->containsUnexpandedParameterPack()); 13063 })) { 13064 UnresolvedSet<8> URS; 13065 for (const UnresolvedSet<8> &Set : Lookups) { 13066 if (Set.empty()) 13067 continue; 13068 URS.append(Set.begin(), Set.end()); 13069 } 13070 return UnresolvedLookupExpr::Create( 13071 SemaRef.Context, /*NamingClass=*/nullptr, 13072 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 13073 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 13074 } 13075 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 13076 // The type must be of struct, union or class type in C and C++ 13077 if (!Type->isStructureOrClassType() && !Type->isUnionType()) 13078 return ExprEmpty(); 13079 SourceLocation Loc = MapperId.getLoc(); 13080 // Perform argument dependent lookup. 13081 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 13082 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 13083 // Return the first user-defined mapper with the desired type. 13084 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13085 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 13086 if (!D->isInvalidDecl() && 13087 SemaRef.Context.hasSameType(D->getType(), Type)) 13088 return D; 13089 return nullptr; 13090 })) 13091 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13092 // Find the first user-defined mapper with a type derived from the desired 13093 // type. 13094 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13095 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 13096 if (!D->isInvalidDecl() && 13097 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 13098 !Type.isMoreQualifiedThan(D->getType())) 13099 return D; 13100 return nullptr; 13101 })) { 13102 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 13103 /*DetectVirtual=*/false); 13104 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 13105 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 13106 VD->getType().getUnqualifiedType()))) { 13107 if (SemaRef.CheckBaseClassAccess( 13108 Loc, VD->getType(), Type, Paths.front(), 13109 /*DiagID=*/0) != Sema::AR_inaccessible) { 13110 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13111 } 13112 } 13113 } 13114 } 13115 // Report error if a mapper is specified, but cannot be found. 13116 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 13117 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 13118 << Type << MapperId.getName(); 13119 return ExprError(); 13120 } 13121 return ExprEmpty(); 13122 } 13123 13124 namespace { 13125 // Utility struct that gathers all the related lists associated with a mappable 13126 // expression. 13127 struct MappableVarListInfo { 13128 // The list of expressions. 13129 ArrayRef<Expr *> VarList; 13130 // The list of processed expressions. 13131 SmallVector<Expr *, 16> ProcessedVarList; 13132 // The mappble components for each expression. 13133 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 13134 // The base declaration of the variable. 13135 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 13136 // The reference to the user-defined mapper associated with every expression. 13137 SmallVector<Expr *, 16> UDMapperList; 13138 13139 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 13140 // We have a list of components and base declarations for each entry in the 13141 // variable list. 13142 VarComponents.reserve(VarList.size()); 13143 VarBaseDeclarations.reserve(VarList.size()); 13144 } 13145 }; 13146 } 13147 13148 // Check the validity of the provided variable list for the provided clause kind 13149 // \a CKind. In the check process the valid expressions, mappable expression 13150 // components, variables, and user-defined mappers are extracted and used to 13151 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 13152 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 13153 // and \a MapperId are expected to be valid if the clause kind is 'map'. 13154 static void checkMappableExpressionList( 13155 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 13156 MappableVarListInfo &MVLI, SourceLocation StartLoc, 13157 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 13158 ArrayRef<Expr *> UnresolvedMappers, 13159 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 13160 bool IsMapTypeImplicit = false) { 13161 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 13162 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 13163 "Unexpected clause kind with mappable expressions!"); 13164 13165 // If the identifier of user-defined mapper is not specified, it is "default". 13166 // We do not change the actual name in this clause to distinguish whether a 13167 // mapper is specified explicitly, i.e., it is not explicitly specified when 13168 // MapperId.getName() is empty. 13169 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 13170 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 13171 MapperId.setName(DeclNames.getIdentifier( 13172 &SemaRef.getASTContext().Idents.get("default"))); 13173 } 13174 13175 // Iterators to find the current unresolved mapper expression. 13176 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 13177 bool UpdateUMIt = false; 13178 Expr *UnresolvedMapper = nullptr; 13179 13180 // Keep track of the mappable components and base declarations in this clause. 13181 // Each entry in the list is going to have a list of components associated. We 13182 // record each set of the components so that we can build the clause later on. 13183 // In the end we should have the same amount of declarations and component 13184 // lists. 13185 13186 for (Expr *RE : MVLI.VarList) { 13187 assert(RE && "Null expr in omp to/from/map clause"); 13188 SourceLocation ELoc = RE->getExprLoc(); 13189 13190 // Find the current unresolved mapper expression. 13191 if (UpdateUMIt && UMIt != UMEnd) { 13192 UMIt++; 13193 assert( 13194 UMIt != UMEnd && 13195 "Expect the size of UnresolvedMappers to match with that of VarList"); 13196 } 13197 UpdateUMIt = true; 13198 if (UMIt != UMEnd) 13199 UnresolvedMapper = *UMIt; 13200 13201 const Expr *VE = RE->IgnoreParenLValueCasts(); 13202 13203 if (VE->isValueDependent() || VE->isTypeDependent() || 13204 VE->isInstantiationDependent() || 13205 VE->containsUnexpandedParameterPack()) { 13206 // Try to find the associated user-defined mapper. 13207 ExprResult ER = buildUserDefinedMapperRef( 13208 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13209 VE->getType().getCanonicalType(), UnresolvedMapper); 13210 if (ER.isInvalid()) 13211 continue; 13212 MVLI.UDMapperList.push_back(ER.get()); 13213 // We can only analyze this information once the missing information is 13214 // resolved. 13215 MVLI.ProcessedVarList.push_back(RE); 13216 continue; 13217 } 13218 13219 Expr *SimpleExpr = RE->IgnoreParenCasts(); 13220 13221 if (!RE->IgnoreParenImpCasts()->isLValue()) { 13222 SemaRef.Diag(ELoc, 13223 diag::err_omp_expected_named_var_member_or_array_expression) 13224 << RE->getSourceRange(); 13225 continue; 13226 } 13227 13228 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 13229 ValueDecl *CurDeclaration = nullptr; 13230 13231 // Obtain the array or member expression bases if required. Also, fill the 13232 // components array with all the components identified in the process. 13233 const Expr *BE = checkMapClauseExpressionBase( 13234 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 13235 if (!BE) 13236 continue; 13237 13238 assert(!CurComponents.empty() && 13239 "Invalid mappable expression information."); 13240 13241 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 13242 // Add store "this" pointer to class in DSAStackTy for future checking 13243 DSAS->addMappedClassesQualTypes(TE->getType()); 13244 // Try to find the associated user-defined mapper. 13245 ExprResult ER = buildUserDefinedMapperRef( 13246 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13247 VE->getType().getCanonicalType(), UnresolvedMapper); 13248 if (ER.isInvalid()) 13249 continue; 13250 MVLI.UDMapperList.push_back(ER.get()); 13251 // Skip restriction checking for variable or field declarations 13252 MVLI.ProcessedVarList.push_back(RE); 13253 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13254 MVLI.VarComponents.back().append(CurComponents.begin(), 13255 CurComponents.end()); 13256 MVLI.VarBaseDeclarations.push_back(nullptr); 13257 continue; 13258 } 13259 13260 // For the following checks, we rely on the base declaration which is 13261 // expected to be associated with the last component. The declaration is 13262 // expected to be a variable or a field (if 'this' is being mapped). 13263 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 13264 assert(CurDeclaration && "Null decl on map clause."); 13265 assert( 13266 CurDeclaration->isCanonicalDecl() && 13267 "Expecting components to have associated only canonical declarations."); 13268 13269 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 13270 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 13271 13272 assert((VD || FD) && "Only variables or fields are expected here!"); 13273 (void)FD; 13274 13275 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 13276 // threadprivate variables cannot appear in a map clause. 13277 // OpenMP 4.5 [2.10.5, target update Construct] 13278 // threadprivate variables cannot appear in a from clause. 13279 if (VD && DSAS->isThreadPrivate(VD)) { 13280 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 13281 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 13282 << getOpenMPClauseName(CKind); 13283 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 13284 continue; 13285 } 13286 13287 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 13288 // A list item cannot appear in both a map clause and a data-sharing 13289 // attribute clause on the same construct. 13290 13291 // Check conflicts with other map clause expressions. We check the conflicts 13292 // with the current construct separately from the enclosing data 13293 // environment, because the restrictions are different. We only have to 13294 // check conflicts across regions for the map clauses. 13295 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 13296 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 13297 break; 13298 if (CKind == OMPC_map && 13299 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 13300 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 13301 break; 13302 13303 // OpenMP 4.5 [2.10.5, target update Construct] 13304 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13305 // If the type of a list item is a reference to a type T then the type will 13306 // be considered to be T for all purposes of this clause. 13307 auto I = llvm::find_if( 13308 CurComponents, 13309 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 13310 return MC.getAssociatedDeclaration(); 13311 }); 13312 assert(I != CurComponents.end() && "Null decl on map clause."); 13313 QualType Type = 13314 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 13315 13316 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 13317 // A list item in a to or from clause must have a mappable type. 13318 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 13319 // A list item must have a mappable type. 13320 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 13321 DSAS, Type)) 13322 continue; 13323 13324 if (CKind == OMPC_map) { 13325 // target enter data 13326 // OpenMP [2.10.2, Restrictions, p. 99] 13327 // A map-type must be specified in all map clauses and must be either 13328 // to or alloc. 13329 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 13330 if (DKind == OMPD_target_enter_data && 13331 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 13332 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 13333 << (IsMapTypeImplicit ? 1 : 0) 13334 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 13335 << getOpenMPDirectiveName(DKind); 13336 continue; 13337 } 13338 13339 // target exit_data 13340 // OpenMP [2.10.3, Restrictions, p. 102] 13341 // A map-type must be specified in all map clauses and must be either 13342 // from, release, or delete. 13343 if (DKind == OMPD_target_exit_data && 13344 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 13345 MapType == OMPC_MAP_delete)) { 13346 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 13347 << (IsMapTypeImplicit ? 1 : 0) 13348 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 13349 << getOpenMPDirectiveName(DKind); 13350 continue; 13351 } 13352 13353 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13354 // A list item cannot appear in both a map clause and a data-sharing 13355 // attribute clause on the same construct 13356 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 13357 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 13358 if (isOpenMPPrivate(DVar.CKind)) { 13359 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13360 << getOpenMPClauseName(DVar.CKind) 13361 << getOpenMPClauseName(OMPC_map) 13362 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 13363 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 13364 continue; 13365 } 13366 } 13367 } 13368 13369 // Try to find the associated user-defined mapper. 13370 ExprResult ER = buildUserDefinedMapperRef( 13371 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13372 Type.getCanonicalType(), UnresolvedMapper); 13373 if (ER.isInvalid()) 13374 continue; 13375 MVLI.UDMapperList.push_back(ER.get()); 13376 13377 // Save the current expression. 13378 MVLI.ProcessedVarList.push_back(RE); 13379 13380 // Store the components in the stack so that they can be used to check 13381 // against other clauses later on. 13382 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 13383 /*WhereFoundClauseKind=*/OMPC_map); 13384 13385 // Save the components and declaration to create the clause. For purposes of 13386 // the clause creation, any component list that has has base 'this' uses 13387 // null as base declaration. 13388 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13389 MVLI.VarComponents.back().append(CurComponents.begin(), 13390 CurComponents.end()); 13391 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 13392 : CurDeclaration); 13393 } 13394 } 13395 13396 OMPClause *Sema::ActOnOpenMPMapClause( 13397 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 13398 ArrayRef<SourceLocation> MapTypeModifiersLoc, 13399 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 13400 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 13401 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 13402 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 13403 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown, 13404 OMPC_MAP_MODIFIER_unknown, 13405 OMPC_MAP_MODIFIER_unknown}; 13406 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers]; 13407 13408 // Process map-type-modifiers, flag errors for duplicate modifiers. 13409 unsigned Count = 0; 13410 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 13411 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 13412 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 13413 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 13414 continue; 13415 } 13416 assert(Count < OMPMapClause::NumberOfModifiers && 13417 "Modifiers exceed the allowed number of map type modifiers"); 13418 Modifiers[Count] = MapTypeModifiers[I]; 13419 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 13420 ++Count; 13421 } 13422 13423 MappableVarListInfo MVLI(VarList); 13424 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 13425 MapperIdScopeSpec, MapperId, UnresolvedMappers, 13426 MapType, IsMapTypeImplicit); 13427 13428 // We need to produce a map clause even if we don't have variables so that 13429 // other diagnostics related with non-existing map clauses are accurate. 13430 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 13431 MVLI.VarBaseDeclarations, MVLI.VarComponents, 13432 MVLI.UDMapperList, Modifiers, ModifiersLoc, 13433 MapperIdScopeSpec.getWithLocInContext(Context), 13434 MapperId, MapType, IsMapTypeImplicit, MapLoc); 13435 } 13436 13437 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 13438 TypeResult ParsedType) { 13439 assert(ParsedType.isUsable()); 13440 13441 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 13442 if (ReductionType.isNull()) 13443 return QualType(); 13444 13445 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 13446 // A type name in a declare reduction directive cannot be a function type, an 13447 // array type, a reference type, or a type qualified with const, volatile or 13448 // restrict. 13449 if (ReductionType.hasQualifiers()) { 13450 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 13451 return QualType(); 13452 } 13453 13454 if (ReductionType->isFunctionType()) { 13455 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 13456 return QualType(); 13457 } 13458 if (ReductionType->isReferenceType()) { 13459 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 13460 return QualType(); 13461 } 13462 if (ReductionType->isArrayType()) { 13463 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 13464 return QualType(); 13465 } 13466 return ReductionType; 13467 } 13468 13469 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 13470 Scope *S, DeclContext *DC, DeclarationName Name, 13471 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 13472 AccessSpecifier AS, Decl *PrevDeclInScope) { 13473 SmallVector<Decl *, 8> Decls; 13474 Decls.reserve(ReductionTypes.size()); 13475 13476 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 13477 forRedeclarationInCurContext()); 13478 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 13479 // A reduction-identifier may not be re-declared in the current scope for the 13480 // same type or for a type that is compatible according to the base language 13481 // rules. 13482 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 13483 OMPDeclareReductionDecl *PrevDRD = nullptr; 13484 bool InCompoundScope = true; 13485 if (S != nullptr) { 13486 // Find previous declaration with the same name not referenced in other 13487 // declarations. 13488 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 13489 InCompoundScope = 13490 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 13491 LookupName(Lookup, S); 13492 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 13493 /*AllowInlineNamespace=*/false); 13494 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 13495 LookupResult::Filter Filter = Lookup.makeFilter(); 13496 while (Filter.hasNext()) { 13497 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 13498 if (InCompoundScope) { 13499 auto I = UsedAsPrevious.find(PrevDecl); 13500 if (I == UsedAsPrevious.end()) 13501 UsedAsPrevious[PrevDecl] = false; 13502 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 13503 UsedAsPrevious[D] = true; 13504 } 13505 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 13506 PrevDecl->getLocation(); 13507 } 13508 Filter.done(); 13509 if (InCompoundScope) { 13510 for (const auto &PrevData : UsedAsPrevious) { 13511 if (!PrevData.second) { 13512 PrevDRD = PrevData.first; 13513 break; 13514 } 13515 } 13516 } 13517 } else if (PrevDeclInScope != nullptr) { 13518 auto *PrevDRDInScope = PrevDRD = 13519 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 13520 do { 13521 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 13522 PrevDRDInScope->getLocation(); 13523 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 13524 } while (PrevDRDInScope != nullptr); 13525 } 13526 for (const auto &TyData : ReductionTypes) { 13527 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 13528 bool Invalid = false; 13529 if (I != PreviousRedeclTypes.end()) { 13530 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 13531 << TyData.first; 13532 Diag(I->second, diag::note_previous_definition); 13533 Invalid = true; 13534 } 13535 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 13536 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 13537 Name, TyData.first, PrevDRD); 13538 DC->addDecl(DRD); 13539 DRD->setAccess(AS); 13540 Decls.push_back(DRD); 13541 if (Invalid) 13542 DRD->setInvalidDecl(); 13543 else 13544 PrevDRD = DRD; 13545 } 13546 13547 return DeclGroupPtrTy::make( 13548 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 13549 } 13550 13551 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 13552 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13553 13554 // Enter new function scope. 13555 PushFunctionScope(); 13556 setFunctionHasBranchProtectedScope(); 13557 getCurFunction()->setHasOMPDeclareReductionCombiner(); 13558 13559 if (S != nullptr) 13560 PushDeclContext(S, DRD); 13561 else 13562 CurContext = DRD; 13563 13564 PushExpressionEvaluationContext( 13565 ExpressionEvaluationContext::PotentiallyEvaluated); 13566 13567 QualType ReductionType = DRD->getType(); 13568 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 13569 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 13570 // uses semantics of argument handles by value, but it should be passed by 13571 // reference. C lang does not support references, so pass all parameters as 13572 // pointers. 13573 // Create 'T omp_in;' variable. 13574 VarDecl *OmpInParm = 13575 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 13576 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 13577 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 13578 // uses semantics of argument handles by value, but it should be passed by 13579 // reference. C lang does not support references, so pass all parameters as 13580 // pointers. 13581 // Create 'T omp_out;' variable. 13582 VarDecl *OmpOutParm = 13583 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 13584 if (S != nullptr) { 13585 PushOnScopeChains(OmpInParm, S); 13586 PushOnScopeChains(OmpOutParm, S); 13587 } else { 13588 DRD->addDecl(OmpInParm); 13589 DRD->addDecl(OmpOutParm); 13590 } 13591 Expr *InE = 13592 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 13593 Expr *OutE = 13594 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 13595 DRD->setCombinerData(InE, OutE); 13596 } 13597 13598 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 13599 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13600 DiscardCleanupsInEvaluationContext(); 13601 PopExpressionEvaluationContext(); 13602 13603 PopDeclContext(); 13604 PopFunctionScopeInfo(); 13605 13606 if (Combiner != nullptr) 13607 DRD->setCombiner(Combiner); 13608 else 13609 DRD->setInvalidDecl(); 13610 } 13611 13612 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 13613 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13614 13615 // Enter new function scope. 13616 PushFunctionScope(); 13617 setFunctionHasBranchProtectedScope(); 13618 13619 if (S != nullptr) 13620 PushDeclContext(S, DRD); 13621 else 13622 CurContext = DRD; 13623 13624 PushExpressionEvaluationContext( 13625 ExpressionEvaluationContext::PotentiallyEvaluated); 13626 13627 QualType ReductionType = DRD->getType(); 13628 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 13629 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 13630 // uses semantics of argument handles by value, but it should be passed by 13631 // reference. C lang does not support references, so pass all parameters as 13632 // pointers. 13633 // Create 'T omp_priv;' variable. 13634 VarDecl *OmpPrivParm = 13635 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 13636 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 13637 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 13638 // uses semantics of argument handles by value, but it should be passed by 13639 // reference. C lang does not support references, so pass all parameters as 13640 // pointers. 13641 // Create 'T omp_orig;' variable. 13642 VarDecl *OmpOrigParm = 13643 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 13644 if (S != nullptr) { 13645 PushOnScopeChains(OmpPrivParm, S); 13646 PushOnScopeChains(OmpOrigParm, S); 13647 } else { 13648 DRD->addDecl(OmpPrivParm); 13649 DRD->addDecl(OmpOrigParm); 13650 } 13651 Expr *OrigE = 13652 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 13653 Expr *PrivE = 13654 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 13655 DRD->setInitializerData(OrigE, PrivE); 13656 return OmpPrivParm; 13657 } 13658 13659 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 13660 VarDecl *OmpPrivParm) { 13661 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13662 DiscardCleanupsInEvaluationContext(); 13663 PopExpressionEvaluationContext(); 13664 13665 PopDeclContext(); 13666 PopFunctionScopeInfo(); 13667 13668 if (Initializer != nullptr) { 13669 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 13670 } else if (OmpPrivParm->hasInit()) { 13671 DRD->setInitializer(OmpPrivParm->getInit(), 13672 OmpPrivParm->isDirectInit() 13673 ? OMPDeclareReductionDecl::DirectInit 13674 : OMPDeclareReductionDecl::CopyInit); 13675 } else { 13676 DRD->setInvalidDecl(); 13677 } 13678 } 13679 13680 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 13681 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 13682 for (Decl *D : DeclReductions.get()) { 13683 if (IsValid) { 13684 if (S) 13685 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 13686 /*AddToContext=*/false); 13687 } else { 13688 D->setInvalidDecl(); 13689 } 13690 } 13691 return DeclReductions; 13692 } 13693 13694 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 13695 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 13696 QualType T = TInfo->getType(); 13697 if (D.isInvalidType()) 13698 return true; 13699 13700 if (getLangOpts().CPlusPlus) { 13701 // Check that there are no default arguments (C++ only). 13702 CheckExtraCXXDefaultArguments(D); 13703 } 13704 13705 return CreateParsedType(T, TInfo); 13706 } 13707 13708 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 13709 TypeResult ParsedType) { 13710 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 13711 13712 QualType MapperType = GetTypeFromParser(ParsedType.get()); 13713 assert(!MapperType.isNull() && "Expect valid mapper type"); 13714 13715 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 13716 // The type must be of struct, union or class type in C and C++ 13717 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 13718 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 13719 return QualType(); 13720 } 13721 return MapperType; 13722 } 13723 13724 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart( 13725 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 13726 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 13727 Decl *PrevDeclInScope) { 13728 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 13729 forRedeclarationInCurContext()); 13730 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 13731 // A mapper-identifier may not be redeclared in the current scope for the 13732 // same type or for a type that is compatible according to the base language 13733 // rules. 13734 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 13735 OMPDeclareMapperDecl *PrevDMD = nullptr; 13736 bool InCompoundScope = true; 13737 if (S != nullptr) { 13738 // Find previous declaration with the same name not referenced in other 13739 // declarations. 13740 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 13741 InCompoundScope = 13742 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 13743 LookupName(Lookup, S); 13744 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 13745 /*AllowInlineNamespace=*/false); 13746 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 13747 LookupResult::Filter Filter = Lookup.makeFilter(); 13748 while (Filter.hasNext()) { 13749 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 13750 if (InCompoundScope) { 13751 auto I = UsedAsPrevious.find(PrevDecl); 13752 if (I == UsedAsPrevious.end()) 13753 UsedAsPrevious[PrevDecl] = false; 13754 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 13755 UsedAsPrevious[D] = true; 13756 } 13757 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 13758 PrevDecl->getLocation(); 13759 } 13760 Filter.done(); 13761 if (InCompoundScope) { 13762 for (const auto &PrevData : UsedAsPrevious) { 13763 if (!PrevData.second) { 13764 PrevDMD = PrevData.first; 13765 break; 13766 } 13767 } 13768 } 13769 } else if (PrevDeclInScope) { 13770 auto *PrevDMDInScope = PrevDMD = 13771 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 13772 do { 13773 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 13774 PrevDMDInScope->getLocation(); 13775 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 13776 } while (PrevDMDInScope != nullptr); 13777 } 13778 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 13779 bool Invalid = false; 13780 if (I != PreviousRedeclTypes.end()) { 13781 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 13782 << MapperType << Name; 13783 Diag(I->second, diag::note_previous_definition); 13784 Invalid = true; 13785 } 13786 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 13787 MapperType, VN, PrevDMD); 13788 DC->addDecl(DMD); 13789 DMD->setAccess(AS); 13790 if (Invalid) 13791 DMD->setInvalidDecl(); 13792 13793 // Enter new function scope. 13794 PushFunctionScope(); 13795 setFunctionHasBranchProtectedScope(); 13796 13797 CurContext = DMD; 13798 13799 return DMD; 13800 } 13801 13802 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD, 13803 Scope *S, 13804 QualType MapperType, 13805 SourceLocation StartLoc, 13806 DeclarationName VN) { 13807 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString()); 13808 if (S) 13809 PushOnScopeChains(VD, S); 13810 else 13811 DMD->addDecl(VD); 13812 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 13813 DMD->setMapperVarRef(MapperVarRefExpr); 13814 } 13815 13816 Sema::DeclGroupPtrTy 13817 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S, 13818 ArrayRef<OMPClause *> ClauseList) { 13819 PopDeclContext(); 13820 PopFunctionScopeInfo(); 13821 13822 if (D) { 13823 if (S) 13824 PushOnScopeChains(D, S, /*AddToContext=*/false); 13825 D->CreateClauses(Context, ClauseList); 13826 } 13827 13828 return DeclGroupPtrTy::make(DeclGroupRef(D)); 13829 } 13830 13831 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 13832 SourceLocation StartLoc, 13833 SourceLocation LParenLoc, 13834 SourceLocation EndLoc) { 13835 Expr *ValExpr = NumTeams; 13836 Stmt *HelperValStmt = nullptr; 13837 13838 // OpenMP [teams Constrcut, Restrictions] 13839 // The num_teams expression must evaluate to a positive integer value. 13840 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 13841 /*StrictlyPositive=*/true)) 13842 return nullptr; 13843 13844 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13845 OpenMPDirectiveKind CaptureRegion = 13846 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 13847 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13848 ValExpr = MakeFullExpr(ValExpr).get(); 13849 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13850 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13851 HelperValStmt = buildPreInits(Context, Captures); 13852 } 13853 13854 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 13855 StartLoc, LParenLoc, EndLoc); 13856 } 13857 13858 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 13859 SourceLocation StartLoc, 13860 SourceLocation LParenLoc, 13861 SourceLocation EndLoc) { 13862 Expr *ValExpr = ThreadLimit; 13863 Stmt *HelperValStmt = nullptr; 13864 13865 // OpenMP [teams Constrcut, Restrictions] 13866 // The thread_limit expression must evaluate to a positive integer value. 13867 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 13868 /*StrictlyPositive=*/true)) 13869 return nullptr; 13870 13871 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13872 OpenMPDirectiveKind CaptureRegion = 13873 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 13874 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13875 ValExpr = MakeFullExpr(ValExpr).get(); 13876 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13877 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13878 HelperValStmt = buildPreInits(Context, Captures); 13879 } 13880 13881 return new (Context) OMPThreadLimitClause( 13882 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 13883 } 13884 13885 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 13886 SourceLocation StartLoc, 13887 SourceLocation LParenLoc, 13888 SourceLocation EndLoc) { 13889 Expr *ValExpr = Priority; 13890 13891 // OpenMP [2.9.1, task Constrcut] 13892 // The priority-value is a non-negative numerical scalar expression. 13893 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 13894 /*StrictlyPositive=*/false)) 13895 return nullptr; 13896 13897 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13898 } 13899 13900 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 13901 SourceLocation StartLoc, 13902 SourceLocation LParenLoc, 13903 SourceLocation EndLoc) { 13904 Expr *ValExpr = Grainsize; 13905 13906 // OpenMP [2.9.2, taskloop Constrcut] 13907 // The parameter of the grainsize clause must be a positive integer 13908 // expression. 13909 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 13910 /*StrictlyPositive=*/true)) 13911 return nullptr; 13912 13913 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13914 } 13915 13916 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 13917 SourceLocation StartLoc, 13918 SourceLocation LParenLoc, 13919 SourceLocation EndLoc) { 13920 Expr *ValExpr = NumTasks; 13921 13922 // OpenMP [2.9.2, taskloop Constrcut] 13923 // The parameter of the num_tasks clause must be a positive integer 13924 // expression. 13925 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 13926 /*StrictlyPositive=*/true)) 13927 return nullptr; 13928 13929 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13930 } 13931 13932 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 13933 SourceLocation LParenLoc, 13934 SourceLocation EndLoc) { 13935 // OpenMP [2.13.2, critical construct, Description] 13936 // ... where hint-expression is an integer constant expression that evaluates 13937 // to a valid lock hint. 13938 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 13939 if (HintExpr.isInvalid()) 13940 return nullptr; 13941 return new (Context) 13942 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 13943 } 13944 13945 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 13946 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 13947 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 13948 SourceLocation EndLoc) { 13949 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 13950 std::string Values; 13951 Values += "'"; 13952 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 13953 Values += "'"; 13954 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 13955 << Values << getOpenMPClauseName(OMPC_dist_schedule); 13956 return nullptr; 13957 } 13958 Expr *ValExpr = ChunkSize; 13959 Stmt *HelperValStmt = nullptr; 13960 if (ChunkSize) { 13961 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 13962 !ChunkSize->isInstantiationDependent() && 13963 !ChunkSize->containsUnexpandedParameterPack()) { 13964 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 13965 ExprResult Val = 13966 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 13967 if (Val.isInvalid()) 13968 return nullptr; 13969 13970 ValExpr = Val.get(); 13971 13972 // OpenMP [2.7.1, Restrictions] 13973 // chunk_size must be a loop invariant integer expression with a positive 13974 // value. 13975 llvm::APSInt Result; 13976 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 13977 if (Result.isSigned() && !Result.isStrictlyPositive()) { 13978 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 13979 << "dist_schedule" << ChunkSize->getSourceRange(); 13980 return nullptr; 13981 } 13982 } else if (getOpenMPCaptureRegionForClause( 13983 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 13984 OMPD_unknown && 13985 !CurContext->isDependentContext()) { 13986 ValExpr = MakeFullExpr(ValExpr).get(); 13987 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13988 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13989 HelperValStmt = buildPreInits(Context, Captures); 13990 } 13991 } 13992 } 13993 13994 return new (Context) 13995 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 13996 Kind, ValExpr, HelperValStmt); 13997 } 13998 13999 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 14000 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 14001 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 14002 SourceLocation KindLoc, SourceLocation EndLoc) { 14003 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 14004 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 14005 std::string Value; 14006 SourceLocation Loc; 14007 Value += "'"; 14008 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 14009 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14010 OMPC_DEFAULTMAP_MODIFIER_tofrom); 14011 Loc = MLoc; 14012 } else { 14013 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14014 OMPC_DEFAULTMAP_scalar); 14015 Loc = KindLoc; 14016 } 14017 Value += "'"; 14018 Diag(Loc, diag::err_omp_unexpected_clause_value) 14019 << Value << getOpenMPClauseName(OMPC_defaultmap); 14020 return nullptr; 14021 } 14022 DSAStack->setDefaultDMAToFromScalar(StartLoc); 14023 14024 return new (Context) 14025 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 14026 } 14027 14028 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 14029 DeclContext *CurLexicalContext = getCurLexicalContext(); 14030 if (!CurLexicalContext->isFileContext() && 14031 !CurLexicalContext->isExternCContext() && 14032 !CurLexicalContext->isExternCXXContext() && 14033 !isa<CXXRecordDecl>(CurLexicalContext) && 14034 !isa<ClassTemplateDecl>(CurLexicalContext) && 14035 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 14036 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 14037 Diag(Loc, diag::err_omp_region_not_file_context); 14038 return false; 14039 } 14040 ++DeclareTargetNestingLevel; 14041 return true; 14042 } 14043 14044 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 14045 assert(DeclareTargetNestingLevel > 0 && 14046 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 14047 --DeclareTargetNestingLevel; 14048 } 14049 14050 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 14051 CXXScopeSpec &ScopeSpec, 14052 const DeclarationNameInfo &Id, 14053 OMPDeclareTargetDeclAttr::MapTypeTy MT, 14054 NamedDeclSetType &SameDirectiveDecls) { 14055 LookupResult Lookup(*this, Id, LookupOrdinaryName); 14056 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 14057 14058 if (Lookup.isAmbiguous()) 14059 return; 14060 Lookup.suppressDiagnostics(); 14061 14062 if (!Lookup.isSingleResult()) { 14063 if (TypoCorrection Corrected = 14064 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 14065 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 14066 CTK_ErrorRecovery)) { 14067 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 14068 << Id.getName()); 14069 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 14070 return; 14071 } 14072 14073 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 14074 return; 14075 } 14076 14077 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 14078 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 14079 isa<FunctionTemplateDecl>(ND)) { 14080 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 14081 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 14082 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14083 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 14084 cast<ValueDecl>(ND)); 14085 if (!Res) { 14086 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 14087 ND->addAttr(A); 14088 if (ASTMutationListener *ML = Context.getASTMutationListener()) 14089 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 14090 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 14091 } else if (*Res != MT) { 14092 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 14093 << Id.getName(); 14094 } 14095 } else { 14096 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 14097 } 14098 } 14099 14100 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 14101 Sema &SemaRef, Decl *D) { 14102 if (!D || !isa<VarDecl>(D)) 14103 return; 14104 auto *VD = cast<VarDecl>(D); 14105 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 14106 return; 14107 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 14108 SemaRef.Diag(SL, diag::note_used_here) << SR; 14109 } 14110 14111 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 14112 Sema &SemaRef, DSAStackTy *Stack, 14113 ValueDecl *VD) { 14114 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 14115 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 14116 /*FullCheck=*/false); 14117 } 14118 14119 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 14120 SourceLocation IdLoc) { 14121 if (!D || D->isInvalidDecl()) 14122 return; 14123 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 14124 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 14125 if (auto *VD = dyn_cast<VarDecl>(D)) { 14126 // Only global variables can be marked as declare target. 14127 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 14128 !VD->isStaticDataMember()) 14129 return; 14130 // 2.10.6: threadprivate variable cannot appear in a declare target 14131 // directive. 14132 if (DSAStack->isThreadPrivate(VD)) { 14133 Diag(SL, diag::err_omp_threadprivate_in_target); 14134 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 14135 return; 14136 } 14137 } 14138 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 14139 D = FTD->getTemplatedDecl(); 14140 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 14141 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14142 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 14143 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 14144 assert(IdLoc.isValid() && "Source location is expected"); 14145 Diag(IdLoc, diag::err_omp_function_in_link_clause); 14146 Diag(FD->getLocation(), diag::note_defined_here) << FD; 14147 return; 14148 } 14149 } 14150 if (auto *VD = dyn_cast<ValueDecl>(D)) { 14151 // Problem if any with var declared with incomplete type will be reported 14152 // as normal, so no need to check it here. 14153 if ((E || !VD->getType()->isIncompleteType()) && 14154 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 14155 return; 14156 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 14157 // Checking declaration inside declare target region. 14158 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 14159 isa<FunctionTemplateDecl>(D)) { 14160 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 14161 Context, OMPDeclareTargetDeclAttr::MT_To); 14162 D->addAttr(A); 14163 if (ASTMutationListener *ML = Context.getASTMutationListener()) 14164 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 14165 } 14166 return; 14167 } 14168 } 14169 if (!E) 14170 return; 14171 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 14172 } 14173 14174 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 14175 CXXScopeSpec &MapperIdScopeSpec, 14176 DeclarationNameInfo &MapperId, 14177 const OMPVarListLocTy &Locs, 14178 ArrayRef<Expr *> UnresolvedMappers) { 14179 MappableVarListInfo MVLI(VarList); 14180 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 14181 MapperIdScopeSpec, MapperId, UnresolvedMappers); 14182 if (MVLI.ProcessedVarList.empty()) 14183 return nullptr; 14184 14185 return OMPToClause::Create( 14186 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 14187 MVLI.VarComponents, MVLI.UDMapperList, 14188 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 14189 } 14190 14191 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 14192 CXXScopeSpec &MapperIdScopeSpec, 14193 DeclarationNameInfo &MapperId, 14194 const OMPVarListLocTy &Locs, 14195 ArrayRef<Expr *> UnresolvedMappers) { 14196 MappableVarListInfo MVLI(VarList); 14197 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 14198 MapperIdScopeSpec, MapperId, UnresolvedMappers); 14199 if (MVLI.ProcessedVarList.empty()) 14200 return nullptr; 14201 14202 return OMPFromClause::Create( 14203 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 14204 MVLI.VarComponents, MVLI.UDMapperList, 14205 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 14206 } 14207 14208 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 14209 const OMPVarListLocTy &Locs) { 14210 MappableVarListInfo MVLI(VarList); 14211 SmallVector<Expr *, 8> PrivateCopies; 14212 SmallVector<Expr *, 8> Inits; 14213 14214 for (Expr *RefExpr : VarList) { 14215 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 14216 SourceLocation ELoc; 14217 SourceRange ERange; 14218 Expr *SimpleRefExpr = RefExpr; 14219 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14220 if (Res.second) { 14221 // It will be analyzed later. 14222 MVLI.ProcessedVarList.push_back(RefExpr); 14223 PrivateCopies.push_back(nullptr); 14224 Inits.push_back(nullptr); 14225 } 14226 ValueDecl *D = Res.first; 14227 if (!D) 14228 continue; 14229 14230 QualType Type = D->getType(); 14231 Type = Type.getNonReferenceType().getUnqualifiedType(); 14232 14233 auto *VD = dyn_cast<VarDecl>(D); 14234 14235 // Item should be a pointer or reference to pointer. 14236 if (!Type->isPointerType()) { 14237 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 14238 << 0 << RefExpr->getSourceRange(); 14239 continue; 14240 } 14241 14242 // Build the private variable and the expression that refers to it. 14243 auto VDPrivate = 14244 buildVarDecl(*this, ELoc, Type, D->getName(), 14245 D->hasAttrs() ? &D->getAttrs() : nullptr, 14246 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14247 if (VDPrivate->isInvalidDecl()) 14248 continue; 14249 14250 CurContext->addDecl(VDPrivate); 14251 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14252 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 14253 14254 // Add temporary variable to initialize the private copy of the pointer. 14255 VarDecl *VDInit = 14256 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 14257 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 14258 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 14259 AddInitializerToDecl(VDPrivate, 14260 DefaultLvalueConversion(VDInitRefExpr).get(), 14261 /*DirectInit=*/false); 14262 14263 // If required, build a capture to implement the privatization initialized 14264 // with the current list item value. 14265 DeclRefExpr *Ref = nullptr; 14266 if (!VD) 14267 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14268 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 14269 PrivateCopies.push_back(VDPrivateRefExpr); 14270 Inits.push_back(VDInitRefExpr); 14271 14272 // We need to add a data sharing attribute for this variable to make sure it 14273 // is correctly captured. A variable that shows up in a use_device_ptr has 14274 // similar properties of a first private variable. 14275 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 14276 14277 // Create a mappable component for the list item. List items in this clause 14278 // only need a component. 14279 MVLI.VarBaseDeclarations.push_back(D); 14280 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14281 MVLI.VarComponents.back().push_back( 14282 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 14283 } 14284 14285 if (MVLI.ProcessedVarList.empty()) 14286 return nullptr; 14287 14288 return OMPUseDevicePtrClause::Create( 14289 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 14290 MVLI.VarBaseDeclarations, MVLI.VarComponents); 14291 } 14292 14293 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 14294 const OMPVarListLocTy &Locs) { 14295 MappableVarListInfo MVLI(VarList); 14296 for (Expr *RefExpr : VarList) { 14297 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 14298 SourceLocation ELoc; 14299 SourceRange ERange; 14300 Expr *SimpleRefExpr = RefExpr; 14301 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14302 if (Res.second) { 14303 // It will be analyzed later. 14304 MVLI.ProcessedVarList.push_back(RefExpr); 14305 } 14306 ValueDecl *D = Res.first; 14307 if (!D) 14308 continue; 14309 14310 QualType Type = D->getType(); 14311 // item should be a pointer or array or reference to pointer or array 14312 if (!Type.getNonReferenceType()->isPointerType() && 14313 !Type.getNonReferenceType()->isArrayType()) { 14314 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 14315 << 0 << RefExpr->getSourceRange(); 14316 continue; 14317 } 14318 14319 // Check if the declaration in the clause does not show up in any data 14320 // sharing attribute. 14321 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14322 if (isOpenMPPrivate(DVar.CKind)) { 14323 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14324 << getOpenMPClauseName(DVar.CKind) 14325 << getOpenMPClauseName(OMPC_is_device_ptr) 14326 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14327 reportOriginalDsa(*this, DSAStack, D, DVar); 14328 continue; 14329 } 14330 14331 const Expr *ConflictExpr; 14332 if (DSAStack->checkMappableExprComponentListsForDecl( 14333 D, /*CurrentRegionOnly=*/true, 14334 [&ConflictExpr]( 14335 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 14336 OpenMPClauseKind) -> bool { 14337 ConflictExpr = R.front().getAssociatedExpression(); 14338 return true; 14339 })) { 14340 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 14341 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 14342 << ConflictExpr->getSourceRange(); 14343 continue; 14344 } 14345 14346 // Store the components in the stack so that they can be used to check 14347 // against other clauses later on. 14348 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 14349 DSAStack->addMappableExpressionComponents( 14350 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 14351 14352 // Record the expression we've just processed. 14353 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 14354 14355 // Create a mappable component for the list item. List items in this clause 14356 // only need a component. We use a null declaration to signal fields in 14357 // 'this'. 14358 assert((isa<DeclRefExpr>(SimpleRefExpr) || 14359 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 14360 "Unexpected device pointer expression!"); 14361 MVLI.VarBaseDeclarations.push_back( 14362 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 14363 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14364 MVLI.VarComponents.back().push_back(MC); 14365 } 14366 14367 if (MVLI.ProcessedVarList.empty()) 14368 return nullptr; 14369 14370 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 14371 MVLI.VarBaseDeclarations, 14372 MVLI.VarComponents); 14373 } 14374