1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// \brief This file implements semantic analysis for OpenMP directives and 11 /// clauses. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "TreeTransform.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTMutationListener.h" 18 #include "clang/AST/CXXInheritance.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclOpenMP.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/OpenMPKinds.h" 27 #include "clang/Basic/TargetInfo.h" 28 #include "clang/Lex/Preprocessor.h" 29 #include "clang/Sema/Initialization.h" 30 #include "clang/Sema/Lookup.h" 31 #include "clang/Sema/Scope.h" 32 #include "clang/Sema/ScopeInfo.h" 33 #include "clang/Sema/SemaInternal.h" 34 #include "llvm/ADT/PointerEmbeddedInt.h" 35 using namespace clang; 36 37 //===----------------------------------------------------------------------===// 38 // Stack of data-sharing attributes for variables 39 //===----------------------------------------------------------------------===// 40 41 namespace { 42 /// \brief Default data sharing attributes, which can be applied to directive. 43 enum DefaultDataSharingAttributes { 44 DSA_unspecified = 0, /// \brief Data sharing attribute not specified. 45 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. 46 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. 47 }; 48 49 /// \brief Stack for tracking declarations used in OpenMP directives and 50 /// clauses and their data-sharing attributes. 51 class DSAStackTy final { 52 public: 53 struct DSAVarData final { 54 OpenMPDirectiveKind DKind = OMPD_unknown; 55 OpenMPClauseKind CKind = OMPC_unknown; 56 Expr *RefExpr = nullptr; 57 DeclRefExpr *PrivateCopy = nullptr; 58 SourceLocation ImplicitDSALoc; 59 DSAVarData() = default; 60 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, Expr *RefExpr, 61 DeclRefExpr *PrivateCopy, SourceLocation ImplicitDSALoc) 62 : DKind(DKind), CKind(CKind), RefExpr(RefExpr), 63 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {} 64 }; 65 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4> 66 OperatorOffsetTy; 67 68 private: 69 struct DSAInfo final { 70 OpenMPClauseKind Attributes = OMPC_unknown; 71 /// Pointer to a reference expression and a flag which shows that the 72 /// variable is marked as lastprivate(true) or not (false). 73 llvm::PointerIntPair<Expr *, 1, bool> RefExpr; 74 DeclRefExpr *PrivateCopy = nullptr; 75 }; 76 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy; 77 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy; 78 typedef std::pair<unsigned, VarDecl *> LCDeclInfo; 79 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy; 80 /// Struct that associates a component with the clause kind where they are 81 /// found. 82 struct MappedExprComponentTy { 83 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 84 OpenMPClauseKind Kind = OMPC_unknown; 85 }; 86 typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy> 87 MappedExprComponentsTy; 88 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>> 89 CriticalsWithHintsTy; 90 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy> 91 DoacrossDependMapTy; 92 struct ReductionData { 93 typedef llvm::PointerEmbeddedInt<BinaryOperatorKind, 16> BOKPtrType; 94 SourceRange ReductionRange; 95 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 96 ReductionData() = default; 97 void set(BinaryOperatorKind BO, SourceRange RR) { 98 ReductionRange = RR; 99 ReductionOp = BO; 100 } 101 void set(const Expr *RefExpr, SourceRange RR) { 102 ReductionRange = RR; 103 ReductionOp = RefExpr; 104 } 105 }; 106 typedef llvm::DenseMap<ValueDecl *, ReductionData> DeclReductionMapTy; 107 108 struct SharingMapTy final { 109 DeclSAMapTy SharingMap; 110 DeclReductionMapTy ReductionMap; 111 AlignedMapTy AlignedMap; 112 MappedExprComponentsTy MappedExprComponents; 113 LoopControlVariablesMapTy LCVMap; 114 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 115 SourceLocation DefaultAttrLoc; 116 OpenMPDirectiveKind Directive = OMPD_unknown; 117 DeclarationNameInfo DirectiveName; 118 Scope *CurScope = nullptr; 119 SourceLocation ConstructLoc; 120 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 121 /// get the data (loop counters etc.) about enclosing loop-based construct. 122 /// This data is required during codegen. 123 DoacrossDependMapTy DoacrossDepends; 124 /// \brief first argument (Expr *) contains optional argument of the 125 /// 'ordered' clause, the second one is true if the regions has 'ordered' 126 /// clause, false otherwise. 127 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion; 128 bool NowaitRegion = false; 129 bool CancelRegion = false; 130 unsigned AssociatedLoops = 1; 131 SourceLocation InnerTeamsRegionLoc; 132 /// Reference to the taskgroup task_reduction reference expression. 133 Expr *TaskgroupReductionRef = nullptr; 134 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 135 Scope *CurScope, SourceLocation Loc) 136 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 137 ConstructLoc(Loc) {} 138 SharingMapTy() = default; 139 }; 140 141 typedef SmallVector<SharingMapTy, 4> StackTy; 142 143 /// \brief Stack of used declaration and their data-sharing attributes. 144 DeclSAMapTy Threadprivates; 145 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 146 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 147 /// \brief true, if check for DSA must be from parent directive, false, if 148 /// from current directive. 149 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 150 Sema &SemaRef; 151 bool ForceCapturing = false; 152 CriticalsWithHintsTy Criticals; 153 154 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; 155 156 DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D); 157 158 /// \brief Checks if the variable is a local for OpenMP region. 159 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); 160 161 bool isStackEmpty() const { 162 return Stack.empty() || 163 Stack.back().second != CurrentNonCapturingFunctionScope || 164 Stack.back().first.empty(); 165 } 166 167 public: 168 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 169 170 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 171 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 172 173 bool isForceVarCapturing() const { return ForceCapturing; } 174 void setForceVarCapturing(bool V) { ForceCapturing = V; } 175 176 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 177 Scope *CurScope, SourceLocation Loc) { 178 if (Stack.empty() || 179 Stack.back().second != CurrentNonCapturingFunctionScope) 180 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 181 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 182 Stack.back().first.back().DefaultAttrLoc = Loc; 183 } 184 185 void pop() { 186 assert(!Stack.back().first.empty() && 187 "Data-sharing attributes stack is empty!"); 188 Stack.back().first.pop_back(); 189 } 190 191 /// Start new OpenMP region stack in new non-capturing function. 192 void pushFunction() { 193 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 194 assert(!isa<CapturingScopeInfo>(CurFnScope)); 195 CurrentNonCapturingFunctionScope = CurFnScope; 196 } 197 /// Pop region stack for non-capturing function. 198 void popFunction(const FunctionScopeInfo *OldFSI) { 199 if (!Stack.empty() && Stack.back().second == OldFSI) { 200 assert(Stack.back().first.empty()); 201 Stack.pop_back(); 202 } 203 CurrentNonCapturingFunctionScope = nullptr; 204 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 205 if (!isa<CapturingScopeInfo>(FSI)) { 206 CurrentNonCapturingFunctionScope = FSI; 207 break; 208 } 209 } 210 } 211 212 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) { 213 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint); 214 } 215 const std::pair<OMPCriticalDirective *, llvm::APSInt> 216 getCriticalWithHint(const DeclarationNameInfo &Name) const { 217 auto I = Criticals.find(Name.getAsString()); 218 if (I != Criticals.end()) 219 return I->second; 220 return std::make_pair(nullptr, llvm::APSInt()); 221 } 222 /// \brief If 'aligned' declaration for given variable \a D was not seen yet, 223 /// add it and return NULL; otherwise return previous occurrence's expression 224 /// for diagnostics. 225 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE); 226 227 /// \brief Register specified variable as loop control variable. 228 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture); 229 /// \brief Check if the specified variable is a loop control variable for 230 /// current region. 231 /// \return The index of the loop control variable in the list of associated 232 /// for-loops (from outer to inner). 233 LCDeclInfo isLoopControlVariable(ValueDecl *D); 234 /// \brief Check if the specified variable is a loop control variable for 235 /// parent region. 236 /// \return The index of the loop control variable in the list of associated 237 /// for-loops (from outer to inner). 238 LCDeclInfo isParentLoopControlVariable(ValueDecl *D); 239 /// \brief Get the loop control variable for the I-th loop (or nullptr) in 240 /// parent directive. 241 ValueDecl *getParentLoopControlVariable(unsigned I); 242 243 /// \brief Adds explicit data sharing attribute to the specified declaration. 244 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, 245 DeclRefExpr *PrivateCopy = nullptr); 246 247 /// Adds additional information for the reduction items with the reduction id 248 /// represented as an operator. 249 void addTaskgroupReductionData(ValueDecl *D, SourceRange SR, 250 BinaryOperatorKind BOK); 251 /// Adds additional information for the reduction items with the reduction id 252 /// represented as reduction identifier. 253 void addTaskgroupReductionData(ValueDecl *D, SourceRange SR, 254 const Expr *ReductionRef); 255 /// Returns the location and reduction operation from the innermost parent 256 /// region for the given \p D. 257 DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR, 258 BinaryOperatorKind &BOK); 259 /// Returns the location and reduction operation from the innermost parent 260 /// region for the given \p D. 261 DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR, 262 const Expr *&ReductionRef); 263 /// Return reduction reference expression for the current taskgroup. 264 Expr *getTaskgroupReductionRef() const { 265 assert(Stack.back().first.back().Directive == OMPD_taskgroup && 266 "taskgroup reference expression requested for non taskgroup " 267 "directive."); 268 return Stack.back().first.back().TaskgroupReductionRef; 269 } 270 271 /// \brief Returns data sharing attributes from top of the stack for the 272 /// specified declaration. 273 DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 274 /// \brief Returns data-sharing attributes for the specified declaration. 275 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent); 276 /// \brief Checks if the specified variables has data-sharing attributes which 277 /// match specified \a CPred predicate in any directive which matches \a DPred 278 /// predicate. 279 DSAVarData hasDSA(ValueDecl *D, 280 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 281 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 282 bool FromParent); 283 /// \brief Checks if the specified variables has data-sharing attributes which 284 /// match specified \a CPred predicate in any innermost directive which 285 /// matches \a DPred predicate. 286 DSAVarData 287 hasInnermostDSA(ValueDecl *D, 288 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 289 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 290 bool FromParent); 291 /// \brief Checks if the specified variables has explicit data-sharing 292 /// attributes which match specified \a CPred predicate at the specified 293 /// OpenMP region. 294 bool hasExplicitDSA(ValueDecl *D, 295 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 296 unsigned Level, bool NotLastprivate = false); 297 298 /// \brief Returns true if the directive at level \Level matches in the 299 /// specified \a DPred predicate. 300 bool hasExplicitDirective( 301 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 302 unsigned Level); 303 304 /// \brief Finds a directive which matches specified \a DPred predicate. 305 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind, 306 const DeclarationNameInfo &, 307 SourceLocation)> &DPred, 308 bool FromParent); 309 310 /// \brief Returns currently analyzed directive. 311 OpenMPDirectiveKind getCurrentDirective() const { 312 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; 313 } 314 /// \brief Returns parent directive. 315 OpenMPDirectiveKind getParentDirective() const { 316 if (isStackEmpty() || Stack.back().first.size() == 1) 317 return OMPD_unknown; 318 return std::next(Stack.back().first.rbegin())->Directive; 319 } 320 321 /// \brief Set default data sharing attribute to none. 322 void setDefaultDSANone(SourceLocation Loc) { 323 assert(!isStackEmpty()); 324 Stack.back().first.back().DefaultAttr = DSA_none; 325 Stack.back().first.back().DefaultAttrLoc = Loc; 326 } 327 /// \brief Set default data sharing attribute to shared. 328 void setDefaultDSAShared(SourceLocation Loc) { 329 assert(!isStackEmpty()); 330 Stack.back().first.back().DefaultAttr = DSA_shared; 331 Stack.back().first.back().DefaultAttrLoc = Loc; 332 } 333 334 DefaultDataSharingAttributes getDefaultDSA() const { 335 return isStackEmpty() ? DSA_unspecified 336 : Stack.back().first.back().DefaultAttr; 337 } 338 SourceLocation getDefaultDSALocation() const { 339 return isStackEmpty() ? SourceLocation() 340 : Stack.back().first.back().DefaultAttrLoc; 341 } 342 343 /// \brief Checks if the specified variable is a threadprivate. 344 bool isThreadPrivate(VarDecl *D) { 345 DSAVarData DVar = getTopDSA(D, false); 346 return isOpenMPThreadPrivate(DVar.CKind); 347 } 348 349 /// \brief Marks current region as ordered (it has an 'ordered' clause). 350 void setOrderedRegion(bool IsOrdered, Expr *Param) { 351 assert(!isStackEmpty()); 352 Stack.back().first.back().OrderedRegion.setInt(IsOrdered); 353 Stack.back().first.back().OrderedRegion.setPointer(Param); 354 } 355 /// \brief Returns true, if parent region is ordered (has associated 356 /// 'ordered' clause), false - otherwise. 357 bool isParentOrderedRegion() const { 358 if (isStackEmpty() || Stack.back().first.size() == 1) 359 return false; 360 return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt(); 361 } 362 /// \brief Returns optional parameter for the ordered region. 363 Expr *getParentOrderedRegionParam() const { 364 if (isStackEmpty() || Stack.back().first.size() == 1) 365 return nullptr; 366 return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer(); 367 } 368 /// \brief Marks current region as nowait (it has a 'nowait' clause). 369 void setNowaitRegion(bool IsNowait = true) { 370 assert(!isStackEmpty()); 371 Stack.back().first.back().NowaitRegion = IsNowait; 372 } 373 /// \brief Returns true, if parent region is nowait (has associated 374 /// 'nowait' clause), false - otherwise. 375 bool isParentNowaitRegion() const { 376 if (isStackEmpty() || Stack.back().first.size() == 1) 377 return false; 378 return std::next(Stack.back().first.rbegin())->NowaitRegion; 379 } 380 /// \brief Marks parent region as cancel region. 381 void setParentCancelRegion(bool Cancel = true) { 382 if (!isStackEmpty() && Stack.back().first.size() > 1) { 383 auto &StackElemRef = *std::next(Stack.back().first.rbegin()); 384 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; 385 } 386 } 387 /// \brief Return true if current region has inner cancel construct. 388 bool isCancelRegion() const { 389 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; 390 } 391 392 /// \brief Set collapse value for the region. 393 void setAssociatedLoops(unsigned Val) { 394 assert(!isStackEmpty()); 395 Stack.back().first.back().AssociatedLoops = Val; 396 } 397 /// \brief Return collapse value for region. 398 unsigned getAssociatedLoops() const { 399 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; 400 } 401 402 /// \brief Marks current target region as one with closely nested teams 403 /// region. 404 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 405 if (!isStackEmpty() && Stack.back().first.size() > 1) { 406 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = 407 TeamsRegionLoc; 408 } 409 } 410 /// \brief Returns true, if current region has closely nested teams region. 411 bool hasInnerTeamsRegion() const { 412 return getInnerTeamsRegionLoc().isValid(); 413 } 414 /// \brief Returns location of the nested teams region (if any). 415 SourceLocation getInnerTeamsRegionLoc() const { 416 return isStackEmpty() ? SourceLocation() 417 : Stack.back().first.back().InnerTeamsRegionLoc; 418 } 419 420 Scope *getCurScope() const { 421 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 422 } 423 Scope *getCurScope() { 424 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 425 } 426 SourceLocation getConstructLoc() { 427 return isStackEmpty() ? SourceLocation() 428 : Stack.back().first.back().ConstructLoc; 429 } 430 431 /// Do the check specified in \a Check to all component lists and return true 432 /// if any issue is found. 433 bool checkMappableExprComponentListsForDecl( 434 ValueDecl *VD, bool CurrentRegionOnly, 435 const llvm::function_ref< 436 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 437 OpenMPClauseKind)> &Check) { 438 if (isStackEmpty()) 439 return false; 440 auto SI = Stack.back().first.rbegin(); 441 auto SE = Stack.back().first.rend(); 442 443 if (SI == SE) 444 return false; 445 446 if (CurrentRegionOnly) { 447 SE = std::next(SI); 448 } else { 449 ++SI; 450 } 451 452 for (; SI != SE; ++SI) { 453 auto MI = SI->MappedExprComponents.find(VD); 454 if (MI != SI->MappedExprComponents.end()) 455 for (auto &L : MI->second.Components) 456 if (Check(L, MI->second.Kind)) 457 return true; 458 } 459 return false; 460 } 461 462 /// Do the check specified in \a Check to all component lists at a given level 463 /// and return true if any issue is found. 464 bool checkMappableExprComponentListsForDeclAtLevel( 465 ValueDecl *VD, unsigned Level, 466 const llvm::function_ref< 467 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 468 OpenMPClauseKind)> &Check) { 469 if (isStackEmpty()) 470 return false; 471 472 auto StartI = Stack.back().first.begin(); 473 auto EndI = Stack.back().first.end(); 474 if (std::distance(StartI, EndI) <= (int)Level) 475 return false; 476 std::advance(StartI, Level); 477 478 auto MI = StartI->MappedExprComponents.find(VD); 479 if (MI != StartI->MappedExprComponents.end()) 480 for (auto &L : MI->second.Components) 481 if (Check(L, MI->second.Kind)) 482 return true; 483 return false; 484 } 485 486 /// Create a new mappable expression component list associated with a given 487 /// declaration and initialize it with the provided list of components. 488 void addMappableExpressionComponents( 489 ValueDecl *VD, 490 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 491 OpenMPClauseKind WhereFoundClauseKind) { 492 assert(!isStackEmpty() && 493 "Not expecting to retrieve components from a empty stack!"); 494 auto &MEC = Stack.back().first.back().MappedExprComponents[VD]; 495 // Create new entry and append the new components there. 496 MEC.Components.resize(MEC.Components.size() + 1); 497 MEC.Components.back().append(Components.begin(), Components.end()); 498 MEC.Kind = WhereFoundClauseKind; 499 } 500 501 unsigned getNestingLevel() const { 502 assert(!isStackEmpty()); 503 return Stack.back().first.size() - 1; 504 } 505 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) { 506 assert(!isStackEmpty() && Stack.back().first.size() > 1); 507 auto &StackElem = *std::next(Stack.back().first.rbegin()); 508 assert(isOpenMPWorksharingDirective(StackElem.Directive)); 509 StackElem.DoacrossDepends.insert({C, OpsOffs}); 510 } 511 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 512 getDoacrossDependClauses() const { 513 assert(!isStackEmpty()); 514 auto &StackElem = Stack.back().first.back(); 515 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 516 auto &Ref = StackElem.DoacrossDepends; 517 return llvm::make_range(Ref.begin(), Ref.end()); 518 } 519 return llvm::make_range(StackElem.DoacrossDepends.end(), 520 StackElem.DoacrossDepends.end()); 521 } 522 }; 523 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 524 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || 525 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; 526 } 527 } // namespace 528 529 static Expr *getExprAsWritten(Expr *E) { 530 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) 531 E = ExprTemp->getSubExpr(); 532 533 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 534 E = MTE->GetTemporaryExpr(); 535 536 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 537 E = Binder->getSubExpr(); 538 539 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 540 E = ICE->getSubExprAsWritten(); 541 return E->IgnoreParens(); 542 } 543 544 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 545 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 546 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 547 D = ME->getMemberDecl(); 548 auto *VD = dyn_cast<VarDecl>(D); 549 auto *FD = dyn_cast<FieldDecl>(D); 550 if (VD != nullptr) { 551 VD = VD->getCanonicalDecl(); 552 D = VD; 553 } else { 554 assert(FD); 555 FD = FD->getCanonicalDecl(); 556 D = FD; 557 } 558 return D; 559 } 560 561 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter, 562 ValueDecl *D) { 563 D = getCanonicalDecl(D); 564 auto *VD = dyn_cast<VarDecl>(D); 565 auto *FD = dyn_cast<FieldDecl>(D); 566 DSAVarData DVar; 567 if (isStackEmpty() || Iter == Stack.back().first.rend()) { 568 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 569 // in a region but not in construct] 570 // File-scope or namespace-scope variables referenced in called routines 571 // in the region are shared unless they appear in a threadprivate 572 // directive. 573 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) 574 DVar.CKind = OMPC_shared; 575 576 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 577 // in a region but not in construct] 578 // Variables with static storage duration that are declared in called 579 // routines in the region are shared. 580 if (VD && VD->hasGlobalStorage()) 581 DVar.CKind = OMPC_shared; 582 583 // Non-static data members are shared by default. 584 if (FD) 585 DVar.CKind = OMPC_shared; 586 587 return DVar; 588 } 589 590 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 591 // in a Construct, C/C++, predetermined, p.1] 592 // Variables with automatic storage duration that are declared in a scope 593 // inside the construct are private. 594 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 595 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 596 DVar.CKind = OMPC_private; 597 return DVar; 598 } 599 600 DVar.DKind = Iter->Directive; 601 // Explicitly specified attributes and local variables with predetermined 602 // attributes. 603 if (Iter->SharingMap.count(D)) { 604 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer(); 605 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy; 606 DVar.CKind = Iter->SharingMap[D].Attributes; 607 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 608 return DVar; 609 } 610 611 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 612 // in a Construct, C/C++, implicitly determined, p.1] 613 // In a parallel or task construct, the data-sharing attributes of these 614 // variables are determined by the default clause, if present. 615 switch (Iter->DefaultAttr) { 616 case DSA_shared: 617 DVar.CKind = OMPC_shared; 618 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 619 return DVar; 620 case DSA_none: 621 return DVar; 622 case DSA_unspecified: 623 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 624 // in a Construct, implicitly determined, p.2] 625 // In a parallel construct, if no default clause is present, these 626 // variables are shared. 627 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 628 if (isOpenMPParallelDirective(DVar.DKind) || 629 isOpenMPTeamsDirective(DVar.DKind)) { 630 DVar.CKind = OMPC_shared; 631 return DVar; 632 } 633 634 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 635 // in a Construct, implicitly determined, p.4] 636 // In a task construct, if no default clause is present, a variable that in 637 // the enclosing context is determined to be shared by all implicit tasks 638 // bound to the current team is shared. 639 if (isOpenMPTaskingDirective(DVar.DKind)) { 640 DSAVarData DVarTemp; 641 auto I = Iter, E = Stack.back().first.rend(); 642 do { 643 ++I; 644 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 645 // Referenced in a Construct, implicitly determined, p.6] 646 // In a task construct, if no default clause is present, a variable 647 // whose data-sharing attribute is not determined by the rules above is 648 // firstprivate. 649 DVarTemp = getDSA(I, D); 650 if (DVarTemp.CKind != OMPC_shared) { 651 DVar.RefExpr = nullptr; 652 DVar.CKind = OMPC_firstprivate; 653 return DVar; 654 } 655 } while (I != E && !isParallelOrTaskRegion(I->Directive)); 656 DVar.CKind = 657 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 658 return DVar; 659 } 660 } 661 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 662 // in a Construct, implicitly determined, p.3] 663 // For constructs other than task, if no default clause is present, these 664 // variables inherit their data-sharing attributes from the enclosing 665 // context. 666 return getDSA(++Iter, D); 667 } 668 669 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) { 670 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 671 D = getCanonicalDecl(D); 672 auto &StackElem = Stack.back().first.back(); 673 auto It = StackElem.AlignedMap.find(D); 674 if (It == StackElem.AlignedMap.end()) { 675 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 676 StackElem.AlignedMap[D] = NewDE; 677 return nullptr; 678 } else { 679 assert(It->second && "Unexpected nullptr expr in the aligned map"); 680 return It->second; 681 } 682 return nullptr; 683 } 684 685 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) { 686 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 687 D = getCanonicalDecl(D); 688 auto &StackElem = Stack.back().first.back(); 689 StackElem.LCVMap.insert( 690 {D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)}); 691 } 692 693 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) { 694 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 695 D = getCanonicalDecl(D); 696 auto &StackElem = Stack.back().first.back(); 697 auto It = StackElem.LCVMap.find(D); 698 if (It != StackElem.LCVMap.end()) 699 return It->second; 700 return {0, nullptr}; 701 } 702 703 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) { 704 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 705 "Data-sharing attributes stack is empty"); 706 D = getCanonicalDecl(D); 707 auto &StackElem = *std::next(Stack.back().first.rbegin()); 708 auto It = StackElem.LCVMap.find(D); 709 if (It != StackElem.LCVMap.end()) 710 return It->second; 711 return {0, nullptr}; 712 } 713 714 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) { 715 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 716 "Data-sharing attributes stack is empty"); 717 auto &StackElem = *std::next(Stack.back().first.rbegin()); 718 if (StackElem.LCVMap.size() < I) 719 return nullptr; 720 for (auto &Pair : StackElem.LCVMap) 721 if (Pair.second.first == I) 722 return Pair.first; 723 return nullptr; 724 } 725 726 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, 727 DeclRefExpr *PrivateCopy) { 728 D = getCanonicalDecl(D); 729 if (A == OMPC_threadprivate) { 730 auto &Data = Threadprivates[D]; 731 Data.Attributes = A; 732 Data.RefExpr.setPointer(E); 733 Data.PrivateCopy = nullptr; 734 } else { 735 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 736 auto &Data = Stack.back().first.back().SharingMap[D]; 737 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 738 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 739 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 740 (isLoopControlVariable(D).first && A == OMPC_private)); 741 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 742 Data.RefExpr.setInt(/*IntVal=*/true); 743 return; 744 } 745 const bool IsLastprivate = 746 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 747 Data.Attributes = A; 748 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 749 Data.PrivateCopy = PrivateCopy; 750 if (PrivateCopy) { 751 auto &Data = Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; 752 Data.Attributes = A; 753 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 754 Data.PrivateCopy = nullptr; 755 } 756 } 757 } 758 759 /// \brief Build a variable declaration for OpenMP loop iteration variable. 760 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 761 StringRef Name, const AttrVec *Attrs = nullptr) { 762 DeclContext *DC = SemaRef.CurContext; 763 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 764 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 765 VarDecl *Decl = 766 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 767 if (Attrs) { 768 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 769 I != E; ++I) 770 Decl->addAttr(*I); 771 } 772 Decl->setImplicit(); 773 return Decl; 774 } 775 776 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 777 SourceLocation Loc, 778 bool RefersToCapture = false) { 779 D->setReferenced(); 780 D->markUsed(S.Context); 781 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 782 SourceLocation(), D, RefersToCapture, Loc, Ty, 783 VK_LValue); 784 } 785 786 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR, 787 BinaryOperatorKind BOK) { 788 D = getCanonicalDecl(D); 789 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 790 assert( 791 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 792 "Additional reduction info may be specified only for reduction items."); 793 auto &ReductionData = Stack.back().first.back().ReductionMap[D]; 794 assert(ReductionData.ReductionRange.isInvalid() && 795 Stack.back().first.back().Directive == OMPD_taskgroup && 796 "Additional reduction info may be specified only once for reduction " 797 "items."); 798 ReductionData.set(BOK, SR); 799 Expr *&TaskgroupReductionRef = 800 Stack.back().first.back().TaskgroupReductionRef; 801 if (!TaskgroupReductionRef) { 802 auto *VD = buildVarDecl(SemaRef, SourceLocation(), 803 SemaRef.Context.VoidPtrTy, ".task_red."); 804 TaskgroupReductionRef = buildDeclRefExpr( 805 SemaRef, VD, SemaRef.Context.VoidPtrTy, SourceLocation()); 806 } 807 } 808 809 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR, 810 const Expr *ReductionRef) { 811 D = getCanonicalDecl(D); 812 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 813 assert( 814 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 815 "Additional reduction info may be specified only for reduction items."); 816 auto &ReductionData = Stack.back().first.back().ReductionMap[D]; 817 assert(ReductionData.ReductionRange.isInvalid() && 818 Stack.back().first.back().Directive == OMPD_taskgroup && 819 "Additional reduction info may be specified only once for reduction " 820 "items."); 821 ReductionData.set(ReductionRef, SR); 822 Expr *&TaskgroupReductionRef = 823 Stack.back().first.back().TaskgroupReductionRef; 824 if (!TaskgroupReductionRef) { 825 auto *VD = buildVarDecl(SemaRef, SourceLocation(), 826 SemaRef.Context.VoidPtrTy, ".task_red."); 827 TaskgroupReductionRef = buildDeclRefExpr( 828 SemaRef, VD, SemaRef.Context.VoidPtrTy, SourceLocation()); 829 } 830 } 831 832 DSAStackTy::DSAVarData 833 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR, 834 BinaryOperatorKind &BOK) { 835 D = getCanonicalDecl(D); 836 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 837 if (Stack.back().first.empty()) 838 return DSAVarData(); 839 for (auto I = std::next(Stack.back().first.rbegin(), 1), 840 E = Stack.back().first.rend(); 841 I != E; std::advance(I, 1)) { 842 auto &Data = I->SharingMap[D]; 843 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 844 continue; 845 auto &ReductionData = I->ReductionMap[D]; 846 if (!ReductionData.ReductionOp || 847 ReductionData.ReductionOp.is<const Expr *>()) 848 return DSAVarData(); 849 SR = ReductionData.ReductionRange; 850 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 851 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 852 Data.PrivateCopy, I->DefaultAttrLoc); 853 } 854 return DSAVarData(); 855 } 856 857 DSAStackTy::DSAVarData 858 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR, 859 const Expr *&ReductionRef) { 860 D = getCanonicalDecl(D); 861 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 862 if (Stack.back().first.empty()) 863 return DSAVarData(); 864 for (auto I = std::next(Stack.back().first.rbegin(), 1), 865 E = Stack.back().first.rend(); 866 I != E; std::advance(I, 1)) { 867 auto &Data = I->SharingMap[D]; 868 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 869 continue; 870 auto &ReductionData = I->ReductionMap[D]; 871 if (!ReductionData.ReductionOp || 872 !ReductionData.ReductionOp.is<const Expr *>()) 873 return DSAVarData(); 874 SR = ReductionData.ReductionRange; 875 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 876 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 877 Data.PrivateCopy, I->DefaultAttrLoc); 878 } 879 return DSAVarData(); 880 } 881 882 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { 883 D = D->getCanonicalDecl(); 884 if (!isStackEmpty() && Stack.back().first.size() > 1) { 885 reverse_iterator I = Iter, E = Stack.back().first.rend(); 886 Scope *TopScope = nullptr; 887 while (I != E && !isParallelOrTaskRegion(I->Directive)) 888 ++I; 889 if (I == E) 890 return false; 891 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 892 Scope *CurScope = getCurScope(); 893 while (CurScope != TopScope && !CurScope->isDeclScope(D)) 894 CurScope = CurScope->getParent(); 895 return CurScope != TopScope; 896 } 897 return false; 898 } 899 900 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) { 901 D = getCanonicalDecl(D); 902 DSAVarData DVar; 903 904 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 905 // in a Construct, C/C++, predetermined, p.1] 906 // Variables appearing in threadprivate directives are threadprivate. 907 auto *VD = dyn_cast<VarDecl>(D); 908 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 909 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 910 SemaRef.getLangOpts().OpenMPUseTLS && 911 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 912 (VD && VD->getStorageClass() == SC_Register && 913 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 914 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 915 D->getLocation()), 916 OMPC_threadprivate); 917 } 918 auto TI = Threadprivates.find(D); 919 if (TI != Threadprivates.end()) { 920 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 921 DVar.CKind = OMPC_threadprivate; 922 return DVar; 923 } 924 925 if (isStackEmpty()) 926 // Not in OpenMP execution region and top scope was already checked. 927 return DVar; 928 929 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 930 // in a Construct, C/C++, predetermined, p.4] 931 // Static data members are shared. 932 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 933 // in a Construct, C/C++, predetermined, p.7] 934 // Variables with static storage duration that are declared in a scope 935 // inside the construct are shared. 936 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; }; 937 if (VD && VD->isStaticDataMember()) { 938 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 939 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 940 return DVar; 941 942 DVar.CKind = OMPC_shared; 943 return DVar; 944 } 945 946 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 947 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 948 Type = SemaRef.getASTContext().getBaseElementType(Type); 949 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 950 // in a Construct, C/C++, predetermined, p.6] 951 // Variables with const qualified type having no mutable member are 952 // shared. 953 CXXRecordDecl *RD = 954 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 955 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 956 if (auto *CTD = CTSD->getSpecializedTemplate()) 957 RD = CTD->getTemplatedDecl(); 958 if (IsConstant && 959 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && 960 RD->hasMutableFields())) { 961 // Variables with const-qualified type having no mutable member may be 962 // listed in a firstprivate clause, even if they are static data members. 963 DSAVarData DVarTemp = hasDSA( 964 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; }, 965 MatchesAlways, FromParent); 966 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 967 return DVar; 968 969 DVar.CKind = OMPC_shared; 970 return DVar; 971 } 972 973 // Explicitly specified attributes and local variables with predetermined 974 // attributes. 975 auto I = Stack.back().first.rbegin(); 976 auto EndI = Stack.back().first.rend(); 977 if (FromParent && I != EndI) 978 std::advance(I, 1); 979 if (I->SharingMap.count(D)) { 980 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer(); 981 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy; 982 DVar.CKind = I->SharingMap[D].Attributes; 983 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 984 DVar.DKind = I->Directive; 985 } 986 987 return DVar; 988 } 989 990 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 991 bool FromParent) { 992 if (isStackEmpty()) { 993 StackTy::reverse_iterator I; 994 return getDSA(I, D); 995 } 996 D = getCanonicalDecl(D); 997 auto StartI = Stack.back().first.rbegin(); 998 auto EndI = Stack.back().first.rend(); 999 if (FromParent && StartI != EndI) 1000 std::advance(StartI, 1); 1001 return getDSA(StartI, D); 1002 } 1003 1004 DSAStackTy::DSAVarData 1005 DSAStackTy::hasDSA(ValueDecl *D, 1006 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 1007 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 1008 bool FromParent) { 1009 if (isStackEmpty()) 1010 return {}; 1011 D = getCanonicalDecl(D); 1012 auto I = Stack.back().first.rbegin(); 1013 auto EndI = Stack.back().first.rend(); 1014 if (FromParent && I != EndI) 1015 std::advance(I, 1); 1016 for (; I != EndI; std::advance(I, 1)) { 1017 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 1018 continue; 1019 auto NewI = I; 1020 DSAVarData DVar = getDSA(NewI, D); 1021 if (I == NewI && CPred(DVar.CKind)) 1022 return DVar; 1023 } 1024 return {}; 1025 } 1026 1027 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1028 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 1029 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 1030 bool FromParent) { 1031 if (isStackEmpty()) 1032 return {}; 1033 D = getCanonicalDecl(D); 1034 auto StartI = Stack.back().first.rbegin(); 1035 auto EndI = Stack.back().first.rend(); 1036 if (FromParent && StartI != EndI) 1037 std::advance(StartI, 1); 1038 if (StartI == EndI || !DPred(StartI->Directive)) 1039 return {}; 1040 auto NewI = StartI; 1041 DSAVarData DVar = getDSA(NewI, D); 1042 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1043 } 1044 1045 bool DSAStackTy::hasExplicitDSA( 1046 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 1047 unsigned Level, bool NotLastprivate) { 1048 if (CPred(ClauseKindMode)) 1049 return true; 1050 if (isStackEmpty()) 1051 return false; 1052 D = getCanonicalDecl(D); 1053 auto StartI = Stack.back().first.begin(); 1054 auto EndI = Stack.back().first.end(); 1055 if (std::distance(StartI, EndI) <= (int)Level) 1056 return false; 1057 std::advance(StartI, Level); 1058 return (StartI->SharingMap.count(D) > 0) && 1059 StartI->SharingMap[D].RefExpr.getPointer() && 1060 CPred(StartI->SharingMap[D].Attributes) && 1061 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt()); 1062 } 1063 1064 bool DSAStackTy::hasExplicitDirective( 1065 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 1066 unsigned Level) { 1067 if (isStackEmpty()) 1068 return false; 1069 auto StartI = Stack.back().first.begin(); 1070 auto EndI = Stack.back().first.end(); 1071 if (std::distance(StartI, EndI) <= (int)Level) 1072 return false; 1073 std::advance(StartI, Level); 1074 return DPred(StartI->Directive); 1075 } 1076 1077 bool DSAStackTy::hasDirective( 1078 const llvm::function_ref<bool(OpenMPDirectiveKind, 1079 const DeclarationNameInfo &, SourceLocation)> 1080 &DPred, 1081 bool FromParent) { 1082 // We look only in the enclosing region. 1083 if (isStackEmpty()) 1084 return false; 1085 auto StartI = std::next(Stack.back().first.rbegin()); 1086 auto EndI = Stack.back().first.rend(); 1087 if (FromParent && StartI != EndI) 1088 StartI = std::next(StartI); 1089 for (auto I = StartI, EE = EndI; I != EE; ++I) { 1090 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1091 return true; 1092 } 1093 return false; 1094 } 1095 1096 void Sema::InitDataSharingAttributesStack() { 1097 VarDataSharingAttributesStack = new DSAStackTy(*this); 1098 } 1099 1100 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1101 1102 void Sema::pushOpenMPFunctionRegion() { 1103 DSAStack->pushFunction(); 1104 } 1105 1106 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1107 DSAStack->popFunction(OldFSI); 1108 } 1109 1110 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) { 1111 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1112 1113 auto &Ctx = getASTContext(); 1114 bool IsByRef = true; 1115 1116 // Find the directive that is associated with the provided scope. 1117 auto Ty = D->getType(); 1118 1119 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1120 // This table summarizes how a given variable should be passed to the device 1121 // given its type and the clauses where it appears. This table is based on 1122 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1123 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1124 // 1125 // ========================================================================= 1126 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1127 // | |(tofrom:scalar)| | pvt | | | | 1128 // ========================================================================= 1129 // | scl | | | | - | | bycopy| 1130 // | scl | | - | x | - | - | bycopy| 1131 // | scl | | x | - | - | - | null | 1132 // | scl | x | | | - | | byref | 1133 // | scl | x | - | x | - | - | bycopy| 1134 // | scl | x | x | - | - | - | null | 1135 // | scl | | - | - | - | x | byref | 1136 // | scl | x | - | - | - | x | byref | 1137 // 1138 // | agg | n.a. | | | - | | byref | 1139 // | agg | n.a. | - | x | - | - | byref | 1140 // | agg | n.a. | x | - | - | - | null | 1141 // | agg | n.a. | - | - | - | x | byref | 1142 // | agg | n.a. | - | - | - | x[] | byref | 1143 // 1144 // | ptr | n.a. | | | - | | bycopy| 1145 // | ptr | n.a. | - | x | - | - | bycopy| 1146 // | ptr | n.a. | x | - | - | - | null | 1147 // | ptr | n.a. | - | - | - | x | byref | 1148 // | ptr | n.a. | - | - | - | x[] | bycopy| 1149 // | ptr | n.a. | - | - | x | | bycopy| 1150 // | ptr | n.a. | - | - | x | x | bycopy| 1151 // | ptr | n.a. | - | - | x | x[] | bycopy| 1152 // ========================================================================= 1153 // Legend: 1154 // scl - scalar 1155 // ptr - pointer 1156 // agg - aggregate 1157 // x - applies 1158 // - - invalid in this combination 1159 // [] - mapped with an array section 1160 // byref - should be mapped by reference 1161 // byval - should be mapped by value 1162 // null - initialize a local variable to null on the device 1163 // 1164 // Observations: 1165 // - All scalar declarations that show up in a map clause have to be passed 1166 // by reference, because they may have been mapped in the enclosing data 1167 // environment. 1168 // - If the scalar value does not fit the size of uintptr, it has to be 1169 // passed by reference, regardless the result in the table above. 1170 // - For pointers mapped by value that have either an implicit map or an 1171 // array section, the runtime library may pass the NULL value to the 1172 // device instead of the value passed to it by the compiler. 1173 1174 if (Ty->isReferenceType()) 1175 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1176 1177 // Locate map clauses and see if the variable being captured is referred to 1178 // in any of those clauses. Here we only care about variables, not fields, 1179 // because fields are part of aggregates. 1180 bool IsVariableUsedInMapClause = false; 1181 bool IsVariableAssociatedWithSection = false; 1182 1183 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1184 D, Level, [&](OMPClauseMappableExprCommon::MappableExprComponentListRef 1185 MapExprComponents, 1186 OpenMPClauseKind WhereFoundClauseKind) { 1187 // Only the map clause information influences how a variable is 1188 // captured. E.g. is_device_ptr does not require changing the default 1189 // behavior. 1190 if (WhereFoundClauseKind != OMPC_map) 1191 return false; 1192 1193 auto EI = MapExprComponents.rbegin(); 1194 auto EE = MapExprComponents.rend(); 1195 1196 assert(EI != EE && "Invalid map expression!"); 1197 1198 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1199 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1200 1201 ++EI; 1202 if (EI == EE) 1203 return false; 1204 1205 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1206 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1207 isa<MemberExpr>(EI->getAssociatedExpression())) { 1208 IsVariableAssociatedWithSection = true; 1209 // There is nothing more we need to know about this variable. 1210 return true; 1211 } 1212 1213 // Keep looking for more map info. 1214 return false; 1215 }); 1216 1217 if (IsVariableUsedInMapClause) { 1218 // If variable is identified in a map clause it is always captured by 1219 // reference except if it is a pointer that is dereferenced somehow. 1220 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1221 } else { 1222 // By default, all the data that has a scalar type is mapped by copy. 1223 IsByRef = !Ty->isScalarType(); 1224 } 1225 } 1226 1227 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1228 IsByRef = !DSAStack->hasExplicitDSA( 1229 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1230 Level, /*NotLastprivate=*/true); 1231 } 1232 1233 // When passing data by copy, we need to make sure it fits the uintptr size 1234 // and alignment, because the runtime library only deals with uintptr types. 1235 // If it does not fit the uintptr size, we need to pass the data by reference 1236 // instead. 1237 if (!IsByRef && 1238 (Ctx.getTypeSizeInChars(Ty) > 1239 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1240 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1241 IsByRef = true; 1242 } 1243 1244 return IsByRef; 1245 } 1246 1247 unsigned Sema::getOpenMPNestingLevel() const { 1248 assert(getLangOpts().OpenMP); 1249 return DSAStack->getNestingLevel(); 1250 } 1251 1252 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) { 1253 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1254 D = getCanonicalDecl(D); 1255 1256 // If we are attempting to capture a global variable in a directive with 1257 // 'target' we return true so that this global is also mapped to the device. 1258 // 1259 // FIXME: If the declaration is enclosed in a 'declare target' directive, 1260 // then it should not be captured. Therefore, an extra check has to be 1261 // inserted here once support for 'declare target' is added. 1262 // 1263 auto *VD = dyn_cast<VarDecl>(D); 1264 if (VD && !VD->hasLocalStorage()) { 1265 if (DSAStack->getCurrentDirective() == OMPD_target && 1266 !DSAStack->isClauseParsingMode()) 1267 return VD; 1268 if (DSAStack->hasDirective( 1269 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1270 SourceLocation) -> bool { 1271 return isOpenMPTargetExecutionDirective(K); 1272 }, 1273 false)) 1274 return VD; 1275 } 1276 1277 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1278 (!DSAStack->isClauseParsingMode() || 1279 DSAStack->getParentDirective() != OMPD_unknown)) { 1280 auto &&Info = DSAStack->isLoopControlVariable(D); 1281 if (Info.first || 1282 (VD && VD->hasLocalStorage() && 1283 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 1284 (VD && DSAStack->isForceVarCapturing())) 1285 return VD ? VD : Info.second; 1286 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1287 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1288 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1289 DVarPrivate = DSAStack->hasDSA( 1290 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, 1291 DSAStack->isClauseParsingMode()); 1292 if (DVarPrivate.CKind != OMPC_unknown) 1293 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1294 } 1295 return nullptr; 1296 } 1297 1298 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) { 1299 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1300 return DSAStack->hasExplicitDSA( 1301 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level); 1302 } 1303 1304 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) { 1305 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1306 // Return true if the current level is no longer enclosed in a target region. 1307 1308 auto *VD = dyn_cast<VarDecl>(D); 1309 return VD && !VD->hasLocalStorage() && 1310 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1311 Level); 1312 } 1313 1314 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1315 1316 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1317 const DeclarationNameInfo &DirName, 1318 Scope *CurScope, SourceLocation Loc) { 1319 DSAStack->push(DKind, DirName, CurScope, Loc); 1320 PushExpressionEvaluationContext( 1321 ExpressionEvaluationContext::PotentiallyEvaluated); 1322 } 1323 1324 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1325 DSAStack->setClauseParsingMode(K); 1326 } 1327 1328 void Sema::EndOpenMPClause() { 1329 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1330 } 1331 1332 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1333 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1334 // A variable of class type (or array thereof) that appears in a lastprivate 1335 // clause requires an accessible, unambiguous default constructor for the 1336 // class type, unless the list item is also specified in a firstprivate 1337 // clause. 1338 if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1339 for (auto *C : D->clauses()) { 1340 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1341 SmallVector<Expr *, 8> PrivateCopies; 1342 for (auto *DE : Clause->varlists()) { 1343 if (DE->isValueDependent() || DE->isTypeDependent()) { 1344 PrivateCopies.push_back(nullptr); 1345 continue; 1346 } 1347 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1348 VarDecl *VD = cast<VarDecl>(DRE->getDecl()); 1349 QualType Type = VD->getType().getNonReferenceType(); 1350 auto DVar = DSAStack->getTopDSA(VD, false); 1351 if (DVar.CKind == OMPC_lastprivate) { 1352 // Generate helper private variable and initialize it with the 1353 // default value. The address of the original variable is replaced 1354 // by the address of the new private variable in CodeGen. This new 1355 // variable is not added to IdResolver, so the code in the OpenMP 1356 // region uses original variable for proper diagnostics. 1357 auto *VDPrivate = buildVarDecl( 1358 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1359 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr); 1360 ActOnUninitializedDecl(VDPrivate); 1361 if (VDPrivate->isInvalidDecl()) 1362 continue; 1363 PrivateCopies.push_back(buildDeclRefExpr( 1364 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1365 } else { 1366 // The variable is also a firstprivate, so initialization sequence 1367 // for private copy is generated already. 1368 PrivateCopies.push_back(nullptr); 1369 } 1370 } 1371 // Set initializers to private copies if no errors were found. 1372 if (PrivateCopies.size() == Clause->varlist_size()) 1373 Clause->setPrivateCopies(PrivateCopies); 1374 } 1375 } 1376 } 1377 1378 DSAStack->pop(); 1379 DiscardCleanupsInEvaluationContext(); 1380 PopExpressionEvaluationContext(); 1381 } 1382 1383 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1384 Expr *NumIterations, Sema &SemaRef, 1385 Scope *S, DSAStackTy *Stack); 1386 1387 namespace { 1388 1389 class VarDeclFilterCCC : public CorrectionCandidateCallback { 1390 private: 1391 Sema &SemaRef; 1392 1393 public: 1394 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1395 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1396 NamedDecl *ND = Candidate.getCorrectionDecl(); 1397 if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1398 return VD->hasGlobalStorage() && 1399 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1400 SemaRef.getCurScope()); 1401 } 1402 return false; 1403 } 1404 }; 1405 1406 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback { 1407 private: 1408 Sema &SemaRef; 1409 1410 public: 1411 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1412 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1413 NamedDecl *ND = Candidate.getCorrectionDecl(); 1414 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 1415 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1416 SemaRef.getCurScope()); 1417 } 1418 return false; 1419 } 1420 }; 1421 1422 } // namespace 1423 1424 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1425 CXXScopeSpec &ScopeSpec, 1426 const DeclarationNameInfo &Id) { 1427 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1428 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1429 1430 if (Lookup.isAmbiguous()) 1431 return ExprError(); 1432 1433 VarDecl *VD; 1434 if (!Lookup.isSingleResult()) { 1435 if (TypoCorrection Corrected = CorrectTypo( 1436 Id, LookupOrdinaryName, CurScope, nullptr, 1437 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1438 diagnoseTypo(Corrected, 1439 PDiag(Lookup.empty() 1440 ? diag::err_undeclared_var_use_suggest 1441 : diag::err_omp_expected_var_arg_suggest) 1442 << Id.getName()); 1443 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1444 } else { 1445 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1446 : diag::err_omp_expected_var_arg) 1447 << Id.getName(); 1448 return ExprError(); 1449 } 1450 } else { 1451 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1452 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1453 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1454 return ExprError(); 1455 } 1456 } 1457 Lookup.suppressDiagnostics(); 1458 1459 // OpenMP [2.9.2, Syntax, C/C++] 1460 // Variables must be file-scope, namespace-scope, or static block-scope. 1461 if (!VD->hasGlobalStorage()) { 1462 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1463 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1464 bool IsDecl = 1465 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1466 Diag(VD->getLocation(), 1467 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1468 << VD; 1469 return ExprError(); 1470 } 1471 1472 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1473 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 1474 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1475 // A threadprivate directive for file-scope variables must appear outside 1476 // any definition or declaration. 1477 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1478 !getCurLexicalContext()->isTranslationUnit()) { 1479 Diag(Id.getLoc(), diag::err_omp_var_scope) 1480 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1481 bool IsDecl = 1482 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1483 Diag(VD->getLocation(), 1484 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1485 << VD; 1486 return ExprError(); 1487 } 1488 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1489 // A threadprivate directive for static class member variables must appear 1490 // in the class definition, in the same scope in which the member 1491 // variables are declared. 1492 if (CanonicalVD->isStaticDataMember() && 1493 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1494 Diag(Id.getLoc(), diag::err_omp_var_scope) 1495 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1496 bool IsDecl = 1497 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1498 Diag(VD->getLocation(), 1499 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1500 << VD; 1501 return ExprError(); 1502 } 1503 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1504 // A threadprivate directive for namespace-scope variables must appear 1505 // outside any definition or declaration other than the namespace 1506 // definition itself. 1507 if (CanonicalVD->getDeclContext()->isNamespace() && 1508 (!getCurLexicalContext()->isFileContext() || 1509 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1510 Diag(Id.getLoc(), diag::err_omp_var_scope) 1511 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1512 bool IsDecl = 1513 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1514 Diag(VD->getLocation(), 1515 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1516 << VD; 1517 return ExprError(); 1518 } 1519 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1520 // A threadprivate directive for static block-scope variables must appear 1521 // in the scope of the variable and not in a nested scope. 1522 if (CanonicalVD->isStaticLocal() && CurScope && 1523 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1524 Diag(Id.getLoc(), diag::err_omp_var_scope) 1525 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1526 bool IsDecl = 1527 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1528 Diag(VD->getLocation(), 1529 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1530 << VD; 1531 return ExprError(); 1532 } 1533 1534 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1535 // A threadprivate directive must lexically precede all references to any 1536 // of the variables in its list. 1537 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1538 Diag(Id.getLoc(), diag::err_omp_var_used) 1539 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1540 return ExprError(); 1541 } 1542 1543 QualType ExprType = VD->getType().getNonReferenceType(); 1544 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 1545 SourceLocation(), VD, 1546 /*RefersToEnclosingVariableOrCapture=*/false, 1547 Id.getLoc(), ExprType, VK_LValue); 1548 } 1549 1550 Sema::DeclGroupPtrTy 1551 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1552 ArrayRef<Expr *> VarList) { 1553 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1554 CurContext->addDecl(D); 1555 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1556 } 1557 return nullptr; 1558 } 1559 1560 namespace { 1561 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1562 Sema &SemaRef; 1563 1564 public: 1565 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1566 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1567 if (VD->hasLocalStorage()) { 1568 SemaRef.Diag(E->getLocStart(), 1569 diag::err_omp_local_var_in_threadprivate_init) 1570 << E->getSourceRange(); 1571 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1572 << VD << VD->getSourceRange(); 1573 return true; 1574 } 1575 } 1576 return false; 1577 } 1578 bool VisitStmt(const Stmt *S) { 1579 for (auto Child : S->children()) { 1580 if (Child && Visit(Child)) 1581 return true; 1582 } 1583 return false; 1584 } 1585 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1586 }; 1587 } // namespace 1588 1589 OMPThreadPrivateDecl * 1590 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1591 SmallVector<Expr *, 8> Vars; 1592 for (auto &RefExpr : VarList) { 1593 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); 1594 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 1595 SourceLocation ILoc = DE->getExprLoc(); 1596 1597 // Mark variable as used. 1598 VD->setReferenced(); 1599 VD->markUsed(Context); 1600 1601 QualType QType = VD->getType(); 1602 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1603 // It will be analyzed later. 1604 Vars.push_back(DE); 1605 continue; 1606 } 1607 1608 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1609 // A threadprivate variable must not have an incomplete type. 1610 if (RequireCompleteType(ILoc, VD->getType(), 1611 diag::err_omp_threadprivate_incomplete_type)) { 1612 continue; 1613 } 1614 1615 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1616 // A threadprivate variable must not have a reference type. 1617 if (VD->getType()->isReferenceType()) { 1618 Diag(ILoc, diag::err_omp_ref_type_arg) 1619 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1620 bool IsDecl = 1621 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1622 Diag(VD->getLocation(), 1623 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1624 << VD; 1625 continue; 1626 } 1627 1628 // Check if this is a TLS variable. If TLS is not being supported, produce 1629 // the corresponding diagnostic. 1630 if ((VD->getTLSKind() != VarDecl::TLS_None && 1631 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1632 getLangOpts().OpenMPUseTLS && 1633 getASTContext().getTargetInfo().isTLSSupported())) || 1634 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1635 !VD->isLocalVarDecl())) { 1636 Diag(ILoc, diag::err_omp_var_thread_local) 1637 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1638 bool IsDecl = 1639 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1640 Diag(VD->getLocation(), 1641 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1642 << VD; 1643 continue; 1644 } 1645 1646 // Check if initial value of threadprivate variable reference variable with 1647 // local storage (it is not supported by runtime). 1648 if (auto Init = VD->getAnyInitializer()) { 1649 LocalVarRefChecker Checker(*this); 1650 if (Checker.Visit(Init)) 1651 continue; 1652 } 1653 1654 Vars.push_back(RefExpr); 1655 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 1656 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 1657 Context, SourceRange(Loc, Loc))); 1658 if (auto *ML = Context.getASTMutationListener()) 1659 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 1660 } 1661 OMPThreadPrivateDecl *D = nullptr; 1662 if (!Vars.empty()) { 1663 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 1664 Vars); 1665 D->setAccess(AS_public); 1666 } 1667 return D; 1668 } 1669 1670 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, 1671 const ValueDecl *D, DSAStackTy::DSAVarData DVar, 1672 bool IsLoopIterVar = false) { 1673 if (DVar.RefExpr) { 1674 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1675 << getOpenMPClauseName(DVar.CKind); 1676 return; 1677 } 1678 enum { 1679 PDSA_StaticMemberShared, 1680 PDSA_StaticLocalVarShared, 1681 PDSA_LoopIterVarPrivate, 1682 PDSA_LoopIterVarLinear, 1683 PDSA_LoopIterVarLastprivate, 1684 PDSA_ConstVarShared, 1685 PDSA_GlobalVarShared, 1686 PDSA_TaskVarFirstprivate, 1687 PDSA_LocalVarPrivate, 1688 PDSA_Implicit 1689 } Reason = PDSA_Implicit; 1690 bool ReportHint = false; 1691 auto ReportLoc = D->getLocation(); 1692 auto *VD = dyn_cast<VarDecl>(D); 1693 if (IsLoopIterVar) { 1694 if (DVar.CKind == OMPC_private) 1695 Reason = PDSA_LoopIterVarPrivate; 1696 else if (DVar.CKind == OMPC_lastprivate) 1697 Reason = PDSA_LoopIterVarLastprivate; 1698 else 1699 Reason = PDSA_LoopIterVarLinear; 1700 } else if (isOpenMPTaskingDirective(DVar.DKind) && 1701 DVar.CKind == OMPC_firstprivate) { 1702 Reason = PDSA_TaskVarFirstprivate; 1703 ReportLoc = DVar.ImplicitDSALoc; 1704 } else if (VD && VD->isStaticLocal()) 1705 Reason = PDSA_StaticLocalVarShared; 1706 else if (VD && VD->isStaticDataMember()) 1707 Reason = PDSA_StaticMemberShared; 1708 else if (VD && VD->isFileVarDecl()) 1709 Reason = PDSA_GlobalVarShared; 1710 else if (D->getType().isConstant(SemaRef.getASTContext())) 1711 Reason = PDSA_ConstVarShared; 1712 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 1713 ReportHint = true; 1714 Reason = PDSA_LocalVarPrivate; 1715 } 1716 if (Reason != PDSA_Implicit) { 1717 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 1718 << Reason << ReportHint 1719 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 1720 } else if (DVar.ImplicitDSALoc.isValid()) { 1721 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 1722 << getOpenMPClauseName(DVar.CKind); 1723 } 1724 } 1725 1726 namespace { 1727 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 1728 DSAStackTy *Stack; 1729 Sema &SemaRef; 1730 bool ErrorFound; 1731 CapturedStmt *CS; 1732 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; 1733 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; 1734 1735 public: 1736 void VisitDeclRefExpr(DeclRefExpr *E) { 1737 if (E->isTypeDependent() || E->isValueDependent() || 1738 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1739 return; 1740 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1741 // Skip internally declared variables. 1742 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) 1743 return; 1744 1745 auto DVar = Stack->getTopDSA(VD, false); 1746 // Check if the variable has explicit DSA set and stop analysis if it so. 1747 if (DVar.RefExpr) 1748 return; 1749 1750 auto ELoc = E->getExprLoc(); 1751 auto DKind = Stack->getCurrentDirective(); 1752 // The default(none) clause requires that each variable that is referenced 1753 // in the construct, and does not have a predetermined data-sharing 1754 // attribute, must have its data-sharing attribute explicitly determined 1755 // by being listed in a data-sharing attribute clause. 1756 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 1757 isParallelOrTaskRegion(DKind) && 1758 VarsWithInheritedDSA.count(VD) == 0) { 1759 VarsWithInheritedDSA[VD] = E; 1760 return; 1761 } 1762 1763 // OpenMP [2.9.3.6, Restrictions, p.2] 1764 // A list item that appears in a reduction clause of the innermost 1765 // enclosing worksharing or parallel construct may not be accessed in an 1766 // explicit task. 1767 DVar = Stack->hasInnermostDSA( 1768 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 1769 [](OpenMPDirectiveKind K) -> bool { 1770 return isOpenMPParallelDirective(K) || 1771 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 1772 }, 1773 /*FromParent=*/true); 1774 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 1775 ErrorFound = true; 1776 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 1777 ReportOriginalDSA(SemaRef, Stack, VD, DVar); 1778 return; 1779 } 1780 1781 // Define implicit data-sharing attributes for task. 1782 DVar = Stack->getImplicitDSA(VD, false); 1783 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 1784 !Stack->isLoopControlVariable(VD).first) 1785 ImplicitFirstprivate.push_back(E); 1786 } 1787 } 1788 void VisitMemberExpr(MemberExpr *E) { 1789 if (E->isTypeDependent() || E->isValueDependent() || 1790 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1791 return; 1792 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 1793 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { 1794 auto DVar = Stack->getTopDSA(FD, false); 1795 // Check if the variable has explicit DSA set and stop analysis if it 1796 // so. 1797 if (DVar.RefExpr) 1798 return; 1799 1800 auto ELoc = E->getExprLoc(); 1801 auto DKind = Stack->getCurrentDirective(); 1802 // OpenMP [2.9.3.6, Restrictions, p.2] 1803 // A list item that appears in a reduction clause of the innermost 1804 // enclosing worksharing or parallel construct may not be accessed in 1805 // an explicit task. 1806 DVar = Stack->hasInnermostDSA( 1807 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 1808 [](OpenMPDirectiveKind K) -> bool { 1809 return isOpenMPParallelDirective(K) || 1810 isOpenMPWorksharingDirective(K) || 1811 isOpenMPTeamsDirective(K); 1812 }, 1813 /*FromParent=*/true); 1814 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 1815 ErrorFound = true; 1816 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 1817 ReportOriginalDSA(SemaRef, Stack, FD, DVar); 1818 return; 1819 } 1820 1821 // Define implicit data-sharing attributes for task. 1822 DVar = Stack->getImplicitDSA(FD, false); 1823 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 1824 !Stack->isLoopControlVariable(FD).first) 1825 ImplicitFirstprivate.push_back(E); 1826 } 1827 } else 1828 Visit(E->getBase()); 1829 } 1830 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 1831 for (auto *C : S->clauses()) { 1832 // Skip analysis of arguments of implicitly defined firstprivate clause 1833 // for task directives. 1834 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) 1835 for (auto *CC : C->children()) { 1836 if (CC) 1837 Visit(CC); 1838 } 1839 } 1840 } 1841 void VisitStmt(Stmt *S) { 1842 for (auto *C : S->children()) { 1843 if (C && !isa<OMPExecutableDirective>(C)) 1844 Visit(C); 1845 } 1846 } 1847 1848 bool isErrorFound() { return ErrorFound; } 1849 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } 1850 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() { 1851 return VarsWithInheritedDSA; 1852 } 1853 1854 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 1855 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 1856 }; 1857 } // namespace 1858 1859 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 1860 switch (DKind) { 1861 case OMPD_parallel: 1862 case OMPD_parallel_for: 1863 case OMPD_parallel_for_simd: 1864 case OMPD_parallel_sections: 1865 case OMPD_teams: { 1866 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1867 QualType KmpInt32PtrTy = 1868 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1869 Sema::CapturedParamNameType Params[] = { 1870 std::make_pair(".global_tid.", KmpInt32PtrTy), 1871 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1872 std::make_pair(StringRef(), QualType()) // __context with shared vars 1873 }; 1874 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1875 Params); 1876 break; 1877 } 1878 case OMPD_target_teams: 1879 case OMPD_target_parallel: { 1880 Sema::CapturedParamNameType ParamsTarget[] = { 1881 std::make_pair(StringRef(), QualType()) // __context with shared vars 1882 }; 1883 // Start a captured region for 'target' with no implicit parameters. 1884 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1885 ParamsTarget); 1886 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1887 QualType KmpInt32PtrTy = 1888 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1889 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 1890 std::make_pair(".global_tid.", KmpInt32PtrTy), 1891 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1892 std::make_pair(StringRef(), QualType()) // __context with shared vars 1893 }; 1894 // Start a captured region for 'teams' or 'parallel'. Both regions have 1895 // the same implicit parameters. 1896 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1897 ParamsTeamsOrParallel); 1898 break; 1899 } 1900 case OMPD_simd: 1901 case OMPD_for: 1902 case OMPD_for_simd: 1903 case OMPD_sections: 1904 case OMPD_section: 1905 case OMPD_single: 1906 case OMPD_master: 1907 case OMPD_critical: 1908 case OMPD_taskgroup: 1909 case OMPD_distribute: 1910 case OMPD_ordered: 1911 case OMPD_atomic: 1912 case OMPD_target_data: 1913 case OMPD_target: 1914 case OMPD_target_parallel_for: 1915 case OMPD_target_parallel_for_simd: 1916 case OMPD_target_simd: { 1917 Sema::CapturedParamNameType Params[] = { 1918 std::make_pair(StringRef(), QualType()) // __context with shared vars 1919 }; 1920 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1921 Params); 1922 break; 1923 } 1924 case OMPD_task: { 1925 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1926 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; 1927 FunctionProtoType::ExtProtoInfo EPI; 1928 EPI.Variadic = true; 1929 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 1930 Sema::CapturedParamNameType Params[] = { 1931 std::make_pair(".global_tid.", KmpInt32Ty), 1932 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), 1933 std::make_pair(".privates.", Context.VoidPtrTy.withConst()), 1934 std::make_pair(".copy_fn.", 1935 Context.getPointerType(CopyFnType).withConst()), 1936 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 1937 std::make_pair(StringRef(), QualType()) // __context with shared vars 1938 }; 1939 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1940 Params); 1941 // Mark this captured region as inlined, because we don't use outlined 1942 // function directly. 1943 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1944 AlwaysInlineAttr::CreateImplicit( 1945 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1946 break; 1947 } 1948 case OMPD_taskloop: 1949 case OMPD_taskloop_simd: { 1950 QualType KmpInt32Ty = 1951 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); 1952 QualType KmpUInt64Ty = 1953 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 1954 QualType KmpInt64Ty = 1955 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 1956 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; 1957 FunctionProtoType::ExtProtoInfo EPI; 1958 EPI.Variadic = true; 1959 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 1960 Sema::CapturedParamNameType Params[] = { 1961 std::make_pair(".global_tid.", KmpInt32Ty), 1962 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), 1963 std::make_pair(".privates.", 1964 Context.VoidPtrTy.withConst().withRestrict()), 1965 std::make_pair( 1966 ".copy_fn.", 1967 Context.getPointerType(CopyFnType).withConst().withRestrict()), 1968 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 1969 std::make_pair(".lb.", KmpUInt64Ty), 1970 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty), 1971 std::make_pair(".liter.", KmpInt32Ty), 1972 std::make_pair(".reductions.", 1973 Context.VoidPtrTy.withConst().withRestrict()), 1974 std::make_pair(StringRef(), QualType()) // __context with shared vars 1975 }; 1976 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1977 Params); 1978 // Mark this captured region as inlined, because we don't use outlined 1979 // function directly. 1980 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1981 AlwaysInlineAttr::CreateImplicit( 1982 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1983 break; 1984 } 1985 case OMPD_distribute_parallel_for_simd: 1986 case OMPD_distribute_simd: 1987 case OMPD_distribute_parallel_for: 1988 case OMPD_teams_distribute: 1989 case OMPD_teams_distribute_simd: 1990 case OMPD_teams_distribute_parallel_for_simd: 1991 case OMPD_teams_distribute_parallel_for: 1992 case OMPD_target_teams_distribute: 1993 case OMPD_target_teams_distribute_parallel_for: 1994 case OMPD_target_teams_distribute_parallel_for_simd: 1995 case OMPD_target_teams_distribute_simd: { 1996 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1997 QualType KmpInt32PtrTy = 1998 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1999 Sema::CapturedParamNameType Params[] = { 2000 std::make_pair(".global_tid.", KmpInt32PtrTy), 2001 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2002 std::make_pair(".previous.lb.", Context.getSizeType()), 2003 std::make_pair(".previous.ub.", Context.getSizeType()), 2004 std::make_pair(StringRef(), QualType()) // __context with shared vars 2005 }; 2006 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2007 Params); 2008 break; 2009 } 2010 case OMPD_threadprivate: 2011 case OMPD_taskyield: 2012 case OMPD_barrier: 2013 case OMPD_taskwait: 2014 case OMPD_cancellation_point: 2015 case OMPD_cancel: 2016 case OMPD_flush: 2017 case OMPD_target_enter_data: 2018 case OMPD_target_exit_data: 2019 case OMPD_declare_reduction: 2020 case OMPD_declare_simd: 2021 case OMPD_declare_target: 2022 case OMPD_end_declare_target: 2023 case OMPD_target_update: 2024 llvm_unreachable("OpenMP Directive is not allowed"); 2025 case OMPD_unknown: 2026 llvm_unreachable("Unknown OpenMP directive"); 2027 } 2028 } 2029 2030 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 2031 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2032 getOpenMPCaptureRegions(CaptureRegions, DKind); 2033 return CaptureRegions.size(); 2034 } 2035 2036 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 2037 Expr *CaptureExpr, bool WithInit, 2038 bool AsExpression) { 2039 assert(CaptureExpr); 2040 ASTContext &C = S.getASTContext(); 2041 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 2042 QualType Ty = Init->getType(); 2043 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 2044 if (S.getLangOpts().CPlusPlus) 2045 Ty = C.getLValueReferenceType(Ty); 2046 else { 2047 Ty = C.getPointerType(Ty); 2048 ExprResult Res = 2049 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 2050 if (!Res.isUsable()) 2051 return nullptr; 2052 Init = Res.get(); 2053 } 2054 WithInit = true; 2055 } 2056 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 2057 CaptureExpr->getLocStart()); 2058 if (!WithInit) 2059 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange())); 2060 S.CurContext->addHiddenDecl(CED); 2061 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 2062 return CED; 2063 } 2064 2065 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2066 bool WithInit) { 2067 OMPCapturedExprDecl *CD; 2068 if (auto *VD = S.IsOpenMPCapturedDecl(D)) 2069 CD = cast<OMPCapturedExprDecl>(VD); 2070 else 2071 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 2072 /*AsExpression=*/false); 2073 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2074 CaptureExpr->getExprLoc()); 2075 } 2076 2077 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 2078 if (!Ref) { 2079 auto *CD = 2080 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."), 2081 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true); 2082 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2083 CaptureExpr->getExprLoc()); 2084 } 2085 ExprResult Res = Ref; 2086 if (!S.getLangOpts().CPlusPlus && 2087 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 2088 Ref->getType()->isPointerType()) 2089 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 2090 if (!Res.isUsable()) 2091 return ExprError(); 2092 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get()); 2093 } 2094 2095 namespace { 2096 // OpenMP directives parsed in this section are represented as a 2097 // CapturedStatement with an associated statement. If a syntax error 2098 // is detected during the parsing of the associated statement, the 2099 // compiler must abort processing and close the CapturedStatement. 2100 // 2101 // Combined directives such as 'target parallel' have more than one 2102 // nested CapturedStatements. This RAII ensures that we unwind out 2103 // of all the nested CapturedStatements when an error is found. 2104 class CaptureRegionUnwinderRAII { 2105 private: 2106 Sema &S; 2107 bool &ErrorFound; 2108 OpenMPDirectiveKind DKind; 2109 2110 public: 2111 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 2112 OpenMPDirectiveKind DKind) 2113 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 2114 ~CaptureRegionUnwinderRAII() { 2115 if (ErrorFound) { 2116 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 2117 while (--ThisCaptureLevel >= 0) 2118 S.ActOnCapturedRegionError(); 2119 } 2120 } 2121 }; 2122 } // namespace 2123 2124 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 2125 ArrayRef<OMPClause *> Clauses) { 2126 bool ErrorFound = false; 2127 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 2128 *this, ErrorFound, DSAStack->getCurrentDirective()); 2129 if (!S.isUsable()) { 2130 ErrorFound = true; 2131 return StmtError(); 2132 } 2133 2134 OMPOrderedClause *OC = nullptr; 2135 OMPScheduleClause *SC = nullptr; 2136 SmallVector<OMPLinearClause *, 4> LCs; 2137 SmallVector<OMPClauseWithPreInit *, 8> PICs; 2138 // This is required for proper codegen. 2139 for (auto *Clause : Clauses) { 2140 if (isOpenMPPrivate(Clause->getClauseKind()) || 2141 Clause->getClauseKind() == OMPC_copyprivate || 2142 (getLangOpts().OpenMPUseTLS && 2143 getASTContext().getTargetInfo().isTLSSupported() && 2144 Clause->getClauseKind() == OMPC_copyin)) { 2145 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 2146 // Mark all variables in private list clauses as used in inner region. 2147 for (auto *VarRef : Clause->children()) { 2148 if (auto *E = cast_or_null<Expr>(VarRef)) { 2149 MarkDeclarationsReferencedInExpr(E); 2150 } 2151 } 2152 DSAStack->setForceVarCapturing(/*V=*/false); 2153 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 2154 if (auto *C = OMPClauseWithPreInit::get(Clause)) 2155 PICs.push_back(C); 2156 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 2157 if (auto *E = C->getPostUpdateExpr()) 2158 MarkDeclarationsReferencedInExpr(E); 2159 } 2160 } 2161 if (Clause->getClauseKind() == OMPC_schedule) 2162 SC = cast<OMPScheduleClause>(Clause); 2163 else if (Clause->getClauseKind() == OMPC_ordered) 2164 OC = cast<OMPOrderedClause>(Clause); 2165 else if (Clause->getClauseKind() == OMPC_linear) 2166 LCs.push_back(cast<OMPLinearClause>(Clause)); 2167 } 2168 // OpenMP, 2.7.1 Loop Construct, Restrictions 2169 // The nonmonotonic modifier cannot be specified if an ordered clause is 2170 // specified. 2171 if (SC && 2172 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 2173 SC->getSecondScheduleModifier() == 2174 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 2175 OC) { 2176 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 2177 ? SC->getFirstScheduleModifierLoc() 2178 : SC->getSecondScheduleModifierLoc(), 2179 diag::err_omp_schedule_nonmonotonic_ordered) 2180 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 2181 ErrorFound = true; 2182 } 2183 if (!LCs.empty() && OC && OC->getNumForLoops()) { 2184 for (auto *C : LCs) { 2185 Diag(C->getLocStart(), diag::err_omp_linear_ordered) 2186 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 2187 } 2188 ErrorFound = true; 2189 } 2190 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 2191 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 2192 OC->getNumForLoops()) { 2193 Diag(OC->getLocStart(), diag::err_omp_ordered_simd) 2194 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 2195 ErrorFound = true; 2196 } 2197 if (ErrorFound) { 2198 return StmtError(); 2199 } 2200 StmtResult SR = S; 2201 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2202 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 2203 for (auto ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 2204 // Mark all variables in private list clauses as used in inner region. 2205 // Required for proper codegen of combined directives. 2206 // TODO: add processing for other clauses. 2207 if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 2208 for (auto *C : PICs) { 2209 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 2210 // Find the particular capture region for the clause if the 2211 // directive is a combined one with multiple capture regions. 2212 // If the directive is not a combined one, the capture region 2213 // associated with the clause is OMPD_unknown and is generated 2214 // only once. 2215 if (CaptureRegion == ThisCaptureRegion || 2216 CaptureRegion == OMPD_unknown) { 2217 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 2218 for (auto *D : DS->decls()) 2219 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 2220 } 2221 } 2222 } 2223 } 2224 SR = ActOnCapturedRegionEnd(SR.get()); 2225 } 2226 return SR; 2227 } 2228 2229 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 2230 OpenMPDirectiveKind CancelRegion, 2231 SourceLocation StartLoc) { 2232 // CancelRegion is only needed for cancel and cancellation_point. 2233 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 2234 return false; 2235 2236 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 2237 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 2238 return false; 2239 2240 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 2241 << getOpenMPDirectiveName(CancelRegion); 2242 return true; 2243 } 2244 2245 static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, 2246 OpenMPDirectiveKind CurrentRegion, 2247 const DeclarationNameInfo &CurrentName, 2248 OpenMPDirectiveKind CancelRegion, 2249 SourceLocation StartLoc) { 2250 if (Stack->getCurScope()) { 2251 auto ParentRegion = Stack->getParentDirective(); 2252 auto OffendingRegion = ParentRegion; 2253 bool NestingProhibited = false; 2254 bool CloseNesting = true; 2255 bool OrphanSeen = false; 2256 enum { 2257 NoRecommend, 2258 ShouldBeInParallelRegion, 2259 ShouldBeInOrderedRegion, 2260 ShouldBeInTargetRegion, 2261 ShouldBeInTeamsRegion 2262 } Recommend = NoRecommend; 2263 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 2264 // OpenMP [2.16, Nesting of Regions] 2265 // OpenMP constructs may not be nested inside a simd region. 2266 // OpenMP [2.8.1,simd Construct, Restrictions] 2267 // An ordered construct with the simd clause is the only OpenMP 2268 // construct that can appear in the simd region. 2269 // Allowing a SIMD construct nested in another SIMD construct is an 2270 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 2271 // message. 2272 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 2273 ? diag::err_omp_prohibited_region_simd 2274 : diag::warn_omp_nesting_simd); 2275 return CurrentRegion != OMPD_simd; 2276 } 2277 if (ParentRegion == OMPD_atomic) { 2278 // OpenMP [2.16, Nesting of Regions] 2279 // OpenMP constructs may not be nested inside an atomic region. 2280 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 2281 return true; 2282 } 2283 if (CurrentRegion == OMPD_section) { 2284 // OpenMP [2.7.2, sections Construct, Restrictions] 2285 // Orphaned section directives are prohibited. That is, the section 2286 // directives must appear within the sections construct and must not be 2287 // encountered elsewhere in the sections region. 2288 if (ParentRegion != OMPD_sections && 2289 ParentRegion != OMPD_parallel_sections) { 2290 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 2291 << (ParentRegion != OMPD_unknown) 2292 << getOpenMPDirectiveName(ParentRegion); 2293 return true; 2294 } 2295 return false; 2296 } 2297 // Allow some constructs (except teams) to be orphaned (they could be 2298 // used in functions, called from OpenMP regions with the required 2299 // preconditions). 2300 if (ParentRegion == OMPD_unknown && 2301 !isOpenMPNestingTeamsDirective(CurrentRegion)) 2302 return false; 2303 if (CurrentRegion == OMPD_cancellation_point || 2304 CurrentRegion == OMPD_cancel) { 2305 // OpenMP [2.16, Nesting of Regions] 2306 // A cancellation point construct for which construct-type-clause is 2307 // taskgroup must be nested inside a task construct. A cancellation 2308 // point construct for which construct-type-clause is not taskgroup must 2309 // be closely nested inside an OpenMP construct that matches the type 2310 // specified in construct-type-clause. 2311 // A cancel construct for which construct-type-clause is taskgroup must be 2312 // nested inside a task construct. A cancel construct for which 2313 // construct-type-clause is not taskgroup must be closely nested inside an 2314 // OpenMP construct that matches the type specified in 2315 // construct-type-clause. 2316 NestingProhibited = 2317 !((CancelRegion == OMPD_parallel && 2318 (ParentRegion == OMPD_parallel || 2319 ParentRegion == OMPD_target_parallel)) || 2320 (CancelRegion == OMPD_for && 2321 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 2322 ParentRegion == OMPD_target_parallel_for)) || 2323 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 2324 (CancelRegion == OMPD_sections && 2325 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 2326 ParentRegion == OMPD_parallel_sections))); 2327 } else if (CurrentRegion == OMPD_master) { 2328 // OpenMP [2.16, Nesting of Regions] 2329 // A master region may not be closely nested inside a worksharing, 2330 // atomic, or explicit task region. 2331 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2332 isOpenMPTaskingDirective(ParentRegion); 2333 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 2334 // OpenMP [2.16, Nesting of Regions] 2335 // A critical region may not be nested (closely or otherwise) inside a 2336 // critical region with the same name. Note that this restriction is not 2337 // sufficient to prevent deadlock. 2338 SourceLocation PreviousCriticalLoc; 2339 bool DeadLock = Stack->hasDirective( 2340 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 2341 const DeclarationNameInfo &DNI, 2342 SourceLocation Loc) -> bool { 2343 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 2344 PreviousCriticalLoc = Loc; 2345 return true; 2346 } else 2347 return false; 2348 }, 2349 false /* skip top directive */); 2350 if (DeadLock) { 2351 SemaRef.Diag(StartLoc, 2352 diag::err_omp_prohibited_region_critical_same_name) 2353 << CurrentName.getName(); 2354 if (PreviousCriticalLoc.isValid()) 2355 SemaRef.Diag(PreviousCriticalLoc, 2356 diag::note_omp_previous_critical_region); 2357 return true; 2358 } 2359 } else if (CurrentRegion == OMPD_barrier) { 2360 // OpenMP [2.16, Nesting of Regions] 2361 // A barrier region may not be closely nested inside a worksharing, 2362 // explicit task, critical, ordered, atomic, or master region. 2363 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2364 isOpenMPTaskingDirective(ParentRegion) || 2365 ParentRegion == OMPD_master || 2366 ParentRegion == OMPD_critical || 2367 ParentRegion == OMPD_ordered; 2368 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 2369 !isOpenMPParallelDirective(CurrentRegion) && 2370 !isOpenMPTeamsDirective(CurrentRegion)) { 2371 // OpenMP [2.16, Nesting of Regions] 2372 // A worksharing region may not be closely nested inside a worksharing, 2373 // explicit task, critical, ordered, atomic, or master region. 2374 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2375 isOpenMPTaskingDirective(ParentRegion) || 2376 ParentRegion == OMPD_master || 2377 ParentRegion == OMPD_critical || 2378 ParentRegion == OMPD_ordered; 2379 Recommend = ShouldBeInParallelRegion; 2380 } else if (CurrentRegion == OMPD_ordered) { 2381 // OpenMP [2.16, Nesting of Regions] 2382 // An ordered region may not be closely nested inside a critical, 2383 // atomic, or explicit task region. 2384 // An ordered region must be closely nested inside a loop region (or 2385 // parallel loop region) with an ordered clause. 2386 // OpenMP [2.8.1,simd Construct, Restrictions] 2387 // An ordered construct with the simd clause is the only OpenMP construct 2388 // that can appear in the simd region. 2389 NestingProhibited = ParentRegion == OMPD_critical || 2390 isOpenMPTaskingDirective(ParentRegion) || 2391 !(isOpenMPSimdDirective(ParentRegion) || 2392 Stack->isParentOrderedRegion()); 2393 Recommend = ShouldBeInOrderedRegion; 2394 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 2395 // OpenMP [2.16, Nesting of Regions] 2396 // If specified, a teams construct must be contained within a target 2397 // construct. 2398 NestingProhibited = ParentRegion != OMPD_target; 2399 OrphanSeen = ParentRegion == OMPD_unknown; 2400 Recommend = ShouldBeInTargetRegion; 2401 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); 2402 } 2403 if (!NestingProhibited && 2404 !isOpenMPTargetExecutionDirective(CurrentRegion) && 2405 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 2406 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 2407 // OpenMP [2.16, Nesting of Regions] 2408 // distribute, parallel, parallel sections, parallel workshare, and the 2409 // parallel loop and parallel loop SIMD constructs are the only OpenMP 2410 // constructs that can be closely nested in the teams region. 2411 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 2412 !isOpenMPDistributeDirective(CurrentRegion); 2413 Recommend = ShouldBeInParallelRegion; 2414 } 2415 if (!NestingProhibited && 2416 isOpenMPNestingDistributeDirective(CurrentRegion)) { 2417 // OpenMP 4.5 [2.17 Nesting of Regions] 2418 // The region associated with the distribute construct must be strictly 2419 // nested inside a teams region 2420 NestingProhibited = 2421 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 2422 Recommend = ShouldBeInTeamsRegion; 2423 } 2424 if (!NestingProhibited && 2425 (isOpenMPTargetExecutionDirective(CurrentRegion) || 2426 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 2427 // OpenMP 4.5 [2.17 Nesting of Regions] 2428 // If a target, target update, target data, target enter data, or 2429 // target exit data construct is encountered during execution of a 2430 // target region, the behavior is unspecified. 2431 NestingProhibited = Stack->hasDirective( 2432 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2433 SourceLocation) -> bool { 2434 if (isOpenMPTargetExecutionDirective(K)) { 2435 OffendingRegion = K; 2436 return true; 2437 } else 2438 return false; 2439 }, 2440 false /* don't skip top directive */); 2441 CloseNesting = false; 2442 } 2443 if (NestingProhibited) { 2444 if (OrphanSeen) { 2445 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 2446 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 2447 } else { 2448 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 2449 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 2450 << Recommend << getOpenMPDirectiveName(CurrentRegion); 2451 } 2452 return true; 2453 } 2454 } 2455 return false; 2456 } 2457 2458 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 2459 ArrayRef<OMPClause *> Clauses, 2460 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 2461 bool ErrorFound = false; 2462 unsigned NamedModifiersNumber = 0; 2463 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 2464 OMPD_unknown + 1); 2465 SmallVector<SourceLocation, 4> NameModifierLoc; 2466 for (const auto *C : Clauses) { 2467 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 2468 // At most one if clause without a directive-name-modifier can appear on 2469 // the directive. 2470 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 2471 if (FoundNameModifiers[CurNM]) { 2472 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) 2473 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 2474 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 2475 ErrorFound = true; 2476 } else if (CurNM != OMPD_unknown) { 2477 NameModifierLoc.push_back(IC->getNameModifierLoc()); 2478 ++NamedModifiersNumber; 2479 } 2480 FoundNameModifiers[CurNM] = IC; 2481 if (CurNM == OMPD_unknown) 2482 continue; 2483 // Check if the specified name modifier is allowed for the current 2484 // directive. 2485 // At most one if clause with the particular directive-name-modifier can 2486 // appear on the directive. 2487 bool MatchFound = false; 2488 for (auto NM : AllowedNameModifiers) { 2489 if (CurNM == NM) { 2490 MatchFound = true; 2491 break; 2492 } 2493 } 2494 if (!MatchFound) { 2495 S.Diag(IC->getNameModifierLoc(), 2496 diag::err_omp_wrong_if_directive_name_modifier) 2497 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 2498 ErrorFound = true; 2499 } 2500 } 2501 } 2502 // If any if clause on the directive includes a directive-name-modifier then 2503 // all if clauses on the directive must include a directive-name-modifier. 2504 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 2505 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 2506 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), 2507 diag::err_omp_no_more_if_clause); 2508 } else { 2509 std::string Values; 2510 std::string Sep(", "); 2511 unsigned AllowedCnt = 0; 2512 unsigned TotalAllowedNum = 2513 AllowedNameModifiers.size() - NamedModifiersNumber; 2514 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 2515 ++Cnt) { 2516 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 2517 if (!FoundNameModifiers[NM]) { 2518 Values += "'"; 2519 Values += getOpenMPDirectiveName(NM); 2520 Values += "'"; 2521 if (AllowedCnt + 2 == TotalAllowedNum) 2522 Values += " or "; 2523 else if (AllowedCnt + 1 != TotalAllowedNum) 2524 Values += Sep; 2525 ++AllowedCnt; 2526 } 2527 } 2528 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), 2529 diag::err_omp_unnamed_if_clause) 2530 << (TotalAllowedNum > 1) << Values; 2531 } 2532 for (auto Loc : NameModifierLoc) { 2533 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 2534 } 2535 ErrorFound = true; 2536 } 2537 return ErrorFound; 2538 } 2539 2540 StmtResult Sema::ActOnOpenMPExecutableDirective( 2541 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 2542 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 2543 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 2544 StmtResult Res = StmtError(); 2545 // First check CancelRegion which is then used in checkNestingOfRegions. 2546 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 2547 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 2548 StartLoc)) 2549 return StmtError(); 2550 2551 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 2552 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; 2553 bool ErrorFound = false; 2554 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 2555 if (AStmt) { 2556 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 2557 2558 // Check default data sharing attributes for referenced variables. 2559 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 2560 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 2561 Stmt *S = AStmt; 2562 while (--ThisCaptureLevel >= 0) 2563 S = cast<CapturedStmt>(S)->getCapturedStmt(); 2564 DSAChecker.Visit(S); 2565 if (DSAChecker.isErrorFound()) 2566 return StmtError(); 2567 // Generate list of implicitly defined firstprivate variables. 2568 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 2569 2570 if (!DSAChecker.getImplicitFirstprivate().empty()) { 2571 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 2572 DSAChecker.getImplicitFirstprivate(), SourceLocation(), 2573 SourceLocation(), SourceLocation())) { 2574 ClausesWithImplicit.push_back(Implicit); 2575 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 2576 DSAChecker.getImplicitFirstprivate().size(); 2577 } else 2578 ErrorFound = true; 2579 } 2580 } 2581 2582 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 2583 switch (Kind) { 2584 case OMPD_parallel: 2585 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 2586 EndLoc); 2587 AllowedNameModifiers.push_back(OMPD_parallel); 2588 break; 2589 case OMPD_simd: 2590 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 2591 VarsWithInheritedDSA); 2592 break; 2593 case OMPD_for: 2594 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 2595 VarsWithInheritedDSA); 2596 break; 2597 case OMPD_for_simd: 2598 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2599 EndLoc, VarsWithInheritedDSA); 2600 break; 2601 case OMPD_sections: 2602 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 2603 EndLoc); 2604 break; 2605 case OMPD_section: 2606 assert(ClausesWithImplicit.empty() && 2607 "No clauses are allowed for 'omp section' directive"); 2608 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 2609 break; 2610 case OMPD_single: 2611 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 2612 EndLoc); 2613 break; 2614 case OMPD_master: 2615 assert(ClausesWithImplicit.empty() && 2616 "No clauses are allowed for 'omp master' directive"); 2617 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 2618 break; 2619 case OMPD_critical: 2620 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 2621 StartLoc, EndLoc); 2622 break; 2623 case OMPD_parallel_for: 2624 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 2625 EndLoc, VarsWithInheritedDSA); 2626 AllowedNameModifiers.push_back(OMPD_parallel); 2627 break; 2628 case OMPD_parallel_for_simd: 2629 Res = ActOnOpenMPParallelForSimdDirective( 2630 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2631 AllowedNameModifiers.push_back(OMPD_parallel); 2632 break; 2633 case OMPD_parallel_sections: 2634 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 2635 StartLoc, EndLoc); 2636 AllowedNameModifiers.push_back(OMPD_parallel); 2637 break; 2638 case OMPD_task: 2639 Res = 2640 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 2641 AllowedNameModifiers.push_back(OMPD_task); 2642 break; 2643 case OMPD_taskyield: 2644 assert(ClausesWithImplicit.empty() && 2645 "No clauses are allowed for 'omp taskyield' directive"); 2646 assert(AStmt == nullptr && 2647 "No associated statement allowed for 'omp taskyield' directive"); 2648 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 2649 break; 2650 case OMPD_barrier: 2651 assert(ClausesWithImplicit.empty() && 2652 "No clauses are allowed for 'omp barrier' directive"); 2653 assert(AStmt == nullptr && 2654 "No associated statement allowed for 'omp barrier' directive"); 2655 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 2656 break; 2657 case OMPD_taskwait: 2658 assert(ClausesWithImplicit.empty() && 2659 "No clauses are allowed for 'omp taskwait' directive"); 2660 assert(AStmt == nullptr && 2661 "No associated statement allowed for 'omp taskwait' directive"); 2662 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 2663 break; 2664 case OMPD_taskgroup: 2665 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 2666 EndLoc); 2667 break; 2668 case OMPD_flush: 2669 assert(AStmt == nullptr && 2670 "No associated statement allowed for 'omp flush' directive"); 2671 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 2672 break; 2673 case OMPD_ordered: 2674 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 2675 EndLoc); 2676 break; 2677 case OMPD_atomic: 2678 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 2679 EndLoc); 2680 break; 2681 case OMPD_teams: 2682 Res = 2683 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 2684 break; 2685 case OMPD_target: 2686 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 2687 EndLoc); 2688 AllowedNameModifiers.push_back(OMPD_target); 2689 break; 2690 case OMPD_target_parallel: 2691 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 2692 StartLoc, EndLoc); 2693 AllowedNameModifiers.push_back(OMPD_target); 2694 AllowedNameModifiers.push_back(OMPD_parallel); 2695 break; 2696 case OMPD_target_parallel_for: 2697 Res = ActOnOpenMPTargetParallelForDirective( 2698 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2699 AllowedNameModifiers.push_back(OMPD_target); 2700 AllowedNameModifiers.push_back(OMPD_parallel); 2701 break; 2702 case OMPD_cancellation_point: 2703 assert(ClausesWithImplicit.empty() && 2704 "No clauses are allowed for 'omp cancellation point' directive"); 2705 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 2706 "cancellation point' directive"); 2707 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 2708 break; 2709 case OMPD_cancel: 2710 assert(AStmt == nullptr && 2711 "No associated statement allowed for 'omp cancel' directive"); 2712 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 2713 CancelRegion); 2714 AllowedNameModifiers.push_back(OMPD_cancel); 2715 break; 2716 case OMPD_target_data: 2717 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 2718 EndLoc); 2719 AllowedNameModifiers.push_back(OMPD_target_data); 2720 break; 2721 case OMPD_target_enter_data: 2722 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 2723 EndLoc); 2724 AllowedNameModifiers.push_back(OMPD_target_enter_data); 2725 break; 2726 case OMPD_target_exit_data: 2727 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 2728 EndLoc); 2729 AllowedNameModifiers.push_back(OMPD_target_exit_data); 2730 break; 2731 case OMPD_taskloop: 2732 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 2733 EndLoc, VarsWithInheritedDSA); 2734 AllowedNameModifiers.push_back(OMPD_taskloop); 2735 break; 2736 case OMPD_taskloop_simd: 2737 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2738 EndLoc, VarsWithInheritedDSA); 2739 AllowedNameModifiers.push_back(OMPD_taskloop); 2740 break; 2741 case OMPD_distribute: 2742 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 2743 EndLoc, VarsWithInheritedDSA); 2744 break; 2745 case OMPD_target_update: 2746 assert(!AStmt && "Statement is not allowed for target update"); 2747 Res = 2748 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc); 2749 AllowedNameModifiers.push_back(OMPD_target_update); 2750 break; 2751 case OMPD_distribute_parallel_for: 2752 Res = ActOnOpenMPDistributeParallelForDirective( 2753 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2754 AllowedNameModifiers.push_back(OMPD_parallel); 2755 break; 2756 case OMPD_distribute_parallel_for_simd: 2757 Res = ActOnOpenMPDistributeParallelForSimdDirective( 2758 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2759 AllowedNameModifiers.push_back(OMPD_parallel); 2760 break; 2761 case OMPD_distribute_simd: 2762 Res = ActOnOpenMPDistributeSimdDirective( 2763 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2764 break; 2765 case OMPD_target_parallel_for_simd: 2766 Res = ActOnOpenMPTargetParallelForSimdDirective( 2767 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2768 AllowedNameModifiers.push_back(OMPD_target); 2769 AllowedNameModifiers.push_back(OMPD_parallel); 2770 break; 2771 case OMPD_target_simd: 2772 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2773 EndLoc, VarsWithInheritedDSA); 2774 AllowedNameModifiers.push_back(OMPD_target); 2775 break; 2776 case OMPD_teams_distribute: 2777 Res = ActOnOpenMPTeamsDistributeDirective( 2778 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2779 break; 2780 case OMPD_teams_distribute_simd: 2781 Res = ActOnOpenMPTeamsDistributeSimdDirective( 2782 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2783 break; 2784 case OMPD_teams_distribute_parallel_for_simd: 2785 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 2786 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2787 AllowedNameModifiers.push_back(OMPD_parallel); 2788 break; 2789 case OMPD_teams_distribute_parallel_for: 2790 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 2791 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2792 AllowedNameModifiers.push_back(OMPD_parallel); 2793 break; 2794 case OMPD_target_teams: 2795 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 2796 EndLoc); 2797 AllowedNameModifiers.push_back(OMPD_target); 2798 break; 2799 case OMPD_target_teams_distribute: 2800 Res = ActOnOpenMPTargetTeamsDistributeDirective( 2801 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2802 AllowedNameModifiers.push_back(OMPD_target); 2803 break; 2804 case OMPD_target_teams_distribute_parallel_for: 2805 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 2806 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2807 AllowedNameModifiers.push_back(OMPD_target); 2808 AllowedNameModifiers.push_back(OMPD_parallel); 2809 break; 2810 case OMPD_target_teams_distribute_parallel_for_simd: 2811 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 2812 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2813 AllowedNameModifiers.push_back(OMPD_target); 2814 AllowedNameModifiers.push_back(OMPD_parallel); 2815 break; 2816 case OMPD_target_teams_distribute_simd: 2817 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 2818 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2819 AllowedNameModifiers.push_back(OMPD_target); 2820 break; 2821 case OMPD_declare_target: 2822 case OMPD_end_declare_target: 2823 case OMPD_threadprivate: 2824 case OMPD_declare_reduction: 2825 case OMPD_declare_simd: 2826 llvm_unreachable("OpenMP Directive is not allowed"); 2827 case OMPD_unknown: 2828 llvm_unreachable("Unknown OpenMP directive"); 2829 } 2830 2831 for (auto P : VarsWithInheritedDSA) { 2832 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 2833 << P.first << P.second->getSourceRange(); 2834 } 2835 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 2836 2837 if (!AllowedNameModifiers.empty()) 2838 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 2839 ErrorFound; 2840 2841 if (ErrorFound) 2842 return StmtError(); 2843 return Res; 2844 } 2845 2846 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 2847 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 2848 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 2849 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 2850 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 2851 assert(Aligneds.size() == Alignments.size()); 2852 assert(Linears.size() == LinModifiers.size()); 2853 assert(Linears.size() == Steps.size()); 2854 if (!DG || DG.get().isNull()) 2855 return DeclGroupPtrTy(); 2856 2857 if (!DG.get().isSingleDecl()) { 2858 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 2859 return DG; 2860 } 2861 auto *ADecl = DG.get().getSingleDecl(); 2862 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 2863 ADecl = FTD->getTemplatedDecl(); 2864 2865 auto *FD = dyn_cast<FunctionDecl>(ADecl); 2866 if (!FD) { 2867 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 2868 return DeclGroupPtrTy(); 2869 } 2870 2871 // OpenMP [2.8.2, declare simd construct, Description] 2872 // The parameter of the simdlen clause must be a constant positive integer 2873 // expression. 2874 ExprResult SL; 2875 if (Simdlen) 2876 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 2877 // OpenMP [2.8.2, declare simd construct, Description] 2878 // The special this pointer can be used as if was one of the arguments to the 2879 // function in any of the linear, aligned, or uniform clauses. 2880 // The uniform clause declares one or more arguments to have an invariant 2881 // value for all concurrent invocations of the function in the execution of a 2882 // single SIMD loop. 2883 llvm::DenseMap<Decl *, Expr *> UniformedArgs; 2884 Expr *UniformedLinearThis = nullptr; 2885 for (auto *E : Uniforms) { 2886 E = E->IgnoreParenImpCasts(); 2887 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 2888 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 2889 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 2890 FD->getParamDecl(PVD->getFunctionScopeIndex()) 2891 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 2892 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E)); 2893 continue; 2894 } 2895 if (isa<CXXThisExpr>(E)) { 2896 UniformedLinearThis = E; 2897 continue; 2898 } 2899 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 2900 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 2901 } 2902 // OpenMP [2.8.2, declare simd construct, Description] 2903 // The aligned clause declares that the object to which each list item points 2904 // is aligned to the number of bytes expressed in the optional parameter of 2905 // the aligned clause. 2906 // The special this pointer can be used as if was one of the arguments to the 2907 // function in any of the linear, aligned, or uniform clauses. 2908 // The type of list items appearing in the aligned clause must be array, 2909 // pointer, reference to array, or reference to pointer. 2910 llvm::DenseMap<Decl *, Expr *> AlignedArgs; 2911 Expr *AlignedThis = nullptr; 2912 for (auto *E : Aligneds) { 2913 E = E->IgnoreParenImpCasts(); 2914 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 2915 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 2916 auto *CanonPVD = PVD->getCanonicalDecl(); 2917 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 2918 FD->getParamDecl(PVD->getFunctionScopeIndex()) 2919 ->getCanonicalDecl() == CanonPVD) { 2920 // OpenMP [2.8.1, simd construct, Restrictions] 2921 // A list-item cannot appear in more than one aligned clause. 2922 if (AlignedArgs.count(CanonPVD) > 0) { 2923 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 2924 << 1 << E->getSourceRange(); 2925 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 2926 diag::note_omp_explicit_dsa) 2927 << getOpenMPClauseName(OMPC_aligned); 2928 continue; 2929 } 2930 AlignedArgs[CanonPVD] = E; 2931 QualType QTy = PVD->getType() 2932 .getNonReferenceType() 2933 .getUnqualifiedType() 2934 .getCanonicalType(); 2935 const Type *Ty = QTy.getTypePtrOrNull(); 2936 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 2937 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 2938 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 2939 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 2940 } 2941 continue; 2942 } 2943 } 2944 if (isa<CXXThisExpr>(E)) { 2945 if (AlignedThis) { 2946 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 2947 << 2 << E->getSourceRange(); 2948 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 2949 << getOpenMPClauseName(OMPC_aligned); 2950 } 2951 AlignedThis = E; 2952 continue; 2953 } 2954 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 2955 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 2956 } 2957 // The optional parameter of the aligned clause, alignment, must be a constant 2958 // positive integer expression. If no optional parameter is specified, 2959 // implementation-defined default alignments for SIMD instructions on the 2960 // target platforms are assumed. 2961 SmallVector<Expr *, 4> NewAligns; 2962 for (auto *E : Alignments) { 2963 ExprResult Align; 2964 if (E) 2965 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 2966 NewAligns.push_back(Align.get()); 2967 } 2968 // OpenMP [2.8.2, declare simd construct, Description] 2969 // The linear clause declares one or more list items to be private to a SIMD 2970 // lane and to have a linear relationship with respect to the iteration space 2971 // of a loop. 2972 // The special this pointer can be used as if was one of the arguments to the 2973 // function in any of the linear, aligned, or uniform clauses. 2974 // When a linear-step expression is specified in a linear clause it must be 2975 // either a constant integer expression or an integer-typed parameter that is 2976 // specified in a uniform clause on the directive. 2977 llvm::DenseMap<Decl *, Expr *> LinearArgs; 2978 const bool IsUniformedThis = UniformedLinearThis != nullptr; 2979 auto MI = LinModifiers.begin(); 2980 for (auto *E : Linears) { 2981 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 2982 ++MI; 2983 E = E->IgnoreParenImpCasts(); 2984 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 2985 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 2986 auto *CanonPVD = PVD->getCanonicalDecl(); 2987 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 2988 FD->getParamDecl(PVD->getFunctionScopeIndex()) 2989 ->getCanonicalDecl() == CanonPVD) { 2990 // OpenMP [2.15.3.7, linear Clause, Restrictions] 2991 // A list-item cannot appear in more than one linear clause. 2992 if (LinearArgs.count(CanonPVD) > 0) { 2993 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 2994 << getOpenMPClauseName(OMPC_linear) 2995 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 2996 Diag(LinearArgs[CanonPVD]->getExprLoc(), 2997 diag::note_omp_explicit_dsa) 2998 << getOpenMPClauseName(OMPC_linear); 2999 continue; 3000 } 3001 // Each argument can appear in at most one uniform or linear clause. 3002 if (UniformedArgs.count(CanonPVD) > 0) { 3003 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3004 << getOpenMPClauseName(OMPC_linear) 3005 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 3006 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 3007 diag::note_omp_explicit_dsa) 3008 << getOpenMPClauseName(OMPC_uniform); 3009 continue; 3010 } 3011 LinearArgs[CanonPVD] = E; 3012 if (E->isValueDependent() || E->isTypeDependent() || 3013 E->isInstantiationDependent() || 3014 E->containsUnexpandedParameterPack()) 3015 continue; 3016 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 3017 PVD->getOriginalType()); 3018 continue; 3019 } 3020 } 3021 if (isa<CXXThisExpr>(E)) { 3022 if (UniformedLinearThis) { 3023 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3024 << getOpenMPClauseName(OMPC_linear) 3025 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 3026 << E->getSourceRange(); 3027 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 3028 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 3029 : OMPC_linear); 3030 continue; 3031 } 3032 UniformedLinearThis = E; 3033 if (E->isValueDependent() || E->isTypeDependent() || 3034 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3035 continue; 3036 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 3037 E->getType()); 3038 continue; 3039 } 3040 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3041 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3042 } 3043 Expr *Step = nullptr; 3044 Expr *NewStep = nullptr; 3045 SmallVector<Expr *, 4> NewSteps; 3046 for (auto *E : Steps) { 3047 // Skip the same step expression, it was checked already. 3048 if (Step == E || !E) { 3049 NewSteps.push_back(E ? NewStep : nullptr); 3050 continue; 3051 } 3052 Step = E; 3053 if (auto *DRE = dyn_cast<DeclRefExpr>(Step)) 3054 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3055 auto *CanonPVD = PVD->getCanonicalDecl(); 3056 if (UniformedArgs.count(CanonPVD) == 0) { 3057 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 3058 << Step->getSourceRange(); 3059 } else if (E->isValueDependent() || E->isTypeDependent() || 3060 E->isInstantiationDependent() || 3061 E->containsUnexpandedParameterPack() || 3062 CanonPVD->getType()->hasIntegerRepresentation()) 3063 NewSteps.push_back(Step); 3064 else { 3065 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 3066 << Step->getSourceRange(); 3067 } 3068 continue; 3069 } 3070 NewStep = Step; 3071 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3072 !Step->isInstantiationDependent() && 3073 !Step->containsUnexpandedParameterPack()) { 3074 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 3075 .get(); 3076 if (NewStep) 3077 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 3078 } 3079 NewSteps.push_back(NewStep); 3080 } 3081 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 3082 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 3083 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 3084 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 3085 const_cast<Expr **>(Linears.data()), Linears.size(), 3086 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 3087 NewSteps.data(), NewSteps.size(), SR); 3088 ADecl->addAttr(NewAttr); 3089 return ConvertDeclToDeclGroup(ADecl); 3090 } 3091 3092 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 3093 Stmt *AStmt, 3094 SourceLocation StartLoc, 3095 SourceLocation EndLoc) { 3096 if (!AStmt) 3097 return StmtError(); 3098 3099 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 3100 // 1.2.2 OpenMP Language Terminology 3101 // Structured block - An executable statement with a single entry at the 3102 // top and a single exit at the bottom. 3103 // The point of exit cannot be a branch out of the structured block. 3104 // longjmp() and throw() must not violate the entry/exit criteria. 3105 CS->getCapturedDecl()->setNothrow(); 3106 3107 getCurFunction()->setHasBranchProtectedScope(); 3108 3109 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 3110 DSAStack->isCancelRegion()); 3111 } 3112 3113 namespace { 3114 /// \brief Helper class for checking canonical form of the OpenMP loops and 3115 /// extracting iteration space of each loop in the loop nest, that will be used 3116 /// for IR generation. 3117 class OpenMPIterationSpaceChecker { 3118 /// \brief Reference to Sema. 3119 Sema &SemaRef; 3120 /// \brief A location for diagnostics (when there is no some better location). 3121 SourceLocation DefaultLoc; 3122 /// \brief A location for diagnostics (when increment is not compatible). 3123 SourceLocation ConditionLoc; 3124 /// \brief A source location for referring to loop init later. 3125 SourceRange InitSrcRange; 3126 /// \brief A source location for referring to condition later. 3127 SourceRange ConditionSrcRange; 3128 /// \brief A source location for referring to increment later. 3129 SourceRange IncrementSrcRange; 3130 /// \brief Loop variable. 3131 ValueDecl *LCDecl = nullptr; 3132 /// \brief Reference to loop variable. 3133 Expr *LCRef = nullptr; 3134 /// \brief Lower bound (initializer for the var). 3135 Expr *LB = nullptr; 3136 /// \brief Upper bound. 3137 Expr *UB = nullptr; 3138 /// \brief Loop step (increment). 3139 Expr *Step = nullptr; 3140 /// \brief This flag is true when condition is one of: 3141 /// Var < UB 3142 /// Var <= UB 3143 /// UB > Var 3144 /// UB >= Var 3145 bool TestIsLessOp = false; 3146 /// \brief This flag is true when condition is strict ( < or > ). 3147 bool TestIsStrictOp = false; 3148 /// \brief This flag is true when step is subtracted on each iteration. 3149 bool SubtractStep = false; 3150 3151 public: 3152 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 3153 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 3154 /// \brief Check init-expr for canonical loop form and save loop counter 3155 /// variable - #Var and its initialization value - #LB. 3156 bool CheckInit(Stmt *S, bool EmitDiags = true); 3157 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags 3158 /// for less/greater and for strict/non-strict comparison. 3159 bool CheckCond(Expr *S); 3160 /// \brief Check incr-expr for canonical loop form and return true if it 3161 /// does not conform, otherwise save loop step (#Step). 3162 bool CheckInc(Expr *S); 3163 /// \brief Return the loop counter variable. 3164 ValueDecl *GetLoopDecl() const { return LCDecl; } 3165 /// \brief Return the reference expression to loop counter variable. 3166 Expr *GetLoopDeclRefExpr() const { return LCRef; } 3167 /// \brief Source range of the loop init. 3168 SourceRange GetInitSrcRange() const { return InitSrcRange; } 3169 /// \brief Source range of the loop condition. 3170 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } 3171 /// \brief Source range of the loop increment. 3172 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } 3173 /// \brief True if the step should be subtracted. 3174 bool ShouldSubtractStep() const { return SubtractStep; } 3175 /// \brief Build the expression to calculate the number of iterations. 3176 Expr * 3177 BuildNumIterations(Scope *S, const bool LimitedType, 3178 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; 3179 /// \brief Build the precondition expression for the loops. 3180 Expr *BuildPreCond(Scope *S, Expr *Cond, 3181 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; 3182 /// \brief Build reference expression to the counter be used for codegen. 3183 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures, 3184 DSAStackTy &DSA) const; 3185 /// \brief Build reference expression to the private counter be used for 3186 /// codegen. 3187 Expr *BuildPrivateCounterVar() const; 3188 /// \brief Build initialization of the counter be used for codegen. 3189 Expr *BuildCounterInit() const; 3190 /// \brief Build step of the counter be used for codegen. 3191 Expr *BuildCounterStep() const; 3192 /// \brief Return true if any expression is dependent. 3193 bool Dependent() const; 3194 3195 private: 3196 /// \brief Check the right-hand side of an assignment in the increment 3197 /// expression. 3198 bool CheckIncRHS(Expr *RHS); 3199 /// \brief Helper to set loop counter variable and its initializer. 3200 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 3201 /// \brief Helper to set upper bound. 3202 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 3203 SourceLocation SL); 3204 /// \brief Helper to set loop increment. 3205 bool SetStep(Expr *NewStep, bool Subtract); 3206 }; 3207 3208 bool OpenMPIterationSpaceChecker::Dependent() const { 3209 if (!LCDecl) { 3210 assert(!LB && !UB && !Step); 3211 return false; 3212 } 3213 return LCDecl->getType()->isDependentType() || 3214 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 3215 (Step && Step->isValueDependent()); 3216 } 3217 3218 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl, 3219 Expr *NewLCRefExpr, 3220 Expr *NewLB) { 3221 // State consistency checking to ensure correct usage. 3222 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 3223 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3224 if (!NewLCDecl || !NewLB) 3225 return true; 3226 LCDecl = getCanonicalDecl(NewLCDecl); 3227 LCRef = NewLCRefExpr; 3228 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 3229 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3230 if ((Ctor->isCopyOrMoveConstructor() || 3231 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3232 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3233 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 3234 LB = NewLB; 3235 return false; 3236 } 3237 3238 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, 3239 SourceRange SR, SourceLocation SL) { 3240 // State consistency checking to ensure correct usage. 3241 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 3242 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3243 if (!NewUB) 3244 return true; 3245 UB = NewUB; 3246 TestIsLessOp = LessOp; 3247 TestIsStrictOp = StrictOp; 3248 ConditionSrcRange = SR; 3249 ConditionLoc = SL; 3250 return false; 3251 } 3252 3253 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { 3254 // State consistency checking to ensure correct usage. 3255 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 3256 if (!NewStep) 3257 return true; 3258 if (!NewStep->isValueDependent()) { 3259 // Check that the step is integer expression. 3260 SourceLocation StepLoc = NewStep->getLocStart(); 3261 ExprResult Val = 3262 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); 3263 if (Val.isInvalid()) 3264 return true; 3265 NewStep = Val.get(); 3266 3267 // OpenMP [2.6, Canonical Loop Form, Restrictions] 3268 // If test-expr is of form var relational-op b and relational-op is < or 3269 // <= then incr-expr must cause var to increase on each iteration of the 3270 // loop. If test-expr is of form var relational-op b and relational-op is 3271 // > or >= then incr-expr must cause var to decrease on each iteration of 3272 // the loop. 3273 // If test-expr is of form b relational-op var and relational-op is < or 3274 // <= then incr-expr must cause var to decrease on each iteration of the 3275 // loop. If test-expr is of form b relational-op var and relational-op is 3276 // > or >= then incr-expr must cause var to increase on each iteration of 3277 // the loop. 3278 llvm::APSInt Result; 3279 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 3280 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 3281 bool IsConstNeg = 3282 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 3283 bool IsConstPos = 3284 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 3285 bool IsConstZero = IsConstant && !Result.getBoolValue(); 3286 if (UB && (IsConstZero || 3287 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 3288 : (IsConstPos || (IsUnsigned && !Subtract))))) { 3289 SemaRef.Diag(NewStep->getExprLoc(), 3290 diag::err_omp_loop_incr_not_compatible) 3291 << LCDecl << TestIsLessOp << NewStep->getSourceRange(); 3292 SemaRef.Diag(ConditionLoc, 3293 diag::note_omp_loop_cond_requres_compatible_incr) 3294 << TestIsLessOp << ConditionSrcRange; 3295 return true; 3296 } 3297 if (TestIsLessOp == Subtract) { 3298 NewStep = 3299 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 3300 .get(); 3301 Subtract = !Subtract; 3302 } 3303 } 3304 3305 Step = NewStep; 3306 SubtractStep = Subtract; 3307 return false; 3308 } 3309 3310 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) { 3311 // Check init-expr for canonical loop form and save loop counter 3312 // variable - #Var and its initialization value - #LB. 3313 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 3314 // var = lb 3315 // integer-type var = lb 3316 // random-access-iterator-type var = lb 3317 // pointer-type var = lb 3318 // 3319 if (!S) { 3320 if (EmitDiags) { 3321 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 3322 } 3323 return true; 3324 } 3325 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 3326 if (!ExprTemp->cleanupsHaveSideEffects()) 3327 S = ExprTemp->getSubExpr(); 3328 3329 InitSrcRange = S->getSourceRange(); 3330 if (Expr *E = dyn_cast<Expr>(S)) 3331 S = E->IgnoreParens(); 3332 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3333 if (BO->getOpcode() == BO_Assign) { 3334 auto *LHS = BO->getLHS()->IgnoreParens(); 3335 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3336 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3337 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3338 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3339 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 3340 } 3341 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3342 if (ME->isArrow() && 3343 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3344 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3345 } 3346 } 3347 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 3348 if (DS->isSingleDecl()) { 3349 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 3350 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 3351 // Accept non-canonical init form here but emit ext. warning. 3352 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 3353 SemaRef.Diag(S->getLocStart(), 3354 diag::ext_omp_loop_not_canonical_init) 3355 << S->getSourceRange(); 3356 return SetLCDeclAndLB(Var, nullptr, Var->getInit()); 3357 } 3358 } 3359 } 3360 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3361 if (CE->getOperator() == OO_Equal) { 3362 auto *LHS = CE->getArg(0); 3363 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3364 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3365 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3366 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3367 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 3368 } 3369 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3370 if (ME->isArrow() && 3371 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3372 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3373 } 3374 } 3375 } 3376 3377 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3378 return false; 3379 if (EmitDiags) { 3380 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 3381 << S->getSourceRange(); 3382 } 3383 return true; 3384 } 3385 3386 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the 3387 /// variable (which may be the loop variable) if possible. 3388 static const ValueDecl *GetInitLCDecl(Expr *E) { 3389 if (!E) 3390 return nullptr; 3391 E = getExprAsWritten(E); 3392 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 3393 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3394 if ((Ctor->isCopyOrMoveConstructor() || 3395 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3396 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3397 E = CE->getArg(0)->IgnoreParenImpCasts(); 3398 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 3399 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 3400 return getCanonicalDecl(VD); 3401 } 3402 if (auto *ME = dyn_cast_or_null<MemberExpr>(E)) 3403 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3404 return getCanonicalDecl(ME->getMemberDecl()); 3405 return nullptr; 3406 } 3407 3408 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { 3409 // Check test-expr for canonical form, save upper-bound UB, flags for 3410 // less/greater and for strict/non-strict comparison. 3411 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3412 // var relational-op b 3413 // b relational-op var 3414 // 3415 if (!S) { 3416 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 3417 return true; 3418 } 3419 S = getExprAsWritten(S); 3420 SourceLocation CondLoc = S->getLocStart(); 3421 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3422 if (BO->isRelationalOp()) { 3423 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3424 return SetUB(BO->getRHS(), 3425 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 3426 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3427 BO->getSourceRange(), BO->getOperatorLoc()); 3428 if (GetInitLCDecl(BO->getRHS()) == LCDecl) 3429 return SetUB(BO->getLHS(), 3430 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 3431 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3432 BO->getSourceRange(), BO->getOperatorLoc()); 3433 } 3434 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3435 if (CE->getNumArgs() == 2) { 3436 auto Op = CE->getOperator(); 3437 switch (Op) { 3438 case OO_Greater: 3439 case OO_GreaterEqual: 3440 case OO_Less: 3441 case OO_LessEqual: 3442 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3443 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 3444 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3445 CE->getOperatorLoc()); 3446 if (GetInitLCDecl(CE->getArg(1)) == LCDecl) 3447 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 3448 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3449 CE->getOperatorLoc()); 3450 break; 3451 default: 3452 break; 3453 } 3454 } 3455 } 3456 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3457 return false; 3458 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 3459 << S->getSourceRange() << LCDecl; 3460 return true; 3461 } 3462 3463 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { 3464 // RHS of canonical loop form increment can be: 3465 // var + incr 3466 // incr + var 3467 // var - incr 3468 // 3469 RHS = RHS->IgnoreParenImpCasts(); 3470 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 3471 if (BO->isAdditiveOp()) { 3472 bool IsAdd = BO->getOpcode() == BO_Add; 3473 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3474 return SetStep(BO->getRHS(), !IsAdd); 3475 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl) 3476 return SetStep(BO->getLHS(), false); 3477 } 3478 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 3479 bool IsAdd = CE->getOperator() == OO_Plus; 3480 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 3481 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3482 return SetStep(CE->getArg(1), !IsAdd); 3483 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl) 3484 return SetStep(CE->getArg(0), false); 3485 } 3486 } 3487 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3488 return false; 3489 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 3490 << RHS->getSourceRange() << LCDecl; 3491 return true; 3492 } 3493 3494 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { 3495 // Check incr-expr for canonical loop form and return true if it 3496 // does not conform. 3497 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3498 // ++var 3499 // var++ 3500 // --var 3501 // var-- 3502 // var += incr 3503 // var -= incr 3504 // var = var + incr 3505 // var = incr + var 3506 // var = var - incr 3507 // 3508 if (!S) { 3509 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 3510 return true; 3511 } 3512 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 3513 if (!ExprTemp->cleanupsHaveSideEffects()) 3514 S = ExprTemp->getSubExpr(); 3515 3516 IncrementSrcRange = S->getSourceRange(); 3517 S = S->IgnoreParens(); 3518 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 3519 if (UO->isIncrementDecrementOp() && 3520 GetInitLCDecl(UO->getSubExpr()) == LCDecl) 3521 return SetStep(SemaRef 3522 .ActOnIntegerConstant(UO->getLocStart(), 3523 (UO->isDecrementOp() ? -1 : 1)) 3524 .get(), 3525 false); 3526 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3527 switch (BO->getOpcode()) { 3528 case BO_AddAssign: 3529 case BO_SubAssign: 3530 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3531 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 3532 break; 3533 case BO_Assign: 3534 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3535 return CheckIncRHS(BO->getRHS()); 3536 break; 3537 default: 3538 break; 3539 } 3540 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3541 switch (CE->getOperator()) { 3542 case OO_PlusPlus: 3543 case OO_MinusMinus: 3544 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3545 return SetStep(SemaRef 3546 .ActOnIntegerConstant( 3547 CE->getLocStart(), 3548 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 3549 .get(), 3550 false); 3551 break; 3552 case OO_PlusEqual: 3553 case OO_MinusEqual: 3554 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3555 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 3556 break; 3557 case OO_Equal: 3558 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3559 return CheckIncRHS(CE->getArg(1)); 3560 break; 3561 default: 3562 break; 3563 } 3564 } 3565 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3566 return false; 3567 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 3568 << S->getSourceRange() << LCDecl; 3569 return true; 3570 } 3571 3572 static ExprResult 3573 tryBuildCapture(Sema &SemaRef, Expr *Capture, 3574 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 3575 if (SemaRef.CurContext->isDependentContext()) 3576 return ExprResult(Capture); 3577 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 3578 return SemaRef.PerformImplicitConversion( 3579 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 3580 /*AllowExplicit=*/true); 3581 auto I = Captures.find(Capture); 3582 if (I != Captures.end()) 3583 return buildCapture(SemaRef, Capture, I->second); 3584 DeclRefExpr *Ref = nullptr; 3585 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 3586 Captures[Capture] = Ref; 3587 return Res; 3588 } 3589 3590 /// \brief Build the expression to calculate the number of iterations. 3591 Expr *OpenMPIterationSpaceChecker::BuildNumIterations( 3592 Scope *S, const bool LimitedType, 3593 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { 3594 ExprResult Diff; 3595 auto VarType = LCDecl->getType().getNonReferenceType(); 3596 if (VarType->isIntegerType() || VarType->isPointerType() || 3597 SemaRef.getLangOpts().CPlusPlus) { 3598 // Upper - Lower 3599 auto *UBExpr = TestIsLessOp ? UB : LB; 3600 auto *LBExpr = TestIsLessOp ? LB : UB; 3601 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 3602 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 3603 if (!Upper || !Lower) 3604 return nullptr; 3605 3606 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 3607 3608 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 3609 // BuildBinOp already emitted error, this one is to point user to upper 3610 // and lower bound, and to tell what is passed to 'operator-'. 3611 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) 3612 << Upper->getSourceRange() << Lower->getSourceRange(); 3613 return nullptr; 3614 } 3615 } 3616 3617 if (!Diff.isUsable()) 3618 return nullptr; 3619 3620 // Upper - Lower [- 1] 3621 if (TestIsStrictOp) 3622 Diff = SemaRef.BuildBinOp( 3623 S, DefaultLoc, BO_Sub, Diff.get(), 3624 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 3625 if (!Diff.isUsable()) 3626 return nullptr; 3627 3628 // Upper - Lower [- 1] + Step 3629 auto NewStep = tryBuildCapture(SemaRef, Step, Captures); 3630 if (!NewStep.isUsable()) 3631 return nullptr; 3632 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 3633 if (!Diff.isUsable()) 3634 return nullptr; 3635 3636 // Parentheses (for dumping/debugging purposes only). 3637 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 3638 if (!Diff.isUsable()) 3639 return nullptr; 3640 3641 // (Upper - Lower [- 1] + Step) / Step 3642 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 3643 if (!Diff.isUsable()) 3644 return nullptr; 3645 3646 // OpenMP runtime requires 32-bit or 64-bit loop variables. 3647 QualType Type = Diff.get()->getType(); 3648 auto &C = SemaRef.Context; 3649 bool UseVarType = VarType->hasIntegerRepresentation() && 3650 C.getTypeSize(Type) > C.getTypeSize(VarType); 3651 if (!Type->isIntegerType() || UseVarType) { 3652 unsigned NewSize = 3653 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 3654 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 3655 : Type->hasSignedIntegerRepresentation(); 3656 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 3657 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 3658 Diff = SemaRef.PerformImplicitConversion( 3659 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 3660 if (!Diff.isUsable()) 3661 return nullptr; 3662 } 3663 } 3664 if (LimitedType) { 3665 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 3666 if (NewSize != C.getTypeSize(Type)) { 3667 if (NewSize < C.getTypeSize(Type)) { 3668 assert(NewSize == 64 && "incorrect loop var size"); 3669 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 3670 << InitSrcRange << ConditionSrcRange; 3671 } 3672 QualType NewType = C.getIntTypeForBitwidth( 3673 NewSize, Type->hasSignedIntegerRepresentation() || 3674 C.getTypeSize(Type) < NewSize); 3675 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 3676 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 3677 Sema::AA_Converting, true); 3678 if (!Diff.isUsable()) 3679 return nullptr; 3680 } 3681 } 3682 } 3683 3684 return Diff.get(); 3685 } 3686 3687 Expr *OpenMPIterationSpaceChecker::BuildPreCond( 3688 Scope *S, Expr *Cond, 3689 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { 3690 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 3691 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 3692 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 3693 3694 auto NewLB = tryBuildCapture(SemaRef, LB, Captures); 3695 auto NewUB = tryBuildCapture(SemaRef, UB, Captures); 3696 if (!NewLB.isUsable() || !NewUB.isUsable()) 3697 return nullptr; 3698 3699 auto CondExpr = SemaRef.BuildBinOp( 3700 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 3701 : (TestIsStrictOp ? BO_GT : BO_GE), 3702 NewLB.get(), NewUB.get()); 3703 if (CondExpr.isUsable()) { 3704 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 3705 SemaRef.Context.BoolTy)) 3706 CondExpr = SemaRef.PerformImplicitConversion( 3707 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 3708 /*AllowExplicit=*/true); 3709 } 3710 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 3711 // Otherwise use original loop conditon and evaluate it in runtime. 3712 return CondExpr.isUsable() ? CondExpr.get() : Cond; 3713 } 3714 3715 /// \brief Build reference expression to the counter be used for codegen. 3716 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar( 3717 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const { 3718 auto *VD = dyn_cast<VarDecl>(LCDecl); 3719 if (!VD) { 3720 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl); 3721 auto *Ref = buildDeclRefExpr( 3722 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 3723 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false); 3724 // If the loop control decl is explicitly marked as private, do not mark it 3725 // as captured again. 3726 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 3727 Captures.insert(std::make_pair(LCRef, Ref)); 3728 return Ref; 3729 } 3730 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 3731 DefaultLoc); 3732 } 3733 3734 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const { 3735 if (LCDecl && !LCDecl->isInvalidDecl()) { 3736 auto Type = LCDecl->getType().getNonReferenceType(); 3737 auto *PrivateVar = 3738 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(), 3739 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr); 3740 if (PrivateVar->isInvalidDecl()) 3741 return nullptr; 3742 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 3743 } 3744 return nullptr; 3745 } 3746 3747 /// \brief Build initialization of the counter to be used for codegen. 3748 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } 3749 3750 /// \brief Build step of the counter be used for codegen. 3751 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } 3752 3753 /// \brief Iteration space of a single for loop. 3754 struct LoopIterationSpace final { 3755 /// \brief Condition of the loop. 3756 Expr *PreCond = nullptr; 3757 /// \brief This expression calculates the number of iterations in the loop. 3758 /// It is always possible to calculate it before starting the loop. 3759 Expr *NumIterations = nullptr; 3760 /// \brief The loop counter variable. 3761 Expr *CounterVar = nullptr; 3762 /// \brief Private loop counter variable. 3763 Expr *PrivateCounterVar = nullptr; 3764 /// \brief This is initializer for the initial value of #CounterVar. 3765 Expr *CounterInit = nullptr; 3766 /// \brief This is step for the #CounterVar used to generate its update: 3767 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 3768 Expr *CounterStep = nullptr; 3769 /// \brief Should step be subtracted? 3770 bool Subtract = false; 3771 /// \brief Source range of the loop init. 3772 SourceRange InitSrcRange; 3773 /// \brief Source range of the loop condition. 3774 SourceRange CondSrcRange; 3775 /// \brief Source range of the loop increment. 3776 SourceRange IncSrcRange; 3777 }; 3778 3779 } // namespace 3780 3781 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 3782 assert(getLangOpts().OpenMP && "OpenMP is not active."); 3783 assert(Init && "Expected loop in canonical form."); 3784 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 3785 if (AssociatedLoops > 0 && 3786 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 3787 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 3788 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) { 3789 if (auto *D = ISC.GetLoopDecl()) { 3790 auto *VD = dyn_cast<VarDecl>(D); 3791 if (!VD) { 3792 if (auto *Private = IsOpenMPCapturedDecl(D)) 3793 VD = Private; 3794 else { 3795 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(), 3796 /*WithInit=*/false); 3797 VD = cast<VarDecl>(Ref->getDecl()); 3798 } 3799 } 3800 DSAStack->addLoopControlVariable(D, VD); 3801 } 3802 } 3803 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 3804 } 3805 } 3806 3807 /// \brief Called on a for stmt to check and extract its iteration space 3808 /// for further processing (such as collapsing). 3809 static bool CheckOpenMPIterationSpace( 3810 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 3811 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 3812 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, 3813 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, 3814 LoopIterationSpace &ResultIterSpace, 3815 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 3816 // OpenMP [2.6, Canonical Loop Form] 3817 // for (init-expr; test-expr; incr-expr) structured-block 3818 auto *For = dyn_cast_or_null<ForStmt>(S); 3819 if (!For) { 3820 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 3821 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 3822 << getOpenMPDirectiveName(DKind) << NestedLoopCount 3823 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 3824 if (NestedLoopCount > 1) { 3825 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 3826 SemaRef.Diag(DSA.getConstructLoc(), 3827 diag::note_omp_collapse_ordered_expr) 3828 << 2 << CollapseLoopCountExpr->getSourceRange() 3829 << OrderedLoopCountExpr->getSourceRange(); 3830 else if (CollapseLoopCountExpr) 3831 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 3832 diag::note_omp_collapse_ordered_expr) 3833 << 0 << CollapseLoopCountExpr->getSourceRange(); 3834 else 3835 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 3836 diag::note_omp_collapse_ordered_expr) 3837 << 1 << OrderedLoopCountExpr->getSourceRange(); 3838 } 3839 return true; 3840 } 3841 assert(For->getBody()); 3842 3843 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 3844 3845 // Check init. 3846 auto Init = For->getInit(); 3847 if (ISC.CheckInit(Init)) 3848 return true; 3849 3850 bool HasErrors = false; 3851 3852 // Check loop variable's type. 3853 if (auto *LCDecl = ISC.GetLoopDecl()) { 3854 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr(); 3855 3856 // OpenMP [2.6, Canonical Loop Form] 3857 // Var is one of the following: 3858 // A variable of signed or unsigned integer type. 3859 // For C++, a variable of a random access iterator type. 3860 // For C, a variable of a pointer type. 3861 auto VarType = LCDecl->getType().getNonReferenceType(); 3862 if (!VarType->isDependentType() && !VarType->isIntegerType() && 3863 !VarType->isPointerType() && 3864 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 3865 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 3866 << SemaRef.getLangOpts().CPlusPlus; 3867 HasErrors = true; 3868 } 3869 3870 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 3871 // a Construct 3872 // The loop iteration variable(s) in the associated for-loop(s) of a for or 3873 // parallel for construct is (are) private. 3874 // The loop iteration variable in the associated for-loop of a simd 3875 // construct with just one associated for-loop is linear with a 3876 // constant-linear-step that is the increment of the associated for-loop. 3877 // Exclude loop var from the list of variables with implicitly defined data 3878 // sharing attributes. 3879 VarsWithImplicitDSA.erase(LCDecl); 3880 3881 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 3882 // in a Construct, C/C++]. 3883 // The loop iteration variable in the associated for-loop of a simd 3884 // construct with just one associated for-loop may be listed in a linear 3885 // clause with a constant-linear-step that is the increment of the 3886 // associated for-loop. 3887 // The loop iteration variable(s) in the associated for-loop(s) of a for or 3888 // parallel for construct may be listed in a private or lastprivate clause. 3889 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 3890 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 3891 // declared in the loop and it is predetermined as a private. 3892 auto PredeterminedCKind = 3893 isOpenMPSimdDirective(DKind) 3894 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 3895 : OMPC_private; 3896 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 3897 DVar.CKind != PredeterminedCKind) || 3898 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 3899 isOpenMPDistributeDirective(DKind)) && 3900 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 3901 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 3902 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 3903 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 3904 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 3905 << getOpenMPClauseName(PredeterminedCKind); 3906 if (DVar.RefExpr == nullptr) 3907 DVar.CKind = PredeterminedCKind; 3908 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 3909 HasErrors = true; 3910 } else if (LoopDeclRefExpr != nullptr) { 3911 // Make the loop iteration variable private (for worksharing constructs), 3912 // linear (for simd directives with the only one associated loop) or 3913 // lastprivate (for simd directives with several collapsed or ordered 3914 // loops). 3915 if (DVar.CKind == OMPC_unknown) 3916 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, 3917 [](OpenMPDirectiveKind) -> bool { return true; }, 3918 /*FromParent=*/false); 3919 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 3920 } 3921 3922 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 3923 3924 // Check test-expr. 3925 HasErrors |= ISC.CheckCond(For->getCond()); 3926 3927 // Check incr-expr. 3928 HasErrors |= ISC.CheckInc(For->getInc()); 3929 } 3930 3931 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 3932 return HasErrors; 3933 3934 // Build the loop's iteration space representation. 3935 ResultIterSpace.PreCond = 3936 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures); 3937 ResultIterSpace.NumIterations = ISC.BuildNumIterations( 3938 DSA.getCurScope(), 3939 (isOpenMPWorksharingDirective(DKind) || 3940 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 3941 Captures); 3942 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA); 3943 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar(); 3944 ResultIterSpace.CounterInit = ISC.BuildCounterInit(); 3945 ResultIterSpace.CounterStep = ISC.BuildCounterStep(); 3946 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); 3947 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); 3948 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); 3949 ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); 3950 3951 HasErrors |= (ResultIterSpace.PreCond == nullptr || 3952 ResultIterSpace.NumIterations == nullptr || 3953 ResultIterSpace.CounterVar == nullptr || 3954 ResultIterSpace.PrivateCounterVar == nullptr || 3955 ResultIterSpace.CounterInit == nullptr || 3956 ResultIterSpace.CounterStep == nullptr); 3957 3958 return HasErrors; 3959 } 3960 3961 /// \brief Build 'VarRef = Start. 3962 static ExprResult 3963 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 3964 ExprResult Start, 3965 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 3966 // Build 'VarRef = Start. 3967 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 3968 if (!NewStart.isUsable()) 3969 return ExprError(); 3970 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 3971 VarRef.get()->getType())) { 3972 NewStart = SemaRef.PerformImplicitConversion( 3973 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 3974 /*AllowExplicit=*/true); 3975 if (!NewStart.isUsable()) 3976 return ExprError(); 3977 } 3978 3979 auto Init = 3980 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 3981 return Init; 3982 } 3983 3984 /// \brief Build 'VarRef = Start + Iter * Step'. 3985 static ExprResult 3986 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc, 3987 ExprResult VarRef, ExprResult Start, ExprResult Iter, 3988 ExprResult Step, bool Subtract, 3989 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) { 3990 // Add parentheses (for debugging purposes only). 3991 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 3992 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 3993 !Step.isUsable()) 3994 return ExprError(); 3995 3996 ExprResult NewStep = Step; 3997 if (Captures) 3998 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 3999 if (NewStep.isInvalid()) 4000 return ExprError(); 4001 ExprResult Update = 4002 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 4003 if (!Update.isUsable()) 4004 return ExprError(); 4005 4006 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 4007 // 'VarRef = Start (+|-) Iter * Step'. 4008 ExprResult NewStart = Start; 4009 if (Captures) 4010 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 4011 if (NewStart.isInvalid()) 4012 return ExprError(); 4013 4014 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 4015 ExprResult SavedUpdate = Update; 4016 ExprResult UpdateVal; 4017 if (VarRef.get()->getType()->isOverloadableType() || 4018 NewStart.get()->getType()->isOverloadableType() || 4019 Update.get()->getType()->isOverloadableType()) { 4020 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4021 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4022 Update = 4023 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4024 if (Update.isUsable()) { 4025 UpdateVal = 4026 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 4027 VarRef.get(), SavedUpdate.get()); 4028 if (UpdateVal.isUsable()) { 4029 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 4030 UpdateVal.get()); 4031 } 4032 } 4033 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4034 } 4035 4036 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 4037 if (!Update.isUsable() || !UpdateVal.isUsable()) { 4038 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 4039 NewStart.get(), SavedUpdate.get()); 4040 if (!Update.isUsable()) 4041 return ExprError(); 4042 4043 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 4044 VarRef.get()->getType())) { 4045 Update = SemaRef.PerformImplicitConversion( 4046 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 4047 if (!Update.isUsable()) 4048 return ExprError(); 4049 } 4050 4051 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 4052 } 4053 return Update; 4054 } 4055 4056 /// \brief Convert integer expression \a E to make it have at least \a Bits 4057 /// bits. 4058 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 4059 if (E == nullptr) 4060 return ExprError(); 4061 auto &C = SemaRef.Context; 4062 QualType OldType = E->getType(); 4063 unsigned HasBits = C.getTypeSize(OldType); 4064 if (HasBits >= Bits) 4065 return ExprResult(E); 4066 // OK to convert to signed, because new type has more bits than old. 4067 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 4068 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 4069 true); 4070 } 4071 4072 /// \brief Check if the given expression \a E is a constant integer that fits 4073 /// into \a Bits bits. 4074 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { 4075 if (E == nullptr) 4076 return false; 4077 llvm::APSInt Result; 4078 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 4079 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 4080 return false; 4081 } 4082 4083 /// Build preinits statement for the given declarations. 4084 static Stmt *buildPreInits(ASTContext &Context, 4085 SmallVectorImpl<Decl *> &PreInits) { 4086 if (!PreInits.empty()) { 4087 return new (Context) DeclStmt( 4088 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 4089 SourceLocation(), SourceLocation()); 4090 } 4091 return nullptr; 4092 } 4093 4094 /// Build preinits statement for the given declarations. 4095 static Stmt *buildPreInits(ASTContext &Context, 4096 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 4097 if (!Captures.empty()) { 4098 SmallVector<Decl *, 16> PreInits; 4099 for (auto &Pair : Captures) 4100 PreInits.push_back(Pair.second->getDecl()); 4101 return buildPreInits(Context, PreInits); 4102 } 4103 return nullptr; 4104 } 4105 4106 /// Build postupdate expression for the given list of postupdates expressions. 4107 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 4108 Expr *PostUpdate = nullptr; 4109 if (!PostUpdates.empty()) { 4110 for (auto *E : PostUpdates) { 4111 Expr *ConvE = S.BuildCStyleCastExpr( 4112 E->getExprLoc(), 4113 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 4114 E->getExprLoc(), E) 4115 .get(); 4116 PostUpdate = PostUpdate 4117 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 4118 PostUpdate, ConvE) 4119 .get() 4120 : ConvE; 4121 } 4122 } 4123 return PostUpdate; 4124 } 4125 4126 /// \brief Called on a for stmt to check itself and nested loops (if any). 4127 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 4128 /// number of collapsed loops otherwise. 4129 static unsigned 4130 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 4131 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 4132 DSAStackTy &DSA, 4133 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, 4134 OMPLoopDirective::HelperExprs &Built) { 4135 unsigned NestedLoopCount = 1; 4136 if (CollapseLoopCountExpr) { 4137 // Found 'collapse' clause - calculate collapse number. 4138 llvm::APSInt Result; 4139 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 4140 NestedLoopCount = Result.getLimitedValue(); 4141 } 4142 if (OrderedLoopCountExpr) { 4143 // Found 'ordered' clause - calculate collapse number. 4144 llvm::APSInt Result; 4145 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 4146 if (Result.getLimitedValue() < NestedLoopCount) { 4147 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4148 diag::err_omp_wrong_ordered_loop_count) 4149 << OrderedLoopCountExpr->getSourceRange(); 4150 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4151 diag::note_collapse_loop_count) 4152 << CollapseLoopCountExpr->getSourceRange(); 4153 } 4154 NestedLoopCount = Result.getLimitedValue(); 4155 } 4156 } 4157 // This is helper routine for loop directives (e.g., 'for', 'simd', 4158 // 'for simd', etc.). 4159 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 4160 SmallVector<LoopIterationSpace, 4> IterSpaces; 4161 IterSpaces.resize(NestedLoopCount); 4162 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 4163 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 4164 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 4165 NestedLoopCount, CollapseLoopCountExpr, 4166 OrderedLoopCountExpr, VarsWithImplicitDSA, 4167 IterSpaces[Cnt], Captures)) 4168 return 0; 4169 // Move on to the next nested for loop, or to the loop body. 4170 // OpenMP [2.8.1, simd construct, Restrictions] 4171 // All loops associated with the construct must be perfectly nested; that 4172 // is, there must be no intervening code nor any OpenMP directive between 4173 // any two loops. 4174 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 4175 } 4176 4177 Built.clear(/* size */ NestedLoopCount); 4178 4179 if (SemaRef.CurContext->isDependentContext()) 4180 return NestedLoopCount; 4181 4182 // An example of what is generated for the following code: 4183 // 4184 // #pragma omp simd collapse(2) ordered(2) 4185 // for (i = 0; i < NI; ++i) 4186 // for (k = 0; k < NK; ++k) 4187 // for (j = J0; j < NJ; j+=2) { 4188 // <loop body> 4189 // } 4190 // 4191 // We generate the code below. 4192 // Note: the loop body may be outlined in CodeGen. 4193 // Note: some counters may be C++ classes, operator- is used to find number of 4194 // iterations and operator+= to calculate counter value. 4195 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 4196 // or i64 is currently supported). 4197 // 4198 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 4199 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 4200 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 4201 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 4202 // // similar updates for vars in clauses (e.g. 'linear') 4203 // <loop body (using local i and j)> 4204 // } 4205 // i = NI; // assign final values of counters 4206 // j = NJ; 4207 // 4208 4209 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 4210 // the iteration counts of the collapsed for loops. 4211 // Precondition tests if there is at least one iteration (all conditions are 4212 // true). 4213 auto PreCond = ExprResult(IterSpaces[0].PreCond); 4214 auto N0 = IterSpaces[0].NumIterations; 4215 ExprResult LastIteration32 = WidenIterationCount( 4216 32 /* Bits */, SemaRef 4217 .PerformImplicitConversion( 4218 N0->IgnoreImpCasts(), N0->getType(), 4219 Sema::AA_Converting, /*AllowExplicit=*/true) 4220 .get(), 4221 SemaRef); 4222 ExprResult LastIteration64 = WidenIterationCount( 4223 64 /* Bits */, SemaRef 4224 .PerformImplicitConversion( 4225 N0->IgnoreImpCasts(), N0->getType(), 4226 Sema::AA_Converting, /*AllowExplicit=*/true) 4227 .get(), 4228 SemaRef); 4229 4230 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 4231 return NestedLoopCount; 4232 4233 auto &C = SemaRef.Context; 4234 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 4235 4236 Scope *CurScope = DSA.getCurScope(); 4237 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 4238 if (PreCond.isUsable()) { 4239 PreCond = 4240 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 4241 PreCond.get(), IterSpaces[Cnt].PreCond); 4242 } 4243 auto N = IterSpaces[Cnt].NumIterations; 4244 SourceLocation Loc = N->getExprLoc(); 4245 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 4246 if (LastIteration32.isUsable()) 4247 LastIteration32 = SemaRef.BuildBinOp( 4248 CurScope, Loc, BO_Mul, LastIteration32.get(), 4249 SemaRef 4250 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4251 Sema::AA_Converting, 4252 /*AllowExplicit=*/true) 4253 .get()); 4254 if (LastIteration64.isUsable()) 4255 LastIteration64 = SemaRef.BuildBinOp( 4256 CurScope, Loc, BO_Mul, LastIteration64.get(), 4257 SemaRef 4258 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4259 Sema::AA_Converting, 4260 /*AllowExplicit=*/true) 4261 .get()); 4262 } 4263 4264 // Choose either the 32-bit or 64-bit version. 4265 ExprResult LastIteration = LastIteration64; 4266 if (LastIteration32.isUsable() && 4267 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 4268 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 4269 FitsInto( 4270 32 /* Bits */, 4271 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 4272 LastIteration64.get(), SemaRef))) 4273 LastIteration = LastIteration32; 4274 QualType VType = LastIteration.get()->getType(); 4275 QualType RealVType = VType; 4276 QualType StrideVType = VType; 4277 if (isOpenMPTaskLoopDirective(DKind)) { 4278 VType = 4279 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 4280 StrideVType = 4281 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 4282 } 4283 4284 if (!LastIteration.isUsable()) 4285 return 0; 4286 4287 // Save the number of iterations. 4288 ExprResult NumIterations = LastIteration; 4289 { 4290 LastIteration = SemaRef.BuildBinOp( 4291 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 4292 LastIteration.get(), 4293 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4294 if (!LastIteration.isUsable()) 4295 return 0; 4296 } 4297 4298 // Calculate the last iteration number beforehand instead of doing this on 4299 // each iteration. Do not do this if the number of iterations may be kfold-ed. 4300 llvm::APSInt Result; 4301 bool IsConstant = 4302 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 4303 ExprResult CalcLastIteration; 4304 if (!IsConstant) { 4305 ExprResult SaveRef = 4306 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 4307 LastIteration = SaveRef; 4308 4309 // Prepare SaveRef + 1. 4310 NumIterations = SemaRef.BuildBinOp( 4311 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 4312 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4313 if (!NumIterations.isUsable()) 4314 return 0; 4315 } 4316 4317 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 4318 4319 // Build variables passed into runtime, necessary for worksharing directives. 4320 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 4321 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 4322 isOpenMPDistributeDirective(DKind)) { 4323 // Lower bound variable, initialized with zero. 4324 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 4325 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 4326 SemaRef.AddInitializerToDecl(LBDecl, 4327 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4328 /*DirectInit*/ false); 4329 4330 // Upper bound variable, initialized with last iteration number. 4331 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 4332 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 4333 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 4334 /*DirectInit*/ false); 4335 4336 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 4337 // This will be used to implement clause 'lastprivate'. 4338 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 4339 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 4340 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 4341 SemaRef.AddInitializerToDecl(ILDecl, 4342 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4343 /*DirectInit*/ false); 4344 4345 // Stride variable returned by runtime (we initialize it to 1 by default). 4346 VarDecl *STDecl = 4347 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 4348 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 4349 SemaRef.AddInitializerToDecl(STDecl, 4350 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 4351 /*DirectInit*/ false); 4352 4353 // Build expression: UB = min(UB, LastIteration) 4354 // It is necessary for CodeGen of directives with static scheduling. 4355 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 4356 UB.get(), LastIteration.get()); 4357 ExprResult CondOp = SemaRef.ActOnConditionalOp( 4358 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); 4359 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 4360 CondOp.get()); 4361 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 4362 4363 // If we have a combined directive that combines 'distribute', 'for' or 4364 // 'simd' we need to be able to access the bounds of the schedule of the 4365 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 4366 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 4367 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4368 4369 // Lower bound variable, initialized with zero. 4370 VarDecl *CombLBDecl = 4371 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 4372 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 4373 SemaRef.AddInitializerToDecl( 4374 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4375 /*DirectInit*/ false); 4376 4377 // Upper bound variable, initialized with last iteration number. 4378 VarDecl *CombUBDecl = 4379 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 4380 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 4381 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 4382 /*DirectInit*/ false); 4383 4384 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 4385 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 4386 ExprResult CombCondOp = 4387 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 4388 LastIteration.get(), CombUB.get()); 4389 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 4390 CombCondOp.get()); 4391 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get()); 4392 4393 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 4394 // We expect to have at least 2 more parameters than the 'parallel' 4395 // directive does - the lower and upper bounds of the previous schedule. 4396 assert(CD->getNumParams() >= 4 && 4397 "Unexpected number of parameters in loop combined directive"); 4398 4399 // Set the proper type for the bounds given what we learned from the 4400 // enclosed loops. 4401 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 4402 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 4403 4404 // Previous lower and upper bounds are obtained from the region 4405 // parameters. 4406 PrevLB = 4407 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 4408 PrevUB = 4409 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 4410 } 4411 } 4412 4413 // Build the iteration variable and its initialization before loop. 4414 ExprResult IV; 4415 ExprResult Init, CombInit; 4416 { 4417 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 4418 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 4419 Expr *RHS = 4420 (isOpenMPWorksharingDirective(DKind) || 4421 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4422 ? LB.get() 4423 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4424 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 4425 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 4426 4427 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4428 Expr *CombRHS = 4429 (isOpenMPWorksharingDirective(DKind) || 4430 isOpenMPTaskLoopDirective(DKind) || 4431 isOpenMPDistributeDirective(DKind)) 4432 ? CombLB.get() 4433 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4434 CombInit = 4435 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 4436 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get()); 4437 } 4438 } 4439 4440 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 4441 SourceLocation CondLoc; 4442 ExprResult Cond = 4443 (isOpenMPWorksharingDirective(DKind) || 4444 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4445 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 4446 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 4447 NumIterations.get()); 4448 ExprResult CombCond; 4449 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4450 CombCond = 4451 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get()); 4452 } 4453 // Loop increment (IV = IV + 1) 4454 SourceLocation IncLoc; 4455 ExprResult Inc = 4456 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 4457 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 4458 if (!Inc.isUsable()) 4459 return 0; 4460 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 4461 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 4462 if (!Inc.isUsable()) 4463 return 0; 4464 4465 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 4466 // Used for directives with static scheduling. 4467 // In combined construct, add combined version that use CombLB and CombUB 4468 // base variables for the update 4469 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 4470 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 4471 isOpenMPDistributeDirective(DKind)) { 4472 // LB + ST 4473 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 4474 if (!NextLB.isUsable()) 4475 return 0; 4476 // LB = LB + ST 4477 NextLB = 4478 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 4479 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 4480 if (!NextLB.isUsable()) 4481 return 0; 4482 // UB + ST 4483 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 4484 if (!NextUB.isUsable()) 4485 return 0; 4486 // UB = UB + ST 4487 NextUB = 4488 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 4489 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 4490 if (!NextUB.isUsable()) 4491 return 0; 4492 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4493 CombNextLB = 4494 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 4495 if (!NextLB.isUsable()) 4496 return 0; 4497 // LB = LB + ST 4498 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 4499 CombNextLB.get()); 4500 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get()); 4501 if (!CombNextLB.isUsable()) 4502 return 0; 4503 // UB + ST 4504 CombNextUB = 4505 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 4506 if (!CombNextUB.isUsable()) 4507 return 0; 4508 // UB = UB + ST 4509 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 4510 CombNextUB.get()); 4511 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get()); 4512 if (!CombNextUB.isUsable()) 4513 return 0; 4514 } 4515 } 4516 4517 // Create increment expression for distribute loop when combined in a same 4518 // directive with for as IV = IV + ST; ensure upper bound expression based 4519 // on PrevUB instead of NumIterations - used to implement 'for' when found 4520 // in combination with 'distribute', like in 'distribute parallel for' 4521 SourceLocation DistIncLoc; 4522 ExprResult DistCond, DistInc, PrevEUB; 4523 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4524 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()); 4525 assert(DistCond.isUsable() && "distribute cond expr was not built"); 4526 4527 DistInc = 4528 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 4529 assert(DistInc.isUsable() && "distribute inc expr was not built"); 4530 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 4531 DistInc.get()); 4532 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get()); 4533 assert(DistInc.isUsable() && "distribute inc expr was not built"); 4534 4535 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 4536 // construct 4537 SourceLocation DistEUBLoc; 4538 ExprResult IsUBGreater = 4539 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 4540 ExprResult CondOp = SemaRef.ActOnConditionalOp( 4541 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 4542 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 4543 CondOp.get()); 4544 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get()); 4545 } 4546 4547 // Build updates and final values of the loop counters. 4548 bool HasErrors = false; 4549 Built.Counters.resize(NestedLoopCount); 4550 Built.Inits.resize(NestedLoopCount); 4551 Built.Updates.resize(NestedLoopCount); 4552 Built.Finals.resize(NestedLoopCount); 4553 SmallVector<Expr *, 4> LoopMultipliers; 4554 { 4555 ExprResult Div; 4556 // Go from inner nested loop to outer. 4557 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 4558 LoopIterationSpace &IS = IterSpaces[Cnt]; 4559 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 4560 // Build: Iter = (IV / Div) % IS.NumIters 4561 // where Div is product of previous iterations' IS.NumIters. 4562 ExprResult Iter; 4563 if (Div.isUsable()) { 4564 Iter = 4565 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 4566 } else { 4567 Iter = IV; 4568 assert((Cnt == (int)NestedLoopCount - 1) && 4569 "unusable div expected on first iteration only"); 4570 } 4571 4572 if (Cnt != 0 && Iter.isUsable()) 4573 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 4574 IS.NumIterations); 4575 if (!Iter.isUsable()) { 4576 HasErrors = true; 4577 break; 4578 } 4579 4580 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 4581 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 4582 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(), 4583 IS.CounterVar->getExprLoc(), 4584 /*RefersToCapture=*/true); 4585 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 4586 IS.CounterInit, Captures); 4587 if (!Init.isUsable()) { 4588 HasErrors = true; 4589 break; 4590 } 4591 ExprResult Update = BuildCounterUpdate( 4592 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 4593 IS.CounterStep, IS.Subtract, &Captures); 4594 if (!Update.isUsable()) { 4595 HasErrors = true; 4596 break; 4597 } 4598 4599 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 4600 ExprResult Final = BuildCounterUpdate( 4601 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 4602 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 4603 if (!Final.isUsable()) { 4604 HasErrors = true; 4605 break; 4606 } 4607 4608 // Build Div for the next iteration: Div <- Div * IS.NumIters 4609 if (Cnt != 0) { 4610 if (Div.isUnset()) 4611 Div = IS.NumIterations; 4612 else 4613 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 4614 IS.NumIterations); 4615 4616 // Add parentheses (for debugging purposes only). 4617 if (Div.isUsable()) 4618 Div = tryBuildCapture(SemaRef, Div.get(), Captures); 4619 if (!Div.isUsable()) { 4620 HasErrors = true; 4621 break; 4622 } 4623 LoopMultipliers.push_back(Div.get()); 4624 } 4625 if (!Update.isUsable() || !Final.isUsable()) { 4626 HasErrors = true; 4627 break; 4628 } 4629 // Save results 4630 Built.Counters[Cnt] = IS.CounterVar; 4631 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 4632 Built.Inits[Cnt] = Init.get(); 4633 Built.Updates[Cnt] = Update.get(); 4634 Built.Finals[Cnt] = Final.get(); 4635 } 4636 } 4637 4638 if (HasErrors) 4639 return 0; 4640 4641 // Save results 4642 Built.IterationVarRef = IV.get(); 4643 Built.LastIteration = LastIteration.get(); 4644 Built.NumIterations = NumIterations.get(); 4645 Built.CalcLastIteration = 4646 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 4647 Built.PreCond = PreCond.get(); 4648 Built.PreInits = buildPreInits(C, Captures); 4649 Built.Cond = Cond.get(); 4650 Built.Init = Init.get(); 4651 Built.Inc = Inc.get(); 4652 Built.LB = LB.get(); 4653 Built.UB = UB.get(); 4654 Built.IL = IL.get(); 4655 Built.ST = ST.get(); 4656 Built.EUB = EUB.get(); 4657 Built.NLB = NextLB.get(); 4658 Built.NUB = NextUB.get(); 4659 Built.PrevLB = PrevLB.get(); 4660 Built.PrevUB = PrevUB.get(); 4661 Built.DistInc = DistInc.get(); 4662 Built.PrevEUB = PrevEUB.get(); 4663 Built.DistCombinedFields.LB = CombLB.get(); 4664 Built.DistCombinedFields.UB = CombUB.get(); 4665 Built.DistCombinedFields.EUB = CombEUB.get(); 4666 Built.DistCombinedFields.Init = CombInit.get(); 4667 Built.DistCombinedFields.Cond = CombCond.get(); 4668 Built.DistCombinedFields.NLB = CombNextLB.get(); 4669 Built.DistCombinedFields.NUB = CombNextUB.get(); 4670 4671 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get(); 4672 // Fill data for doacross depend clauses. 4673 for (auto Pair : DSA.getDoacrossDependClauses()) { 4674 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 4675 Pair.first->setCounterValue(CounterVal); 4676 else { 4677 if (NestedLoopCount != Pair.second.size() || 4678 NestedLoopCount != LoopMultipliers.size() + 1) { 4679 // Erroneous case - clause has some problems. 4680 Pair.first->setCounterValue(CounterVal); 4681 continue; 4682 } 4683 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink); 4684 auto I = Pair.second.rbegin(); 4685 auto IS = IterSpaces.rbegin(); 4686 auto ILM = LoopMultipliers.rbegin(); 4687 Expr *UpCounterVal = CounterVal; 4688 Expr *Multiplier = nullptr; 4689 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 4690 if (I->first) { 4691 assert(IS->CounterStep); 4692 Expr *NormalizedOffset = 4693 SemaRef 4694 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div, 4695 I->first, IS->CounterStep) 4696 .get(); 4697 if (Multiplier) { 4698 NormalizedOffset = 4699 SemaRef 4700 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul, 4701 NormalizedOffset, Multiplier) 4702 .get(); 4703 } 4704 assert(I->second == OO_Plus || I->second == OO_Minus); 4705 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub; 4706 UpCounterVal = SemaRef 4707 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK, 4708 UpCounterVal, NormalizedOffset) 4709 .get(); 4710 } 4711 Multiplier = *ILM; 4712 ++I; 4713 ++IS; 4714 ++ILM; 4715 } 4716 Pair.first->setCounterValue(UpCounterVal); 4717 } 4718 } 4719 4720 return NestedLoopCount; 4721 } 4722 4723 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 4724 auto CollapseClauses = 4725 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 4726 if (CollapseClauses.begin() != CollapseClauses.end()) 4727 return (*CollapseClauses.begin())->getNumForLoops(); 4728 return nullptr; 4729 } 4730 4731 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 4732 auto OrderedClauses = 4733 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 4734 if (OrderedClauses.begin() != OrderedClauses.end()) 4735 return (*OrderedClauses.begin())->getNumForLoops(); 4736 return nullptr; 4737 } 4738 4739 static bool checkSimdlenSafelenSpecified(Sema &S, 4740 const ArrayRef<OMPClause *> Clauses) { 4741 OMPSafelenClause *Safelen = nullptr; 4742 OMPSimdlenClause *Simdlen = nullptr; 4743 4744 for (auto *Clause : Clauses) { 4745 if (Clause->getClauseKind() == OMPC_safelen) 4746 Safelen = cast<OMPSafelenClause>(Clause); 4747 else if (Clause->getClauseKind() == OMPC_simdlen) 4748 Simdlen = cast<OMPSimdlenClause>(Clause); 4749 if (Safelen && Simdlen) 4750 break; 4751 } 4752 4753 if (Simdlen && Safelen) { 4754 llvm::APSInt SimdlenRes, SafelenRes; 4755 auto SimdlenLength = Simdlen->getSimdlen(); 4756 auto SafelenLength = Safelen->getSafelen(); 4757 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 4758 SimdlenLength->isInstantiationDependent() || 4759 SimdlenLength->containsUnexpandedParameterPack()) 4760 return false; 4761 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 4762 SafelenLength->isInstantiationDependent() || 4763 SafelenLength->containsUnexpandedParameterPack()) 4764 return false; 4765 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); 4766 SafelenLength->EvaluateAsInt(SafelenRes, S.Context); 4767 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 4768 // If both simdlen and safelen clauses are specified, the value of the 4769 // simdlen parameter must be less than or equal to the value of the safelen 4770 // parameter. 4771 if (SimdlenRes > SafelenRes) { 4772 S.Diag(SimdlenLength->getExprLoc(), 4773 diag::err_omp_wrong_simdlen_safelen_values) 4774 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 4775 return true; 4776 } 4777 } 4778 return false; 4779 } 4780 4781 StmtResult Sema::ActOnOpenMPSimdDirective( 4782 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4783 SourceLocation EndLoc, 4784 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4785 if (!AStmt) 4786 return StmtError(); 4787 4788 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4789 OMPLoopDirective::HelperExprs B; 4790 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4791 // define the nested loops number. 4792 unsigned NestedLoopCount = CheckOpenMPLoop( 4793 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 4794 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 4795 if (NestedLoopCount == 0) 4796 return StmtError(); 4797 4798 assert((CurContext->isDependentContext() || B.builtAll()) && 4799 "omp simd loop exprs were not built"); 4800 4801 if (!CurContext->isDependentContext()) { 4802 // Finalize the clauses that need pre-built expressions for CodeGen. 4803 for (auto C : Clauses) { 4804 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4805 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4806 B.NumIterations, *this, CurScope, 4807 DSAStack)) 4808 return StmtError(); 4809 } 4810 } 4811 4812 if (checkSimdlenSafelenSpecified(*this, Clauses)) 4813 return StmtError(); 4814 4815 getCurFunction()->setHasBranchProtectedScope(); 4816 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4817 Clauses, AStmt, B); 4818 } 4819 4820 StmtResult Sema::ActOnOpenMPForDirective( 4821 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4822 SourceLocation EndLoc, 4823 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4824 if (!AStmt) 4825 return StmtError(); 4826 4827 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4828 OMPLoopDirective::HelperExprs B; 4829 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4830 // define the nested loops number. 4831 unsigned NestedLoopCount = CheckOpenMPLoop( 4832 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 4833 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 4834 if (NestedLoopCount == 0) 4835 return StmtError(); 4836 4837 assert((CurContext->isDependentContext() || B.builtAll()) && 4838 "omp for loop exprs were not built"); 4839 4840 if (!CurContext->isDependentContext()) { 4841 // Finalize the clauses that need pre-built expressions for CodeGen. 4842 for (auto C : Clauses) { 4843 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4844 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4845 B.NumIterations, *this, CurScope, 4846 DSAStack)) 4847 return StmtError(); 4848 } 4849 } 4850 4851 getCurFunction()->setHasBranchProtectedScope(); 4852 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4853 Clauses, AStmt, B, DSAStack->isCancelRegion()); 4854 } 4855 4856 StmtResult Sema::ActOnOpenMPForSimdDirective( 4857 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4858 SourceLocation EndLoc, 4859 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4860 if (!AStmt) 4861 return StmtError(); 4862 4863 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4864 OMPLoopDirective::HelperExprs B; 4865 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4866 // define the nested loops number. 4867 unsigned NestedLoopCount = 4868 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 4869 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 4870 VarsWithImplicitDSA, B); 4871 if (NestedLoopCount == 0) 4872 return StmtError(); 4873 4874 assert((CurContext->isDependentContext() || B.builtAll()) && 4875 "omp for simd loop exprs were not built"); 4876 4877 if (!CurContext->isDependentContext()) { 4878 // Finalize the clauses that need pre-built expressions for CodeGen. 4879 for (auto C : Clauses) { 4880 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4881 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4882 B.NumIterations, *this, CurScope, 4883 DSAStack)) 4884 return StmtError(); 4885 } 4886 } 4887 4888 if (checkSimdlenSafelenSpecified(*this, Clauses)) 4889 return StmtError(); 4890 4891 getCurFunction()->setHasBranchProtectedScope(); 4892 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4893 Clauses, AStmt, B); 4894 } 4895 4896 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 4897 Stmt *AStmt, 4898 SourceLocation StartLoc, 4899 SourceLocation EndLoc) { 4900 if (!AStmt) 4901 return StmtError(); 4902 4903 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4904 auto BaseStmt = AStmt; 4905 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 4906 BaseStmt = CS->getCapturedStmt(); 4907 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 4908 auto S = C->children(); 4909 if (S.begin() == S.end()) 4910 return StmtError(); 4911 // All associated statements must be '#pragma omp section' except for 4912 // the first one. 4913 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 4914 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 4915 if (SectionStmt) 4916 Diag(SectionStmt->getLocStart(), 4917 diag::err_omp_sections_substmt_not_section); 4918 return StmtError(); 4919 } 4920 cast<OMPSectionDirective>(SectionStmt) 4921 ->setHasCancel(DSAStack->isCancelRegion()); 4922 } 4923 } else { 4924 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 4925 return StmtError(); 4926 } 4927 4928 getCurFunction()->setHasBranchProtectedScope(); 4929 4930 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4931 DSAStack->isCancelRegion()); 4932 } 4933 4934 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 4935 SourceLocation StartLoc, 4936 SourceLocation EndLoc) { 4937 if (!AStmt) 4938 return StmtError(); 4939 4940 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4941 4942 getCurFunction()->setHasBranchProtectedScope(); 4943 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 4944 4945 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 4946 DSAStack->isCancelRegion()); 4947 } 4948 4949 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 4950 Stmt *AStmt, 4951 SourceLocation StartLoc, 4952 SourceLocation EndLoc) { 4953 if (!AStmt) 4954 return StmtError(); 4955 4956 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4957 4958 getCurFunction()->setHasBranchProtectedScope(); 4959 4960 // OpenMP [2.7.3, single Construct, Restrictions] 4961 // The copyprivate clause must not be used with the nowait clause. 4962 OMPClause *Nowait = nullptr; 4963 OMPClause *Copyprivate = nullptr; 4964 for (auto *Clause : Clauses) { 4965 if (Clause->getClauseKind() == OMPC_nowait) 4966 Nowait = Clause; 4967 else if (Clause->getClauseKind() == OMPC_copyprivate) 4968 Copyprivate = Clause; 4969 if (Copyprivate && Nowait) { 4970 Diag(Copyprivate->getLocStart(), 4971 diag::err_omp_single_copyprivate_with_nowait); 4972 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); 4973 return StmtError(); 4974 } 4975 } 4976 4977 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 4978 } 4979 4980 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 4981 SourceLocation StartLoc, 4982 SourceLocation EndLoc) { 4983 if (!AStmt) 4984 return StmtError(); 4985 4986 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4987 4988 getCurFunction()->setHasBranchProtectedScope(); 4989 4990 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 4991 } 4992 4993 StmtResult Sema::ActOnOpenMPCriticalDirective( 4994 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 4995 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 4996 if (!AStmt) 4997 return StmtError(); 4998 4999 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5000 5001 bool ErrorFound = false; 5002 llvm::APSInt Hint; 5003 SourceLocation HintLoc; 5004 bool DependentHint = false; 5005 for (auto *C : Clauses) { 5006 if (C->getClauseKind() == OMPC_hint) { 5007 if (!DirName.getName()) { 5008 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name); 5009 ErrorFound = true; 5010 } 5011 Expr *E = cast<OMPHintClause>(C)->getHint(); 5012 if (E->isTypeDependent() || E->isValueDependent() || 5013 E->isInstantiationDependent()) 5014 DependentHint = true; 5015 else { 5016 Hint = E->EvaluateKnownConstInt(Context); 5017 HintLoc = C->getLocStart(); 5018 } 5019 } 5020 } 5021 if (ErrorFound) 5022 return StmtError(); 5023 auto Pair = DSAStack->getCriticalWithHint(DirName); 5024 if (Pair.first && DirName.getName() && !DependentHint) { 5025 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 5026 Diag(StartLoc, diag::err_omp_critical_with_hint); 5027 if (HintLoc.isValid()) { 5028 Diag(HintLoc, diag::note_omp_critical_hint_here) 5029 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 5030 } else 5031 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 5032 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 5033 Diag(C->getLocStart(), diag::note_omp_critical_hint_here) 5034 << 1 5035 << C->getHint()->EvaluateKnownConstInt(Context).toString( 5036 /*Radix=*/10, /*Signed=*/false); 5037 } else 5038 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1; 5039 } 5040 } 5041 5042 getCurFunction()->setHasBranchProtectedScope(); 5043 5044 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 5045 Clauses, AStmt); 5046 if (!Pair.first && DirName.getName() && !DependentHint) 5047 DSAStack->addCriticalWithHint(Dir, Hint); 5048 return Dir; 5049 } 5050 5051 StmtResult Sema::ActOnOpenMPParallelForDirective( 5052 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5053 SourceLocation EndLoc, 5054 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5055 if (!AStmt) 5056 return StmtError(); 5057 5058 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5059 // 1.2.2 OpenMP Language Terminology 5060 // Structured block - An executable statement with a single entry at the 5061 // top and a single exit at the bottom. 5062 // The point of exit cannot be a branch out of the structured block. 5063 // longjmp() and throw() must not violate the entry/exit criteria. 5064 CS->getCapturedDecl()->setNothrow(); 5065 5066 OMPLoopDirective::HelperExprs B; 5067 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5068 // define the nested loops number. 5069 unsigned NestedLoopCount = 5070 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 5071 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5072 VarsWithImplicitDSA, B); 5073 if (NestedLoopCount == 0) 5074 return StmtError(); 5075 5076 assert((CurContext->isDependentContext() || B.builtAll()) && 5077 "omp parallel for loop exprs were not built"); 5078 5079 if (!CurContext->isDependentContext()) { 5080 // Finalize the clauses that need pre-built expressions for CodeGen. 5081 for (auto C : Clauses) { 5082 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5083 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5084 B.NumIterations, *this, CurScope, 5085 DSAStack)) 5086 return StmtError(); 5087 } 5088 } 5089 5090 getCurFunction()->setHasBranchProtectedScope(); 5091 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 5092 NestedLoopCount, Clauses, AStmt, B, 5093 DSAStack->isCancelRegion()); 5094 } 5095 5096 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 5097 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5098 SourceLocation EndLoc, 5099 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5100 if (!AStmt) 5101 return StmtError(); 5102 5103 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5104 // 1.2.2 OpenMP Language Terminology 5105 // Structured block - An executable statement with a single entry at the 5106 // top and a single exit at the bottom. 5107 // The point of exit cannot be a branch out of the structured block. 5108 // longjmp() and throw() must not violate the entry/exit criteria. 5109 CS->getCapturedDecl()->setNothrow(); 5110 5111 OMPLoopDirective::HelperExprs B; 5112 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5113 // define the nested loops number. 5114 unsigned NestedLoopCount = 5115 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 5116 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5117 VarsWithImplicitDSA, B); 5118 if (NestedLoopCount == 0) 5119 return StmtError(); 5120 5121 if (!CurContext->isDependentContext()) { 5122 // Finalize the clauses that need pre-built expressions for CodeGen. 5123 for (auto C : Clauses) { 5124 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5125 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5126 B.NumIterations, *this, CurScope, 5127 DSAStack)) 5128 return StmtError(); 5129 } 5130 } 5131 5132 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5133 return StmtError(); 5134 5135 getCurFunction()->setHasBranchProtectedScope(); 5136 return OMPParallelForSimdDirective::Create( 5137 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 5138 } 5139 5140 StmtResult 5141 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 5142 Stmt *AStmt, SourceLocation StartLoc, 5143 SourceLocation EndLoc) { 5144 if (!AStmt) 5145 return StmtError(); 5146 5147 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5148 auto BaseStmt = AStmt; 5149 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5150 BaseStmt = CS->getCapturedStmt(); 5151 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5152 auto S = C->children(); 5153 if (S.begin() == S.end()) 5154 return StmtError(); 5155 // All associated statements must be '#pragma omp section' except for 5156 // the first one. 5157 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5158 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5159 if (SectionStmt) 5160 Diag(SectionStmt->getLocStart(), 5161 diag::err_omp_parallel_sections_substmt_not_section); 5162 return StmtError(); 5163 } 5164 cast<OMPSectionDirective>(SectionStmt) 5165 ->setHasCancel(DSAStack->isCancelRegion()); 5166 } 5167 } else { 5168 Diag(AStmt->getLocStart(), 5169 diag::err_omp_parallel_sections_not_compound_stmt); 5170 return StmtError(); 5171 } 5172 5173 getCurFunction()->setHasBranchProtectedScope(); 5174 5175 return OMPParallelSectionsDirective::Create( 5176 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 5177 } 5178 5179 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 5180 Stmt *AStmt, SourceLocation StartLoc, 5181 SourceLocation EndLoc) { 5182 if (!AStmt) 5183 return StmtError(); 5184 5185 auto *CS = cast<CapturedStmt>(AStmt); 5186 // 1.2.2 OpenMP Language Terminology 5187 // Structured block - An executable statement with a single entry at the 5188 // top and a single exit at the bottom. 5189 // The point of exit cannot be a branch out of the structured block. 5190 // longjmp() and throw() must not violate the entry/exit criteria. 5191 CS->getCapturedDecl()->setNothrow(); 5192 5193 getCurFunction()->setHasBranchProtectedScope(); 5194 5195 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5196 DSAStack->isCancelRegion()); 5197 } 5198 5199 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 5200 SourceLocation EndLoc) { 5201 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 5202 } 5203 5204 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 5205 SourceLocation EndLoc) { 5206 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 5207 } 5208 5209 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 5210 SourceLocation EndLoc) { 5211 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 5212 } 5213 5214 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 5215 Stmt *AStmt, 5216 SourceLocation StartLoc, 5217 SourceLocation EndLoc) { 5218 if (!AStmt) 5219 return StmtError(); 5220 5221 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5222 5223 getCurFunction()->setHasBranchProtectedScope(); 5224 5225 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 5226 AStmt, 5227 DSAStack->getTaskgroupReductionRef()); 5228 } 5229 5230 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 5231 SourceLocation StartLoc, 5232 SourceLocation EndLoc) { 5233 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 5234 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 5235 } 5236 5237 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 5238 Stmt *AStmt, 5239 SourceLocation StartLoc, 5240 SourceLocation EndLoc) { 5241 OMPClause *DependFound = nullptr; 5242 OMPClause *DependSourceClause = nullptr; 5243 OMPClause *DependSinkClause = nullptr; 5244 bool ErrorFound = false; 5245 OMPThreadsClause *TC = nullptr; 5246 OMPSIMDClause *SC = nullptr; 5247 for (auto *C : Clauses) { 5248 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 5249 DependFound = C; 5250 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 5251 if (DependSourceClause) { 5252 Diag(C->getLocStart(), diag::err_omp_more_one_clause) 5253 << getOpenMPDirectiveName(OMPD_ordered) 5254 << getOpenMPClauseName(OMPC_depend) << 2; 5255 ErrorFound = true; 5256 } else 5257 DependSourceClause = C; 5258 if (DependSinkClause) { 5259 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5260 << 0; 5261 ErrorFound = true; 5262 } 5263 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 5264 if (DependSourceClause) { 5265 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5266 << 1; 5267 ErrorFound = true; 5268 } 5269 DependSinkClause = C; 5270 } 5271 } else if (C->getClauseKind() == OMPC_threads) 5272 TC = cast<OMPThreadsClause>(C); 5273 else if (C->getClauseKind() == OMPC_simd) 5274 SC = cast<OMPSIMDClause>(C); 5275 } 5276 if (!ErrorFound && !SC && 5277 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 5278 // OpenMP [2.8.1,simd Construct, Restrictions] 5279 // An ordered construct with the simd clause is the only OpenMP construct 5280 // that can appear in the simd region. 5281 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 5282 ErrorFound = true; 5283 } else if (DependFound && (TC || SC)) { 5284 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd) 5285 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 5286 ErrorFound = true; 5287 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) { 5288 Diag(DependFound->getLocStart(), 5289 diag::err_omp_ordered_directive_without_param); 5290 ErrorFound = true; 5291 } else if (TC || Clauses.empty()) { 5292 if (auto *Param = DSAStack->getParentOrderedRegionParam()) { 5293 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; 5294 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 5295 << (TC != nullptr); 5296 Diag(Param->getLocStart(), diag::note_omp_ordered_param); 5297 ErrorFound = true; 5298 } 5299 } 5300 if ((!AStmt && !DependFound) || ErrorFound) 5301 return StmtError(); 5302 5303 if (AStmt) { 5304 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5305 5306 getCurFunction()->setHasBranchProtectedScope(); 5307 } 5308 5309 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5310 } 5311 5312 namespace { 5313 /// \brief Helper class for checking expression in 'omp atomic [update]' 5314 /// construct. 5315 class OpenMPAtomicUpdateChecker { 5316 /// \brief Error results for atomic update expressions. 5317 enum ExprAnalysisErrorCode { 5318 /// \brief A statement is not an expression statement. 5319 NotAnExpression, 5320 /// \brief Expression is not builtin binary or unary operation. 5321 NotABinaryOrUnaryExpression, 5322 /// \brief Unary operation is not post-/pre- increment/decrement operation. 5323 NotAnUnaryIncDecExpression, 5324 /// \brief An expression is not of scalar type. 5325 NotAScalarType, 5326 /// \brief A binary operation is not an assignment operation. 5327 NotAnAssignmentOp, 5328 /// \brief RHS part of the binary operation is not a binary expression. 5329 NotABinaryExpression, 5330 /// \brief RHS part is not additive/multiplicative/shift/biwise binary 5331 /// expression. 5332 NotABinaryOperator, 5333 /// \brief RHS binary operation does not have reference to the updated LHS 5334 /// part. 5335 NotAnUpdateExpression, 5336 /// \brief No errors is found. 5337 NoError 5338 }; 5339 /// \brief Reference to Sema. 5340 Sema &SemaRef; 5341 /// \brief A location for note diagnostics (when error is found). 5342 SourceLocation NoteLoc; 5343 /// \brief 'x' lvalue part of the source atomic expression. 5344 Expr *X; 5345 /// \brief 'expr' rvalue part of the source atomic expression. 5346 Expr *E; 5347 /// \brief Helper expression of the form 5348 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5349 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5350 Expr *UpdateExpr; 5351 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is 5352 /// important for non-associative operations. 5353 bool IsXLHSInRHSPart; 5354 BinaryOperatorKind Op; 5355 SourceLocation OpLoc; 5356 /// \brief true if the source expression is a postfix unary operation, false 5357 /// if it is a prefix unary operation. 5358 bool IsPostfixUpdate; 5359 5360 public: 5361 OpenMPAtomicUpdateChecker(Sema &SemaRef) 5362 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 5363 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 5364 /// \brief Check specified statement that it is suitable for 'atomic update' 5365 /// constructs and extract 'x', 'expr' and Operation from the original 5366 /// expression. If DiagId and NoteId == 0, then only check is performed 5367 /// without error notification. 5368 /// \param DiagId Diagnostic which should be emitted if error is found. 5369 /// \param NoteId Diagnostic note for the main error message. 5370 /// \return true if statement is not an update expression, false otherwise. 5371 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 5372 /// \brief Return the 'x' lvalue part of the source atomic expression. 5373 Expr *getX() const { return X; } 5374 /// \brief Return the 'expr' rvalue part of the source atomic expression. 5375 Expr *getExpr() const { return E; } 5376 /// \brief Return the update expression used in calculation of the updated 5377 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5378 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5379 Expr *getUpdateExpr() const { return UpdateExpr; } 5380 /// \brief Return true if 'x' is LHS in RHS part of full update expression, 5381 /// false otherwise. 5382 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 5383 5384 /// \brief true if the source expression is a postfix unary operation, false 5385 /// if it is a prefix unary operation. 5386 bool isPostfixUpdate() const { return IsPostfixUpdate; } 5387 5388 private: 5389 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 5390 unsigned NoteId = 0); 5391 }; 5392 } // namespace 5393 5394 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 5395 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 5396 ExprAnalysisErrorCode ErrorFound = NoError; 5397 SourceLocation ErrorLoc, NoteLoc; 5398 SourceRange ErrorRange, NoteRange; 5399 // Allowed constructs are: 5400 // x = x binop expr; 5401 // x = expr binop x; 5402 if (AtomicBinOp->getOpcode() == BO_Assign) { 5403 X = AtomicBinOp->getLHS(); 5404 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 5405 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 5406 if (AtomicInnerBinOp->isMultiplicativeOp() || 5407 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 5408 AtomicInnerBinOp->isBitwiseOp()) { 5409 Op = AtomicInnerBinOp->getOpcode(); 5410 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 5411 auto *LHS = AtomicInnerBinOp->getLHS(); 5412 auto *RHS = AtomicInnerBinOp->getRHS(); 5413 llvm::FoldingSetNodeID XId, LHSId, RHSId; 5414 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 5415 /*Canonical=*/true); 5416 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 5417 /*Canonical=*/true); 5418 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 5419 /*Canonical=*/true); 5420 if (XId == LHSId) { 5421 E = RHS; 5422 IsXLHSInRHSPart = true; 5423 } else if (XId == RHSId) { 5424 E = LHS; 5425 IsXLHSInRHSPart = false; 5426 } else { 5427 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5428 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5429 NoteLoc = X->getExprLoc(); 5430 NoteRange = X->getSourceRange(); 5431 ErrorFound = NotAnUpdateExpression; 5432 } 5433 } else { 5434 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5435 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5436 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 5437 NoteRange = SourceRange(NoteLoc, NoteLoc); 5438 ErrorFound = NotABinaryOperator; 5439 } 5440 } else { 5441 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 5442 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 5443 ErrorFound = NotABinaryExpression; 5444 } 5445 } else { 5446 ErrorLoc = AtomicBinOp->getExprLoc(); 5447 ErrorRange = AtomicBinOp->getSourceRange(); 5448 NoteLoc = AtomicBinOp->getOperatorLoc(); 5449 NoteRange = SourceRange(NoteLoc, NoteLoc); 5450 ErrorFound = NotAnAssignmentOp; 5451 } 5452 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 5453 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 5454 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 5455 return true; 5456 } else if (SemaRef.CurContext->isDependentContext()) 5457 E = X = UpdateExpr = nullptr; 5458 return ErrorFound != NoError; 5459 } 5460 5461 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 5462 unsigned NoteId) { 5463 ExprAnalysisErrorCode ErrorFound = NoError; 5464 SourceLocation ErrorLoc, NoteLoc; 5465 SourceRange ErrorRange, NoteRange; 5466 // Allowed constructs are: 5467 // x++; 5468 // x--; 5469 // ++x; 5470 // --x; 5471 // x binop= expr; 5472 // x = x binop expr; 5473 // x = expr binop x; 5474 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 5475 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 5476 if (AtomicBody->getType()->isScalarType() || 5477 AtomicBody->isInstantiationDependent()) { 5478 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 5479 AtomicBody->IgnoreParenImpCasts())) { 5480 // Check for Compound Assignment Operation 5481 Op = BinaryOperator::getOpForCompoundAssignment( 5482 AtomicCompAssignOp->getOpcode()); 5483 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 5484 E = AtomicCompAssignOp->getRHS(); 5485 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 5486 IsXLHSInRHSPart = true; 5487 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 5488 AtomicBody->IgnoreParenImpCasts())) { 5489 // Check for Binary Operation 5490 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 5491 return true; 5492 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 5493 AtomicBody->IgnoreParenImpCasts())) { 5494 // Check for Unary Operation 5495 if (AtomicUnaryOp->isIncrementDecrementOp()) { 5496 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 5497 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 5498 OpLoc = AtomicUnaryOp->getOperatorLoc(); 5499 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 5500 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 5501 IsXLHSInRHSPart = true; 5502 } else { 5503 ErrorFound = NotAnUnaryIncDecExpression; 5504 ErrorLoc = AtomicUnaryOp->getExprLoc(); 5505 ErrorRange = AtomicUnaryOp->getSourceRange(); 5506 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 5507 NoteRange = SourceRange(NoteLoc, NoteLoc); 5508 } 5509 } else if (!AtomicBody->isInstantiationDependent()) { 5510 ErrorFound = NotABinaryOrUnaryExpression; 5511 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 5512 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 5513 } 5514 } else { 5515 ErrorFound = NotAScalarType; 5516 NoteLoc = ErrorLoc = AtomicBody->getLocStart(); 5517 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5518 } 5519 } else { 5520 ErrorFound = NotAnExpression; 5521 NoteLoc = ErrorLoc = S->getLocStart(); 5522 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5523 } 5524 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 5525 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 5526 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 5527 return true; 5528 } else if (SemaRef.CurContext->isDependentContext()) 5529 E = X = UpdateExpr = nullptr; 5530 if (ErrorFound == NoError && E && X) { 5531 // Build an update expression of form 'OpaqueValueExpr(x) binop 5532 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 5533 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 5534 auto *OVEX = new (SemaRef.getASTContext()) 5535 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 5536 auto *OVEExpr = new (SemaRef.getASTContext()) 5537 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 5538 auto Update = 5539 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 5540 IsXLHSInRHSPart ? OVEExpr : OVEX); 5541 if (Update.isInvalid()) 5542 return true; 5543 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 5544 Sema::AA_Casting); 5545 if (Update.isInvalid()) 5546 return true; 5547 UpdateExpr = Update.get(); 5548 } 5549 return ErrorFound != NoError; 5550 } 5551 5552 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 5553 Stmt *AStmt, 5554 SourceLocation StartLoc, 5555 SourceLocation EndLoc) { 5556 if (!AStmt) 5557 return StmtError(); 5558 5559 auto *CS = cast<CapturedStmt>(AStmt); 5560 // 1.2.2 OpenMP Language Terminology 5561 // Structured block - An executable statement with a single entry at the 5562 // top and a single exit at the bottom. 5563 // The point of exit cannot be a branch out of the structured block. 5564 // longjmp() and throw() must not violate the entry/exit criteria. 5565 OpenMPClauseKind AtomicKind = OMPC_unknown; 5566 SourceLocation AtomicKindLoc; 5567 for (auto *C : Clauses) { 5568 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 5569 C->getClauseKind() == OMPC_update || 5570 C->getClauseKind() == OMPC_capture) { 5571 if (AtomicKind != OMPC_unknown) { 5572 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) 5573 << SourceRange(C->getLocStart(), C->getLocEnd()); 5574 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 5575 << getOpenMPClauseName(AtomicKind); 5576 } else { 5577 AtomicKind = C->getClauseKind(); 5578 AtomicKindLoc = C->getLocStart(); 5579 } 5580 } 5581 } 5582 5583 auto Body = CS->getCapturedStmt(); 5584 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 5585 Body = EWC->getSubExpr(); 5586 5587 Expr *X = nullptr; 5588 Expr *V = nullptr; 5589 Expr *E = nullptr; 5590 Expr *UE = nullptr; 5591 bool IsXLHSInRHSPart = false; 5592 bool IsPostfixUpdate = false; 5593 // OpenMP [2.12.6, atomic Construct] 5594 // In the next expressions: 5595 // * x and v (as applicable) are both l-value expressions with scalar type. 5596 // * During the execution of an atomic region, multiple syntactic 5597 // occurrences of x must designate the same storage location. 5598 // * Neither of v and expr (as applicable) may access the storage location 5599 // designated by x. 5600 // * Neither of x and expr (as applicable) may access the storage location 5601 // designated by v. 5602 // * expr is an expression with scalar type. 5603 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 5604 // * binop, binop=, ++, and -- are not overloaded operators. 5605 // * The expression x binop expr must be numerically equivalent to x binop 5606 // (expr). This requirement is satisfied if the operators in expr have 5607 // precedence greater than binop, or by using parentheses around expr or 5608 // subexpressions of expr. 5609 // * The expression expr binop x must be numerically equivalent to (expr) 5610 // binop x. This requirement is satisfied if the operators in expr have 5611 // precedence equal to or greater than binop, or by using parentheses around 5612 // expr or subexpressions of expr. 5613 // * For forms that allow multiple occurrences of x, the number of times 5614 // that x is evaluated is unspecified. 5615 if (AtomicKind == OMPC_read) { 5616 enum { 5617 NotAnExpression, 5618 NotAnAssignmentOp, 5619 NotAScalarType, 5620 NotAnLValue, 5621 NoError 5622 } ErrorFound = NoError; 5623 SourceLocation ErrorLoc, NoteLoc; 5624 SourceRange ErrorRange, NoteRange; 5625 // If clause is read: 5626 // v = x; 5627 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 5628 auto *AtomicBinOp = 5629 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5630 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5631 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 5632 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 5633 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 5634 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 5635 if (!X->isLValue() || !V->isLValue()) { 5636 auto NotLValueExpr = X->isLValue() ? V : X; 5637 ErrorFound = NotAnLValue; 5638 ErrorLoc = AtomicBinOp->getExprLoc(); 5639 ErrorRange = AtomicBinOp->getSourceRange(); 5640 NoteLoc = NotLValueExpr->getExprLoc(); 5641 NoteRange = NotLValueExpr->getSourceRange(); 5642 } 5643 } else if (!X->isInstantiationDependent() || 5644 !V->isInstantiationDependent()) { 5645 auto NotScalarExpr = 5646 (X->isInstantiationDependent() || X->getType()->isScalarType()) 5647 ? V 5648 : X; 5649 ErrorFound = NotAScalarType; 5650 ErrorLoc = AtomicBinOp->getExprLoc(); 5651 ErrorRange = AtomicBinOp->getSourceRange(); 5652 NoteLoc = NotScalarExpr->getExprLoc(); 5653 NoteRange = NotScalarExpr->getSourceRange(); 5654 } 5655 } else if (!AtomicBody->isInstantiationDependent()) { 5656 ErrorFound = NotAnAssignmentOp; 5657 ErrorLoc = AtomicBody->getExprLoc(); 5658 ErrorRange = AtomicBody->getSourceRange(); 5659 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5660 : AtomicBody->getExprLoc(); 5661 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5662 : AtomicBody->getSourceRange(); 5663 } 5664 } else { 5665 ErrorFound = NotAnExpression; 5666 NoteLoc = ErrorLoc = Body->getLocStart(); 5667 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5668 } 5669 if (ErrorFound != NoError) { 5670 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 5671 << ErrorRange; 5672 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 5673 << NoteRange; 5674 return StmtError(); 5675 } else if (CurContext->isDependentContext()) 5676 V = X = nullptr; 5677 } else if (AtomicKind == OMPC_write) { 5678 enum { 5679 NotAnExpression, 5680 NotAnAssignmentOp, 5681 NotAScalarType, 5682 NotAnLValue, 5683 NoError 5684 } ErrorFound = NoError; 5685 SourceLocation ErrorLoc, NoteLoc; 5686 SourceRange ErrorRange, NoteRange; 5687 // If clause is write: 5688 // x = expr; 5689 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 5690 auto *AtomicBinOp = 5691 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5692 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5693 X = AtomicBinOp->getLHS(); 5694 E = AtomicBinOp->getRHS(); 5695 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 5696 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 5697 if (!X->isLValue()) { 5698 ErrorFound = NotAnLValue; 5699 ErrorLoc = AtomicBinOp->getExprLoc(); 5700 ErrorRange = AtomicBinOp->getSourceRange(); 5701 NoteLoc = X->getExprLoc(); 5702 NoteRange = X->getSourceRange(); 5703 } 5704 } else if (!X->isInstantiationDependent() || 5705 !E->isInstantiationDependent()) { 5706 auto NotScalarExpr = 5707 (X->isInstantiationDependent() || X->getType()->isScalarType()) 5708 ? E 5709 : X; 5710 ErrorFound = NotAScalarType; 5711 ErrorLoc = AtomicBinOp->getExprLoc(); 5712 ErrorRange = AtomicBinOp->getSourceRange(); 5713 NoteLoc = NotScalarExpr->getExprLoc(); 5714 NoteRange = NotScalarExpr->getSourceRange(); 5715 } 5716 } else if (!AtomicBody->isInstantiationDependent()) { 5717 ErrorFound = NotAnAssignmentOp; 5718 ErrorLoc = AtomicBody->getExprLoc(); 5719 ErrorRange = AtomicBody->getSourceRange(); 5720 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5721 : AtomicBody->getExprLoc(); 5722 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5723 : AtomicBody->getSourceRange(); 5724 } 5725 } else { 5726 ErrorFound = NotAnExpression; 5727 NoteLoc = ErrorLoc = Body->getLocStart(); 5728 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5729 } 5730 if (ErrorFound != NoError) { 5731 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 5732 << ErrorRange; 5733 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 5734 << NoteRange; 5735 return StmtError(); 5736 } else if (CurContext->isDependentContext()) 5737 E = X = nullptr; 5738 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 5739 // If clause is update: 5740 // x++; 5741 // x--; 5742 // ++x; 5743 // --x; 5744 // x binop= expr; 5745 // x = x binop expr; 5746 // x = expr binop x; 5747 OpenMPAtomicUpdateChecker Checker(*this); 5748 if (Checker.checkStatement( 5749 Body, (AtomicKind == OMPC_update) 5750 ? diag::err_omp_atomic_update_not_expression_statement 5751 : diag::err_omp_atomic_not_expression_statement, 5752 diag::note_omp_atomic_update)) 5753 return StmtError(); 5754 if (!CurContext->isDependentContext()) { 5755 E = Checker.getExpr(); 5756 X = Checker.getX(); 5757 UE = Checker.getUpdateExpr(); 5758 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5759 } 5760 } else if (AtomicKind == OMPC_capture) { 5761 enum { 5762 NotAnAssignmentOp, 5763 NotACompoundStatement, 5764 NotTwoSubstatements, 5765 NotASpecificExpression, 5766 NoError 5767 } ErrorFound = NoError; 5768 SourceLocation ErrorLoc, NoteLoc; 5769 SourceRange ErrorRange, NoteRange; 5770 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 5771 // If clause is a capture: 5772 // v = x++; 5773 // v = x--; 5774 // v = ++x; 5775 // v = --x; 5776 // v = x binop= expr; 5777 // v = x = x binop expr; 5778 // v = x = expr binop x; 5779 auto *AtomicBinOp = 5780 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5781 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5782 V = AtomicBinOp->getLHS(); 5783 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 5784 OpenMPAtomicUpdateChecker Checker(*this); 5785 if (Checker.checkStatement( 5786 Body, diag::err_omp_atomic_capture_not_expression_statement, 5787 diag::note_omp_atomic_update)) 5788 return StmtError(); 5789 E = Checker.getExpr(); 5790 X = Checker.getX(); 5791 UE = Checker.getUpdateExpr(); 5792 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5793 IsPostfixUpdate = Checker.isPostfixUpdate(); 5794 } else if (!AtomicBody->isInstantiationDependent()) { 5795 ErrorLoc = AtomicBody->getExprLoc(); 5796 ErrorRange = AtomicBody->getSourceRange(); 5797 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5798 : AtomicBody->getExprLoc(); 5799 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5800 : AtomicBody->getSourceRange(); 5801 ErrorFound = NotAnAssignmentOp; 5802 } 5803 if (ErrorFound != NoError) { 5804 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 5805 << ErrorRange; 5806 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 5807 return StmtError(); 5808 } else if (CurContext->isDependentContext()) { 5809 UE = V = E = X = nullptr; 5810 } 5811 } else { 5812 // If clause is a capture: 5813 // { v = x; x = expr; } 5814 // { v = x; x++; } 5815 // { v = x; x--; } 5816 // { v = x; ++x; } 5817 // { v = x; --x; } 5818 // { v = x; x binop= expr; } 5819 // { v = x; x = x binop expr; } 5820 // { v = x; x = expr binop x; } 5821 // { x++; v = x; } 5822 // { x--; v = x; } 5823 // { ++x; v = x; } 5824 // { --x; v = x; } 5825 // { x binop= expr; v = x; } 5826 // { x = x binop expr; v = x; } 5827 // { x = expr binop x; v = x; } 5828 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 5829 // Check that this is { expr1; expr2; } 5830 if (CS->size() == 2) { 5831 auto *First = CS->body_front(); 5832 auto *Second = CS->body_back(); 5833 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 5834 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 5835 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 5836 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 5837 // Need to find what subexpression is 'v' and what is 'x'. 5838 OpenMPAtomicUpdateChecker Checker(*this); 5839 bool IsUpdateExprFound = !Checker.checkStatement(Second); 5840 BinaryOperator *BinOp = nullptr; 5841 if (IsUpdateExprFound) { 5842 BinOp = dyn_cast<BinaryOperator>(First); 5843 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 5844 } 5845 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 5846 // { v = x; x++; } 5847 // { v = x; x--; } 5848 // { v = x; ++x; } 5849 // { v = x; --x; } 5850 // { v = x; x binop= expr; } 5851 // { v = x; x = x binop expr; } 5852 // { v = x; x = expr binop x; } 5853 // Check that the first expression has form v = x. 5854 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 5855 llvm::FoldingSetNodeID XId, PossibleXId; 5856 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 5857 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 5858 IsUpdateExprFound = XId == PossibleXId; 5859 if (IsUpdateExprFound) { 5860 V = BinOp->getLHS(); 5861 X = Checker.getX(); 5862 E = Checker.getExpr(); 5863 UE = Checker.getUpdateExpr(); 5864 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5865 IsPostfixUpdate = true; 5866 } 5867 } 5868 if (!IsUpdateExprFound) { 5869 IsUpdateExprFound = !Checker.checkStatement(First); 5870 BinOp = nullptr; 5871 if (IsUpdateExprFound) { 5872 BinOp = dyn_cast<BinaryOperator>(Second); 5873 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 5874 } 5875 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 5876 // { x++; v = x; } 5877 // { x--; v = x; } 5878 // { ++x; v = x; } 5879 // { --x; v = x; } 5880 // { x binop= expr; v = x; } 5881 // { x = x binop expr; v = x; } 5882 // { x = expr binop x; v = x; } 5883 // Check that the second expression has form v = x. 5884 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 5885 llvm::FoldingSetNodeID XId, PossibleXId; 5886 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 5887 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 5888 IsUpdateExprFound = XId == PossibleXId; 5889 if (IsUpdateExprFound) { 5890 V = BinOp->getLHS(); 5891 X = Checker.getX(); 5892 E = Checker.getExpr(); 5893 UE = Checker.getUpdateExpr(); 5894 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5895 IsPostfixUpdate = false; 5896 } 5897 } 5898 } 5899 if (!IsUpdateExprFound) { 5900 // { v = x; x = expr; } 5901 auto *FirstExpr = dyn_cast<Expr>(First); 5902 auto *SecondExpr = dyn_cast<Expr>(Second); 5903 if (!FirstExpr || !SecondExpr || 5904 !(FirstExpr->isInstantiationDependent() || 5905 SecondExpr->isInstantiationDependent())) { 5906 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 5907 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 5908 ErrorFound = NotAnAssignmentOp; 5909 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 5910 : First->getLocStart(); 5911 NoteRange = ErrorRange = FirstBinOp 5912 ? FirstBinOp->getSourceRange() 5913 : SourceRange(ErrorLoc, ErrorLoc); 5914 } else { 5915 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 5916 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 5917 ErrorFound = NotAnAssignmentOp; 5918 NoteLoc = ErrorLoc = SecondBinOp 5919 ? SecondBinOp->getOperatorLoc() 5920 : Second->getLocStart(); 5921 NoteRange = ErrorRange = 5922 SecondBinOp ? SecondBinOp->getSourceRange() 5923 : SourceRange(ErrorLoc, ErrorLoc); 5924 } else { 5925 auto *PossibleXRHSInFirst = 5926 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 5927 auto *PossibleXLHSInSecond = 5928 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 5929 llvm::FoldingSetNodeID X1Id, X2Id; 5930 PossibleXRHSInFirst->Profile(X1Id, Context, 5931 /*Canonical=*/true); 5932 PossibleXLHSInSecond->Profile(X2Id, Context, 5933 /*Canonical=*/true); 5934 IsUpdateExprFound = X1Id == X2Id; 5935 if (IsUpdateExprFound) { 5936 V = FirstBinOp->getLHS(); 5937 X = SecondBinOp->getLHS(); 5938 E = SecondBinOp->getRHS(); 5939 UE = nullptr; 5940 IsXLHSInRHSPart = false; 5941 IsPostfixUpdate = true; 5942 } else { 5943 ErrorFound = NotASpecificExpression; 5944 ErrorLoc = FirstBinOp->getExprLoc(); 5945 ErrorRange = FirstBinOp->getSourceRange(); 5946 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 5947 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 5948 } 5949 } 5950 } 5951 } 5952 } 5953 } else { 5954 NoteLoc = ErrorLoc = Body->getLocStart(); 5955 NoteRange = ErrorRange = 5956 SourceRange(Body->getLocStart(), Body->getLocStart()); 5957 ErrorFound = NotTwoSubstatements; 5958 } 5959 } else { 5960 NoteLoc = ErrorLoc = Body->getLocStart(); 5961 NoteRange = ErrorRange = 5962 SourceRange(Body->getLocStart(), Body->getLocStart()); 5963 ErrorFound = NotACompoundStatement; 5964 } 5965 if (ErrorFound != NoError) { 5966 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 5967 << ErrorRange; 5968 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 5969 return StmtError(); 5970 } else if (CurContext->isDependentContext()) { 5971 UE = V = E = X = nullptr; 5972 } 5973 } 5974 } 5975 5976 getCurFunction()->setHasBranchProtectedScope(); 5977 5978 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5979 X, V, E, UE, IsXLHSInRHSPart, 5980 IsPostfixUpdate); 5981 } 5982 5983 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 5984 Stmt *AStmt, 5985 SourceLocation StartLoc, 5986 SourceLocation EndLoc) { 5987 if (!AStmt) 5988 return StmtError(); 5989 5990 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5991 // 1.2.2 OpenMP Language Terminology 5992 // Structured block - An executable statement with a single entry at the 5993 // top and a single exit at the bottom. 5994 // The point of exit cannot be a branch out of the structured block. 5995 // longjmp() and throw() must not violate the entry/exit criteria. 5996 CS->getCapturedDecl()->setNothrow(); 5997 5998 // OpenMP [2.16, Nesting of Regions] 5999 // If specified, a teams construct must be contained within a target 6000 // construct. That target construct must contain no statements or directives 6001 // outside of the teams construct. 6002 if (DSAStack->hasInnerTeamsRegion()) { 6003 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); 6004 bool OMPTeamsFound = true; 6005 if (auto *CS = dyn_cast<CompoundStmt>(S)) { 6006 auto I = CS->body_begin(); 6007 while (I != CS->body_end()) { 6008 auto *OED = dyn_cast<OMPExecutableDirective>(*I); 6009 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 6010 OMPTeamsFound = false; 6011 break; 6012 } 6013 ++I; 6014 } 6015 assert(I != CS->body_end() && "Not found statement"); 6016 S = *I; 6017 } else { 6018 auto *OED = dyn_cast<OMPExecutableDirective>(S); 6019 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 6020 } 6021 if (!OMPTeamsFound) { 6022 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 6023 Diag(DSAStack->getInnerTeamsRegionLoc(), 6024 diag::note_omp_nested_teams_construct_here); 6025 Diag(S->getLocStart(), diag::note_omp_nested_statement_here) 6026 << isa<OMPExecutableDirective>(S); 6027 return StmtError(); 6028 } 6029 } 6030 6031 getCurFunction()->setHasBranchProtectedScope(); 6032 6033 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6034 } 6035 6036 StmtResult 6037 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 6038 Stmt *AStmt, SourceLocation StartLoc, 6039 SourceLocation EndLoc) { 6040 if (!AStmt) 6041 return StmtError(); 6042 6043 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6044 // 1.2.2 OpenMP Language Terminology 6045 // Structured block - An executable statement with a single entry at the 6046 // top and a single exit at the bottom. 6047 // The point of exit cannot be a branch out of the structured block. 6048 // longjmp() and throw() must not violate the entry/exit criteria. 6049 CS->getCapturedDecl()->setNothrow(); 6050 6051 getCurFunction()->setHasBranchProtectedScope(); 6052 6053 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6054 AStmt); 6055 } 6056 6057 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 6058 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6059 SourceLocation EndLoc, 6060 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6061 if (!AStmt) 6062 return StmtError(); 6063 6064 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6065 // 1.2.2 OpenMP Language Terminology 6066 // Structured block - An executable statement with a single entry at the 6067 // top and a single exit at the bottom. 6068 // The point of exit cannot be a branch out of the structured block. 6069 // longjmp() and throw() must not violate the entry/exit criteria. 6070 CS->getCapturedDecl()->setNothrow(); 6071 6072 OMPLoopDirective::HelperExprs B; 6073 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6074 // define the nested loops number. 6075 unsigned NestedLoopCount = 6076 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 6077 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6078 VarsWithImplicitDSA, B); 6079 if (NestedLoopCount == 0) 6080 return StmtError(); 6081 6082 assert((CurContext->isDependentContext() || B.builtAll()) && 6083 "omp target parallel for loop exprs were not built"); 6084 6085 if (!CurContext->isDependentContext()) { 6086 // Finalize the clauses that need pre-built expressions for CodeGen. 6087 for (auto C : Clauses) { 6088 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6089 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6090 B.NumIterations, *this, CurScope, 6091 DSAStack)) 6092 return StmtError(); 6093 } 6094 } 6095 6096 getCurFunction()->setHasBranchProtectedScope(); 6097 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 6098 NestedLoopCount, Clauses, AStmt, 6099 B, DSAStack->isCancelRegion()); 6100 } 6101 6102 /// Check for existence of a map clause in the list of clauses. 6103 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 6104 const OpenMPClauseKind K) { 6105 return llvm::any_of( 6106 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 6107 } 6108 6109 template <typename... Params> 6110 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 6111 const Params... ClauseTypes) { 6112 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 6113 } 6114 6115 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 6116 Stmt *AStmt, 6117 SourceLocation StartLoc, 6118 SourceLocation EndLoc) { 6119 if (!AStmt) 6120 return StmtError(); 6121 6122 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6123 6124 // OpenMP [2.10.1, Restrictions, p. 97] 6125 // At least one map clause must appear on the directive. 6126 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 6127 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6128 << "'map' or 'use_device_ptr'" 6129 << getOpenMPDirectiveName(OMPD_target_data); 6130 return StmtError(); 6131 } 6132 6133 getCurFunction()->setHasBranchProtectedScope(); 6134 6135 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6136 AStmt); 6137 } 6138 6139 StmtResult 6140 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 6141 SourceLocation StartLoc, 6142 SourceLocation EndLoc) { 6143 // OpenMP [2.10.2, Restrictions, p. 99] 6144 // At least one map clause must appear on the directive. 6145 if (!hasClauses(Clauses, OMPC_map)) { 6146 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6147 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 6148 return StmtError(); 6149 } 6150 6151 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, 6152 Clauses); 6153 } 6154 6155 StmtResult 6156 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 6157 SourceLocation StartLoc, 6158 SourceLocation EndLoc) { 6159 // OpenMP [2.10.3, Restrictions, p. 102] 6160 // At least one map clause must appear on the directive. 6161 if (!hasClauses(Clauses, OMPC_map)) { 6162 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6163 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 6164 return StmtError(); 6165 } 6166 6167 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses); 6168 } 6169 6170 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 6171 SourceLocation StartLoc, 6172 SourceLocation EndLoc) { 6173 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 6174 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 6175 return StmtError(); 6176 } 6177 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses); 6178 } 6179 6180 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 6181 Stmt *AStmt, SourceLocation StartLoc, 6182 SourceLocation EndLoc) { 6183 if (!AStmt) 6184 return StmtError(); 6185 6186 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6187 // 1.2.2 OpenMP Language Terminology 6188 // Structured block - An executable statement with a single entry at the 6189 // top and a single exit at the bottom. 6190 // The point of exit cannot be a branch out of the structured block. 6191 // longjmp() and throw() must not violate the entry/exit criteria. 6192 CS->getCapturedDecl()->setNothrow(); 6193 6194 getCurFunction()->setHasBranchProtectedScope(); 6195 6196 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6197 } 6198 6199 StmtResult 6200 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 6201 SourceLocation EndLoc, 6202 OpenMPDirectiveKind CancelRegion) { 6203 if (DSAStack->isParentNowaitRegion()) { 6204 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 6205 return StmtError(); 6206 } 6207 if (DSAStack->isParentOrderedRegion()) { 6208 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 6209 return StmtError(); 6210 } 6211 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 6212 CancelRegion); 6213 } 6214 6215 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 6216 SourceLocation StartLoc, 6217 SourceLocation EndLoc, 6218 OpenMPDirectiveKind CancelRegion) { 6219 if (DSAStack->isParentNowaitRegion()) { 6220 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 6221 return StmtError(); 6222 } 6223 if (DSAStack->isParentOrderedRegion()) { 6224 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 6225 return StmtError(); 6226 } 6227 DSAStack->setParentCancelRegion(/*Cancel=*/true); 6228 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6229 CancelRegion); 6230 } 6231 6232 static bool checkGrainsizeNumTasksClauses(Sema &S, 6233 ArrayRef<OMPClause *> Clauses) { 6234 OMPClause *PrevClause = nullptr; 6235 bool ErrorFound = false; 6236 for (auto *C : Clauses) { 6237 if (C->getClauseKind() == OMPC_grainsize || 6238 C->getClauseKind() == OMPC_num_tasks) { 6239 if (!PrevClause) 6240 PrevClause = C; 6241 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 6242 S.Diag(C->getLocStart(), 6243 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 6244 << getOpenMPClauseName(C->getClauseKind()) 6245 << getOpenMPClauseName(PrevClause->getClauseKind()); 6246 S.Diag(PrevClause->getLocStart(), 6247 diag::note_omp_previous_grainsize_num_tasks) 6248 << getOpenMPClauseName(PrevClause->getClauseKind()); 6249 ErrorFound = true; 6250 } 6251 } 6252 } 6253 return ErrorFound; 6254 } 6255 6256 static bool checkReductionClauseWithNogroup(Sema &S, 6257 ArrayRef<OMPClause *> Clauses) { 6258 OMPClause *ReductionClause = nullptr; 6259 OMPClause *NogroupClause = nullptr; 6260 for (auto *C : Clauses) { 6261 if (C->getClauseKind() == OMPC_reduction) { 6262 ReductionClause = C; 6263 if (NogroupClause) 6264 break; 6265 continue; 6266 } 6267 if (C->getClauseKind() == OMPC_nogroup) { 6268 NogroupClause = C; 6269 if (ReductionClause) 6270 break; 6271 continue; 6272 } 6273 } 6274 if (ReductionClause && NogroupClause) { 6275 S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup) 6276 << SourceRange(NogroupClause->getLocStart(), 6277 NogroupClause->getLocEnd()); 6278 return true; 6279 } 6280 return false; 6281 } 6282 6283 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 6284 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6285 SourceLocation EndLoc, 6286 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6287 if (!AStmt) 6288 return StmtError(); 6289 6290 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6291 OMPLoopDirective::HelperExprs B; 6292 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6293 // define the nested loops number. 6294 unsigned NestedLoopCount = 6295 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 6296 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6297 VarsWithImplicitDSA, B); 6298 if (NestedLoopCount == 0) 6299 return StmtError(); 6300 6301 assert((CurContext->isDependentContext() || B.builtAll()) && 6302 "omp for loop exprs were not built"); 6303 6304 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6305 // The grainsize clause and num_tasks clause are mutually exclusive and may 6306 // not appear on the same taskloop directive. 6307 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 6308 return StmtError(); 6309 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6310 // If a reduction clause is present on the taskloop directive, the nogroup 6311 // clause must not be specified. 6312 if (checkReductionClauseWithNogroup(*this, Clauses)) 6313 return StmtError(); 6314 6315 getCurFunction()->setHasBranchProtectedScope(); 6316 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 6317 NestedLoopCount, Clauses, AStmt, B); 6318 } 6319 6320 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 6321 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6322 SourceLocation EndLoc, 6323 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6324 if (!AStmt) 6325 return StmtError(); 6326 6327 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6328 OMPLoopDirective::HelperExprs B; 6329 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6330 // define the nested loops number. 6331 unsigned NestedLoopCount = 6332 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 6333 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6334 VarsWithImplicitDSA, B); 6335 if (NestedLoopCount == 0) 6336 return StmtError(); 6337 6338 assert((CurContext->isDependentContext() || B.builtAll()) && 6339 "omp for loop exprs were not built"); 6340 6341 if (!CurContext->isDependentContext()) { 6342 // Finalize the clauses that need pre-built expressions for CodeGen. 6343 for (auto C : Clauses) { 6344 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6345 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6346 B.NumIterations, *this, CurScope, 6347 DSAStack)) 6348 return StmtError(); 6349 } 6350 } 6351 6352 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6353 // The grainsize clause and num_tasks clause are mutually exclusive and may 6354 // not appear on the same taskloop directive. 6355 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 6356 return StmtError(); 6357 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6358 // If a reduction clause is present on the taskloop directive, the nogroup 6359 // clause must not be specified. 6360 if (checkReductionClauseWithNogroup(*this, Clauses)) 6361 return StmtError(); 6362 6363 getCurFunction()->setHasBranchProtectedScope(); 6364 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 6365 NestedLoopCount, Clauses, AStmt, B); 6366 } 6367 6368 StmtResult Sema::ActOnOpenMPDistributeDirective( 6369 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6370 SourceLocation EndLoc, 6371 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6372 if (!AStmt) 6373 return StmtError(); 6374 6375 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6376 OMPLoopDirective::HelperExprs B; 6377 // In presence of clause 'collapse' with number of loops, it will 6378 // define the nested loops number. 6379 unsigned NestedLoopCount = 6380 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 6381 nullptr /*ordered not a clause on distribute*/, AStmt, 6382 *this, *DSAStack, VarsWithImplicitDSA, B); 6383 if (NestedLoopCount == 0) 6384 return StmtError(); 6385 6386 assert((CurContext->isDependentContext() || B.builtAll()) && 6387 "omp for loop exprs were not built"); 6388 6389 getCurFunction()->setHasBranchProtectedScope(); 6390 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 6391 NestedLoopCount, Clauses, AStmt, B); 6392 } 6393 6394 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 6395 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6396 SourceLocation EndLoc, 6397 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6398 if (!AStmt) 6399 return StmtError(); 6400 6401 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6402 // 1.2.2 OpenMP Language Terminology 6403 // Structured block - An executable statement with a single entry at the 6404 // top and a single exit at the bottom. 6405 // The point of exit cannot be a branch out of the structured block. 6406 // longjmp() and throw() must not violate the entry/exit criteria. 6407 CS->getCapturedDecl()->setNothrow(); 6408 6409 OMPLoopDirective::HelperExprs B; 6410 // In presence of clause 'collapse' with number of loops, it will 6411 // define the nested loops number. 6412 unsigned NestedLoopCount = CheckOpenMPLoop( 6413 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 6414 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6415 VarsWithImplicitDSA, B); 6416 if (NestedLoopCount == 0) 6417 return StmtError(); 6418 6419 assert((CurContext->isDependentContext() || B.builtAll()) && 6420 "omp for loop exprs were not built"); 6421 6422 getCurFunction()->setHasBranchProtectedScope(); 6423 return OMPDistributeParallelForDirective::Create( 6424 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6425 } 6426 6427 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 6428 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6429 SourceLocation EndLoc, 6430 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6431 if (!AStmt) 6432 return StmtError(); 6433 6434 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6435 // 1.2.2 OpenMP Language Terminology 6436 // Structured block - An executable statement with a single entry at the 6437 // top and a single exit at the bottom. 6438 // The point of exit cannot be a branch out of the structured block. 6439 // longjmp() and throw() must not violate the entry/exit criteria. 6440 CS->getCapturedDecl()->setNothrow(); 6441 6442 OMPLoopDirective::HelperExprs B; 6443 // In presence of clause 'collapse' with number of loops, it will 6444 // define the nested loops number. 6445 unsigned NestedLoopCount = CheckOpenMPLoop( 6446 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 6447 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6448 VarsWithImplicitDSA, B); 6449 if (NestedLoopCount == 0) 6450 return StmtError(); 6451 6452 assert((CurContext->isDependentContext() || B.builtAll()) && 6453 "omp for loop exprs were not built"); 6454 6455 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6456 return StmtError(); 6457 6458 getCurFunction()->setHasBranchProtectedScope(); 6459 return OMPDistributeParallelForSimdDirective::Create( 6460 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6461 } 6462 6463 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 6464 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6465 SourceLocation EndLoc, 6466 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6467 if (!AStmt) 6468 return StmtError(); 6469 6470 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6471 // 1.2.2 OpenMP Language Terminology 6472 // Structured block - An executable statement with a single entry at the 6473 // top and a single exit at the bottom. 6474 // The point of exit cannot be a branch out of the structured block. 6475 // longjmp() and throw() must not violate the entry/exit criteria. 6476 CS->getCapturedDecl()->setNothrow(); 6477 6478 OMPLoopDirective::HelperExprs B; 6479 // In presence of clause 'collapse' with number of loops, it will 6480 // define the nested loops number. 6481 unsigned NestedLoopCount = 6482 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 6483 nullptr /*ordered not a clause on distribute*/, AStmt, 6484 *this, *DSAStack, VarsWithImplicitDSA, B); 6485 if (NestedLoopCount == 0) 6486 return StmtError(); 6487 6488 assert((CurContext->isDependentContext() || B.builtAll()) && 6489 "omp for loop exprs were not built"); 6490 6491 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6492 return StmtError(); 6493 6494 getCurFunction()->setHasBranchProtectedScope(); 6495 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 6496 NestedLoopCount, Clauses, AStmt, B); 6497 } 6498 6499 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 6500 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6501 SourceLocation EndLoc, 6502 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6503 if (!AStmt) 6504 return StmtError(); 6505 6506 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6507 // 1.2.2 OpenMP Language Terminology 6508 // Structured block - An executable statement with a single entry at the 6509 // top and a single exit at the bottom. 6510 // The point of exit cannot be a branch out of the structured block. 6511 // longjmp() and throw() must not violate the entry/exit criteria. 6512 CS->getCapturedDecl()->setNothrow(); 6513 6514 OMPLoopDirective::HelperExprs B; 6515 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6516 // define the nested loops number. 6517 unsigned NestedLoopCount = CheckOpenMPLoop( 6518 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 6519 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6520 VarsWithImplicitDSA, B); 6521 if (NestedLoopCount == 0) 6522 return StmtError(); 6523 6524 assert((CurContext->isDependentContext() || B.builtAll()) && 6525 "omp target parallel for simd loop exprs were not built"); 6526 6527 if (!CurContext->isDependentContext()) { 6528 // Finalize the clauses that need pre-built expressions for CodeGen. 6529 for (auto C : Clauses) { 6530 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6531 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6532 B.NumIterations, *this, CurScope, 6533 DSAStack)) 6534 return StmtError(); 6535 } 6536 } 6537 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6538 return StmtError(); 6539 6540 getCurFunction()->setHasBranchProtectedScope(); 6541 return OMPTargetParallelForSimdDirective::Create( 6542 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6543 } 6544 6545 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 6546 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6547 SourceLocation EndLoc, 6548 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6549 if (!AStmt) 6550 return StmtError(); 6551 6552 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6553 // 1.2.2 OpenMP Language Terminology 6554 // Structured block - An executable statement with a single entry at the 6555 // top and a single exit at the bottom. 6556 // The point of exit cannot be a branch out of the structured block. 6557 // longjmp() and throw() must not violate the entry/exit criteria. 6558 CS->getCapturedDecl()->setNothrow(); 6559 6560 OMPLoopDirective::HelperExprs B; 6561 // In presence of clause 'collapse' with number of loops, it will define the 6562 // nested loops number. 6563 unsigned NestedLoopCount = 6564 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 6565 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6566 VarsWithImplicitDSA, B); 6567 if (NestedLoopCount == 0) 6568 return StmtError(); 6569 6570 assert((CurContext->isDependentContext() || B.builtAll()) && 6571 "omp target simd loop exprs were not built"); 6572 6573 if (!CurContext->isDependentContext()) { 6574 // Finalize the clauses that need pre-built expressions for CodeGen. 6575 for (auto C : Clauses) { 6576 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6577 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6578 B.NumIterations, *this, CurScope, 6579 DSAStack)) 6580 return StmtError(); 6581 } 6582 } 6583 6584 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6585 return StmtError(); 6586 6587 getCurFunction()->setHasBranchProtectedScope(); 6588 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 6589 NestedLoopCount, Clauses, AStmt, B); 6590 } 6591 6592 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 6593 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6594 SourceLocation EndLoc, 6595 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6596 if (!AStmt) 6597 return StmtError(); 6598 6599 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6600 // 1.2.2 OpenMP Language Terminology 6601 // Structured block - An executable statement with a single entry at the 6602 // top and a single exit at the bottom. 6603 // The point of exit cannot be a branch out of the structured block. 6604 // longjmp() and throw() must not violate the entry/exit criteria. 6605 CS->getCapturedDecl()->setNothrow(); 6606 6607 OMPLoopDirective::HelperExprs B; 6608 // In presence of clause 'collapse' with number of loops, it will 6609 // define the nested loops number. 6610 unsigned NestedLoopCount = 6611 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 6612 nullptr /*ordered not a clause on distribute*/, AStmt, 6613 *this, *DSAStack, VarsWithImplicitDSA, B); 6614 if (NestedLoopCount == 0) 6615 return StmtError(); 6616 6617 assert((CurContext->isDependentContext() || B.builtAll()) && 6618 "omp teams distribute loop exprs were not built"); 6619 6620 getCurFunction()->setHasBranchProtectedScope(); 6621 return OMPTeamsDistributeDirective::Create( 6622 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6623 } 6624 6625 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 6626 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6627 SourceLocation EndLoc, 6628 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6629 if (!AStmt) 6630 return StmtError(); 6631 6632 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6633 // 1.2.2 OpenMP Language Terminology 6634 // Structured block - An executable statement with a single entry at the 6635 // top and a single exit at the bottom. 6636 // The point of exit cannot be a branch out of the structured block. 6637 // longjmp() and throw() must not violate the entry/exit criteria. 6638 CS->getCapturedDecl()->setNothrow(); 6639 6640 OMPLoopDirective::HelperExprs B; 6641 // In presence of clause 'collapse' with number of loops, it will 6642 // define the nested loops number. 6643 unsigned NestedLoopCount = CheckOpenMPLoop( 6644 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 6645 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6646 VarsWithImplicitDSA, B); 6647 6648 if (NestedLoopCount == 0) 6649 return StmtError(); 6650 6651 assert((CurContext->isDependentContext() || B.builtAll()) && 6652 "omp teams distribute simd loop exprs were not built"); 6653 6654 if (!CurContext->isDependentContext()) { 6655 // Finalize the clauses that need pre-built expressions for CodeGen. 6656 for (auto C : Clauses) { 6657 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6658 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6659 B.NumIterations, *this, CurScope, 6660 DSAStack)) 6661 return StmtError(); 6662 } 6663 } 6664 6665 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6666 return StmtError(); 6667 6668 getCurFunction()->setHasBranchProtectedScope(); 6669 return OMPTeamsDistributeSimdDirective::Create( 6670 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6671 } 6672 6673 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6674 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6675 SourceLocation EndLoc, 6676 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6677 if (!AStmt) 6678 return StmtError(); 6679 6680 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6681 // 1.2.2 OpenMP Language Terminology 6682 // Structured block - An executable statement with a single entry at the 6683 // top and a single exit at the bottom. 6684 // The point of exit cannot be a branch out of the structured block. 6685 // longjmp() and throw() must not violate the entry/exit criteria. 6686 CS->getCapturedDecl()->setNothrow(); 6687 6688 OMPLoopDirective::HelperExprs B; 6689 // In presence of clause 'collapse' with number of loops, it will 6690 // define the nested loops number. 6691 auto NestedLoopCount = CheckOpenMPLoop( 6692 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 6693 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6694 VarsWithImplicitDSA, B); 6695 6696 if (NestedLoopCount == 0) 6697 return StmtError(); 6698 6699 assert((CurContext->isDependentContext() || B.builtAll()) && 6700 "omp for loop exprs were not built"); 6701 6702 if (!CurContext->isDependentContext()) { 6703 // Finalize the clauses that need pre-built expressions for CodeGen. 6704 for (auto C : Clauses) { 6705 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6706 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6707 B.NumIterations, *this, CurScope, 6708 DSAStack)) 6709 return StmtError(); 6710 } 6711 } 6712 6713 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6714 return StmtError(); 6715 6716 getCurFunction()->setHasBranchProtectedScope(); 6717 return OMPTeamsDistributeParallelForSimdDirective::Create( 6718 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6719 } 6720 6721 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 6722 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6723 SourceLocation EndLoc, 6724 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6725 if (!AStmt) 6726 return StmtError(); 6727 6728 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6729 // 1.2.2 OpenMP Language Terminology 6730 // Structured block - An executable statement with a single entry at the 6731 // top and a single exit at the bottom. 6732 // The point of exit cannot be a branch out of the structured block. 6733 // longjmp() and throw() must not violate the entry/exit criteria. 6734 CS->getCapturedDecl()->setNothrow(); 6735 6736 OMPLoopDirective::HelperExprs B; 6737 // In presence of clause 'collapse' with number of loops, it will 6738 // define the nested loops number. 6739 unsigned NestedLoopCount = CheckOpenMPLoop( 6740 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 6741 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6742 VarsWithImplicitDSA, B); 6743 6744 if (NestedLoopCount == 0) 6745 return StmtError(); 6746 6747 assert((CurContext->isDependentContext() || B.builtAll()) && 6748 "omp for loop exprs were not built"); 6749 6750 if (!CurContext->isDependentContext()) { 6751 // Finalize the clauses that need pre-built expressions for CodeGen. 6752 for (auto C : Clauses) { 6753 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6754 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6755 B.NumIterations, *this, CurScope, 6756 DSAStack)) 6757 return StmtError(); 6758 } 6759 } 6760 6761 getCurFunction()->setHasBranchProtectedScope(); 6762 return OMPTeamsDistributeParallelForDirective::Create( 6763 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6764 } 6765 6766 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 6767 Stmt *AStmt, 6768 SourceLocation StartLoc, 6769 SourceLocation EndLoc) { 6770 if (!AStmt) 6771 return StmtError(); 6772 6773 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6774 // 1.2.2 OpenMP Language Terminology 6775 // Structured block - An executable statement with a single entry at the 6776 // top and a single exit at the bottom. 6777 // The point of exit cannot be a branch out of the structured block. 6778 // longjmp() and throw() must not violate the entry/exit criteria. 6779 CS->getCapturedDecl()->setNothrow(); 6780 6781 getCurFunction()->setHasBranchProtectedScope(); 6782 6783 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 6784 AStmt); 6785 } 6786 6787 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 6788 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6789 SourceLocation EndLoc, 6790 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6791 if (!AStmt) 6792 return StmtError(); 6793 6794 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6795 // 1.2.2 OpenMP Language Terminology 6796 // Structured block - An executable statement with a single entry at the 6797 // top and a single exit at the bottom. 6798 // The point of exit cannot be a branch out of the structured block. 6799 // longjmp() and throw() must not violate the entry/exit criteria. 6800 CS->getCapturedDecl()->setNothrow(); 6801 6802 OMPLoopDirective::HelperExprs B; 6803 // In presence of clause 'collapse' with number of loops, it will 6804 // define the nested loops number. 6805 auto NestedLoopCount = CheckOpenMPLoop( 6806 OMPD_target_teams_distribute, 6807 getCollapseNumberExpr(Clauses), 6808 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6809 VarsWithImplicitDSA, B); 6810 if (NestedLoopCount == 0) 6811 return StmtError(); 6812 6813 assert((CurContext->isDependentContext() || B.builtAll()) && 6814 "omp target teams distribute loop exprs were not built"); 6815 6816 getCurFunction()->setHasBranchProtectedScope(); 6817 return OMPTargetTeamsDistributeDirective::Create( 6818 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6819 } 6820 6821 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6822 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6823 SourceLocation EndLoc, 6824 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6825 if (!AStmt) 6826 return StmtError(); 6827 6828 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6829 // 1.2.2 OpenMP Language Terminology 6830 // Structured block - An executable statement with a single entry at the 6831 // top and a single exit at the bottom. 6832 // The point of exit cannot be a branch out of the structured block. 6833 // longjmp() and throw() must not violate the entry/exit criteria. 6834 CS->getCapturedDecl()->setNothrow(); 6835 6836 OMPLoopDirective::HelperExprs B; 6837 // In presence of clause 'collapse' with number of loops, it will 6838 // define the nested loops number. 6839 auto NestedLoopCount = CheckOpenMPLoop( 6840 OMPD_target_teams_distribute_parallel_for, 6841 getCollapseNumberExpr(Clauses), 6842 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6843 VarsWithImplicitDSA, B); 6844 if (NestedLoopCount == 0) 6845 return StmtError(); 6846 6847 assert((CurContext->isDependentContext() || B.builtAll()) && 6848 "omp target teams distribute parallel for loop exprs were not built"); 6849 6850 if (!CurContext->isDependentContext()) { 6851 // Finalize the clauses that need pre-built expressions for CodeGen. 6852 for (auto C : Clauses) { 6853 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6854 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6855 B.NumIterations, *this, CurScope, 6856 DSAStack)) 6857 return StmtError(); 6858 } 6859 } 6860 6861 getCurFunction()->setHasBranchProtectedScope(); 6862 return OMPTargetTeamsDistributeParallelForDirective::Create( 6863 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6864 } 6865 6866 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6867 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6868 SourceLocation EndLoc, 6869 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6870 if (!AStmt) 6871 return StmtError(); 6872 6873 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6874 // 1.2.2 OpenMP Language Terminology 6875 // Structured block - An executable statement with a single entry at the 6876 // top and a single exit at the bottom. 6877 // The point of exit cannot be a branch out of the structured block. 6878 // longjmp() and throw() must not violate the entry/exit criteria. 6879 CS->getCapturedDecl()->setNothrow(); 6880 6881 OMPLoopDirective::HelperExprs B; 6882 // In presence of clause 'collapse' with number of loops, it will 6883 // define the nested loops number. 6884 auto NestedLoopCount = CheckOpenMPLoop( 6885 OMPD_target_teams_distribute_parallel_for_simd, 6886 getCollapseNumberExpr(Clauses), 6887 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6888 VarsWithImplicitDSA, B); 6889 if (NestedLoopCount == 0) 6890 return StmtError(); 6891 6892 assert((CurContext->isDependentContext() || B.builtAll()) && 6893 "omp target teams distribute parallel for simd loop exprs were not " 6894 "built"); 6895 6896 if (!CurContext->isDependentContext()) { 6897 // Finalize the clauses that need pre-built expressions for CodeGen. 6898 for (auto C : Clauses) { 6899 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6900 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6901 B.NumIterations, *this, CurScope, 6902 DSAStack)) 6903 return StmtError(); 6904 } 6905 } 6906 6907 getCurFunction()->setHasBranchProtectedScope(); 6908 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 6909 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6910 } 6911 6912 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 6913 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6914 SourceLocation EndLoc, 6915 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6916 if (!AStmt) 6917 return StmtError(); 6918 6919 auto *CS = cast<CapturedStmt>(AStmt); 6920 // 1.2.2 OpenMP Language Terminology 6921 // Structured block - An executable statement with a single entry at the 6922 // top and a single exit at the bottom. 6923 // The point of exit cannot be a branch out of the structured block. 6924 // longjmp() and throw() must not violate the entry/exit criteria. 6925 CS->getCapturedDecl()->setNothrow(); 6926 6927 OMPLoopDirective::HelperExprs B; 6928 // In presence of clause 'collapse' with number of loops, it will 6929 // define the nested loops number. 6930 auto NestedLoopCount = CheckOpenMPLoop( 6931 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 6932 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6933 VarsWithImplicitDSA, B); 6934 if (NestedLoopCount == 0) 6935 return StmtError(); 6936 6937 assert((CurContext->isDependentContext() || B.builtAll()) && 6938 "omp target teams distribute simd loop exprs were not built"); 6939 6940 getCurFunction()->setHasBranchProtectedScope(); 6941 return OMPTargetTeamsDistributeSimdDirective::Create( 6942 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6943 } 6944 6945 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 6946 SourceLocation StartLoc, 6947 SourceLocation LParenLoc, 6948 SourceLocation EndLoc) { 6949 OMPClause *Res = nullptr; 6950 switch (Kind) { 6951 case OMPC_final: 6952 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 6953 break; 6954 case OMPC_num_threads: 6955 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 6956 break; 6957 case OMPC_safelen: 6958 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 6959 break; 6960 case OMPC_simdlen: 6961 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 6962 break; 6963 case OMPC_collapse: 6964 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 6965 break; 6966 case OMPC_ordered: 6967 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 6968 break; 6969 case OMPC_device: 6970 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 6971 break; 6972 case OMPC_num_teams: 6973 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 6974 break; 6975 case OMPC_thread_limit: 6976 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 6977 break; 6978 case OMPC_priority: 6979 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 6980 break; 6981 case OMPC_grainsize: 6982 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 6983 break; 6984 case OMPC_num_tasks: 6985 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 6986 break; 6987 case OMPC_hint: 6988 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 6989 break; 6990 case OMPC_if: 6991 case OMPC_default: 6992 case OMPC_proc_bind: 6993 case OMPC_schedule: 6994 case OMPC_private: 6995 case OMPC_firstprivate: 6996 case OMPC_lastprivate: 6997 case OMPC_shared: 6998 case OMPC_reduction: 6999 case OMPC_task_reduction: 7000 case OMPC_in_reduction: 7001 case OMPC_linear: 7002 case OMPC_aligned: 7003 case OMPC_copyin: 7004 case OMPC_copyprivate: 7005 case OMPC_nowait: 7006 case OMPC_untied: 7007 case OMPC_mergeable: 7008 case OMPC_threadprivate: 7009 case OMPC_flush: 7010 case OMPC_read: 7011 case OMPC_write: 7012 case OMPC_update: 7013 case OMPC_capture: 7014 case OMPC_seq_cst: 7015 case OMPC_depend: 7016 case OMPC_threads: 7017 case OMPC_simd: 7018 case OMPC_map: 7019 case OMPC_nogroup: 7020 case OMPC_dist_schedule: 7021 case OMPC_defaultmap: 7022 case OMPC_unknown: 7023 case OMPC_uniform: 7024 case OMPC_to: 7025 case OMPC_from: 7026 case OMPC_use_device_ptr: 7027 case OMPC_is_device_ptr: 7028 llvm_unreachable("Clause is not allowed."); 7029 } 7030 return Res; 7031 } 7032 7033 // An OpenMP directive such as 'target parallel' has two captured regions: 7034 // for the 'target' and 'parallel' respectively. This function returns 7035 // the region in which to capture expressions associated with a clause. 7036 // A return value of OMPD_unknown signifies that the expression should not 7037 // be captured. 7038 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 7039 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 7040 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 7041 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 7042 7043 switch (CKind) { 7044 case OMPC_if: 7045 switch (DKind) { 7046 case OMPD_target_parallel: 7047 // If this clause applies to the nested 'parallel' region, capture within 7048 // the 'target' region, otherwise do not capture. 7049 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 7050 CaptureRegion = OMPD_target; 7051 break; 7052 case OMPD_cancel: 7053 case OMPD_parallel: 7054 case OMPD_parallel_sections: 7055 case OMPD_parallel_for: 7056 case OMPD_parallel_for_simd: 7057 case OMPD_target: 7058 case OMPD_target_simd: 7059 case OMPD_target_parallel_for: 7060 case OMPD_target_parallel_for_simd: 7061 case OMPD_target_teams: 7062 case OMPD_target_teams_distribute: 7063 case OMPD_target_teams_distribute_simd: 7064 case OMPD_target_teams_distribute_parallel_for: 7065 case OMPD_target_teams_distribute_parallel_for_simd: 7066 case OMPD_teams_distribute_parallel_for: 7067 case OMPD_teams_distribute_parallel_for_simd: 7068 case OMPD_distribute_parallel_for: 7069 case OMPD_distribute_parallel_for_simd: 7070 case OMPD_task: 7071 case OMPD_taskloop: 7072 case OMPD_taskloop_simd: 7073 case OMPD_target_data: 7074 case OMPD_target_enter_data: 7075 case OMPD_target_exit_data: 7076 case OMPD_target_update: 7077 // Do not capture if-clause expressions. 7078 break; 7079 case OMPD_threadprivate: 7080 case OMPD_taskyield: 7081 case OMPD_barrier: 7082 case OMPD_taskwait: 7083 case OMPD_cancellation_point: 7084 case OMPD_flush: 7085 case OMPD_declare_reduction: 7086 case OMPD_declare_simd: 7087 case OMPD_declare_target: 7088 case OMPD_end_declare_target: 7089 case OMPD_teams: 7090 case OMPD_simd: 7091 case OMPD_for: 7092 case OMPD_for_simd: 7093 case OMPD_sections: 7094 case OMPD_section: 7095 case OMPD_single: 7096 case OMPD_master: 7097 case OMPD_critical: 7098 case OMPD_taskgroup: 7099 case OMPD_distribute: 7100 case OMPD_ordered: 7101 case OMPD_atomic: 7102 case OMPD_distribute_simd: 7103 case OMPD_teams_distribute: 7104 case OMPD_teams_distribute_simd: 7105 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 7106 case OMPD_unknown: 7107 llvm_unreachable("Unknown OpenMP directive"); 7108 } 7109 break; 7110 case OMPC_num_threads: 7111 switch (DKind) { 7112 case OMPD_target_parallel: 7113 CaptureRegion = OMPD_target; 7114 break; 7115 case OMPD_cancel: 7116 case OMPD_parallel: 7117 case OMPD_parallel_sections: 7118 case OMPD_parallel_for: 7119 case OMPD_parallel_for_simd: 7120 case OMPD_target: 7121 case OMPD_target_simd: 7122 case OMPD_target_parallel_for: 7123 case OMPD_target_parallel_for_simd: 7124 case OMPD_target_teams: 7125 case OMPD_target_teams_distribute: 7126 case OMPD_target_teams_distribute_simd: 7127 case OMPD_target_teams_distribute_parallel_for: 7128 case OMPD_target_teams_distribute_parallel_for_simd: 7129 case OMPD_teams_distribute_parallel_for: 7130 case OMPD_teams_distribute_parallel_for_simd: 7131 case OMPD_distribute_parallel_for: 7132 case OMPD_distribute_parallel_for_simd: 7133 case OMPD_task: 7134 case OMPD_taskloop: 7135 case OMPD_taskloop_simd: 7136 case OMPD_target_data: 7137 case OMPD_target_enter_data: 7138 case OMPD_target_exit_data: 7139 case OMPD_target_update: 7140 // Do not capture num_threads-clause expressions. 7141 break; 7142 case OMPD_threadprivate: 7143 case OMPD_taskyield: 7144 case OMPD_barrier: 7145 case OMPD_taskwait: 7146 case OMPD_cancellation_point: 7147 case OMPD_flush: 7148 case OMPD_declare_reduction: 7149 case OMPD_declare_simd: 7150 case OMPD_declare_target: 7151 case OMPD_end_declare_target: 7152 case OMPD_teams: 7153 case OMPD_simd: 7154 case OMPD_for: 7155 case OMPD_for_simd: 7156 case OMPD_sections: 7157 case OMPD_section: 7158 case OMPD_single: 7159 case OMPD_master: 7160 case OMPD_critical: 7161 case OMPD_taskgroup: 7162 case OMPD_distribute: 7163 case OMPD_ordered: 7164 case OMPD_atomic: 7165 case OMPD_distribute_simd: 7166 case OMPD_teams_distribute: 7167 case OMPD_teams_distribute_simd: 7168 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 7169 case OMPD_unknown: 7170 llvm_unreachable("Unknown OpenMP directive"); 7171 } 7172 break; 7173 case OMPC_num_teams: 7174 switch (DKind) { 7175 case OMPD_target_teams: 7176 CaptureRegion = OMPD_target; 7177 break; 7178 case OMPD_cancel: 7179 case OMPD_parallel: 7180 case OMPD_parallel_sections: 7181 case OMPD_parallel_for: 7182 case OMPD_parallel_for_simd: 7183 case OMPD_target: 7184 case OMPD_target_simd: 7185 case OMPD_target_parallel: 7186 case OMPD_target_parallel_for: 7187 case OMPD_target_parallel_for_simd: 7188 case OMPD_target_teams_distribute: 7189 case OMPD_target_teams_distribute_simd: 7190 case OMPD_target_teams_distribute_parallel_for: 7191 case OMPD_target_teams_distribute_parallel_for_simd: 7192 case OMPD_teams_distribute_parallel_for: 7193 case OMPD_teams_distribute_parallel_for_simd: 7194 case OMPD_distribute_parallel_for: 7195 case OMPD_distribute_parallel_for_simd: 7196 case OMPD_task: 7197 case OMPD_taskloop: 7198 case OMPD_taskloop_simd: 7199 case OMPD_target_data: 7200 case OMPD_target_enter_data: 7201 case OMPD_target_exit_data: 7202 case OMPD_target_update: 7203 case OMPD_teams: 7204 case OMPD_teams_distribute: 7205 case OMPD_teams_distribute_simd: 7206 // Do not capture num_teams-clause expressions. 7207 break; 7208 case OMPD_threadprivate: 7209 case OMPD_taskyield: 7210 case OMPD_barrier: 7211 case OMPD_taskwait: 7212 case OMPD_cancellation_point: 7213 case OMPD_flush: 7214 case OMPD_declare_reduction: 7215 case OMPD_declare_simd: 7216 case OMPD_declare_target: 7217 case OMPD_end_declare_target: 7218 case OMPD_simd: 7219 case OMPD_for: 7220 case OMPD_for_simd: 7221 case OMPD_sections: 7222 case OMPD_section: 7223 case OMPD_single: 7224 case OMPD_master: 7225 case OMPD_critical: 7226 case OMPD_taskgroup: 7227 case OMPD_distribute: 7228 case OMPD_ordered: 7229 case OMPD_atomic: 7230 case OMPD_distribute_simd: 7231 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 7232 case OMPD_unknown: 7233 llvm_unreachable("Unknown OpenMP directive"); 7234 } 7235 break; 7236 case OMPC_thread_limit: 7237 switch (DKind) { 7238 case OMPD_target_teams: 7239 CaptureRegion = OMPD_target; 7240 break; 7241 case OMPD_cancel: 7242 case OMPD_parallel: 7243 case OMPD_parallel_sections: 7244 case OMPD_parallel_for: 7245 case OMPD_parallel_for_simd: 7246 case OMPD_target: 7247 case OMPD_target_simd: 7248 case OMPD_target_parallel: 7249 case OMPD_target_parallel_for: 7250 case OMPD_target_parallel_for_simd: 7251 case OMPD_target_teams_distribute: 7252 case OMPD_target_teams_distribute_simd: 7253 case OMPD_target_teams_distribute_parallel_for: 7254 case OMPD_target_teams_distribute_parallel_for_simd: 7255 case OMPD_teams_distribute_parallel_for: 7256 case OMPD_teams_distribute_parallel_for_simd: 7257 case OMPD_distribute_parallel_for: 7258 case OMPD_distribute_parallel_for_simd: 7259 case OMPD_task: 7260 case OMPD_taskloop: 7261 case OMPD_taskloop_simd: 7262 case OMPD_target_data: 7263 case OMPD_target_enter_data: 7264 case OMPD_target_exit_data: 7265 case OMPD_target_update: 7266 case OMPD_teams: 7267 case OMPD_teams_distribute: 7268 case OMPD_teams_distribute_simd: 7269 // Do not capture thread_limit-clause expressions. 7270 break; 7271 case OMPD_threadprivate: 7272 case OMPD_taskyield: 7273 case OMPD_barrier: 7274 case OMPD_taskwait: 7275 case OMPD_cancellation_point: 7276 case OMPD_flush: 7277 case OMPD_declare_reduction: 7278 case OMPD_declare_simd: 7279 case OMPD_declare_target: 7280 case OMPD_end_declare_target: 7281 case OMPD_simd: 7282 case OMPD_for: 7283 case OMPD_for_simd: 7284 case OMPD_sections: 7285 case OMPD_section: 7286 case OMPD_single: 7287 case OMPD_master: 7288 case OMPD_critical: 7289 case OMPD_taskgroup: 7290 case OMPD_distribute: 7291 case OMPD_ordered: 7292 case OMPD_atomic: 7293 case OMPD_distribute_simd: 7294 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 7295 case OMPD_unknown: 7296 llvm_unreachable("Unknown OpenMP directive"); 7297 } 7298 break; 7299 case OMPC_schedule: 7300 case OMPC_dist_schedule: 7301 case OMPC_firstprivate: 7302 case OMPC_lastprivate: 7303 case OMPC_reduction: 7304 case OMPC_task_reduction: 7305 case OMPC_in_reduction: 7306 case OMPC_linear: 7307 case OMPC_default: 7308 case OMPC_proc_bind: 7309 case OMPC_final: 7310 case OMPC_safelen: 7311 case OMPC_simdlen: 7312 case OMPC_collapse: 7313 case OMPC_private: 7314 case OMPC_shared: 7315 case OMPC_aligned: 7316 case OMPC_copyin: 7317 case OMPC_copyprivate: 7318 case OMPC_ordered: 7319 case OMPC_nowait: 7320 case OMPC_untied: 7321 case OMPC_mergeable: 7322 case OMPC_threadprivate: 7323 case OMPC_flush: 7324 case OMPC_read: 7325 case OMPC_write: 7326 case OMPC_update: 7327 case OMPC_capture: 7328 case OMPC_seq_cst: 7329 case OMPC_depend: 7330 case OMPC_device: 7331 case OMPC_threads: 7332 case OMPC_simd: 7333 case OMPC_map: 7334 case OMPC_priority: 7335 case OMPC_grainsize: 7336 case OMPC_nogroup: 7337 case OMPC_num_tasks: 7338 case OMPC_hint: 7339 case OMPC_defaultmap: 7340 case OMPC_unknown: 7341 case OMPC_uniform: 7342 case OMPC_to: 7343 case OMPC_from: 7344 case OMPC_use_device_ptr: 7345 case OMPC_is_device_ptr: 7346 llvm_unreachable("Unexpected OpenMP clause."); 7347 } 7348 return CaptureRegion; 7349 } 7350 7351 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 7352 Expr *Condition, SourceLocation StartLoc, 7353 SourceLocation LParenLoc, 7354 SourceLocation NameModifierLoc, 7355 SourceLocation ColonLoc, 7356 SourceLocation EndLoc) { 7357 Expr *ValExpr = Condition; 7358 Stmt *HelperValStmt = nullptr; 7359 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 7360 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 7361 !Condition->isInstantiationDependent() && 7362 !Condition->containsUnexpandedParameterPack()) { 7363 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 7364 if (Val.isInvalid()) 7365 return nullptr; 7366 7367 ValExpr = MakeFullExpr(Val.get()).get(); 7368 7369 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 7370 CaptureRegion = 7371 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 7372 if (CaptureRegion != OMPD_unknown) { 7373 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 7374 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 7375 HelperValStmt = buildPreInits(Context, Captures); 7376 } 7377 } 7378 7379 return new (Context) 7380 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 7381 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 7382 } 7383 7384 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 7385 SourceLocation StartLoc, 7386 SourceLocation LParenLoc, 7387 SourceLocation EndLoc) { 7388 Expr *ValExpr = Condition; 7389 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 7390 !Condition->isInstantiationDependent() && 7391 !Condition->containsUnexpandedParameterPack()) { 7392 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 7393 if (Val.isInvalid()) 7394 return nullptr; 7395 7396 ValExpr = MakeFullExpr(Val.get()).get(); 7397 } 7398 7399 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 7400 } 7401 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 7402 Expr *Op) { 7403 if (!Op) 7404 return ExprError(); 7405 7406 class IntConvertDiagnoser : public ICEConvertDiagnoser { 7407 public: 7408 IntConvertDiagnoser() 7409 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 7410 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 7411 QualType T) override { 7412 return S.Diag(Loc, diag::err_omp_not_integral) << T; 7413 } 7414 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 7415 QualType T) override { 7416 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 7417 } 7418 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 7419 QualType T, 7420 QualType ConvTy) override { 7421 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 7422 } 7423 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 7424 QualType ConvTy) override { 7425 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 7426 << ConvTy->isEnumeralType() << ConvTy; 7427 } 7428 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 7429 QualType T) override { 7430 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 7431 } 7432 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 7433 QualType ConvTy) override { 7434 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 7435 << ConvTy->isEnumeralType() << ConvTy; 7436 } 7437 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 7438 QualType) override { 7439 llvm_unreachable("conversion functions are permitted"); 7440 } 7441 } ConvertDiagnoser; 7442 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 7443 } 7444 7445 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 7446 OpenMPClauseKind CKind, 7447 bool StrictlyPositive) { 7448 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 7449 !ValExpr->isInstantiationDependent()) { 7450 SourceLocation Loc = ValExpr->getExprLoc(); 7451 ExprResult Value = 7452 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 7453 if (Value.isInvalid()) 7454 return false; 7455 7456 ValExpr = Value.get(); 7457 // The expression must evaluate to a non-negative integer value. 7458 llvm::APSInt Result; 7459 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 7460 Result.isSigned() && 7461 !((!StrictlyPositive && Result.isNonNegative()) || 7462 (StrictlyPositive && Result.isStrictlyPositive()))) { 7463 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 7464 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 7465 << ValExpr->getSourceRange(); 7466 return false; 7467 } 7468 } 7469 return true; 7470 } 7471 7472 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 7473 SourceLocation StartLoc, 7474 SourceLocation LParenLoc, 7475 SourceLocation EndLoc) { 7476 Expr *ValExpr = NumThreads; 7477 Stmt *HelperValStmt = nullptr; 7478 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 7479 7480 // OpenMP [2.5, Restrictions] 7481 // The num_threads expression must evaluate to a positive integer value. 7482 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 7483 /*StrictlyPositive=*/true)) 7484 return nullptr; 7485 7486 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 7487 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 7488 if (CaptureRegion != OMPD_unknown) { 7489 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 7490 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 7491 HelperValStmt = buildPreInits(Context, Captures); 7492 } 7493 7494 return new (Context) OMPNumThreadsClause( 7495 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 7496 } 7497 7498 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 7499 OpenMPClauseKind CKind, 7500 bool StrictlyPositive) { 7501 if (!E) 7502 return ExprError(); 7503 if (E->isValueDependent() || E->isTypeDependent() || 7504 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 7505 return E; 7506 llvm::APSInt Result; 7507 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 7508 if (ICE.isInvalid()) 7509 return ExprError(); 7510 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 7511 (!StrictlyPositive && !Result.isNonNegative())) { 7512 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 7513 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 7514 << E->getSourceRange(); 7515 return ExprError(); 7516 } 7517 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 7518 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 7519 << E->getSourceRange(); 7520 return ExprError(); 7521 } 7522 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 7523 DSAStack->setAssociatedLoops(Result.getExtValue()); 7524 else if (CKind == OMPC_ordered) 7525 DSAStack->setAssociatedLoops(Result.getExtValue()); 7526 return ICE; 7527 } 7528 7529 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 7530 SourceLocation LParenLoc, 7531 SourceLocation EndLoc) { 7532 // OpenMP [2.8.1, simd construct, Description] 7533 // The parameter of the safelen clause must be a constant 7534 // positive integer expression. 7535 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 7536 if (Safelen.isInvalid()) 7537 return nullptr; 7538 return new (Context) 7539 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 7540 } 7541 7542 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 7543 SourceLocation LParenLoc, 7544 SourceLocation EndLoc) { 7545 // OpenMP [2.8.1, simd construct, Description] 7546 // The parameter of the simdlen clause must be a constant 7547 // positive integer expression. 7548 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 7549 if (Simdlen.isInvalid()) 7550 return nullptr; 7551 return new (Context) 7552 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 7553 } 7554 7555 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 7556 SourceLocation StartLoc, 7557 SourceLocation LParenLoc, 7558 SourceLocation EndLoc) { 7559 // OpenMP [2.7.1, loop construct, Description] 7560 // OpenMP [2.8.1, simd construct, Description] 7561 // OpenMP [2.9.6, distribute construct, Description] 7562 // The parameter of the collapse clause must be a constant 7563 // positive integer expression. 7564 ExprResult NumForLoopsResult = 7565 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 7566 if (NumForLoopsResult.isInvalid()) 7567 return nullptr; 7568 return new (Context) 7569 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 7570 } 7571 7572 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 7573 SourceLocation EndLoc, 7574 SourceLocation LParenLoc, 7575 Expr *NumForLoops) { 7576 // OpenMP [2.7.1, loop construct, Description] 7577 // OpenMP [2.8.1, simd construct, Description] 7578 // OpenMP [2.9.6, distribute construct, Description] 7579 // The parameter of the ordered clause must be a constant 7580 // positive integer expression if any. 7581 if (NumForLoops && LParenLoc.isValid()) { 7582 ExprResult NumForLoopsResult = 7583 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 7584 if (NumForLoopsResult.isInvalid()) 7585 return nullptr; 7586 NumForLoops = NumForLoopsResult.get(); 7587 } else 7588 NumForLoops = nullptr; 7589 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); 7590 return new (Context) 7591 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); 7592 } 7593 7594 OMPClause *Sema::ActOnOpenMPSimpleClause( 7595 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 7596 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 7597 OMPClause *Res = nullptr; 7598 switch (Kind) { 7599 case OMPC_default: 7600 Res = 7601 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 7602 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 7603 break; 7604 case OMPC_proc_bind: 7605 Res = ActOnOpenMPProcBindClause( 7606 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 7607 LParenLoc, EndLoc); 7608 break; 7609 case OMPC_if: 7610 case OMPC_final: 7611 case OMPC_num_threads: 7612 case OMPC_safelen: 7613 case OMPC_simdlen: 7614 case OMPC_collapse: 7615 case OMPC_schedule: 7616 case OMPC_private: 7617 case OMPC_firstprivate: 7618 case OMPC_lastprivate: 7619 case OMPC_shared: 7620 case OMPC_reduction: 7621 case OMPC_task_reduction: 7622 case OMPC_in_reduction: 7623 case OMPC_linear: 7624 case OMPC_aligned: 7625 case OMPC_copyin: 7626 case OMPC_copyprivate: 7627 case OMPC_ordered: 7628 case OMPC_nowait: 7629 case OMPC_untied: 7630 case OMPC_mergeable: 7631 case OMPC_threadprivate: 7632 case OMPC_flush: 7633 case OMPC_read: 7634 case OMPC_write: 7635 case OMPC_update: 7636 case OMPC_capture: 7637 case OMPC_seq_cst: 7638 case OMPC_depend: 7639 case OMPC_device: 7640 case OMPC_threads: 7641 case OMPC_simd: 7642 case OMPC_map: 7643 case OMPC_num_teams: 7644 case OMPC_thread_limit: 7645 case OMPC_priority: 7646 case OMPC_grainsize: 7647 case OMPC_nogroup: 7648 case OMPC_num_tasks: 7649 case OMPC_hint: 7650 case OMPC_dist_schedule: 7651 case OMPC_defaultmap: 7652 case OMPC_unknown: 7653 case OMPC_uniform: 7654 case OMPC_to: 7655 case OMPC_from: 7656 case OMPC_use_device_ptr: 7657 case OMPC_is_device_ptr: 7658 llvm_unreachable("Clause is not allowed."); 7659 } 7660 return Res; 7661 } 7662 7663 static std::string 7664 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 7665 ArrayRef<unsigned> Exclude = llvm::None) { 7666 std::string Values; 7667 unsigned Bound = Last >= 2 ? Last - 2 : 0; 7668 unsigned Skipped = Exclude.size(); 7669 auto S = Exclude.begin(), E = Exclude.end(); 7670 for (unsigned i = First; i < Last; ++i) { 7671 if (std::find(S, E, i) != E) { 7672 --Skipped; 7673 continue; 7674 } 7675 Values += "'"; 7676 Values += getOpenMPSimpleClauseTypeName(K, i); 7677 Values += "'"; 7678 if (i == Bound - Skipped) 7679 Values += " or "; 7680 else if (i != Bound + 1 - Skipped) 7681 Values += ", "; 7682 } 7683 return Values; 7684 } 7685 7686 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 7687 SourceLocation KindKwLoc, 7688 SourceLocation StartLoc, 7689 SourceLocation LParenLoc, 7690 SourceLocation EndLoc) { 7691 if (Kind == OMPC_DEFAULT_unknown) { 7692 static_assert(OMPC_DEFAULT_unknown > 0, 7693 "OMPC_DEFAULT_unknown not greater than 0"); 7694 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 7695 << getListOfPossibleValues(OMPC_default, /*First=*/0, 7696 /*Last=*/OMPC_DEFAULT_unknown) 7697 << getOpenMPClauseName(OMPC_default); 7698 return nullptr; 7699 } 7700 switch (Kind) { 7701 case OMPC_DEFAULT_none: 7702 DSAStack->setDefaultDSANone(KindKwLoc); 7703 break; 7704 case OMPC_DEFAULT_shared: 7705 DSAStack->setDefaultDSAShared(KindKwLoc); 7706 break; 7707 case OMPC_DEFAULT_unknown: 7708 llvm_unreachable("Clause kind is not allowed."); 7709 break; 7710 } 7711 return new (Context) 7712 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 7713 } 7714 7715 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 7716 SourceLocation KindKwLoc, 7717 SourceLocation StartLoc, 7718 SourceLocation LParenLoc, 7719 SourceLocation EndLoc) { 7720 if (Kind == OMPC_PROC_BIND_unknown) { 7721 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 7722 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 7723 /*Last=*/OMPC_PROC_BIND_unknown) 7724 << getOpenMPClauseName(OMPC_proc_bind); 7725 return nullptr; 7726 } 7727 return new (Context) 7728 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 7729 } 7730 7731 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 7732 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 7733 SourceLocation StartLoc, SourceLocation LParenLoc, 7734 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 7735 SourceLocation EndLoc) { 7736 OMPClause *Res = nullptr; 7737 switch (Kind) { 7738 case OMPC_schedule: 7739 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 7740 assert(Argument.size() == NumberOfElements && 7741 ArgumentLoc.size() == NumberOfElements); 7742 Res = ActOnOpenMPScheduleClause( 7743 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 7744 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 7745 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 7746 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 7747 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 7748 break; 7749 case OMPC_if: 7750 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 7751 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 7752 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 7753 DelimLoc, EndLoc); 7754 break; 7755 case OMPC_dist_schedule: 7756 Res = ActOnOpenMPDistScheduleClause( 7757 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 7758 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 7759 break; 7760 case OMPC_defaultmap: 7761 enum { Modifier, DefaultmapKind }; 7762 Res = ActOnOpenMPDefaultmapClause( 7763 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 7764 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 7765 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 7766 EndLoc); 7767 break; 7768 case OMPC_final: 7769 case OMPC_num_threads: 7770 case OMPC_safelen: 7771 case OMPC_simdlen: 7772 case OMPC_collapse: 7773 case OMPC_default: 7774 case OMPC_proc_bind: 7775 case OMPC_private: 7776 case OMPC_firstprivate: 7777 case OMPC_lastprivate: 7778 case OMPC_shared: 7779 case OMPC_reduction: 7780 case OMPC_task_reduction: 7781 case OMPC_in_reduction: 7782 case OMPC_linear: 7783 case OMPC_aligned: 7784 case OMPC_copyin: 7785 case OMPC_copyprivate: 7786 case OMPC_ordered: 7787 case OMPC_nowait: 7788 case OMPC_untied: 7789 case OMPC_mergeable: 7790 case OMPC_threadprivate: 7791 case OMPC_flush: 7792 case OMPC_read: 7793 case OMPC_write: 7794 case OMPC_update: 7795 case OMPC_capture: 7796 case OMPC_seq_cst: 7797 case OMPC_depend: 7798 case OMPC_device: 7799 case OMPC_threads: 7800 case OMPC_simd: 7801 case OMPC_map: 7802 case OMPC_num_teams: 7803 case OMPC_thread_limit: 7804 case OMPC_priority: 7805 case OMPC_grainsize: 7806 case OMPC_nogroup: 7807 case OMPC_num_tasks: 7808 case OMPC_hint: 7809 case OMPC_unknown: 7810 case OMPC_uniform: 7811 case OMPC_to: 7812 case OMPC_from: 7813 case OMPC_use_device_ptr: 7814 case OMPC_is_device_ptr: 7815 llvm_unreachable("Clause is not allowed."); 7816 } 7817 return Res; 7818 } 7819 7820 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 7821 OpenMPScheduleClauseModifier M2, 7822 SourceLocation M1Loc, SourceLocation M2Loc) { 7823 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 7824 SmallVector<unsigned, 2> Excluded; 7825 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 7826 Excluded.push_back(M2); 7827 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 7828 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 7829 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 7830 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 7831 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 7832 << getListOfPossibleValues(OMPC_schedule, 7833 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 7834 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 7835 Excluded) 7836 << getOpenMPClauseName(OMPC_schedule); 7837 return true; 7838 } 7839 return false; 7840 } 7841 7842 OMPClause *Sema::ActOnOpenMPScheduleClause( 7843 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 7844 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 7845 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 7846 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 7847 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 7848 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 7849 return nullptr; 7850 // OpenMP, 2.7.1, Loop Construct, Restrictions 7851 // Either the monotonic modifier or the nonmonotonic modifier can be specified 7852 // but not both. 7853 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 7854 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 7855 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 7856 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 7857 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 7858 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 7859 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 7860 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 7861 return nullptr; 7862 } 7863 if (Kind == OMPC_SCHEDULE_unknown) { 7864 std::string Values; 7865 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 7866 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 7867 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 7868 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 7869 Exclude); 7870 } else { 7871 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 7872 /*Last=*/OMPC_SCHEDULE_unknown); 7873 } 7874 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 7875 << Values << getOpenMPClauseName(OMPC_schedule); 7876 return nullptr; 7877 } 7878 // OpenMP, 2.7.1, Loop Construct, Restrictions 7879 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 7880 // schedule(guided). 7881 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 7882 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 7883 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 7884 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 7885 diag::err_omp_schedule_nonmonotonic_static); 7886 return nullptr; 7887 } 7888 Expr *ValExpr = ChunkSize; 7889 Stmt *HelperValStmt = nullptr; 7890 if (ChunkSize) { 7891 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 7892 !ChunkSize->isInstantiationDependent() && 7893 !ChunkSize->containsUnexpandedParameterPack()) { 7894 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 7895 ExprResult Val = 7896 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 7897 if (Val.isInvalid()) 7898 return nullptr; 7899 7900 ValExpr = Val.get(); 7901 7902 // OpenMP [2.7.1, Restrictions] 7903 // chunk_size must be a loop invariant integer expression with a positive 7904 // value. 7905 llvm::APSInt Result; 7906 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 7907 if (Result.isSigned() && !Result.isStrictlyPositive()) { 7908 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 7909 << "schedule" << 1 << ChunkSize->getSourceRange(); 7910 return nullptr; 7911 } 7912 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && 7913 !CurContext->isDependentContext()) { 7914 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 7915 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 7916 HelperValStmt = buildPreInits(Context, Captures); 7917 } 7918 } 7919 } 7920 7921 return new (Context) 7922 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 7923 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 7924 } 7925 7926 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 7927 SourceLocation StartLoc, 7928 SourceLocation EndLoc) { 7929 OMPClause *Res = nullptr; 7930 switch (Kind) { 7931 case OMPC_ordered: 7932 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 7933 break; 7934 case OMPC_nowait: 7935 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 7936 break; 7937 case OMPC_untied: 7938 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 7939 break; 7940 case OMPC_mergeable: 7941 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 7942 break; 7943 case OMPC_read: 7944 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 7945 break; 7946 case OMPC_write: 7947 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 7948 break; 7949 case OMPC_update: 7950 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 7951 break; 7952 case OMPC_capture: 7953 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 7954 break; 7955 case OMPC_seq_cst: 7956 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 7957 break; 7958 case OMPC_threads: 7959 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 7960 break; 7961 case OMPC_simd: 7962 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 7963 break; 7964 case OMPC_nogroup: 7965 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 7966 break; 7967 case OMPC_if: 7968 case OMPC_final: 7969 case OMPC_num_threads: 7970 case OMPC_safelen: 7971 case OMPC_simdlen: 7972 case OMPC_collapse: 7973 case OMPC_schedule: 7974 case OMPC_private: 7975 case OMPC_firstprivate: 7976 case OMPC_lastprivate: 7977 case OMPC_shared: 7978 case OMPC_reduction: 7979 case OMPC_task_reduction: 7980 case OMPC_in_reduction: 7981 case OMPC_linear: 7982 case OMPC_aligned: 7983 case OMPC_copyin: 7984 case OMPC_copyprivate: 7985 case OMPC_default: 7986 case OMPC_proc_bind: 7987 case OMPC_threadprivate: 7988 case OMPC_flush: 7989 case OMPC_depend: 7990 case OMPC_device: 7991 case OMPC_map: 7992 case OMPC_num_teams: 7993 case OMPC_thread_limit: 7994 case OMPC_priority: 7995 case OMPC_grainsize: 7996 case OMPC_num_tasks: 7997 case OMPC_hint: 7998 case OMPC_dist_schedule: 7999 case OMPC_defaultmap: 8000 case OMPC_unknown: 8001 case OMPC_uniform: 8002 case OMPC_to: 8003 case OMPC_from: 8004 case OMPC_use_device_ptr: 8005 case OMPC_is_device_ptr: 8006 llvm_unreachable("Clause is not allowed."); 8007 } 8008 return Res; 8009 } 8010 8011 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 8012 SourceLocation EndLoc) { 8013 DSAStack->setNowaitRegion(); 8014 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 8015 } 8016 8017 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 8018 SourceLocation EndLoc) { 8019 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 8020 } 8021 8022 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 8023 SourceLocation EndLoc) { 8024 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 8025 } 8026 8027 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 8028 SourceLocation EndLoc) { 8029 return new (Context) OMPReadClause(StartLoc, EndLoc); 8030 } 8031 8032 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 8033 SourceLocation EndLoc) { 8034 return new (Context) OMPWriteClause(StartLoc, EndLoc); 8035 } 8036 8037 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 8038 SourceLocation EndLoc) { 8039 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 8040 } 8041 8042 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 8043 SourceLocation EndLoc) { 8044 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 8045 } 8046 8047 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 8048 SourceLocation EndLoc) { 8049 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 8050 } 8051 8052 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 8053 SourceLocation EndLoc) { 8054 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 8055 } 8056 8057 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 8058 SourceLocation EndLoc) { 8059 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 8060 } 8061 8062 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 8063 SourceLocation EndLoc) { 8064 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 8065 } 8066 8067 OMPClause *Sema::ActOnOpenMPVarListClause( 8068 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 8069 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 8070 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 8071 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 8072 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 8073 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 8074 SourceLocation DepLinMapLoc) { 8075 OMPClause *Res = nullptr; 8076 switch (Kind) { 8077 case OMPC_private: 8078 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 8079 break; 8080 case OMPC_firstprivate: 8081 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 8082 break; 8083 case OMPC_lastprivate: 8084 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 8085 break; 8086 case OMPC_shared: 8087 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 8088 break; 8089 case OMPC_reduction: 8090 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 8091 EndLoc, ReductionIdScopeSpec, ReductionId); 8092 break; 8093 case OMPC_task_reduction: 8094 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 8095 EndLoc, ReductionIdScopeSpec, 8096 ReductionId); 8097 break; 8098 case OMPC_in_reduction: 8099 Res = 8100 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 8101 EndLoc, ReductionIdScopeSpec, ReductionId); 8102 break; 8103 case OMPC_linear: 8104 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 8105 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 8106 break; 8107 case OMPC_aligned: 8108 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 8109 ColonLoc, EndLoc); 8110 break; 8111 case OMPC_copyin: 8112 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 8113 break; 8114 case OMPC_copyprivate: 8115 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 8116 break; 8117 case OMPC_flush: 8118 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 8119 break; 8120 case OMPC_depend: 8121 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 8122 StartLoc, LParenLoc, EndLoc); 8123 break; 8124 case OMPC_map: 8125 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, 8126 DepLinMapLoc, ColonLoc, VarList, StartLoc, 8127 LParenLoc, EndLoc); 8128 break; 8129 case OMPC_to: 8130 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); 8131 break; 8132 case OMPC_from: 8133 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); 8134 break; 8135 case OMPC_use_device_ptr: 8136 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 8137 break; 8138 case OMPC_is_device_ptr: 8139 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 8140 break; 8141 case OMPC_if: 8142 case OMPC_final: 8143 case OMPC_num_threads: 8144 case OMPC_safelen: 8145 case OMPC_simdlen: 8146 case OMPC_collapse: 8147 case OMPC_default: 8148 case OMPC_proc_bind: 8149 case OMPC_schedule: 8150 case OMPC_ordered: 8151 case OMPC_nowait: 8152 case OMPC_untied: 8153 case OMPC_mergeable: 8154 case OMPC_threadprivate: 8155 case OMPC_read: 8156 case OMPC_write: 8157 case OMPC_update: 8158 case OMPC_capture: 8159 case OMPC_seq_cst: 8160 case OMPC_device: 8161 case OMPC_threads: 8162 case OMPC_simd: 8163 case OMPC_num_teams: 8164 case OMPC_thread_limit: 8165 case OMPC_priority: 8166 case OMPC_grainsize: 8167 case OMPC_nogroup: 8168 case OMPC_num_tasks: 8169 case OMPC_hint: 8170 case OMPC_dist_schedule: 8171 case OMPC_defaultmap: 8172 case OMPC_unknown: 8173 case OMPC_uniform: 8174 llvm_unreachable("Clause is not allowed."); 8175 } 8176 return Res; 8177 } 8178 8179 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 8180 ExprObjectKind OK, SourceLocation Loc) { 8181 ExprResult Res = BuildDeclRefExpr( 8182 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 8183 if (!Res.isUsable()) 8184 return ExprError(); 8185 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 8186 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 8187 if (!Res.isUsable()) 8188 return ExprError(); 8189 } 8190 if (VK != VK_LValue && Res.get()->isGLValue()) { 8191 Res = DefaultLvalueConversion(Res.get()); 8192 if (!Res.isUsable()) 8193 return ExprError(); 8194 } 8195 return Res; 8196 } 8197 8198 static std::pair<ValueDecl *, bool> 8199 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 8200 SourceRange &ERange, bool AllowArraySection = false) { 8201 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 8202 RefExpr->containsUnexpandedParameterPack()) 8203 return std::make_pair(nullptr, true); 8204 8205 // OpenMP [3.1, C/C++] 8206 // A list item is a variable name. 8207 // OpenMP [2.9.3.3, Restrictions, p.1] 8208 // A variable that is part of another variable (as an array or 8209 // structure element) cannot appear in a private clause. 8210 RefExpr = RefExpr->IgnoreParens(); 8211 enum { 8212 NoArrayExpr = -1, 8213 ArraySubscript = 0, 8214 OMPArraySection = 1 8215 } IsArrayExpr = NoArrayExpr; 8216 if (AllowArraySection) { 8217 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 8218 auto *Base = ASE->getBase()->IgnoreParenImpCasts(); 8219 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 8220 Base = TempASE->getBase()->IgnoreParenImpCasts(); 8221 RefExpr = Base; 8222 IsArrayExpr = ArraySubscript; 8223 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 8224 auto *Base = OASE->getBase()->IgnoreParenImpCasts(); 8225 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 8226 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 8227 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 8228 Base = TempASE->getBase()->IgnoreParenImpCasts(); 8229 RefExpr = Base; 8230 IsArrayExpr = OMPArraySection; 8231 } 8232 } 8233 ELoc = RefExpr->getExprLoc(); 8234 ERange = RefExpr->getSourceRange(); 8235 RefExpr = RefExpr->IgnoreParenImpCasts(); 8236 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 8237 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 8238 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 8239 (S.getCurrentThisType().isNull() || !ME || 8240 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 8241 !isa<FieldDecl>(ME->getMemberDecl()))) { 8242 if (IsArrayExpr != NoArrayExpr) 8243 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 8244 << ERange; 8245 else { 8246 S.Diag(ELoc, 8247 AllowArraySection 8248 ? diag::err_omp_expected_var_name_member_expr_or_array_item 8249 : diag::err_omp_expected_var_name_member_expr) 8250 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 8251 } 8252 return std::make_pair(nullptr, false); 8253 } 8254 return std::make_pair( 8255 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 8256 } 8257 8258 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 8259 SourceLocation StartLoc, 8260 SourceLocation LParenLoc, 8261 SourceLocation EndLoc) { 8262 SmallVector<Expr *, 8> Vars; 8263 SmallVector<Expr *, 8> PrivateCopies; 8264 for (auto &RefExpr : VarList) { 8265 assert(RefExpr && "NULL expr in OpenMP private clause."); 8266 SourceLocation ELoc; 8267 SourceRange ERange; 8268 Expr *SimpleRefExpr = RefExpr; 8269 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 8270 if (Res.second) { 8271 // It will be analyzed later. 8272 Vars.push_back(RefExpr); 8273 PrivateCopies.push_back(nullptr); 8274 } 8275 ValueDecl *D = Res.first; 8276 if (!D) 8277 continue; 8278 8279 QualType Type = D->getType(); 8280 auto *VD = dyn_cast<VarDecl>(D); 8281 8282 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 8283 // A variable that appears in a private clause must not have an incomplete 8284 // type or a reference type. 8285 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 8286 continue; 8287 Type = Type.getNonReferenceType(); 8288 8289 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 8290 // in a Construct] 8291 // Variables with the predetermined data-sharing attributes may not be 8292 // listed in data-sharing attributes clauses, except for the cases 8293 // listed below. For these exceptions only, listing a predetermined 8294 // variable in a data-sharing attribute clause is allowed and overrides 8295 // the variable's predetermined data-sharing attributes. 8296 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8297 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 8298 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 8299 << getOpenMPClauseName(OMPC_private); 8300 ReportOriginalDSA(*this, DSAStack, D, DVar); 8301 continue; 8302 } 8303 8304 auto CurrDir = DSAStack->getCurrentDirective(); 8305 // Variably modified types are not supported for tasks. 8306 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 8307 isOpenMPTaskingDirective(CurrDir)) { 8308 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 8309 << getOpenMPClauseName(OMPC_private) << Type 8310 << getOpenMPDirectiveName(CurrDir); 8311 bool IsDecl = 8312 !VD || 8313 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8314 Diag(D->getLocation(), 8315 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8316 << D; 8317 continue; 8318 } 8319 8320 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 8321 // A list item cannot appear in both a map clause and a data-sharing 8322 // attribute clause on the same construct 8323 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || 8324 CurrDir == OMPD_target_teams || 8325 CurrDir == OMPD_target_teams_distribute || 8326 CurrDir == OMPD_target_teams_distribute_parallel_for || 8327 CurrDir == OMPD_target_teams_distribute_parallel_for_simd || 8328 CurrDir == OMPD_target_teams_distribute_simd || 8329 CurrDir == OMPD_target_parallel_for_simd || 8330 CurrDir == OMPD_target_parallel_for) { 8331 OpenMPClauseKind ConflictKind; 8332 if (DSAStack->checkMappableExprComponentListsForDecl( 8333 VD, /*CurrentRegionOnly=*/true, 8334 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 8335 OpenMPClauseKind WhereFoundClauseKind) -> bool { 8336 ConflictKind = WhereFoundClauseKind; 8337 return true; 8338 })) { 8339 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 8340 << getOpenMPClauseName(OMPC_private) 8341 << getOpenMPClauseName(ConflictKind) 8342 << getOpenMPDirectiveName(CurrDir); 8343 ReportOriginalDSA(*this, DSAStack, D, DVar); 8344 continue; 8345 } 8346 } 8347 8348 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 8349 // A variable of class type (or array thereof) that appears in a private 8350 // clause requires an accessible, unambiguous default constructor for the 8351 // class type. 8352 // Generate helper private variable and initialize it with the default 8353 // value. The address of the original variable is replaced by the address of 8354 // the new private variable in CodeGen. This new variable is not added to 8355 // IdResolver, so the code in the OpenMP region uses original variable for 8356 // proper diagnostics. 8357 Type = Type.getUnqualifiedType(); 8358 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 8359 D->hasAttrs() ? &D->getAttrs() : nullptr); 8360 ActOnUninitializedDecl(VDPrivate); 8361 if (VDPrivate->isInvalidDecl()) 8362 continue; 8363 auto VDPrivateRefExpr = buildDeclRefExpr( 8364 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 8365 8366 DeclRefExpr *Ref = nullptr; 8367 if (!VD && !CurContext->isDependentContext()) 8368 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 8369 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 8370 Vars.push_back((VD || CurContext->isDependentContext()) 8371 ? RefExpr->IgnoreParens() 8372 : Ref); 8373 PrivateCopies.push_back(VDPrivateRefExpr); 8374 } 8375 8376 if (Vars.empty()) 8377 return nullptr; 8378 8379 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 8380 PrivateCopies); 8381 } 8382 8383 namespace { 8384 class DiagsUninitializedSeveretyRAII { 8385 private: 8386 DiagnosticsEngine &Diags; 8387 SourceLocation SavedLoc; 8388 bool IsIgnored; 8389 8390 public: 8391 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 8392 bool IsIgnored) 8393 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 8394 if (!IsIgnored) { 8395 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 8396 /*Map*/ diag::Severity::Ignored, Loc); 8397 } 8398 } 8399 ~DiagsUninitializedSeveretyRAII() { 8400 if (!IsIgnored) 8401 Diags.popMappings(SavedLoc); 8402 } 8403 }; 8404 } 8405 8406 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 8407 SourceLocation StartLoc, 8408 SourceLocation LParenLoc, 8409 SourceLocation EndLoc) { 8410 SmallVector<Expr *, 8> Vars; 8411 SmallVector<Expr *, 8> PrivateCopies; 8412 SmallVector<Expr *, 8> Inits; 8413 SmallVector<Decl *, 4> ExprCaptures; 8414 bool IsImplicitClause = 8415 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 8416 auto ImplicitClauseLoc = DSAStack->getConstructLoc(); 8417 8418 for (auto &RefExpr : VarList) { 8419 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 8420 SourceLocation ELoc; 8421 SourceRange ERange; 8422 Expr *SimpleRefExpr = RefExpr; 8423 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 8424 if (Res.second) { 8425 // It will be analyzed later. 8426 Vars.push_back(RefExpr); 8427 PrivateCopies.push_back(nullptr); 8428 Inits.push_back(nullptr); 8429 } 8430 ValueDecl *D = Res.first; 8431 if (!D) 8432 continue; 8433 8434 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 8435 QualType Type = D->getType(); 8436 auto *VD = dyn_cast<VarDecl>(D); 8437 8438 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 8439 // A variable that appears in a private clause must not have an incomplete 8440 // type or a reference type. 8441 if (RequireCompleteType(ELoc, Type, 8442 diag::err_omp_firstprivate_incomplete_type)) 8443 continue; 8444 Type = Type.getNonReferenceType(); 8445 8446 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 8447 // A variable of class type (or array thereof) that appears in a private 8448 // clause requires an accessible, unambiguous copy constructor for the 8449 // class type. 8450 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 8451 8452 // If an implicit firstprivate variable found it was checked already. 8453 DSAStackTy::DSAVarData TopDVar; 8454 if (!IsImplicitClause) { 8455 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8456 TopDVar = DVar; 8457 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 8458 bool IsConstant = ElemType.isConstant(Context); 8459 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 8460 // A list item that specifies a given variable may not appear in more 8461 // than one clause on the same directive, except that a variable may be 8462 // specified in both firstprivate and lastprivate clauses. 8463 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 8464 // A list item may appear in a firstprivate or lastprivate clause but not 8465 // both. 8466 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 8467 (CurrDir == OMPD_distribute || DVar.CKind != OMPC_lastprivate) && 8468 DVar.RefExpr) { 8469 Diag(ELoc, diag::err_omp_wrong_dsa) 8470 << getOpenMPClauseName(DVar.CKind) 8471 << getOpenMPClauseName(OMPC_firstprivate); 8472 ReportOriginalDSA(*this, DSAStack, D, DVar); 8473 continue; 8474 } 8475 8476 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 8477 // in a Construct] 8478 // Variables with the predetermined data-sharing attributes may not be 8479 // listed in data-sharing attributes clauses, except for the cases 8480 // listed below. For these exceptions only, listing a predetermined 8481 // variable in a data-sharing attribute clause is allowed and overrides 8482 // the variable's predetermined data-sharing attributes. 8483 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 8484 // in a Construct, C/C++, p.2] 8485 // Variables with const-qualified type having no mutable member may be 8486 // listed in a firstprivate clause, even if they are static data members. 8487 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 8488 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 8489 Diag(ELoc, diag::err_omp_wrong_dsa) 8490 << getOpenMPClauseName(DVar.CKind) 8491 << getOpenMPClauseName(OMPC_firstprivate); 8492 ReportOriginalDSA(*this, DSAStack, D, DVar); 8493 continue; 8494 } 8495 8496 // OpenMP [2.9.3.4, Restrictions, p.2] 8497 // A list item that is private within a parallel region must not appear 8498 // in a firstprivate clause on a worksharing construct if any of the 8499 // worksharing regions arising from the worksharing construct ever bind 8500 // to any of the parallel regions arising from the parallel construct. 8501 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 8502 // A list item that is private within a teams region must not appear in a 8503 // firstprivate clause on a distribute construct if any of the distribute 8504 // regions arising from the distribute construct ever bind to any of the 8505 // teams regions arising from the teams construct. 8506 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 8507 // A list item that appears in a reduction clause of a teams construct 8508 // must not appear in a firstprivate clause on a distribute construct if 8509 // any of the distribute regions arising from the distribute construct 8510 // ever bind to any of the teams regions arising from the teams construct. 8511 if ((isOpenMPWorksharingDirective(CurrDir) || 8512 isOpenMPDistributeDirective(CurrDir)) && 8513 !isOpenMPParallelDirective(CurrDir) && 8514 !isOpenMPTeamsDirective(CurrDir)) { 8515 DVar = DSAStack->getImplicitDSA(D, true); 8516 if (DVar.CKind != OMPC_shared && 8517 (isOpenMPParallelDirective(DVar.DKind) || 8518 isOpenMPTeamsDirective(DVar.DKind) || 8519 DVar.DKind == OMPD_unknown)) { 8520 Diag(ELoc, diag::err_omp_required_access) 8521 << getOpenMPClauseName(OMPC_firstprivate) 8522 << getOpenMPClauseName(OMPC_shared); 8523 ReportOriginalDSA(*this, DSAStack, D, DVar); 8524 continue; 8525 } 8526 } 8527 // OpenMP [2.9.3.4, Restrictions, p.3] 8528 // A list item that appears in a reduction clause of a parallel construct 8529 // must not appear in a firstprivate clause on a worksharing or task 8530 // construct if any of the worksharing or task regions arising from the 8531 // worksharing or task construct ever bind to any of the parallel regions 8532 // arising from the parallel construct. 8533 // OpenMP [2.9.3.4, Restrictions, p.4] 8534 // A list item that appears in a reduction clause in worksharing 8535 // construct must not appear in a firstprivate clause in a task construct 8536 // encountered during execution of any of the worksharing regions arising 8537 // from the worksharing construct. 8538 if (isOpenMPTaskingDirective(CurrDir)) { 8539 DVar = DSAStack->hasInnermostDSA( 8540 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 8541 [](OpenMPDirectiveKind K) -> bool { 8542 return isOpenMPParallelDirective(K) || 8543 isOpenMPWorksharingDirective(K) || 8544 isOpenMPTeamsDirective(K); 8545 }, 8546 /*FromParent=*/true); 8547 if (DVar.CKind == OMPC_reduction && 8548 (isOpenMPParallelDirective(DVar.DKind) || 8549 isOpenMPWorksharingDirective(DVar.DKind) || 8550 isOpenMPTeamsDirective(DVar.DKind))) { 8551 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 8552 << getOpenMPDirectiveName(DVar.DKind); 8553 ReportOriginalDSA(*this, DSAStack, D, DVar); 8554 continue; 8555 } 8556 } 8557 8558 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 8559 // A list item cannot appear in both a map clause and a data-sharing 8560 // attribute clause on the same construct 8561 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || 8562 CurrDir == OMPD_target_teams || 8563 CurrDir == OMPD_target_teams_distribute || 8564 CurrDir == OMPD_target_teams_distribute_parallel_for || 8565 CurrDir == OMPD_target_teams_distribute_parallel_for_simd || 8566 CurrDir == OMPD_target_teams_distribute_simd || 8567 CurrDir == OMPD_target_parallel_for_simd || 8568 CurrDir == OMPD_target_parallel_for) { 8569 OpenMPClauseKind ConflictKind; 8570 if (DSAStack->checkMappableExprComponentListsForDecl( 8571 VD, /*CurrentRegionOnly=*/true, 8572 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 8573 OpenMPClauseKind WhereFoundClauseKind) -> bool { 8574 ConflictKind = WhereFoundClauseKind; 8575 return true; 8576 })) { 8577 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 8578 << getOpenMPClauseName(OMPC_firstprivate) 8579 << getOpenMPClauseName(ConflictKind) 8580 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 8581 ReportOriginalDSA(*this, DSAStack, D, DVar); 8582 continue; 8583 } 8584 } 8585 } 8586 8587 // Variably modified types are not supported for tasks. 8588 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 8589 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 8590 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 8591 << getOpenMPClauseName(OMPC_firstprivate) << Type 8592 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 8593 bool IsDecl = 8594 !VD || 8595 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8596 Diag(D->getLocation(), 8597 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8598 << D; 8599 continue; 8600 } 8601 8602 Type = Type.getUnqualifiedType(); 8603 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 8604 D->hasAttrs() ? &D->getAttrs() : nullptr); 8605 // Generate helper private variable and initialize it with the value of the 8606 // original variable. The address of the original variable is replaced by 8607 // the address of the new private variable in the CodeGen. This new variable 8608 // is not added to IdResolver, so the code in the OpenMP region uses 8609 // original variable for proper diagnostics and variable capturing. 8610 Expr *VDInitRefExpr = nullptr; 8611 // For arrays generate initializer for single element and replace it by the 8612 // original array element in CodeGen. 8613 if (Type->isArrayType()) { 8614 auto VDInit = 8615 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 8616 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 8617 auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); 8618 ElemType = ElemType.getUnqualifiedType(); 8619 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 8620 ".firstprivate.temp"); 8621 InitializedEntity Entity = 8622 InitializedEntity::InitializeVariable(VDInitTemp); 8623 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 8624 8625 InitializationSequence InitSeq(*this, Entity, Kind, Init); 8626 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 8627 if (Result.isInvalid()) 8628 VDPrivate->setInvalidDecl(); 8629 else 8630 VDPrivate->setInit(Result.getAs<Expr>()); 8631 // Remove temp variable declaration. 8632 Context.Deallocate(VDInitTemp); 8633 } else { 8634 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 8635 ".firstprivate.temp"); 8636 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 8637 RefExpr->getExprLoc()); 8638 AddInitializerToDecl(VDPrivate, 8639 DefaultLvalueConversion(VDInitRefExpr).get(), 8640 /*DirectInit=*/false); 8641 } 8642 if (VDPrivate->isInvalidDecl()) { 8643 if (IsImplicitClause) { 8644 Diag(RefExpr->getExprLoc(), 8645 diag::note_omp_task_predetermined_firstprivate_here); 8646 } 8647 continue; 8648 } 8649 CurContext->addDecl(VDPrivate); 8650 auto VDPrivateRefExpr = buildDeclRefExpr( 8651 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 8652 RefExpr->getExprLoc()); 8653 DeclRefExpr *Ref = nullptr; 8654 if (!VD && !CurContext->isDependentContext()) { 8655 if (TopDVar.CKind == OMPC_lastprivate) 8656 Ref = TopDVar.PrivateCopy; 8657 else { 8658 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 8659 if (!IsOpenMPCapturedDecl(D)) 8660 ExprCaptures.push_back(Ref->getDecl()); 8661 } 8662 } 8663 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 8664 Vars.push_back((VD || CurContext->isDependentContext()) 8665 ? RefExpr->IgnoreParens() 8666 : Ref); 8667 PrivateCopies.push_back(VDPrivateRefExpr); 8668 Inits.push_back(VDInitRefExpr); 8669 } 8670 8671 if (Vars.empty()) 8672 return nullptr; 8673 8674 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 8675 Vars, PrivateCopies, Inits, 8676 buildPreInits(Context, ExprCaptures)); 8677 } 8678 8679 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 8680 SourceLocation StartLoc, 8681 SourceLocation LParenLoc, 8682 SourceLocation EndLoc) { 8683 SmallVector<Expr *, 8> Vars; 8684 SmallVector<Expr *, 8> SrcExprs; 8685 SmallVector<Expr *, 8> DstExprs; 8686 SmallVector<Expr *, 8> AssignmentOps; 8687 SmallVector<Decl *, 4> ExprCaptures; 8688 SmallVector<Expr *, 4> ExprPostUpdates; 8689 for (auto &RefExpr : VarList) { 8690 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 8691 SourceLocation ELoc; 8692 SourceRange ERange; 8693 Expr *SimpleRefExpr = RefExpr; 8694 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 8695 if (Res.second) { 8696 // It will be analyzed later. 8697 Vars.push_back(RefExpr); 8698 SrcExprs.push_back(nullptr); 8699 DstExprs.push_back(nullptr); 8700 AssignmentOps.push_back(nullptr); 8701 } 8702 ValueDecl *D = Res.first; 8703 if (!D) 8704 continue; 8705 8706 QualType Type = D->getType(); 8707 auto *VD = dyn_cast<VarDecl>(D); 8708 8709 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 8710 // A variable that appears in a lastprivate clause must not have an 8711 // incomplete type or a reference type. 8712 if (RequireCompleteType(ELoc, Type, 8713 diag::err_omp_lastprivate_incomplete_type)) 8714 continue; 8715 Type = Type.getNonReferenceType(); 8716 8717 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 8718 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 8719 // in a Construct] 8720 // Variables with the predetermined data-sharing attributes may not be 8721 // listed in data-sharing attributes clauses, except for the cases 8722 // listed below. 8723 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 8724 // A list item may appear in a firstprivate or lastprivate clause but not 8725 // both. 8726 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8727 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 8728 (CurrDir == OMPD_distribute || DVar.CKind != OMPC_firstprivate) && 8729 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 8730 Diag(ELoc, diag::err_omp_wrong_dsa) 8731 << getOpenMPClauseName(DVar.CKind) 8732 << getOpenMPClauseName(OMPC_lastprivate); 8733 ReportOriginalDSA(*this, DSAStack, D, DVar); 8734 continue; 8735 } 8736 8737 // OpenMP [2.14.3.5, Restrictions, p.2] 8738 // A list item that is private within a parallel region, or that appears in 8739 // the reduction clause of a parallel construct, must not appear in a 8740 // lastprivate clause on a worksharing construct if any of the corresponding 8741 // worksharing regions ever binds to any of the corresponding parallel 8742 // regions. 8743 DSAStackTy::DSAVarData TopDVar = DVar; 8744 if (isOpenMPWorksharingDirective(CurrDir) && 8745 !isOpenMPParallelDirective(CurrDir) && 8746 !isOpenMPTeamsDirective(CurrDir)) { 8747 DVar = DSAStack->getImplicitDSA(D, true); 8748 if (DVar.CKind != OMPC_shared) { 8749 Diag(ELoc, diag::err_omp_required_access) 8750 << getOpenMPClauseName(OMPC_lastprivate) 8751 << getOpenMPClauseName(OMPC_shared); 8752 ReportOriginalDSA(*this, DSAStack, D, DVar); 8753 continue; 8754 } 8755 } 8756 8757 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 8758 // A variable of class type (or array thereof) that appears in a 8759 // lastprivate clause requires an accessible, unambiguous default 8760 // constructor for the class type, unless the list item is also specified 8761 // in a firstprivate clause. 8762 // A variable of class type (or array thereof) that appears in a 8763 // lastprivate clause requires an accessible, unambiguous copy assignment 8764 // operator for the class type. 8765 Type = Context.getBaseElementType(Type).getNonReferenceType(); 8766 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(), 8767 Type.getUnqualifiedType(), ".lastprivate.src", 8768 D->hasAttrs() ? &D->getAttrs() : nullptr); 8769 auto *PseudoSrcExpr = 8770 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 8771 auto *DstVD = 8772 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 8773 D->hasAttrs() ? &D->getAttrs() : nullptr); 8774 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 8775 // For arrays generate assignment operation for single element and replace 8776 // it by the original array element in CodeGen. 8777 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 8778 PseudoDstExpr, PseudoSrcExpr); 8779 if (AssignmentOp.isInvalid()) 8780 continue; 8781 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 8782 /*DiscardedValue=*/true); 8783 if (AssignmentOp.isInvalid()) 8784 continue; 8785 8786 DeclRefExpr *Ref = nullptr; 8787 if (!VD && !CurContext->isDependentContext()) { 8788 if (TopDVar.CKind == OMPC_firstprivate) 8789 Ref = TopDVar.PrivateCopy; 8790 else { 8791 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 8792 if (!IsOpenMPCapturedDecl(D)) 8793 ExprCaptures.push_back(Ref->getDecl()); 8794 } 8795 if (TopDVar.CKind == OMPC_firstprivate || 8796 (!IsOpenMPCapturedDecl(D) && 8797 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 8798 ExprResult RefRes = DefaultLvalueConversion(Ref); 8799 if (!RefRes.isUsable()) 8800 continue; 8801 ExprResult PostUpdateRes = 8802 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 8803 RefRes.get()); 8804 if (!PostUpdateRes.isUsable()) 8805 continue; 8806 ExprPostUpdates.push_back( 8807 IgnoredValueConversions(PostUpdateRes.get()).get()); 8808 } 8809 } 8810 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 8811 Vars.push_back((VD || CurContext->isDependentContext()) 8812 ? RefExpr->IgnoreParens() 8813 : Ref); 8814 SrcExprs.push_back(PseudoSrcExpr); 8815 DstExprs.push_back(PseudoDstExpr); 8816 AssignmentOps.push_back(AssignmentOp.get()); 8817 } 8818 8819 if (Vars.empty()) 8820 return nullptr; 8821 8822 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 8823 Vars, SrcExprs, DstExprs, AssignmentOps, 8824 buildPreInits(Context, ExprCaptures), 8825 buildPostUpdate(*this, ExprPostUpdates)); 8826 } 8827 8828 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 8829 SourceLocation StartLoc, 8830 SourceLocation LParenLoc, 8831 SourceLocation EndLoc) { 8832 SmallVector<Expr *, 8> Vars; 8833 for (auto &RefExpr : VarList) { 8834 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 8835 SourceLocation ELoc; 8836 SourceRange ERange; 8837 Expr *SimpleRefExpr = RefExpr; 8838 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 8839 if (Res.second) { 8840 // It will be analyzed later. 8841 Vars.push_back(RefExpr); 8842 } 8843 ValueDecl *D = Res.first; 8844 if (!D) 8845 continue; 8846 8847 auto *VD = dyn_cast<VarDecl>(D); 8848 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 8849 // in a Construct] 8850 // Variables with the predetermined data-sharing attributes may not be 8851 // listed in data-sharing attributes clauses, except for the cases 8852 // listed below. For these exceptions only, listing a predetermined 8853 // variable in a data-sharing attribute clause is allowed and overrides 8854 // the variable's predetermined data-sharing attributes. 8855 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8856 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 8857 DVar.RefExpr) { 8858 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 8859 << getOpenMPClauseName(OMPC_shared); 8860 ReportOriginalDSA(*this, DSAStack, D, DVar); 8861 continue; 8862 } 8863 8864 DeclRefExpr *Ref = nullptr; 8865 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 8866 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 8867 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 8868 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 8869 ? RefExpr->IgnoreParens() 8870 : Ref); 8871 } 8872 8873 if (Vars.empty()) 8874 return nullptr; 8875 8876 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 8877 } 8878 8879 namespace { 8880 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 8881 DSAStackTy *Stack; 8882 8883 public: 8884 bool VisitDeclRefExpr(DeclRefExpr *E) { 8885 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 8886 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); 8887 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 8888 return false; 8889 if (DVar.CKind != OMPC_unknown) 8890 return true; 8891 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 8892 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, 8893 /*FromParent=*/true); 8894 if (DVarPrivate.CKind != OMPC_unknown) 8895 return true; 8896 return false; 8897 } 8898 return false; 8899 } 8900 bool VisitStmt(Stmt *S) { 8901 for (auto Child : S->children()) { 8902 if (Child && Visit(Child)) 8903 return true; 8904 } 8905 return false; 8906 } 8907 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 8908 }; 8909 } // namespace 8910 8911 namespace { 8912 // Transform MemberExpression for specified FieldDecl of current class to 8913 // DeclRefExpr to specified OMPCapturedExprDecl. 8914 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 8915 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 8916 ValueDecl *Field; 8917 DeclRefExpr *CapturedExpr; 8918 8919 public: 8920 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 8921 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 8922 8923 ExprResult TransformMemberExpr(MemberExpr *E) { 8924 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 8925 E->getMemberDecl() == Field) { 8926 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 8927 return CapturedExpr; 8928 } 8929 return BaseTransform::TransformMemberExpr(E); 8930 } 8931 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 8932 }; 8933 } // namespace 8934 8935 template <typename T> 8936 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups, 8937 const llvm::function_ref<T(ValueDecl *)> &Gen) { 8938 for (auto &Set : Lookups) { 8939 for (auto *D : Set) { 8940 if (auto Res = Gen(cast<ValueDecl>(D))) 8941 return Res; 8942 } 8943 } 8944 return T(); 8945 } 8946 8947 static ExprResult 8948 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 8949 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 8950 const DeclarationNameInfo &ReductionId, QualType Ty, 8951 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 8952 if (ReductionIdScopeSpec.isInvalid()) 8953 return ExprError(); 8954 SmallVector<UnresolvedSet<8>, 4> Lookups; 8955 if (S) { 8956 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 8957 Lookup.suppressDiagnostics(); 8958 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 8959 auto *D = Lookup.getRepresentativeDecl(); 8960 do { 8961 S = S->getParent(); 8962 } while (S && !S->isDeclScope(D)); 8963 if (S) 8964 S = S->getParent(); 8965 Lookups.push_back(UnresolvedSet<8>()); 8966 Lookups.back().append(Lookup.begin(), Lookup.end()); 8967 Lookup.clear(); 8968 } 8969 } else if (auto *ULE = 8970 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 8971 Lookups.push_back(UnresolvedSet<8>()); 8972 Decl *PrevD = nullptr; 8973 for (auto *D : ULE->decls()) { 8974 if (D == PrevD) 8975 Lookups.push_back(UnresolvedSet<8>()); 8976 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 8977 Lookups.back().addDecl(DRD); 8978 PrevD = D; 8979 } 8980 } 8981 if (Ty->isDependentType() || Ty->isInstantiationDependentType() || 8982 Ty->containsUnexpandedParameterPack() || 8983 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool { 8984 return !D->isInvalidDecl() && 8985 (D->getType()->isDependentType() || 8986 D->getType()->isInstantiationDependentType() || 8987 D->getType()->containsUnexpandedParameterPack()); 8988 })) { 8989 UnresolvedSet<8> ResSet; 8990 for (auto &Set : Lookups) { 8991 ResSet.append(Set.begin(), Set.end()); 8992 // The last item marks the end of all declarations at the specified scope. 8993 ResSet.addDecl(Set[Set.size() - 1]); 8994 } 8995 return UnresolvedLookupExpr::Create( 8996 SemaRef.Context, /*NamingClass=*/nullptr, 8997 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 8998 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 8999 } 9000 if (auto *VD = filterLookupForUDR<ValueDecl *>( 9001 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 9002 if (!D->isInvalidDecl() && 9003 SemaRef.Context.hasSameType(D->getType(), Ty)) 9004 return D; 9005 return nullptr; 9006 })) 9007 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 9008 if (auto *VD = filterLookupForUDR<ValueDecl *>( 9009 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 9010 if (!D->isInvalidDecl() && 9011 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 9012 !Ty.isMoreQualifiedThan(D->getType())) 9013 return D; 9014 return nullptr; 9015 })) { 9016 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 9017 /*DetectVirtual=*/false); 9018 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 9019 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 9020 VD->getType().getUnqualifiedType()))) { 9021 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 9022 /*DiagID=*/0) != 9023 Sema::AR_inaccessible) { 9024 SemaRef.BuildBasePathArray(Paths, BasePath); 9025 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 9026 } 9027 } 9028 } 9029 } 9030 if (ReductionIdScopeSpec.isSet()) { 9031 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 9032 return ExprError(); 9033 } 9034 return ExprEmpty(); 9035 } 9036 9037 namespace { 9038 /// Data for the reduction-based clauses. 9039 struct ReductionData { 9040 /// List of original reduction items. 9041 SmallVector<Expr *, 8> Vars; 9042 /// List of private copies of the reduction items. 9043 SmallVector<Expr *, 8> Privates; 9044 /// LHS expressions for the reduction_op expressions. 9045 SmallVector<Expr *, 8> LHSs; 9046 /// RHS expressions for the reduction_op expressions. 9047 SmallVector<Expr *, 8> RHSs; 9048 /// Reduction operation expression. 9049 SmallVector<Expr *, 8> ReductionOps; 9050 /// List of captures for clause. 9051 SmallVector<Decl *, 4> ExprCaptures; 9052 /// List of postupdate expressions. 9053 SmallVector<Expr *, 4> ExprPostUpdates; 9054 ReductionData() = delete; 9055 /// Reserves required memory for the reduction data. 9056 ReductionData(unsigned Size) { 9057 Vars.reserve(Size); 9058 Privates.reserve(Size); 9059 LHSs.reserve(Size); 9060 RHSs.reserve(Size); 9061 ReductionOps.reserve(Size); 9062 ExprCaptures.reserve(Size); 9063 ExprPostUpdates.reserve(Size); 9064 } 9065 /// Stores reduction item and reduction operation only (required for dependent 9066 /// reduction item). 9067 void push(Expr *Item, Expr *ReductionOp) { 9068 Vars.emplace_back(Item); 9069 Privates.emplace_back(nullptr); 9070 LHSs.emplace_back(nullptr); 9071 RHSs.emplace_back(nullptr); 9072 ReductionOps.emplace_back(ReductionOp); 9073 } 9074 /// Stores reduction data. 9075 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, 9076 Expr *ReductionOp) { 9077 Vars.emplace_back(Item); 9078 Privates.emplace_back(Private); 9079 LHSs.emplace_back(LHS); 9080 RHSs.emplace_back(RHS); 9081 ReductionOps.emplace_back(ReductionOp); 9082 } 9083 }; 9084 } // namespace 9085 9086 static bool ActOnOMPReductionKindClause( 9087 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 9088 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 9089 SourceLocation ColonLoc, SourceLocation EndLoc, 9090 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 9091 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 9092 auto DN = ReductionId.getName(); 9093 auto OOK = DN.getCXXOverloadedOperator(); 9094 BinaryOperatorKind BOK = BO_Comma; 9095 9096 ASTContext &Context = S.Context; 9097 // OpenMP [2.14.3.6, reduction clause] 9098 // C 9099 // reduction-identifier is either an identifier or one of the following 9100 // operators: +, -, *, &, |, ^, && and || 9101 // C++ 9102 // reduction-identifier is either an id-expression or one of the following 9103 // operators: +, -, *, &, |, ^, && and || 9104 switch (OOK) { 9105 case OO_Plus: 9106 case OO_Minus: 9107 BOK = BO_Add; 9108 break; 9109 case OO_Star: 9110 BOK = BO_Mul; 9111 break; 9112 case OO_Amp: 9113 BOK = BO_And; 9114 break; 9115 case OO_Pipe: 9116 BOK = BO_Or; 9117 break; 9118 case OO_Caret: 9119 BOK = BO_Xor; 9120 break; 9121 case OO_AmpAmp: 9122 BOK = BO_LAnd; 9123 break; 9124 case OO_PipePipe: 9125 BOK = BO_LOr; 9126 break; 9127 case OO_New: 9128 case OO_Delete: 9129 case OO_Array_New: 9130 case OO_Array_Delete: 9131 case OO_Slash: 9132 case OO_Percent: 9133 case OO_Tilde: 9134 case OO_Exclaim: 9135 case OO_Equal: 9136 case OO_Less: 9137 case OO_Greater: 9138 case OO_LessEqual: 9139 case OO_GreaterEqual: 9140 case OO_PlusEqual: 9141 case OO_MinusEqual: 9142 case OO_StarEqual: 9143 case OO_SlashEqual: 9144 case OO_PercentEqual: 9145 case OO_CaretEqual: 9146 case OO_AmpEqual: 9147 case OO_PipeEqual: 9148 case OO_LessLess: 9149 case OO_GreaterGreater: 9150 case OO_LessLessEqual: 9151 case OO_GreaterGreaterEqual: 9152 case OO_EqualEqual: 9153 case OO_ExclaimEqual: 9154 case OO_PlusPlus: 9155 case OO_MinusMinus: 9156 case OO_Comma: 9157 case OO_ArrowStar: 9158 case OO_Arrow: 9159 case OO_Call: 9160 case OO_Subscript: 9161 case OO_Conditional: 9162 case OO_Coawait: 9163 case NUM_OVERLOADED_OPERATORS: 9164 llvm_unreachable("Unexpected reduction identifier"); 9165 case OO_None: 9166 if (auto *II = DN.getAsIdentifierInfo()) { 9167 if (II->isStr("max")) 9168 BOK = BO_GT; 9169 else if (II->isStr("min")) 9170 BOK = BO_LT; 9171 } 9172 break; 9173 } 9174 SourceRange ReductionIdRange; 9175 if (ReductionIdScopeSpec.isValid()) 9176 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 9177 else 9178 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 9179 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 9180 9181 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 9182 bool FirstIter = true; 9183 for (auto RefExpr : VarList) { 9184 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 9185 // OpenMP [2.1, C/C++] 9186 // A list item is a variable or array section, subject to the restrictions 9187 // specified in Section 2.4 on page 42 and in each of the sections 9188 // describing clauses and directives for which a list appears. 9189 // OpenMP [2.14.3.3, Restrictions, p.1] 9190 // A variable that is part of another variable (as an array or 9191 // structure element) cannot appear in a private clause. 9192 if (!FirstIter && IR != ER) 9193 ++IR; 9194 FirstIter = false; 9195 SourceLocation ELoc; 9196 SourceRange ERange; 9197 Expr *SimpleRefExpr = RefExpr; 9198 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 9199 /*AllowArraySection=*/true); 9200 if (Res.second) { 9201 // Try to find 'declare reduction' corresponding construct before using 9202 // builtin/overloaded operators. 9203 QualType Type = Context.DependentTy; 9204 CXXCastPath BasePath; 9205 ExprResult DeclareReductionRef = buildDeclareReductionRef( 9206 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 9207 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 9208 Expr *ReductionOp = nullptr; 9209 if (S.CurContext->isDependentContext() && 9210 (DeclareReductionRef.isUnset() || 9211 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 9212 ReductionOp = DeclareReductionRef.get(); 9213 // It will be analyzed later. 9214 RD.push(RefExpr, ReductionOp); 9215 } 9216 ValueDecl *D = Res.first; 9217 if (!D) 9218 continue; 9219 9220 QualType Type; 9221 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 9222 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 9223 if (ASE) 9224 Type = ASE->getType().getNonReferenceType(); 9225 else if (OASE) { 9226 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 9227 if (auto *ATy = BaseType->getAsArrayTypeUnsafe()) 9228 Type = ATy->getElementType(); 9229 else 9230 Type = BaseType->getPointeeType(); 9231 Type = Type.getNonReferenceType(); 9232 } else 9233 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 9234 auto *VD = dyn_cast<VarDecl>(D); 9235 9236 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9237 // A variable that appears in a private clause must not have an incomplete 9238 // type or a reference type. 9239 if (S.RequireCompleteType(ELoc, Type, 9240 diag::err_omp_reduction_incomplete_type)) 9241 continue; 9242 // OpenMP [2.14.3.6, reduction clause, Restrictions] 9243 // A list item that appears in a reduction clause must not be 9244 // const-qualified. 9245 if (Type.getNonReferenceType().isConstant(Context)) { 9246 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange; 9247 if (!ASE && !OASE) { 9248 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 9249 VarDecl::DeclarationOnly; 9250 S.Diag(D->getLocation(), 9251 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9252 << D; 9253 } 9254 continue; 9255 } 9256 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 9257 // If a list-item is a reference type then it must bind to the same object 9258 // for all threads of the team. 9259 if (!ASE && !OASE && VD) { 9260 VarDecl *VDDef = VD->getDefinition(); 9261 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 9262 DSARefChecker Check(Stack); 9263 if (Check.Visit(VDDef->getInit())) { 9264 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 9265 << getOpenMPClauseName(ClauseKind) << ERange; 9266 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 9267 continue; 9268 } 9269 } 9270 } 9271 9272 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 9273 // in a Construct] 9274 // Variables with the predetermined data-sharing attributes may not be 9275 // listed in data-sharing attributes clauses, except for the cases 9276 // listed below. For these exceptions only, listing a predetermined 9277 // variable in a data-sharing attribute clause is allowed and overrides 9278 // the variable's predetermined data-sharing attributes. 9279 // OpenMP [2.14.3.6, Restrictions, p.3] 9280 // Any number of reduction clauses can be specified on the directive, 9281 // but a list item can appear only once in the reduction clauses for that 9282 // directive. 9283 DSAStackTy::DSAVarData DVar; 9284 DVar = Stack->getTopDSA(D, false); 9285 if (DVar.CKind == OMPC_reduction) { 9286 S.Diag(ELoc, diag::err_omp_once_referenced) 9287 << getOpenMPClauseName(ClauseKind); 9288 if (DVar.RefExpr) 9289 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 9290 continue; 9291 } else if (DVar.CKind != OMPC_unknown) { 9292 S.Diag(ELoc, diag::err_omp_wrong_dsa) 9293 << getOpenMPClauseName(DVar.CKind) 9294 << getOpenMPClauseName(OMPC_reduction); 9295 ReportOriginalDSA(S, Stack, D, DVar); 9296 continue; 9297 } 9298 9299 // OpenMP [2.14.3.6, Restrictions, p.1] 9300 // A list item that appears in a reduction clause of a worksharing 9301 // construct must be shared in the parallel regions to which any of the 9302 // worksharing regions arising from the worksharing construct bind. 9303 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 9304 if (isOpenMPWorksharingDirective(CurrDir) && 9305 !isOpenMPParallelDirective(CurrDir) && 9306 !isOpenMPTeamsDirective(CurrDir)) { 9307 DVar = Stack->getImplicitDSA(D, true); 9308 if (DVar.CKind != OMPC_shared) { 9309 S.Diag(ELoc, diag::err_omp_required_access) 9310 << getOpenMPClauseName(OMPC_reduction) 9311 << getOpenMPClauseName(OMPC_shared); 9312 ReportOriginalDSA(S, Stack, D, DVar); 9313 continue; 9314 } 9315 } 9316 9317 // Try to find 'declare reduction' corresponding construct before using 9318 // builtin/overloaded operators. 9319 CXXCastPath BasePath; 9320 ExprResult DeclareReductionRef = buildDeclareReductionRef( 9321 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 9322 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 9323 if (DeclareReductionRef.isInvalid()) 9324 continue; 9325 if (S.CurContext->isDependentContext() && 9326 (DeclareReductionRef.isUnset() || 9327 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 9328 RD.push(RefExpr, DeclareReductionRef.get()); 9329 continue; 9330 } 9331 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 9332 // Not allowed reduction identifier is found. 9333 S.Diag(ReductionId.getLocStart(), 9334 diag::err_omp_unknown_reduction_identifier) 9335 << Type << ReductionIdRange; 9336 continue; 9337 } 9338 9339 // OpenMP [2.14.3.6, reduction clause, Restrictions] 9340 // The type of a list item that appears in a reduction clause must be valid 9341 // for the reduction-identifier. For a max or min reduction in C, the type 9342 // of the list item must be an allowed arithmetic data type: char, int, 9343 // float, double, or _Bool, possibly modified with long, short, signed, or 9344 // unsigned. For a max or min reduction in C++, the type of the list item 9345 // must be an allowed arithmetic data type: char, wchar_t, int, float, 9346 // double, or bool, possibly modified with long, short, signed, or unsigned. 9347 if (DeclareReductionRef.isUnset()) { 9348 if ((BOK == BO_GT || BOK == BO_LT) && 9349 !(Type->isScalarType() || 9350 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 9351 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 9352 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 9353 if (!ASE && !OASE) { 9354 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 9355 VarDecl::DeclarationOnly; 9356 S.Diag(D->getLocation(), 9357 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9358 << D; 9359 } 9360 continue; 9361 } 9362 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 9363 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 9364 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 9365 << getOpenMPClauseName(ClauseKind); 9366 if (!ASE && !OASE) { 9367 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 9368 VarDecl::DeclarationOnly; 9369 S.Diag(D->getLocation(), 9370 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9371 << D; 9372 } 9373 continue; 9374 } 9375 } 9376 9377 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 9378 auto *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 9379 D->hasAttrs() ? &D->getAttrs() : nullptr); 9380 auto *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 9381 D->hasAttrs() ? &D->getAttrs() : nullptr); 9382 auto PrivateTy = Type; 9383 if (OASE || 9384 (!ASE && 9385 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 9386 // For arrays/array sections only: 9387 // Create pseudo array type for private copy. The size for this array will 9388 // be generated during codegen. 9389 // For array subscripts or single variables Private Ty is the same as Type 9390 // (type of the variable or single array element). 9391 PrivateTy = Context.getVariableArrayType( 9392 Type, 9393 new (Context) OpaqueValueExpr(SourceLocation(), Context.getSizeType(), 9394 VK_RValue), 9395 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 9396 } else if (!ASE && !OASE && 9397 Context.getAsArrayType(D->getType().getNonReferenceType())) 9398 PrivateTy = D->getType().getNonReferenceType(); 9399 // Private copy. 9400 auto *PrivateVD = buildVarDecl(S, ELoc, PrivateTy, D->getName(), 9401 D->hasAttrs() ? &D->getAttrs() : nullptr); 9402 // Add initializer for private variable. 9403 Expr *Init = nullptr; 9404 auto *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 9405 auto *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 9406 if (DeclareReductionRef.isUsable()) { 9407 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 9408 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 9409 if (DRD->getInitializer()) { 9410 Init = DRDRef; 9411 RHSVD->setInit(DRDRef); 9412 RHSVD->setInitStyle(VarDecl::CallInit); 9413 } 9414 } else { 9415 switch (BOK) { 9416 case BO_Add: 9417 case BO_Xor: 9418 case BO_Or: 9419 case BO_LOr: 9420 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 9421 if (Type->isScalarType() || Type->isAnyComplexType()) 9422 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 9423 break; 9424 case BO_Mul: 9425 case BO_LAnd: 9426 if (Type->isScalarType() || Type->isAnyComplexType()) { 9427 // '*' and '&&' reduction ops - initializer is '1'. 9428 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 9429 } 9430 break; 9431 case BO_And: { 9432 // '&' reduction op - initializer is '~0'. 9433 QualType OrigType = Type; 9434 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 9435 Type = ComplexTy->getElementType(); 9436 if (Type->isRealFloatingType()) { 9437 llvm::APFloat InitValue = 9438 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 9439 /*isIEEE=*/true); 9440 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 9441 Type, ELoc); 9442 } else if (Type->isScalarType()) { 9443 auto Size = Context.getTypeSize(Type); 9444 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 9445 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 9446 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 9447 } 9448 if (Init && OrigType->isAnyComplexType()) { 9449 // Init = 0xFFFF + 0xFFFFi; 9450 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 9451 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 9452 } 9453 Type = OrigType; 9454 break; 9455 } 9456 case BO_LT: 9457 case BO_GT: { 9458 // 'min' reduction op - initializer is 'Largest representable number in 9459 // the reduction list item type'. 9460 // 'max' reduction op - initializer is 'Least representable number in 9461 // the reduction list item type'. 9462 if (Type->isIntegerType() || Type->isPointerType()) { 9463 bool IsSigned = Type->hasSignedIntegerRepresentation(); 9464 auto Size = Context.getTypeSize(Type); 9465 QualType IntTy = 9466 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 9467 llvm::APInt InitValue = 9468 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 9469 : llvm::APInt::getMinValue(Size) 9470 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 9471 : llvm::APInt::getMaxValue(Size); 9472 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 9473 if (Type->isPointerType()) { 9474 // Cast to pointer type. 9475 auto CastExpr = S.BuildCStyleCastExpr( 9476 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), 9477 SourceLocation(), Init); 9478 if (CastExpr.isInvalid()) 9479 continue; 9480 Init = CastExpr.get(); 9481 } 9482 } else if (Type->isRealFloatingType()) { 9483 llvm::APFloat InitValue = llvm::APFloat::getLargest( 9484 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 9485 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 9486 Type, ELoc); 9487 } 9488 break; 9489 } 9490 case BO_PtrMemD: 9491 case BO_PtrMemI: 9492 case BO_MulAssign: 9493 case BO_Div: 9494 case BO_Rem: 9495 case BO_Sub: 9496 case BO_Shl: 9497 case BO_Shr: 9498 case BO_LE: 9499 case BO_GE: 9500 case BO_EQ: 9501 case BO_NE: 9502 case BO_AndAssign: 9503 case BO_XorAssign: 9504 case BO_OrAssign: 9505 case BO_Assign: 9506 case BO_AddAssign: 9507 case BO_SubAssign: 9508 case BO_DivAssign: 9509 case BO_RemAssign: 9510 case BO_ShlAssign: 9511 case BO_ShrAssign: 9512 case BO_Comma: 9513 llvm_unreachable("Unexpected reduction operation"); 9514 } 9515 } 9516 if (Init && DeclareReductionRef.isUnset()) 9517 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 9518 else if (!Init) 9519 S.ActOnUninitializedDecl(RHSVD); 9520 if (RHSVD->isInvalidDecl()) 9521 continue; 9522 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 9523 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 9524 << Type << ReductionIdRange; 9525 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 9526 VarDecl::DeclarationOnly; 9527 S.Diag(D->getLocation(), 9528 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9529 << D; 9530 continue; 9531 } 9532 // Store initializer for single element in private copy. Will be used during 9533 // codegen. 9534 PrivateVD->setInit(RHSVD->getInit()); 9535 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 9536 auto *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 9537 ExprResult ReductionOp; 9538 if (DeclareReductionRef.isUsable()) { 9539 QualType RedTy = DeclareReductionRef.get()->getType(); 9540 QualType PtrRedTy = Context.getPointerType(RedTy); 9541 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 9542 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 9543 if (!BasePath.empty()) { 9544 LHS = S.DefaultLvalueConversion(LHS.get()); 9545 RHS = S.DefaultLvalueConversion(RHS.get()); 9546 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 9547 CK_UncheckedDerivedToBase, LHS.get(), 9548 &BasePath, LHS.get()->getValueKind()); 9549 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 9550 CK_UncheckedDerivedToBase, RHS.get(), 9551 &BasePath, RHS.get()->getValueKind()); 9552 } 9553 FunctionProtoType::ExtProtoInfo EPI; 9554 QualType Params[] = {PtrRedTy, PtrRedTy}; 9555 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 9556 auto *OVE = new (Context) OpaqueValueExpr( 9557 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 9558 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 9559 Expr *Args[] = {LHS.get(), RHS.get()}; 9560 ReductionOp = new (Context) 9561 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 9562 } else { 9563 ReductionOp = S.BuildBinOp( 9564 Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE); 9565 if (ReductionOp.isUsable()) { 9566 if (BOK != BO_LT && BOK != BO_GT) { 9567 ReductionOp = 9568 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(), 9569 BO_Assign, LHSDRE, ReductionOp.get()); 9570 } else { 9571 auto *ConditionalOp = new (Context) ConditionalOperator( 9572 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), 9573 RHSDRE, Type, VK_LValue, OK_Ordinary); 9574 ReductionOp = 9575 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(), 9576 BO_Assign, LHSDRE, ConditionalOp); 9577 } 9578 if (ReductionOp.isUsable()) 9579 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get()); 9580 } 9581 if (!ReductionOp.isUsable()) 9582 continue; 9583 } 9584 9585 // OpenMP [2.15.4.6, Restrictions, p.2] 9586 // A list item that appears in an in_reduction clause of a task construct 9587 // must appear in a task_reduction clause of a construct associated with a 9588 // taskgroup region that includes the participating task in its taskgroup 9589 // set. The construct associated with the innermost region that meets this 9590 // condition must specify the same reduction-identifier as the in_reduction 9591 // clause. 9592 if (ClauseKind == OMPC_in_reduction) { 9593 SourceRange ParentSR; 9594 BinaryOperatorKind ParentBOK; 9595 const Expr *ParentReductionOp; 9596 DSAStackTy::DSAVarData ParentBOKDSA = 9597 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK); 9598 DSAStackTy::DSAVarData ParentReductionOpDSA = 9599 Stack->getTopMostTaskgroupReductionData(D, ParentSR, 9600 ParentReductionOp); 9601 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 9602 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 9603 if (!IsParentBOK && !IsParentReductionOp) { 9604 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 9605 continue; 9606 } 9607 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 9608 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 9609 IsParentReductionOp) { 9610 bool EmitError = true; 9611 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 9612 llvm::FoldingSetNodeID RedId, ParentRedId; 9613 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 9614 DeclareReductionRef.get()->Profile(RedId, Context, 9615 /*Canonical=*/true); 9616 EmitError = RedId != ParentRedId; 9617 } 9618 if (EmitError) { 9619 S.Diag(ReductionId.getLocStart(), 9620 diag::err_omp_reduction_identifier_mismatch) 9621 << ReductionIdRange << RefExpr->getSourceRange(); 9622 S.Diag(ParentSR.getBegin(), 9623 diag::note_omp_previous_reduction_identifier) 9624 << ParentSR 9625 << (IsParentBOK ? ParentBOKDSA.RefExpr 9626 : ParentReductionOpDSA.RefExpr) 9627 ->getSourceRange(); 9628 continue; 9629 } 9630 } 9631 } 9632 9633 DeclRefExpr *Ref = nullptr; 9634 Expr *VarsExpr = RefExpr->IgnoreParens(); 9635 if (!VD && !S.CurContext->isDependentContext()) { 9636 if (ASE || OASE) { 9637 TransformExprToCaptures RebuildToCapture(S, D); 9638 VarsExpr = 9639 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 9640 Ref = RebuildToCapture.getCapturedExpr(); 9641 } else { 9642 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 9643 } 9644 if (!S.IsOpenMPCapturedDecl(D)) { 9645 RD.ExprCaptures.emplace_back(Ref->getDecl()); 9646 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 9647 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 9648 if (!RefRes.isUsable()) 9649 continue; 9650 ExprResult PostUpdateRes = 9651 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 9652 RefRes.get()); 9653 if (!PostUpdateRes.isUsable()) 9654 continue; 9655 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 9656 Stack->getCurrentDirective() == OMPD_taskgroup) { 9657 S.Diag(RefExpr->getExprLoc(), 9658 diag::err_omp_reduction_non_addressable_expression) 9659 << RefExpr->getSourceRange(); 9660 continue; 9661 } 9662 RD.ExprPostUpdates.emplace_back( 9663 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 9664 } 9665 } 9666 } 9667 // All reduction items are still marked as reduction (to do not increase 9668 // code base size). 9669 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 9670 if (CurrDir == OMPD_taskgroup) { 9671 if (DeclareReductionRef.isUsable()) 9672 Stack->addTaskgroupReductionData(D, ReductionIdRange, 9673 DeclareReductionRef.get()); 9674 else 9675 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 9676 } 9677 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get()); 9678 } 9679 return RD.Vars.empty(); 9680 } 9681 9682 OMPClause *Sema::ActOnOpenMPReductionClause( 9683 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 9684 SourceLocation ColonLoc, SourceLocation EndLoc, 9685 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 9686 ArrayRef<Expr *> UnresolvedReductions) { 9687 ReductionData RD(VarList.size()); 9688 9689 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 9690 StartLoc, LParenLoc, ColonLoc, EndLoc, 9691 ReductionIdScopeSpec, ReductionId, 9692 UnresolvedReductions, RD)) 9693 return nullptr; 9694 9695 return OMPReductionClause::Create( 9696 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 9697 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 9698 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 9699 buildPreInits(Context, RD.ExprCaptures), 9700 buildPostUpdate(*this, RD.ExprPostUpdates)); 9701 } 9702 9703 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 9704 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 9705 SourceLocation ColonLoc, SourceLocation EndLoc, 9706 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 9707 ArrayRef<Expr *> UnresolvedReductions) { 9708 ReductionData RD(VarList.size()); 9709 9710 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, 9711 VarList, StartLoc, LParenLoc, ColonLoc, 9712 EndLoc, ReductionIdScopeSpec, ReductionId, 9713 UnresolvedReductions, RD)) 9714 return nullptr; 9715 9716 return OMPTaskReductionClause::Create( 9717 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 9718 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 9719 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 9720 buildPreInits(Context, RD.ExprCaptures), 9721 buildPostUpdate(*this, RD.ExprPostUpdates)); 9722 } 9723 9724 OMPClause *Sema::ActOnOpenMPInReductionClause( 9725 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 9726 SourceLocation ColonLoc, SourceLocation EndLoc, 9727 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 9728 ArrayRef<Expr *> UnresolvedReductions) { 9729 ReductionData RD(VarList.size()); 9730 9731 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 9732 StartLoc, LParenLoc, ColonLoc, EndLoc, 9733 ReductionIdScopeSpec, ReductionId, 9734 UnresolvedReductions, RD)) 9735 return nullptr; 9736 9737 return OMPInReductionClause::Create( 9738 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 9739 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 9740 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 9741 buildPreInits(Context, RD.ExprCaptures), 9742 buildPostUpdate(*this, RD.ExprPostUpdates)); 9743 } 9744 9745 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 9746 SourceLocation LinLoc) { 9747 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 9748 LinKind == OMPC_LINEAR_unknown) { 9749 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 9750 return true; 9751 } 9752 return false; 9753 } 9754 9755 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc, 9756 OpenMPLinearClauseKind LinKind, 9757 QualType Type) { 9758 auto *VD = dyn_cast_or_null<VarDecl>(D); 9759 // A variable must not have an incomplete type or a reference type. 9760 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 9761 return true; 9762 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 9763 !Type->isReferenceType()) { 9764 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 9765 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 9766 return true; 9767 } 9768 Type = Type.getNonReferenceType(); 9769 9770 // A list item must not be const-qualified. 9771 if (Type.isConstant(Context)) { 9772 Diag(ELoc, diag::err_omp_const_variable) 9773 << getOpenMPClauseName(OMPC_linear); 9774 if (D) { 9775 bool IsDecl = 9776 !VD || 9777 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9778 Diag(D->getLocation(), 9779 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9780 << D; 9781 } 9782 return true; 9783 } 9784 9785 // A list item must be of integral or pointer type. 9786 Type = Type.getUnqualifiedType().getCanonicalType(); 9787 const auto *Ty = Type.getTypePtrOrNull(); 9788 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 9789 !Ty->isPointerType())) { 9790 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 9791 if (D) { 9792 bool IsDecl = 9793 !VD || 9794 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9795 Diag(D->getLocation(), 9796 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9797 << D; 9798 } 9799 return true; 9800 } 9801 return false; 9802 } 9803 9804 OMPClause *Sema::ActOnOpenMPLinearClause( 9805 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 9806 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 9807 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 9808 SmallVector<Expr *, 8> Vars; 9809 SmallVector<Expr *, 8> Privates; 9810 SmallVector<Expr *, 8> Inits; 9811 SmallVector<Decl *, 4> ExprCaptures; 9812 SmallVector<Expr *, 4> ExprPostUpdates; 9813 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 9814 LinKind = OMPC_LINEAR_val; 9815 for (auto &RefExpr : VarList) { 9816 assert(RefExpr && "NULL expr in OpenMP linear clause."); 9817 SourceLocation ELoc; 9818 SourceRange ERange; 9819 Expr *SimpleRefExpr = RefExpr; 9820 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 9821 /*AllowArraySection=*/false); 9822 if (Res.second) { 9823 // It will be analyzed later. 9824 Vars.push_back(RefExpr); 9825 Privates.push_back(nullptr); 9826 Inits.push_back(nullptr); 9827 } 9828 ValueDecl *D = Res.first; 9829 if (!D) 9830 continue; 9831 9832 QualType Type = D->getType(); 9833 auto *VD = dyn_cast<VarDecl>(D); 9834 9835 // OpenMP [2.14.3.7, linear clause] 9836 // A list-item cannot appear in more than one linear clause. 9837 // A list-item that appears in a linear clause cannot appear in any 9838 // other data-sharing attribute clause. 9839 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 9840 if (DVar.RefExpr) { 9841 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 9842 << getOpenMPClauseName(OMPC_linear); 9843 ReportOriginalDSA(*this, DSAStack, D, DVar); 9844 continue; 9845 } 9846 9847 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 9848 continue; 9849 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 9850 9851 // Build private copy of original var. 9852 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(), 9853 D->hasAttrs() ? &D->getAttrs() : nullptr); 9854 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 9855 // Build var to save initial value. 9856 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 9857 Expr *InitExpr; 9858 DeclRefExpr *Ref = nullptr; 9859 if (!VD && !CurContext->isDependentContext()) { 9860 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9861 if (!IsOpenMPCapturedDecl(D)) { 9862 ExprCaptures.push_back(Ref->getDecl()); 9863 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 9864 ExprResult RefRes = DefaultLvalueConversion(Ref); 9865 if (!RefRes.isUsable()) 9866 continue; 9867 ExprResult PostUpdateRes = 9868 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 9869 SimpleRefExpr, RefRes.get()); 9870 if (!PostUpdateRes.isUsable()) 9871 continue; 9872 ExprPostUpdates.push_back( 9873 IgnoredValueConversions(PostUpdateRes.get()).get()); 9874 } 9875 } 9876 } 9877 if (LinKind == OMPC_LINEAR_uval) 9878 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 9879 else 9880 InitExpr = VD ? SimpleRefExpr : Ref; 9881 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 9882 /*DirectInit=*/false); 9883 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 9884 9885 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 9886 Vars.push_back((VD || CurContext->isDependentContext()) 9887 ? RefExpr->IgnoreParens() 9888 : Ref); 9889 Privates.push_back(PrivateRef); 9890 Inits.push_back(InitRef); 9891 } 9892 9893 if (Vars.empty()) 9894 return nullptr; 9895 9896 Expr *StepExpr = Step; 9897 Expr *CalcStepExpr = nullptr; 9898 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 9899 !Step->isInstantiationDependent() && 9900 !Step->containsUnexpandedParameterPack()) { 9901 SourceLocation StepLoc = Step->getLocStart(); 9902 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 9903 if (Val.isInvalid()) 9904 return nullptr; 9905 StepExpr = Val.get(); 9906 9907 // Build var to save the step value. 9908 VarDecl *SaveVar = 9909 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 9910 ExprResult SaveRef = 9911 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 9912 ExprResult CalcStep = 9913 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 9914 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 9915 9916 // Warn about zero linear step (it would be probably better specified as 9917 // making corresponding variables 'const'). 9918 llvm::APSInt Result; 9919 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 9920 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 9921 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 9922 << (Vars.size() > 1); 9923 if (!IsConstant && CalcStep.isUsable()) { 9924 // Calculate the step beforehand instead of doing this on each iteration. 9925 // (This is not used if the number of iterations may be kfold-ed). 9926 CalcStepExpr = CalcStep.get(); 9927 } 9928 } 9929 9930 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 9931 ColonLoc, EndLoc, Vars, Privates, Inits, 9932 StepExpr, CalcStepExpr, 9933 buildPreInits(Context, ExprCaptures), 9934 buildPostUpdate(*this, ExprPostUpdates)); 9935 } 9936 9937 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 9938 Expr *NumIterations, Sema &SemaRef, 9939 Scope *S, DSAStackTy *Stack) { 9940 // Walk the vars and build update/final expressions for the CodeGen. 9941 SmallVector<Expr *, 8> Updates; 9942 SmallVector<Expr *, 8> Finals; 9943 Expr *Step = Clause.getStep(); 9944 Expr *CalcStep = Clause.getCalcStep(); 9945 // OpenMP [2.14.3.7, linear clause] 9946 // If linear-step is not specified it is assumed to be 1. 9947 if (Step == nullptr) 9948 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9949 else if (CalcStep) { 9950 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 9951 } 9952 bool HasErrors = false; 9953 auto CurInit = Clause.inits().begin(); 9954 auto CurPrivate = Clause.privates().begin(); 9955 auto LinKind = Clause.getModifier(); 9956 for (auto &RefExpr : Clause.varlists()) { 9957 SourceLocation ELoc; 9958 SourceRange ERange; 9959 Expr *SimpleRefExpr = RefExpr; 9960 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, 9961 /*AllowArraySection=*/false); 9962 ValueDecl *D = Res.first; 9963 if (Res.second || !D) { 9964 Updates.push_back(nullptr); 9965 Finals.push_back(nullptr); 9966 HasErrors = true; 9967 continue; 9968 } 9969 auto &&Info = Stack->isLoopControlVariable(D); 9970 Expr *InitExpr = *CurInit; 9971 9972 // Build privatized reference to the current linear var. 9973 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 9974 Expr *CapturedRef; 9975 if (LinKind == OMPC_LINEAR_uval) 9976 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 9977 else 9978 CapturedRef = 9979 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 9980 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 9981 /*RefersToCapture=*/true); 9982 9983 // Build update: Var = InitExpr + IV * Step 9984 ExprResult Update; 9985 if (!Info.first) { 9986 Update = 9987 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 9988 InitExpr, IV, Step, /* Subtract */ false); 9989 } else 9990 Update = *CurPrivate; 9991 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), 9992 /*DiscardedValue=*/true); 9993 9994 // Build final: Var = InitExpr + NumIterations * Step 9995 ExprResult Final; 9996 if (!Info.first) { 9997 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 9998 InitExpr, NumIterations, Step, 9999 /* Subtract */ false); 10000 } else 10001 Final = *CurPrivate; 10002 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), 10003 /*DiscardedValue=*/true); 10004 10005 if (!Update.isUsable() || !Final.isUsable()) { 10006 Updates.push_back(nullptr); 10007 Finals.push_back(nullptr); 10008 HasErrors = true; 10009 } else { 10010 Updates.push_back(Update.get()); 10011 Finals.push_back(Final.get()); 10012 } 10013 ++CurInit; 10014 ++CurPrivate; 10015 } 10016 Clause.setUpdates(Updates); 10017 Clause.setFinals(Finals); 10018 return HasErrors; 10019 } 10020 10021 OMPClause *Sema::ActOnOpenMPAlignedClause( 10022 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 10023 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 10024 10025 SmallVector<Expr *, 8> Vars; 10026 for (auto &RefExpr : VarList) { 10027 assert(RefExpr && "NULL expr in OpenMP linear clause."); 10028 SourceLocation ELoc; 10029 SourceRange ERange; 10030 Expr *SimpleRefExpr = RefExpr; 10031 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 10032 /*AllowArraySection=*/false); 10033 if (Res.second) { 10034 // It will be analyzed later. 10035 Vars.push_back(RefExpr); 10036 } 10037 ValueDecl *D = Res.first; 10038 if (!D) 10039 continue; 10040 10041 QualType QType = D->getType(); 10042 auto *VD = dyn_cast<VarDecl>(D); 10043 10044 // OpenMP [2.8.1, simd construct, Restrictions] 10045 // The type of list items appearing in the aligned clause must be 10046 // array, pointer, reference to array, or reference to pointer. 10047 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 10048 const Type *Ty = QType.getTypePtrOrNull(); 10049 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 10050 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 10051 << QType << getLangOpts().CPlusPlus << ERange; 10052 bool IsDecl = 10053 !VD || 10054 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10055 Diag(D->getLocation(), 10056 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10057 << D; 10058 continue; 10059 } 10060 10061 // OpenMP [2.8.1, simd construct, Restrictions] 10062 // A list-item cannot appear in more than one aligned clause. 10063 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 10064 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 10065 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 10066 << getOpenMPClauseName(OMPC_aligned); 10067 continue; 10068 } 10069 10070 DeclRefExpr *Ref = nullptr; 10071 if (!VD && IsOpenMPCapturedDecl(D)) 10072 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10073 Vars.push_back(DefaultFunctionArrayConversion( 10074 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 10075 .get()); 10076 } 10077 10078 // OpenMP [2.8.1, simd construct, Description] 10079 // The parameter of the aligned clause, alignment, must be a constant 10080 // positive integer expression. 10081 // If no optional parameter is specified, implementation-defined default 10082 // alignments for SIMD instructions on the target platforms are assumed. 10083 if (Alignment != nullptr) { 10084 ExprResult AlignResult = 10085 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 10086 if (AlignResult.isInvalid()) 10087 return nullptr; 10088 Alignment = AlignResult.get(); 10089 } 10090 if (Vars.empty()) 10091 return nullptr; 10092 10093 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 10094 EndLoc, Vars, Alignment); 10095 } 10096 10097 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 10098 SourceLocation StartLoc, 10099 SourceLocation LParenLoc, 10100 SourceLocation EndLoc) { 10101 SmallVector<Expr *, 8> Vars; 10102 SmallVector<Expr *, 8> SrcExprs; 10103 SmallVector<Expr *, 8> DstExprs; 10104 SmallVector<Expr *, 8> AssignmentOps; 10105 for (auto &RefExpr : VarList) { 10106 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 10107 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 10108 // It will be analyzed later. 10109 Vars.push_back(RefExpr); 10110 SrcExprs.push_back(nullptr); 10111 DstExprs.push_back(nullptr); 10112 AssignmentOps.push_back(nullptr); 10113 continue; 10114 } 10115 10116 SourceLocation ELoc = RefExpr->getExprLoc(); 10117 // OpenMP [2.1, C/C++] 10118 // A list item is a variable name. 10119 // OpenMP [2.14.4.1, Restrictions, p.1] 10120 // A list item that appears in a copyin clause must be threadprivate. 10121 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 10122 if (!DE || !isa<VarDecl>(DE->getDecl())) { 10123 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 10124 << 0 << RefExpr->getSourceRange(); 10125 continue; 10126 } 10127 10128 Decl *D = DE->getDecl(); 10129 VarDecl *VD = cast<VarDecl>(D); 10130 10131 QualType Type = VD->getType(); 10132 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 10133 // It will be analyzed later. 10134 Vars.push_back(DE); 10135 SrcExprs.push_back(nullptr); 10136 DstExprs.push_back(nullptr); 10137 AssignmentOps.push_back(nullptr); 10138 continue; 10139 } 10140 10141 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 10142 // A list item that appears in a copyin clause must be threadprivate. 10143 if (!DSAStack->isThreadPrivate(VD)) { 10144 Diag(ELoc, diag::err_omp_required_access) 10145 << getOpenMPClauseName(OMPC_copyin) 10146 << getOpenMPDirectiveName(OMPD_threadprivate); 10147 continue; 10148 } 10149 10150 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 10151 // A variable of class type (or array thereof) that appears in a 10152 // copyin clause requires an accessible, unambiguous copy assignment 10153 // operator for the class type. 10154 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 10155 auto *SrcVD = 10156 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), 10157 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 10158 auto *PseudoSrcExpr = buildDeclRefExpr( 10159 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 10160 auto *DstVD = 10161 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", 10162 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 10163 auto *PseudoDstExpr = 10164 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 10165 // For arrays generate assignment operation for single element and replace 10166 // it by the original array element in CodeGen. 10167 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 10168 PseudoDstExpr, PseudoSrcExpr); 10169 if (AssignmentOp.isInvalid()) 10170 continue; 10171 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 10172 /*DiscardedValue=*/true); 10173 if (AssignmentOp.isInvalid()) 10174 continue; 10175 10176 DSAStack->addDSA(VD, DE, OMPC_copyin); 10177 Vars.push_back(DE); 10178 SrcExprs.push_back(PseudoSrcExpr); 10179 DstExprs.push_back(PseudoDstExpr); 10180 AssignmentOps.push_back(AssignmentOp.get()); 10181 } 10182 10183 if (Vars.empty()) 10184 return nullptr; 10185 10186 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 10187 SrcExprs, DstExprs, AssignmentOps); 10188 } 10189 10190 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 10191 SourceLocation StartLoc, 10192 SourceLocation LParenLoc, 10193 SourceLocation EndLoc) { 10194 SmallVector<Expr *, 8> Vars; 10195 SmallVector<Expr *, 8> SrcExprs; 10196 SmallVector<Expr *, 8> DstExprs; 10197 SmallVector<Expr *, 8> AssignmentOps; 10198 for (auto &RefExpr : VarList) { 10199 assert(RefExpr && "NULL expr in OpenMP linear clause."); 10200 SourceLocation ELoc; 10201 SourceRange ERange; 10202 Expr *SimpleRefExpr = RefExpr; 10203 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 10204 /*AllowArraySection=*/false); 10205 if (Res.second) { 10206 // It will be analyzed later. 10207 Vars.push_back(RefExpr); 10208 SrcExprs.push_back(nullptr); 10209 DstExprs.push_back(nullptr); 10210 AssignmentOps.push_back(nullptr); 10211 } 10212 ValueDecl *D = Res.first; 10213 if (!D) 10214 continue; 10215 10216 QualType Type = D->getType(); 10217 auto *VD = dyn_cast<VarDecl>(D); 10218 10219 // OpenMP [2.14.4.2, Restrictions, p.2] 10220 // A list item that appears in a copyprivate clause may not appear in a 10221 // private or firstprivate clause on the single construct. 10222 if (!VD || !DSAStack->isThreadPrivate(VD)) { 10223 auto DVar = DSAStack->getTopDSA(D, false); 10224 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 10225 DVar.RefExpr) { 10226 Diag(ELoc, diag::err_omp_wrong_dsa) 10227 << getOpenMPClauseName(DVar.CKind) 10228 << getOpenMPClauseName(OMPC_copyprivate); 10229 ReportOriginalDSA(*this, DSAStack, D, DVar); 10230 continue; 10231 } 10232 10233 // OpenMP [2.11.4.2, Restrictions, p.1] 10234 // All list items that appear in a copyprivate clause must be either 10235 // threadprivate or private in the enclosing context. 10236 if (DVar.CKind == OMPC_unknown) { 10237 DVar = DSAStack->getImplicitDSA(D, false); 10238 if (DVar.CKind == OMPC_shared) { 10239 Diag(ELoc, diag::err_omp_required_access) 10240 << getOpenMPClauseName(OMPC_copyprivate) 10241 << "threadprivate or private in the enclosing context"; 10242 ReportOriginalDSA(*this, DSAStack, D, DVar); 10243 continue; 10244 } 10245 } 10246 } 10247 10248 // Variably modified types are not supported. 10249 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 10250 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10251 << getOpenMPClauseName(OMPC_copyprivate) << Type 10252 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10253 bool IsDecl = 10254 !VD || 10255 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10256 Diag(D->getLocation(), 10257 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10258 << D; 10259 continue; 10260 } 10261 10262 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 10263 // A variable of class type (or array thereof) that appears in a 10264 // copyin clause requires an accessible, unambiguous copy assignment 10265 // operator for the class type. 10266 Type = Context.getBaseElementType(Type.getNonReferenceType()) 10267 .getUnqualifiedType(); 10268 auto *SrcVD = 10269 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src", 10270 D->hasAttrs() ? &D->getAttrs() : nullptr); 10271 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 10272 auto *DstVD = 10273 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst", 10274 D->hasAttrs() ? &D->getAttrs() : nullptr); 10275 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 10276 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 10277 PseudoDstExpr, PseudoSrcExpr); 10278 if (AssignmentOp.isInvalid()) 10279 continue; 10280 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 10281 /*DiscardedValue=*/true); 10282 if (AssignmentOp.isInvalid()) 10283 continue; 10284 10285 // No need to mark vars as copyprivate, they are already threadprivate or 10286 // implicitly private. 10287 assert(VD || IsOpenMPCapturedDecl(D)); 10288 Vars.push_back( 10289 VD ? RefExpr->IgnoreParens() 10290 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 10291 SrcExprs.push_back(PseudoSrcExpr); 10292 DstExprs.push_back(PseudoDstExpr); 10293 AssignmentOps.push_back(AssignmentOp.get()); 10294 } 10295 10296 if (Vars.empty()) 10297 return nullptr; 10298 10299 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10300 Vars, SrcExprs, DstExprs, AssignmentOps); 10301 } 10302 10303 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 10304 SourceLocation StartLoc, 10305 SourceLocation LParenLoc, 10306 SourceLocation EndLoc) { 10307 if (VarList.empty()) 10308 return nullptr; 10309 10310 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 10311 } 10312 10313 OMPClause * 10314 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 10315 SourceLocation DepLoc, SourceLocation ColonLoc, 10316 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 10317 SourceLocation LParenLoc, SourceLocation EndLoc) { 10318 if (DSAStack->getCurrentDirective() == OMPD_ordered && 10319 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 10320 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 10321 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 10322 return nullptr; 10323 } 10324 if (DSAStack->getCurrentDirective() != OMPD_ordered && 10325 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 10326 DepKind == OMPC_DEPEND_sink)) { 10327 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 10328 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 10329 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 10330 /*Last=*/OMPC_DEPEND_unknown, Except) 10331 << getOpenMPClauseName(OMPC_depend); 10332 return nullptr; 10333 } 10334 SmallVector<Expr *, 8> Vars; 10335 DSAStackTy::OperatorOffsetTy OpsOffs; 10336 llvm::APSInt DepCounter(/*BitWidth=*/32); 10337 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 10338 if (DepKind == OMPC_DEPEND_sink) { 10339 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) { 10340 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 10341 TotalDepCount.setIsUnsigned(/*Val=*/true); 10342 } 10343 } 10344 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) || 10345 DSAStack->getParentOrderedRegionParam()) { 10346 for (auto &RefExpr : VarList) { 10347 assert(RefExpr && "NULL expr in OpenMP shared clause."); 10348 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 10349 // It will be analyzed later. 10350 Vars.push_back(RefExpr); 10351 continue; 10352 } 10353 10354 SourceLocation ELoc = RefExpr->getExprLoc(); 10355 auto *SimpleExpr = RefExpr->IgnoreParenCasts(); 10356 if (DepKind == OMPC_DEPEND_sink) { 10357 if (DepCounter >= TotalDepCount) { 10358 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 10359 continue; 10360 } 10361 ++DepCounter; 10362 // OpenMP [2.13.9, Summary] 10363 // depend(dependence-type : vec), where dependence-type is: 10364 // 'sink' and where vec is the iteration vector, which has the form: 10365 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 10366 // where n is the value specified by the ordered clause in the loop 10367 // directive, xi denotes the loop iteration variable of the i-th nested 10368 // loop associated with the loop directive, and di is a constant 10369 // non-negative integer. 10370 if (CurContext->isDependentContext()) { 10371 // It will be analyzed later. 10372 Vars.push_back(RefExpr); 10373 continue; 10374 } 10375 SimpleExpr = SimpleExpr->IgnoreImplicit(); 10376 OverloadedOperatorKind OOK = OO_None; 10377 SourceLocation OOLoc; 10378 Expr *LHS = SimpleExpr; 10379 Expr *RHS = nullptr; 10380 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 10381 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 10382 OOLoc = BO->getOperatorLoc(); 10383 LHS = BO->getLHS()->IgnoreParenImpCasts(); 10384 RHS = BO->getRHS()->IgnoreParenImpCasts(); 10385 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 10386 OOK = OCE->getOperator(); 10387 OOLoc = OCE->getOperatorLoc(); 10388 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 10389 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 10390 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 10391 OOK = MCE->getMethodDecl() 10392 ->getNameInfo() 10393 .getName() 10394 .getCXXOverloadedOperator(); 10395 OOLoc = MCE->getCallee()->getExprLoc(); 10396 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 10397 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 10398 } 10399 SourceLocation ELoc; 10400 SourceRange ERange; 10401 auto Res = getPrivateItem(*this, LHS, ELoc, ERange, 10402 /*AllowArraySection=*/false); 10403 if (Res.second) { 10404 // It will be analyzed later. 10405 Vars.push_back(RefExpr); 10406 } 10407 ValueDecl *D = Res.first; 10408 if (!D) 10409 continue; 10410 10411 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 10412 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 10413 continue; 10414 } 10415 if (RHS) { 10416 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 10417 RHS, OMPC_depend, /*StrictlyPositive=*/false); 10418 if (RHSRes.isInvalid()) 10419 continue; 10420 } 10421 if (!CurContext->isDependentContext() && 10422 DSAStack->getParentOrderedRegionParam() && 10423 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 10424 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 10425 << DSAStack->getParentLoopControlVariable( 10426 DepCounter.getZExtValue()); 10427 continue; 10428 } 10429 OpsOffs.push_back({RHS, OOK}); 10430 } else { 10431 // OpenMP [2.11.1.1, Restrictions, p.3] 10432 // A variable that is part of another variable (such as a field of a 10433 // structure) but is not an array element or an array section cannot 10434 // appear in a depend clause. 10435 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 10436 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 10437 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 10438 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 10439 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) || 10440 (ASE && 10441 !ASE->getBase() 10442 ->getType() 10443 .getNonReferenceType() 10444 ->isPointerType() && 10445 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 10446 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item) 10447 << 0 << RefExpr->getSourceRange(); 10448 continue; 10449 } 10450 } 10451 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 10452 } 10453 10454 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 10455 TotalDepCount > VarList.size() && 10456 DSAStack->getParentOrderedRegionParam()) { 10457 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 10458 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 10459 } 10460 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 10461 Vars.empty()) 10462 return nullptr; 10463 } 10464 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10465 DepKind, DepLoc, ColonLoc, Vars); 10466 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) 10467 DSAStack->addDoacrossDependClause(C, OpsOffs); 10468 return C; 10469 } 10470 10471 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 10472 SourceLocation LParenLoc, 10473 SourceLocation EndLoc) { 10474 Expr *ValExpr = Device; 10475 10476 // OpenMP [2.9.1, Restrictions] 10477 // The device expression must evaluate to a non-negative integer value. 10478 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 10479 /*StrictlyPositive=*/false)) 10480 return nullptr; 10481 10482 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10483 } 10484 10485 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc, 10486 DSAStackTy *Stack, CXXRecordDecl *RD) { 10487 if (!RD || RD->isInvalidDecl()) 10488 return true; 10489 10490 auto QTy = SemaRef.Context.getRecordType(RD); 10491 if (RD->isDynamicClass()) { 10492 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 10493 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target); 10494 return false; 10495 } 10496 auto *DC = RD; 10497 bool IsCorrect = true; 10498 for (auto *I : DC->decls()) { 10499 if (I) { 10500 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) { 10501 if (MD->isStatic()) { 10502 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 10503 SemaRef.Diag(MD->getLocation(), 10504 diag::note_omp_static_member_in_target); 10505 IsCorrect = false; 10506 } 10507 } else if (auto *VD = dyn_cast<VarDecl>(I)) { 10508 if (VD->isStaticDataMember()) { 10509 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 10510 SemaRef.Diag(VD->getLocation(), 10511 diag::note_omp_static_member_in_target); 10512 IsCorrect = false; 10513 } 10514 } 10515 } 10516 } 10517 10518 for (auto &I : RD->bases()) { 10519 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack, 10520 I.getType()->getAsCXXRecordDecl())) 10521 IsCorrect = false; 10522 } 10523 return IsCorrect; 10524 } 10525 10526 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 10527 DSAStackTy *Stack, QualType QTy) { 10528 NamedDecl *ND; 10529 if (QTy->isIncompleteType(&ND)) { 10530 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 10531 return false; 10532 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) { 10533 if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD)) 10534 return false; 10535 } 10536 return true; 10537 } 10538 10539 /// \brief Return true if it can be proven that the provided array expression 10540 /// (array section or array subscript) does NOT specify the whole size of the 10541 /// array whose base type is \a BaseQTy. 10542 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 10543 const Expr *E, 10544 QualType BaseQTy) { 10545 auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 10546 10547 // If this is an array subscript, it refers to the whole size if the size of 10548 // the dimension is constant and equals 1. Also, an array section assumes the 10549 // format of an array subscript if no colon is used. 10550 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 10551 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 10552 return ATy->getSize().getSExtValue() != 1; 10553 // Size can't be evaluated statically. 10554 return false; 10555 } 10556 10557 assert(OASE && "Expecting array section if not an array subscript."); 10558 auto *LowerBound = OASE->getLowerBound(); 10559 auto *Length = OASE->getLength(); 10560 10561 // If there is a lower bound that does not evaluates to zero, we are not 10562 // covering the whole dimension. 10563 if (LowerBound) { 10564 llvm::APSInt ConstLowerBound; 10565 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) 10566 return false; // Can't get the integer value as a constant. 10567 if (ConstLowerBound.getSExtValue()) 10568 return true; 10569 } 10570 10571 // If we don't have a length we covering the whole dimension. 10572 if (!Length) 10573 return false; 10574 10575 // If the base is a pointer, we don't have a way to get the size of the 10576 // pointee. 10577 if (BaseQTy->isPointerType()) 10578 return false; 10579 10580 // We can only check if the length is the same as the size of the dimension 10581 // if we have a constant array. 10582 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 10583 if (!CATy) 10584 return false; 10585 10586 llvm::APSInt ConstLength; 10587 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 10588 return false; // Can't get the integer value as a constant. 10589 10590 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 10591 } 10592 10593 // Return true if it can be proven that the provided array expression (array 10594 // section or array subscript) does NOT specify a single element of the array 10595 // whose base type is \a BaseQTy. 10596 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 10597 const Expr *E, 10598 QualType BaseQTy) { 10599 auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 10600 10601 // An array subscript always refer to a single element. Also, an array section 10602 // assumes the format of an array subscript if no colon is used. 10603 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 10604 return false; 10605 10606 assert(OASE && "Expecting array section if not an array subscript."); 10607 auto *Length = OASE->getLength(); 10608 10609 // If we don't have a length we have to check if the array has unitary size 10610 // for this dimension. Also, we should always expect a length if the base type 10611 // is pointer. 10612 if (!Length) { 10613 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 10614 return ATy->getSize().getSExtValue() != 1; 10615 // We cannot assume anything. 10616 return false; 10617 } 10618 10619 // Check if the length evaluates to 1. 10620 llvm::APSInt ConstLength; 10621 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 10622 return false; // Can't get the integer value as a constant. 10623 10624 return ConstLength.getSExtValue() != 1; 10625 } 10626 10627 // Return the expression of the base of the mappable expression or null if it 10628 // cannot be determined and do all the necessary checks to see if the expression 10629 // is valid as a standalone mappable expression. In the process, record all the 10630 // components of the expression. 10631 static Expr *CheckMapClauseExpressionBase( 10632 Sema &SemaRef, Expr *E, 10633 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 10634 OpenMPClauseKind CKind) { 10635 SourceLocation ELoc = E->getExprLoc(); 10636 SourceRange ERange = E->getSourceRange(); 10637 10638 // The base of elements of list in a map clause have to be either: 10639 // - a reference to variable or field. 10640 // - a member expression. 10641 // - an array expression. 10642 // 10643 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 10644 // reference to 'r'. 10645 // 10646 // If we have: 10647 // 10648 // struct SS { 10649 // Bla S; 10650 // foo() { 10651 // #pragma omp target map (S.Arr[:12]); 10652 // } 10653 // } 10654 // 10655 // We want to retrieve the member expression 'this->S'; 10656 10657 Expr *RelevantExpr = nullptr; 10658 10659 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 10660 // If a list item is an array section, it must specify contiguous storage. 10661 // 10662 // For this restriction it is sufficient that we make sure only references 10663 // to variables or fields and array expressions, and that no array sections 10664 // exist except in the rightmost expression (unless they cover the whole 10665 // dimension of the array). E.g. these would be invalid: 10666 // 10667 // r.ArrS[3:5].Arr[6:7] 10668 // 10669 // r.ArrS[3:5].x 10670 // 10671 // but these would be valid: 10672 // r.ArrS[3].Arr[6:7] 10673 // 10674 // r.ArrS[3].x 10675 10676 bool AllowUnitySizeArraySection = true; 10677 bool AllowWholeSizeArraySection = true; 10678 10679 while (!RelevantExpr) { 10680 E = E->IgnoreParenImpCasts(); 10681 10682 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 10683 if (!isa<VarDecl>(CurE->getDecl())) 10684 break; 10685 10686 RelevantExpr = CurE; 10687 10688 // If we got a reference to a declaration, we should not expect any array 10689 // section before that. 10690 AllowUnitySizeArraySection = false; 10691 AllowWholeSizeArraySection = false; 10692 10693 // Record the component. 10694 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent( 10695 CurE, CurE->getDecl())); 10696 continue; 10697 } 10698 10699 if (auto *CurE = dyn_cast<MemberExpr>(E)) { 10700 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 10701 10702 if (isa<CXXThisExpr>(BaseE)) 10703 // We found a base expression: this->Val. 10704 RelevantExpr = CurE; 10705 else 10706 E = BaseE; 10707 10708 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 10709 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 10710 << CurE->getSourceRange(); 10711 break; 10712 } 10713 10714 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 10715 10716 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 10717 // A bit-field cannot appear in a map clause. 10718 // 10719 if (FD->isBitField()) { 10720 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 10721 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 10722 break; 10723 } 10724 10725 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 10726 // If the type of a list item is a reference to a type T then the type 10727 // will be considered to be T for all purposes of this clause. 10728 QualType CurType = BaseE->getType().getNonReferenceType(); 10729 10730 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 10731 // A list item cannot be a variable that is a member of a structure with 10732 // a union type. 10733 // 10734 if (auto *RT = CurType->getAs<RecordType>()) 10735 if (RT->isUnionType()) { 10736 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 10737 << CurE->getSourceRange(); 10738 break; 10739 } 10740 10741 // If we got a member expression, we should not expect any array section 10742 // before that: 10743 // 10744 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 10745 // If a list item is an element of a structure, only the rightmost symbol 10746 // of the variable reference can be an array section. 10747 // 10748 AllowUnitySizeArraySection = false; 10749 AllowWholeSizeArraySection = false; 10750 10751 // Record the component. 10752 CurComponents.push_back( 10753 OMPClauseMappableExprCommon::MappableComponent(CurE, FD)); 10754 continue; 10755 } 10756 10757 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 10758 E = CurE->getBase()->IgnoreParenImpCasts(); 10759 10760 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 10761 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 10762 << 0 << CurE->getSourceRange(); 10763 break; 10764 } 10765 10766 // If we got an array subscript that express the whole dimension we 10767 // can have any array expressions before. If it only expressing part of 10768 // the dimension, we can only have unitary-size array expressions. 10769 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 10770 E->getType())) 10771 AllowWholeSizeArraySection = false; 10772 10773 // Record the component - we don't have any declaration associated. 10774 CurComponents.push_back( 10775 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); 10776 continue; 10777 } 10778 10779 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 10780 E = CurE->getBase()->IgnoreParenImpCasts(); 10781 10782 auto CurType = 10783 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 10784 10785 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 10786 // If the type of a list item is a reference to a type T then the type 10787 // will be considered to be T for all purposes of this clause. 10788 if (CurType->isReferenceType()) 10789 CurType = CurType->getPointeeType(); 10790 10791 bool IsPointer = CurType->isAnyPointerType(); 10792 10793 if (!IsPointer && !CurType->isArrayType()) { 10794 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 10795 << 0 << CurE->getSourceRange(); 10796 break; 10797 } 10798 10799 bool NotWhole = 10800 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 10801 bool NotUnity = 10802 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 10803 10804 if (AllowWholeSizeArraySection) { 10805 // Any array section is currently allowed. Allowing a whole size array 10806 // section implies allowing a unity array section as well. 10807 // 10808 // If this array section refers to the whole dimension we can still 10809 // accept other array sections before this one, except if the base is a 10810 // pointer. Otherwise, only unitary sections are accepted. 10811 if (NotWhole || IsPointer) 10812 AllowWholeSizeArraySection = false; 10813 } else if (AllowUnitySizeArraySection && NotUnity) { 10814 // A unity or whole array section is not allowed and that is not 10815 // compatible with the properties of the current array section. 10816 SemaRef.Diag( 10817 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 10818 << CurE->getSourceRange(); 10819 break; 10820 } 10821 10822 // Record the component - we don't have any declaration associated. 10823 CurComponents.push_back( 10824 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); 10825 continue; 10826 } 10827 10828 // If nothing else worked, this is not a valid map clause expression. 10829 SemaRef.Diag(ELoc, 10830 diag::err_omp_expected_named_var_member_or_array_expression) 10831 << ERange; 10832 break; 10833 } 10834 10835 return RelevantExpr; 10836 } 10837 10838 // Return true if expression E associated with value VD has conflicts with other 10839 // map information. 10840 static bool CheckMapConflicts( 10841 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E, 10842 bool CurrentRegionOnly, 10843 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 10844 OpenMPClauseKind CKind) { 10845 assert(VD && E); 10846 SourceLocation ELoc = E->getExprLoc(); 10847 SourceRange ERange = E->getSourceRange(); 10848 10849 // In order to easily check the conflicts we need to match each component of 10850 // the expression under test with the components of the expressions that are 10851 // already in the stack. 10852 10853 assert(!CurComponents.empty() && "Map clause expression with no components!"); 10854 assert(CurComponents.back().getAssociatedDeclaration() == VD && 10855 "Map clause expression with unexpected base!"); 10856 10857 // Variables to help detecting enclosing problems in data environment nests. 10858 bool IsEnclosedByDataEnvironmentExpr = false; 10859 const Expr *EnclosingExpr = nullptr; 10860 10861 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 10862 VD, CurrentRegionOnly, 10863 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef 10864 StackComponents, 10865 OpenMPClauseKind) -> bool { 10866 10867 assert(!StackComponents.empty() && 10868 "Map clause expression with no components!"); 10869 assert(StackComponents.back().getAssociatedDeclaration() == VD && 10870 "Map clause expression with unexpected base!"); 10871 10872 // The whole expression in the stack. 10873 auto *RE = StackComponents.front().getAssociatedExpression(); 10874 10875 // Expressions must start from the same base. Here we detect at which 10876 // point both expressions diverge from each other and see if we can 10877 // detect if the memory referred to both expressions is contiguous and 10878 // do not overlap. 10879 auto CI = CurComponents.rbegin(); 10880 auto CE = CurComponents.rend(); 10881 auto SI = StackComponents.rbegin(); 10882 auto SE = StackComponents.rend(); 10883 for (; CI != CE && SI != SE; ++CI, ++SI) { 10884 10885 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 10886 // At most one list item can be an array item derived from a given 10887 // variable in map clauses of the same construct. 10888 if (CurrentRegionOnly && 10889 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 10890 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 10891 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 10892 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 10893 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 10894 diag::err_omp_multiple_array_items_in_map_clause) 10895 << CI->getAssociatedExpression()->getSourceRange(); 10896 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 10897 diag::note_used_here) 10898 << SI->getAssociatedExpression()->getSourceRange(); 10899 return true; 10900 } 10901 10902 // Do both expressions have the same kind? 10903 if (CI->getAssociatedExpression()->getStmtClass() != 10904 SI->getAssociatedExpression()->getStmtClass()) 10905 break; 10906 10907 // Are we dealing with different variables/fields? 10908 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 10909 break; 10910 } 10911 // Check if the extra components of the expressions in the enclosing 10912 // data environment are redundant for the current base declaration. 10913 // If they are, the maps completely overlap, which is legal. 10914 for (; SI != SE; ++SI) { 10915 QualType Type; 10916 if (auto *ASE = 10917 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 10918 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 10919 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>( 10920 SI->getAssociatedExpression())) { 10921 auto *E = OASE->getBase()->IgnoreParenImpCasts(); 10922 Type = 10923 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 10924 } 10925 if (Type.isNull() || Type->isAnyPointerType() || 10926 CheckArrayExpressionDoesNotReferToWholeSize( 10927 SemaRef, SI->getAssociatedExpression(), Type)) 10928 break; 10929 } 10930 10931 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 10932 // List items of map clauses in the same construct must not share 10933 // original storage. 10934 // 10935 // If the expressions are exactly the same or one is a subset of the 10936 // other, it means they are sharing storage. 10937 if (CI == CE && SI == SE) { 10938 if (CurrentRegionOnly) { 10939 if (CKind == OMPC_map) 10940 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 10941 else { 10942 assert(CKind == OMPC_to || CKind == OMPC_from); 10943 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 10944 << ERange; 10945 } 10946 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10947 << RE->getSourceRange(); 10948 return true; 10949 } else { 10950 // If we find the same expression in the enclosing data environment, 10951 // that is legal. 10952 IsEnclosedByDataEnvironmentExpr = true; 10953 return false; 10954 } 10955 } 10956 10957 QualType DerivedType = 10958 std::prev(CI)->getAssociatedDeclaration()->getType(); 10959 SourceLocation DerivedLoc = 10960 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 10961 10962 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 10963 // If the type of a list item is a reference to a type T then the type 10964 // will be considered to be T for all purposes of this clause. 10965 DerivedType = DerivedType.getNonReferenceType(); 10966 10967 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 10968 // A variable for which the type is pointer and an array section 10969 // derived from that variable must not appear as list items of map 10970 // clauses of the same construct. 10971 // 10972 // Also, cover one of the cases in: 10973 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 10974 // If any part of the original storage of a list item has corresponding 10975 // storage in the device data environment, all of the original storage 10976 // must have corresponding storage in the device data environment. 10977 // 10978 if (DerivedType->isAnyPointerType()) { 10979 if (CI == CE || SI == SE) { 10980 SemaRef.Diag( 10981 DerivedLoc, 10982 diag::err_omp_pointer_mapped_along_with_derived_section) 10983 << DerivedLoc; 10984 } else { 10985 assert(CI != CE && SI != SE); 10986 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced) 10987 << DerivedLoc; 10988 } 10989 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10990 << RE->getSourceRange(); 10991 return true; 10992 } 10993 10994 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 10995 // List items of map clauses in the same construct must not share 10996 // original storage. 10997 // 10998 // An expression is a subset of the other. 10999 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 11000 if (CKind == OMPC_map) 11001 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 11002 else { 11003 assert(CKind == OMPC_to || CKind == OMPC_from); 11004 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 11005 << ERange; 11006 } 11007 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 11008 << RE->getSourceRange(); 11009 return true; 11010 } 11011 11012 // The current expression uses the same base as other expression in the 11013 // data environment but does not contain it completely. 11014 if (!CurrentRegionOnly && SI != SE) 11015 EnclosingExpr = RE; 11016 11017 // The current expression is a subset of the expression in the data 11018 // environment. 11019 IsEnclosedByDataEnvironmentExpr |= 11020 (!CurrentRegionOnly && CI != CE && SI == SE); 11021 11022 return false; 11023 }); 11024 11025 if (CurrentRegionOnly) 11026 return FoundError; 11027 11028 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 11029 // If any part of the original storage of a list item has corresponding 11030 // storage in the device data environment, all of the original storage must 11031 // have corresponding storage in the device data environment. 11032 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 11033 // If a list item is an element of a structure, and a different element of 11034 // the structure has a corresponding list item in the device data environment 11035 // prior to a task encountering the construct associated with the map clause, 11036 // then the list item must also have a corresponding list item in the device 11037 // data environment prior to the task encountering the construct. 11038 // 11039 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 11040 SemaRef.Diag(ELoc, 11041 diag::err_omp_original_storage_is_shared_and_does_not_contain) 11042 << ERange; 11043 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 11044 << EnclosingExpr->getSourceRange(); 11045 return true; 11046 } 11047 11048 return FoundError; 11049 } 11050 11051 namespace { 11052 // Utility struct that gathers all the related lists associated with a mappable 11053 // expression. 11054 struct MappableVarListInfo final { 11055 // The list of expressions. 11056 ArrayRef<Expr *> VarList; 11057 // The list of processed expressions. 11058 SmallVector<Expr *, 16> ProcessedVarList; 11059 // The mappble components for each expression. 11060 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 11061 // The base declaration of the variable. 11062 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 11063 11064 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 11065 // We have a list of components and base declarations for each entry in the 11066 // variable list. 11067 VarComponents.reserve(VarList.size()); 11068 VarBaseDeclarations.reserve(VarList.size()); 11069 } 11070 }; 11071 } 11072 11073 // Check the validity of the provided variable list for the provided clause kind 11074 // \a CKind. In the check process the valid expressions, and mappable expression 11075 // components and variables are extracted and used to fill \a Vars, 11076 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and 11077 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. 11078 static void 11079 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, 11080 OpenMPClauseKind CKind, MappableVarListInfo &MVLI, 11081 SourceLocation StartLoc, 11082 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 11083 bool IsMapTypeImplicit = false) { 11084 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 11085 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 11086 "Unexpected clause kind with mappable expressions!"); 11087 11088 // Keep track of the mappable components and base declarations in this clause. 11089 // Each entry in the list is going to have a list of components associated. We 11090 // record each set of the components so that we can build the clause later on. 11091 // In the end we should have the same amount of declarations and component 11092 // lists. 11093 11094 for (auto &RE : MVLI.VarList) { 11095 assert(RE && "Null expr in omp to/from/map clause"); 11096 SourceLocation ELoc = RE->getExprLoc(); 11097 11098 auto *VE = RE->IgnoreParenLValueCasts(); 11099 11100 if (VE->isValueDependent() || VE->isTypeDependent() || 11101 VE->isInstantiationDependent() || 11102 VE->containsUnexpandedParameterPack()) { 11103 // We can only analyze this information once the missing information is 11104 // resolved. 11105 MVLI.ProcessedVarList.push_back(RE); 11106 continue; 11107 } 11108 11109 auto *SimpleExpr = RE->IgnoreParenCasts(); 11110 11111 if (!RE->IgnoreParenImpCasts()->isLValue()) { 11112 SemaRef.Diag(ELoc, 11113 diag::err_omp_expected_named_var_member_or_array_expression) 11114 << RE->getSourceRange(); 11115 continue; 11116 } 11117 11118 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 11119 ValueDecl *CurDeclaration = nullptr; 11120 11121 // Obtain the array or member expression bases if required. Also, fill the 11122 // components array with all the components identified in the process. 11123 auto *BE = 11124 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind); 11125 if (!BE) 11126 continue; 11127 11128 assert(!CurComponents.empty() && 11129 "Invalid mappable expression information."); 11130 11131 // For the following checks, we rely on the base declaration which is 11132 // expected to be associated with the last component. The declaration is 11133 // expected to be a variable or a field (if 'this' is being mapped). 11134 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 11135 assert(CurDeclaration && "Null decl on map clause."); 11136 assert( 11137 CurDeclaration->isCanonicalDecl() && 11138 "Expecting components to have associated only canonical declarations."); 11139 11140 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 11141 auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 11142 11143 assert((VD || FD) && "Only variables or fields are expected here!"); 11144 (void)FD; 11145 11146 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 11147 // threadprivate variables cannot appear in a map clause. 11148 // OpenMP 4.5 [2.10.5, target update Construct] 11149 // threadprivate variables cannot appear in a from clause. 11150 if (VD && DSAS->isThreadPrivate(VD)) { 11151 auto DVar = DSAS->getTopDSA(VD, false); 11152 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 11153 << getOpenMPClauseName(CKind); 11154 ReportOriginalDSA(SemaRef, DSAS, VD, DVar); 11155 continue; 11156 } 11157 11158 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 11159 // A list item cannot appear in both a map clause and a data-sharing 11160 // attribute clause on the same construct. 11161 11162 // Check conflicts with other map clause expressions. We check the conflicts 11163 // with the current construct separately from the enclosing data 11164 // environment, because the restrictions are different. We only have to 11165 // check conflicts across regions for the map clauses. 11166 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 11167 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 11168 break; 11169 if (CKind == OMPC_map && 11170 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 11171 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 11172 break; 11173 11174 // OpenMP 4.5 [2.10.5, target update Construct] 11175 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 11176 // If the type of a list item is a reference to a type T then the type will 11177 // be considered to be T for all purposes of this clause. 11178 QualType Type = CurDeclaration->getType().getNonReferenceType(); 11179 11180 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 11181 // A list item in a to or from clause must have a mappable type. 11182 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 11183 // A list item must have a mappable type. 11184 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 11185 DSAS, Type)) 11186 continue; 11187 11188 if (CKind == OMPC_map) { 11189 // target enter data 11190 // OpenMP [2.10.2, Restrictions, p. 99] 11191 // A map-type must be specified in all map clauses and must be either 11192 // to or alloc. 11193 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 11194 if (DKind == OMPD_target_enter_data && 11195 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 11196 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 11197 << (IsMapTypeImplicit ? 1 : 0) 11198 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 11199 << getOpenMPDirectiveName(DKind); 11200 continue; 11201 } 11202 11203 // target exit_data 11204 // OpenMP [2.10.3, Restrictions, p. 102] 11205 // A map-type must be specified in all map clauses and must be either 11206 // from, release, or delete. 11207 if (DKind == OMPD_target_exit_data && 11208 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 11209 MapType == OMPC_MAP_delete)) { 11210 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 11211 << (IsMapTypeImplicit ? 1 : 0) 11212 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 11213 << getOpenMPDirectiveName(DKind); 11214 continue; 11215 } 11216 11217 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 11218 // A list item cannot appear in both a map clause and a data-sharing 11219 // attribute clause on the same construct 11220 if ((DKind == OMPD_target || DKind == OMPD_target_teams || 11221 DKind == OMPD_target_teams_distribute || 11222 DKind == OMPD_target_teams_distribute_parallel_for || 11223 DKind == OMPD_target_teams_distribute_parallel_for_simd || 11224 DKind == OMPD_target_teams_distribute_simd) && VD) { 11225 auto DVar = DSAS->getTopDSA(VD, false); 11226 if (isOpenMPPrivate(DVar.CKind)) { 11227 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 11228 << getOpenMPClauseName(DVar.CKind) 11229 << getOpenMPClauseName(OMPC_map) 11230 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 11231 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar); 11232 continue; 11233 } 11234 } 11235 } 11236 11237 // Save the current expression. 11238 MVLI.ProcessedVarList.push_back(RE); 11239 11240 // Store the components in the stack so that they can be used to check 11241 // against other clauses later on. 11242 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 11243 /*WhereFoundClauseKind=*/OMPC_map); 11244 11245 // Save the components and declaration to create the clause. For purposes of 11246 // the clause creation, any component list that has has base 'this' uses 11247 // null as base declaration. 11248 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 11249 MVLI.VarComponents.back().append(CurComponents.begin(), 11250 CurComponents.end()); 11251 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 11252 : CurDeclaration); 11253 } 11254 } 11255 11256 OMPClause * 11257 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, 11258 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 11259 SourceLocation MapLoc, SourceLocation ColonLoc, 11260 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 11261 SourceLocation LParenLoc, SourceLocation EndLoc) { 11262 MappableVarListInfo MVLI(VarList); 11263 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, 11264 MapType, IsMapTypeImplicit); 11265 11266 // We need to produce a map clause even if we don't have variables so that 11267 // other diagnostics related with non-existing map clauses are accurate. 11268 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11269 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 11270 MVLI.VarComponents, MapTypeModifier, MapType, 11271 IsMapTypeImplicit, MapLoc); 11272 } 11273 11274 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 11275 TypeResult ParsedType) { 11276 assert(ParsedType.isUsable()); 11277 11278 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 11279 if (ReductionType.isNull()) 11280 return QualType(); 11281 11282 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 11283 // A type name in a declare reduction directive cannot be a function type, an 11284 // array type, a reference type, or a type qualified with const, volatile or 11285 // restrict. 11286 if (ReductionType.hasQualifiers()) { 11287 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 11288 return QualType(); 11289 } 11290 11291 if (ReductionType->isFunctionType()) { 11292 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 11293 return QualType(); 11294 } 11295 if (ReductionType->isReferenceType()) { 11296 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 11297 return QualType(); 11298 } 11299 if (ReductionType->isArrayType()) { 11300 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 11301 return QualType(); 11302 } 11303 return ReductionType; 11304 } 11305 11306 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 11307 Scope *S, DeclContext *DC, DeclarationName Name, 11308 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 11309 AccessSpecifier AS, Decl *PrevDeclInScope) { 11310 SmallVector<Decl *, 8> Decls; 11311 Decls.reserve(ReductionTypes.size()); 11312 11313 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 11314 ForRedeclaration); 11315 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 11316 // A reduction-identifier may not be re-declared in the current scope for the 11317 // same type or for a type that is compatible according to the base language 11318 // rules. 11319 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 11320 OMPDeclareReductionDecl *PrevDRD = nullptr; 11321 bool InCompoundScope = true; 11322 if (S != nullptr) { 11323 // Find previous declaration with the same name not referenced in other 11324 // declarations. 11325 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 11326 InCompoundScope = 11327 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 11328 LookupName(Lookup, S); 11329 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 11330 /*AllowInlineNamespace=*/false); 11331 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 11332 auto Filter = Lookup.makeFilter(); 11333 while (Filter.hasNext()) { 11334 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 11335 if (InCompoundScope) { 11336 auto I = UsedAsPrevious.find(PrevDecl); 11337 if (I == UsedAsPrevious.end()) 11338 UsedAsPrevious[PrevDecl] = false; 11339 if (auto *D = PrevDecl->getPrevDeclInScope()) 11340 UsedAsPrevious[D] = true; 11341 } 11342 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 11343 PrevDecl->getLocation(); 11344 } 11345 Filter.done(); 11346 if (InCompoundScope) { 11347 for (auto &PrevData : UsedAsPrevious) { 11348 if (!PrevData.second) { 11349 PrevDRD = PrevData.first; 11350 break; 11351 } 11352 } 11353 } 11354 } else if (PrevDeclInScope != nullptr) { 11355 auto *PrevDRDInScope = PrevDRD = 11356 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 11357 do { 11358 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 11359 PrevDRDInScope->getLocation(); 11360 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 11361 } while (PrevDRDInScope != nullptr); 11362 } 11363 for (auto &TyData : ReductionTypes) { 11364 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 11365 bool Invalid = false; 11366 if (I != PreviousRedeclTypes.end()) { 11367 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 11368 << TyData.first; 11369 Diag(I->second, diag::note_previous_definition); 11370 Invalid = true; 11371 } 11372 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 11373 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 11374 Name, TyData.first, PrevDRD); 11375 DC->addDecl(DRD); 11376 DRD->setAccess(AS); 11377 Decls.push_back(DRD); 11378 if (Invalid) 11379 DRD->setInvalidDecl(); 11380 else 11381 PrevDRD = DRD; 11382 } 11383 11384 return DeclGroupPtrTy::make( 11385 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 11386 } 11387 11388 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 11389 auto *DRD = cast<OMPDeclareReductionDecl>(D); 11390 11391 // Enter new function scope. 11392 PushFunctionScope(); 11393 getCurFunction()->setHasBranchProtectedScope(); 11394 getCurFunction()->setHasOMPDeclareReductionCombiner(); 11395 11396 if (S != nullptr) 11397 PushDeclContext(S, DRD); 11398 else 11399 CurContext = DRD; 11400 11401 PushExpressionEvaluationContext( 11402 ExpressionEvaluationContext::PotentiallyEvaluated); 11403 11404 QualType ReductionType = DRD->getType(); 11405 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 11406 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 11407 // uses semantics of argument handles by value, but it should be passed by 11408 // reference. C lang does not support references, so pass all parameters as 11409 // pointers. 11410 // Create 'T omp_in;' variable. 11411 auto *OmpInParm = 11412 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 11413 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 11414 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 11415 // uses semantics of argument handles by value, but it should be passed by 11416 // reference. C lang does not support references, so pass all parameters as 11417 // pointers. 11418 // Create 'T omp_out;' variable. 11419 auto *OmpOutParm = 11420 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 11421 if (S != nullptr) { 11422 PushOnScopeChains(OmpInParm, S); 11423 PushOnScopeChains(OmpOutParm, S); 11424 } else { 11425 DRD->addDecl(OmpInParm); 11426 DRD->addDecl(OmpOutParm); 11427 } 11428 } 11429 11430 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 11431 auto *DRD = cast<OMPDeclareReductionDecl>(D); 11432 DiscardCleanupsInEvaluationContext(); 11433 PopExpressionEvaluationContext(); 11434 11435 PopDeclContext(); 11436 PopFunctionScopeInfo(); 11437 11438 if (Combiner != nullptr) 11439 DRD->setCombiner(Combiner); 11440 else 11441 DRD->setInvalidDecl(); 11442 } 11443 11444 void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 11445 auto *DRD = cast<OMPDeclareReductionDecl>(D); 11446 11447 // Enter new function scope. 11448 PushFunctionScope(); 11449 getCurFunction()->setHasBranchProtectedScope(); 11450 11451 if (S != nullptr) 11452 PushDeclContext(S, DRD); 11453 else 11454 CurContext = DRD; 11455 11456 PushExpressionEvaluationContext( 11457 ExpressionEvaluationContext::PotentiallyEvaluated); 11458 11459 QualType ReductionType = DRD->getType(); 11460 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 11461 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 11462 // uses semantics of argument handles by value, but it should be passed by 11463 // reference. C lang does not support references, so pass all parameters as 11464 // pointers. 11465 // Create 'T omp_priv;' variable. 11466 auto *OmpPrivParm = 11467 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 11468 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 11469 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 11470 // uses semantics of argument handles by value, but it should be passed by 11471 // reference. C lang does not support references, so pass all parameters as 11472 // pointers. 11473 // Create 'T omp_orig;' variable. 11474 auto *OmpOrigParm = 11475 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 11476 if (S != nullptr) { 11477 PushOnScopeChains(OmpPrivParm, S); 11478 PushOnScopeChains(OmpOrigParm, S); 11479 } else { 11480 DRD->addDecl(OmpPrivParm); 11481 DRD->addDecl(OmpOrigParm); 11482 } 11483 } 11484 11485 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, 11486 Expr *Initializer) { 11487 auto *DRD = cast<OMPDeclareReductionDecl>(D); 11488 DiscardCleanupsInEvaluationContext(); 11489 PopExpressionEvaluationContext(); 11490 11491 PopDeclContext(); 11492 PopFunctionScopeInfo(); 11493 11494 if (Initializer != nullptr) 11495 DRD->setInitializer(Initializer); 11496 else 11497 DRD->setInvalidDecl(); 11498 } 11499 11500 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 11501 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 11502 for (auto *D : DeclReductions.get()) { 11503 if (IsValid) { 11504 auto *DRD = cast<OMPDeclareReductionDecl>(D); 11505 if (S != nullptr) 11506 PushOnScopeChains(DRD, S, /*AddToContext=*/false); 11507 } else 11508 D->setInvalidDecl(); 11509 } 11510 return DeclReductions; 11511 } 11512 11513 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 11514 SourceLocation StartLoc, 11515 SourceLocation LParenLoc, 11516 SourceLocation EndLoc) { 11517 Expr *ValExpr = NumTeams; 11518 Stmt *HelperValStmt = nullptr; 11519 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11520 11521 // OpenMP [teams Constrcut, Restrictions] 11522 // The num_teams expression must evaluate to a positive integer value. 11523 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 11524 /*StrictlyPositive=*/true)) 11525 return nullptr; 11526 11527 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11528 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 11529 if (CaptureRegion != OMPD_unknown) { 11530 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 11531 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11532 HelperValStmt = buildPreInits(Context, Captures); 11533 } 11534 11535 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 11536 StartLoc, LParenLoc, EndLoc); 11537 } 11538 11539 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 11540 SourceLocation StartLoc, 11541 SourceLocation LParenLoc, 11542 SourceLocation EndLoc) { 11543 Expr *ValExpr = ThreadLimit; 11544 Stmt *HelperValStmt = nullptr; 11545 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11546 11547 // OpenMP [teams Constrcut, Restrictions] 11548 // The thread_limit expression must evaluate to a positive integer value. 11549 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 11550 /*StrictlyPositive=*/true)) 11551 return nullptr; 11552 11553 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11554 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 11555 if (CaptureRegion != OMPD_unknown) { 11556 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 11557 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11558 HelperValStmt = buildPreInits(Context, Captures); 11559 } 11560 11561 return new (Context) OMPThreadLimitClause( 11562 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 11563 } 11564 11565 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 11566 SourceLocation StartLoc, 11567 SourceLocation LParenLoc, 11568 SourceLocation EndLoc) { 11569 Expr *ValExpr = Priority; 11570 11571 // OpenMP [2.9.1, task Constrcut] 11572 // The priority-value is a non-negative numerical scalar expression. 11573 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 11574 /*StrictlyPositive=*/false)) 11575 return nullptr; 11576 11577 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 11578 } 11579 11580 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 11581 SourceLocation StartLoc, 11582 SourceLocation LParenLoc, 11583 SourceLocation EndLoc) { 11584 Expr *ValExpr = Grainsize; 11585 11586 // OpenMP [2.9.2, taskloop Constrcut] 11587 // The parameter of the grainsize clause must be a positive integer 11588 // expression. 11589 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 11590 /*StrictlyPositive=*/true)) 11591 return nullptr; 11592 11593 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 11594 } 11595 11596 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 11597 SourceLocation StartLoc, 11598 SourceLocation LParenLoc, 11599 SourceLocation EndLoc) { 11600 Expr *ValExpr = NumTasks; 11601 11602 // OpenMP [2.9.2, taskloop Constrcut] 11603 // The parameter of the num_tasks clause must be a positive integer 11604 // expression. 11605 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 11606 /*StrictlyPositive=*/true)) 11607 return nullptr; 11608 11609 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 11610 } 11611 11612 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 11613 SourceLocation LParenLoc, 11614 SourceLocation EndLoc) { 11615 // OpenMP [2.13.2, critical construct, Description] 11616 // ... where hint-expression is an integer constant expression that evaluates 11617 // to a valid lock hint. 11618 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 11619 if (HintExpr.isInvalid()) 11620 return nullptr; 11621 return new (Context) 11622 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 11623 } 11624 11625 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 11626 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 11627 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 11628 SourceLocation EndLoc) { 11629 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 11630 std::string Values; 11631 Values += "'"; 11632 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 11633 Values += "'"; 11634 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 11635 << Values << getOpenMPClauseName(OMPC_dist_schedule); 11636 return nullptr; 11637 } 11638 Expr *ValExpr = ChunkSize; 11639 Stmt *HelperValStmt = nullptr; 11640 if (ChunkSize) { 11641 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 11642 !ChunkSize->isInstantiationDependent() && 11643 !ChunkSize->containsUnexpandedParameterPack()) { 11644 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 11645 ExprResult Val = 11646 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 11647 if (Val.isInvalid()) 11648 return nullptr; 11649 11650 ValExpr = Val.get(); 11651 11652 // OpenMP [2.7.1, Restrictions] 11653 // chunk_size must be a loop invariant integer expression with a positive 11654 // value. 11655 llvm::APSInt Result; 11656 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 11657 if (Result.isSigned() && !Result.isStrictlyPositive()) { 11658 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 11659 << "dist_schedule" << ChunkSize->getSourceRange(); 11660 return nullptr; 11661 } 11662 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && 11663 !CurContext->isDependentContext()) { 11664 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 11665 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11666 HelperValStmt = buildPreInits(Context, Captures); 11667 } 11668 } 11669 } 11670 11671 return new (Context) 11672 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 11673 Kind, ValExpr, HelperValStmt); 11674 } 11675 11676 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 11677 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 11678 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 11679 SourceLocation KindLoc, SourceLocation EndLoc) { 11680 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 11681 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 11682 std::string Value; 11683 SourceLocation Loc; 11684 Value += "'"; 11685 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 11686 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 11687 OMPC_DEFAULTMAP_MODIFIER_tofrom); 11688 Loc = MLoc; 11689 } else { 11690 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 11691 OMPC_DEFAULTMAP_scalar); 11692 Loc = KindLoc; 11693 } 11694 Value += "'"; 11695 Diag(Loc, diag::err_omp_unexpected_clause_value) 11696 << Value << getOpenMPClauseName(OMPC_defaultmap); 11697 return nullptr; 11698 } 11699 11700 return new (Context) 11701 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 11702 } 11703 11704 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 11705 DeclContext *CurLexicalContext = getCurLexicalContext(); 11706 if (!CurLexicalContext->isFileContext() && 11707 !CurLexicalContext->isExternCContext() && 11708 !CurLexicalContext->isExternCXXContext()) { 11709 Diag(Loc, diag::err_omp_region_not_file_context); 11710 return false; 11711 } 11712 if (IsInOpenMPDeclareTargetContext) { 11713 Diag(Loc, diag::err_omp_enclosed_declare_target); 11714 return false; 11715 } 11716 11717 IsInOpenMPDeclareTargetContext = true; 11718 return true; 11719 } 11720 11721 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 11722 assert(IsInOpenMPDeclareTargetContext && 11723 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 11724 11725 IsInOpenMPDeclareTargetContext = false; 11726 } 11727 11728 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 11729 CXXScopeSpec &ScopeSpec, 11730 const DeclarationNameInfo &Id, 11731 OMPDeclareTargetDeclAttr::MapTypeTy MT, 11732 NamedDeclSetType &SameDirectiveDecls) { 11733 LookupResult Lookup(*this, Id, LookupOrdinaryName); 11734 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 11735 11736 if (Lookup.isAmbiguous()) 11737 return; 11738 Lookup.suppressDiagnostics(); 11739 11740 if (!Lookup.isSingleResult()) { 11741 if (TypoCorrection Corrected = 11742 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 11743 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 11744 CTK_ErrorRecovery)) { 11745 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 11746 << Id.getName()); 11747 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 11748 return; 11749 } 11750 11751 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 11752 return; 11753 } 11754 11755 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 11756 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 11757 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 11758 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 11759 11760 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) { 11761 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 11762 ND->addAttr(A); 11763 if (ASTMutationListener *ML = Context.getASTMutationListener()) 11764 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 11765 checkDeclIsAllowedInOpenMPTarget(nullptr, ND); 11766 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) { 11767 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 11768 << Id.getName(); 11769 } 11770 } else 11771 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 11772 } 11773 11774 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 11775 Sema &SemaRef, Decl *D) { 11776 if (!D) 11777 return; 11778 Decl *LD = nullptr; 11779 if (isa<TagDecl>(D)) { 11780 LD = cast<TagDecl>(D)->getDefinition(); 11781 } else if (isa<VarDecl>(D)) { 11782 LD = cast<VarDecl>(D)->getDefinition(); 11783 11784 // If this is an implicit variable that is legal and we do not need to do 11785 // anything. 11786 if (cast<VarDecl>(D)->isImplicit()) { 11787 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 11788 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 11789 D->addAttr(A); 11790 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 11791 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 11792 return; 11793 } 11794 11795 } else if (isa<FunctionDecl>(D)) { 11796 const FunctionDecl *FD = nullptr; 11797 if (cast<FunctionDecl>(D)->hasBody(FD)) 11798 LD = const_cast<FunctionDecl *>(FD); 11799 11800 // If the definition is associated with the current declaration in the 11801 // target region (it can be e.g. a lambda) that is legal and we do not need 11802 // to do anything else. 11803 if (LD == D) { 11804 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 11805 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 11806 D->addAttr(A); 11807 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 11808 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 11809 return; 11810 } 11811 } 11812 if (!LD) 11813 LD = D; 11814 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() && 11815 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) { 11816 // Outlined declaration is not declared target. 11817 if (LD->isOutOfLine()) { 11818 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 11819 SemaRef.Diag(SL, diag::note_used_here) << SR; 11820 } else { 11821 DeclContext *DC = LD->getDeclContext(); 11822 while (DC) { 11823 if (isa<FunctionDecl>(DC) && 11824 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>()) 11825 break; 11826 DC = DC->getParent(); 11827 } 11828 if (DC) 11829 return; 11830 11831 // Is not declared in target context. 11832 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 11833 SemaRef.Diag(SL, diag::note_used_here) << SR; 11834 } 11835 // Mark decl as declared target to prevent further diagnostic. 11836 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 11837 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 11838 D->addAttr(A); 11839 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 11840 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 11841 } 11842 } 11843 11844 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 11845 Sema &SemaRef, DSAStackTy *Stack, 11846 ValueDecl *VD) { 11847 if (VD->hasAttr<OMPDeclareTargetDeclAttr>()) 11848 return true; 11849 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType())) 11850 return false; 11851 return true; 11852 } 11853 11854 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { 11855 if (!D || D->isInvalidDecl()) 11856 return; 11857 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 11858 SourceLocation SL = E ? E->getLocStart() : D->getLocation(); 11859 // 2.10.6: threadprivate variable cannot appear in a declare target directive. 11860 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 11861 if (DSAStack->isThreadPrivate(VD)) { 11862 Diag(SL, diag::err_omp_threadprivate_in_target); 11863 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 11864 return; 11865 } 11866 } 11867 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { 11868 // Problem if any with var declared with incomplete type will be reported 11869 // as normal, so no need to check it here. 11870 if ((E || !VD->getType()->isIncompleteType()) && 11871 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { 11872 // Mark decl as declared target to prevent further diagnostic. 11873 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) { 11874 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 11875 Context, OMPDeclareTargetDeclAttr::MT_To); 11876 VD->addAttr(A); 11877 if (ASTMutationListener *ML = Context.getASTMutationListener()) 11878 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A); 11879 } 11880 return; 11881 } 11882 } 11883 if (!E) { 11884 // Checking declaration inside declare target region. 11885 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() && 11886 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) { 11887 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 11888 Context, OMPDeclareTargetDeclAttr::MT_To); 11889 D->addAttr(A); 11890 if (ASTMutationListener *ML = Context.getASTMutationListener()) 11891 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 11892 } 11893 return; 11894 } 11895 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 11896 } 11897 11898 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 11899 SourceLocation StartLoc, 11900 SourceLocation LParenLoc, 11901 SourceLocation EndLoc) { 11902 MappableVarListInfo MVLI(VarList); 11903 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); 11904 if (MVLI.ProcessedVarList.empty()) 11905 return nullptr; 11906 11907 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11908 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 11909 MVLI.VarComponents); 11910 } 11911 11912 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 11913 SourceLocation StartLoc, 11914 SourceLocation LParenLoc, 11915 SourceLocation EndLoc) { 11916 MappableVarListInfo MVLI(VarList); 11917 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); 11918 if (MVLI.ProcessedVarList.empty()) 11919 return nullptr; 11920 11921 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11922 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 11923 MVLI.VarComponents); 11924 } 11925 11926 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 11927 SourceLocation StartLoc, 11928 SourceLocation LParenLoc, 11929 SourceLocation EndLoc) { 11930 MappableVarListInfo MVLI(VarList); 11931 SmallVector<Expr *, 8> PrivateCopies; 11932 SmallVector<Expr *, 8> Inits; 11933 11934 for (auto &RefExpr : VarList) { 11935 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 11936 SourceLocation ELoc; 11937 SourceRange ERange; 11938 Expr *SimpleRefExpr = RefExpr; 11939 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11940 if (Res.second) { 11941 // It will be analyzed later. 11942 MVLI.ProcessedVarList.push_back(RefExpr); 11943 PrivateCopies.push_back(nullptr); 11944 Inits.push_back(nullptr); 11945 } 11946 ValueDecl *D = Res.first; 11947 if (!D) 11948 continue; 11949 11950 QualType Type = D->getType(); 11951 Type = Type.getNonReferenceType().getUnqualifiedType(); 11952 11953 auto *VD = dyn_cast<VarDecl>(D); 11954 11955 // Item should be a pointer or reference to pointer. 11956 if (!Type->isPointerType()) { 11957 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 11958 << 0 << RefExpr->getSourceRange(); 11959 continue; 11960 } 11961 11962 // Build the private variable and the expression that refers to it. 11963 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 11964 D->hasAttrs() ? &D->getAttrs() : nullptr); 11965 if (VDPrivate->isInvalidDecl()) 11966 continue; 11967 11968 CurContext->addDecl(VDPrivate); 11969 auto VDPrivateRefExpr = buildDeclRefExpr( 11970 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 11971 11972 // Add temporary variable to initialize the private copy of the pointer. 11973 auto *VDInit = 11974 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 11975 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 11976 RefExpr->getExprLoc()); 11977 AddInitializerToDecl(VDPrivate, 11978 DefaultLvalueConversion(VDInitRefExpr).get(), 11979 /*DirectInit=*/false); 11980 11981 // If required, build a capture to implement the privatization initialized 11982 // with the current list item value. 11983 DeclRefExpr *Ref = nullptr; 11984 if (!VD) 11985 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11986 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 11987 PrivateCopies.push_back(VDPrivateRefExpr); 11988 Inits.push_back(VDInitRefExpr); 11989 11990 // We need to add a data sharing attribute for this variable to make sure it 11991 // is correctly captured. A variable that shows up in a use_device_ptr has 11992 // similar properties of a first private variable. 11993 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 11994 11995 // Create a mappable component for the list item. List items in this clause 11996 // only need a component. 11997 MVLI.VarBaseDeclarations.push_back(D); 11998 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 11999 MVLI.VarComponents.back().push_back( 12000 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 12001 } 12002 12003 if (MVLI.ProcessedVarList.empty()) 12004 return nullptr; 12005 12006 return OMPUseDevicePtrClause::Create( 12007 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 12008 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); 12009 } 12010 12011 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 12012 SourceLocation StartLoc, 12013 SourceLocation LParenLoc, 12014 SourceLocation EndLoc) { 12015 MappableVarListInfo MVLI(VarList); 12016 for (auto &RefExpr : VarList) { 12017 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 12018 SourceLocation ELoc; 12019 SourceRange ERange; 12020 Expr *SimpleRefExpr = RefExpr; 12021 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12022 if (Res.second) { 12023 // It will be analyzed later. 12024 MVLI.ProcessedVarList.push_back(RefExpr); 12025 } 12026 ValueDecl *D = Res.first; 12027 if (!D) 12028 continue; 12029 12030 QualType Type = D->getType(); 12031 // item should be a pointer or array or reference to pointer or array 12032 if (!Type.getNonReferenceType()->isPointerType() && 12033 !Type.getNonReferenceType()->isArrayType()) { 12034 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 12035 << 0 << RefExpr->getSourceRange(); 12036 continue; 12037 } 12038 12039 // Check if the declaration in the clause does not show up in any data 12040 // sharing attribute. 12041 auto DVar = DSAStack->getTopDSA(D, false); 12042 if (isOpenMPPrivate(DVar.CKind)) { 12043 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 12044 << getOpenMPClauseName(DVar.CKind) 12045 << getOpenMPClauseName(OMPC_is_device_ptr) 12046 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 12047 ReportOriginalDSA(*this, DSAStack, D, DVar); 12048 continue; 12049 } 12050 12051 Expr *ConflictExpr; 12052 if (DSAStack->checkMappableExprComponentListsForDecl( 12053 D, /*CurrentRegionOnly=*/true, 12054 [&ConflictExpr]( 12055 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 12056 OpenMPClauseKind) -> bool { 12057 ConflictExpr = R.front().getAssociatedExpression(); 12058 return true; 12059 })) { 12060 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 12061 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 12062 << ConflictExpr->getSourceRange(); 12063 continue; 12064 } 12065 12066 // Store the components in the stack so that they can be used to check 12067 // against other clauses later on. 12068 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 12069 DSAStack->addMappableExpressionComponents( 12070 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 12071 12072 // Record the expression we've just processed. 12073 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 12074 12075 // Create a mappable component for the list item. List items in this clause 12076 // only need a component. We use a null declaration to signal fields in 12077 // 'this'. 12078 assert((isa<DeclRefExpr>(SimpleRefExpr) || 12079 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 12080 "Unexpected device pointer expression!"); 12081 MVLI.VarBaseDeclarations.push_back( 12082 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 12083 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 12084 MVLI.VarComponents.back().push_back(MC); 12085 } 12086 12087 if (MVLI.ProcessedVarList.empty()) 12088 return nullptr; 12089 12090 return OMPIsDevicePtrClause::Create( 12091 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 12092 MVLI.VarBaseDeclarations, MVLI.VarComponents); 12093 } 12094