1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// This file implements semantic analysis for OpenMP directives and
12 //===----------------------------------------------------------------------===//
14 #include "TreeTransform.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/AST/StmtCXX.h"
22 #include "clang/AST/StmtOpenMP.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/AST/TypeOrdering.h"
25 #include "clang/Basic/OpenMPKinds.h"
26 #include "clang/Sema/Initialization.h"
27 #include "clang/Sema/Lookup.h"
28 #include "clang/Sema/Scope.h"
29 #include "clang/Sema/ScopeInfo.h"
30 #include "clang/Sema/SemaInternal.h"
31 #include "llvm/ADT/PointerEmbeddedInt.h"
32 using namespace clang;
34 //===----------------------------------------------------------------------===//
35 // Stack of data-sharing attributes for variables
36 //===----------------------------------------------------------------------===//
38 static const Expr *checkMapClauseExpressionBase(
39 Sema &SemaRef, Expr *E,
40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
41 OpenMPClauseKind CKind, bool NoDiagnose);
44 /// Default data sharing attributes, which can be applied to directive.
45 enum DefaultDataSharingAttributes {
46 DSA_unspecified = 0, /// Data sharing attribute not specified.
47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
51 /// Attributes of the defaultmap clause.
52 enum DefaultMapAttributes {
53 DMA_unspecified, /// Default mapping is not specified.
54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
57 /// Stack for tracking declarations used in OpenMP directives and
58 /// clauses and their data-sharing attributes.
62 OpenMPDirectiveKind DKind = OMPD_unknown;
63 OpenMPClauseKind CKind = OMPC_unknown;
64 const Expr *RefExpr = nullptr;
65 DeclRefExpr *PrivateCopy = nullptr;
66 SourceLocation ImplicitDSALoc;
67 DSAVarData() = default;
68 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70 SourceLocation ImplicitDSALoc)
71 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
74 using OperatorOffsetTy =
75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
76 using DoacrossDependMapTy =
77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
81 OpenMPClauseKind Attributes = OMPC_unknown;
82 /// Pointer to a reference expression and a flag which shows that the
83 /// variable is marked as lastprivate(true) or not (false).
84 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
85 DeclRefExpr *PrivateCopy = nullptr;
87 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90 using LoopControlVariablesMapTy =
91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
92 /// Struct that associates a component with the clause kind where they are
94 struct MappedExprComponentTy {
95 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96 OpenMPClauseKind Kind = OMPC_unknown;
98 using MappedExprComponentsTy =
99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100 using CriticalsWithHintsTy =
101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
102 struct ReductionData {
103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
104 SourceRange ReductionRange;
105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
106 ReductionData() = default;
107 void set(BinaryOperatorKind BO, SourceRange RR) {
111 void set(const Expr *RefExpr, SourceRange RR) {
113 ReductionOp = RefExpr;
116 using DeclReductionMapTy =
117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
119 struct SharingMapTy {
120 DeclSAMapTy SharingMap;
121 DeclReductionMapTy ReductionMap;
122 AlignedMapTy AlignedMap;
123 MappedExprComponentsTy MappedExprComponents;
124 LoopControlVariablesMapTy LCVMap;
125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
126 SourceLocation DefaultAttrLoc;
127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128 SourceLocation DefaultMapAttrLoc;
129 OpenMPDirectiveKind Directive = OMPD_unknown;
130 DeclarationNameInfo DirectiveName;
131 Scope *CurScope = nullptr;
132 SourceLocation ConstructLoc;
133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134 /// get the data (loop counters etc.) about enclosing loop-based construct.
135 /// This data is required during codegen.
136 DoacrossDependMapTy DoacrossDepends;
137 /// First argument (Expr *) contains optional argument of the
138 /// 'ordered' clause, the second one is true if the regions has 'ordered'
139 /// clause, false otherwise.
140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
141 unsigned AssociatedLoops = 1;
142 bool HasMutipleLoops = false;
143 const Decl *PossiblyLoopCounter = nullptr;
144 bool NowaitRegion = false;
145 bool CancelRegion = false;
146 bool LoopStart = false;
147 bool BodyComplete = false;
148 SourceLocation InnerTeamsRegionLoc;
149 /// Reference to the taskgroup task_reduction reference expression.
150 Expr *TaskgroupReductionRef = nullptr;
151 llvm::DenseSet<QualType> MappedClassesQualTypes;
152 /// List of globals marked as declare target link in this target region
153 /// (isOpenMPTargetExecutionDirective(Directive) == true).
154 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
155 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
156 Scope *CurScope, SourceLocation Loc)
157 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
159 SharingMapTy() = default;
162 using StackTy = SmallVector<SharingMapTy, 4>;
164 /// Stack of used declaration and their data-sharing attributes.
165 DeclSAMapTy Threadprivates;
166 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
167 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
168 /// true, if check for DSA must be from parent directive, false, if
169 /// from current directive.
170 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
172 bool ForceCapturing = false;
173 /// true if all the vaiables in the target executable directives must be
174 /// captured by reference.
175 bool ForceCaptureByReferenceInTargetExecutable = false;
176 CriticalsWithHintsTy Criticals;
177 unsigned IgnoredStackElements = 0;
179 /// Iterators over the stack iterate in order from innermost to outermost
181 using const_iterator = StackTy::const_reverse_iterator;
182 const_iterator begin() const {
183 return Stack.empty() ? const_iterator()
184 : Stack.back().first.rbegin() + IgnoredStackElements;
186 const_iterator end() const {
187 return Stack.empty() ? const_iterator() : Stack.back().first.rend();
189 using iterator = StackTy::reverse_iterator;
191 return Stack.empty() ? iterator()
192 : Stack.back().first.rbegin() + IgnoredStackElements;
195 return Stack.empty() ? iterator() : Stack.back().first.rend();
198 // Convenience operations to get at the elements of the stack.
200 bool isStackEmpty() const {
201 return Stack.empty() ||
202 Stack.back().second != CurrentNonCapturingFunctionScope ||
203 Stack.back().first.size() <= IgnoredStackElements;
205 size_t getStackSize() const {
206 return isStackEmpty() ? 0
207 : Stack.back().first.size() - IgnoredStackElements;
210 SharingMapTy *getTopOfStackOrNull() {
211 size_t Size = getStackSize();
214 return &Stack.back().first[Size - 1];
216 const SharingMapTy *getTopOfStackOrNull() const {
217 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull();
219 SharingMapTy &getTopOfStack() {
220 assert(!isStackEmpty() && "no current directive");
221 return *getTopOfStackOrNull();
223 const SharingMapTy &getTopOfStack() const {
224 return const_cast<DSAStackTy&>(*this).getTopOfStack();
227 SharingMapTy *getSecondOnStackOrNull() {
228 size_t Size = getStackSize();
231 return &Stack.back().first[Size - 2];
233 const SharingMapTy *getSecondOnStackOrNull() const {
234 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull();
237 /// Get the stack element at a certain level (previously returned by
238 /// \c getNestingLevel).
240 /// Note that nesting levels count from outermost to innermost, and this is
241 /// the reverse of our iteration order where new inner levels are pushed at
242 /// the front of the stack.
243 SharingMapTy &getStackElemAtLevel(unsigned Level) {
244 assert(Level < getStackSize() && "no such stack element");
245 return Stack.back().first[Level];
247 const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
248 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level);
251 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
253 /// Checks if the variable is a local for OpenMP region.
254 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
256 /// Vector of previously declared requires directives
257 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
258 /// omp_allocator_handle_t type.
259 QualType OMPAllocatorHandleT;
260 /// Expression for the predefined allocators.
261 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
263 /// Vector of previously encountered target directives
264 SmallVector<SourceLocation, 2> TargetLocations;
267 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
269 /// Sets omp_allocator_handle_t type.
270 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
271 /// Gets omp_allocator_handle_t type.
272 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
273 /// Sets the given default allocator.
274 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
276 OMPPredefinedAllocators[AllocatorKind] = Allocator;
278 /// Returns the specified default allocator.
279 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
280 return OMPPredefinedAllocators[AllocatorKind];
283 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
284 OpenMPClauseKind getClauseParsingMode() const {
285 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
286 return ClauseKindMode;
288 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
290 bool isBodyComplete() const {
291 const SharingMapTy *Top = getTopOfStackOrNull();
292 return Top && Top->BodyComplete;
294 void setBodyComplete() {
295 getTopOfStack().BodyComplete = true;
298 bool isForceVarCapturing() const { return ForceCapturing; }
299 void setForceVarCapturing(bool V) { ForceCapturing = V; }
301 void setForceCaptureByReferenceInTargetExecutable(bool V) {
302 ForceCaptureByReferenceInTargetExecutable = V;
304 bool isForceCaptureByReferenceInTargetExecutable() const {
305 return ForceCaptureByReferenceInTargetExecutable;
308 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
309 Scope *CurScope, SourceLocation Loc) {
310 assert(!IgnoredStackElements &&
311 "cannot change stack while ignoring elements");
313 Stack.back().second != CurrentNonCapturingFunctionScope)
314 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
315 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
316 Stack.back().first.back().DefaultAttrLoc = Loc;
320 assert(!IgnoredStackElements &&
321 "cannot change stack while ignoring elements");
322 assert(!Stack.back().first.empty() &&
323 "Data-sharing attributes stack is empty!");
324 Stack.back().first.pop_back();
327 /// RAII object to temporarily leave the scope of a directive when we want to
328 /// logically operate in its parent.
329 class ParentDirectiveScope {
333 ParentDirectiveScope(DSAStackTy &Self, bool Activate)
334 : Self(Self), Active(false) {
338 ~ParentDirectiveScope() { disable(); }
341 --Self.IgnoredStackElements;
347 ++Self.IgnoredStackElements;
353 /// Marks that we're started loop parsing.
355 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
356 "Expected loop-based directive.");
357 getTopOfStack().LoopStart = true;
359 /// Start capturing of the variables in the loop context.
361 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
362 "Expected loop-based directive.");
363 getTopOfStack().LoopStart = false;
365 /// true, if variables are captured, false otherwise.
366 bool isLoopStarted() const {
367 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
368 "Expected loop-based directive.");
369 return !getTopOfStack().LoopStart;
371 /// Marks (or clears) declaration as possibly loop counter.
372 void resetPossibleLoopCounter(const Decl *D = nullptr) {
373 getTopOfStack().PossiblyLoopCounter =
374 D ? D->getCanonicalDecl() : D;
376 /// Gets the possible loop counter decl.
377 const Decl *getPossiblyLoopCunter() const {
378 return getTopOfStack().PossiblyLoopCounter;
380 /// Start new OpenMP region stack in new non-capturing function.
381 void pushFunction() {
382 assert(!IgnoredStackElements &&
383 "cannot change stack while ignoring elements");
384 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
385 assert(!isa<CapturingScopeInfo>(CurFnScope));
386 CurrentNonCapturingFunctionScope = CurFnScope;
388 /// Pop region stack for non-capturing function.
389 void popFunction(const FunctionScopeInfo *OldFSI) {
390 assert(!IgnoredStackElements &&
391 "cannot change stack while ignoring elements");
392 if (!Stack.empty() && Stack.back().second == OldFSI) {
393 assert(Stack.back().first.empty());
396 CurrentNonCapturingFunctionScope = nullptr;
397 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
398 if (!isa<CapturingScopeInfo>(FSI)) {
399 CurrentNonCapturingFunctionScope = FSI;
405 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
406 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
408 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
409 getCriticalWithHint(const DeclarationNameInfo &Name) const {
410 auto I = Criticals.find(Name.getAsString());
411 if (I != Criticals.end())
413 return std::make_pair(nullptr, llvm::APSInt());
415 /// If 'aligned' declaration for given variable \a D was not seen yet,
416 /// add it and return NULL; otherwise return previous occurrence's expression
418 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
420 /// Register specified variable as loop control variable.
421 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
422 /// Check if the specified variable is a loop control variable for
424 /// \return The index of the loop control variable in the list of associated
425 /// for-loops (from outer to inner).
426 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
427 /// Check if the specified variable is a loop control variable for
429 /// \return The index of the loop control variable in the list of associated
430 /// for-loops (from outer to inner).
431 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
432 /// Get the loop control variable for the I-th loop (or nullptr) in
433 /// parent directive.
434 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
436 /// Adds explicit data sharing attribute to the specified declaration.
437 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
438 DeclRefExpr *PrivateCopy = nullptr);
440 /// Adds additional information for the reduction items with the reduction id
441 /// represented as an operator.
442 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
443 BinaryOperatorKind BOK);
444 /// Adds additional information for the reduction items with the reduction id
445 /// represented as reduction identifier.
446 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
447 const Expr *ReductionRef);
448 /// Returns the location and reduction operation from the innermost parent
449 /// region for the given \p D.
451 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
452 BinaryOperatorKind &BOK,
453 Expr *&TaskgroupDescriptor) const;
454 /// Returns the location and reduction operation from the innermost parent
455 /// region for the given \p D.
457 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
458 const Expr *&ReductionRef,
459 Expr *&TaskgroupDescriptor) const;
460 /// Return reduction reference expression for the current taskgroup.
461 Expr *getTaskgroupReductionRef() const {
462 assert(getTopOfStack().Directive == OMPD_taskgroup &&
463 "taskgroup reference expression requested for non taskgroup "
465 return getTopOfStack().TaskgroupReductionRef;
467 /// Checks if the given \p VD declaration is actually a taskgroup reduction
468 /// descriptor variable at the \p Level of OpenMP regions.
469 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
470 return getStackElemAtLevel(Level).TaskgroupReductionRef &&
471 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
475 /// Returns data sharing attributes from top of the stack for the
476 /// specified declaration.
477 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
478 /// Returns data-sharing attributes for the specified declaration.
479 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
480 /// Checks if the specified variables has data-sharing attributes which
481 /// match specified \a CPred predicate in any directive which matches \a DPred
484 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
485 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
486 bool FromParent) const;
487 /// Checks if the specified variables has data-sharing attributes which
488 /// match specified \a CPred predicate in any innermost directive which
489 /// matches \a DPred predicate.
491 hasInnermostDSA(ValueDecl *D,
492 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
493 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
494 bool FromParent) const;
495 /// Checks if the specified variables has explicit data-sharing
496 /// attributes which match specified \a CPred predicate at the specified
498 bool hasExplicitDSA(const ValueDecl *D,
499 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
500 unsigned Level, bool NotLastprivate = false) const;
502 /// Returns true if the directive at level \Level matches in the
503 /// specified \a DPred predicate.
504 bool hasExplicitDirective(
505 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
506 unsigned Level) const;
508 /// Finds a directive which matches specified \a DPred predicate.
510 const llvm::function_ref<bool(
511 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
513 bool FromParent) const;
515 /// Returns currently analyzed directive.
516 OpenMPDirectiveKind getCurrentDirective() const {
517 const SharingMapTy *Top = getTopOfStackOrNull();
518 return Top ? Top->Directive : OMPD_unknown;
520 /// Returns directive kind at specified level.
521 OpenMPDirectiveKind getDirective(unsigned Level) const {
522 assert(!isStackEmpty() && "No directive at specified level.");
523 return getStackElemAtLevel(Level).Directive;
525 /// Returns parent directive.
526 OpenMPDirectiveKind getParentDirective() const {
527 const SharingMapTy *Parent = getSecondOnStackOrNull();
528 return Parent ? Parent->Directive : OMPD_unknown;
531 /// Add requires decl to internal vector
532 void addRequiresDecl(OMPRequiresDecl *RD) {
533 RequiresDecls.push_back(RD);
536 /// Checks if the defined 'requires' directive has specified type of clause.
537 template <typename ClauseType>
538 bool hasRequiresDeclWithClause() {
539 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540 return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541 return isa<ClauseType>(C);
546 /// Checks for a duplicate clause amongst previously declared requires
548 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
549 bool IsDuplicate = false;
550 for (OMPClause *CNew : ClauseList) {
551 for (const OMPRequiresDecl *D : RequiresDecls) {
552 for (const OMPClause *CPrev : D->clauselists()) {
553 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
554 SemaRef.Diag(CNew->getBeginLoc(),
555 diag::err_omp_requires_clause_redeclaration)
556 << getOpenMPClauseName(CNew->getClauseKind());
557 SemaRef.Diag(CPrev->getBeginLoc(),
558 diag::note_omp_requires_previous_clause)
559 << getOpenMPClauseName(CPrev->getClauseKind());
568 /// Add location of previously encountered target to internal vector
569 void addTargetDirLocation(SourceLocation LocStart) {
570 TargetLocations.push_back(LocStart);
573 // Return previously encountered target region locations.
574 ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
575 return TargetLocations;
578 /// Set default data sharing attribute to none.
579 void setDefaultDSANone(SourceLocation Loc) {
580 getTopOfStack().DefaultAttr = DSA_none;
581 getTopOfStack().DefaultAttrLoc = Loc;
583 /// Set default data sharing attribute to shared.
584 void setDefaultDSAShared(SourceLocation Loc) {
585 getTopOfStack().DefaultAttr = DSA_shared;
586 getTopOfStack().DefaultAttrLoc = Loc;
588 /// Set default data mapping attribute to 'tofrom:scalar'.
589 void setDefaultDMAToFromScalar(SourceLocation Loc) {
590 getTopOfStack().DefaultMapAttr = DMA_tofrom_scalar;
591 getTopOfStack().DefaultMapAttrLoc = Loc;
594 DefaultDataSharingAttributes getDefaultDSA() const {
595 return isStackEmpty() ? DSA_unspecified
596 : getTopOfStack().DefaultAttr;
598 SourceLocation getDefaultDSALocation() const {
599 return isStackEmpty() ? SourceLocation()
600 : getTopOfStack().DefaultAttrLoc;
602 DefaultMapAttributes getDefaultDMA() const {
603 return isStackEmpty() ? DMA_unspecified
604 : getTopOfStack().DefaultMapAttr;
606 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
607 return getStackElemAtLevel(Level).DefaultMapAttr;
609 SourceLocation getDefaultDMALocation() const {
610 return isStackEmpty() ? SourceLocation()
611 : getTopOfStack().DefaultMapAttrLoc;
614 /// Checks if the specified variable is a threadprivate.
615 bool isThreadPrivate(VarDecl *D) {
616 const DSAVarData DVar = getTopDSA(D, false);
617 return isOpenMPThreadPrivate(DVar.CKind);
620 /// Marks current region as ordered (it has an 'ordered' clause).
621 void setOrderedRegion(bool IsOrdered, const Expr *Param,
622 OMPOrderedClause *Clause) {
624 getTopOfStack().OrderedRegion.emplace(Param, Clause);
626 getTopOfStack().OrderedRegion.reset();
628 /// Returns true, if region is ordered (has associated 'ordered' clause),
629 /// false - otherwise.
630 bool isOrderedRegion() const {
631 if (const SharingMapTy *Top = getTopOfStackOrNull())
632 return Top->OrderedRegion.hasValue();
635 /// Returns optional parameter for the ordered region.
636 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
637 if (const SharingMapTy *Top = getTopOfStackOrNull())
638 if (Top->OrderedRegion.hasValue())
639 return Top->OrderedRegion.getValue();
640 return std::make_pair(nullptr, nullptr);
642 /// Returns true, if parent region is ordered (has associated
643 /// 'ordered' clause), false - otherwise.
644 bool isParentOrderedRegion() const {
645 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
646 return Parent->OrderedRegion.hasValue();
649 /// Returns optional parameter for the ordered region.
650 std::pair<const Expr *, OMPOrderedClause *>
651 getParentOrderedRegionParam() const {
652 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
653 if (Parent->OrderedRegion.hasValue())
654 return Parent->OrderedRegion.getValue();
655 return std::make_pair(nullptr, nullptr);
657 /// Marks current region as nowait (it has a 'nowait' clause).
658 void setNowaitRegion(bool IsNowait = true) {
659 getTopOfStack().NowaitRegion = IsNowait;
661 /// Returns true, if parent region is nowait (has associated
662 /// 'nowait' clause), false - otherwise.
663 bool isParentNowaitRegion() const {
664 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
665 return Parent->NowaitRegion;
668 /// Marks parent region as cancel region.
669 void setParentCancelRegion(bool Cancel = true) {
670 if (SharingMapTy *Parent = getSecondOnStackOrNull())
671 Parent->CancelRegion |= Cancel;
673 /// Return true if current region has inner cancel construct.
674 bool isCancelRegion() const {
675 const SharingMapTy *Top = getTopOfStackOrNull();
676 return Top ? Top->CancelRegion : false;
679 /// Set collapse value for the region.
680 void setAssociatedLoops(unsigned Val) {
681 getTopOfStack().AssociatedLoops = Val;
683 getTopOfStack().HasMutipleLoops = true;
685 /// Return collapse value for region.
686 unsigned getAssociatedLoops() const {
687 const SharingMapTy *Top = getTopOfStackOrNull();
688 return Top ? Top->AssociatedLoops : 0;
690 /// Returns true if the construct is associated with multiple loops.
691 bool hasMutipleLoops() const {
692 const SharingMapTy *Top = getTopOfStackOrNull();
693 return Top ? Top->HasMutipleLoops : false;
696 /// Marks current target region as one with closely nested teams
698 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
699 if (SharingMapTy *Parent = getSecondOnStackOrNull())
700 Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
702 /// Returns true, if current region has closely nested teams region.
703 bool hasInnerTeamsRegion() const {
704 return getInnerTeamsRegionLoc().isValid();
706 /// Returns location of the nested teams region (if any).
707 SourceLocation getInnerTeamsRegionLoc() const {
708 const SharingMapTy *Top = getTopOfStackOrNull();
709 return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
712 Scope *getCurScope() const {
713 const SharingMapTy *Top = getTopOfStackOrNull();
714 return Top ? Top->CurScope : nullptr;
716 SourceLocation getConstructLoc() const {
717 const SharingMapTy *Top = getTopOfStackOrNull();
718 return Top ? Top->ConstructLoc : SourceLocation();
721 /// Do the check specified in \a Check to all component lists and return true
722 /// if any issue is found.
723 bool checkMappableExprComponentListsForDecl(
724 const ValueDecl *VD, bool CurrentRegionOnly,
725 const llvm::function_ref<
726 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
737 if (CurrentRegionOnly)
742 for (; SI != SE; ++SI) {
743 auto MI = SI->MappedExprComponents.find(VD);
744 if (MI != SI->MappedExprComponents.end())
745 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
746 MI->second.Components)
747 if (Check(L, MI->second.Kind))
753 /// Do the check specified in \a Check to all component lists at a given level
754 /// and return true if any issue is found.
755 bool checkMappableExprComponentListsForDeclAtLevel(
756 const ValueDecl *VD, unsigned Level,
757 const llvm::function_ref<
758 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
761 if (getStackSize() <= Level)
764 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
765 auto MI = StackElem.MappedExprComponents.find(VD);
766 if (MI != StackElem.MappedExprComponents.end())
767 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
768 MI->second.Components)
769 if (Check(L, MI->second.Kind))
774 /// Create a new mappable expression component list associated with a given
775 /// declaration and initialize it with the provided list of components.
776 void addMappableExpressionComponents(
778 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
779 OpenMPClauseKind WhereFoundClauseKind) {
780 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
781 // Create new entry and append the new components there.
782 MEC.Components.resize(MEC.Components.size() + 1);
783 MEC.Components.back().append(Components.begin(), Components.end());
784 MEC.Kind = WhereFoundClauseKind;
787 unsigned getNestingLevel() const {
788 assert(!isStackEmpty());
789 return getStackSize() - 1;
791 void addDoacrossDependClause(OMPDependClause *C,
792 const OperatorOffsetTy &OpsOffs) {
793 SharingMapTy *Parent = getSecondOnStackOrNull();
794 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
795 Parent->DoacrossDepends.try_emplace(C, OpsOffs);
797 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
798 getDoacrossDependClauses() const {
799 const SharingMapTy &StackElem = getTopOfStack();
800 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
801 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
802 return llvm::make_range(Ref.begin(), Ref.end());
804 return llvm::make_range(StackElem.DoacrossDepends.end(),
805 StackElem.DoacrossDepends.end());
808 // Store types of classes which have been explicitly mapped
809 void addMappedClassesQualTypes(QualType QT) {
810 SharingMapTy &StackElem = getTopOfStack();
811 StackElem.MappedClassesQualTypes.insert(QT);
814 // Return set of mapped classes types
815 bool isClassPreviouslyMapped(QualType QT) const {
816 const SharingMapTy &StackElem = getTopOfStack();
817 return StackElem.MappedClassesQualTypes.count(QT) != 0;
820 /// Adds global declare target to the parent target region.
821 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
822 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
823 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
824 "Expected declare target link global.");
825 for (auto &Elem : *this) {
826 if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
827 Elem.DeclareTargetLinkVarDecls.push_back(E);
833 /// Returns the list of globals with declare target link if current directive
835 ArrayRef<DeclRefExpr *> getLinkGlobals() const {
836 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
837 "Expected target executable directive.");
838 return getTopOfStack().DeclareTargetLinkVarDecls;
842 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
843 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
846 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
847 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) ||
848 DKind == OMPD_unknown;
853 static const Expr *getExprAsWritten(const Expr *E) {
854 if (const auto *FE = dyn_cast<FullExpr>(E))
855 E = FE->getSubExpr();
857 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
858 E = MTE->GetTemporaryExpr();
860 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
861 E = Binder->getSubExpr();
863 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
864 E = ICE->getSubExprAsWritten();
865 return E->IgnoreParens();
868 static Expr *getExprAsWritten(Expr *E) {
869 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
872 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
873 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
874 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
875 D = ME->getMemberDecl();
876 const auto *VD = dyn_cast<VarDecl>(D);
877 const auto *FD = dyn_cast<FieldDecl>(D);
879 VD = VD->getCanonicalDecl();
883 FD = FD->getCanonicalDecl();
889 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
890 return const_cast<ValueDecl *>(
891 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
894 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
895 ValueDecl *D) const {
896 D = getCanonicalDecl(D);
897 auto *VD = dyn_cast<VarDecl>(D);
898 const auto *FD = dyn_cast<FieldDecl>(D);
901 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
902 // in a region but not in construct]
903 // File-scope or namespace-scope variables referenced in called routines
904 // in the region are shared unless they appear in a threadprivate
906 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
907 DVar.CKind = OMPC_shared;
909 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
910 // in a region but not in construct]
911 // Variables with static storage duration that are declared in called
912 // routines in the region are shared.
913 if (VD && VD->hasGlobalStorage())
914 DVar.CKind = OMPC_shared;
916 // Non-static data members are shared by default.
918 DVar.CKind = OMPC_shared;
923 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
924 // in a Construct, C/C++, predetermined, p.1]
925 // Variables with automatic storage duration that are declared in a scope
926 // inside the construct are private.
927 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
928 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
929 DVar.CKind = OMPC_private;
933 DVar.DKind = Iter->Directive;
934 // Explicitly specified attributes and local variables with predetermined
936 if (Iter->SharingMap.count(D)) {
937 const DSAInfo &Data = Iter->SharingMap.lookup(D);
938 DVar.RefExpr = Data.RefExpr.getPointer();
939 DVar.PrivateCopy = Data.PrivateCopy;
940 DVar.CKind = Data.Attributes;
941 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
945 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
946 // in a Construct, C/C++, implicitly determined, p.1]
947 // In a parallel or task construct, the data-sharing attributes of these
948 // variables are determined by the default clause, if present.
949 switch (Iter->DefaultAttr) {
951 DVar.CKind = OMPC_shared;
952 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
956 case DSA_unspecified:
957 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
958 // in a Construct, implicitly determined, p.2]
959 // In a parallel construct, if no default clause is present, these
960 // variables are shared.
961 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
962 if (isOpenMPParallelDirective(DVar.DKind) ||
963 isOpenMPTeamsDirective(DVar.DKind)) {
964 DVar.CKind = OMPC_shared;
968 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
969 // in a Construct, implicitly determined, p.4]
970 // In a task construct, if no default clause is present, a variable that in
971 // the enclosing context is determined to be shared by all implicit tasks
972 // bound to the current team is shared.
973 if (isOpenMPTaskingDirective(DVar.DKind)) {
975 const_iterator I = Iter, E = end();
978 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
979 // Referenced in a Construct, implicitly determined, p.6]
980 // In a task construct, if no default clause is present, a variable
981 // whose data-sharing attribute is not determined by the rules above is
983 DVarTemp = getDSA(I, D);
984 if (DVarTemp.CKind != OMPC_shared) {
985 DVar.RefExpr = nullptr;
986 DVar.CKind = OMPC_firstprivate;
989 } while (I != E && !isImplicitTaskingRegion(I->Directive));
991 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
995 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
996 // in a Construct, implicitly determined, p.3]
997 // For constructs other than task, if no default clause is present, these
998 // variables inherit their data-sharing attributes from the enclosing
1000 return getDSA(++Iter, D);
1003 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1004 const Expr *NewDE) {
1005 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1006 D = getCanonicalDecl(D);
1007 SharingMapTy &StackElem = getTopOfStack();
1008 auto It = StackElem.AlignedMap.find(D);
1009 if (It == StackElem.AlignedMap.end()) {
1010 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1011 StackElem.AlignedMap[D] = NewDE;
1014 assert(It->second && "Unexpected nullptr expr in the aligned map");
1018 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1019 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1020 D = getCanonicalDecl(D);
1021 SharingMapTy &StackElem = getTopOfStack();
1022 StackElem.LCVMap.try_emplace(
1023 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1026 const DSAStackTy::LCDeclInfo
1027 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1028 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1029 D = getCanonicalDecl(D);
1030 const SharingMapTy &StackElem = getTopOfStack();
1031 auto It = StackElem.LCVMap.find(D);
1032 if (It != StackElem.LCVMap.end())
1034 return {0, nullptr};
1037 const DSAStackTy::LCDeclInfo
1038 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1039 const SharingMapTy *Parent = getSecondOnStackOrNull();
1040 assert(Parent && "Data-sharing attributes stack is empty");
1041 D = getCanonicalDecl(D);
1042 auto It = Parent->LCVMap.find(D);
1043 if (It != Parent->LCVMap.end())
1045 return {0, nullptr};
1048 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1049 const SharingMapTy *Parent = getSecondOnStackOrNull();
1050 assert(Parent && "Data-sharing attributes stack is empty");
1051 if (Parent->LCVMap.size() < I)
1053 for (const auto &Pair : Parent->LCVMap)
1054 if (Pair.second.first == I)
1059 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1060 DeclRefExpr *PrivateCopy) {
1061 D = getCanonicalDecl(D);
1062 if (A == OMPC_threadprivate) {
1063 DSAInfo &Data = Threadprivates[D];
1064 Data.Attributes = A;
1065 Data.RefExpr.setPointer(E);
1066 Data.PrivateCopy = nullptr;
1068 DSAInfo &Data = getTopOfStack().SharingMap[D];
1069 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1070 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1071 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1072 (isLoopControlVariable(D).first && A == OMPC_private));
1073 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1074 Data.RefExpr.setInt(/*IntVal=*/true);
1077 const bool IsLastprivate =
1078 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1079 Data.Attributes = A;
1080 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1081 Data.PrivateCopy = PrivateCopy;
1083 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1084 Data.Attributes = A;
1085 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1086 Data.PrivateCopy = nullptr;
1091 /// Build a variable declaration for OpenMP loop iteration variable.
1092 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1093 StringRef Name, const AttrVec *Attrs = nullptr,
1094 DeclRefExpr *OrigRef = nullptr) {
1095 DeclContext *DC = SemaRef.CurContext;
1096 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1097 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1099 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1101 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1105 Decl->setImplicit();
1108 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1113 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1115 bool RefersToCapture = false) {
1117 D->markUsed(S.Context);
1118 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1119 SourceLocation(), D, RefersToCapture, Loc, Ty,
1123 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1124 BinaryOperatorKind BOK) {
1125 D = getCanonicalDecl(D);
1126 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1128 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1129 "Additional reduction info may be specified only for reduction items.");
1130 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1131 assert(ReductionData.ReductionRange.isInvalid() &&
1132 getTopOfStack().Directive == OMPD_taskgroup &&
1133 "Additional reduction info may be specified only once for reduction "
1135 ReductionData.set(BOK, SR);
1136 Expr *&TaskgroupReductionRef =
1137 getTopOfStack().TaskgroupReductionRef;
1138 if (!TaskgroupReductionRef) {
1139 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1140 SemaRef.Context.VoidPtrTy, ".task_red.");
1141 TaskgroupReductionRef =
1142 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1146 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1147 const Expr *ReductionRef) {
1148 D = getCanonicalDecl(D);
1149 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1151 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1152 "Additional reduction info may be specified only for reduction items.");
1153 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1154 assert(ReductionData.ReductionRange.isInvalid() &&
1155 getTopOfStack().Directive == OMPD_taskgroup &&
1156 "Additional reduction info may be specified only once for reduction "
1158 ReductionData.set(ReductionRef, SR);
1159 Expr *&TaskgroupReductionRef =
1160 getTopOfStack().TaskgroupReductionRef;
1161 if (!TaskgroupReductionRef) {
1162 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1163 SemaRef.Context.VoidPtrTy, ".task_red.");
1164 TaskgroupReductionRef =
1165 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1169 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1170 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1171 Expr *&TaskgroupDescriptor) const {
1172 D = getCanonicalDecl(D);
1173 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1174 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1175 const DSAInfo &Data = I->SharingMap.lookup(D);
1176 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1178 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1179 if (!ReductionData.ReductionOp ||
1180 ReductionData.ReductionOp.is<const Expr *>())
1181 return DSAVarData();
1182 SR = ReductionData.ReductionRange;
1183 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1184 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1185 "expression for the descriptor is not "
1187 TaskgroupDescriptor = I->TaskgroupReductionRef;
1188 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1189 Data.PrivateCopy, I->DefaultAttrLoc);
1191 return DSAVarData();
1194 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1195 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1196 Expr *&TaskgroupDescriptor) const {
1197 D = getCanonicalDecl(D);
1198 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1199 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1200 const DSAInfo &Data = I->SharingMap.lookup(D);
1201 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1203 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1204 if (!ReductionData.ReductionOp ||
1205 !ReductionData.ReductionOp.is<const Expr *>())
1206 return DSAVarData();
1207 SR = ReductionData.ReductionRange;
1208 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1209 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1210 "expression for the descriptor is not "
1212 TaskgroupDescriptor = I->TaskgroupReductionRef;
1213 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1214 Data.PrivateCopy, I->DefaultAttrLoc);
1216 return DSAVarData();
1219 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1220 D = D->getCanonicalDecl();
1221 for (const_iterator E = end(); I != E; ++I) {
1222 if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1223 isOpenMPTargetExecutionDirective(I->Directive)) {
1224 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1225 Scope *CurScope = getCurScope();
1226 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1227 CurScope = CurScope->getParent();
1228 return CurScope != TopScope;
1234 static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1235 bool AcceptIfMutable = true,
1236 bool *IsClassType = nullptr) {
1237 ASTContext &Context = SemaRef.getASTContext();
1238 Type = Type.getNonReferenceType().getCanonicalType();
1239 bool IsConstant = Type.isConstant(Context);
1240 Type = Context.getBaseElementType(Type);
1241 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1242 ? Type->getAsCXXRecordDecl()
1244 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1245 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1246 RD = CTD->getTemplatedDecl();
1249 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1250 RD->hasDefinition() && RD->hasMutableFields());
1253 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1254 QualType Type, OpenMPClauseKind CKind,
1255 SourceLocation ELoc,
1256 bool AcceptIfMutable = true,
1257 bool ListItemNotVar = false) {
1258 ASTContext &Context = SemaRef.getASTContext();
1260 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1261 unsigned Diag = ListItemNotVar
1262 ? diag::err_omp_const_list_item
1263 : IsClassType ? diag::err_omp_const_not_mutable_variable
1264 : diag::err_omp_const_variable;
1265 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1266 if (!ListItemNotVar && D) {
1267 const VarDecl *VD = dyn_cast<VarDecl>(D);
1268 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1269 VarDecl::DeclarationOnly;
1270 SemaRef.Diag(D->getLocation(),
1271 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1279 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1281 D = getCanonicalDecl(D);
1284 auto *VD = dyn_cast<VarDecl>(D);
1285 auto TI = Threadprivates.find(D);
1286 if (TI != Threadprivates.end()) {
1287 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1288 DVar.CKind = OMPC_threadprivate;
1291 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1292 DVar.RefExpr = buildDeclRefExpr(
1293 SemaRef, VD, D->getType().getNonReferenceType(),
1294 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1295 DVar.CKind = OMPC_threadprivate;
1296 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1299 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1300 // in a Construct, C/C++, predetermined, p.1]
1301 // Variables appearing in threadprivate directives are threadprivate.
1302 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1303 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1304 SemaRef.getLangOpts().OpenMPUseTLS &&
1305 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1306 (VD && VD->getStorageClass() == SC_Register &&
1307 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1308 DVar.RefExpr = buildDeclRefExpr(
1309 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1310 DVar.CKind = OMPC_threadprivate;
1311 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1314 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1315 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1316 !isLoopControlVariable(D).first) {
1317 const_iterator IterTarget =
1318 std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1319 return isOpenMPTargetExecutionDirective(Data.Directive);
1321 if (IterTarget != end()) {
1322 const_iterator ParentIterTarget = IterTarget + 1;
1323 for (const_iterator Iter = begin();
1324 Iter != ParentIterTarget; ++Iter) {
1325 if (isOpenMPLocal(VD, Iter)) {
1327 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1329 DVar.CKind = OMPC_threadprivate;
1333 if (!isClauseParsingMode() || IterTarget != begin()) {
1334 auto DSAIter = IterTarget->SharingMap.find(D);
1335 if (DSAIter != IterTarget->SharingMap.end() &&
1336 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1337 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1338 DVar.CKind = OMPC_threadprivate;
1341 const_iterator End = end();
1342 if (!SemaRef.isOpenMPCapturedByRef(
1343 D, std::distance(ParentIterTarget, End))) {
1345 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1346 IterTarget->ConstructLoc);
1347 DVar.CKind = OMPC_threadprivate;
1355 // Not in OpenMP execution region and top scope was already checked.
1358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1359 // in a Construct, C/C++, predetermined, p.4]
1360 // Static data members are shared.
1361 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1362 // in a Construct, C/C++, predetermined, p.7]
1363 // Variables with static storage duration that are declared in a scope
1364 // inside the construct are shared.
1365 if (VD && VD->isStaticDataMember()) {
1366 // Check for explicitly specified attributes.
1367 const_iterator I = begin();
1368 const_iterator EndI = end();
1369 if (FromParent && I != EndI)
1371 auto It = I->SharingMap.find(D);
1372 if (It != I->SharingMap.end()) {
1373 const DSAInfo &Data = It->getSecond();
1374 DVar.RefExpr = Data.RefExpr.getPointer();
1375 DVar.PrivateCopy = Data.PrivateCopy;
1376 DVar.CKind = Data.Attributes;
1377 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1378 DVar.DKind = I->Directive;
1382 DVar.CKind = OMPC_shared;
1386 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1387 // The predetermined shared attribute for const-qualified types having no
1388 // mutable members was removed after OpenMP 3.1.
1389 if (SemaRef.LangOpts.OpenMP <= 31) {
1390 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1391 // in a Construct, C/C++, predetermined, p.6]
1392 // Variables with const qualified type having no mutable member are
1394 if (isConstNotMutableType(SemaRef, D->getType())) {
1395 // Variables with const-qualified type having no mutable member may be
1396 // listed in a firstprivate clause, even if they are static data members.
1397 DSAVarData DVarTemp = hasInnermostDSA(
1399 [](OpenMPClauseKind C) {
1400 return C == OMPC_firstprivate || C == OMPC_shared;
1402 MatchesAlways, FromParent);
1403 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1406 DVar.CKind = OMPC_shared;
1411 // Explicitly specified attributes and local variables with predetermined
1413 const_iterator I = begin();
1414 const_iterator EndI = end();
1415 if (FromParent && I != EndI)
1417 auto It = I->SharingMap.find(D);
1418 if (It != I->SharingMap.end()) {
1419 const DSAInfo &Data = It->getSecond();
1420 DVar.RefExpr = Data.RefExpr.getPointer();
1421 DVar.PrivateCopy = Data.PrivateCopy;
1422 DVar.CKind = Data.Attributes;
1423 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1424 DVar.DKind = I->Directive;
1430 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1431 bool FromParent) const {
1432 if (isStackEmpty()) {
1434 return getDSA(I, D);
1436 D = getCanonicalDecl(D);
1437 const_iterator StartI = begin();
1438 const_iterator EndI = end();
1439 if (FromParent && StartI != EndI)
1441 return getDSA(StartI, D);
1444 const DSAStackTy::DSAVarData
1445 DSAStackTy::hasDSA(ValueDecl *D,
1446 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1447 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1448 bool FromParent) const {
1451 D = getCanonicalDecl(D);
1452 const_iterator I = begin();
1453 const_iterator EndI = end();
1454 if (FromParent && I != EndI)
1456 for (; I != EndI; ++I) {
1457 if (!DPred(I->Directive) &&
1458 !isImplicitOrExplicitTaskingRegion(I->Directive))
1460 const_iterator NewI = I;
1461 DSAVarData DVar = getDSA(NewI, D);
1462 if (I == NewI && CPred(DVar.CKind))
1468 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1469 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1470 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1471 bool FromParent) const {
1474 D = getCanonicalDecl(D);
1475 const_iterator StartI = begin();
1476 const_iterator EndI = end();
1477 if (FromParent && StartI != EndI)
1479 if (StartI == EndI || !DPred(StartI->Directive))
1481 const_iterator NewI = StartI;
1482 DSAVarData DVar = getDSA(NewI, D);
1483 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1486 bool DSAStackTy::hasExplicitDSA(
1487 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1488 unsigned Level, bool NotLastprivate) const {
1489 if (getStackSize() <= Level)
1491 D = getCanonicalDecl(D);
1492 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1493 auto I = StackElem.SharingMap.find(D);
1494 if (I != StackElem.SharingMap.end() &&
1495 I->getSecond().RefExpr.getPointer() &&
1496 CPred(I->getSecond().Attributes) &&
1497 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1499 // Check predetermined rules for the loop control variables.
1500 auto LI = StackElem.LCVMap.find(D);
1501 if (LI != StackElem.LCVMap.end())
1502 return CPred(OMPC_private);
1506 bool DSAStackTy::hasExplicitDirective(
1507 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1508 unsigned Level) const {
1509 if (getStackSize() <= Level)
1511 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1512 return DPred(StackElem.Directive);
1515 bool DSAStackTy::hasDirective(
1516 const llvm::function_ref<bool(OpenMPDirectiveKind,
1517 const DeclarationNameInfo &, SourceLocation)>
1519 bool FromParent) const {
1520 // We look only in the enclosing region.
1521 size_t Skip = FromParent ? 2 : 1;
1522 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1524 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1530 void Sema::InitDataSharingAttributesStack() {
1531 VarDataSharingAttributesStack = new DSAStackTy(*this);
1534 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1536 void Sema::pushOpenMPFunctionRegion() {
1537 DSAStack->pushFunction();
1540 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1541 DSAStack->popFunction(OldFSI);
1544 static bool isOpenMPDeviceDelayedContext(Sema &S) {
1545 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1546 "Expected OpenMP device compilation.");
1547 return !S.isInOpenMPTargetExecutionDirective() &&
1548 !S.isInOpenMPDeclareTargetContext();
1551 /// Do we know that we will eventually codegen the given function?
1552 static bool isKnownEmitted(Sema &S, FunctionDecl *FD) {
1553 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1554 "Expected OpenMP device compilation.");
1555 // Templates are emitted when they're instantiated.
1556 if (FD->isDependentContext())
1559 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1560 FD->getCanonicalDecl()))
1563 // Otherwise, the function is known-emitted if it's in our set of
1564 // known-emitted functions.
1565 return S.DeviceKnownEmittedFns.count(FD) > 0;
1568 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1570 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1571 "Expected OpenMP device compilation.");
1572 return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) &&
1573 !isKnownEmitted(*this, getCurFunctionDecl()))
1574 ? DeviceDiagBuilder::K_Deferred
1575 : DeviceDiagBuilder::K_Immediate,
1576 Loc, DiagID, getCurFunctionDecl(), *this);
1579 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) {
1580 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1581 "Expected OpenMP device compilation.");
1582 assert(Callee && "Callee may not be null.");
1583 FunctionDecl *Caller = getCurFunctionDecl();
1585 // If the caller is known-emitted, mark the callee as known-emitted.
1586 // Otherwise, mark the call in our call graph so we can traverse it later.
1587 if (!isOpenMPDeviceDelayedContext(*this) ||
1588 (Caller && isKnownEmitted(*this, Caller)))
1589 markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted);
1591 DeviceCallGraph[Caller].insert({Callee, Loc});
1594 void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1595 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1596 "OpenMP device compilation mode is expected.");
1597 QualType Ty = E->getType();
1598 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
1599 ((Ty->isFloat128Type() ||
1600 (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) &&
1601 !Context.getTargetInfo().hasFloat128Type()) ||
1602 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1603 !Context.getTargetInfo().hasInt128Type()))
1604 targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type)
1605 << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
1606 << Context.getTargetInfo().getTriple().str() << E->getSourceRange();
1609 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
1610 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1612 ASTContext &Ctx = getASTContext();
1613 bool IsByRef = true;
1615 // Find the directive that is associated with the provided scope.
1616 D = cast<ValueDecl>(D->getCanonicalDecl());
1617 QualType Ty = D->getType();
1619 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1620 // This table summarizes how a given variable should be passed to the device
1621 // given its type and the clauses where it appears. This table is based on
1622 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1623 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1625 // =========================================================================
1626 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1627 // | |(tofrom:scalar)| | pvt | | | |
1628 // =========================================================================
1629 // | scl | | | | - | | bycopy|
1630 // | scl | | - | x | - | - | bycopy|
1631 // | scl | | x | - | - | - | null |
1632 // | scl | x | | | - | | byref |
1633 // | scl | x | - | x | - | - | bycopy|
1634 // | scl | x | x | - | - | - | null |
1635 // | scl | | - | - | - | x | byref |
1636 // | scl | x | - | - | - | x | byref |
1638 // | agg | n.a. | | | - | | byref |
1639 // | agg | n.a. | - | x | - | - | byref |
1640 // | agg | n.a. | x | - | - | - | null |
1641 // | agg | n.a. | - | - | - | x | byref |
1642 // | agg | n.a. | - | - | - | x[] | byref |
1644 // | ptr | n.a. | | | - | | bycopy|
1645 // | ptr | n.a. | - | x | - | - | bycopy|
1646 // | ptr | n.a. | x | - | - | - | null |
1647 // | ptr | n.a. | - | - | - | x | byref |
1648 // | ptr | n.a. | - | - | - | x[] | bycopy|
1649 // | ptr | n.a. | - | - | x | | bycopy|
1650 // | ptr | n.a. | - | - | x | x | bycopy|
1651 // | ptr | n.a. | - | - | x | x[] | bycopy|
1652 // =========================================================================
1658 // - - invalid in this combination
1659 // [] - mapped with an array section
1660 // byref - should be mapped by reference
1661 // byval - should be mapped by value
1662 // null - initialize a local variable to null on the device
1665 // - All scalar declarations that show up in a map clause have to be passed
1666 // by reference, because they may have been mapped in the enclosing data
1668 // - If the scalar value does not fit the size of uintptr, it has to be
1669 // passed by reference, regardless the result in the table above.
1670 // - For pointers mapped by value that have either an implicit map or an
1671 // array section, the runtime library may pass the NULL value to the
1672 // device instead of the value passed to it by the compiler.
1674 if (Ty->isReferenceType())
1675 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1677 // Locate map clauses and see if the variable being captured is referred to
1678 // in any of those clauses. Here we only care about variables, not fields,
1679 // because fields are part of aggregates.
1680 bool IsVariableUsedInMapClause = false;
1681 bool IsVariableAssociatedWithSection = false;
1683 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1685 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1686 OMPClauseMappableExprCommon::MappableExprComponentListRef
1688 OpenMPClauseKind WhereFoundClauseKind) {
1689 // Only the map clause information influences how a variable is
1690 // captured. E.g. is_device_ptr does not require changing the default
1692 if (WhereFoundClauseKind != OMPC_map)
1695 auto EI = MapExprComponents.rbegin();
1696 auto EE = MapExprComponents.rend();
1698 assert(EI != EE && "Invalid map expression!");
1700 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1701 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1707 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1708 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1709 isa<MemberExpr>(EI->getAssociatedExpression())) {
1710 IsVariableAssociatedWithSection = true;
1711 // There is nothing more we need to know about this variable.
1715 // Keep looking for more map info.
1719 if (IsVariableUsedInMapClause) {
1720 // If variable is identified in a map clause it is always captured by
1721 // reference except if it is a pointer that is dereferenced somehow.
1722 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1724 // By default, all the data that has a scalar type is mapped by copy
1725 // (except for reduction variables).
1727 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1728 !Ty->isAnyPointerType()) ||
1729 !Ty->isScalarType() ||
1730 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1731 DSAStack->hasExplicitDSA(
1732 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
1736 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1738 !DSAStack->hasExplicitDSA(
1740 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1741 Level, /*NotLastprivate=*/true) &&
1742 // If the variable is artificial and must be captured by value - try to
1743 // capture by value.
1744 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1745 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
1748 // When passing data by copy, we need to make sure it fits the uintptr size
1749 // and alignment, because the runtime library only deals with uintptr types.
1750 // If it does not fit the uintptr size, we need to pass the data by reference
1753 (Ctx.getTypeSizeInChars(Ty) >
1754 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1755 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1762 unsigned Sema::getOpenMPNestingLevel() const {
1763 assert(getLangOpts().OpenMP);
1764 return DSAStack->getNestingLevel();
1767 bool Sema::isInOpenMPTargetExecutionDirective() const {
1768 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1769 !DSAStack->isClauseParsingMode()) ||
1770 DSAStack->hasDirective(
1771 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1772 SourceLocation) -> bool {
1773 return isOpenMPTargetExecutionDirective(K);
1778 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
1780 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1781 D = getCanonicalDecl(D);
1783 // If we want to determine whether the variable should be captured from the
1784 // perspective of the current capturing scope, and we've already left all the
1785 // capturing scopes of the top directive on the stack, check from the
1786 // perspective of its parent directive (if any) instead.
1787 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
1788 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
1790 // If we are attempting to capture a global variable in a directive with
1791 // 'target' we return true so that this global is also mapped to the device.
1793 auto *VD = dyn_cast<VarDecl>(D);
1794 if (VD && !VD->hasLocalStorage() &&
1795 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1796 if (isInOpenMPDeclareTargetContext()) {
1797 // Try to mark variable as declare target if it is used in capturing
1799 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1800 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
1802 } else if (isInOpenMPTargetExecutionDirective()) {
1803 // If the declaration is enclosed in a 'declare target' directive,
1804 // then it should not be captured.
1806 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1812 if (CheckScopeInfo) {
1813 bool OpenMPFound = false;
1814 for (unsigned I = StopAt + 1; I > 0; --I) {
1815 FunctionScopeInfo *FSI = FunctionScopes[I - 1];
1816 if(!isa<CapturingScopeInfo>(FSI))
1818 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
1819 if (RSI->CapRegionKind == CR_OpenMP) {
1828 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1829 (!DSAStack->isClauseParsingMode() ||
1830 DSAStack->getParentDirective() != OMPD_unknown)) {
1831 auto &&Info = DSAStack->isLoopControlVariable(D);
1833 (VD && VD->hasLocalStorage() &&
1834 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
1835 (VD && DSAStack->isForceVarCapturing()))
1836 return VD ? VD : Info.second;
1837 DSAStackTy::DSAVarData DVarPrivate =
1838 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1839 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1840 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1841 // Threadprivate variables must not be captured.
1842 if (isOpenMPThreadPrivate(DVarPrivate.CKind))
1844 // The variable is not private or it is the variable in the directive with
1845 // default(none) clause and not used in any clause.
1846 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1847 [](OpenMPDirectiveKind) { return true; },
1848 DSAStack->isClauseParsingMode());
1849 if (DVarPrivate.CKind != OMPC_unknown ||
1850 (VD && DSAStack->getDefaultDSA() == DSA_none))
1851 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1856 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1857 unsigned Level) const {
1858 SmallVector<OpenMPDirectiveKind, 4> Regions;
1859 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1860 FunctionScopesIndex -= Regions.size();
1863 void Sema::startOpenMPLoop() {
1864 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1865 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1866 DSAStack->loopInit();
1869 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
1870 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1871 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1872 if (DSAStack->getAssociatedLoops() > 0 &&
1873 !DSAStack->isLoopStarted()) {
1874 DSAStack->resetPossibleLoopCounter(D);
1875 DSAStack->loopStart();
1878 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1879 DSAStack->isLoopControlVariable(D).first) &&
1880 !DSAStack->hasExplicitDSA(
1881 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1882 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1885 if (const auto *VD = dyn_cast<VarDecl>(D)) {
1886 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
1887 DSAStack->isForceVarCapturing() &&
1888 !DSAStack->hasExplicitDSA(
1889 D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level))
1892 return DSAStack->hasExplicitDSA(
1893 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
1894 (DSAStack->isClauseParsingMode() &&
1895 DSAStack->getClauseParsingMode() == OMPC_private) ||
1896 // Consider taskgroup reduction descriptor variable a private to avoid
1897 // possible capture in the region.
1898 (DSAStack->hasExplicitDirective(
1899 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1901 DSAStack->isTaskgroupReductionRef(D, Level));
1904 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1906 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1907 D = getCanonicalDecl(D);
1908 OpenMPClauseKind OMPC = OMPC_unknown;
1909 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1910 const unsigned NewLevel = I - 1;
1911 if (DSAStack->hasExplicitDSA(D,
1912 [&OMPC](const OpenMPClauseKind K) {
1913 if (isOpenMPPrivate(K)) {
1921 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1923 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1924 OpenMPClauseKind) { return true; })) {
1928 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1931 if (D->getType()->isScalarType() &&
1932 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1933 DefaultMapAttributes::DMA_tofrom_scalar)
1934 OMPC = OMPC_firstprivate;
1938 if (OMPC != OMPC_unknown)
1939 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1942 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1943 unsigned Level) const {
1944 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1945 // Return true if the current level is no longer enclosed in a target region.
1947 const auto *VD = dyn_cast<VarDecl>(D);
1948 return VD && !VD->hasLocalStorage() &&
1949 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1953 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1955 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1956 const DeclarationNameInfo &DirName,
1957 Scope *CurScope, SourceLocation Loc) {
1958 DSAStack->push(DKind, DirName, CurScope, Loc);
1959 PushExpressionEvaluationContext(
1960 ExpressionEvaluationContext::PotentiallyEvaluated);
1963 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1964 DSAStack->setClauseParsingMode(K);
1967 void Sema::EndOpenMPClause() {
1968 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1971 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
1972 ArrayRef<OMPClause *> Clauses);
1974 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1975 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1976 // A variable of class type (or array thereof) that appears in a lastprivate
1977 // clause requires an accessible, unambiguous default constructor for the
1978 // class type, unless the list item is also specified in a firstprivate
1980 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1981 for (OMPClause *C : D->clauses()) {
1982 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1983 SmallVector<Expr *, 8> PrivateCopies;
1984 for (Expr *DE : Clause->varlists()) {
1985 if (DE->isValueDependent() || DE->isTypeDependent()) {
1986 PrivateCopies.push_back(nullptr);
1989 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1990 auto *VD = cast<VarDecl>(DRE->getDecl());
1991 QualType Type = VD->getType().getNonReferenceType();
1992 const DSAStackTy::DSAVarData DVar =
1993 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1994 if (DVar.CKind == OMPC_lastprivate) {
1995 // Generate helper private variable and initialize it with the
1996 // default value. The address of the original variable is replaced
1997 // by the address of the new private variable in CodeGen. This new
1998 // variable is not added to IdResolver, so the code in the OpenMP
1999 // region uses original variable for proper diagnostics.
2000 VarDecl *VDPrivate = buildVarDecl(
2001 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2002 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
2003 ActOnUninitializedDecl(VDPrivate);
2004 if (VDPrivate->isInvalidDecl()) {
2005 PrivateCopies.push_back(nullptr);
2008 PrivateCopies.push_back(buildDeclRefExpr(
2009 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2011 // The variable is also a firstprivate, so initialization sequence
2012 // for private copy is generated already.
2013 PrivateCopies.push_back(nullptr);
2016 Clause->setPrivateCopies(PrivateCopies);
2019 // Check allocate clauses.
2020 if (!CurContext->isDependentContext())
2021 checkAllocateClauses(*this, DSAStack, D->clauses());
2025 DiscardCleanupsInEvaluationContext();
2026 PopExpressionEvaluationContext();
2029 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2030 Expr *NumIterations, Sema &SemaRef,
2031 Scope *S, DSAStackTy *Stack);
2035 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2040 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2041 bool ValidateCandidate(const TypoCorrection &Candidate) override {
2042 NamedDecl *ND = Candidate.getCorrectionDecl();
2043 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2044 return VD->hasGlobalStorage() &&
2045 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2046 SemaRef.getCurScope());
2051 std::unique_ptr<CorrectionCandidateCallback> clone() override {
2052 return llvm::make_unique<VarDeclFilterCCC>(*this);
2057 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2062 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2063 bool ValidateCandidate(const TypoCorrection &Candidate) override {
2064 NamedDecl *ND = Candidate.getCorrectionDecl();
2065 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2066 isa<FunctionDecl>(ND))) {
2067 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2068 SemaRef.getCurScope());
2073 std::unique_ptr<CorrectionCandidateCallback> clone() override {
2074 return llvm::make_unique<VarOrFuncDeclFilterCCC>(*this);
2080 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2081 CXXScopeSpec &ScopeSpec,
2082 const DeclarationNameInfo &Id,
2083 OpenMPDirectiveKind Kind) {
2084 LookupResult Lookup(*this, Id, LookupOrdinaryName);
2085 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2087 if (Lookup.isAmbiguous())
2091 if (!Lookup.isSingleResult()) {
2092 VarDeclFilterCCC CCC(*this);
2093 if (TypoCorrection Corrected =
2094 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2095 CTK_ErrorRecovery)) {
2096 diagnoseTypo(Corrected,
2097 PDiag(Lookup.empty()
2098 ? diag::err_undeclared_var_use_suggest
2099 : diag::err_omp_expected_var_arg_suggest)
2101 VD = Corrected.getCorrectionDeclAs<VarDecl>();
2103 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2104 : diag::err_omp_expected_var_arg)
2108 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2109 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2110 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2113 Lookup.suppressDiagnostics();
2115 // OpenMP [2.9.2, Syntax, C/C++]
2116 // Variables must be file-scope, namespace-scope, or static block-scope.
2117 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
2118 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2119 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2121 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2122 Diag(VD->getLocation(),
2123 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2128 VarDecl *CanonicalVD = VD->getCanonicalDecl();
2129 NamedDecl *ND = CanonicalVD;
2130 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2131 // A threadprivate directive for file-scope variables must appear outside
2132 // any definition or declaration.
2133 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2134 !getCurLexicalContext()->isTranslationUnit()) {
2135 Diag(Id.getLoc(), diag::err_omp_var_scope)
2136 << getOpenMPDirectiveName(Kind) << VD;
2138 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2139 Diag(VD->getLocation(),
2140 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2144 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2145 // A threadprivate directive for static class member variables must appear
2146 // in the class definition, in the same scope in which the member
2147 // variables are declared.
2148 if (CanonicalVD->isStaticDataMember() &&
2149 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2150 Diag(Id.getLoc(), diag::err_omp_var_scope)
2151 << getOpenMPDirectiveName(Kind) << VD;
2153 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2154 Diag(VD->getLocation(),
2155 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2159 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2160 // A threadprivate directive for namespace-scope variables must appear
2161 // outside any definition or declaration other than the namespace
2162 // definition itself.
2163 if (CanonicalVD->getDeclContext()->isNamespace() &&
2164 (!getCurLexicalContext()->isFileContext() ||
2165 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2166 Diag(Id.getLoc(), diag::err_omp_var_scope)
2167 << getOpenMPDirectiveName(Kind) << VD;
2169 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2170 Diag(VD->getLocation(),
2171 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2175 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2176 // A threadprivate directive for static block-scope variables must appear
2177 // in the scope of the variable and not in a nested scope.
2178 if (CanonicalVD->isLocalVarDecl() && CurScope &&
2179 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2180 Diag(Id.getLoc(), diag::err_omp_var_scope)
2181 << getOpenMPDirectiveName(Kind) << VD;
2183 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2184 Diag(VD->getLocation(),
2185 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2190 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2191 // A threadprivate directive must lexically precede all references to any
2192 // of the variables in its list.
2193 if (Kind == OMPD_threadprivate && VD->isUsed() &&
2194 !DSAStack->isThreadPrivate(VD)) {
2195 Diag(Id.getLoc(), diag::err_omp_var_used)
2196 << getOpenMPDirectiveName(Kind) << VD;
2200 QualType ExprType = VD->getType().getNonReferenceType();
2201 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2202 SourceLocation(), VD,
2203 /*RefersToEnclosingVariableOrCapture=*/false,
2204 Id.getLoc(), ExprType, VK_LValue);
2207 Sema::DeclGroupPtrTy
2208 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2209 ArrayRef<Expr *> VarList) {
2210 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2211 CurContext->addDecl(D);
2212 return DeclGroupPtrTy::make(DeclGroupRef(D));
2218 class LocalVarRefChecker final
2219 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2223 bool VisitDeclRefExpr(const DeclRefExpr *E) {
2224 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2225 if (VD->hasLocalStorage()) {
2226 SemaRef.Diag(E->getBeginLoc(),
2227 diag::err_omp_local_var_in_threadprivate_init)
2228 << E->getSourceRange();
2229 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2230 << VD << VD->getSourceRange();
2236 bool VisitStmt(const Stmt *S) {
2237 for (const Stmt *Child : S->children()) {
2238 if (Child && Visit(Child))
2243 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2247 OMPThreadPrivateDecl *
2248 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2249 SmallVector<Expr *, 8> Vars;
2250 for (Expr *RefExpr : VarList) {
2251 auto *DE = cast<DeclRefExpr>(RefExpr);
2252 auto *VD = cast<VarDecl>(DE->getDecl());
2253 SourceLocation ILoc = DE->getExprLoc();
2255 // Mark variable as used.
2256 VD->setReferenced();
2257 VD->markUsed(Context);
2259 QualType QType = VD->getType();
2260 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2261 // It will be analyzed later.
2266 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2267 // A threadprivate variable must not have an incomplete type.
2268 if (RequireCompleteType(ILoc, VD->getType(),
2269 diag::err_omp_threadprivate_incomplete_type)) {
2273 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2274 // A threadprivate variable must not have a reference type.
2275 if (VD->getType()->isReferenceType()) {
2276 Diag(ILoc, diag::err_omp_ref_type_arg)
2277 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2279 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2280 Diag(VD->getLocation(),
2281 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2286 // Check if this is a TLS variable. If TLS is not being supported, produce
2287 // the corresponding diagnostic.
2288 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2289 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2290 getLangOpts().OpenMPUseTLS &&
2291 getASTContext().getTargetInfo().isTLSSupported())) ||
2292 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2293 !VD->isLocalVarDecl())) {
2294 Diag(ILoc, diag::err_omp_var_thread_local)
2295 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
2297 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2298 Diag(VD->getLocation(),
2299 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2304 // Check if initial value of threadprivate variable reference variable with
2305 // local storage (it is not supported by runtime).
2306 if (const Expr *Init = VD->getAnyInitializer()) {
2307 LocalVarRefChecker Checker(*this);
2308 if (Checker.Visit(Init))
2312 Vars.push_back(RefExpr);
2313 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
2314 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2315 Context, SourceRange(Loc, Loc)));
2316 if (ASTMutationListener *ML = Context.getASTMutationListener())
2317 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
2319 OMPThreadPrivateDecl *D = nullptr;
2320 if (!Vars.empty()) {
2321 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2323 D->setAccess(AS_public);
2328 static OMPAllocateDeclAttr::AllocatorTypeTy
2329 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2331 return OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2332 if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2333 Allocator->isInstantiationDependent() ||
2334 Allocator->containsUnexpandedParameterPack())
2335 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2336 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2337 const Expr *AE = Allocator->IgnoreParenImpCasts();
2338 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2339 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
2340 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
2341 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
2342 llvm::FoldingSetNodeID AEId, DAEId;
2343 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2344 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2345 if (AEId == DAEId) {
2346 AllocatorKindRes = AllocatorKind;
2350 return AllocatorKindRes;
2353 static bool checkPreviousOMPAllocateAttribute(
2354 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2355 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2356 if (!VD->hasAttr<OMPAllocateDeclAttr>())
2358 const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2359 Expr *PrevAllocator = A->getAllocator();
2360 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2361 getAllocatorKind(S, Stack, PrevAllocator);
2362 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2363 if (AllocatorsMatch &&
2364 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
2365 Allocator && PrevAllocator) {
2366 const Expr *AE = Allocator->IgnoreParenImpCasts();
2367 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2368 llvm::FoldingSetNodeID AEId, PAEId;
2369 AE->Profile(AEId, S.Context, /*Canonical=*/true);
2370 PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2371 AllocatorsMatch = AEId == PAEId;
2373 if (!AllocatorsMatch) {
2374 SmallString<256> AllocatorBuffer;
2375 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2377 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2378 SmallString<256> PrevAllocatorBuffer;
2379 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2381 PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2382 S.getPrintingPolicy());
2384 SourceLocation AllocatorLoc =
2385 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
2386 SourceRange AllocatorRange =
2387 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
2388 SourceLocation PrevAllocatorLoc =
2389 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
2390 SourceRange PrevAllocatorRange =
2391 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
2392 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2393 << (Allocator ? 1 : 0) << AllocatorStream.str()
2394 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
2396 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
2397 << PrevAllocatorRange;
2404 applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
2405 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
2406 Expr *Allocator, SourceRange SR) {
2407 if (VD->hasAttr<OMPAllocateDeclAttr>())
2410 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2411 Allocator->isInstantiationDependent() ||
2412 Allocator->containsUnexpandedParameterPack()))
2414 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
2417 if (ASTMutationListener *ML = S.Context.getASTMutationListener())
2418 ML->DeclarationMarkedOpenMPAllocate(VD, A);
2421 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
2422 SourceLocation Loc, ArrayRef<Expr *> VarList,
2423 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2424 assert(Clauses.size() <= 1 && "Expected at most one clause.");
2425 Expr *Allocator = nullptr;
2426 if (Clauses.empty()) {
2427 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
2428 // allocate directives that appear in a target region must specify an
2429 // allocator clause unless a requires directive with the dynamic_allocators
2430 // clause is present in the same compilation unit.
2431 if (LangOpts.OpenMPIsDevice &&
2432 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
2433 targetDiag(Loc, diag::err_expected_allocator_clause);
2435 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
2437 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
2438 getAllocatorKind(*this, DSAStack, Allocator);
2439 SmallVector<Expr *, 8> Vars;
2440 for (Expr *RefExpr : VarList) {
2441 auto *DE = cast<DeclRefExpr>(RefExpr);
2442 auto *VD = cast<VarDecl>(DE->getDecl());
2444 // Check if this is a TLS variable or global register.
2445 if (VD->getTLSKind() != VarDecl::TLS_None ||
2446 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
2447 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2448 !VD->isLocalVarDecl()))
2451 // If the used several times in the allocate directive, the same allocator
2453 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
2454 AllocatorKind, Allocator))
2457 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
2458 // If a list item has a static storage type, the allocator expression in the
2459 // allocator clause must be a constant expression that evaluates to one of
2460 // the predefined memory allocator values.
2461 if (Allocator && VD->hasGlobalStorage()) {
2462 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
2463 Diag(Allocator->getExprLoc(),
2464 diag::err_omp_expected_predefined_allocator)
2465 << Allocator->getSourceRange();
2466 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2467 VarDecl::DeclarationOnly;
2468 Diag(VD->getLocation(),
2469 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2475 Vars.push_back(RefExpr);
2476 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
2477 DE->getSourceRange());
2482 Owner = getCurLexicalContext();
2483 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
2484 D->setAccess(AS_public);
2486 return DeclGroupPtrTy::make(DeclGroupRef(D));
2489 Sema::DeclGroupPtrTy
2490 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2491 ArrayRef<OMPClause *> ClauseList) {
2492 OMPRequiresDecl *D = nullptr;
2493 if (!CurContext->isFileContext()) {
2494 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2496 D = CheckOMPRequiresDecl(Loc, ClauseList);
2498 CurContext->addDecl(D);
2499 DSAStack->addRequiresDecl(D);
2502 return DeclGroupPtrTy::make(DeclGroupRef(D));
2505 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2506 ArrayRef<OMPClause *> ClauseList) {
2507 /// For target specific clauses, the requires directive cannot be
2508 /// specified after the handling of any of the target regions in the
2509 /// current compilation unit.
2510 ArrayRef<SourceLocation> TargetLocations =
2511 DSAStack->getEncounteredTargetLocs();
2512 if (!TargetLocations.empty()) {
2513 for (const OMPClause *CNew : ClauseList) {
2514 // Check if any of the requires clauses affect target regions.
2515 if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
2516 isa<OMPUnifiedAddressClause>(CNew) ||
2517 isa<OMPReverseOffloadClause>(CNew) ||
2518 isa<OMPDynamicAllocatorsClause>(CNew)) {
2519 Diag(Loc, diag::err_omp_target_before_requires)
2520 << getOpenMPClauseName(CNew->getClauseKind());
2521 for (SourceLocation TargetLoc : TargetLocations) {
2522 Diag(TargetLoc, diag::note_omp_requires_encountered_target);
2528 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2529 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2534 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2536 const DSAStackTy::DSAVarData &DVar,
2537 bool IsLoopIterVar = false) {
2539 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2540 << getOpenMPClauseName(DVar.CKind);
2544 PDSA_StaticMemberShared,
2545 PDSA_StaticLocalVarShared,
2546 PDSA_LoopIterVarPrivate,
2547 PDSA_LoopIterVarLinear,
2548 PDSA_LoopIterVarLastprivate,
2549 PDSA_ConstVarShared,
2550 PDSA_GlobalVarShared,
2551 PDSA_TaskVarFirstprivate,
2552 PDSA_LocalVarPrivate,
2554 } Reason = PDSA_Implicit;
2555 bool ReportHint = false;
2556 auto ReportLoc = D->getLocation();
2557 auto *VD = dyn_cast<VarDecl>(D);
2558 if (IsLoopIterVar) {
2559 if (DVar.CKind == OMPC_private)
2560 Reason = PDSA_LoopIterVarPrivate;
2561 else if (DVar.CKind == OMPC_lastprivate)
2562 Reason = PDSA_LoopIterVarLastprivate;
2564 Reason = PDSA_LoopIterVarLinear;
2565 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2566 DVar.CKind == OMPC_firstprivate) {
2567 Reason = PDSA_TaskVarFirstprivate;
2568 ReportLoc = DVar.ImplicitDSALoc;
2569 } else if (VD && VD->isStaticLocal())
2570 Reason = PDSA_StaticLocalVarShared;
2571 else if (VD && VD->isStaticDataMember())
2572 Reason = PDSA_StaticMemberShared;
2573 else if (VD && VD->isFileVarDecl())
2574 Reason = PDSA_GlobalVarShared;
2575 else if (D->getType().isConstant(SemaRef.getASTContext()))
2576 Reason = PDSA_ConstVarShared;
2577 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
2579 Reason = PDSA_LocalVarPrivate;
2581 if (Reason != PDSA_Implicit) {
2582 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
2583 << Reason << ReportHint
2584 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2585 } else if (DVar.ImplicitDSALoc.isValid()) {
2586 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2587 << getOpenMPClauseName(DVar.CKind);
2592 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
2595 bool ErrorFound = false;
2596 CapturedStmt *CS = nullptr;
2597 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2598 llvm::SmallVector<Expr *, 4> ImplicitMap;
2599 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2600 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
2602 void VisitSubCaptures(OMPExecutableDirective *S) {
2603 // Check implicitly captured variables.
2604 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2606 visitSubCaptures(S->getInnermostCapturedStmt());
2610 void VisitDeclRefExpr(DeclRefExpr *E) {
2611 if (E->isTypeDependent() || E->isValueDependent() ||
2612 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2614 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2615 // Check the datasharing rules for the expressions in the clauses.
2617 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
2618 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
2619 Visit(CED->getInit());
2622 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
2623 // Do not analyze internal variables and do not enclose them into
2624 // implicit clauses.
2626 VD = VD->getCanonicalDecl();
2627 // Skip internally declared variables.
2628 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD))
2631 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
2632 // Check if the variable has explicit DSA set and stop analysis if it so.
2633 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
2636 // Skip internally declared static variables.
2637 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2638 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2639 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
2640 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
2641 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
2644 SourceLocation ELoc = E->getExprLoc();
2645 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2646 // The default(none) clause requires that each variable that is referenced
2647 // in the construct, and does not have a predetermined data-sharing
2648 // attribute, must have its data-sharing attribute explicitly determined
2649 // by being listed in a data-sharing attribute clause.
2650 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
2651 isImplicitOrExplicitTaskingRegion(DKind) &&
2652 VarsWithInheritedDSA.count(VD) == 0) {
2653 VarsWithInheritedDSA[VD] = E;
2657 if (isOpenMPTargetExecutionDirective(DKind) &&
2658 !Stack->isLoopControlVariable(VD).first) {
2659 if (!Stack->checkMappableExprComponentListsForDecl(
2660 VD, /*CurrentRegionOnly=*/true,
2661 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2664 // Variable is used if it has been marked as an array, array
2665 // section or the variable iself.
2666 return StackComponents.size() == 1 ||
2668 std::next(StackComponents.rbegin()),
2669 StackComponents.rend(),
2670 [](const OMPClauseMappableExprCommon::
2671 MappableComponent &MC) {
2672 return MC.getAssociatedDeclaration() ==
2674 (isa<OMPArraySectionExpr>(
2675 MC.getAssociatedExpression()) ||
2676 isa<ArraySubscriptExpr>(
2677 MC.getAssociatedExpression()));
2680 bool IsFirstprivate = false;
2681 // By default lambdas are captured as firstprivates.
2682 if (const auto *RD =
2683 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
2684 IsFirstprivate = RD->isLambda();
2687 (VD->getType().getNonReferenceType()->isScalarType() &&
2688 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
2690 ImplicitFirstprivate.emplace_back(E);
2692 ImplicitMap.emplace_back(E);
2697 // OpenMP [2.9.3.6, Restrictions, p.2]
2698 // A list item that appears in a reduction clause of the innermost
2699 // enclosing worksharing or parallel construct may not be accessed in an
2701 DVar = Stack->hasInnermostDSA(
2702 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2703 [](OpenMPDirectiveKind K) {
2704 return isOpenMPParallelDirective(K) ||
2705 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2707 /*FromParent=*/true);
2708 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2710 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2711 reportOriginalDsa(SemaRef, Stack, VD, DVar);
2715 // Define implicit data-sharing attributes for task.
2716 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
2717 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2718 !Stack->isLoopControlVariable(VD).first) {
2719 ImplicitFirstprivate.push_back(E);
2723 // Store implicitly used globals with declare target link for parent
2725 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
2726 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2727 Stack->addToParentTargetRegionLinkGlobals(E);
2732 void VisitMemberExpr(MemberExpr *E) {
2733 if (E->isTypeDependent() || E->isValueDependent() ||
2734 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2736 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2737 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2738 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
2741 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
2742 // Check if the variable has explicit DSA set and stop analysis if it
2744 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2747 if (isOpenMPTargetExecutionDirective(DKind) &&
2748 !Stack->isLoopControlVariable(FD).first &&
2749 !Stack->checkMappableExprComponentListsForDecl(
2750 FD, /*CurrentRegionOnly=*/true,
2751 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2754 return isa<CXXThisExpr>(
2756 StackComponents.back().getAssociatedExpression())
2760 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2761 // A bit-field cannot appear in a map clause.
2763 if (FD->isBitField())
2766 // Check to see if the member expression is referencing a class that
2767 // has already been explicitly mapped
2768 if (Stack->isClassPreviouslyMapped(TE->getType()))
2771 ImplicitMap.emplace_back(E);
2775 SourceLocation ELoc = E->getExprLoc();
2776 // OpenMP [2.9.3.6, Restrictions, p.2]
2777 // A list item that appears in a reduction clause of the innermost
2778 // enclosing worksharing or parallel construct may not be accessed in
2779 // an explicit task.
2780 DVar = Stack->hasInnermostDSA(
2781 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2782 [](OpenMPDirectiveKind K) {
2783 return isOpenMPParallelDirective(K) ||
2784 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2786 /*FromParent=*/true);
2787 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2789 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2790 reportOriginalDsa(SemaRef, Stack, FD, DVar);
2794 // Define implicit data-sharing attributes for task.
2795 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
2796 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2797 !Stack->isLoopControlVariable(FD).first) {
2798 // Check if there is a captured expression for the current field in the
2799 // region. Do not mark it as firstprivate unless there is no captured
2801 // TODO: try to make it firstprivate.
2802 if (DVar.CKind != OMPC_unknown)
2803 ImplicitFirstprivate.push_back(E);
2807 if (isOpenMPTargetExecutionDirective(DKind)) {
2808 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
2809 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
2810 /*NoDiagnose=*/true))
2812 const auto *VD = cast<ValueDecl>(
2813 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2814 if (!Stack->checkMappableExprComponentListsForDecl(
2815 VD, /*CurrentRegionOnly=*/true,
2817 OMPClauseMappableExprCommon::MappableExprComponentListRef
2820 auto CCI = CurComponents.rbegin();
2821 auto CCE = CurComponents.rend();
2822 for (const auto &SC : llvm::reverse(StackComponents)) {
2823 // Do both expressions have the same kind?
2824 if (CCI->getAssociatedExpression()->getStmtClass() !=
2825 SC.getAssociatedExpression()->getStmtClass())
2826 if (!(isa<OMPArraySectionExpr>(
2827 SC.getAssociatedExpression()) &&
2828 isa<ArraySubscriptExpr>(
2829 CCI->getAssociatedExpression())))
2832 const Decl *CCD = CCI->getAssociatedDeclaration();
2833 const Decl *SCD = SC.getAssociatedDeclaration();
2834 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2835 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2838 std::advance(CCI, 1);
2844 Visit(E->getBase());
2847 Visit(E->getBase());
2850 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
2851 for (OMPClause *C : S->clauses()) {
2852 // Skip analysis of arguments of implicitly defined firstprivate clause
2853 // for task|target directives.
2854 // Skip analysis of arguments of implicitly defined map clause for target
2856 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2858 for (Stmt *CC : C->children()) {
2864 // Check implicitly captured variables.
2865 VisitSubCaptures(S);
2867 void VisitStmt(Stmt *S) {
2868 for (Stmt *C : S->children()) {
2870 // Check implicitly captured variables in the task-based directives to
2871 // check if they must be firstprivatized.
2877 void visitSubCaptures(CapturedStmt *S) {
2878 for (const CapturedStmt::Capture &Cap : S->captures()) {
2879 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
2881 VarDecl *VD = Cap.getCapturedVar();
2882 // Do not try to map the variable if it or its sub-component was mapped
2884 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2885 Stack->checkMappableExprComponentListsForDecl(
2886 VD, /*CurrentRegionOnly=*/true,
2887 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2888 OpenMPClauseKind) { return true; }))
2890 DeclRefExpr *DRE = buildDeclRefExpr(
2891 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2892 Cap.getLocation(), /*RefersToCapture=*/true);
2896 bool isErrorFound() const { return ErrorFound; }
2897 ArrayRef<Expr *> getImplicitFirstprivate() const {
2898 return ImplicitFirstprivate;
2900 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
2901 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
2902 return VarsWithInheritedDSA;
2905 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2906 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
2907 // Process declare target link variables for the target directives.
2908 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
2909 for (DeclRefExpr *E : Stack->getLinkGlobals())
2916 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
2919 case OMPD_parallel_for:
2920 case OMPD_parallel_for_simd:
2921 case OMPD_parallel_sections:
2923 case OMPD_teams_distribute:
2924 case OMPD_teams_distribute_simd: {
2925 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2926 QualType KmpInt32PtrTy =
2927 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2928 Sema::CapturedParamNameType Params[] = {
2929 std::make_pair(".global_tid.", KmpInt32PtrTy),
2930 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2931 std::make_pair(StringRef(), QualType()) // __context with shared vars
2933 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2937 case OMPD_target_teams:
2938 case OMPD_target_parallel:
2939 case OMPD_target_parallel_for:
2940 case OMPD_target_parallel_for_simd:
2941 case OMPD_target_teams_distribute:
2942 case OMPD_target_teams_distribute_simd: {
2943 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2944 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2945 QualType KmpInt32PtrTy =
2946 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2947 QualType Args[] = {VoidPtrTy};
2948 FunctionProtoType::ExtProtoInfo EPI;
2949 EPI.Variadic = true;
2950 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2951 Sema::CapturedParamNameType Params[] = {
2952 std::make_pair(".global_tid.", KmpInt32Ty),
2953 std::make_pair(".part_id.", KmpInt32PtrTy),
2954 std::make_pair(".privates.", VoidPtrTy),
2957 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2958 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2959 std::make_pair(StringRef(), QualType()) // __context with shared vars
2961 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2963 // Mark this captured region as inlined, because we don't use outlined
2964 // function directly.
2965 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2966 AlwaysInlineAttr::CreateImplicit(
2967 Context, AlwaysInlineAttr::Keyword_forceinline));
2968 Sema::CapturedParamNameType ParamsTarget[] = {
2969 std::make_pair(StringRef(), QualType()) // __context with shared vars
2971 // Start a captured region for 'target' with no implicit parameters.
2972 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2974 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
2975 std::make_pair(".global_tid.", KmpInt32PtrTy),
2976 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2977 std::make_pair(StringRef(), QualType()) // __context with shared vars
2979 // Start a captured region for 'teams' or 'parallel'. Both regions have
2980 // the same implicit parameters.
2981 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2982 ParamsTeamsOrParallel);
2986 case OMPD_target_simd: {
2987 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2988 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2989 QualType KmpInt32PtrTy =
2990 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2991 QualType Args[] = {VoidPtrTy};
2992 FunctionProtoType::ExtProtoInfo EPI;
2993 EPI.Variadic = true;
2994 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2995 Sema::CapturedParamNameType Params[] = {
2996 std::make_pair(".global_tid.", KmpInt32Ty),
2997 std::make_pair(".part_id.", KmpInt32PtrTy),
2998 std::make_pair(".privates.", VoidPtrTy),
3001 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3002 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3003 std::make_pair(StringRef(), QualType()) // __context with shared vars
3005 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3007 // Mark this captured region as inlined, because we don't use outlined
3008 // function directly.
3009 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3010 AlwaysInlineAttr::CreateImplicit(
3011 Context, AlwaysInlineAttr::Keyword_forceinline));
3012 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3013 std::make_pair(StringRef(), QualType()));
3024 case OMPD_taskgroup:
3025 case OMPD_distribute:
3026 case OMPD_distribute_simd:
3029 case OMPD_target_data: {
3030 Sema::CapturedParamNameType Params[] = {
3031 std::make_pair(StringRef(), QualType()) // __context with shared vars
3033 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3038 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3039 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3040 QualType KmpInt32PtrTy =
3041 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3042 QualType Args[] = {VoidPtrTy};
3043 FunctionProtoType::ExtProtoInfo EPI;
3044 EPI.Variadic = true;
3045 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3046 Sema::CapturedParamNameType Params[] = {
3047 std::make_pair(".global_tid.", KmpInt32Ty),
3048 std::make_pair(".part_id.", KmpInt32PtrTy),
3049 std::make_pair(".privates.", VoidPtrTy),
3052 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3053 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3054 std::make_pair(StringRef(), QualType()) // __context with shared vars
3056 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3058 // Mark this captured region as inlined, because we don't use outlined
3059 // function directly.
3060 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3061 AlwaysInlineAttr::CreateImplicit(
3062 Context, AlwaysInlineAttr::Keyword_forceinline));
3066 case OMPD_taskloop_simd: {
3067 QualType KmpInt32Ty =
3068 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3070 QualType KmpUInt64Ty =
3071 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3073 QualType KmpInt64Ty =
3074 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3076 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3077 QualType KmpInt32PtrTy =
3078 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3079 QualType Args[] = {VoidPtrTy};
3080 FunctionProtoType::ExtProtoInfo EPI;
3081 EPI.Variadic = true;
3082 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3083 Sema::CapturedParamNameType Params[] = {
3084 std::make_pair(".global_tid.", KmpInt32Ty),
3085 std::make_pair(".part_id.", KmpInt32PtrTy),
3086 std::make_pair(".privates.", VoidPtrTy),
3089 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3090 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3091 std::make_pair(".lb.", KmpUInt64Ty),
3092 std::make_pair(".ub.", KmpUInt64Ty),
3093 std::make_pair(".st.", KmpInt64Ty),
3094 std::make_pair(".liter.", KmpInt32Ty),
3095 std::make_pair(".reductions.", VoidPtrTy),
3096 std::make_pair(StringRef(), QualType()) // __context with shared vars
3098 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3100 // Mark this captured region as inlined, because we don't use outlined
3101 // function directly.
3102 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3103 AlwaysInlineAttr::CreateImplicit(
3104 Context, AlwaysInlineAttr::Keyword_forceinline));
3107 case OMPD_distribute_parallel_for_simd:
3108 case OMPD_distribute_parallel_for: {
3109 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3110 QualType KmpInt32PtrTy =
3111 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3112 Sema::CapturedParamNameType Params[] = {
3113 std::make_pair(".global_tid.", KmpInt32PtrTy),
3114 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3115 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3116 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3117 std::make_pair(StringRef(), QualType()) // __context with shared vars
3119 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3123 case OMPD_target_teams_distribute_parallel_for:
3124 case OMPD_target_teams_distribute_parallel_for_simd: {
3125 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3126 QualType KmpInt32PtrTy =
3127 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3128 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3130 QualType Args[] = {VoidPtrTy};
3131 FunctionProtoType::ExtProtoInfo EPI;
3132 EPI.Variadic = true;
3133 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3134 Sema::CapturedParamNameType Params[] = {
3135 std::make_pair(".global_tid.", KmpInt32Ty),
3136 std::make_pair(".part_id.", KmpInt32PtrTy),
3137 std::make_pair(".privates.", VoidPtrTy),
3140 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3141 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3142 std::make_pair(StringRef(), QualType()) // __context with shared vars
3144 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3146 // Mark this captured region as inlined, because we don't use outlined
3147 // function directly.
3148 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3149 AlwaysInlineAttr::CreateImplicit(
3150 Context, AlwaysInlineAttr::Keyword_forceinline));
3151 Sema::CapturedParamNameType ParamsTarget[] = {
3152 std::make_pair(StringRef(), QualType()) // __context with shared vars
3154 // Start a captured region for 'target' with no implicit parameters.
3155 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3158 Sema::CapturedParamNameType ParamsTeams[] = {
3159 std::make_pair(".global_tid.", KmpInt32PtrTy),
3160 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3161 std::make_pair(StringRef(), QualType()) // __context with shared vars
3163 // Start a captured region for 'target' with no implicit parameters.
3164 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3167 Sema::CapturedParamNameType ParamsParallel[] = {
3168 std::make_pair(".global_tid.", KmpInt32PtrTy),
3169 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3170 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3171 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3172 std::make_pair(StringRef(), QualType()) // __context with shared vars
3174 // Start a captured region for 'teams' or 'parallel'. Both regions have
3175 // the same implicit parameters.
3176 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3181 case OMPD_teams_distribute_parallel_for:
3182 case OMPD_teams_distribute_parallel_for_simd: {
3183 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3184 QualType KmpInt32PtrTy =
3185 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3187 Sema::CapturedParamNameType ParamsTeams[] = {
3188 std::make_pair(".global_tid.", KmpInt32PtrTy),
3189 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3190 std::make_pair(StringRef(), QualType()) // __context with shared vars
3192 // Start a captured region for 'target' with no implicit parameters.
3193 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3196 Sema::CapturedParamNameType ParamsParallel[] = {
3197 std::make_pair(".global_tid.", KmpInt32PtrTy),
3198 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3199 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3200 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3201 std::make_pair(StringRef(), QualType()) // __context with shared vars
3203 // Start a captured region for 'teams' or 'parallel'. Both regions have
3204 // the same implicit parameters.
3205 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3209 case OMPD_target_update:
3210 case OMPD_target_enter_data:
3211 case OMPD_target_exit_data: {
3212 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3213 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3214 QualType KmpInt32PtrTy =
3215 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3216 QualType Args[] = {VoidPtrTy};
3217 FunctionProtoType::ExtProtoInfo EPI;
3218 EPI.Variadic = true;
3219 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3220 Sema::CapturedParamNameType Params[] = {
3221 std::make_pair(".global_tid.", KmpInt32Ty),
3222 std::make_pair(".part_id.", KmpInt32PtrTy),
3223 std::make_pair(".privates.", VoidPtrTy),
3226 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3227 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3228 std::make_pair(StringRef(), QualType()) // __context with shared vars
3230 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3232 // Mark this captured region as inlined, because we don't use outlined
3233 // function directly.
3234 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3235 AlwaysInlineAttr::CreateImplicit(
3236 Context, AlwaysInlineAttr::Keyword_forceinline));
3239 case OMPD_threadprivate:
3241 case OMPD_taskyield:
3244 case OMPD_cancellation_point:
3247 case OMPD_declare_reduction:
3248 case OMPD_declare_mapper:
3249 case OMPD_declare_simd:
3250 case OMPD_declare_target:
3251 case OMPD_end_declare_target:
3253 llvm_unreachable("OpenMP Directive is not allowed");
3255 llvm_unreachable("Unknown OpenMP directive");
3259 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
3260 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3261 getOpenMPCaptureRegions(CaptureRegions, DKind);
3262 return CaptureRegions.size();
3265 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
3266 Expr *CaptureExpr, bool WithInit,
3267 bool AsExpression) {
3268 assert(CaptureExpr);
3269 ASTContext &C = S.getASTContext();
3270 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
3271 QualType Ty = Init->getType();
3272 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
3273 if (S.getLangOpts().CPlusPlus) {
3274 Ty = C.getLValueReferenceType(Ty);
3276 Ty = C.getPointerType(Ty);
3278 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
3279 if (!Res.isUsable())
3285 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
3286 CaptureExpr->getBeginLoc());
3288 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
3289 S.CurContext->addHiddenDecl(CED);
3290 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
3294 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3296 OMPCapturedExprDecl *CD;
3297 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
3298 CD = cast<OMPCapturedExprDecl>(VD);
3300 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3301 /*AsExpression=*/false);
3302 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3303 CaptureExpr->getExprLoc());
3306 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
3307 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
3309 OMPCapturedExprDecl *CD = buildCaptureDecl(
3310 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3311 /*WithInit=*/true, /*AsExpression=*/true);
3312 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3313 CaptureExpr->getExprLoc());
3315 ExprResult Res = Ref;
3316 if (!S.getLangOpts().CPlusPlus &&
3317 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
3318 Ref->getType()->isPointerType()) {
3319 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
3320 if (!Res.isUsable())
3323 return S.DefaultLvalueConversion(Res.get());
3327 // OpenMP directives parsed in this section are represented as a
3328 // CapturedStatement with an associated statement. If a syntax error
3329 // is detected during the parsing of the associated statement, the
3330 // compiler must abort processing and close the CapturedStatement.
3332 // Combined directives such as 'target parallel' have more than one
3333 // nested CapturedStatements. This RAII ensures that we unwind out
3334 // of all the nested CapturedStatements when an error is found.
3335 class CaptureRegionUnwinderRAII {
3339 OpenMPDirectiveKind DKind = OMPD_unknown;
3342 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3343 OpenMPDirectiveKind DKind)
3344 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3345 ~CaptureRegionUnwinderRAII() {
3347 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3348 while (--ThisCaptureLevel >= 0)
3349 S.ActOnCapturedRegionError();
3355 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
3356 // Capture variables captured by reference in lambdas for target-based
3358 if (!CurContext->isDependentContext() &&
3359 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
3360 isOpenMPTargetDataManagementDirective(
3361 DSAStack->getCurrentDirective()))) {
3362 QualType Type = V->getType();
3363 if (const auto *RD = Type.getCanonicalType()
3364 .getNonReferenceType()
3365 ->getAsCXXRecordDecl()) {
3366 bool SavedForceCaptureByReferenceInTargetExecutable =
3367 DSAStack->isForceCaptureByReferenceInTargetExecutable();
3368 DSAStack->setForceCaptureByReferenceInTargetExecutable(
3370 if (RD->isLambda()) {
3371 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
3372 FieldDecl *ThisCapture;
3373 RD->getCaptureFields(Captures, ThisCapture);
3374 for (const LambdaCapture &LC : RD->captures()) {
3375 if (LC.getCaptureKind() == LCK_ByRef) {
3376 VarDecl *VD = LC.getCapturedVar();
3377 DeclContext *VDC = VD->getDeclContext();
3378 if (!VDC->Encloses(CurContext))
3380 MarkVariableReferenced(LC.getLocation(), VD);
3381 } else if (LC.getCaptureKind() == LCK_This) {
3382 QualType ThisTy = getCurrentThisType();
3383 if (!ThisTy.isNull() &&
3384 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
3385 CheckCXXThisCapture(LC.getLocation());
3389 DSAStack->setForceCaptureByReferenceInTargetExecutable(
3390 SavedForceCaptureByReferenceInTargetExecutable);
3395 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
3396 ArrayRef<OMPClause *> Clauses) {
3397 bool ErrorFound = false;
3398 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3399 *this, ErrorFound, DSAStack->getCurrentDirective());
3400 if (!S.isUsable()) {
3405 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3406 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
3407 OMPOrderedClause *OC = nullptr;
3408 OMPScheduleClause *SC = nullptr;
3409 SmallVector<const OMPLinearClause *, 4> LCs;
3410 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
3411 // This is required for proper codegen.
3412 for (OMPClause *Clause : Clauses) {
3413 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3414 Clause->getClauseKind() == OMPC_in_reduction) {
3415 // Capture taskgroup task_reduction descriptors inside the tasking regions
3416 // with the corresponding in_reduction items.
3417 auto *IRC = cast<OMPInReductionClause>(Clause);
3418 for (Expr *E : IRC->taskgroup_descriptors())
3420 MarkDeclarationsReferencedInExpr(E);
3422 if (isOpenMPPrivate(Clause->getClauseKind()) ||
3423 Clause->getClauseKind() == OMPC_copyprivate ||
3424 (getLangOpts().OpenMPUseTLS &&
3425 getASTContext().getTargetInfo().isTLSSupported() &&
3426 Clause->getClauseKind() == OMPC_copyin)) {
3427 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
3428 // Mark all variables in private list clauses as used in inner region.
3429 for (Stmt *VarRef : Clause->children()) {
3430 if (auto *E = cast_or_null<Expr>(VarRef)) {
3431 MarkDeclarationsReferencedInExpr(E);
3434 DSAStack->setForceVarCapturing(/*V=*/false);
3435 } else if (CaptureRegions.size() > 1 ||
3436 CaptureRegions.back() != OMPD_unknown) {
3437 if (auto *C = OMPClauseWithPreInit::get(Clause))
3439 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
3440 if (Expr *E = C->getPostUpdateExpr())
3441 MarkDeclarationsReferencedInExpr(E);
3444 if (Clause->getClauseKind() == OMPC_schedule)
3445 SC = cast<OMPScheduleClause>(Clause);
3446 else if (Clause->getClauseKind() == OMPC_ordered)
3447 OC = cast<OMPOrderedClause>(Clause);
3448 else if (Clause->getClauseKind() == OMPC_linear)
3449 LCs.push_back(cast<OMPLinearClause>(Clause));
3451 // OpenMP, 2.7.1 Loop Construct, Restrictions
3452 // The nonmonotonic modifier cannot be specified if an ordered clause is
3455 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
3456 SC->getSecondScheduleModifier() ==
3457 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3459 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3460 ? SC->getFirstScheduleModifierLoc()
3461 : SC->getSecondScheduleModifierLoc(),
3462 diag::err_omp_schedule_nonmonotonic_ordered)
3463 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3466 if (!LCs.empty() && OC && OC->getNumForLoops()) {
3467 for (const OMPLinearClause *C : LCs) {
3468 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
3469 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3473 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3474 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
3475 OC->getNumForLoops()) {
3476 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
3477 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3484 unsigned CompletedRegions = 0;
3485 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
3486 // Mark all variables in private list clauses as used in inner region.
3487 // Required for proper codegen of combined directives.
3488 // TODO: add processing for other clauses.
3489 if (ThisCaptureRegion != OMPD_unknown) {
3490 for (const clang::OMPClauseWithPreInit *C : PICs) {
3491 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3492 // Find the particular capture region for the clause if the
3493 // directive is a combined one with multiple capture regions.
3494 // If the directive is not a combined one, the capture region
3495 // associated with the clause is OMPD_unknown and is generated
3497 if (CaptureRegion == ThisCaptureRegion ||
3498 CaptureRegion == OMPD_unknown) {
3499 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
3500 for (Decl *D : DS->decls())
3501 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3506 if (++CompletedRegions == CaptureRegions.size())
3507 DSAStack->setBodyComplete();
3508 SR = ActOnCapturedRegionEnd(SR.get());
3513 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3514 OpenMPDirectiveKind CancelRegion,
3515 SourceLocation StartLoc) {
3516 // CancelRegion is only needed for cancel and cancellation_point.
3517 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3520 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3521 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3524 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3525 << getOpenMPDirectiveName(CancelRegion);
3529 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
3530 OpenMPDirectiveKind CurrentRegion,
3531 const DeclarationNameInfo &CurrentName,
3532 OpenMPDirectiveKind CancelRegion,
3533 SourceLocation StartLoc) {
3534 if (Stack->getCurScope()) {
3535 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3536 OpenMPDirectiveKind OffendingRegion = ParentRegion;
3537 bool NestingProhibited = false;
3538 bool CloseNesting = true;
3539 bool OrphanSeen = false;
3542 ShouldBeInParallelRegion,
3543 ShouldBeInOrderedRegion,
3544 ShouldBeInTargetRegion,
3545 ShouldBeInTeamsRegion
3546 } Recommend = NoRecommend;
3547 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
3548 // OpenMP [2.16, Nesting of Regions]
3549 // OpenMP constructs may not be nested inside a simd region.
3550 // OpenMP [2.8.1,simd Construct, Restrictions]
3551 // An ordered construct with the simd clause is the only OpenMP
3552 // construct that can appear in the simd region.
3553 // Allowing a SIMD construct nested in another SIMD construct is an
3554 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3556 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3557 ? diag::err_omp_prohibited_region_simd
3558 : diag::warn_omp_nesting_simd);
3559 return CurrentRegion != OMPD_simd;
3561 if (ParentRegion == OMPD_atomic) {
3562 // OpenMP [2.16, Nesting of Regions]
3563 // OpenMP constructs may not be nested inside an atomic region.
3564 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3567 if (CurrentRegion == OMPD_section) {
3568 // OpenMP [2.7.2, sections Construct, Restrictions]
3569 // Orphaned section directives are prohibited. That is, the section
3570 // directives must appear within the sections construct and must not be
3571 // encountered elsewhere in the sections region.
3572 if (ParentRegion != OMPD_sections &&
3573 ParentRegion != OMPD_parallel_sections) {
3574 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3575 << (ParentRegion != OMPD_unknown)
3576 << getOpenMPDirectiveName(ParentRegion);
3581 // Allow some constructs (except teams and cancellation constructs) to be
3582 // orphaned (they could be used in functions, called from OpenMP regions
3583 // with the required preconditions).
3584 if (ParentRegion == OMPD_unknown &&
3585 !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3586 CurrentRegion != OMPD_cancellation_point &&
3587 CurrentRegion != OMPD_cancel)
3589 if (CurrentRegion == OMPD_cancellation_point ||
3590 CurrentRegion == OMPD_cancel) {
3591 // OpenMP [2.16, Nesting of Regions]
3592 // A cancellation point construct for which construct-type-clause is
3593 // taskgroup must be nested inside a task construct. A cancellation
3594 // point construct for which construct-type-clause is not taskgroup must
3595 // be closely nested inside an OpenMP construct that matches the type
3596 // specified in construct-type-clause.
3597 // A cancel construct for which construct-type-clause is taskgroup must be
3598 // nested inside a task construct. A cancel construct for which
3599 // construct-type-clause is not taskgroup must be closely nested inside an
3600 // OpenMP construct that matches the type specified in
3601 // construct-type-clause.
3603 !((CancelRegion == OMPD_parallel &&
3604 (ParentRegion == OMPD_parallel ||
3605 ParentRegion == OMPD_target_parallel)) ||
3606 (CancelRegion == OMPD_for &&
3607 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3608 ParentRegion == OMPD_target_parallel_for ||
3609 ParentRegion == OMPD_distribute_parallel_for ||
3610 ParentRegion == OMPD_teams_distribute_parallel_for ||
3611 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
3612 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3613 (CancelRegion == OMPD_sections &&
3614 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3615 ParentRegion == OMPD_parallel_sections)));
3616 OrphanSeen = ParentRegion == OMPD_unknown;
3617 } else if (CurrentRegion == OMPD_master) {
3618 // OpenMP [2.16, Nesting of Regions]
3619 // A master region may not be closely nested inside a worksharing,
3620 // atomic, or explicit task region.
3621 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3622 isOpenMPTaskingDirective(ParentRegion);
3623 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3624 // OpenMP [2.16, Nesting of Regions]
3625 // A critical region may not be nested (closely or otherwise) inside a
3626 // critical region with the same name. Note that this restriction is not
3627 // sufficient to prevent deadlock.
3628 SourceLocation PreviousCriticalLoc;
3629 bool DeadLock = Stack->hasDirective(
3630 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3631 const DeclarationNameInfo &DNI,
3632 SourceLocation Loc) {
3633 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3634 PreviousCriticalLoc = Loc;
3639 false /* skip top directive */);
3641 SemaRef.Diag(StartLoc,
3642 diag::err_omp_prohibited_region_critical_same_name)
3643 << CurrentName.getName();
3644 if (PreviousCriticalLoc.isValid())
3645 SemaRef.Diag(PreviousCriticalLoc,
3646 diag::note_omp_previous_critical_region);
3649 } else if (CurrentRegion == OMPD_barrier) {
3650 // OpenMP [2.16, Nesting of Regions]
3651 // A barrier region may not be closely nested inside a worksharing,
3652 // explicit task, critical, ordered, atomic, or master region.
3653 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3654 isOpenMPTaskingDirective(ParentRegion) ||
3655 ParentRegion == OMPD_master ||
3656 ParentRegion == OMPD_critical ||
3657 ParentRegion == OMPD_ordered;
3658 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
3659 !isOpenMPParallelDirective(CurrentRegion) &&
3660 !isOpenMPTeamsDirective(CurrentRegion)) {
3661 // OpenMP [2.16, Nesting of Regions]
3662 // A worksharing region may not be closely nested inside a worksharing,
3663 // explicit task, critical, ordered, atomic, or master region.
3664 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3665 isOpenMPTaskingDirective(ParentRegion) ||
3666 ParentRegion == OMPD_master ||
3667 ParentRegion == OMPD_critical ||
3668 ParentRegion == OMPD_ordered;
3669 Recommend = ShouldBeInParallelRegion;
3670 } else if (CurrentRegion == OMPD_ordered) {
3671 // OpenMP [2.16, Nesting of Regions]
3672 // An ordered region may not be closely nested inside a critical,
3673 // atomic, or explicit task region.
3674 // An ordered region must be closely nested inside a loop region (or
3675 // parallel loop region) with an ordered clause.
3676 // OpenMP [2.8.1,simd Construct, Restrictions]
3677 // An ordered construct with the simd clause is the only OpenMP construct
3678 // that can appear in the simd region.
3679 NestingProhibited = ParentRegion == OMPD_critical ||
3680 isOpenMPTaskingDirective(ParentRegion) ||
3681 !(isOpenMPSimdDirective(ParentRegion) ||
3682 Stack->isParentOrderedRegion());
3683 Recommend = ShouldBeInOrderedRegion;
3684 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
3685 // OpenMP [2.16, Nesting of Regions]
3686 // If specified, a teams construct must be contained within a target
3688 NestingProhibited = ParentRegion != OMPD_target;
3689 OrphanSeen = ParentRegion == OMPD_unknown;
3690 Recommend = ShouldBeInTargetRegion;
3692 if (!NestingProhibited &&
3693 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3694 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3695 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
3696 // OpenMP [2.16, Nesting of Regions]
3697 // distribute, parallel, parallel sections, parallel workshare, and the
3698 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3699 // constructs that can be closely nested in the teams region.
3700 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3701 !isOpenMPDistributeDirective(CurrentRegion);
3702 Recommend = ShouldBeInParallelRegion;
3704 if (!NestingProhibited &&
3705 isOpenMPNestingDistributeDirective(CurrentRegion)) {
3706 // OpenMP 4.5 [2.17 Nesting of Regions]
3707 // The region associated with the distribute construct must be strictly
3708 // nested inside a teams region
3710 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
3711 Recommend = ShouldBeInTeamsRegion;
3713 if (!NestingProhibited &&
3714 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3715 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3716 // OpenMP 4.5 [2.17 Nesting of Regions]
3717 // If a target, target update, target data, target enter data, or
3718 // target exit data construct is encountered during execution of a
3719 // target region, the behavior is unspecified.
3720 NestingProhibited = Stack->hasDirective(
3721 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3723 if (isOpenMPTargetExecutionDirective(K)) {
3724 OffendingRegion = K;
3729 false /* don't skip top directive */);
3730 CloseNesting = false;
3732 if (NestingProhibited) {
3734 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3735 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3737 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3738 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3739 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3747 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3748 ArrayRef<OMPClause *> Clauses,
3749 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3750 bool ErrorFound = false;
3751 unsigned NamedModifiersNumber = 0;
3752 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3754 SmallVector<SourceLocation, 4> NameModifierLoc;
3755 for (const OMPClause *C : Clauses) {
3756 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3757 // At most one if clause without a directive-name-modifier can appear on
3759 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3760 if (FoundNameModifiers[CurNM]) {
3761 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
3762 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3763 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3765 } else if (CurNM != OMPD_unknown) {
3766 NameModifierLoc.push_back(IC->getNameModifierLoc());
3767 ++NamedModifiersNumber;
3769 FoundNameModifiers[CurNM] = IC;
3770 if (CurNM == OMPD_unknown)
3772 // Check if the specified name modifier is allowed for the current
3774 // At most one if clause with the particular directive-name-modifier can
3775 // appear on the directive.
3776 bool MatchFound = false;
3777 for (auto NM : AllowedNameModifiers) {
3784 S.Diag(IC->getNameModifierLoc(),
3785 diag::err_omp_wrong_if_directive_name_modifier)
3786 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3791 // If any if clause on the directive includes a directive-name-modifier then
3792 // all if clauses on the directive must include a directive-name-modifier.
3793 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3794 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3795 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
3796 diag::err_omp_no_more_if_clause);
3799 std::string Sep(", ");
3800 unsigned AllowedCnt = 0;
3801 unsigned TotalAllowedNum =
3802 AllowedNameModifiers.size() - NamedModifiersNumber;
3803 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3805 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3806 if (!FoundNameModifiers[NM]) {
3808 Values += getOpenMPDirectiveName(NM);
3810 if (AllowedCnt + 2 == TotalAllowedNum)
3812 else if (AllowedCnt + 1 != TotalAllowedNum)
3817 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
3818 diag::err_omp_unnamed_if_clause)
3819 << (TotalAllowedNum > 1) << Values;
3821 for (SourceLocation Loc : NameModifierLoc) {
3822 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3829 static std::pair<ValueDecl *, bool>
3830 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
3831 SourceRange &ERange, bool AllowArraySection = false) {
3832 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3833 RefExpr->containsUnexpandedParameterPack())
3834 return std::make_pair(nullptr, true);
3836 // OpenMP [3.1, C/C++]
3837 // A list item is a variable name.
3838 // OpenMP [2.9.3.3, Restrictions, p.1]
3839 // A variable that is part of another variable (as an array or
3840 // structure element) cannot appear in a private clause.
3841 RefExpr = RefExpr->IgnoreParens();
3846 } IsArrayExpr = NoArrayExpr;
3847 if (AllowArraySection) {
3848 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
3849 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
3850 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
3851 Base = TempASE->getBase()->IgnoreParenImpCasts();
3853 IsArrayExpr = ArraySubscript;
3854 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
3855 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
3856 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
3857 Base = TempOASE->getBase()->IgnoreParenImpCasts();
3858 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
3859 Base = TempASE->getBase()->IgnoreParenImpCasts();
3861 IsArrayExpr = OMPArraySection;
3864 ELoc = RefExpr->getExprLoc();
3865 ERange = RefExpr->getSourceRange();
3866 RefExpr = RefExpr->IgnoreParenImpCasts();
3867 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3868 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
3869 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
3870 (S.getCurrentThisType().isNull() || !ME ||
3871 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
3872 !isa<FieldDecl>(ME->getMemberDecl()))) {
3873 if (IsArrayExpr != NoArrayExpr) {
3874 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
3879 ? diag::err_omp_expected_var_name_member_expr_or_array_item
3880 : diag::err_omp_expected_var_name_member_expr)
3881 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
3883 return std::make_pair(nullptr, false);
3885 return std::make_pair(
3886 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
3889 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
3890 ArrayRef<OMPClause *> Clauses) {
3891 assert(!S.CurContext->isDependentContext() &&
3892 "Expected non-dependent context.");
3893 auto AllocateRange =
3894 llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
3895 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
3897 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
3898 return isOpenMPPrivate(C->getClauseKind());
3900 for (OMPClause *Cl : PrivateRange) {
3901 MutableArrayRef<Expr *>::iterator I, It, Et;
3902 if (Cl->getClauseKind() == OMPC_private) {
3903 auto *PC = cast<OMPPrivateClause>(Cl);
3904 I = PC->private_copies().begin();
3905 It = PC->varlist_begin();
3906 Et = PC->varlist_end();
3907 } else if (Cl->getClauseKind() == OMPC_firstprivate) {
3908 auto *PC = cast<OMPFirstprivateClause>(Cl);
3909 I = PC->private_copies().begin();
3910 It = PC->varlist_begin();
3911 Et = PC->varlist_end();
3912 } else if (Cl->getClauseKind() == OMPC_lastprivate) {
3913 auto *PC = cast<OMPLastprivateClause>(Cl);
3914 I = PC->private_copies().begin();
3915 It = PC->varlist_begin();
3916 Et = PC->varlist_end();
3917 } else if (Cl->getClauseKind() == OMPC_linear) {
3918 auto *PC = cast<OMPLinearClause>(Cl);
3919 I = PC->privates().begin();
3920 It = PC->varlist_begin();
3921 Et = PC->varlist_end();
3922 } else if (Cl->getClauseKind() == OMPC_reduction) {
3923 auto *PC = cast<OMPReductionClause>(Cl);
3924 I = PC->privates().begin();
3925 It = PC->varlist_begin();
3926 Et = PC->varlist_end();
3927 } else if (Cl->getClauseKind() == OMPC_task_reduction) {
3928 auto *PC = cast<OMPTaskReductionClause>(Cl);
3929 I = PC->privates().begin();
3930 It = PC->varlist_begin();
3931 Et = PC->varlist_end();
3932 } else if (Cl->getClauseKind() == OMPC_in_reduction) {
3933 auto *PC = cast<OMPInReductionClause>(Cl);
3934 I = PC->privates().begin();
3935 It = PC->varlist_begin();
3936 Et = PC->varlist_end();
3938 llvm_unreachable("Expected private clause.");
3940 for (Expr *E : llvm::make_range(It, Et)) {
3945 SourceLocation ELoc;
3947 Expr *SimpleRefExpr = E;
3948 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
3949 /*AllowArraySection=*/true);
3950 DeclToCopy.try_emplace(Res.first,
3951 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
3955 for (OMPClause *C : AllocateRange) {
3956 auto *AC = cast<OMPAllocateClause>(C);
3957 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3958 getAllocatorKind(S, Stack, AC->getAllocator());
3959 // OpenMP, 2.11.4 allocate Clause, Restrictions.
3960 // For task, taskloop or target directives, allocation requests to memory
3961 // allocators with the trait access set to thread result in unspecified
3963 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
3964 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
3965 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
3966 S.Diag(AC->getAllocator()->getExprLoc(),
3967 diag::warn_omp_allocate_thread_on_task_target_directive)
3968 << getOpenMPDirectiveName(Stack->getCurrentDirective());
3970 for (Expr *E : AC->varlists()) {
3971 SourceLocation ELoc;
3973 Expr *SimpleRefExpr = E;
3974 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
3975 ValueDecl *VD = Res.first;
3976 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
3977 if (!isOpenMPPrivate(Data.CKind)) {
3978 S.Diag(E->getExprLoc(),
3979 diag::err_omp_expected_private_copy_for_allocate);
3982 VarDecl *PrivateVD = DeclToCopy[VD];
3983 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
3984 AllocatorKind, AC->getAllocator()))
3986 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
3987 E->getSourceRange());
3992 StmtResult Sema::ActOnOpenMPExecutableDirective(
3993 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3994 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3995 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3996 StmtResult Res = StmtError();
3997 // First check CancelRegion which is then used in checkNestingOfRegions.
3998 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3999 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
4003 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
4004 VarsWithInheritedDSAType VarsWithInheritedDSA;
4005 bool ErrorFound = false;
4006 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
4007 if (AStmt && !CurContext->isDependentContext()) {
4008 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4010 // Check default data sharing attributes for referenced variables.
4011 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
4012 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
4014 while (--ThisCaptureLevel >= 0)
4015 S = cast<CapturedStmt>(S)->getCapturedStmt();
4016 DSAChecker.Visit(S);
4017 if (!isOpenMPTargetDataManagementDirective(Kind) &&
4018 !isOpenMPTaskingDirective(Kind)) {
4019 // Visit subcaptures to generate implicit clauses for captured vars.
4020 auto *CS = cast<CapturedStmt>(AStmt);
4021 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4022 getOpenMPCaptureRegions(CaptureRegions, Kind);
4023 // Ignore outer tasking regions for target directives.
4024 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
4025 CS = cast<CapturedStmt>(CS->getCapturedStmt());
4026 DSAChecker.visitSubCaptures(CS);
4028 if (DSAChecker.isErrorFound())
4030 // Generate list of implicitly defined firstprivate variables.
4031 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
4033 SmallVector<Expr *, 4> ImplicitFirstprivates(
4034 DSAChecker.getImplicitFirstprivate().begin(),
4035 DSAChecker.getImplicitFirstprivate().end());
4036 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
4037 DSAChecker.getImplicitMap().end());
4038 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
4039 for (OMPClause *C : Clauses) {
4040 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
4041 for (Expr *E : IRC->taskgroup_descriptors())
4043 ImplicitFirstprivates.emplace_back(E);
4046 if (!ImplicitFirstprivates.empty()) {
4047 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
4048 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
4049 SourceLocation())) {
4050 ClausesWithImplicit.push_back(Implicit);
4051 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
4052 ImplicitFirstprivates.size();
4057 if (!ImplicitMaps.empty()) {
4058 CXXScopeSpec MapperIdScopeSpec;
4059 DeclarationNameInfo MapperId;
4060 if (OMPClause *Implicit = ActOnOpenMPMapClause(
4061 llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
4062 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
4063 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
4064 ClausesWithImplicit.emplace_back(Implicit);
4066 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
4073 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
4076 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
4078 AllowedNameModifiers.push_back(OMPD_parallel);
4081 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4082 VarsWithInheritedDSA);
4085 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4086 VarsWithInheritedDSA);
4089 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4090 EndLoc, VarsWithInheritedDSA);
4093 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
4097 assert(ClausesWithImplicit.empty() &&
4098 "No clauses are allowed for 'omp section' directive");
4099 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
4102 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
4106 assert(ClausesWithImplicit.empty() &&
4107 "No clauses are allowed for 'omp master' directive");
4108 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
4111 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
4114 case OMPD_parallel_for:
4115 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
4116 EndLoc, VarsWithInheritedDSA);
4117 AllowedNameModifiers.push_back(OMPD_parallel);
4119 case OMPD_parallel_for_simd:
4120 Res = ActOnOpenMPParallelForSimdDirective(
4121 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4122 AllowedNameModifiers.push_back(OMPD_parallel);
4124 case OMPD_parallel_sections:
4125 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
4127 AllowedNameModifiers.push_back(OMPD_parallel);
4131 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4132 AllowedNameModifiers.push_back(OMPD_task);
4134 case OMPD_taskyield:
4135 assert(ClausesWithImplicit.empty() &&
4136 "No clauses are allowed for 'omp taskyield' directive");
4137 assert(AStmt == nullptr &&
4138 "No associated statement allowed for 'omp taskyield' directive");
4139 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
4142 assert(ClausesWithImplicit.empty() &&
4143 "No clauses are allowed for 'omp barrier' directive");
4144 assert(AStmt == nullptr &&
4145 "No associated statement allowed for 'omp barrier' directive");
4146 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
4149 assert(ClausesWithImplicit.empty() &&
4150 "No clauses are allowed for 'omp taskwait' directive");
4151 assert(AStmt == nullptr &&
4152 "No associated statement allowed for 'omp taskwait' directive");
4153 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
4155 case OMPD_taskgroup:
4156 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
4160 assert(AStmt == nullptr &&
4161 "No associated statement allowed for 'omp flush' directive");
4162 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
4165 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
4169 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
4174 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4177 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
4179 AllowedNameModifiers.push_back(OMPD_target);
4181 case OMPD_target_parallel:
4182 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
4184 AllowedNameModifiers.push_back(OMPD_target);
4185 AllowedNameModifiers.push_back(OMPD_parallel);
4187 case OMPD_target_parallel_for:
4188 Res = ActOnOpenMPTargetParallelForDirective(
4189 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4190 AllowedNameModifiers.push_back(OMPD_target);
4191 AllowedNameModifiers.push_back(OMPD_parallel);
4193 case OMPD_cancellation_point:
4194 assert(ClausesWithImplicit.empty() &&
4195 "No clauses are allowed for 'omp cancellation point' directive");
4196 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
4197 "cancellation point' directive");
4198 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
4201 assert(AStmt == nullptr &&
4202 "No associated statement allowed for 'omp cancel' directive");
4203 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
4205 AllowedNameModifiers.push_back(OMPD_cancel);
4207 case OMPD_target_data:
4208 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
4210 AllowedNameModifiers.push_back(OMPD_target_data);
4212 case OMPD_target_enter_data:
4213 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
4215 AllowedNameModifiers.push_back(OMPD_target_enter_data);
4217 case OMPD_target_exit_data:
4218 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
4220 AllowedNameModifiers.push_back(OMPD_target_exit_data);
4223 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
4224 EndLoc, VarsWithInheritedDSA);
4225 AllowedNameModifiers.push_back(OMPD_taskloop);
4227 case OMPD_taskloop_simd:
4228 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4229 EndLoc, VarsWithInheritedDSA);
4230 AllowedNameModifiers.push_back(OMPD_taskloop);
4232 case OMPD_distribute:
4233 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
4234 EndLoc, VarsWithInheritedDSA);
4236 case OMPD_target_update:
4237 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
4239 AllowedNameModifiers.push_back(OMPD_target_update);
4241 case OMPD_distribute_parallel_for:
4242 Res = ActOnOpenMPDistributeParallelForDirective(
4243 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4244 AllowedNameModifiers.push_back(OMPD_parallel);
4246 case OMPD_distribute_parallel_for_simd:
4247 Res = ActOnOpenMPDistributeParallelForSimdDirective(
4248 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4249 AllowedNameModifiers.push_back(OMPD_parallel);
4251 case OMPD_distribute_simd:
4252 Res = ActOnOpenMPDistributeSimdDirective(
4253 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4255 case OMPD_target_parallel_for_simd:
4256 Res = ActOnOpenMPTargetParallelForSimdDirective(
4257 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4258 AllowedNameModifiers.push_back(OMPD_target);
4259 AllowedNameModifiers.push_back(OMPD_parallel);
4261 case OMPD_target_simd:
4262 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4263 EndLoc, VarsWithInheritedDSA);
4264 AllowedNameModifiers.push_back(OMPD_target);
4266 case OMPD_teams_distribute:
4267 Res = ActOnOpenMPTeamsDistributeDirective(
4268 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4270 case OMPD_teams_distribute_simd:
4271 Res = ActOnOpenMPTeamsDistributeSimdDirective(
4272 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4274 case OMPD_teams_distribute_parallel_for_simd:
4275 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
4276 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4277 AllowedNameModifiers.push_back(OMPD_parallel);
4279 case OMPD_teams_distribute_parallel_for:
4280 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
4281 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4282 AllowedNameModifiers.push_back(OMPD_parallel);
4284 case OMPD_target_teams:
4285 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
4287 AllowedNameModifiers.push_back(OMPD_target);
4289 case OMPD_target_teams_distribute:
4290 Res = ActOnOpenMPTargetTeamsDistributeDirective(
4291 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4292 AllowedNameModifiers.push_back(OMPD_target);
4294 case OMPD_target_teams_distribute_parallel_for:
4295 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
4296 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4297 AllowedNameModifiers.push_back(OMPD_target);
4298 AllowedNameModifiers.push_back(OMPD_parallel);
4300 case OMPD_target_teams_distribute_parallel_for_simd:
4301 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
4302 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4303 AllowedNameModifiers.push_back(OMPD_target);
4304 AllowedNameModifiers.push_back(OMPD_parallel);
4306 case OMPD_target_teams_distribute_simd:
4307 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
4308 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4309 AllowedNameModifiers.push_back(OMPD_target);
4311 case OMPD_declare_target:
4312 case OMPD_end_declare_target:
4313 case OMPD_threadprivate:
4315 case OMPD_declare_reduction:
4316 case OMPD_declare_mapper:
4317 case OMPD_declare_simd:
4319 llvm_unreachable("OpenMP Directive is not allowed");
4321 llvm_unreachable("Unknown OpenMP directive");
4324 ErrorFound = Res.isInvalid() || ErrorFound;
4326 // Check variables in the clauses if default(none) was specified.
4327 if (DSAStack->getDefaultDSA() == DSA_none) {
4328 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
4329 for (OMPClause *C : Clauses) {
4330 switch (C->getClauseKind()) {
4331 case OMPC_num_threads:
4332 case OMPC_dist_schedule:
4333 // Do not analyse if no parent teams directive.
4334 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()))
4338 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()) &&
4339 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
4346 case OMPC_num_teams:
4347 case OMPC_thread_limit:
4349 case OMPC_grainsize:
4350 case OMPC_num_tasks:
4357 case OMPC_proc_bind:
4359 case OMPC_firstprivate:
4360 case OMPC_lastprivate:
4362 case OMPC_reduction:
4363 case OMPC_task_reduction:
4364 case OMPC_in_reduction:
4368 case OMPC_copyprivate:
4371 case OMPC_mergeable:
4383 case OMPC_defaultmap:
4386 case OMPC_use_device_ptr:
4387 case OMPC_is_device_ptr:
4389 case OMPC_allocator:
4391 case OMPC_threadprivate:
4394 case OMPC_unified_address:
4395 case OMPC_unified_shared_memory:
4396 case OMPC_reverse_offload:
4397 case OMPC_dynamic_allocators:
4398 case OMPC_atomic_default_mem_order:
4399 llvm_unreachable("Unexpected clause");
4401 for (Stmt *CC : C->children()) {
4403 DSAChecker.Visit(CC);
4406 for (auto &P : DSAChecker.getVarsWithInheritedDSA())
4407 VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
4409 for (const auto &P : VarsWithInheritedDSA) {
4410 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
4413 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
4414 << P.first << P.second->getSourceRange();
4415 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
4418 if (!AllowedNameModifiers.empty())
4419 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
4425 if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) {
4426 Res.getAs<OMPExecutableDirective>()
4427 ->getStructuredBlock()
4428 ->setIsOMPStructuredBlock(true);
4431 if (!CurContext->isDependentContext() &&
4432 isOpenMPTargetExecutionDirective(Kind) &&
4433 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4434 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
4435 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
4436 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
4437 // Register target to DSA Stack.
4438 DSAStack->addTargetDirLocation(StartLoc);
4444 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
4445 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
4446 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
4447 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
4448 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
4449 assert(Aligneds.size() == Alignments.size());
4450 assert(Linears.size() == LinModifiers.size());
4451 assert(Linears.size() == Steps.size());
4452 if (!DG || DG.get().isNull())
4453 return DeclGroupPtrTy();
4455 if (!DG.get().isSingleDecl()) {
4456 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
4459 Decl *ADecl = DG.get().getSingleDecl();
4460 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4461 ADecl = FTD->getTemplatedDecl();
4463 auto *FD = dyn_cast<FunctionDecl>(ADecl);
4465 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
4466 return DeclGroupPtrTy();
4469 // OpenMP [2.8.2, declare simd construct, Description]
4470 // The parameter of the simdlen clause must be a constant positive integer
4474 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
4475 // OpenMP [2.8.2, declare simd construct, Description]
4476 // The special this pointer can be used as if was one of the arguments to the
4477 // function in any of the linear, aligned, or uniform clauses.
4478 // The uniform clause declares one or more arguments to have an invariant
4479 // value for all concurrent invocations of the function in the execution of a
4480 // single SIMD loop.
4481 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
4482 const Expr *UniformedLinearThis = nullptr;
4483 for (const Expr *E : Uniforms) {
4484 E = E->IgnoreParenImpCasts();
4485 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4486 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
4487 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4488 FD->getParamDecl(PVD->getFunctionScopeIndex())
4489 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
4490 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
4493 if (isa<CXXThisExpr>(E)) {
4494 UniformedLinearThis = E;
4497 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4498 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4500 // OpenMP [2.8.2, declare simd construct, Description]
4501 // The aligned clause declares that the object to which each list item points
4502 // is aligned to the number of bytes expressed in the optional parameter of
4503 // the aligned clause.
4504 // The special this pointer can be used as if was one of the arguments to the
4505 // function in any of the linear, aligned, or uniform clauses.
4506 // The type of list items appearing in the aligned clause must be array,
4507 // pointer, reference to array, or reference to pointer.
4508 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
4509 const Expr *AlignedThis = nullptr;
4510 for (const Expr *E : Aligneds) {
4511 E = E->IgnoreParenImpCasts();
4512 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4513 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4514 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4515 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4516 FD->getParamDecl(PVD->getFunctionScopeIndex())
4517 ->getCanonicalDecl() == CanonPVD) {
4518 // OpenMP [2.8.1, simd construct, Restrictions]
4519 // A list-item cannot appear in more than one aligned clause.
4520 if (AlignedArgs.count(CanonPVD) > 0) {
4521 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4522 << 1 << E->getSourceRange();
4523 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
4524 diag::note_omp_explicit_dsa)
4525 << getOpenMPClauseName(OMPC_aligned);
4528 AlignedArgs[CanonPVD] = E;
4529 QualType QTy = PVD->getType()
4530 .getNonReferenceType()
4531 .getUnqualifiedType()
4532 .getCanonicalType();
4533 const Type *Ty = QTy.getTypePtrOrNull();
4534 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
4535 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
4536 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
4537 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
4542 if (isa<CXXThisExpr>(E)) {
4544 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4545 << 2 << E->getSourceRange();
4546 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
4547 << getOpenMPClauseName(OMPC_aligned);
4552 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4553 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4555 // The optional parameter of the aligned clause, alignment, must be a constant
4556 // positive integer expression. If no optional parameter is specified,
4557 // implementation-defined default alignments for SIMD instructions on the
4558 // target platforms are assumed.
4559 SmallVector<const Expr *, 4> NewAligns;
4560 for (Expr *E : Alignments) {
4563 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
4564 NewAligns.push_back(Align.get());
4566 // OpenMP [2.8.2, declare simd construct, Description]
4567 // The linear clause declares one or more list items to be private to a SIMD
4568 // lane and to have a linear relationship with respect to the iteration space
4570 // The special this pointer can be used as if was one of the arguments to the
4571 // function in any of the linear, aligned, or uniform clauses.
4572 // When a linear-step expression is specified in a linear clause it must be
4573 // either a constant integer expression or an integer-typed parameter that is
4574 // specified in a uniform clause on the directive.
4575 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
4576 const bool IsUniformedThis = UniformedLinearThis != nullptr;
4577 auto MI = LinModifiers.begin();
4578 for (const Expr *E : Linears) {
4579 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
4581 E = E->IgnoreParenImpCasts();
4582 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4583 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4584 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4585 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4586 FD->getParamDecl(PVD->getFunctionScopeIndex())
4587 ->getCanonicalDecl() == CanonPVD) {
4588 // OpenMP [2.15.3.7, linear Clause, Restrictions]
4589 // A list-item cannot appear in more than one linear clause.
4590 if (LinearArgs.count(CanonPVD) > 0) {
4591 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4592 << getOpenMPClauseName(OMPC_linear)
4593 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
4594 Diag(LinearArgs[CanonPVD]->getExprLoc(),
4595 diag::note_omp_explicit_dsa)
4596 << getOpenMPClauseName(OMPC_linear);
4599 // Each argument can appear in at most one uniform or linear clause.
4600 if (UniformedArgs.count(CanonPVD) > 0) {
4601 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4602 << getOpenMPClauseName(OMPC_linear)
4603 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
4604 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
4605 diag::note_omp_explicit_dsa)
4606 << getOpenMPClauseName(OMPC_uniform);
4609 LinearArgs[CanonPVD] = E;
4610 if (E->isValueDependent() || E->isTypeDependent() ||
4611 E->isInstantiationDependent() ||
4612 E->containsUnexpandedParameterPack())
4614 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4615 PVD->getOriginalType());
4619 if (isa<CXXThisExpr>(E)) {
4620 if (UniformedLinearThis) {
4621 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4622 << getOpenMPClauseName(OMPC_linear)
4623 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
4624 << E->getSourceRange();
4625 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4626 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4630 UniformedLinearThis = E;
4631 if (E->isValueDependent() || E->isTypeDependent() ||
4632 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4634 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4638 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4639 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4641 Expr *Step = nullptr;
4642 Expr *NewStep = nullptr;
4643 SmallVector<Expr *, 4> NewSteps;
4644 for (Expr *E : Steps) {
4645 // Skip the same step expression, it was checked already.
4646 if (Step == E || !E) {
4647 NewSteps.push_back(E ? NewStep : nullptr);
4651 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
4652 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4653 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4654 if (UniformedArgs.count(CanonPVD) == 0) {
4655 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4656 << Step->getSourceRange();
4657 } else if (E->isValueDependent() || E->isTypeDependent() ||
4658 E->isInstantiationDependent() ||
4659 E->containsUnexpandedParameterPack() ||
4660 CanonPVD->getType()->hasIntegerRepresentation()) {
4661 NewSteps.push_back(Step);
4663 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4664 << Step->getSourceRange();
4669 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4670 !Step->isInstantiationDependent() &&
4671 !Step->containsUnexpandedParameterPack()) {
4672 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4675 NewStep = VerifyIntegerConstantExpression(NewStep).get();
4677 NewSteps.push_back(NewStep);
4679 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4680 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
4681 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
4682 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4683 const_cast<Expr **>(Linears.data()), Linears.size(),
4684 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4685 NewSteps.data(), NewSteps.size(), SR);
4686 ADecl->addAttr(NewAttr);
4687 return ConvertDeclToDeclGroup(ADecl);
4690 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4692 SourceLocation StartLoc,
4693 SourceLocation EndLoc) {
4697 auto *CS = cast<CapturedStmt>(AStmt);
4698 // 1.2.2 OpenMP Language Terminology
4699 // Structured block - An executable statement with a single entry at the
4700 // top and a single exit at the bottom.
4701 // The point of exit cannot be a branch out of the structured block.
4702 // longjmp() and throw() must not violate the entry/exit criteria.
4703 CS->getCapturedDecl()->setNothrow();
4705 setFunctionHasBranchProtectedScope();
4707 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4708 DSAStack->isCancelRegion());
4712 /// Helper class for checking canonical form of the OpenMP loops and
4713 /// extracting iteration space of each loop in the loop nest, that will be used
4714 /// for IR generation.
4715 class OpenMPIterationSpaceChecker {
4716 /// Reference to Sema.
4718 /// Data-sharing stack.
4720 /// A location for diagnostics (when there is no some better location).
4721 SourceLocation DefaultLoc;
4722 /// A location for diagnostics (when increment is not compatible).
4723 SourceLocation ConditionLoc;
4724 /// A source location for referring to loop init later.
4725 SourceRange InitSrcRange;
4726 /// A source location for referring to condition later.
4727 SourceRange ConditionSrcRange;
4728 /// A source location for referring to increment later.
4729 SourceRange IncrementSrcRange;
4731 ValueDecl *LCDecl = nullptr;
4732 /// Reference to loop variable.
4733 Expr *LCRef = nullptr;
4734 /// Lower bound (initializer for the var).
4738 /// Loop step (increment).
4739 Expr *Step = nullptr;
4740 /// This flag is true when condition is one of:
4745 /// This will have no value when the condition is !=
4746 llvm::Optional<bool> TestIsLessOp;
4747 /// This flag is true when condition is strict ( < or > ).
4748 bool TestIsStrictOp = false;
4749 /// This flag is true when step is subtracted on each iteration.
4750 bool SubtractStep = false;
4751 /// The outer loop counter this loop depends on (if any).
4752 const ValueDecl *DepDecl = nullptr;
4753 /// Contains number of loop (starts from 1) on which loop counter init
4754 /// expression of this loop depends on.
4755 Optional<unsigned> InitDependOnLC;
4756 /// Contains number of loop (starts from 1) on which loop counter condition
4757 /// expression of this loop depends on.
4758 Optional<unsigned> CondDependOnLC;
4759 /// Checks if the provide statement depends on the loop counter.
4760 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
4763 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack,
4764 SourceLocation DefaultLoc)
4765 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc),
4766 ConditionLoc(DefaultLoc) {}
4767 /// Check init-expr for canonical loop form and save loop counter
4768 /// variable - #Var and its initialization value - #LB.
4769 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
4770 /// Check test-expr for canonical form, save upper-bound (#UB), flags
4771 /// for less/greater and for strict/non-strict comparison.
4772 bool checkAndSetCond(Expr *S);
4773 /// Check incr-expr for canonical loop form and return true if it
4774 /// does not conform, otherwise save loop step (#Step).
4775 bool checkAndSetInc(Expr *S);
4776 /// Return the loop counter variable.
4777 ValueDecl *getLoopDecl() const { return LCDecl; }
4778 /// Return the reference expression to loop counter variable.
4779 Expr *getLoopDeclRefExpr() const { return LCRef; }
4780 /// Source range of the loop init.
4781 SourceRange getInitSrcRange() const { return InitSrcRange; }
4782 /// Source range of the loop condition.
4783 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
4784 /// Source range of the loop increment.
4785 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
4786 /// True if the step should be subtracted.
4787 bool shouldSubtractStep() const { return SubtractStep; }
4788 /// True, if the compare operator is strict (<, > or !=).
4789 bool isStrictTestOp() const { return TestIsStrictOp; }
4790 /// Build the expression to calculate the number of iterations.
4791 Expr *buildNumIterations(
4792 Scope *S, const bool LimitedType,
4793 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
4794 /// Build the precondition expression for the loops.
4796 buildPreCond(Scope *S, Expr *Cond,
4797 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
4798 /// Build reference expression to the counter be used for codegen.
4800 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4801 DSAStackTy &DSA) const;
4802 /// Build reference expression to the private counter be used for
4804 Expr *buildPrivateCounterVar() const;
4805 /// Build initialization of the counter be used for codegen.
4806 Expr *buildCounterInit() const;
4807 /// Build step of the counter be used for codegen.
4808 Expr *buildCounterStep() const;
4809 /// Build loop data with counter value for depend clauses in ordered
4812 buildOrderedLoopData(Scope *S, Expr *Counter,
4813 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4814 SourceLocation Loc, Expr *Inc = nullptr,
4815 OverloadedOperatorKind OOK = OO_Amp);
4816 /// Return true if any expression is dependent.
4817 bool dependent() const;
4820 /// Check the right-hand side of an assignment in the increment
4822 bool checkAndSetIncRHS(Expr *RHS);
4823 /// Helper to set loop counter variable and its initializer.
4824 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
4826 /// Helper to set upper bound.
4827 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
4828 SourceRange SR, SourceLocation SL);
4829 /// Helper to set loop increment.
4830 bool setStep(Expr *NewStep, bool Subtract);
4833 bool OpenMPIterationSpaceChecker::dependent() const {
4835 assert(!LB && !UB && !Step);
4838 return LCDecl->getType()->isDependentType() ||
4839 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4840 (Step && Step->isValueDependent());
4843 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
4845 Expr *NewLB, bool EmitDiags) {
4846 // State consistency checking to ensure correct usage.
4847 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
4848 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4849 if (!NewLCDecl || !NewLB)
4851 LCDecl = getCanonicalDecl(NewLCDecl);
4852 LCRef = NewLCRefExpr;
4853 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4854 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4855 if ((Ctor->isCopyOrMoveConstructor() ||
4856 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4857 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4858 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
4861 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
4865 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
4866 llvm::Optional<bool> LessOp,
4867 bool StrictOp, SourceRange SR,
4868 SourceLocation SL) {
4869 // State consistency checking to ensure correct usage.
4870 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4871 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4876 TestIsLessOp = LessOp;
4877 TestIsStrictOp = StrictOp;
4878 ConditionSrcRange = SR;
4880 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
4884 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
4885 // State consistency checking to ensure correct usage.
4886 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
4889 if (!NewStep->isValueDependent()) {
4890 // Check that the step is integer expression.
4891 SourceLocation StepLoc = NewStep->getBeginLoc();
4892 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4893 StepLoc, getExprAsWritten(NewStep));
4894 if (Val.isInvalid())
4896 NewStep = Val.get();
4898 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4899 // If test-expr is of form var relational-op b and relational-op is < or
4900 // <= then incr-expr must cause var to increase on each iteration of the
4901 // loop. If test-expr is of form var relational-op b and relational-op is
4902 // > or >= then incr-expr must cause var to decrease on each iteration of
4904 // If test-expr is of form b relational-op var and relational-op is < or
4905 // <= then incr-expr must cause var to decrease on each iteration of the
4906 // loop. If test-expr is of form b relational-op var and relational-op is
4907 // > or >= then incr-expr must cause var to increase on each iteration of
4909 llvm::APSInt Result;
4910 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4911 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4913 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
4915 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
4916 bool IsConstZero = IsConstant && !Result.getBoolValue();
4918 // != with increment is treated as <; != with decrement is treated as >
4919 if (!TestIsLessOp.hasValue())
4920 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
4921 if (UB && (IsConstZero ||
4922 (TestIsLessOp.getValue() ?
4923 (IsConstNeg || (IsUnsigned && Subtract)) :
4924 (IsConstPos || (IsUnsigned && !Subtract))))) {
4925 SemaRef.Diag(NewStep->getExprLoc(),
4926 diag::err_omp_loop_incr_not_compatible)
4927 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
4928 SemaRef.Diag(ConditionLoc,
4929 diag::note_omp_loop_cond_requres_compatible_incr)
4930 << TestIsLessOp.getValue() << ConditionSrcRange;
4933 if (TestIsLessOp.getValue() == Subtract) {
4935 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4937 Subtract = !Subtract;
4942 SubtractStep = Subtract;
4947 /// Checker for the non-rectangular loops. Checks if the initializer or
4948 /// condition expression references loop counter variable.
4949 class LoopCounterRefChecker final
4950 : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
4953 const ValueDecl *CurLCDecl = nullptr;
4954 const ValueDecl *DepDecl = nullptr;
4955 const ValueDecl *PrevDepDecl = nullptr;
4956 bool IsInitializer = true;
4957 unsigned BaseLoopId = 0;
4958 bool checkDecl(const Expr *E, const ValueDecl *VD) {
4959 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
4960 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
4961 << (IsInitializer ? 0 : 1);
4964 const auto &&Data = Stack.isLoopControlVariable(VD);
4965 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
4966 // The type of the loop iterator on which we depend may not have a random
4967 // access iterator type.
4968 if (Data.first && VD->getType()->isRecordType()) {
4969 SmallString<128> Name;
4970 llvm::raw_svector_ostream OS(Name);
4971 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
4972 /*Qualified=*/true);
4973 SemaRef.Diag(E->getExprLoc(),
4974 diag::err_omp_wrong_dependency_iterator_type)
4976 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
4980 (DepDecl || (PrevDepDecl &&
4981 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
4982 if (!DepDecl && PrevDepDecl)
4983 DepDecl = PrevDepDecl;
4984 SmallString<128> Name;
4985 llvm::raw_svector_ostream OS(Name);
4986 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
4987 /*Qualified=*/true);
4988 SemaRef.Diag(E->getExprLoc(),
4989 diag::err_omp_invariant_or_linear_dependency)
4995 BaseLoopId = Data.first;
5001 bool VisitDeclRefExpr(const DeclRefExpr *E) {
5002 const ValueDecl *VD = E->getDecl();
5003 if (isa<VarDecl>(VD))
5004 return checkDecl(E, VD);
5007 bool VisitMemberExpr(const MemberExpr *E) {
5008 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
5009 const ValueDecl *VD = E->getMemberDecl();
5010 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
5011 return checkDecl(E, VD);
5015 bool VisitStmt(const Stmt *S) {
5017 for (const Stmt *Child : S->children())
5018 Res = Child && Visit(Child) && Res;
5021 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
5022 const ValueDecl *CurLCDecl, bool IsInitializer,
5023 const ValueDecl *PrevDepDecl = nullptr)
5024 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
5025 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {}
5026 unsigned getBaseLoopId() const {
5027 assert(CurLCDecl && "Expected loop dependency.");
5030 const ValueDecl *getDepDecl() const {
5031 assert(CurLCDecl && "Expected loop dependency.");
5038 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
5039 bool IsInitializer) {
5040 // Check for the non-rectangular loops.
5041 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
5043 if (LoopStmtChecker.Visit(S)) {
5044 DepDecl = LoopStmtChecker.getDepDecl();
5045 return LoopStmtChecker.getBaseLoopId();
5050 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
5051 // Check init-expr for canonical loop form and save loop counter
5052 // variable - #Var and its initialization value - #LB.
5053 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
5055 // integer-type var = lb
5056 // random-access-iterator-type var = lb
5057 // pointer-type var = lb
5061 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
5065 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5066 if (!ExprTemp->cleanupsHaveSideEffects())
5067 S = ExprTemp->getSubExpr();
5069 InitSrcRange = S->getSourceRange();
5070 if (Expr *E = dyn_cast<Expr>(S))
5071 S = E->IgnoreParens();
5072 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
5073 if (BO->getOpcode() == BO_Assign) {
5074 Expr *LHS = BO->getLHS()->IgnoreParens();
5075 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5076 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5077 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
5078 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5080 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
5082 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5083 if (ME->isArrow() &&
5084 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5085 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5089 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
5090 if (DS->isSingleDecl()) {
5091 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
5092 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
5093 // Accept non-canonical init form here but emit ext. warning.
5094 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
5095 SemaRef.Diag(S->getBeginLoc(),
5096 diag::ext_omp_loop_not_canonical_init)
5097 << S->getSourceRange();
5098 return setLCDeclAndLB(
5100 buildDeclRefExpr(SemaRef, Var,
5101 Var->getType().getNonReferenceType(),
5103 Var->getInit(), EmitDiags);
5107 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
5108 if (CE->getOperator() == OO_Equal) {
5109 Expr *LHS = CE->getArg(0);
5110 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5111 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5112 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
5113 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5115 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
5117 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5118 if (ME->isArrow() &&
5119 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5120 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5126 if (dependent() || SemaRef.CurContext->isDependentContext())
5129 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
5130 << S->getSourceRange();
5135 /// Ignore parenthesizes, implicit casts, copy constructor and return the
5136 /// variable (which may be the loop variable) if possible.
5137 static const ValueDecl *getInitLCDecl(const Expr *E) {
5140 E = getExprAsWritten(E);
5141 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
5142 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
5143 if ((Ctor->isCopyOrMoveConstructor() ||
5144 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5145 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
5146 E = CE->getArg(0)->IgnoreParenImpCasts();
5147 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
5148 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
5149 return getCanonicalDecl(VD);
5151 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
5152 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5153 return getCanonicalDecl(ME->getMemberDecl());
5157 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
5158 // Check test-expr for canonical form, save upper-bound UB, flags for
5159 // less/greater and for strict/non-strict comparison.
5160 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
5161 // var relational-op b
5162 // b relational-op var
5165 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
5168 S = getExprAsWritten(S);
5169 SourceLocation CondLoc = S->getBeginLoc();
5170 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
5171 if (BO->isRelationalOp()) {
5172 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5173 return setUB(BO->getRHS(),
5174 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
5175 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5176 BO->getSourceRange(), BO->getOperatorLoc());
5177 if (getInitLCDecl(BO->getRHS()) == LCDecl)
5178 return setUB(BO->getLHS(),
5179 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
5180 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5181 BO->getSourceRange(), BO->getOperatorLoc());
5182 } else if (BO->getOpcode() == BO_NE)
5183 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
5184 BO->getRHS() : BO->getLHS(),
5185 /*LessOp=*/llvm::None,
5187 BO->getSourceRange(), BO->getOperatorLoc());
5188 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
5189 if (CE->getNumArgs() == 2) {
5190 auto Op = CE->getOperator();
5193 case OO_GreaterEqual:
5196 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5197 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
5198 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5199 CE->getOperatorLoc());
5200 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
5201 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
5202 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5203 CE->getOperatorLoc());
5205 case OO_ExclaimEqual:
5206 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
5207 CE->getArg(1) : CE->getArg(0),
5208 /*LessOp=*/llvm::None,
5210 CE->getSourceRange(),
5211 CE->getOperatorLoc());
5218 if (dependent() || SemaRef.CurContext->isDependentContext())
5220 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
5221 << S->getSourceRange() << LCDecl;
5225 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
5226 // RHS of canonical loop form increment can be:
5231 RHS = RHS->IgnoreParenImpCasts();
5232 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
5233 if (BO->isAdditiveOp()) {
5234 bool IsAdd = BO->getOpcode() == BO_Add;
5235 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5236 return setStep(BO->getRHS(), !IsAdd);
5237 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
5238 return setStep(BO->getLHS(), /*Subtract=*/false);
5240 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
5241 bool IsAdd = CE->getOperator() == OO_Plus;
5242 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
5243 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5244 return setStep(CE->getArg(1), !IsAdd);
5245 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
5246 return setStep(CE->getArg(0), /*Subtract=*/false);
5249 if (dependent() || SemaRef.CurContext->isDependentContext())
5251 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
5252 << RHS->getSourceRange() << LCDecl;
5256 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
5257 // Check incr-expr for canonical loop form and return true if it
5258 // does not conform.
5259 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
5271 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
5274 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5275 if (!ExprTemp->cleanupsHaveSideEffects())
5276 S = ExprTemp->getSubExpr();
5278 IncrementSrcRange = S->getSourceRange();
5279 S = S->IgnoreParens();
5280 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
5281 if (UO->isIncrementDecrementOp() &&
5282 getInitLCDecl(UO->getSubExpr()) == LCDecl)
5283 return setStep(SemaRef
5284 .ActOnIntegerConstant(UO->getBeginLoc(),
5285 (UO->isDecrementOp() ? -1 : 1))
5287 /*Subtract=*/false);
5288 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
5289 switch (BO->getOpcode()) {
5292 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5293 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
5296 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5297 return checkAndSetIncRHS(BO->getRHS());
5302 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
5303 switch (CE->getOperator()) {
5306 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5307 return setStep(SemaRef
5308 .ActOnIntegerConstant(
5310 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
5312 /*Subtract=*/false);
5316 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5317 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
5320 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5321 return checkAndSetIncRHS(CE->getArg(1));
5327 if (dependent() || SemaRef.CurContext->isDependentContext())
5329 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
5330 << S->getSourceRange() << LCDecl;
5335 tryBuildCapture(Sema &SemaRef, Expr *Capture,
5336 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5337 if (SemaRef.CurContext->isDependentContext())
5338 return ExprResult(Capture);
5339 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
5340 return SemaRef.PerformImplicitConversion(
5341 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
5342 /*AllowExplicit=*/true);
5343 auto I = Captures.find(Capture);
5344 if (I != Captures.end())
5345 return buildCapture(SemaRef, Capture, I->second);
5346 DeclRefExpr *Ref = nullptr;
5347 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
5348 Captures[Capture] = Ref;
5352 /// Build the expression to calculate the number of iterations.
5353 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
5354 Scope *S, const bool LimitedType,
5355 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
5357 QualType VarType = LCDecl->getType().getNonReferenceType();
5358 if (VarType->isIntegerType() || VarType->isPointerType() ||
5359 SemaRef.getLangOpts().CPlusPlus) {
5361 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
5362 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
5363 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
5364 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
5365 if (!Upper || !Lower)
5368 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
5370 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
5371 // BuildBinOp already emitted error, this one is to point user to upper
5372 // and lower bound, and to tell what is passed to 'operator-'.
5373 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
5374 << Upper->getSourceRange() << Lower->getSourceRange();
5379 if (!Diff.isUsable())
5382 // Upper - Lower [- 1]
5384 Diff = SemaRef.BuildBinOp(
5385 S, DefaultLoc, BO_Sub, Diff.get(),
5386 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5387 if (!Diff.isUsable())
5390 // Upper - Lower [- 1] + Step
5391 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
5392 if (!NewStep.isUsable())
5394 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
5395 if (!Diff.isUsable())
5398 // Parentheses (for dumping/debugging purposes only).
5399 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5400 if (!Diff.isUsable())
5403 // (Upper - Lower [- 1] + Step) / Step
5404 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
5405 if (!Diff.isUsable())
5408 // OpenMP runtime requires 32-bit or 64-bit loop variables.
5409 QualType Type = Diff.get()->getType();
5410 ASTContext &C = SemaRef.Context;
5411 bool UseVarType = VarType->hasIntegerRepresentation() &&
5412 C.getTypeSize(Type) > C.getTypeSize(VarType);
5413 if (!Type->isIntegerType() || UseVarType) {
5415 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
5416 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
5417 : Type->hasSignedIntegerRepresentation();
5418 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
5419 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
5420 Diff = SemaRef.PerformImplicitConversion(
5421 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
5422 if (!Diff.isUsable())
5427 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
5428 if (NewSize != C.getTypeSize(Type)) {
5429 if (NewSize < C.getTypeSize(Type)) {
5430 assert(NewSize == 64 && "incorrect loop var size");
5431 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
5432 << InitSrcRange << ConditionSrcRange;
5434 QualType NewType = C.getIntTypeForBitwidth(
5435 NewSize, Type->hasSignedIntegerRepresentation() ||
5436 C.getTypeSize(Type) < NewSize);
5437 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
5438 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
5439 Sema::AA_Converting, true);
5440 if (!Diff.isUsable())
5449 Expr *OpenMPIterationSpaceChecker::buildPreCond(
5450 Scope *S, Expr *Cond,
5451 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
5452 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
5453 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5454 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5456 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
5457 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
5458 if (!NewLB.isUsable() || !NewUB.isUsable())
5461 ExprResult CondExpr =
5462 SemaRef.BuildBinOp(S, DefaultLoc,
5463 TestIsLessOp.getValue() ?
5464 (TestIsStrictOp ? BO_LT : BO_LE) :
5465 (TestIsStrictOp ? BO_GT : BO_GE),
5466 NewLB.get(), NewUB.get());
5467 if (CondExpr.isUsable()) {
5468 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
5469 SemaRef.Context.BoolTy))
5470 CondExpr = SemaRef.PerformImplicitConversion(
5471 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
5472 /*AllowExplicit=*/true);
5474 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5475 // Otherwise use original loop condition and evaluate it in runtime.
5476 return CondExpr.isUsable() ? CondExpr.get() : Cond;
5479 /// Build reference expression to the counter be used for codegen.
5480 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
5481 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5482 DSAStackTy &DSA) const {
5483 auto *VD = dyn_cast<VarDecl>(LCDecl);
5485 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
5486 DeclRefExpr *Ref = buildDeclRefExpr(
5487 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
5488 const DSAStackTy::DSAVarData Data =
5489 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
5490 // If the loop control decl is explicitly marked as private, do not mark it
5491 // as captured again.
5492 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
5493 Captures.insert(std::make_pair(LCRef, Ref));
5496 return cast<DeclRefExpr>(LCRef);
5499 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
5500 if (LCDecl && !LCDecl->isInvalidDecl()) {
5501 QualType Type = LCDecl->getType().getNonReferenceType();
5502 VarDecl *PrivateVar = buildVarDecl(
5503 SemaRef, DefaultLoc, Type, LCDecl->getName(),
5504 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
5505 isa<VarDecl>(LCDecl)
5506 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
5508 if (PrivateVar->isInvalidDecl())
5510 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
5515 /// Build initialization of the counter to be used for codegen.
5516 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
5518 /// Build step of the counter be used for codegen.
5519 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
5521 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
5522 Scope *S, Expr *Counter,
5523 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
5524 Expr *Inc, OverloadedOperatorKind OOK) {
5525 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
5529 assert((OOK == OO_Plus || OOK == OO_Minus) &&
5530 "Expected only + or - operations for depend clauses.");
5531 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
5532 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
5537 QualType VarType = LCDecl->getType().getNonReferenceType();
5538 if (VarType->isIntegerType() || VarType->isPointerType() ||
5539 SemaRef.getLangOpts().CPlusPlus) {
5541 Expr *Upper = TestIsLessOp.getValue()
5543 : tryBuildCapture(SemaRef, UB, Captures).get();
5544 Expr *Lower = TestIsLessOp.getValue()
5545 ? tryBuildCapture(SemaRef, LB, Captures).get()
5547 if (!Upper || !Lower)
5550 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
5552 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
5553 // BuildBinOp already emitted error, this one is to point user to upper
5554 // and lower bound, and to tell what is passed to 'operator-'.
5555 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
5556 << Upper->getSourceRange() << Lower->getSourceRange();
5561 if (!Diff.isUsable())
5564 // Parentheses (for dumping/debugging purposes only).
5565 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5566 if (!Diff.isUsable())
5569 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
5570 if (!NewStep.isUsable())
5572 // (Upper - Lower) / Step
5573 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
5574 if (!Diff.isUsable())
5580 /// Iteration space of a single for loop.
5581 struct LoopIterationSpace final {
5582 /// True if the condition operator is the strict compare operator (<, > or
5584 bool IsStrictCompare = false;
5585 /// Condition of the loop.
5586 Expr *PreCond = nullptr;
5587 /// This expression calculates the number of iterations in the loop.
5588 /// It is always possible to calculate it before starting the loop.
5589 Expr *NumIterations = nullptr;
5590 /// The loop counter variable.
5591 Expr *CounterVar = nullptr;
5592 /// Private loop counter variable.
5593 Expr *PrivateCounterVar = nullptr;
5594 /// This is initializer for the initial value of #CounterVar.
5595 Expr *CounterInit = nullptr;
5596 /// This is step for the #CounterVar used to generate its update:
5597 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
5598 Expr *CounterStep = nullptr;
5599 /// Should step be subtracted?
5600 bool Subtract = false;
5601 /// Source range of the loop init.
5602 SourceRange InitSrcRange;
5603 /// Source range of the loop condition.
5604 SourceRange CondSrcRange;
5605 /// Source range of the loop increment.
5606 SourceRange IncSrcRange;
5611 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
5612 assert(getLangOpts().OpenMP && "OpenMP is not active.");
5613 assert(Init && "Expected loop in canonical form.");
5614 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
5615 if (AssociatedLoops > 0 &&
5616 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
5617 DSAStack->loopStart();
5618 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc);
5619 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
5620 if (ValueDecl *D = ISC.getLoopDecl()) {
5621 auto *VD = dyn_cast<VarDecl>(D);
5622 DeclRefExpr *PrivateRef = nullptr;
5624 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
5627 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
5628 /*WithInit=*/false);
5629 VD = cast<VarDecl>(PrivateRef->getDecl());
5632 DSAStack->addLoopControlVariable(D, VD);
5633 const Decl *LD = DSAStack->getPossiblyLoopCunter();
5634 if (LD != D->getCanonicalDecl()) {
5635 DSAStack->resetPossibleLoopCounter();
5636 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
5637 MarkDeclarationsReferencedInExpr(
5638 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
5639 Var->getType().getNonLValueExprType(Context),
5640 ForLoc, /*RefersToCapture=*/true));
5642 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
5643 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
5644 // Referenced in a Construct, C/C++]. The loop iteration variable in the
5645 // associated for-loop of a simd construct with just one associated
5646 // for-loop may be listed in a linear clause with a constant-linear-step
5647 // that is the increment of the associated for-loop. The loop iteration
5648 // variable(s) in the associated for-loop(s) of a for or parallel for
5649 // construct may be listed in a private or lastprivate clause.
5650 DSAStackTy::DSAVarData DVar =
5651 DSAStack->getTopDSA(D, /*FromParent=*/false);
5652 // If LoopVarRefExpr is nullptr it means the corresponding loop variable
5653 // is declared in the loop and it is predetermined as a private.
5654 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
5655 OpenMPClauseKind PredeterminedCKind =
5656 isOpenMPSimdDirective(DKind)
5657 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
5659 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5660 DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
5661 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
5662 DVar.CKind != OMPC_private))) ||
5663 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
5664 isOpenMPDistributeDirective(DKind)) &&
5665 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5666 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
5667 (DVar.CKind != OMPC_private || DVar.RefExpr)) {
5668 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
5669 << getOpenMPClauseName(DVar.CKind)
5670 << getOpenMPDirectiveName(DKind)
5671 << getOpenMPClauseName(PredeterminedCKind);
5672 if (DVar.RefExpr == nullptr)
5673 DVar.CKind = PredeterminedCKind;
5674 reportOriginalDsa(*this, DSAStack, D, DVar,
5675 /*IsLoopIterVar=*/true);
5676 } else if (LoopDeclRefExpr) {
5677 // Make the loop iteration variable private (for worksharing
5678 // constructs), linear (for simd directives with the only one
5679 // associated loop) or lastprivate (for simd directives with several
5680 // collapsed or ordered loops).
5681 if (DVar.CKind == OMPC_unknown)
5682 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
5687 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
5691 /// Called on a for stmt to check and extract its iteration space
5692 /// for further processing (such as collapsing).
5693 static bool checkOpenMPIterationSpace(
5694 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
5695 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
5696 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
5697 Expr *OrderedLoopCountExpr,
5698 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
5699 LoopIterationSpace &ResultIterSpace,
5700 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5701 // OpenMP [2.6, Canonical Loop Form]
5702 // for (init-expr; test-expr; incr-expr) structured-block
5703 auto *For = dyn_cast_or_null<ForStmt>(S);
5705 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
5706 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
5707 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
5708 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
5709 if (TotalNestedLoopCount > 1) {
5710 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
5711 SemaRef.Diag(DSA.getConstructLoc(),
5712 diag::note_omp_collapse_ordered_expr)
5713 << 2 << CollapseLoopCountExpr->getSourceRange()
5714 << OrderedLoopCountExpr->getSourceRange();
5715 else if (CollapseLoopCountExpr)
5716 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5717 diag::note_omp_collapse_ordered_expr)
5718 << 0 << CollapseLoopCountExpr->getSourceRange();
5720 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5721 diag::note_omp_collapse_ordered_expr)
5722 << 1 << OrderedLoopCountExpr->getSourceRange();
5726 assert(For->getBody());
5728 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, For->getForLoc());
5731 Stmt *Init = For->getInit();
5732 if (ISC.checkAndSetInit(Init))
5735 bool HasErrors = false;
5737 // Check loop variable's type.
5738 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
5739 // OpenMP [2.6, Canonical Loop Form]
5740 // Var is one of the following:
5741 // A variable of signed or unsigned integer type.
5742 // For C++, a variable of a random access iterator type.
5743 // For C, a variable of a pointer type.
5744 QualType VarType = LCDecl->getType().getNonReferenceType();
5745 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
5746 !VarType->isPointerType() &&
5747 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
5748 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
5749 << SemaRef.getLangOpts().CPlusPlus;
5753 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
5755 // The loop iteration variable(s) in the associated for-loop(s) of a for or
5756 // parallel for construct is (are) private.
5757 // The loop iteration variable in the associated for-loop of a simd
5758 // construct with just one associated for-loop is linear with a
5759 // constant-linear-step that is the increment of the associated for-loop.
5760 // Exclude loop var from the list of variables with implicitly defined data
5761 // sharing attributes.
5762 VarsWithImplicitDSA.erase(LCDecl);
5764 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
5767 HasErrors |= ISC.checkAndSetCond(For->getCond());
5770 HasErrors |= ISC.checkAndSetInc(For->getInc());
5773 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
5776 // Build the loop's iteration space representation.
5777 ResultIterSpace.PreCond =
5778 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
5779 ResultIterSpace.NumIterations = ISC.buildNumIterations(
5781 (isOpenMPWorksharingDirective(DKind) ||
5782 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
5784 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
5785 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
5786 ResultIterSpace.CounterInit = ISC.buildCounterInit();
5787 ResultIterSpace.CounterStep = ISC.buildCounterStep();
5788 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
5789 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
5790 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
5791 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
5792 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp();
5794 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
5795 ResultIterSpace.NumIterations == nullptr ||
5796 ResultIterSpace.CounterVar == nullptr ||
5797 ResultIterSpace.PrivateCounterVar == nullptr ||
5798 ResultIterSpace.CounterInit == nullptr ||
5799 ResultIterSpace.CounterStep == nullptr);
5800 if (!HasErrors && DSA.isOrderedRegion()) {
5801 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
5802 if (CurrentNestedLoopCount <
5803 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
5804 DSA.getOrderedRegionParam().second->setLoopNumIterations(
5805 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
5806 DSA.getOrderedRegionParam().second->setLoopCounter(
5807 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
5810 for (auto &Pair : DSA.getDoacrossDependClauses()) {
5811 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
5812 // Erroneous case - clause has some problems.
5815 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
5816 Pair.second.size() <= CurrentNestedLoopCount) {
5817 // Erroneous case - clause has some problems.
5818 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
5822 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5823 CntValue = ISC.buildOrderedLoopData(
5824 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5825 Pair.first->getDependencyLoc());
5827 CntValue = ISC.buildOrderedLoopData(
5828 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5829 Pair.first->getDependencyLoc(),
5830 Pair.second[CurrentNestedLoopCount].first,
5831 Pair.second[CurrentNestedLoopCount].second);
5832 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
5839 /// Build 'VarRef = Start.
5841 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
5843 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5844 // Build 'VarRef = Start.
5845 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
5846 if (!NewStart.isUsable())
5848 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
5849 VarRef.get()->getType())) {
5850 NewStart = SemaRef.PerformImplicitConversion(
5851 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
5852 /*AllowExplicit=*/true);
5853 if (!NewStart.isUsable())
5858 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5862 /// Build 'VarRef = Start + Iter * Step'.
5863 static ExprResult buildCounterUpdate(
5864 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
5865 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
5866 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
5867 // Add parentheses (for debugging purposes only).
5868 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
5869 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
5873 ExprResult NewStep = Step;
5875 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
5876 if (NewStep.isInvalid())
5879 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
5880 if (!Update.isUsable())
5883 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
5884 // 'VarRef = Start (+|-) Iter * Step'.
5885 ExprResult NewStart = Start;
5887 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
5888 if (NewStart.isInvalid())
5891 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
5892 ExprResult SavedUpdate = Update;
5893 ExprResult UpdateVal;
5894 if (VarRef.get()->getType()->isOverloadableType() ||
5895 NewStart.get()->getType()->isOverloadableType() ||
5896 Update.get()->getType()->isOverloadableType()) {
5897 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5898 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5900 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5901 if (Update.isUsable()) {
5903 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5904 VarRef.get(), SavedUpdate.get());
5905 if (UpdateVal.isUsable()) {
5906 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5910 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5913 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5914 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5915 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5916 NewStart.get(), SavedUpdate.get());
5917 if (!Update.isUsable())
5920 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5921 VarRef.get()->getType())) {
5922 Update = SemaRef.PerformImplicitConversion(
5923 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5924 if (!Update.isUsable())
5928 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5933 /// Convert integer expression \a E to make it have at least \a Bits
5935 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
5938 ASTContext &C = SemaRef.Context;
5939 QualType OldType = E->getType();
5940 unsigned HasBits = C.getTypeSize(OldType);
5941 if (HasBits >= Bits)
5942 return ExprResult(E);
5943 // OK to convert to signed, because new type has more bits than old.
5944 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5945 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5949 /// Check if the given expression \a E is a constant integer that fits
5950 /// into \a Bits bits.
5951 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
5954 llvm::APSInt Result;
5955 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5956 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5960 /// Build preinits statement for the given declarations.
5961 static Stmt *buildPreInits(ASTContext &Context,
5962 MutableArrayRef<Decl *> PreInits) {
5963 if (!PreInits.empty()) {
5964 return new (Context) DeclStmt(
5965 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5966 SourceLocation(), SourceLocation());
5971 /// Build preinits statement for the given declarations.
5973 buildPreInits(ASTContext &Context,
5974 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5975 if (!Captures.empty()) {
5976 SmallVector<Decl *, 16> PreInits;
5977 for (const auto &Pair : Captures)
5978 PreInits.push_back(Pair.second->getDecl());
5979 return buildPreInits(Context, PreInits);
5984 /// Build postupdate expression for the given list of postupdates expressions.
5985 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5986 Expr *PostUpdate = nullptr;
5987 if (!PostUpdates.empty()) {
5988 for (Expr *E : PostUpdates) {
5989 Expr *ConvE = S.BuildCStyleCastExpr(
5991 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5994 PostUpdate = PostUpdate
5995 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
6004 /// Called on a for stmt to check itself and nested loops (if any).
6005 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
6006 /// number of collapsed loops otherwise.
6008 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
6009 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
6011 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
6012 OMPLoopDirective::HelperExprs &Built) {
6013 unsigned NestedLoopCount = 1;
6014 if (CollapseLoopCountExpr) {
6015 // Found 'collapse' clause - calculate collapse number.
6016 Expr::EvalResult Result;
6017 if (!CollapseLoopCountExpr->isValueDependent() &&
6018 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
6019 NestedLoopCount = Result.Val.getInt().getLimitedValue();
6021 Built.clear(/*Size=*/1);
6025 unsigned OrderedLoopCount = 1;
6026 if (OrderedLoopCountExpr) {
6027 // Found 'ordered' clause - calculate collapse number.
6028 Expr::EvalResult EVResult;
6029 if (!OrderedLoopCountExpr->isValueDependent() &&
6030 OrderedLoopCountExpr->EvaluateAsInt(EVResult,
6031 SemaRef.getASTContext())) {
6032 llvm::APSInt Result = EVResult.Val.getInt();
6033 if (Result.getLimitedValue() < NestedLoopCount) {
6034 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6035 diag::err_omp_wrong_ordered_loop_count)
6036 << OrderedLoopCountExpr->getSourceRange();
6037 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6038 diag::note_collapse_loop_count)
6039 << CollapseLoopCountExpr->getSourceRange();
6041 OrderedLoopCount = Result.getLimitedValue();
6043 Built.clear(/*Size=*/1);
6047 // This is helper routine for loop directives (e.g., 'for', 'simd',
6048 // 'for simd', etc.).
6049 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
6050 SmallVector<LoopIterationSpace, 4> IterSpaces(
6051 std::max(OrderedLoopCount, NestedLoopCount));
6052 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
6053 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
6054 if (checkOpenMPIterationSpace(
6055 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6056 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
6057 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
6060 // Move on to the next nested for loop, or to the loop body.
6061 // OpenMP [2.8.1, simd construct, Restrictions]
6062 // All loops associated with the construct must be perfectly nested; that
6063 // is, there must be no intervening code nor any OpenMP directive between
6065 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
6067 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
6068 if (checkOpenMPIterationSpace(
6069 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6070 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
6071 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
6074 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
6075 // Handle initialization of captured loop iterator variables.
6076 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
6077 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
6078 Captures[DRE] = DRE;
6081 // Move on to the next nested for loop, or to the loop body.
6082 // OpenMP [2.8.1, simd construct, Restrictions]
6083 // All loops associated with the construct must be perfectly nested; that
6084 // is, there must be no intervening code nor any OpenMP directive between
6086 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
6089 Built.clear(/* size */ NestedLoopCount);
6091 if (SemaRef.CurContext->isDependentContext())
6092 return NestedLoopCount;
6094 // An example of what is generated for the following code:
6096 // #pragma omp simd collapse(2) ordered(2)
6097 // for (i = 0; i < NI; ++i)
6098 // for (k = 0; k < NK; ++k)
6099 // for (j = J0; j < NJ; j+=2) {
6103 // We generate the code below.
6104 // Note: the loop body may be outlined in CodeGen.
6105 // Note: some counters may be C++ classes, operator- is used to find number of
6106 // iterations and operator+= to calculate counter value.
6107 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
6108 // or i64 is currently supported).
6110 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
6111 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
6112 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
6113 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
6114 // // similar updates for vars in clauses (e.g. 'linear')
6115 // <loop body (using local i and j)>
6117 // i = NI; // assign final values of counters
6121 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
6122 // the iteration counts of the collapsed for loops.
6123 // Precondition tests if there is at least one iteration (all conditions are
6125 auto PreCond = ExprResult(IterSpaces[0].PreCond);
6126 Expr *N0 = IterSpaces[0].NumIterations;
6127 ExprResult LastIteration32 =
6128 widenIterationCount(/*Bits=*/32,
6130 .PerformImplicitConversion(
6131 N0->IgnoreImpCasts(), N0->getType(),
6132 Sema::AA_Converting, /*AllowExplicit=*/true)
6135 ExprResult LastIteration64 = widenIterationCount(
6138 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
6139 Sema::AA_Converting,
6140 /*AllowExplicit=*/true)
6144 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
6145 return NestedLoopCount;
6147 ASTContext &C = SemaRef.Context;
6148 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
6150 Scope *CurScope = DSA.getCurScope();
6151 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
6152 if (PreCond.isUsable()) {
6154 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
6155 PreCond.get(), IterSpaces[Cnt].PreCond);
6157 Expr *N = IterSpaces[Cnt].NumIterations;
6158 SourceLocation Loc = N->getExprLoc();
6159 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
6160 if (LastIteration32.isUsable())
6161 LastIteration32 = SemaRef.BuildBinOp(
6162 CurScope, Loc, BO_Mul, LastIteration32.get(),
6164 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
6165 Sema::AA_Converting,
6166 /*AllowExplicit=*/true)
6168 if (LastIteration64.isUsable())
6169 LastIteration64 = SemaRef.BuildBinOp(
6170 CurScope, Loc, BO_Mul, LastIteration64.get(),
6172 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
6173 Sema::AA_Converting,
6174 /*AllowExplicit=*/true)
6178 // Choose either the 32-bit or 64-bit version.
6179 ExprResult LastIteration = LastIteration64;
6180 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
6181 (LastIteration32.isUsable() &&
6182 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
6183 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
6186 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
6187 LastIteration64.get(), SemaRef))))
6188 LastIteration = LastIteration32;
6189 QualType VType = LastIteration.get()->getType();
6190 QualType RealVType = VType;
6191 QualType StrideVType = VType;
6192 if (isOpenMPTaskLoopDirective(DKind)) {
6194 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
6196 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
6199 if (!LastIteration.isUsable())
6202 // Save the number of iterations.
6203 ExprResult NumIterations = LastIteration;
6205 LastIteration = SemaRef.BuildBinOp(
6206 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
6207 LastIteration.get(),
6208 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6209 if (!LastIteration.isUsable())
6213 // Calculate the last iteration number beforehand instead of doing this on
6214 // each iteration. Do not do this if the number of iterations may be kfold-ed.
6215 llvm::APSInt Result;
6217 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
6218 ExprResult CalcLastIteration;
6220 ExprResult SaveRef =
6221 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
6222 LastIteration = SaveRef;
6224 // Prepare SaveRef + 1.
6225 NumIterations = SemaRef.BuildBinOp(
6226 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
6227 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6228 if (!NumIterations.isUsable())
6232 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
6234 // Build variables passed into runtime, necessary for worksharing directives.
6235 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
6236 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
6237 isOpenMPDistributeDirective(DKind)) {
6238 // Lower bound variable, initialized with zero.
6239 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
6240 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
6241 SemaRef.AddInitializerToDecl(LBDecl,
6242 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
6243 /*DirectInit*/ false);
6245 // Upper bound variable, initialized with last iteration number.
6246 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
6247 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
6248 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
6249 /*DirectInit*/ false);
6251 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
6252 // This will be used to implement clause 'lastprivate'.
6253 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
6254 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
6255 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
6256 SemaRef.AddInitializerToDecl(ILDecl,
6257 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
6258 /*DirectInit*/ false);
6260 // Stride variable returned by runtime (we initialize it to 1 by default).
6262 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
6263 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
6264 SemaRef.AddInitializerToDecl(STDecl,
6265 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
6266 /*DirectInit*/ false);
6268 // Build expression: UB = min(UB, LastIteration)
6269 // It is necessary for CodeGen of directives with static scheduling.
6270 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
6271 UB.get(), LastIteration.get());
6272 ExprResult CondOp = SemaRef.ActOnConditionalOp(
6273 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
6274 LastIteration.get(), UB.get());
6275 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
6277 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
6279 // If we have a combined directive that combines 'distribute', 'for' or
6280 // 'simd' we need to be able to access the bounds of the schedule of the
6281 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
6282 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
6283 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6284 // Lower bound variable, initialized with zero.
6285 VarDecl *CombLBDecl =
6286 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
6287 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
6288 SemaRef.AddInitializerToDecl(
6289 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
6290 /*DirectInit*/ false);
6292 // Upper bound variable, initialized with last iteration number.
6293 VarDecl *CombUBDecl =
6294 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
6295 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
6296 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
6297 /*DirectInit*/ false);
6299 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
6300 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
6301 ExprResult CombCondOp =
6302 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
6303 LastIteration.get(), CombUB.get());
6304 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
6307 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
6309 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
6310 // We expect to have at least 2 more parameters than the 'parallel'
6311 // directive does - the lower and upper bounds of the previous schedule.
6312 assert(CD->getNumParams() >= 4 &&
6313 "Unexpected number of parameters in loop combined directive");
6315 // Set the proper type for the bounds given what we learned from the
6317 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
6318 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
6320 // Previous lower and upper bounds are obtained from the region
6323 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
6325 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
6329 // Build the iteration variable and its initialization before loop.
6331 ExprResult Init, CombInit;
6333 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
6334 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
6336 (isOpenMPWorksharingDirective(DKind) ||
6337 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
6339 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
6340 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
6341 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
6343 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6345 (isOpenMPWorksharingDirective(DKind) ||
6346 isOpenMPTaskLoopDirective(DKind) ||
6347 isOpenMPDistributeDirective(DKind))
6349 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
6351 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
6353 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
6357 bool UseStrictCompare =
6358 RealVType->hasUnsignedIntegerRepresentation() &&
6359 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
6360 return LIS.IsStrictCompare;
6362 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
6363 // unsigned IV)) for worksharing loops.
6364 SourceLocation CondLoc = AStmt->getBeginLoc();
6365 Expr *BoundUB = UB.get();
6366 if (UseStrictCompare) {
6369 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
6370 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
6373 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
6376 (isOpenMPWorksharingDirective(DKind) ||
6377 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
6378 ? SemaRef.BuildBinOp(CurScope, CondLoc,
6379 UseStrictCompare ? BO_LT : BO_LE, IV.get(),
6381 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
6382 NumIterations.get());
6383 ExprResult CombDistCond;
6384 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6385 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
6386 NumIterations.get());
6389 ExprResult CombCond;
6390 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6391 Expr *BoundCombUB = CombUB.get();
6392 if (UseStrictCompare) {
6396 CurScope, CondLoc, BO_Add, BoundCombUB,
6397 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
6400 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
6404 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
6405 IV.get(), BoundCombUB);
6407 // Loop increment (IV = IV + 1)
6408 SourceLocation IncLoc = AStmt->getBeginLoc();
6410 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
6411 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
6412 if (!Inc.isUsable())
6414 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
6415 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
6416 if (!Inc.isUsable())
6419 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
6420 // Used for directives with static scheduling.
6421 // In combined construct, add combined version that use CombLB and CombUB
6422 // base variables for the update
6423 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
6424 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
6425 isOpenMPDistributeDirective(DKind)) {
6427 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
6428 if (!NextLB.isUsable())
6432 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
6434 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
6435 if (!NextLB.isUsable())
6438 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
6439 if (!NextUB.isUsable())
6443 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
6445 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
6446 if (!NextUB.isUsable())
6448 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6450 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
6451 if (!NextLB.isUsable())
6454 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
6456 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
6457 /*DiscardedValue*/ false);
6458 if (!CombNextLB.isUsable())
6462 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
6463 if (!CombNextUB.isUsable())
6466 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
6468 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
6469 /*DiscardedValue*/ false);
6470 if (!CombNextUB.isUsable())
6475 // Create increment expression for distribute loop when combined in a same
6476 // directive with for as IV = IV + ST; ensure upper bound expression based
6477 // on PrevUB instead of NumIterations - used to implement 'for' when found
6478 // in combination with 'distribute', like in 'distribute parallel for'
6479 SourceLocation DistIncLoc = AStmt->getBeginLoc();
6480 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
6481 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6482 DistCond = SemaRef.BuildBinOp(
6483 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
6484 assert(DistCond.isUsable() && "distribute cond expr was not built");
6487 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
6488 assert(DistInc.isUsable() && "distribute inc expr was not built");
6489 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
6492 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
6493 assert(DistInc.isUsable() && "distribute inc expr was not built");
6495 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
6497 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
6498 ExprResult IsUBGreater =
6499 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
6500 ExprResult CondOp = SemaRef.ActOnConditionalOp(
6501 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
6502 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
6505 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
6507 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
6508 // parallel for is in combination with a distribute directive with
6509 // schedule(static, 1)
6510 Expr *BoundPrevUB = PrevUB.get();
6511 if (UseStrictCompare) {
6515 CurScope, CondLoc, BO_Add, BoundPrevUB,
6516 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
6519 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
6523 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
6524 IV.get(), BoundPrevUB);
6527 // Build updates and final values of the loop counters.
6528 bool HasErrors = false;
6529 Built.Counters.resize(NestedLoopCount);
6530 Built.Inits.resize(NestedLoopCount);
6531 Built.Updates.resize(NestedLoopCount);
6532 Built.Finals.resize(NestedLoopCount);
6534 // We implement the following algorithm for obtaining the
6535 // original loop iteration variable values based on the
6536 // value of the collapsed loop iteration variable IV.
6538 // Let n+1 be the number of collapsed loops in the nest.
6539 // Iteration variables (I0, I1, .... In)
6540 // Iteration counts (N0, N1, ... Nn)
6544 // To compute Ik for loop k, 0 <= k <= n, generate:
6545 // Prod = N(k+1) * N(k+2) * ... * Nn;
6547 // Acc -= Ik * Prod;
6549 ExprResult Acc = IV;
6550 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
6551 LoopIterationSpace &IS = IterSpaces[Cnt];
6552 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
6557 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6558 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
6559 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
6560 IterSpaces[K].NumIterations);
6562 // Iter = Acc / Prod
6563 // If there is at least one more inner loop to avoid
6564 // multiplication by 1.
6565 if (Cnt + 1 < NestedLoopCount)
6566 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
6567 Acc.get(), Prod.get());
6570 if (!Iter.isUsable()) {
6576 // Acc -= Iter * Prod
6577 // Check if there is at least one more inner loop to avoid
6578 // multiplication by 1.
6579 if (Cnt + 1 < NestedLoopCount)
6580 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
6581 Iter.get(), Prod.get());
6584 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
6585 Acc.get(), Prod.get());
6587 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
6588 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
6589 DeclRefExpr *CounterVar = buildDeclRefExpr(
6590 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
6591 /*RefersToCapture=*/true);
6592 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
6593 IS.CounterInit, Captures);
6594 if (!Init.isUsable()) {
6598 ExprResult Update = buildCounterUpdate(
6599 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
6600 IS.CounterStep, IS.Subtract, &Captures);
6601 if (!Update.isUsable()) {
6606 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
6607 ExprResult Final = buildCounterUpdate(
6608 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
6609 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
6610 if (!Final.isUsable()) {
6615 if (!Update.isUsable() || !Final.isUsable()) {
6620 Built.Counters[Cnt] = IS.CounterVar;
6621 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
6622 Built.Inits[Cnt] = Init.get();
6623 Built.Updates[Cnt] = Update.get();
6624 Built.Finals[Cnt] = Final.get();
6632 Built.IterationVarRef = IV.get();
6633 Built.LastIteration = LastIteration.get();
6634 Built.NumIterations = NumIterations.get();
6635 Built.CalcLastIteration = SemaRef
6636 .ActOnFinishFullExpr(CalcLastIteration.get(),
6637 /*DiscardedValue*/ false)
6639 Built.PreCond = PreCond.get();
6640 Built.PreInits = buildPreInits(C, Captures);
6641 Built.Cond = Cond.get();
6642 Built.Init = Init.get();
6643 Built.Inc = Inc.get();
6644 Built.LB = LB.get();
6645 Built.UB = UB.get();
6646 Built.IL = IL.get();
6647 Built.ST = ST.get();
6648 Built.EUB = EUB.get();
6649 Built.NLB = NextLB.get();
6650 Built.NUB = NextUB.get();
6651 Built.PrevLB = PrevLB.get();
6652 Built.PrevUB = PrevUB.get();
6653 Built.DistInc = DistInc.get();
6654 Built.PrevEUB = PrevEUB.get();
6655 Built.DistCombinedFields.LB = CombLB.get();
6656 Built.DistCombinedFields.UB = CombUB.get();
6657 Built.DistCombinedFields.EUB = CombEUB.get();
6658 Built.DistCombinedFields.Init = CombInit.get();
6659 Built.DistCombinedFields.Cond = CombCond.get();
6660 Built.DistCombinedFields.NLB = CombNextLB.get();
6661 Built.DistCombinedFields.NUB = CombNextUB.get();
6662 Built.DistCombinedFields.DistCond = CombDistCond.get();
6663 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
6665 return NestedLoopCount;
6668 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
6669 auto CollapseClauses =
6670 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
6671 if (CollapseClauses.begin() != CollapseClauses.end())
6672 return (*CollapseClauses.begin())->getNumForLoops();
6676 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
6677 auto OrderedClauses =
6678 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
6679 if (OrderedClauses.begin() != OrderedClauses.end())
6680 return (*OrderedClauses.begin())->getNumForLoops();
6684 static bool checkSimdlenSafelenSpecified(Sema &S,
6685 const ArrayRef<OMPClause *> Clauses) {
6686 const OMPSafelenClause *Safelen = nullptr;
6687 const OMPSimdlenClause *Simdlen = nullptr;
6689 for (const OMPClause *Clause : Clauses) {
6690 if (Clause->getClauseKind() == OMPC_safelen)
6691 Safelen = cast<OMPSafelenClause>(Clause);
6692 else if (Clause->getClauseKind() == OMPC_simdlen)
6693 Simdlen = cast<OMPSimdlenClause>(Clause);
6694 if (Safelen && Simdlen)
6698 if (Simdlen && Safelen) {
6699 const Expr *SimdlenLength = Simdlen->getSimdlen();
6700 const Expr *SafelenLength = Safelen->getSafelen();
6701 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
6702 SimdlenLength->isInstantiationDependent() ||
6703 SimdlenLength->containsUnexpandedParameterPack())
6705 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
6706 SafelenLength->isInstantiationDependent() ||
6707 SafelenLength->containsUnexpandedParameterPack())
6709 Expr::EvalResult SimdlenResult, SafelenResult;
6710 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
6711 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
6712 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
6713 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
6714 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
6715 // If both simdlen and safelen clauses are specified, the value of the
6716 // simdlen parameter must be less than or equal to the value of the safelen
6718 if (SimdlenRes > SafelenRes) {
6719 S.Diag(SimdlenLength->getExprLoc(),
6720 diag::err_omp_wrong_simdlen_safelen_values)
6721 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
6729 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
6730 SourceLocation StartLoc, SourceLocation EndLoc,
6731 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
6735 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6736 OMPLoopDirective::HelperExprs B;
6737 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6738 // define the nested loops number.
6739 unsigned NestedLoopCount = checkOpenMPLoop(
6740 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
6741 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
6742 if (NestedLoopCount == 0)
6745 assert((CurContext->isDependentContext() || B.builtAll()) &&
6746 "omp simd loop exprs were not built");
6748 if (!CurContext->isDependentContext()) {
6749 // Finalize the clauses that need pre-built expressions for CodeGen.
6750 for (OMPClause *C : Clauses) {
6751 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6752 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6753 B.NumIterations, *this, CurScope,
6759 if (checkSimdlenSafelenSpecified(*this, Clauses))
6762 setFunctionHasBranchProtectedScope();
6763 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
6768 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
6769 SourceLocation StartLoc, SourceLocation EndLoc,
6770 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
6774 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6775 OMPLoopDirective::HelperExprs B;
6776 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6777 // define the nested loops number.
6778 unsigned NestedLoopCount = checkOpenMPLoop(
6779 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
6780 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
6781 if (NestedLoopCount == 0)
6784 assert((CurContext->isDependentContext() || B.builtAll()) &&
6785 "omp for loop exprs were not built");
6787 if (!CurContext->isDependentContext()) {
6788 // Finalize the clauses that need pre-built expressions for CodeGen.
6789 for (OMPClause *C : Clauses) {
6790 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6791 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6792 B.NumIterations, *this, CurScope,
6798 setFunctionHasBranchProtectedScope();
6799 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
6800 Clauses, AStmt, B, DSAStack->isCancelRegion());
6803 StmtResult Sema::ActOnOpenMPForSimdDirective(
6804 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6805 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
6809 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6810 OMPLoopDirective::HelperExprs B;
6811 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6812 // define the nested loops number.
6813 unsigned NestedLoopCount =
6814 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
6815 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6816 VarsWithImplicitDSA, B);
6817 if (NestedLoopCount == 0)
6820 assert((CurContext->isDependentContext() || B.builtAll()) &&
6821 "omp for simd loop exprs were not built");
6823 if (!CurContext->isDependentContext()) {
6824 // Finalize the clauses that need pre-built expressions for CodeGen.
6825 for (OMPClause *C : Clauses) {
6826 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6827 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6828 B.NumIterations, *this, CurScope,
6834 if (checkSimdlenSafelenSpecified(*this, Clauses))
6837 setFunctionHasBranchProtectedScope();
6838 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
6842 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
6844 SourceLocation StartLoc,
6845 SourceLocation EndLoc) {
6849 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6850 auto BaseStmt = AStmt;
6851 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
6852 BaseStmt = CS->getCapturedStmt();
6853 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
6854 auto S = C->children();
6855 if (S.begin() == S.end())
6857 // All associated statements must be '#pragma omp section' except for
6859 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
6860 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6862 Diag(SectionStmt->getBeginLoc(),
6863 diag::err_omp_sections_substmt_not_section);
6866 cast<OMPSectionDirective>(SectionStmt)
6867 ->setHasCancel(DSAStack->isCancelRegion());
6870 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
6874 setFunctionHasBranchProtectedScope();
6876 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6877 DSAStack->isCancelRegion());
6880 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
6881 SourceLocation StartLoc,
6882 SourceLocation EndLoc) {
6886 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6888 setFunctionHasBranchProtectedScope();
6889 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
6891 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
6892 DSAStack->isCancelRegion());
6895 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
6897 SourceLocation StartLoc,
6898 SourceLocation EndLoc) {
6902 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6904 setFunctionHasBranchProtectedScope();
6906 // OpenMP [2.7.3, single Construct, Restrictions]
6907 // The copyprivate clause must not be used with the nowait clause.
6908 const OMPClause *Nowait = nullptr;
6909 const OMPClause *Copyprivate = nullptr;
6910 for (const OMPClause *Clause : Clauses) {
6911 if (Clause->getClauseKind() == OMPC_nowait)
6913 else if (Clause->getClauseKind() == OMPC_copyprivate)
6914 Copyprivate = Clause;
6915 if (Copyprivate && Nowait) {
6916 Diag(Copyprivate->getBeginLoc(),
6917 diag::err_omp_single_copyprivate_with_nowait);
6918 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
6923 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6926 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
6927 SourceLocation StartLoc,
6928 SourceLocation EndLoc) {
6932 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6934 setFunctionHasBranchProtectedScope();
6936 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
6939 StmtResult Sema::ActOnOpenMPCriticalDirective(
6940 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
6941 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
6945 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6947 bool ErrorFound = false;
6949 SourceLocation HintLoc;
6950 bool DependentHint = false;
6951 for (const OMPClause *C : Clauses) {
6952 if (C->getClauseKind() == OMPC_hint) {
6953 if (!DirName.getName()) {
6954 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
6957 Expr *E = cast<OMPHintClause>(C)->getHint();
6958 if (E->isTypeDependent() || E->isValueDependent() ||
6959 E->isInstantiationDependent()) {
6960 DependentHint = true;
6962 Hint = E->EvaluateKnownConstInt(Context);
6963 HintLoc = C->getBeginLoc();
6969 const auto Pair = DSAStack->getCriticalWithHint(DirName);
6970 if (Pair.first && DirName.getName() && !DependentHint) {
6971 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
6972 Diag(StartLoc, diag::err_omp_critical_with_hint);
6973 if (HintLoc.isValid())
6974 Diag(HintLoc, diag::note_omp_critical_hint_here)
6975 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
6977 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
6978 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
6979 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
6981 << C->getHint()->EvaluateKnownConstInt(Context).toString(
6982 /*Radix=*/10, /*Signed=*/false);
6984 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
6989 setFunctionHasBranchProtectedScope();
6991 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
6993 if (!Pair.first && DirName.getName() && !DependentHint)
6994 DSAStack->addCriticalWithHint(Dir, Hint);
6998 StmtResult Sema::ActOnOpenMPParallelForDirective(
6999 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7000 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7004 auto *CS = cast<CapturedStmt>(AStmt);
7005 // 1.2.2 OpenMP Language Terminology
7006 // Structured block - An executable statement with a single entry at the
7007 // top and a single exit at the bottom.
7008 // The point of exit cannot be a branch out of the structured block.
7009 // longjmp() and throw() must not violate the entry/exit criteria.
7010 CS->getCapturedDecl()->setNothrow();
7012 OMPLoopDirective::HelperExprs B;
7013 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7014 // define the nested loops number.
7015 unsigned NestedLoopCount =
7016 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
7017 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7018 VarsWithImplicitDSA, B);
7019 if (NestedLoopCount == 0)
7022 assert((CurContext->isDependentContext() || B.builtAll()) &&
7023 "omp parallel for loop exprs were not built");
7025 if (!CurContext->isDependentContext()) {
7026 // Finalize the clauses that need pre-built expressions for CodeGen.
7027 for (OMPClause *C : Clauses) {
7028 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7029 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7030 B.NumIterations, *this, CurScope,
7036 setFunctionHasBranchProtectedScope();
7037 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
7038 NestedLoopCount, Clauses, AStmt, B,
7039 DSAStack->isCancelRegion());
7042 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
7043 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7044 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7048 auto *CS = cast<CapturedStmt>(AStmt);
7049 // 1.2.2 OpenMP Language Terminology
7050 // Structured block - An executable statement with a single entry at the
7051 // top and a single exit at the bottom.
7052 // The point of exit cannot be a branch out of the structured block.
7053 // longjmp() and throw() must not violate the entry/exit criteria.
7054 CS->getCapturedDecl()->setNothrow();
7056 OMPLoopDirective::HelperExprs B;
7057 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7058 // define the nested loops number.
7059 unsigned NestedLoopCount =
7060 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
7061 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7062 VarsWithImplicitDSA, B);
7063 if (NestedLoopCount == 0)
7066 if (!CurContext->isDependentContext()) {
7067 // Finalize the clauses that need pre-built expressions for CodeGen.
7068 for (OMPClause *C : Clauses) {
7069 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7070 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7071 B.NumIterations, *this, CurScope,
7077 if (checkSimdlenSafelenSpecified(*this, Clauses))
7080 setFunctionHasBranchProtectedScope();
7081 return OMPParallelForSimdDirective::Create(
7082 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7086 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
7087 Stmt *AStmt, SourceLocation StartLoc,
7088 SourceLocation EndLoc) {
7092 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7093 auto BaseStmt = AStmt;
7094 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
7095 BaseStmt = CS->getCapturedStmt();
7096 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
7097 auto S = C->children();
7098 if (S.begin() == S.end())
7100 // All associated statements must be '#pragma omp section' except for
7102 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
7103 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
7105 Diag(SectionStmt->getBeginLoc(),
7106 diag::err_omp_parallel_sections_substmt_not_section);
7109 cast<OMPSectionDirective>(SectionStmt)
7110 ->setHasCancel(DSAStack->isCancelRegion());
7113 Diag(AStmt->getBeginLoc(),
7114 diag::err_omp_parallel_sections_not_compound_stmt);
7118 setFunctionHasBranchProtectedScope();
7120 return OMPParallelSectionsDirective::Create(
7121 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
7124 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
7125 Stmt *AStmt, SourceLocation StartLoc,
7126 SourceLocation EndLoc) {
7130 auto *CS = cast<CapturedStmt>(AStmt);
7131 // 1.2.2 OpenMP Language Terminology
7132 // Structured block - An executable statement with a single entry at the
7133 // top and a single exit at the bottom.
7134 // The point of exit cannot be a branch out of the structured block.
7135 // longjmp() and throw() must not violate the entry/exit criteria.
7136 CS->getCapturedDecl()->setNothrow();
7138 setFunctionHasBranchProtectedScope();
7140 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7141 DSAStack->isCancelRegion());
7144 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
7145 SourceLocation EndLoc) {
7146 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
7149 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
7150 SourceLocation EndLoc) {
7151 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
7154 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
7155 SourceLocation EndLoc) {
7156 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
7159 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
7161 SourceLocation StartLoc,
7162 SourceLocation EndLoc) {
7166 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7168 setFunctionHasBranchProtectedScope();
7170 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
7172 DSAStack->getTaskgroupReductionRef());
7175 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
7176 SourceLocation StartLoc,
7177 SourceLocation EndLoc) {
7178 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
7179 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
7182 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
7184 SourceLocation StartLoc,
7185 SourceLocation EndLoc) {
7186 const OMPClause *DependFound = nullptr;
7187 const OMPClause *DependSourceClause = nullptr;
7188 const OMPClause *DependSinkClause = nullptr;
7189 bool ErrorFound = false;
7190 const OMPThreadsClause *TC = nullptr;
7191 const OMPSIMDClause *SC = nullptr;
7192 for (const OMPClause *C : Clauses) {
7193 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
7195 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
7196 if (DependSourceClause) {
7197 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
7198 << getOpenMPDirectiveName(OMPD_ordered)
7199 << getOpenMPClauseName(OMPC_depend) << 2;
7202 DependSourceClause = C;
7204 if (DependSinkClause) {
7205 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
7209 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
7210 if (DependSourceClause) {
7211 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
7215 DependSinkClause = C;
7217 } else if (C->getClauseKind() == OMPC_threads) {
7218 TC = cast<OMPThreadsClause>(C);
7219 } else if (C->getClauseKind() == OMPC_simd) {
7220 SC = cast<OMPSIMDClause>(C);
7223 if (!ErrorFound && !SC &&
7224 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
7225 // OpenMP [2.8.1,simd Construct, Restrictions]
7226 // An ordered construct with the simd clause is the only OpenMP construct
7227 // that can appear in the simd region.
7228 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
7230 } else if (DependFound && (TC || SC)) {
7231 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
7232 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
7234 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
7235 Diag(DependFound->getBeginLoc(),
7236 diag::err_omp_ordered_directive_without_param);
7238 } else if (TC || Clauses.empty()) {
7239 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
7240 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
7241 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
7243 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
7247 if ((!AStmt && !DependFound) || ErrorFound)
7251 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7253 setFunctionHasBranchProtectedScope();
7256 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7260 /// Helper class for checking expression in 'omp atomic [update]'
7262 class OpenMPAtomicUpdateChecker {
7263 /// Error results for atomic update expressions.
7264 enum ExprAnalysisErrorCode {
7265 /// A statement is not an expression statement.
7267 /// Expression is not builtin binary or unary operation.
7268 NotABinaryOrUnaryExpression,
7269 /// Unary operation is not post-/pre- increment/decrement operation.
7270 NotAnUnaryIncDecExpression,
7271 /// An expression is not of scalar type.
7273 /// A binary operation is not an assignment operation.
7275 /// RHS part of the binary operation is not a binary expression.
7276 NotABinaryExpression,
7277 /// RHS part is not additive/multiplicative/shift/biwise binary
7280 /// RHS binary operation does not have reference to the updated LHS
7282 NotAnUpdateExpression,
7283 /// No errors is found.
7286 /// Reference to Sema.
7288 /// A location for note diagnostics (when error is found).
7289 SourceLocation NoteLoc;
7290 /// 'x' lvalue part of the source atomic expression.
7292 /// 'expr' rvalue part of the source atomic expression.
7294 /// Helper expression of the form
7295 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
7296 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
7298 /// Is 'x' a LHS in a RHS part of full update expression. It is
7299 /// important for non-associative operations.
7300 bool IsXLHSInRHSPart;
7301 BinaryOperatorKind Op;
7302 SourceLocation OpLoc;
7303 /// true if the source expression is a postfix unary operation, false
7304 /// if it is a prefix unary operation.
7305 bool IsPostfixUpdate;
7308 OpenMPAtomicUpdateChecker(Sema &SemaRef)
7309 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
7310 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
7311 /// Check specified statement that it is suitable for 'atomic update'
7312 /// constructs and extract 'x', 'expr' and Operation from the original
7313 /// expression. If DiagId and NoteId == 0, then only check is performed
7314 /// without error notification.
7315 /// \param DiagId Diagnostic which should be emitted if error is found.
7316 /// \param NoteId Diagnostic note for the main error message.
7317 /// \return true if statement is not an update expression, false otherwise.
7318 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
7319 /// Return the 'x' lvalue part of the source atomic expression.
7320 Expr *getX() const { return X; }
7321 /// Return the 'expr' rvalue part of the source atomic expression.
7322 Expr *getExpr() const { return E; }
7323 /// Return the update expression used in calculation of the updated
7324 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
7325 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
7326 Expr *getUpdateExpr() const { return UpdateExpr; }
7327 /// Return true if 'x' is LHS in RHS part of full update expression,
7328 /// false otherwise.
7329 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
7331 /// true if the source expression is a postfix unary operation, false
7332 /// if it is a prefix unary operation.
7333 bool isPostfixUpdate() const { return IsPostfixUpdate; }
7336 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
7337 unsigned NoteId = 0);
7341 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
7342 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
7343 ExprAnalysisErrorCode ErrorFound = NoError;
7344 SourceLocation ErrorLoc, NoteLoc;
7345 SourceRange ErrorRange, NoteRange;
7346 // Allowed constructs are:
7347 // x = x binop expr;
7348 // x = expr binop x;
7349 if (AtomicBinOp->getOpcode() == BO_Assign) {
7350 X = AtomicBinOp->getLHS();
7351 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
7352 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
7353 if (AtomicInnerBinOp->isMultiplicativeOp() ||
7354 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
7355 AtomicInnerBinOp->isBitwiseOp()) {
7356 Op = AtomicInnerBinOp->getOpcode();
7357 OpLoc = AtomicInnerBinOp->getOperatorLoc();
7358 Expr *LHS = AtomicInnerBinOp->getLHS();
7359 Expr *RHS = AtomicInnerBinOp->getRHS();
7360 llvm::FoldingSetNodeID XId, LHSId, RHSId;
7361 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
7362 /*Canonical=*/true);
7363 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
7364 /*Canonical=*/true);
7365 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
7366 /*Canonical=*/true);
7369 IsXLHSInRHSPart = true;
7370 } else if (XId == RHSId) {
7372 IsXLHSInRHSPart = false;
7374 ErrorLoc = AtomicInnerBinOp->getExprLoc();
7375 ErrorRange = AtomicInnerBinOp->getSourceRange();
7376 NoteLoc = X->getExprLoc();
7377 NoteRange = X->getSourceRange();
7378 ErrorFound = NotAnUpdateExpression;
7381 ErrorLoc = AtomicInnerBinOp->getExprLoc();
7382 ErrorRange = AtomicInnerBinOp->getSourceRange();
7383 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
7384 NoteRange = SourceRange(NoteLoc, NoteLoc);
7385 ErrorFound = NotABinaryOperator;
7388 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
7389 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
7390 ErrorFound = NotABinaryExpression;
7393 ErrorLoc = AtomicBinOp->getExprLoc();
7394 ErrorRange = AtomicBinOp->getSourceRange();
7395 NoteLoc = AtomicBinOp->getOperatorLoc();
7396 NoteRange = SourceRange(NoteLoc, NoteLoc);
7397 ErrorFound = NotAnAssignmentOp;
7399 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
7400 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
7401 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
7404 if (SemaRef.CurContext->isDependentContext())
7405 E = X = UpdateExpr = nullptr;
7406 return ErrorFound != NoError;
7409 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
7411 ExprAnalysisErrorCode ErrorFound = NoError;
7412 SourceLocation ErrorLoc, NoteLoc;
7413 SourceRange ErrorRange, NoteRange;
7414 // Allowed constructs are:
7420 // x = x binop expr;
7421 // x = expr binop x;
7422 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
7423 AtomicBody = AtomicBody->IgnoreParenImpCasts();
7424 if (AtomicBody->getType()->isScalarType() ||
7425 AtomicBody->isInstantiationDependent()) {
7426 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
7427 AtomicBody->IgnoreParenImpCasts())) {
7428 // Check for Compound Assignment Operation
7429 Op = BinaryOperator::getOpForCompoundAssignment(
7430 AtomicCompAssignOp->getOpcode());
7431 OpLoc = AtomicCompAssignOp->getOperatorLoc();
7432 E = AtomicCompAssignOp->getRHS();
7433 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
7434 IsXLHSInRHSPart = true;
7435 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
7436 AtomicBody->IgnoreParenImpCasts())) {
7437 // Check for Binary Operation
7438 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
7440 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
7441 AtomicBody->IgnoreParenImpCasts())) {
7442 // Check for Unary Operation
7443 if (AtomicUnaryOp->isIncrementDecrementOp()) {
7444 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
7445 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
7446 OpLoc = AtomicUnaryOp->getOperatorLoc();
7447 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
7448 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
7449 IsXLHSInRHSPart = true;
7451 ErrorFound = NotAnUnaryIncDecExpression;
7452 ErrorLoc = AtomicUnaryOp->getExprLoc();
7453 ErrorRange = AtomicUnaryOp->getSourceRange();
7454 NoteLoc = AtomicUnaryOp->getOperatorLoc();
7455 NoteRange = SourceRange(NoteLoc, NoteLoc);
7457 } else if (!AtomicBody->isInstantiationDependent()) {
7458 ErrorFound = NotABinaryOrUnaryExpression;
7459 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
7460 NoteRange = ErrorRange = AtomicBody->getSourceRange();
7463 ErrorFound = NotAScalarType;
7464 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
7465 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
7468 ErrorFound = NotAnExpression;
7469 NoteLoc = ErrorLoc = S->getBeginLoc();
7470 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
7472 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
7473 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
7474 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
7477 if (SemaRef.CurContext->isDependentContext())
7478 E = X = UpdateExpr = nullptr;
7479 if (ErrorFound == NoError && E && X) {
7480 // Build an update expression of form 'OpaqueValueExpr(x) binop
7481 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
7482 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
7483 auto *OVEX = new (SemaRef.getASTContext())
7484 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
7485 auto *OVEExpr = new (SemaRef.getASTContext())
7486 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
7488 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
7489 IsXLHSInRHSPart ? OVEExpr : OVEX);
7490 if (Update.isInvalid())
7492 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
7494 if (Update.isInvalid())
7496 UpdateExpr = Update.get();
7498 return ErrorFound != NoError;
7501 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
7503 SourceLocation StartLoc,
7504 SourceLocation EndLoc) {
7508 auto *CS = cast<CapturedStmt>(AStmt);
7509 // 1.2.2 OpenMP Language Terminology
7510 // Structured block - An executable statement with a single entry at the
7511 // top and a single exit at the bottom.
7512 // The point of exit cannot be a branch out of the structured block.
7513 // longjmp() and throw() must not violate the entry/exit criteria.
7514 OpenMPClauseKind AtomicKind = OMPC_unknown;
7515 SourceLocation AtomicKindLoc;
7516 for (const OMPClause *C : Clauses) {
7517 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
7518 C->getClauseKind() == OMPC_update ||
7519 C->getClauseKind() == OMPC_capture) {
7520 if (AtomicKind != OMPC_unknown) {
7521 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
7522 << SourceRange(C->getBeginLoc(), C->getEndLoc());
7523 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
7524 << getOpenMPClauseName(AtomicKind);
7526 AtomicKind = C->getClauseKind();
7527 AtomicKindLoc = C->getBeginLoc();
7532 Stmt *Body = CS->getCapturedStmt();
7533 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
7534 Body = EWC->getSubExpr();
7540 bool IsXLHSInRHSPart = false;
7541 bool IsPostfixUpdate = false;
7542 // OpenMP [2.12.6, atomic Construct]
7543 // In the next expressions:
7544 // * x and v (as applicable) are both l-value expressions with scalar type.
7545 // * During the execution of an atomic region, multiple syntactic
7546 // occurrences of x must designate the same storage location.
7547 // * Neither of v and expr (as applicable) may access the storage location
7549 // * Neither of x and expr (as applicable) may access the storage location
7551 // * expr is an expression with scalar type.
7552 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
7553 // * binop, binop=, ++, and -- are not overloaded operators.
7554 // * The expression x binop expr must be numerically equivalent to x binop
7555 // (expr). This requirement is satisfied if the operators in expr have
7556 // precedence greater than binop, or by using parentheses around expr or
7557 // subexpressions of expr.
7558 // * The expression expr binop x must be numerically equivalent to (expr)
7559 // binop x. This requirement is satisfied if the operators in expr have
7560 // precedence equal to or greater than binop, or by using parentheses around
7561 // expr or subexpressions of expr.
7562 // * For forms that allow multiple occurrences of x, the number of times
7563 // that x is evaluated is unspecified.
7564 if (AtomicKind == OMPC_read) {
7571 } ErrorFound = NoError;
7572 SourceLocation ErrorLoc, NoteLoc;
7573 SourceRange ErrorRange, NoteRange;
7574 // If clause is read:
7576 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
7577 const auto *AtomicBinOp =
7578 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
7579 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
7580 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
7581 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
7582 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
7583 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
7584 if (!X->isLValue() || !V->isLValue()) {
7585 const Expr *NotLValueExpr = X->isLValue() ? V : X;
7586 ErrorFound = NotAnLValue;
7587 ErrorLoc = AtomicBinOp->getExprLoc();
7588 ErrorRange = AtomicBinOp->getSourceRange();
7589 NoteLoc = NotLValueExpr->getExprLoc();
7590 NoteRange = NotLValueExpr->getSourceRange();
7592 } else if (!X->isInstantiationDependent() ||
7593 !V->isInstantiationDependent()) {
7594 const Expr *NotScalarExpr =
7595 (X->isInstantiationDependent() || X->getType()->isScalarType())
7598 ErrorFound = NotAScalarType;
7599 ErrorLoc = AtomicBinOp->getExprLoc();
7600 ErrorRange = AtomicBinOp->getSourceRange();
7601 NoteLoc = NotScalarExpr->getExprLoc();
7602 NoteRange = NotScalarExpr->getSourceRange();
7604 } else if (!AtomicBody->isInstantiationDependent()) {
7605 ErrorFound = NotAnAssignmentOp;
7606 ErrorLoc = AtomicBody->getExprLoc();
7607 ErrorRange = AtomicBody->getSourceRange();
7608 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
7609 : AtomicBody->getExprLoc();
7610 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
7611 : AtomicBody->getSourceRange();
7614 ErrorFound = NotAnExpression;
7615 NoteLoc = ErrorLoc = Body->getBeginLoc();
7616 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
7618 if (ErrorFound != NoError) {
7619 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
7621 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
7625 if (CurContext->isDependentContext())
7627 } else if (AtomicKind == OMPC_write) {
7634 } ErrorFound = NoError;
7635 SourceLocation ErrorLoc, NoteLoc;
7636 SourceRange ErrorRange, NoteRange;
7637 // If clause is write:
7639 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
7640 const auto *AtomicBinOp =
7641 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
7642 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
7643 X = AtomicBinOp->getLHS();
7644 E = AtomicBinOp->getRHS();
7645 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
7646 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
7647 if (!X->isLValue()) {
7648 ErrorFound = NotAnLValue;
7649 ErrorLoc = AtomicBinOp->getExprLoc();
7650 ErrorRange = AtomicBinOp->getSourceRange();
7651 NoteLoc = X->getExprLoc();
7652 NoteRange = X->getSourceRange();
7654 } else if (!X->isInstantiationDependent() ||
7655 !E->isInstantiationDependent()) {
7656 const Expr *NotScalarExpr =
7657 (X->isInstantiationDependent() || X->getType()->isScalarType())
7660 ErrorFound = NotAScalarType;
7661 ErrorLoc = AtomicBinOp->getExprLoc();
7662 ErrorRange = AtomicBinOp->getSourceRange();
7663 NoteLoc = NotScalarExpr->getExprLoc();
7664 NoteRange = NotScalarExpr->getSourceRange();
7666 } else if (!AtomicBody->isInstantiationDependent()) {
7667 ErrorFound = NotAnAssignmentOp;
7668 ErrorLoc = AtomicBody->getExprLoc();
7669 ErrorRange = AtomicBody->getSourceRange();
7670 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
7671 : AtomicBody->getExprLoc();
7672 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
7673 : AtomicBody->getSourceRange();
7676 ErrorFound = NotAnExpression;
7677 NoteLoc = ErrorLoc = Body->getBeginLoc();
7678 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
7680 if (ErrorFound != NoError) {
7681 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
7683 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
7687 if (CurContext->isDependentContext())
7689 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
7690 // If clause is update:
7696 // x = x binop expr;
7697 // x = expr binop x;
7698 OpenMPAtomicUpdateChecker Checker(*this);
7699 if (Checker.checkStatement(
7700 Body, (AtomicKind == OMPC_update)
7701 ? diag::err_omp_atomic_update_not_expression_statement
7702 : diag::err_omp_atomic_not_expression_statement,
7703 diag::note_omp_atomic_update))
7705 if (!CurContext->isDependentContext()) {
7706 E = Checker.getExpr();
7708 UE = Checker.getUpdateExpr();
7709 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
7711 } else if (AtomicKind == OMPC_capture) {
7714 NotACompoundStatement,
7715 NotTwoSubstatements,
7716 NotASpecificExpression,
7718 } ErrorFound = NoError;
7719 SourceLocation ErrorLoc, NoteLoc;
7720 SourceRange ErrorRange, NoteRange;
7721 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
7722 // If clause is a capture:
7727 // v = x binop= expr;
7728 // v = x = x binop expr;
7729 // v = x = expr binop x;
7730 const auto *AtomicBinOp =
7731 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
7732 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
7733 V = AtomicBinOp->getLHS();
7734 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
7735 OpenMPAtomicUpdateChecker Checker(*this);
7736 if (Checker.checkStatement(
7737 Body, diag::err_omp_atomic_capture_not_expression_statement,
7738 diag::note_omp_atomic_update))
7740 E = Checker.getExpr();
7742 UE = Checker.getUpdateExpr();
7743 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
7744 IsPostfixUpdate = Checker.isPostfixUpdate();
7745 } else if (!AtomicBody->isInstantiationDependent()) {
7746 ErrorLoc = AtomicBody->getExprLoc();
7747 ErrorRange = AtomicBody->getSourceRange();
7748 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
7749 : AtomicBody->getExprLoc();
7750 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
7751 : AtomicBody->getSourceRange();
7752 ErrorFound = NotAnAssignmentOp;
7754 if (ErrorFound != NoError) {
7755 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
7757 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
7760 if (CurContext->isDependentContext())
7761 UE = V = E = X = nullptr;
7763 // If clause is a capture:
7764 // { v = x; x = expr; }
7769 // { v = x; x binop= expr; }
7770 // { v = x; x = x binop expr; }
7771 // { v = x; x = expr binop x; }
7776 // { x binop= expr; v = x; }
7777 // { x = x binop expr; v = x; }
7778 // { x = expr binop x; v = x; }
7779 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
7780 // Check that this is { expr1; expr2; }
7781 if (CS->size() == 2) {
7782 Stmt *First = CS->body_front();
7783 Stmt *Second = CS->body_back();
7784 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
7785 First = EWC->getSubExpr()->IgnoreParenImpCasts();
7786 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
7787 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
7788 // Need to find what subexpression is 'v' and what is 'x'.
7789 OpenMPAtomicUpdateChecker Checker(*this);
7790 bool IsUpdateExprFound = !Checker.checkStatement(Second);
7791 BinaryOperator *BinOp = nullptr;
7792 if (IsUpdateExprFound) {
7793 BinOp = dyn_cast<BinaryOperator>(First);
7794 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
7796 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
7801 // { v = x; x binop= expr; }
7802 // { v = x; x = x binop expr; }
7803 // { v = x; x = expr binop x; }
7804 // Check that the first expression has form v = x.
7805 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
7806 llvm::FoldingSetNodeID XId, PossibleXId;
7807 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
7808 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
7809 IsUpdateExprFound = XId == PossibleXId;
7810 if (IsUpdateExprFound) {
7811 V = BinOp->getLHS();
7813 E = Checker.getExpr();
7814 UE = Checker.getUpdateExpr();
7815 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
7816 IsPostfixUpdate = true;
7819 if (!IsUpdateExprFound) {
7820 IsUpdateExprFound = !Checker.checkStatement(First);
7822 if (IsUpdateExprFound) {
7823 BinOp = dyn_cast<BinaryOperator>(Second);
7824 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
7826 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
7831 // { x binop= expr; v = x; }
7832 // { x = x binop expr; v = x; }
7833 // { x = expr binop x; v = x; }
7834 // Check that the second expression has form v = x.
7835 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
7836 llvm::FoldingSetNodeID XId, PossibleXId;
7837 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
7838 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
7839 IsUpdateExprFound = XId == PossibleXId;
7840 if (IsUpdateExprFound) {
7841 V = BinOp->getLHS();
7843 E = Checker.getExpr();
7844 UE = Checker.getUpdateExpr();
7845 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
7846 IsPostfixUpdate = false;
7850 if (!IsUpdateExprFound) {
7851 // { v = x; x = expr; }
7852 auto *FirstExpr = dyn_cast<Expr>(First);
7853 auto *SecondExpr = dyn_cast<Expr>(Second);
7854 if (!FirstExpr || !SecondExpr ||
7855 !(FirstExpr->isInstantiationDependent() ||
7856 SecondExpr->isInstantiationDependent())) {
7857 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
7858 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
7859 ErrorFound = NotAnAssignmentOp;
7860 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
7861 : First->getBeginLoc();
7862 NoteRange = ErrorRange = FirstBinOp
7863 ? FirstBinOp->getSourceRange()
7864 : SourceRange(ErrorLoc, ErrorLoc);
7866 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
7867 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
7868 ErrorFound = NotAnAssignmentOp;
7869 NoteLoc = ErrorLoc = SecondBinOp
7870 ? SecondBinOp->getOperatorLoc()
7871 : Second->getBeginLoc();
7872 NoteRange = ErrorRange =
7873 SecondBinOp ? SecondBinOp->getSourceRange()
7874 : SourceRange(ErrorLoc, ErrorLoc);
7876 Expr *PossibleXRHSInFirst =
7877 FirstBinOp->getRHS()->IgnoreParenImpCasts();
7878 Expr *PossibleXLHSInSecond =
7879 SecondBinOp->getLHS()->IgnoreParenImpCasts();
7880 llvm::FoldingSetNodeID X1Id, X2Id;
7881 PossibleXRHSInFirst->Profile(X1Id, Context,
7882 /*Canonical=*/true);
7883 PossibleXLHSInSecond->Profile(X2Id, Context,
7884 /*Canonical=*/true);
7885 IsUpdateExprFound = X1Id == X2Id;
7886 if (IsUpdateExprFound) {
7887 V = FirstBinOp->getLHS();
7888 X = SecondBinOp->getLHS();
7889 E = SecondBinOp->getRHS();
7891 IsXLHSInRHSPart = false;
7892 IsPostfixUpdate = true;
7894 ErrorFound = NotASpecificExpression;
7895 ErrorLoc = FirstBinOp->getExprLoc();
7896 ErrorRange = FirstBinOp->getSourceRange();
7897 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
7898 NoteRange = SecondBinOp->getRHS()->getSourceRange();
7905 NoteLoc = ErrorLoc = Body->getBeginLoc();
7906 NoteRange = ErrorRange =
7907 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
7908 ErrorFound = NotTwoSubstatements;
7911 NoteLoc = ErrorLoc = Body->getBeginLoc();
7912 NoteRange = ErrorRange =
7913 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
7914 ErrorFound = NotACompoundStatement;
7916 if (ErrorFound != NoError) {
7917 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
7919 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
7922 if (CurContext->isDependentContext())
7923 UE = V = E = X = nullptr;
7927 setFunctionHasBranchProtectedScope();
7929 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7930 X, V, E, UE, IsXLHSInRHSPart,
7934 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
7936 SourceLocation StartLoc,
7937 SourceLocation EndLoc) {
7941 auto *CS = cast<CapturedStmt>(AStmt);
7942 // 1.2.2 OpenMP Language Terminology
7943 // Structured block - An executable statement with a single entry at the
7944 // top and a single exit at the bottom.
7945 // The point of exit cannot be a branch out of the structured block.
7946 // longjmp() and throw() must not violate the entry/exit criteria.
7947 CS->getCapturedDecl()->setNothrow();
7948 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
7949 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7950 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7951 // 1.2.2 OpenMP Language Terminology
7952 // Structured block - An executable statement with a single entry at the
7953 // top and a single exit at the bottom.
7954 // The point of exit cannot be a branch out of the structured block.
7955 // longjmp() and throw() must not violate the entry/exit criteria.
7956 CS->getCapturedDecl()->setNothrow();
7959 // OpenMP [2.16, Nesting of Regions]
7960 // If specified, a teams construct must be contained within a target
7961 // construct. That target construct must contain no statements or directives
7962 // outside of the teams construct.
7963 if (DSAStack->hasInnerTeamsRegion()) {
7964 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
7965 bool OMPTeamsFound = true;
7966 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7967 auto I = CS->body_begin();
7968 while (I != CS->body_end()) {
7969 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
7970 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
7973 OMPTeamsFound = false;
7978 assert(I != CS->body_end() && "Not found statement");
7981 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
7982 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
7984 if (!OMPTeamsFound) {
7985 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
7986 Diag(DSAStack->getInnerTeamsRegionLoc(),
7987 diag::note_omp_nested_teams_construct_here);
7988 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
7989 << isa<OMPExecutableDirective>(S);
7994 setFunctionHasBranchProtectedScope();
7996 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8000 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
8001 Stmt *AStmt, SourceLocation StartLoc,
8002 SourceLocation EndLoc) {
8006 auto *CS = cast<CapturedStmt>(AStmt);
8007 // 1.2.2 OpenMP Language Terminology
8008 // Structured block - An executable statement with a single entry at the
8009 // top and a single exit at the bottom.
8010 // The point of exit cannot be a branch out of the structured block.
8011 // longjmp() and throw() must not violate the entry/exit criteria.
8012 CS->getCapturedDecl()->setNothrow();
8013 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
8014 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8015 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8016 // 1.2.2 OpenMP Language Terminology
8017 // Structured block - An executable statement with a single entry at the
8018 // top and a single exit at the bottom.
8019 // The point of exit cannot be a branch out of the structured block.
8020 // longjmp() and throw() must not violate the entry/exit criteria.
8021 CS->getCapturedDecl()->setNothrow();
8024 setFunctionHasBranchProtectedScope();
8026 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
8030 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
8031 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8032 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8036 auto *CS = cast<CapturedStmt>(AStmt);
8037 // 1.2.2 OpenMP Language Terminology
8038 // Structured block - An executable statement with a single entry at the
8039 // top and a single exit at the bottom.
8040 // The point of exit cannot be a branch out of the structured block.
8041 // longjmp() and throw() must not violate the entry/exit criteria.
8042 CS->getCapturedDecl()->setNothrow();
8043 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
8044 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8045 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8046 // 1.2.2 OpenMP Language Terminology
8047 // Structured block - An executable statement with a single entry at the
8048 // top and a single exit at the bottom.
8049 // The point of exit cannot be a branch out of the structured block.
8050 // longjmp() and throw() must not violate the entry/exit criteria.
8051 CS->getCapturedDecl()->setNothrow();
8054 OMPLoopDirective::HelperExprs B;
8055 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8056 // define the nested loops number.
8057 unsigned NestedLoopCount =
8058 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
8059 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
8060 VarsWithImplicitDSA, B);
8061 if (NestedLoopCount == 0)
8064 assert((CurContext->isDependentContext() || B.builtAll()) &&
8065 "omp target parallel for loop exprs were not built");
8067 if (!CurContext->isDependentContext()) {
8068 // Finalize the clauses that need pre-built expressions for CodeGen.
8069 for (OMPClause *C : Clauses) {
8070 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8071 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8072 B.NumIterations, *this, CurScope,
8078 setFunctionHasBranchProtectedScope();
8079 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
8080 NestedLoopCount, Clauses, AStmt,
8081 B, DSAStack->isCancelRegion());
8084 /// Check for existence of a map clause in the list of clauses.
8085 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
8086 const OpenMPClauseKind K) {
8087 return llvm::any_of(
8088 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
8091 template <typename... Params>
8092 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
8093 const Params... ClauseTypes) {
8094 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
8097 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
8099 SourceLocation StartLoc,
8100 SourceLocation EndLoc) {
8104 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8106 // OpenMP [2.10.1, Restrictions, p. 97]
8107 // At least one map clause must appear on the directive.
8108 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
8109 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8110 << "'map' or 'use_device_ptr'"
8111 << getOpenMPDirectiveName(OMPD_target_data);
8115 setFunctionHasBranchProtectedScope();
8117 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8122 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
8123 SourceLocation StartLoc,
8124 SourceLocation EndLoc, Stmt *AStmt) {
8128 auto *CS = cast<CapturedStmt>(AStmt);
8129 // 1.2.2 OpenMP Language Terminology
8130 // Structured block - An executable statement with a single entry at the
8131 // top and a single exit at the bottom.
8132 // The point of exit cannot be a branch out of the structured block.
8133 // longjmp() and throw() must not violate the entry/exit criteria.
8134 CS->getCapturedDecl()->setNothrow();
8135 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
8136 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8137 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8138 // 1.2.2 OpenMP Language Terminology
8139 // Structured block - An executable statement with a single entry at the
8140 // top and a single exit at the bottom.
8141 // The point of exit cannot be a branch out of the structured block.
8142 // longjmp() and throw() must not violate the entry/exit criteria.
8143 CS->getCapturedDecl()->setNothrow();
8146 // OpenMP [2.10.2, Restrictions, p. 99]
8147 // At least one map clause must appear on the directive.
8148 if (!hasClauses(Clauses, OMPC_map)) {
8149 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8150 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
8154 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8159 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
8160 SourceLocation StartLoc,
8161 SourceLocation EndLoc, Stmt *AStmt) {
8165 auto *CS = cast<CapturedStmt>(AStmt);
8166 // 1.2.2 OpenMP Language Terminology
8167 // Structured block - An executable statement with a single entry at the
8168 // top and a single exit at the bottom.
8169 // The point of exit cannot be a branch out of the structured block.
8170 // longjmp() and throw() must not violate the entry/exit criteria.
8171 CS->getCapturedDecl()->setNothrow();
8172 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
8173 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8174 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8175 // 1.2.2 OpenMP Language Terminology
8176 // Structured block - An executable statement with a single entry at the
8177 // top and a single exit at the bottom.
8178 // The point of exit cannot be a branch out of the structured block.
8179 // longjmp() and throw() must not violate the entry/exit criteria.
8180 CS->getCapturedDecl()->setNothrow();
8183 // OpenMP [2.10.3, Restrictions, p. 102]
8184 // At least one map clause must appear on the directive.
8185 if (!hasClauses(Clauses, OMPC_map)) {
8186 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8187 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
8191 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8195 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
8196 SourceLocation StartLoc,
8197 SourceLocation EndLoc,
8202 auto *CS = cast<CapturedStmt>(AStmt);
8203 // 1.2.2 OpenMP Language Terminology
8204 // Structured block - An executable statement with a single entry at the
8205 // top and a single exit at the bottom.
8206 // The point of exit cannot be a branch out of the structured block.
8207 // longjmp() and throw() must not violate the entry/exit criteria.
8208 CS->getCapturedDecl()->setNothrow();
8209 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
8210 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8211 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8212 // 1.2.2 OpenMP Language Terminology
8213 // Structured block - An executable statement with a single entry at the
8214 // top and a single exit at the bottom.
8215 // The point of exit cannot be a branch out of the structured block.
8216 // longjmp() and throw() must not violate the entry/exit criteria.
8217 CS->getCapturedDecl()->setNothrow();
8220 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
8221 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
8224 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
8228 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
8229 Stmt *AStmt, SourceLocation StartLoc,
8230 SourceLocation EndLoc) {
8234 auto *CS = cast<CapturedStmt>(AStmt);
8235 // 1.2.2 OpenMP Language Terminology
8236 // Structured block - An executable statement with a single entry at the
8237 // top and a single exit at the bottom.
8238 // The point of exit cannot be a branch out of the structured block.
8239 // longjmp() and throw() must not violate the entry/exit criteria.
8240 CS->getCapturedDecl()->setNothrow();
8242 setFunctionHasBranchProtectedScope();
8244 DSAStack->setParentTeamsRegionLoc(StartLoc);
8246 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8250 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
8251 SourceLocation EndLoc,
8252 OpenMPDirectiveKind CancelRegion) {
8253 if (DSAStack->isParentNowaitRegion()) {
8254 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
8257 if (DSAStack->isParentOrderedRegion()) {
8258 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
8261 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
8265 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
8266 SourceLocation StartLoc,
8267 SourceLocation EndLoc,
8268 OpenMPDirectiveKind CancelRegion) {
8269 if (DSAStack->isParentNowaitRegion()) {
8270 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
8273 if (DSAStack->isParentOrderedRegion()) {
8274 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
8277 DSAStack->setParentCancelRegion(/*Cancel=*/true);
8278 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
8282 static bool checkGrainsizeNumTasksClauses(Sema &S,
8283 ArrayRef<OMPClause *> Clauses) {
8284 const OMPClause *PrevClause = nullptr;
8285 bool ErrorFound = false;
8286 for (const OMPClause *C : Clauses) {
8287 if (C->getClauseKind() == OMPC_grainsize ||
8288 C->getClauseKind() == OMPC_num_tasks) {
8291 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
8292 S.Diag(C->getBeginLoc(),
8293 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
8294 << getOpenMPClauseName(C->getClauseKind())
8295 << getOpenMPClauseName(PrevClause->getClauseKind());
8296 S.Diag(PrevClause->getBeginLoc(),
8297 diag::note_omp_previous_grainsize_num_tasks)
8298 << getOpenMPClauseName(PrevClause->getClauseKind());
8306 static bool checkReductionClauseWithNogroup(Sema &S,
8307 ArrayRef<OMPClause *> Clauses) {
8308 const OMPClause *ReductionClause = nullptr;
8309 const OMPClause *NogroupClause = nullptr;
8310 for (const OMPClause *C : Clauses) {
8311 if (C->getClauseKind() == OMPC_reduction) {
8312 ReductionClause = C;
8317 if (C->getClauseKind() == OMPC_nogroup) {
8319 if (ReductionClause)
8324 if (ReductionClause && NogroupClause) {
8325 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
8326 << SourceRange(NogroupClause->getBeginLoc(),
8327 NogroupClause->getEndLoc());
8333 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
8334 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8335 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8339 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8340 OMPLoopDirective::HelperExprs B;
8341 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8342 // define the nested loops number.
8343 unsigned NestedLoopCount =
8344 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
8345 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
8346 VarsWithImplicitDSA, B);
8347 if (NestedLoopCount == 0)
8350 assert((CurContext->isDependentContext() || B.builtAll()) &&
8351 "omp for loop exprs were not built");
8353 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8354 // The grainsize clause and num_tasks clause are mutually exclusive and may
8355 // not appear on the same taskloop directive.
8356 if (checkGrainsizeNumTasksClauses(*this, Clauses))
8358 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8359 // If a reduction clause is present on the taskloop directive, the nogroup
8360 // clause must not be specified.
8361 if (checkReductionClauseWithNogroup(*this, Clauses))
8364 setFunctionHasBranchProtectedScope();
8365 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
8366 NestedLoopCount, Clauses, AStmt, B);
8369 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
8370 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8371 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8375 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8376 OMPLoopDirective::HelperExprs B;
8377 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8378 // define the nested loops number.
8379 unsigned NestedLoopCount =
8380 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
8381 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
8382 VarsWithImplicitDSA, B);
8383 if (NestedLoopCount == 0)
8386 assert((CurContext->isDependentContext() || B.builtAll()) &&
8387 "omp for loop exprs were not built");
8389 if (!CurContext->isDependentContext()) {
8390 // Finalize the clauses that need pre-built expressions for CodeGen.
8391 for (OMPClause *C : Clauses) {
8392 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8393 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8394 B.NumIterations, *this, CurScope,
8400 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8401 // The grainsize clause and num_tasks clause are mutually exclusive and may
8402 // not appear on the same taskloop directive.
8403 if (checkGrainsizeNumTasksClauses(*this, Clauses))
8405 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8406 // If a reduction clause is present on the taskloop directive, the nogroup
8407 // clause must not be specified.
8408 if (checkReductionClauseWithNogroup(*this, Clauses))
8410 if (checkSimdlenSafelenSpecified(*this, Clauses))
8413 setFunctionHasBranchProtectedScope();
8414 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
8415 NestedLoopCount, Clauses, AStmt, B);
8418 StmtResult Sema::ActOnOpenMPDistributeDirective(
8419 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8420 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8424 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8425 OMPLoopDirective::HelperExprs B;
8426 // In presence of clause 'collapse' with number of loops, it will
8427 // define the nested loops number.
8428 unsigned NestedLoopCount =
8429 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
8430 nullptr /*ordered not a clause on distribute*/, AStmt,
8431 *this, *DSAStack, VarsWithImplicitDSA, B);
8432 if (NestedLoopCount == 0)
8435 assert((CurContext->isDependentContext() || B.builtAll()) &&
8436 "omp for loop exprs were not built");
8438 setFunctionHasBranchProtectedScope();
8439 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
8440 NestedLoopCount, Clauses, AStmt, B);
8443 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
8444 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8445 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8449 auto *CS = cast<CapturedStmt>(AStmt);
8450 // 1.2.2 OpenMP Language Terminology
8451 // Structured block - An executable statement with a single entry at the
8452 // top and a single exit at the bottom.
8453 // The point of exit cannot be a branch out of the structured block.
8454 // longjmp() and throw() must not violate the entry/exit criteria.
8455 CS->getCapturedDecl()->setNothrow();
8456 for (int ThisCaptureLevel =
8457 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
8458 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8459 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8460 // 1.2.2 OpenMP Language Terminology
8461 // Structured block - An executable statement with a single entry at the
8462 // top and a single exit at the bottom.
8463 // The point of exit cannot be a branch out of the structured block.
8464 // longjmp() and throw() must not violate the entry/exit criteria.
8465 CS->getCapturedDecl()->setNothrow();
8468 OMPLoopDirective::HelperExprs B;
8469 // In presence of clause 'collapse' with number of loops, it will
8470 // define the nested loops number.
8471 unsigned NestedLoopCount = checkOpenMPLoop(
8472 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
8473 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
8474 VarsWithImplicitDSA, B);
8475 if (NestedLoopCount == 0)
8478 assert((CurContext->isDependentContext() || B.builtAll()) &&
8479 "omp for loop exprs were not built");
8481 setFunctionHasBranchProtectedScope();
8482 return OMPDistributeParallelForDirective::Create(
8483 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8484 DSAStack->isCancelRegion());
8487 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
8488 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8489 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8493 auto *CS = cast<CapturedStmt>(AStmt);
8494 // 1.2.2 OpenMP Language Terminology
8495 // Structured block - An executable statement with a single entry at the
8496 // top and a single exit at the bottom.
8497 // The point of exit cannot be a branch out of the structured block.
8498 // longjmp() and throw() must not violate the entry/exit criteria.
8499 CS->getCapturedDecl()->setNothrow();
8500 for (int ThisCaptureLevel =
8501 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
8502 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8503 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8504 // 1.2.2 OpenMP Language Terminology
8505 // Structured block - An executable statement with a single entry at the
8506 // top and a single exit at the bottom.
8507 // The point of exit cannot be a branch out of the structured block.
8508 // longjmp() and throw() must not violate the entry/exit criteria.
8509 CS->getCapturedDecl()->setNothrow();
8512 OMPLoopDirective::HelperExprs B;
8513 // In presence of clause 'collapse' with number of loops, it will
8514 // define the nested loops number.
8515 unsigned NestedLoopCount = checkOpenMPLoop(
8516 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
8517 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
8518 VarsWithImplicitDSA, B);
8519 if (NestedLoopCount == 0)
8522 assert((CurContext->isDependentContext() || B.builtAll()) &&
8523 "omp for loop exprs were not built");
8525 if (!CurContext->isDependentContext()) {
8526 // Finalize the clauses that need pre-built expressions for CodeGen.
8527 for (OMPClause *C : Clauses) {
8528 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8529 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8530 B.NumIterations, *this, CurScope,
8536 if (checkSimdlenSafelenSpecified(*this, Clauses))
8539 setFunctionHasBranchProtectedScope();
8540 return OMPDistributeParallelForSimdDirective::Create(
8541 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8544 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
8545 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8546 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8550 auto *CS = cast<CapturedStmt>(AStmt);
8551 // 1.2.2 OpenMP Language Terminology
8552 // Structured block - An executable statement with a single entry at the
8553 // top and a single exit at the bottom.
8554 // The point of exit cannot be a branch out of the structured block.
8555 // longjmp() and throw() must not violate the entry/exit criteria.
8556 CS->getCapturedDecl()->setNothrow();
8557 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
8558 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8559 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8560 // 1.2.2 OpenMP Language Terminology
8561 // Structured block - An executable statement with a single entry at the
8562 // top and a single exit at the bottom.
8563 // The point of exit cannot be a branch out of the structured block.
8564 // longjmp() and throw() must not violate the entry/exit criteria.
8565 CS->getCapturedDecl()->setNothrow();
8568 OMPLoopDirective::HelperExprs B;
8569 // In presence of clause 'collapse' with number of loops, it will
8570 // define the nested loops number.
8571 unsigned NestedLoopCount =
8572 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
8573 nullptr /*ordered not a clause on distribute*/, CS, *this,
8574 *DSAStack, VarsWithImplicitDSA, B);
8575 if (NestedLoopCount == 0)
8578 assert((CurContext->isDependentContext() || B.builtAll()) &&
8579 "omp for loop exprs were not built");
8581 if (!CurContext->isDependentContext()) {
8582 // Finalize the clauses that need pre-built expressions for CodeGen.
8583 for (OMPClause *C : Clauses) {
8584 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8585 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8586 B.NumIterations, *this, CurScope,
8592 if (checkSimdlenSafelenSpecified(*this, Clauses))
8595 setFunctionHasBranchProtectedScope();
8596 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
8597 NestedLoopCount, Clauses, AStmt, B);
8600 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
8601 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8602 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8606 auto *CS = cast<CapturedStmt>(AStmt);
8607 // 1.2.2 OpenMP Language Terminology
8608 // Structured block - An executable statement with a single entry at the
8609 // top and a single exit at the bottom.
8610 // The point of exit cannot be a branch out of the structured block.
8611 // longjmp() and throw() must not violate the entry/exit criteria.
8612 CS->getCapturedDecl()->setNothrow();
8613 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
8614 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8615 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8616 // 1.2.2 OpenMP Language Terminology
8617 // Structured block - An executable statement with a single entry at the
8618 // top and a single exit at the bottom.
8619 // The point of exit cannot be a branch out of the structured block.
8620 // longjmp() and throw() must not violate the entry/exit criteria.
8621 CS->getCapturedDecl()->setNothrow();
8624 OMPLoopDirective::HelperExprs B;
8625 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8626 // define the nested loops number.
8627 unsigned NestedLoopCount = checkOpenMPLoop(
8628 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
8629 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
8630 VarsWithImplicitDSA, B);
8631 if (NestedLoopCount == 0)
8634 assert((CurContext->isDependentContext() || B.builtAll()) &&
8635 "omp target parallel for simd loop exprs were not built");
8637 if (!CurContext->isDependentContext()) {
8638 // Finalize the clauses that need pre-built expressions for CodeGen.
8639 for (OMPClause *C : Clauses) {
8640 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8641 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8642 B.NumIterations, *this, CurScope,
8647 if (checkSimdlenSafelenSpecified(*this, Clauses))
8650 setFunctionHasBranchProtectedScope();
8651 return OMPTargetParallelForSimdDirective::Create(
8652 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8655 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
8656 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8657 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8661 auto *CS = cast<CapturedStmt>(AStmt);
8662 // 1.2.2 OpenMP Language Terminology
8663 // Structured block - An executable statement with a single entry at the
8664 // top and a single exit at the bottom.
8665 // The point of exit cannot be a branch out of the structured block.
8666 // longjmp() and throw() must not violate the entry/exit criteria.
8667 CS->getCapturedDecl()->setNothrow();
8668 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
8669 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8670 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8671 // 1.2.2 OpenMP Language Terminology
8672 // Structured block - An executable statement with a single entry at the
8673 // top and a single exit at the bottom.
8674 // The point of exit cannot be a branch out of the structured block.
8675 // longjmp() and throw() must not violate the entry/exit criteria.
8676 CS->getCapturedDecl()->setNothrow();
8679 OMPLoopDirective::HelperExprs B;
8680 // In presence of clause 'collapse' with number of loops, it will define the
8681 // nested loops number.
8682 unsigned NestedLoopCount =
8683 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
8684 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
8685 VarsWithImplicitDSA, B);
8686 if (NestedLoopCount == 0)
8689 assert((CurContext->isDependentContext() || B.builtAll()) &&
8690 "omp target simd loop exprs were not built");
8692 if (!CurContext->isDependentContext()) {
8693 // Finalize the clauses that need pre-built expressions for CodeGen.
8694 for (OMPClause *C : Clauses) {
8695 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8696 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8697 B.NumIterations, *this, CurScope,
8703 if (checkSimdlenSafelenSpecified(*this, Clauses))
8706 setFunctionHasBranchProtectedScope();
8707 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
8708 NestedLoopCount, Clauses, AStmt, B);
8711 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
8712 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8713 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8717 auto *CS = cast<CapturedStmt>(AStmt);
8718 // 1.2.2 OpenMP Language Terminology
8719 // Structured block - An executable statement with a single entry at the
8720 // top and a single exit at the bottom.
8721 // The point of exit cannot be a branch out of the structured block.
8722 // longjmp() and throw() must not violate the entry/exit criteria.
8723 CS->getCapturedDecl()->setNothrow();
8724 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
8725 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8726 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8727 // 1.2.2 OpenMP Language Terminology
8728 // Structured block - An executable statement with a single entry at the
8729 // top and a single exit at the bottom.
8730 // The point of exit cannot be a branch out of the structured block.
8731 // longjmp() and throw() must not violate the entry/exit criteria.
8732 CS->getCapturedDecl()->setNothrow();
8735 OMPLoopDirective::HelperExprs B;
8736 // In presence of clause 'collapse' with number of loops, it will
8737 // define the nested loops number.
8738 unsigned NestedLoopCount =
8739 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
8740 nullptr /*ordered not a clause on distribute*/, CS, *this,
8741 *DSAStack, VarsWithImplicitDSA, B);
8742 if (NestedLoopCount == 0)
8745 assert((CurContext->isDependentContext() || B.builtAll()) &&
8746 "omp teams distribute loop exprs were not built");
8748 setFunctionHasBranchProtectedScope();
8750 DSAStack->setParentTeamsRegionLoc(StartLoc);
8752 return OMPTeamsDistributeDirective::Create(
8753 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8756 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
8757 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8758 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8762 auto *CS = cast<CapturedStmt>(AStmt);
8763 // 1.2.2 OpenMP Language Terminology
8764 // Structured block - An executable statement with a single entry at the
8765 // top and a single exit at the bottom.
8766 // The point of exit cannot be a branch out of the structured block.
8767 // longjmp() and throw() must not violate the entry/exit criteria.
8768 CS->getCapturedDecl()->setNothrow();
8769 for (int ThisCaptureLevel =
8770 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
8771 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8772 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8773 // 1.2.2 OpenMP Language Terminology
8774 // Structured block - An executable statement with a single entry at the
8775 // top and a single exit at the bottom.
8776 // The point of exit cannot be a branch out of the structured block.
8777 // longjmp() and throw() must not violate the entry/exit criteria.
8778 CS->getCapturedDecl()->setNothrow();
8782 OMPLoopDirective::HelperExprs B;
8783 // In presence of clause 'collapse' with number of loops, it will
8784 // define the nested loops number.
8785 unsigned NestedLoopCount = checkOpenMPLoop(
8786 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
8787 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
8788 VarsWithImplicitDSA, B);
8790 if (NestedLoopCount == 0)
8793 assert((CurContext->isDependentContext() || B.builtAll()) &&
8794 "omp teams distribute simd loop exprs were not built");
8796 if (!CurContext->isDependentContext()) {
8797 // Finalize the clauses that need pre-built expressions for CodeGen.
8798 for (OMPClause *C : Clauses) {
8799 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8800 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8801 B.NumIterations, *this, CurScope,
8807 if (checkSimdlenSafelenSpecified(*this, Clauses))
8810 setFunctionHasBranchProtectedScope();
8812 DSAStack->setParentTeamsRegionLoc(StartLoc);
8814 return OMPTeamsDistributeSimdDirective::Create(
8815 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8818 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
8819 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8820 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8824 auto *CS = cast<CapturedStmt>(AStmt);
8825 // 1.2.2 OpenMP Language Terminology
8826 // Structured block - An executable statement with a single entry at the
8827 // top and a single exit at the bottom.
8828 // The point of exit cannot be a branch out of the structured block.
8829 // longjmp() and throw() must not violate the entry/exit criteria.
8830 CS->getCapturedDecl()->setNothrow();
8832 for (int ThisCaptureLevel =
8833 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
8834 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8835 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8836 // 1.2.2 OpenMP Language Terminology
8837 // Structured block - An executable statement with a single entry at the
8838 // top and a single exit at the bottom.
8839 // The point of exit cannot be a branch out of the structured block.
8840 // longjmp() and throw() must not violate the entry/exit criteria.
8841 CS->getCapturedDecl()->setNothrow();
8844 OMPLoopDirective::HelperExprs B;
8845 // In presence of clause 'collapse' with number of loops, it will
8846 // define the nested loops number.
8847 unsigned NestedLoopCount = checkOpenMPLoop(
8848 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
8849 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
8850 VarsWithImplicitDSA, B);
8852 if (NestedLoopCount == 0)
8855 assert((CurContext->isDependentContext() || B.builtAll()) &&
8856 "omp for loop exprs were not built");
8858 if (!CurContext->isDependentContext()) {
8859 // Finalize the clauses that need pre-built expressions for CodeGen.
8860 for (OMPClause *C : Clauses) {
8861 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8862 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8863 B.NumIterations, *this, CurScope,
8869 if (checkSimdlenSafelenSpecified(*this, Clauses))
8872 setFunctionHasBranchProtectedScope();
8874 DSAStack->setParentTeamsRegionLoc(StartLoc);
8876 return OMPTeamsDistributeParallelForSimdDirective::Create(
8877 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8880 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
8881 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8882 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8886 auto *CS = cast<CapturedStmt>(AStmt);
8887 // 1.2.2 OpenMP Language Terminology
8888 // Structured block - An executable statement with a single entry at the
8889 // top and a single exit at the bottom.
8890 // The point of exit cannot be a branch out of the structured block.
8891 // longjmp() and throw() must not violate the entry/exit criteria.
8892 CS->getCapturedDecl()->setNothrow();
8894 for (int ThisCaptureLevel =
8895 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
8896 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8897 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8898 // 1.2.2 OpenMP Language Terminology
8899 // Structured block - An executable statement with a single entry at the
8900 // top and a single exit at the bottom.
8901 // The point of exit cannot be a branch out of the structured block.
8902 // longjmp() and throw() must not violate the entry/exit criteria.
8903 CS->getCapturedDecl()->setNothrow();
8906 OMPLoopDirective::HelperExprs B;
8907 // In presence of clause 'collapse' with number of loops, it will
8908 // define the nested loops number.
8909 unsigned NestedLoopCount = checkOpenMPLoop(
8910 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
8911 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
8912 VarsWithImplicitDSA, B);
8914 if (NestedLoopCount == 0)
8917 assert((CurContext->isDependentContext() || B.builtAll()) &&
8918 "omp for loop exprs were not built");
8920 setFunctionHasBranchProtectedScope();
8922 DSAStack->setParentTeamsRegionLoc(StartLoc);
8924 return OMPTeamsDistributeParallelForDirective::Create(
8925 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8926 DSAStack->isCancelRegion());
8929 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
8931 SourceLocation StartLoc,
8932 SourceLocation EndLoc) {
8936 auto *CS = cast<CapturedStmt>(AStmt);
8937 // 1.2.2 OpenMP Language Terminology
8938 // Structured block - An executable statement with a single entry at the
8939 // top and a single exit at the bottom.
8940 // The point of exit cannot be a branch out of the structured block.
8941 // longjmp() and throw() must not violate the entry/exit criteria.
8942 CS->getCapturedDecl()->setNothrow();
8944 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
8945 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8946 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8947 // 1.2.2 OpenMP Language Terminology
8948 // Structured block - An executable statement with a single entry at the
8949 // top and a single exit at the bottom.
8950 // The point of exit cannot be a branch out of the structured block.
8951 // longjmp() and throw() must not violate the entry/exit criteria.
8952 CS->getCapturedDecl()->setNothrow();
8954 setFunctionHasBranchProtectedScope();
8956 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
8960 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
8961 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8962 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8966 auto *CS = cast<CapturedStmt>(AStmt);
8967 // 1.2.2 OpenMP Language Terminology
8968 // Structured block - An executable statement with a single entry at the
8969 // top and a single exit at the bottom.
8970 // The point of exit cannot be a branch out of the structured block.
8971 // longjmp() and throw() must not violate the entry/exit criteria.
8972 CS->getCapturedDecl()->setNothrow();
8973 for (int ThisCaptureLevel =
8974 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
8975 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8976 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8977 // 1.2.2 OpenMP Language Terminology
8978 // Structured block - An executable statement with a single entry at the
8979 // top and a single exit at the bottom.
8980 // The point of exit cannot be a branch out of the structured block.
8981 // longjmp() and throw() must not violate the entry/exit criteria.
8982 CS->getCapturedDecl()->setNothrow();
8985 OMPLoopDirective::HelperExprs B;
8986 // In presence of clause 'collapse' with number of loops, it will
8987 // define the nested loops number.
8988 unsigned NestedLoopCount = checkOpenMPLoop(
8989 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
8990 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
8991 VarsWithImplicitDSA, B);
8992 if (NestedLoopCount == 0)
8995 assert((CurContext->isDependentContext() || B.builtAll()) &&
8996 "omp target teams distribute loop exprs were not built");
8998 setFunctionHasBranchProtectedScope();
8999 return OMPTargetTeamsDistributeDirective::Create(
9000 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9003 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
9004 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9005 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9009 auto *CS = cast<CapturedStmt>(AStmt);
9010 // 1.2.2 OpenMP Language Terminology
9011 // Structured block - An executable statement with a single entry at the
9012 // top and a single exit at the bottom.
9013 // The point of exit cannot be a branch out of the structured block.
9014 // longjmp() and throw() must not violate the entry/exit criteria.
9015 CS->getCapturedDecl()->setNothrow();
9016 for (int ThisCaptureLevel =
9017 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
9018 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9019 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9020 // 1.2.2 OpenMP Language Terminology
9021 // Structured block - An executable statement with a single entry at the
9022 // top and a single exit at the bottom.
9023 // The point of exit cannot be a branch out of the structured block.
9024 // longjmp() and throw() must not violate the entry/exit criteria.
9025 CS->getCapturedDecl()->setNothrow();
9028 OMPLoopDirective::HelperExprs B;
9029 // In presence of clause 'collapse' with number of loops, it will
9030 // define the nested loops number.
9031 unsigned NestedLoopCount = checkOpenMPLoop(
9032 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
9033 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9034 VarsWithImplicitDSA, B);
9035 if (NestedLoopCount == 0)
9038 assert((CurContext->isDependentContext() || B.builtAll()) &&
9039 "omp target teams distribute parallel for loop exprs were not built");
9041 if (!CurContext->isDependentContext()) {
9042 // Finalize the clauses that need pre-built expressions for CodeGen.
9043 for (OMPClause *C : Clauses) {
9044 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9045 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9046 B.NumIterations, *this, CurScope,
9052 setFunctionHasBranchProtectedScope();
9053 return OMPTargetTeamsDistributeParallelForDirective::Create(
9054 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9055 DSAStack->isCancelRegion());
9058 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
9059 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9060 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9064 auto *CS = cast<CapturedStmt>(AStmt);
9065 // 1.2.2 OpenMP Language Terminology
9066 // Structured block - An executable statement with a single entry at the
9067 // top and a single exit at the bottom.
9068 // The point of exit cannot be a branch out of the structured block.
9069 // longjmp() and throw() must not violate the entry/exit criteria.
9070 CS->getCapturedDecl()->setNothrow();
9071 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
9072 OMPD_target_teams_distribute_parallel_for_simd);
9073 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9074 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9075 // 1.2.2 OpenMP Language Terminology
9076 // Structured block - An executable statement with a single entry at the
9077 // top and a single exit at the bottom.
9078 // The point of exit cannot be a branch out of the structured block.
9079 // longjmp() and throw() must not violate the entry/exit criteria.
9080 CS->getCapturedDecl()->setNothrow();
9083 OMPLoopDirective::HelperExprs B;
9084 // In presence of clause 'collapse' with number of loops, it will
9085 // define the nested loops number.
9086 unsigned NestedLoopCount =
9087 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
9088 getCollapseNumberExpr(Clauses),
9089 nullptr /*ordered not a clause on distribute*/, CS, *this,
9090 *DSAStack, VarsWithImplicitDSA, B);
9091 if (NestedLoopCount == 0)
9094 assert((CurContext->isDependentContext() || B.builtAll()) &&
9095 "omp target teams distribute parallel for simd loop exprs were not "
9098 if (!CurContext->isDependentContext()) {
9099 // Finalize the clauses that need pre-built expressions for CodeGen.
9100 for (OMPClause *C : Clauses) {
9101 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9102 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9103 B.NumIterations, *this, CurScope,
9109 if (checkSimdlenSafelenSpecified(*this, Clauses))
9112 setFunctionHasBranchProtectedScope();
9113 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
9114 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9117 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
9118 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9119 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9123 auto *CS = cast<CapturedStmt>(AStmt);
9124 // 1.2.2 OpenMP Language Terminology
9125 // Structured block - An executable statement with a single entry at the
9126 // top and a single exit at the bottom.
9127 // The point of exit cannot be a branch out of the structured block.
9128 // longjmp() and throw() must not violate the entry/exit criteria.
9129 CS->getCapturedDecl()->setNothrow();
9130 for (int ThisCaptureLevel =
9131 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
9132 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9133 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9134 // 1.2.2 OpenMP Language Terminology
9135 // Structured block - An executable statement with a single entry at the
9136 // top and a single exit at the bottom.
9137 // The point of exit cannot be a branch out of the structured block.
9138 // longjmp() and throw() must not violate the entry/exit criteria.
9139 CS->getCapturedDecl()->setNothrow();
9142 OMPLoopDirective::HelperExprs B;
9143 // In presence of clause 'collapse' with number of loops, it will
9144 // define the nested loops number.
9145 unsigned NestedLoopCount = checkOpenMPLoop(
9146 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
9147 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9148 VarsWithImplicitDSA, B);
9149 if (NestedLoopCount == 0)
9152 assert((CurContext->isDependentContext() || B.builtAll()) &&
9153 "omp target teams distribute simd loop exprs were not built");
9155 if (!CurContext->isDependentContext()) {
9156 // Finalize the clauses that need pre-built expressions for CodeGen.
9157 for (OMPClause *C : Clauses) {
9158 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9159 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9160 B.NumIterations, *this, CurScope,
9166 if (checkSimdlenSafelenSpecified(*this, Clauses))
9169 setFunctionHasBranchProtectedScope();
9170 return OMPTargetTeamsDistributeSimdDirective::Create(
9171 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9174 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
9175 SourceLocation StartLoc,
9176 SourceLocation LParenLoc,
9177 SourceLocation EndLoc) {
9178 OMPClause *Res = nullptr;
9181 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
9183 case OMPC_num_threads:
9184 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
9187 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
9190 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
9192 case OMPC_allocator:
9193 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
9196 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
9199 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
9202 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
9204 case OMPC_num_teams:
9205 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
9207 case OMPC_thread_limit:
9208 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
9211 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
9213 case OMPC_grainsize:
9214 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
9216 case OMPC_num_tasks:
9217 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
9220 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
9224 case OMPC_proc_bind:
9227 case OMPC_firstprivate:
9228 case OMPC_lastprivate:
9230 case OMPC_reduction:
9231 case OMPC_task_reduction:
9232 case OMPC_in_reduction:
9236 case OMPC_copyprivate:
9239 case OMPC_mergeable:
9240 case OMPC_threadprivate:
9253 case OMPC_dist_schedule:
9254 case OMPC_defaultmap:
9259 case OMPC_use_device_ptr:
9260 case OMPC_is_device_ptr:
9261 case OMPC_unified_address:
9262 case OMPC_unified_shared_memory:
9263 case OMPC_reverse_offload:
9264 case OMPC_dynamic_allocators:
9265 case OMPC_atomic_default_mem_order:
9266 llvm_unreachable("Clause is not allowed.");
9271 // An OpenMP directive such as 'target parallel' has two captured regions:
9272 // for the 'target' and 'parallel' respectively. This function returns
9273 // the region in which to capture expressions associated with a clause.
9274 // A return value of OMPD_unknown signifies that the expression should not
9276 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
9277 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
9278 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
9279 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
9283 case OMPD_target_parallel:
9284 case OMPD_target_parallel_for:
9285 case OMPD_target_parallel_for_simd:
9286 // If this clause applies to the nested 'parallel' region, capture within
9287 // the 'target' region, otherwise do not capture.
9288 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
9289 CaptureRegion = OMPD_target;
9291 case OMPD_target_teams_distribute_parallel_for:
9292 case OMPD_target_teams_distribute_parallel_for_simd:
9293 // If this clause applies to the nested 'parallel' region, capture within
9294 // the 'teams' region, otherwise do not capture.
9295 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
9296 CaptureRegion = OMPD_teams;
9298 case OMPD_teams_distribute_parallel_for:
9299 case OMPD_teams_distribute_parallel_for_simd:
9300 CaptureRegion = OMPD_teams;
9302 case OMPD_target_update:
9303 case OMPD_target_enter_data:
9304 case OMPD_target_exit_data:
9305 CaptureRegion = OMPD_task;
9309 case OMPD_parallel_sections:
9310 case OMPD_parallel_for:
9311 case OMPD_parallel_for_simd:
9313 case OMPD_target_simd:
9314 case OMPD_target_teams:
9315 case OMPD_target_teams_distribute:
9316 case OMPD_target_teams_distribute_simd:
9317 case OMPD_distribute_parallel_for:
9318 case OMPD_distribute_parallel_for_simd:
9321 case OMPD_taskloop_simd:
9322 case OMPD_target_data:
9323 // Do not capture if-clause expressions.
9325 case OMPD_threadprivate:
9327 case OMPD_taskyield:
9330 case OMPD_cancellation_point:
9332 case OMPD_declare_reduction:
9333 case OMPD_declare_mapper:
9334 case OMPD_declare_simd:
9335 case OMPD_declare_target:
9336 case OMPD_end_declare_target:
9346 case OMPD_taskgroup:
9347 case OMPD_distribute:
9350 case OMPD_distribute_simd:
9351 case OMPD_teams_distribute:
9352 case OMPD_teams_distribute_simd:
9354 llvm_unreachable("Unexpected OpenMP directive with if-clause");
9356 llvm_unreachable("Unknown OpenMP directive");
9359 case OMPC_num_threads:
9361 case OMPD_target_parallel:
9362 case OMPD_target_parallel_for:
9363 case OMPD_target_parallel_for_simd:
9364 CaptureRegion = OMPD_target;
9366 case OMPD_teams_distribute_parallel_for:
9367 case OMPD_teams_distribute_parallel_for_simd:
9368 case OMPD_target_teams_distribute_parallel_for:
9369 case OMPD_target_teams_distribute_parallel_for_simd:
9370 CaptureRegion = OMPD_teams;
9373 case OMPD_parallel_sections:
9374 case OMPD_parallel_for:
9375 case OMPD_parallel_for_simd:
9376 case OMPD_distribute_parallel_for:
9377 case OMPD_distribute_parallel_for_simd:
9378 // Do not capture num_threads-clause expressions.
9380 case OMPD_target_data:
9381 case OMPD_target_enter_data:
9382 case OMPD_target_exit_data:
9383 case OMPD_target_update:
9385 case OMPD_target_simd:
9386 case OMPD_target_teams:
9387 case OMPD_target_teams_distribute:
9388 case OMPD_target_teams_distribute_simd:
9392 case OMPD_taskloop_simd:
9393 case OMPD_threadprivate:
9395 case OMPD_taskyield:
9398 case OMPD_cancellation_point:
9400 case OMPD_declare_reduction:
9401 case OMPD_declare_mapper:
9402 case OMPD_declare_simd:
9403 case OMPD_declare_target:
9404 case OMPD_end_declare_target:
9414 case OMPD_taskgroup:
9415 case OMPD_distribute:
9418 case OMPD_distribute_simd:
9419 case OMPD_teams_distribute:
9420 case OMPD_teams_distribute_simd:
9422 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
9424 llvm_unreachable("Unknown OpenMP directive");
9427 case OMPC_num_teams:
9429 case OMPD_target_teams:
9430 case OMPD_target_teams_distribute:
9431 case OMPD_target_teams_distribute_simd:
9432 case OMPD_target_teams_distribute_parallel_for:
9433 case OMPD_target_teams_distribute_parallel_for_simd:
9434 CaptureRegion = OMPD_target;
9436 case OMPD_teams_distribute_parallel_for:
9437 case OMPD_teams_distribute_parallel_for_simd:
9439 case OMPD_teams_distribute:
9440 case OMPD_teams_distribute_simd:
9441 // Do not capture num_teams-clause expressions.
9443 case OMPD_distribute_parallel_for:
9444 case OMPD_distribute_parallel_for_simd:
9447 case OMPD_taskloop_simd:
9448 case OMPD_target_data:
9449 case OMPD_target_enter_data:
9450 case OMPD_target_exit_data:
9451 case OMPD_target_update:
9454 case OMPD_parallel_sections:
9455 case OMPD_parallel_for:
9456 case OMPD_parallel_for_simd:
9458 case OMPD_target_simd:
9459 case OMPD_target_parallel:
9460 case OMPD_target_parallel_for:
9461 case OMPD_target_parallel_for_simd:
9462 case OMPD_threadprivate:
9464 case OMPD_taskyield:
9467 case OMPD_cancellation_point:
9469 case OMPD_declare_reduction:
9470 case OMPD_declare_mapper:
9471 case OMPD_declare_simd:
9472 case OMPD_declare_target:
9473 case OMPD_end_declare_target:
9482 case OMPD_taskgroup:
9483 case OMPD_distribute:
9486 case OMPD_distribute_simd:
9488 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
9490 llvm_unreachable("Unknown OpenMP directive");
9493 case OMPC_thread_limit:
9495 case OMPD_target_teams:
9496 case OMPD_target_teams_distribute:
9497 case OMPD_target_teams_distribute_simd:
9498 case OMPD_target_teams_distribute_parallel_for:
9499 case OMPD_target_teams_distribute_parallel_for_simd:
9500 CaptureRegion = OMPD_target;
9502 case OMPD_teams_distribute_parallel_for:
9503 case OMPD_teams_distribute_parallel_for_simd:
9505 case OMPD_teams_distribute:
9506 case OMPD_teams_distribute_simd:
9507 // Do not capture thread_limit-clause expressions.
9509 case OMPD_distribute_parallel_for:
9510 case OMPD_distribute_parallel_for_simd:
9513 case OMPD_taskloop_simd:
9514 case OMPD_target_data:
9515 case OMPD_target_enter_data:
9516 case OMPD_target_exit_data:
9517 case OMPD_target_update:
9520 case OMPD_parallel_sections:
9521 case OMPD_parallel_for:
9522 case OMPD_parallel_for_simd:
9524 case OMPD_target_simd:
9525 case OMPD_target_parallel:
9526 case OMPD_target_parallel_for:
9527 case OMPD_target_parallel_for_simd:
9528 case OMPD_threadprivate:
9530 case OMPD_taskyield:
9533 case OMPD_cancellation_point:
9535 case OMPD_declare_reduction:
9536 case OMPD_declare_mapper:
9537 case OMPD_declare_simd:
9538 case OMPD_declare_target:
9539 case OMPD_end_declare_target:
9548 case OMPD_taskgroup:
9549 case OMPD_distribute:
9552 case OMPD_distribute_simd:
9554 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
9556 llvm_unreachable("Unknown OpenMP directive");
9561 case OMPD_parallel_for:
9562 case OMPD_parallel_for_simd:
9563 case OMPD_distribute_parallel_for:
9564 case OMPD_distribute_parallel_for_simd:
9565 case OMPD_teams_distribute_parallel_for:
9566 case OMPD_teams_distribute_parallel_for_simd:
9567 case OMPD_target_parallel_for:
9568 case OMPD_target_parallel_for_simd:
9569 case OMPD_target_teams_distribute_parallel_for:
9570 case OMPD_target_teams_distribute_parallel_for_simd:
9571 CaptureRegion = OMPD_parallel;
9575 // Do not capture schedule-clause expressions.
9579 case OMPD_taskloop_simd:
9580 case OMPD_target_data:
9581 case OMPD_target_enter_data:
9582 case OMPD_target_exit_data:
9583 case OMPD_target_update:
9585 case OMPD_teams_distribute:
9586 case OMPD_teams_distribute_simd:
9587 case OMPD_target_teams_distribute:
9588 case OMPD_target_teams_distribute_simd:
9590 case OMPD_target_simd:
9591 case OMPD_target_parallel:
9594 case OMPD_parallel_sections:
9595 case OMPD_threadprivate:
9597 case OMPD_taskyield:
9600 case OMPD_cancellation_point:
9602 case OMPD_declare_reduction:
9603 case OMPD_declare_mapper:
9604 case OMPD_declare_simd:
9605 case OMPD_declare_target:
9606 case OMPD_end_declare_target:
9613 case OMPD_taskgroup:
9614 case OMPD_distribute:
9617 case OMPD_distribute_simd:
9618 case OMPD_target_teams:
9620 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
9622 llvm_unreachable("Unknown OpenMP directive");
9625 case OMPC_dist_schedule:
9627 case OMPD_teams_distribute_parallel_for:
9628 case OMPD_teams_distribute_parallel_for_simd:
9629 case OMPD_teams_distribute:
9630 case OMPD_teams_distribute_simd:
9631 case OMPD_target_teams_distribute_parallel_for:
9632 case OMPD_target_teams_distribute_parallel_for_simd:
9633 case OMPD_target_teams_distribute:
9634 case OMPD_target_teams_distribute_simd:
9635 CaptureRegion = OMPD_teams;
9637 case OMPD_distribute_parallel_for:
9638 case OMPD_distribute_parallel_for_simd:
9639 case OMPD_distribute:
9640 case OMPD_distribute_simd:
9641 // Do not capture thread_limit-clause expressions.
9643 case OMPD_parallel_for:
9644 case OMPD_parallel_for_simd:
9645 case OMPD_target_parallel_for_simd:
9646 case OMPD_target_parallel_for:
9649 case OMPD_taskloop_simd:
9650 case OMPD_target_data:
9651 case OMPD_target_enter_data:
9652 case OMPD_target_exit_data:
9653 case OMPD_target_update:
9656 case OMPD_target_simd:
9657 case OMPD_target_parallel:
9660 case OMPD_parallel_sections:
9661 case OMPD_threadprivate:
9663 case OMPD_taskyield:
9666 case OMPD_cancellation_point:
9668 case OMPD_declare_reduction:
9669 case OMPD_declare_mapper:
9670 case OMPD_declare_simd:
9671 case OMPD_declare_target:
9672 case OMPD_end_declare_target:
9681 case OMPD_taskgroup:
9684 case OMPD_target_teams:
9686 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
9688 llvm_unreachable("Unknown OpenMP directive");
9693 case OMPD_target_update:
9694 case OMPD_target_enter_data:
9695 case OMPD_target_exit_data:
9697 case OMPD_target_simd:
9698 case OMPD_target_teams:
9699 case OMPD_target_parallel:
9700 case OMPD_target_teams_distribute:
9701 case OMPD_target_teams_distribute_simd:
9702 case OMPD_target_parallel_for:
9703 case OMPD_target_parallel_for_simd:
9704 case OMPD_target_teams_distribute_parallel_for:
9705 case OMPD_target_teams_distribute_parallel_for_simd:
9706 CaptureRegion = OMPD_task;
9708 case OMPD_target_data:
9709 // Do not capture device-clause expressions.
9711 case OMPD_teams_distribute_parallel_for:
9712 case OMPD_teams_distribute_parallel_for_simd:
9714 case OMPD_teams_distribute:
9715 case OMPD_teams_distribute_simd:
9716 case OMPD_distribute_parallel_for:
9717 case OMPD_distribute_parallel_for_simd:
9720 case OMPD_taskloop_simd:
9723 case OMPD_parallel_sections:
9724 case OMPD_parallel_for:
9725 case OMPD_parallel_for_simd:
9726 case OMPD_threadprivate:
9728 case OMPD_taskyield:
9731 case OMPD_cancellation_point:
9733 case OMPD_declare_reduction:
9734 case OMPD_declare_mapper:
9735 case OMPD_declare_simd:
9736 case OMPD_declare_target:
9737 case OMPD_end_declare_target:
9746 case OMPD_taskgroup:
9747 case OMPD_distribute:
9750 case OMPD_distribute_simd:
9752 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
9754 llvm_unreachable("Unknown OpenMP directive");
9757 case OMPC_firstprivate:
9758 case OMPC_lastprivate:
9759 case OMPC_reduction:
9760 case OMPC_task_reduction:
9761 case OMPC_in_reduction:
9764 case OMPC_proc_bind:
9768 case OMPC_allocator:
9774 case OMPC_copyprivate:
9778 case OMPC_mergeable:
9779 case OMPC_threadprivate:
9792 case OMPC_grainsize:
9794 case OMPC_num_tasks:
9796 case OMPC_defaultmap:
9801 case OMPC_use_device_ptr:
9802 case OMPC_is_device_ptr:
9803 case OMPC_unified_address:
9804 case OMPC_unified_shared_memory:
9805 case OMPC_reverse_offload:
9806 case OMPC_dynamic_allocators:
9807 case OMPC_atomic_default_mem_order:
9808 llvm_unreachable("Unexpected OpenMP clause.");
9810 return CaptureRegion;
9813 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
9814 Expr *Condition, SourceLocation StartLoc,
9815 SourceLocation LParenLoc,
9816 SourceLocation NameModifierLoc,
9817 SourceLocation ColonLoc,
9818 SourceLocation EndLoc) {
9819 Expr *ValExpr = Condition;
9820 Stmt *HelperValStmt = nullptr;
9821 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
9822 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
9823 !Condition->isInstantiationDependent() &&
9824 !Condition->containsUnexpandedParameterPack()) {
9825 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
9826 if (Val.isInvalid())
9829 ValExpr = Val.get();
9831 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9833 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
9834 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
9835 ValExpr = MakeFullExpr(ValExpr).get();
9836 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
9837 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9838 HelperValStmt = buildPreInits(Context, Captures);
9842 return new (Context)
9843 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
9844 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
9847 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
9848 SourceLocation StartLoc,
9849 SourceLocation LParenLoc,
9850 SourceLocation EndLoc) {
9851 Expr *ValExpr = Condition;
9852 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
9853 !Condition->isInstantiationDependent() &&
9854 !Condition->containsUnexpandedParameterPack()) {
9855 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
9856 if (Val.isInvalid())
9859 ValExpr = MakeFullExpr(Val.get()).get();
9862 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9864 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
9869 class IntConvertDiagnoser : public ICEConvertDiagnoser {
9871 IntConvertDiagnoser()
9872 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
9873 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
9874 QualType T) override {
9875 return S.Diag(Loc, diag::err_omp_not_integral) << T;
9877 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
9878 QualType T) override {
9879 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
9881 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
9883 QualType ConvTy) override {
9884 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
9886 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
9887 QualType ConvTy) override {
9888 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
9889 << ConvTy->isEnumeralType() << ConvTy;
9891 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
9892 QualType T) override {
9893 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
9895 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
9896 QualType ConvTy) override {
9897 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
9898 << ConvTy->isEnumeralType() << ConvTy;
9900 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
9901 QualType) override {
9902 llvm_unreachable("conversion functions are permitted");
9905 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
9908 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
9909 OpenMPClauseKind CKind,
9910 bool StrictlyPositive) {
9911 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
9912 !ValExpr->isInstantiationDependent()) {
9913 SourceLocation Loc = ValExpr->getExprLoc();
9915 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
9916 if (Value.isInvalid())
9919 ValExpr = Value.get();
9920 // The expression must evaluate to a non-negative integer value.
9921 llvm::APSInt Result;
9922 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
9923 Result.isSigned() &&
9924 !((!StrictlyPositive && Result.isNonNegative()) ||
9925 (StrictlyPositive && Result.isStrictlyPositive()))) {
9926 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
9927 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9928 << ValExpr->getSourceRange();
9935 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
9936 SourceLocation StartLoc,
9937 SourceLocation LParenLoc,
9938 SourceLocation EndLoc) {
9939 Expr *ValExpr = NumThreads;
9940 Stmt *HelperValStmt = nullptr;
9942 // OpenMP [2.5, Restrictions]
9943 // The num_threads expression must evaluate to a positive integer value.
9944 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
9945 /*StrictlyPositive=*/true))
9948 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9949 OpenMPDirectiveKind CaptureRegion =
9950 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
9951 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
9952 ValExpr = MakeFullExpr(ValExpr).get();
9953 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
9954 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9955 HelperValStmt = buildPreInits(Context, Captures);
9958 return new (Context) OMPNumThreadsClause(
9959 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
9962 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
9963 OpenMPClauseKind CKind,
9964 bool StrictlyPositive) {
9967 if (E->isValueDependent() || E->isTypeDependent() ||
9968 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
9970 llvm::APSInt Result;
9971 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
9972 if (ICE.isInvalid())
9974 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
9975 (!StrictlyPositive && !Result.isNonNegative())) {
9976 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
9977 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9978 << E->getSourceRange();
9981 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
9982 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
9983 << E->getSourceRange();
9986 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
9987 DSAStack->setAssociatedLoops(Result.getExtValue());
9988 else if (CKind == OMPC_ordered)
9989 DSAStack->setAssociatedLoops(Result.getExtValue());
9993 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
9994 SourceLocation LParenLoc,
9995 SourceLocation EndLoc) {
9996 // OpenMP [2.8.1, simd construct, Description]
9997 // The parameter of the safelen clause must be a constant
9998 // positive integer expression.
9999 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
10000 if (Safelen.isInvalid())
10002 return new (Context)
10003 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
10006 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
10007 SourceLocation LParenLoc,
10008 SourceLocation EndLoc) {
10009 // OpenMP [2.8.1, simd construct, Description]
10010 // The parameter of the simdlen clause must be a constant
10011 // positive integer expression.
10012 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
10013 if (Simdlen.isInvalid())
10015 return new (Context)
10016 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
10019 /// Tries to find omp_allocator_handle_t type.
10020 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
10021 DSAStackTy *Stack) {
10022 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
10023 if (!OMPAllocatorHandleT.isNull())
10025 // Build the predefined allocator expressions.
10026 bool ErrorFound = false;
10027 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
10028 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
10029 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
10030 StringRef Allocator =
10031 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
10032 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
10033 auto *VD = dyn_cast_or_null<ValueDecl>(
10034 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
10039 QualType AllocatorType =
10040 VD->getType().getNonLValueExprType(S.getASTContext());
10041 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
10042 if (!Res.isUsable()) {
10046 if (OMPAllocatorHandleT.isNull())
10047 OMPAllocatorHandleT = AllocatorType;
10048 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
10052 Stack->setAllocator(AllocatorKind, Res.get());
10055 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found);
10058 OMPAllocatorHandleT.addConst();
10059 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
10063 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
10064 SourceLocation LParenLoc,
10065 SourceLocation EndLoc) {
10066 // OpenMP [2.11.3, allocate Directive, Description]
10067 // allocator is an expression of omp_allocator_handle_t type.
10068 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
10071 ExprResult Allocator = DefaultLvalueConversion(A);
10072 if (Allocator.isInvalid())
10074 Allocator = PerformImplicitConversion(Allocator.get(),
10075 DSAStack->getOMPAllocatorHandleT(),
10076 Sema::AA_Initializing,
10077 /*AllowExplicit=*/true);
10078 if (Allocator.isInvalid())
10080 return new (Context)
10081 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
10084 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
10085 SourceLocation StartLoc,
10086 SourceLocation LParenLoc,
10087 SourceLocation EndLoc) {
10088 // OpenMP [2.7.1, loop construct, Description]
10089 // OpenMP [2.8.1, simd construct, Description]
10090 // OpenMP [2.9.6, distribute construct, Description]
10091 // The parameter of the collapse clause must be a constant
10092 // positive integer expression.
10093 ExprResult NumForLoopsResult =
10094 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
10095 if (NumForLoopsResult.isInvalid())
10097 return new (Context)
10098 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
10101 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
10102 SourceLocation EndLoc,
10103 SourceLocation LParenLoc,
10104 Expr *NumForLoops) {
10105 // OpenMP [2.7.1, loop construct, Description]
10106 // OpenMP [2.8.1, simd construct, Description]
10107 // OpenMP [2.9.6, distribute construct, Description]
10108 // The parameter of the ordered clause must be a constant
10109 // positive integer expression if any.
10110 if (NumForLoops && LParenLoc.isValid()) {
10111 ExprResult NumForLoopsResult =
10112 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
10113 if (NumForLoopsResult.isInvalid())
10115 NumForLoops = NumForLoopsResult.get();
10117 NumForLoops = nullptr;
10119 auto *Clause = OMPOrderedClause::Create(
10120 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
10121 StartLoc, LParenLoc, EndLoc);
10122 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
10126 OMPClause *Sema::ActOnOpenMPSimpleClause(
10127 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
10128 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
10129 OMPClause *Res = nullptr;
10133 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
10134 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
10136 case OMPC_proc_bind:
10137 Res = ActOnOpenMPProcBindClause(
10138 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
10139 LParenLoc, EndLoc);
10141 case OMPC_atomic_default_mem_order:
10142 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
10143 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
10144 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
10148 case OMPC_num_threads:
10151 case OMPC_allocator:
10152 case OMPC_collapse:
10153 case OMPC_schedule:
10155 case OMPC_firstprivate:
10156 case OMPC_lastprivate:
10158 case OMPC_reduction:
10159 case OMPC_task_reduction:
10160 case OMPC_in_reduction:
10164 case OMPC_copyprivate:
10168 case OMPC_mergeable:
10169 case OMPC_threadprivate:
10170 case OMPC_allocate:
10182 case OMPC_num_teams:
10183 case OMPC_thread_limit:
10184 case OMPC_priority:
10185 case OMPC_grainsize:
10187 case OMPC_num_tasks:
10189 case OMPC_dist_schedule:
10190 case OMPC_defaultmap:
10195 case OMPC_use_device_ptr:
10196 case OMPC_is_device_ptr:
10197 case OMPC_unified_address:
10198 case OMPC_unified_shared_memory:
10199 case OMPC_reverse_offload:
10200 case OMPC_dynamic_allocators:
10201 llvm_unreachable("Clause is not allowed.");
10207 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
10208 ArrayRef<unsigned> Exclude = llvm::None) {
10209 SmallString<256> Buffer;
10210 llvm::raw_svector_ostream Out(Buffer);
10211 unsigned Bound = Last >= 2 ? Last - 2 : 0;
10212 unsigned Skipped = Exclude.size();
10213 auto S = Exclude.begin(), E = Exclude.end();
10214 for (unsigned I = First; I < Last; ++I) {
10215 if (std::find(S, E, I) != E) {
10219 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
10220 if (I == Bound - Skipped)
10222 else if (I != Bound + 1 - Skipped)
10228 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
10229 SourceLocation KindKwLoc,
10230 SourceLocation StartLoc,
10231 SourceLocation LParenLoc,
10232 SourceLocation EndLoc) {
10233 if (Kind == OMPC_DEFAULT_unknown) {
10234 static_assert(OMPC_DEFAULT_unknown > 0,
10235 "OMPC_DEFAULT_unknown not greater than 0");
10236 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
10237 << getListOfPossibleValues(OMPC_default, /*First=*/0,
10238 /*Last=*/OMPC_DEFAULT_unknown)
10239 << getOpenMPClauseName(OMPC_default);
10243 case OMPC_DEFAULT_none:
10244 DSAStack->setDefaultDSANone(KindKwLoc);
10246 case OMPC_DEFAULT_shared:
10247 DSAStack->setDefaultDSAShared(KindKwLoc);
10249 case OMPC_DEFAULT_unknown:
10250 llvm_unreachable("Clause kind is not allowed.");
10253 return new (Context)
10254 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
10257 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
10258 SourceLocation KindKwLoc,
10259 SourceLocation StartLoc,
10260 SourceLocation LParenLoc,
10261 SourceLocation EndLoc) {
10262 if (Kind == OMPC_PROC_BIND_unknown) {
10263 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
10264 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
10265 /*Last=*/OMPC_PROC_BIND_unknown)
10266 << getOpenMPClauseName(OMPC_proc_bind);
10269 return new (Context)
10270 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
10273 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
10274 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
10275 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
10276 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
10277 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
10278 << getListOfPossibleValues(
10279 OMPC_atomic_default_mem_order, /*First=*/0,
10280 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
10281 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
10284 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
10285 LParenLoc, EndLoc);
10288 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
10289 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
10290 SourceLocation StartLoc, SourceLocation LParenLoc,
10291 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
10292 SourceLocation EndLoc) {
10293 OMPClause *Res = nullptr;
10295 case OMPC_schedule:
10296 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
10297 assert(Argument.size() == NumberOfElements &&
10298 ArgumentLoc.size() == NumberOfElements);
10299 Res = ActOnOpenMPScheduleClause(
10300 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
10301 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
10302 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
10303 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
10304 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
10307 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
10308 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
10309 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
10312 case OMPC_dist_schedule:
10313 Res = ActOnOpenMPDistScheduleClause(
10314 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
10315 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
10317 case OMPC_defaultmap:
10318 enum { Modifier, DefaultmapKind };
10319 Res = ActOnOpenMPDefaultmapClause(
10320 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
10321 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
10322 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
10326 case OMPC_num_threads:
10329 case OMPC_allocator:
10330 case OMPC_collapse:
10332 case OMPC_proc_bind:
10334 case OMPC_firstprivate:
10335 case OMPC_lastprivate:
10337 case OMPC_reduction:
10338 case OMPC_task_reduction:
10339 case OMPC_in_reduction:
10343 case OMPC_copyprivate:
10347 case OMPC_mergeable:
10348 case OMPC_threadprivate:
10349 case OMPC_allocate:
10361 case OMPC_num_teams:
10362 case OMPC_thread_limit:
10363 case OMPC_priority:
10364 case OMPC_grainsize:
10366 case OMPC_num_tasks:
10372 case OMPC_use_device_ptr:
10373 case OMPC_is_device_ptr:
10374 case OMPC_unified_address:
10375 case OMPC_unified_shared_memory:
10376 case OMPC_reverse_offload:
10377 case OMPC_dynamic_allocators:
10378 case OMPC_atomic_default_mem_order:
10379 llvm_unreachable("Clause is not allowed.");
10384 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
10385 OpenMPScheduleClauseModifier M2,
10386 SourceLocation M1Loc, SourceLocation M2Loc) {
10387 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
10388 SmallVector<unsigned, 2> Excluded;
10389 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
10390 Excluded.push_back(M2);
10391 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
10392 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
10393 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
10394 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
10395 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
10396 << getListOfPossibleValues(OMPC_schedule,
10397 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
10398 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
10400 << getOpenMPClauseName(OMPC_schedule);
10406 OMPClause *Sema::ActOnOpenMPScheduleClause(
10407 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
10408 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10409 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
10410 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
10411 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
10412 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
10414 // OpenMP, 2.7.1, Loop Construct, Restrictions
10415 // Either the monotonic modifier or the nonmonotonic modifier can be specified
10417 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
10418 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
10419 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
10420 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
10421 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
10422 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
10423 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
10424 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
10427 if (Kind == OMPC_SCHEDULE_unknown) {
10428 std::string Values;
10429 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
10430 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
10431 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
10432 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
10435 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
10436 /*Last=*/OMPC_SCHEDULE_unknown);
10438 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10439 << Values << getOpenMPClauseName(OMPC_schedule);
10442 // OpenMP, 2.7.1, Loop Construct, Restrictions
10443 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
10444 // schedule(guided).
10445 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
10446 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
10447 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
10448 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
10449 diag::err_omp_schedule_nonmonotonic_static);
10452 Expr *ValExpr = ChunkSize;
10453 Stmt *HelperValStmt = nullptr;
10455 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10456 !ChunkSize->isInstantiationDependent() &&
10457 !ChunkSize->containsUnexpandedParameterPack()) {
10458 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
10460 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10461 if (Val.isInvalid())
10464 ValExpr = Val.get();
10466 // OpenMP [2.7.1, Restrictions]
10467 // chunk_size must be a loop invariant integer expression with a positive
10469 llvm::APSInt Result;
10470 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10471 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10472 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10473 << "schedule" << 1 << ChunkSize->getSourceRange();
10476 } else if (getOpenMPCaptureRegionForClause(
10477 DSAStack->getCurrentDirective(), OMPC_schedule) !=
10479 !CurContext->isDependentContext()) {
10480 ValExpr = MakeFullExpr(ValExpr).get();
10481 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
10482 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10483 HelperValStmt = buildPreInits(Context, Captures);
10488 return new (Context)
10489 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
10490 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
10493 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
10494 SourceLocation StartLoc,
10495 SourceLocation EndLoc) {
10496 OMPClause *Res = nullptr;
10499 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
10502 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
10505 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
10507 case OMPC_mergeable:
10508 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
10511 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
10514 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
10517 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
10520 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
10523 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
10526 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
10529 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
10532 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
10534 case OMPC_unified_address:
10535 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
10537 case OMPC_unified_shared_memory:
10538 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
10540 case OMPC_reverse_offload:
10541 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
10543 case OMPC_dynamic_allocators:
10544 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
10548 case OMPC_num_threads:
10551 case OMPC_allocator:
10552 case OMPC_collapse:
10553 case OMPC_schedule:
10555 case OMPC_firstprivate:
10556 case OMPC_lastprivate:
10558 case OMPC_reduction:
10559 case OMPC_task_reduction:
10560 case OMPC_in_reduction:
10564 case OMPC_copyprivate:
10566 case OMPC_proc_bind:
10567 case OMPC_threadprivate:
10568 case OMPC_allocate:
10573 case OMPC_num_teams:
10574 case OMPC_thread_limit:
10575 case OMPC_priority:
10576 case OMPC_grainsize:
10577 case OMPC_num_tasks:
10579 case OMPC_dist_schedule:
10580 case OMPC_defaultmap:
10585 case OMPC_use_device_ptr:
10586 case OMPC_is_device_ptr:
10587 case OMPC_atomic_default_mem_order:
10588 llvm_unreachable("Clause is not allowed.");
10593 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
10594 SourceLocation EndLoc) {
10595 DSAStack->setNowaitRegion();
10596 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
10599 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
10600 SourceLocation EndLoc) {
10601 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
10604 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
10605 SourceLocation EndLoc) {
10606 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
10609 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
10610 SourceLocation EndLoc) {
10611 return new (Context) OMPReadClause(StartLoc, EndLoc);
10614 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
10615 SourceLocation EndLoc) {
10616 return new (Context) OMPWriteClause(StartLoc, EndLoc);
10619 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
10620 SourceLocation EndLoc) {
10621 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
10624 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
10625 SourceLocation EndLoc) {
10626 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
10629 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
10630 SourceLocation EndLoc) {
10631 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
10634 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
10635 SourceLocation EndLoc) {
10636 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
10639 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
10640 SourceLocation EndLoc) {
10641 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
10644 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
10645 SourceLocation EndLoc) {
10646 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
10649 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
10650 SourceLocation EndLoc) {
10651 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
10654 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
10655 SourceLocation EndLoc) {
10656 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
10659 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
10660 SourceLocation EndLoc) {
10661 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
10664 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
10665 SourceLocation EndLoc) {
10666 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
10669 OMPClause *Sema::ActOnOpenMPVarListClause(
10670 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
10671 const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
10672 CXXScopeSpec &ReductionOrMapperIdScopeSpec,
10673 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
10674 OpenMPLinearClauseKind LinKind,
10675 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
10676 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
10677 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
10678 SourceLocation StartLoc = Locs.StartLoc;
10679 SourceLocation LParenLoc = Locs.LParenLoc;
10680 SourceLocation EndLoc = Locs.EndLoc;
10681 OMPClause *Res = nullptr;
10684 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
10686 case OMPC_firstprivate:
10687 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
10689 case OMPC_lastprivate:
10690 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
10693 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
10695 case OMPC_reduction:
10696 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
10697 EndLoc, ReductionOrMapperIdScopeSpec,
10698 ReductionOrMapperId);
10700 case OMPC_task_reduction:
10701 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
10702 EndLoc, ReductionOrMapperIdScopeSpec,
10703 ReductionOrMapperId);
10705 case OMPC_in_reduction:
10706 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
10707 EndLoc, ReductionOrMapperIdScopeSpec,
10708 ReductionOrMapperId);
10711 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
10712 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
10715 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
10719 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
10721 case OMPC_copyprivate:
10722 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
10725 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
10728 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
10729 StartLoc, LParenLoc, EndLoc);
10732 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
10733 ReductionOrMapperIdScopeSpec,
10734 ReductionOrMapperId, MapType, IsMapTypeImplicit,
10735 DepLinMapLoc, ColonLoc, VarList, Locs);
10738 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
10739 ReductionOrMapperId, Locs);
10742 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
10743 ReductionOrMapperId, Locs);
10745 case OMPC_use_device_ptr:
10746 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
10748 case OMPC_is_device_ptr:
10749 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
10751 case OMPC_allocate:
10752 Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc,
10757 case OMPC_num_threads:
10760 case OMPC_allocator:
10761 case OMPC_collapse:
10763 case OMPC_proc_bind:
10764 case OMPC_schedule:
10768 case OMPC_mergeable:
10769 case OMPC_threadprivate:
10778 case OMPC_num_teams:
10779 case OMPC_thread_limit:
10780 case OMPC_priority:
10781 case OMPC_grainsize:
10783 case OMPC_num_tasks:
10785 case OMPC_dist_schedule:
10786 case OMPC_defaultmap:
10789 case OMPC_unified_address:
10790 case OMPC_unified_shared_memory:
10791 case OMPC_reverse_offload:
10792 case OMPC_dynamic_allocators:
10793 case OMPC_atomic_default_mem_order:
10794 llvm_unreachable("Clause is not allowed.");
10799 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
10800 ExprObjectKind OK, SourceLocation Loc) {
10801 ExprResult Res = BuildDeclRefExpr(
10802 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
10803 if (!Res.isUsable())
10804 return ExprError();
10805 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
10806 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
10807 if (!Res.isUsable())
10808 return ExprError();
10810 if (VK != VK_LValue && Res.get()->isGLValue()) {
10811 Res = DefaultLvalueConversion(Res.get());
10812 if (!Res.isUsable())
10813 return ExprError();
10818 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
10819 SourceLocation StartLoc,
10820 SourceLocation LParenLoc,
10821 SourceLocation EndLoc) {
10822 SmallVector<Expr *, 8> Vars;
10823 SmallVector<Expr *, 8> PrivateCopies;
10824 for (Expr *RefExpr : VarList) {
10825 assert(RefExpr && "NULL expr in OpenMP private clause.");
10826 SourceLocation ELoc;
10827 SourceRange ERange;
10828 Expr *SimpleRefExpr = RefExpr;
10829 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
10831 // It will be analyzed later.
10832 Vars.push_back(RefExpr);
10833 PrivateCopies.push_back(nullptr);
10835 ValueDecl *D = Res.first;
10839 QualType Type = D->getType();
10840 auto *VD = dyn_cast<VarDecl>(D);
10842 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10843 // A variable that appears in a private clause must not have an incomplete
10844 // type or a reference type.
10845 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
10847 Type = Type.getNonReferenceType();
10849 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10850 // A variable that is privatized must not have a const-qualified type
10851 // unless it is of class type with a mutable member. This restriction does
10852 // not apply to the firstprivate clause.
10854 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
10855 // A variable that appears in a private clause must not have a
10856 // const-qualified type unless it is of class type with a mutable member.
10857 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
10860 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10862 // Variables with the predetermined data-sharing attributes may not be
10863 // listed in data-sharing attributes clauses, except for the cases
10864 // listed below. For these exceptions only, listing a predetermined
10865 // variable in a data-sharing attribute clause is allowed and overrides
10866 // the variable's predetermined data-sharing attributes.
10867 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
10868 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
10869 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10870 << getOpenMPClauseName(OMPC_private);
10871 reportOriginalDsa(*this, DSAStack, D, DVar);
10875 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
10876 // Variably modified types are not supported for tasks.
10877 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
10878 isOpenMPTaskingDirective(CurrDir)) {
10879 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10880 << getOpenMPClauseName(OMPC_private) << Type
10881 << getOpenMPDirectiveName(CurrDir);
10884 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10885 Diag(D->getLocation(),
10886 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10891 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10892 // A list item cannot appear in both a map clause and a data-sharing
10893 // attribute clause on the same construct
10894 if (isOpenMPTargetExecutionDirective(CurrDir)) {
10895 OpenMPClauseKind ConflictKind;
10896 if (DSAStack->checkMappableExprComponentListsForDecl(
10897 VD, /*CurrentRegionOnly=*/true,
10898 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
10899 OpenMPClauseKind WhereFoundClauseKind) -> bool {
10900 ConflictKind = WhereFoundClauseKind;
10903 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
10904 << getOpenMPClauseName(OMPC_private)
10905 << getOpenMPClauseName(ConflictKind)
10906 << getOpenMPDirectiveName(CurrDir);
10907 reportOriginalDsa(*this, DSAStack, D, DVar);
10912 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
10913 // A variable of class type (or array thereof) that appears in a private
10914 // clause requires an accessible, unambiguous default constructor for the
10916 // Generate helper private variable and initialize it with the default
10917 // value. The address of the original variable is replaced by the address of
10918 // the new private variable in CodeGen. This new variable is not added to
10919 // IdResolver, so the code in the OpenMP region uses original variable for
10920 // proper diagnostics.
10921 Type = Type.getUnqualifiedType();
10922 VarDecl *VDPrivate =
10923 buildVarDecl(*this, ELoc, Type, D->getName(),
10924 D->hasAttrs() ? &D->getAttrs() : nullptr,
10925 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
10926 ActOnUninitializedDecl(VDPrivate);
10927 if (VDPrivate->isInvalidDecl())
10929 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
10930 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
10932 DeclRefExpr *Ref = nullptr;
10933 if (!VD && !CurContext->isDependentContext())
10934 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
10935 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
10936 Vars.push_back((VD || CurContext->isDependentContext())
10937 ? RefExpr->IgnoreParens()
10939 PrivateCopies.push_back(VDPrivateRefExpr);
10945 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10950 class DiagsUninitializedSeveretyRAII {
10952 DiagnosticsEngine &Diags;
10953 SourceLocation SavedLoc;
10954 bool IsIgnored = false;
10957 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
10959 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
10961 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
10962 /*Map*/ diag::Severity::Ignored, Loc);
10965 ~DiagsUninitializedSeveretyRAII() {
10967 Diags.popMappings(SavedLoc);
10972 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
10973 SourceLocation StartLoc,
10974 SourceLocation LParenLoc,
10975 SourceLocation EndLoc) {
10976 SmallVector<Expr *, 8> Vars;
10977 SmallVector<Expr *, 8> PrivateCopies;
10978 SmallVector<Expr *, 8> Inits;
10979 SmallVector<Decl *, 4> ExprCaptures;
10980 bool IsImplicitClause =
10981 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
10982 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
10984 for (Expr *RefExpr : VarList) {
10985 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
10986 SourceLocation ELoc;
10987 SourceRange ERange;
10988 Expr *SimpleRefExpr = RefExpr;
10989 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
10991 // It will be analyzed later.
10992 Vars.push_back(RefExpr);
10993 PrivateCopies.push_back(nullptr);
10994 Inits.push_back(nullptr);
10996 ValueDecl *D = Res.first;
11000 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
11001 QualType Type = D->getType();
11002 auto *VD = dyn_cast<VarDecl>(D);
11004 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11005 // A variable that appears in a private clause must not have an incomplete
11006 // type or a reference type.
11007 if (RequireCompleteType(ELoc, Type,
11008 diag::err_omp_firstprivate_incomplete_type))
11010 Type = Type.getNonReferenceType();
11012 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
11013 // A variable of class type (or array thereof) that appears in a private
11014 // clause requires an accessible, unambiguous copy constructor for the
11016 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11018 // If an implicit firstprivate variable found it was checked already.
11019 DSAStackTy::DSAVarData TopDVar;
11020 if (!IsImplicitClause) {
11021 DSAStackTy::DSAVarData DVar =
11022 DSAStack->getTopDSA(D, /*FromParent=*/false);
11024 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
11025 bool IsConstant = ElemType.isConstant(Context);
11026 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
11027 // A list item that specifies a given variable may not appear in more
11028 // than one clause on the same directive, except that a variable may be
11029 // specified in both firstprivate and lastprivate clauses.
11030 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
11031 // A list item may appear in a firstprivate or lastprivate clause but not
11033 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
11034 (isOpenMPDistributeDirective(CurrDir) ||
11035 DVar.CKind != OMPC_lastprivate) &&
11037 Diag(ELoc, diag::err_omp_wrong_dsa)
11038 << getOpenMPClauseName(DVar.CKind)
11039 << getOpenMPClauseName(OMPC_firstprivate);
11040 reportOriginalDsa(*this, DSAStack, D, DVar);
11044 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11046 // Variables with the predetermined data-sharing attributes may not be
11047 // listed in data-sharing attributes clauses, except for the cases
11048 // listed below. For these exceptions only, listing a predetermined
11049 // variable in a data-sharing attribute clause is allowed and overrides
11050 // the variable's predetermined data-sharing attributes.
11051 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11052 // in a Construct, C/C++, p.2]
11053 // Variables with const-qualified type having no mutable member may be
11054 // listed in a firstprivate clause, even if they are static data members.
11055 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
11056 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
11057 Diag(ELoc, diag::err_omp_wrong_dsa)
11058 << getOpenMPClauseName(DVar.CKind)
11059 << getOpenMPClauseName(OMPC_firstprivate);
11060 reportOriginalDsa(*this, DSAStack, D, DVar);
11064 // OpenMP [2.9.3.4, Restrictions, p.2]
11065 // A list item that is private within a parallel region must not appear
11066 // in a firstprivate clause on a worksharing construct if any of the
11067 // worksharing regions arising from the worksharing construct ever bind
11068 // to any of the parallel regions arising from the parallel construct.
11069 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
11070 // A list item that is private within a teams region must not appear in a
11071 // firstprivate clause on a distribute construct if any of the distribute
11072 // regions arising from the distribute construct ever bind to any of the
11073 // teams regions arising from the teams construct.
11074 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
11075 // A list item that appears in a reduction clause of a teams construct
11076 // must not appear in a firstprivate clause on a distribute construct if
11077 // any of the distribute regions arising from the distribute construct
11078 // ever bind to any of the teams regions arising from the teams construct.
11079 if ((isOpenMPWorksharingDirective(CurrDir) ||
11080 isOpenMPDistributeDirective(CurrDir)) &&
11081 !isOpenMPParallelDirective(CurrDir) &&
11082 !isOpenMPTeamsDirective(CurrDir)) {
11083 DVar = DSAStack->getImplicitDSA(D, true);
11084 if (DVar.CKind != OMPC_shared &&
11085 (isOpenMPParallelDirective(DVar.DKind) ||
11086 isOpenMPTeamsDirective(DVar.DKind) ||
11087 DVar.DKind == OMPD_unknown)) {
11088 Diag(ELoc, diag::err_omp_required_access)
11089 << getOpenMPClauseName(OMPC_firstprivate)
11090 << getOpenMPClauseName(OMPC_shared);
11091 reportOriginalDsa(*this, DSAStack, D, DVar);
11095 // OpenMP [2.9.3.4, Restrictions, p.3]
11096 // A list item that appears in a reduction clause of a parallel construct
11097 // must not appear in a firstprivate clause on a worksharing or task
11098 // construct if any of the worksharing or task regions arising from the
11099 // worksharing or task construct ever bind to any of the parallel regions
11100 // arising from the parallel construct.
11101 // OpenMP [2.9.3.4, Restrictions, p.4]
11102 // A list item that appears in a reduction clause in worksharing
11103 // construct must not appear in a firstprivate clause in a task construct
11104 // encountered during execution of any of the worksharing regions arising
11105 // from the worksharing construct.
11106 if (isOpenMPTaskingDirective(CurrDir)) {
11107 DVar = DSAStack->hasInnermostDSA(
11108 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
11109 [](OpenMPDirectiveKind K) {
11110 return isOpenMPParallelDirective(K) ||
11111 isOpenMPWorksharingDirective(K) ||
11112 isOpenMPTeamsDirective(K);
11114 /*FromParent=*/true);
11115 if (DVar.CKind == OMPC_reduction &&
11116 (isOpenMPParallelDirective(DVar.DKind) ||
11117 isOpenMPWorksharingDirective(DVar.DKind) ||
11118 isOpenMPTeamsDirective(DVar.DKind))) {
11119 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
11120 << getOpenMPDirectiveName(DVar.DKind);
11121 reportOriginalDsa(*this, DSAStack, D, DVar);
11126 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11127 // A list item cannot appear in both a map clause and a data-sharing
11128 // attribute clause on the same construct
11129 if (isOpenMPTargetExecutionDirective(CurrDir)) {
11130 OpenMPClauseKind ConflictKind;
11131 if (DSAStack->checkMappableExprComponentListsForDecl(
11132 VD, /*CurrentRegionOnly=*/true,
11134 OMPClauseMappableExprCommon::MappableExprComponentListRef,
11135 OpenMPClauseKind WhereFoundClauseKind) {
11136 ConflictKind = WhereFoundClauseKind;
11139 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11140 << getOpenMPClauseName(OMPC_firstprivate)
11141 << getOpenMPClauseName(ConflictKind)
11142 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11143 reportOriginalDsa(*this, DSAStack, D, DVar);
11149 // Variably modified types are not supported for tasks.
11150 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
11151 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
11152 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11153 << getOpenMPClauseName(OMPC_firstprivate) << Type
11154 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11157 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11158 Diag(D->getLocation(),
11159 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11164 Type = Type.getUnqualifiedType();
11165 VarDecl *VDPrivate =
11166 buildVarDecl(*this, ELoc, Type, D->getName(),
11167 D->hasAttrs() ? &D->getAttrs() : nullptr,
11168 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
11169 // Generate helper private variable and initialize it with the value of the
11170 // original variable. The address of the original variable is replaced by
11171 // the address of the new private variable in the CodeGen. This new variable
11172 // is not added to IdResolver, so the code in the OpenMP region uses
11173 // original variable for proper diagnostics and variable capturing.
11174 Expr *VDInitRefExpr = nullptr;
11175 // For arrays generate initializer for single element and replace it by the
11176 // original array element in CodeGen.
11177 if (Type->isArrayType()) {
11179 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
11180 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
11181 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
11182 ElemType = ElemType.getUnqualifiedType();
11183 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
11184 ".firstprivate.temp");
11185 InitializedEntity Entity =
11186 InitializedEntity::InitializeVariable(VDInitTemp);
11187 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
11189 InitializationSequence InitSeq(*this, Entity, Kind, Init);
11190 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
11191 if (Result.isInvalid())
11192 VDPrivate->setInvalidDecl();
11194 VDPrivate->setInit(Result.getAs<Expr>());
11195 // Remove temp variable declaration.
11196 Context.Deallocate(VDInitTemp);
11198 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
11199 ".firstprivate.temp");
11200 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11201 RefExpr->getExprLoc());
11202 AddInitializerToDecl(VDPrivate,
11203 DefaultLvalueConversion(VDInitRefExpr).get(),
11204 /*DirectInit=*/false);
11206 if (VDPrivate->isInvalidDecl()) {
11207 if (IsImplicitClause) {
11208 Diag(RefExpr->getExprLoc(),
11209 diag::note_omp_task_predetermined_firstprivate_here);
11213 CurContext->addDecl(VDPrivate);
11214 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
11215 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
11216 RefExpr->getExprLoc());
11217 DeclRefExpr *Ref = nullptr;
11218 if (!VD && !CurContext->isDependentContext()) {
11219 if (TopDVar.CKind == OMPC_lastprivate) {
11220 Ref = TopDVar.PrivateCopy;
11222 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11223 if (!isOpenMPCapturedDecl(D))
11224 ExprCaptures.push_back(Ref->getDecl());
11227 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
11228 Vars.push_back((VD || CurContext->isDependentContext())
11229 ? RefExpr->IgnoreParens()
11231 PrivateCopies.push_back(VDPrivateRefExpr);
11232 Inits.push_back(VDInitRefExpr);
11238 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11239 Vars, PrivateCopies, Inits,
11240 buildPreInits(Context, ExprCaptures));
11243 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
11244 SourceLocation StartLoc,
11245 SourceLocation LParenLoc,
11246 SourceLocation EndLoc) {
11247 SmallVector<Expr *, 8> Vars;
11248 SmallVector<Expr *, 8> SrcExprs;
11249 SmallVector<Expr *, 8> DstExprs;
11250 SmallVector<Expr *, 8> AssignmentOps;
11251 SmallVector<Decl *, 4> ExprCaptures;
11252 SmallVector<Expr *, 4> ExprPostUpdates;
11253 for (Expr *RefExpr : VarList) {
11254 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
11255 SourceLocation ELoc;
11256 SourceRange ERange;
11257 Expr *SimpleRefExpr = RefExpr;
11258 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11260 // It will be analyzed later.
11261 Vars.push_back(RefExpr);
11262 SrcExprs.push_back(nullptr);
11263 DstExprs.push_back(nullptr);
11264 AssignmentOps.push_back(nullptr);
11266 ValueDecl *D = Res.first;
11270 QualType Type = D->getType();
11271 auto *VD = dyn_cast<VarDecl>(D);
11273 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
11274 // A variable that appears in a lastprivate clause must not have an
11275 // incomplete type or a reference type.
11276 if (RequireCompleteType(ELoc, Type,
11277 diag::err_omp_lastprivate_incomplete_type))
11279 Type = Type.getNonReferenceType();
11281 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11282 // A variable that is privatized must not have a const-qualified type
11283 // unless it is of class type with a mutable member. This restriction does
11284 // not apply to the firstprivate clause.
11286 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
11287 // A variable that appears in a lastprivate clause must not have a
11288 // const-qualified type unless it is of class type with a mutable member.
11289 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
11292 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
11293 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
11295 // Variables with the predetermined data-sharing attributes may not be
11296 // listed in data-sharing attributes clauses, except for the cases
11298 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
11299 // A list item may appear in a firstprivate or lastprivate clause but not
11301 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
11302 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
11303 (isOpenMPDistributeDirective(CurrDir) ||
11304 DVar.CKind != OMPC_firstprivate) &&
11305 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
11306 Diag(ELoc, diag::err_omp_wrong_dsa)
11307 << getOpenMPClauseName(DVar.CKind)
11308 << getOpenMPClauseName(OMPC_lastprivate);
11309 reportOriginalDsa(*this, DSAStack, D, DVar);
11313 // OpenMP [2.14.3.5, Restrictions, p.2]
11314 // A list item that is private within a parallel region, or that appears in
11315 // the reduction clause of a parallel construct, must not appear in a
11316 // lastprivate clause on a worksharing construct if any of the corresponding
11317 // worksharing regions ever binds to any of the corresponding parallel
11319 DSAStackTy::DSAVarData TopDVar = DVar;
11320 if (isOpenMPWorksharingDirective(CurrDir) &&
11321 !isOpenMPParallelDirective(CurrDir) &&
11322 !isOpenMPTeamsDirective(CurrDir)) {
11323 DVar = DSAStack->getImplicitDSA(D, true);
11324 if (DVar.CKind != OMPC_shared) {
11325 Diag(ELoc, diag::err_omp_required_access)
11326 << getOpenMPClauseName(OMPC_lastprivate)
11327 << getOpenMPClauseName(OMPC_shared);
11328 reportOriginalDsa(*this, DSAStack, D, DVar);
11333 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
11334 // A variable of class type (or array thereof) that appears in a
11335 // lastprivate clause requires an accessible, unambiguous default
11336 // constructor for the class type, unless the list item is also specified
11337 // in a firstprivate clause.
11338 // A variable of class type (or array thereof) that appears in a
11339 // lastprivate clause requires an accessible, unambiguous copy assignment
11340 // operator for the class type.
11341 Type = Context.getBaseElementType(Type).getNonReferenceType();
11342 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
11343 Type.getUnqualifiedType(), ".lastprivate.src",
11344 D->hasAttrs() ? &D->getAttrs() : nullptr);
11345 DeclRefExpr *PseudoSrcExpr =
11346 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
11348 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
11349 D->hasAttrs() ? &D->getAttrs() : nullptr);
11350 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11351 // For arrays generate assignment operation for single element and replace
11352 // it by the original array element in CodeGen.
11353 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
11354 PseudoDstExpr, PseudoSrcExpr);
11355 if (AssignmentOp.isInvalid())
11358 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
11359 if (AssignmentOp.isInvalid())
11362 DeclRefExpr *Ref = nullptr;
11363 if (!VD && !CurContext->isDependentContext()) {
11364 if (TopDVar.CKind == OMPC_firstprivate) {
11365 Ref = TopDVar.PrivateCopy;
11367 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
11368 if (!isOpenMPCapturedDecl(D))
11369 ExprCaptures.push_back(Ref->getDecl());
11371 if (TopDVar.CKind == OMPC_firstprivate ||
11372 (!isOpenMPCapturedDecl(D) &&
11373 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
11374 ExprResult RefRes = DefaultLvalueConversion(Ref);
11375 if (!RefRes.isUsable())
11377 ExprResult PostUpdateRes =
11378 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11380 if (!PostUpdateRes.isUsable())
11382 ExprPostUpdates.push_back(
11383 IgnoredValueConversions(PostUpdateRes.get()).get());
11386 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
11387 Vars.push_back((VD || CurContext->isDependentContext())
11388 ? RefExpr->IgnoreParens()
11390 SrcExprs.push_back(PseudoSrcExpr);
11391 DstExprs.push_back(PseudoDstExpr);
11392 AssignmentOps.push_back(AssignmentOp.get());
11398 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11399 Vars, SrcExprs, DstExprs, AssignmentOps,
11400 buildPreInits(Context, ExprCaptures),
11401 buildPostUpdate(*this, ExprPostUpdates));
11404 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
11405 SourceLocation StartLoc,
11406 SourceLocation LParenLoc,
11407 SourceLocation EndLoc) {
11408 SmallVector<Expr *, 8> Vars;
11409 for (Expr *RefExpr : VarList) {
11410 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
11411 SourceLocation ELoc;
11412 SourceRange ERange;
11413 Expr *SimpleRefExpr = RefExpr;
11414 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11416 // It will be analyzed later.
11417 Vars.push_back(RefExpr);
11419 ValueDecl *D = Res.first;
11423 auto *VD = dyn_cast<VarDecl>(D);
11424 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11426 // Variables with the predetermined data-sharing attributes may not be
11427 // listed in data-sharing attributes clauses, except for the cases
11428 // listed below. For these exceptions only, listing a predetermined
11429 // variable in a data-sharing attribute clause is allowed and overrides
11430 // the variable's predetermined data-sharing attributes.
11431 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
11432 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
11434 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11435 << getOpenMPClauseName(OMPC_shared);
11436 reportOriginalDsa(*this, DSAStack, D, DVar);
11440 DeclRefExpr *Ref = nullptr;
11441 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
11442 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11443 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
11444 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
11445 ? RefExpr->IgnoreParens()
11452 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
11456 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
11460 bool VisitDeclRefExpr(DeclRefExpr *E) {
11461 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
11462 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
11463 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
11465 if (DVar.CKind != OMPC_unknown)
11467 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
11468 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
11469 /*FromParent=*/true);
11470 return DVarPrivate.CKind != OMPC_unknown;
11474 bool VisitStmt(Stmt *S) {
11475 for (Stmt *Child : S->children()) {
11476 if (Child && Visit(Child))
11481 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
11486 // Transform MemberExpression for specified FieldDecl of current class to
11487 // DeclRefExpr to specified OMPCapturedExprDecl.
11488 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
11489 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
11490 ValueDecl *Field = nullptr;
11491 DeclRefExpr *CapturedExpr = nullptr;
11494 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
11495 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
11497 ExprResult TransformMemberExpr(MemberExpr *E) {
11498 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
11499 E->getMemberDecl() == Field) {
11500 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
11501 return CapturedExpr;
11503 return BaseTransform::TransformMemberExpr(E);
11505 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
11509 template <typename T, typename U>
11510 static T filterLookupForUDReductionAndMapper(
11511 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
11512 for (U &Set : Lookups) {
11513 for (auto *D : Set) {
11514 if (T Res = Gen(cast<ValueDecl>(D)))
11521 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
11522 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
11524 for (auto RD : D->redecls()) {
11525 // Don't bother with extra checks if we already know this one isn't visible.
11529 auto ND = cast<NamedDecl>(RD);
11530 if (LookupResult::isVisible(SemaRef, ND))
11538 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
11539 SourceLocation Loc, QualType Ty,
11540 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
11541 // Find all of the associated namespaces and classes based on the
11542 // arguments we have.
11543 Sema::AssociatedNamespaceSet AssociatedNamespaces;
11544 Sema::AssociatedClassSet AssociatedClasses;
11545 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
11546 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
11547 AssociatedClasses);
11549 // C++ [basic.lookup.argdep]p3:
11550 // Let X be the lookup set produced by unqualified lookup (3.4.1)
11551 // and let Y be the lookup set produced by argument dependent
11552 // lookup (defined as follows). If X contains [...] then Y is
11553 // empty. Otherwise Y is the set of declarations found in the
11554 // namespaces associated with the argument types as described
11555 // below. The set of declarations found by the lookup of the name
11556 // is the union of X and Y.
11558 // Here, we compute Y and add its members to the overloaded
11560 for (auto *NS : AssociatedNamespaces) {
11561 // When considering an associated namespace, the lookup is the
11562 // same as the lookup performed when the associated namespace is
11563 // used as a qualifier (3.4.3.2) except that:
11565 // -- Any using-directives in the associated namespace are
11568 // -- Any namespace-scope friend functions declared in
11569 // associated classes are visible within their respective
11570 // namespaces even if they are not visible during an ordinary
11572 DeclContext::lookup_result R = NS->lookup(Id.getName());
11573 for (auto *D : R) {
11574 auto *Underlying = D;
11575 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
11576 Underlying = USD->getTargetDecl();
11578 if (!isa<OMPDeclareReductionDecl>(Underlying) &&
11579 !isa<OMPDeclareMapperDecl>(Underlying))
11582 if (!SemaRef.isVisible(D)) {
11583 D = findAcceptableDecl(SemaRef, D);
11586 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
11587 Underlying = USD->getTargetDecl();
11589 Lookups.emplace_back();
11590 Lookups.back().addDecl(Underlying);
11596 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
11597 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
11598 const DeclarationNameInfo &ReductionId, QualType Ty,
11599 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
11600 if (ReductionIdScopeSpec.isInvalid())
11601 return ExprError();
11602 SmallVector<UnresolvedSet<8>, 4> Lookups;
11604 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
11605 Lookup.suppressDiagnostics();
11606 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
11607 NamedDecl *D = Lookup.getRepresentativeDecl();
11609 S = S->getParent();
11610 } while (S && !S->isDeclScope(D));
11612 S = S->getParent();
11613 Lookups.emplace_back();
11614 Lookups.back().append(Lookup.begin(), Lookup.end());
11617 } else if (auto *ULE =
11618 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
11619 Lookups.push_back(UnresolvedSet<8>());
11620 Decl *PrevD = nullptr;
11621 for (NamedDecl *D : ULE->decls()) {
11623 Lookups.push_back(UnresolvedSet<8>());
11624 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
11625 Lookups.back().addDecl(DRD);
11629 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
11630 Ty->isInstantiationDependentType() ||
11631 Ty->containsUnexpandedParameterPack() ||
11632 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
11633 return !D->isInvalidDecl() &&
11634 (D->getType()->isDependentType() ||
11635 D->getType()->isInstantiationDependentType() ||
11636 D->getType()->containsUnexpandedParameterPack());
11638 UnresolvedSet<8> ResSet;
11639 for (const UnresolvedSet<8> &Set : Lookups) {
11642 ResSet.append(Set.begin(), Set.end());
11643 // The last item marks the end of all declarations at the specified scope.
11644 ResSet.addDecl(Set[Set.size() - 1]);
11646 return UnresolvedLookupExpr::Create(
11647 SemaRef.Context, /*NamingClass=*/nullptr,
11648 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
11649 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
11651 // Lookup inside the classes.
11652 // C++ [over.match.oper]p3:
11653 // For a unary operator @ with an operand of a type whose
11654 // cv-unqualified version is T1, and for a binary operator @ with
11655 // a left operand of a type whose cv-unqualified version is T1 and
11656 // a right operand of a type whose cv-unqualified version is T2,
11657 // three sets of candidate functions, designated member
11658 // candidates, non-member candidates and built-in candidates, are
11659 // constructed as follows:
11660 // -- If T1 is a complete class type or a class currently being
11661 // defined, the set of member candidates is the result of the
11662 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
11663 // the set of member candidates is empty.
11664 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
11665 Lookup.suppressDiagnostics();
11666 if (const auto *TyRec = Ty->getAs<RecordType>()) {
11667 // Complete the type if it can be completed.
11668 // If the type is neither complete nor being defined, bail out now.
11669 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
11670 TyRec->getDecl()->getDefinition()) {
11672 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
11673 if (Lookup.empty()) {
11674 Lookups.emplace_back();
11675 Lookups.back().append(Lookup.begin(), Lookup.end());
11680 if (SemaRef.getLangOpts().CPlusPlus)
11681 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
11682 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
11683 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
11684 if (!D->isInvalidDecl() &&
11685 SemaRef.Context.hasSameType(D->getType(), Ty))
11689 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
11691 if (SemaRef.getLangOpts().CPlusPlus) {
11692 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
11693 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
11694 if (!D->isInvalidDecl() &&
11695 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
11696 !Ty.isMoreQualifiedThan(D->getType()))
11700 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
11701 /*DetectVirtual=*/false);
11702 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
11703 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
11704 VD->getType().getUnqualifiedType()))) {
11705 if (SemaRef.CheckBaseClassAccess(
11706 Loc, VD->getType(), Ty, Paths.front(),
11707 /*DiagID=*/0) != Sema::AR_inaccessible) {
11708 SemaRef.BuildBasePathArray(Paths, BasePath);
11709 return SemaRef.BuildDeclRefExpr(
11710 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
11716 if (ReductionIdScopeSpec.isSet()) {
11717 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
11718 return ExprError();
11720 return ExprEmpty();
11724 /// Data for the reduction-based clauses.
11725 struct ReductionData {
11726 /// List of original reduction items.
11727 SmallVector<Expr *, 8> Vars;
11728 /// List of private copies of the reduction items.
11729 SmallVector<Expr *, 8> Privates;
11730 /// LHS expressions for the reduction_op expressions.
11731 SmallVector<Expr *, 8> LHSs;
11732 /// RHS expressions for the reduction_op expressions.
11733 SmallVector<Expr *, 8> RHSs;
11734 /// Reduction operation expression.
11735 SmallVector<Expr *, 8> ReductionOps;
11736 /// Taskgroup descriptors for the corresponding reduction items in
11737 /// in_reduction clauses.
11738 SmallVector<Expr *, 8> TaskgroupDescriptors;
11739 /// List of captures for clause.
11740 SmallVector<Decl *, 4> ExprCaptures;
11741 /// List of postupdate expressions.
11742 SmallVector<Expr *, 4> ExprPostUpdates;
11743 ReductionData() = delete;
11744 /// Reserves required memory for the reduction data.
11745 ReductionData(unsigned Size) {
11746 Vars.reserve(Size);
11747 Privates.reserve(Size);
11748 LHSs.reserve(Size);
11749 RHSs.reserve(Size);
11750 ReductionOps.reserve(Size);
11751 TaskgroupDescriptors.reserve(Size);
11752 ExprCaptures.reserve(Size);
11753 ExprPostUpdates.reserve(Size);
11755 /// Stores reduction item and reduction operation only (required for dependent
11756 /// reduction item).
11757 void push(Expr *Item, Expr *ReductionOp) {
11758 Vars.emplace_back(Item);
11759 Privates.emplace_back(nullptr);
11760 LHSs.emplace_back(nullptr);
11761 RHSs.emplace_back(nullptr);
11762 ReductionOps.emplace_back(ReductionOp);
11763 TaskgroupDescriptors.emplace_back(nullptr);
11765 /// Stores reduction data.
11766 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
11767 Expr *TaskgroupDescriptor) {
11768 Vars.emplace_back(Item);
11769 Privates.emplace_back(Private);
11770 LHSs.emplace_back(LHS);
11771 RHSs.emplace_back(RHS);
11772 ReductionOps.emplace_back(ReductionOp);
11773 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
11778 static bool checkOMPArraySectionConstantForReduction(
11779 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
11780 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
11781 const Expr *Length = OASE->getLength();
11782 if (Length == nullptr) {
11783 // For array sections of the form [1:] or [:], we would need to analyze
11784 // the lower bound...
11785 if (OASE->getColonLoc().isValid())
11788 // This is an array subscript which has implicit length 1!
11789 SingleElement = true;
11790 ArraySizes.push_back(llvm::APSInt::get(1));
11792 Expr::EvalResult Result;
11793 if (!Length->EvaluateAsInt(Result, Context))
11796 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
11797 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
11798 ArraySizes.push_back(ConstantLengthValue);
11801 // Get the base of this array section and walk up from there.
11802 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
11804 // We require length = 1 for all array sections except the right-most to
11805 // guarantee that the memory region is contiguous and has no holes in it.
11806 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
11807 Length = TempOASE->getLength();
11808 if (Length == nullptr) {
11809 // For array sections of the form [1:] or [:], we would need to analyze
11810 // the lower bound...
11811 if (OASE->getColonLoc().isValid())
11814 // This is an array subscript which has implicit length 1!
11815 ArraySizes.push_back(llvm::APSInt::get(1));
11817 Expr::EvalResult Result;
11818 if (!Length->EvaluateAsInt(Result, Context))
11821 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
11822 if (ConstantLengthValue.getSExtValue() != 1)
11825 ArraySizes.push_back(ConstantLengthValue);
11827 Base = TempOASE->getBase()->IgnoreParenImpCasts();
11830 // If we have a single element, we don't need to add the implicit lengths.
11831 if (!SingleElement) {
11832 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
11833 // Has implicit length 1!
11834 ArraySizes.push_back(llvm::APSInt::get(1));
11835 Base = TempASE->getBase()->IgnoreParenImpCasts();
11839 // This array section can be privatized as a single value or as a constant
11844 static bool actOnOMPReductionKindClause(
11845 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
11846 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11847 SourceLocation ColonLoc, SourceLocation EndLoc,
11848 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11849 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
11850 DeclarationName DN = ReductionId.getName();
11851 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
11852 BinaryOperatorKind BOK = BO_Comma;
11854 ASTContext &Context = S.Context;
11855 // OpenMP [2.14.3.6, reduction clause]
11857 // reduction-identifier is either an identifier or one of the following
11858 // operators: +, -, *, &, |, ^, && and ||
11860 // reduction-identifier is either an id-expression or one of the following
11861 // operators: +, -, *, &, |, ^, && and ||
11888 case OO_Array_Delete:
11897 case OO_GreaterEqual:
11899 case OO_MinusEqual:
11901 case OO_SlashEqual:
11902 case OO_PercentEqual:
11903 case OO_CaretEqual:
11907 case OO_GreaterGreater:
11908 case OO_LessLessEqual:
11909 case OO_GreaterGreaterEqual:
11910 case OO_EqualEqual:
11911 case OO_ExclaimEqual:
11914 case OO_MinusMinus:
11920 case OO_Conditional:
11922 case NUM_OVERLOADED_OPERATORS:
11923 llvm_unreachable("Unexpected reduction identifier");
11925 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
11926 if (II->isStr("max"))
11928 else if (II->isStr("min"))
11933 SourceRange ReductionIdRange;
11934 if (ReductionIdScopeSpec.isValid())
11935 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
11937 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
11938 ReductionIdRange.setEnd(ReductionId.getEndLoc());
11940 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
11941 bool FirstIter = true;
11942 for (Expr *RefExpr : VarList) {
11943 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
11944 // OpenMP [2.1, C/C++]
11945 // A list item is a variable or array section, subject to the restrictions
11946 // specified in Section 2.4 on page 42 and in each of the sections
11947 // describing clauses and directives for which a list appears.
11948 // OpenMP [2.14.3.3, Restrictions, p.1]
11949 // A variable that is part of another variable (as an array or
11950 // structure element) cannot appear in a private clause.
11951 if (!FirstIter && IR != ER)
11954 SourceLocation ELoc;
11955 SourceRange ERange;
11956 Expr *SimpleRefExpr = RefExpr;
11957 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
11958 /*AllowArraySection=*/true);
11960 // Try to find 'declare reduction' corresponding construct before using
11961 // builtin/overloaded operators.
11962 QualType Type = Context.DependentTy;
11963 CXXCastPath BasePath;
11964 ExprResult DeclareReductionRef = buildDeclareReductionRef(
11965 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
11966 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
11967 Expr *ReductionOp = nullptr;
11968 if (S.CurContext->isDependentContext() &&
11969 (DeclareReductionRef.isUnset() ||
11970 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
11971 ReductionOp = DeclareReductionRef.get();
11972 // It will be analyzed later.
11973 RD.push(RefExpr, ReductionOp);
11975 ValueDecl *D = Res.first;
11979 Expr *TaskgroupDescriptor = nullptr;
11981 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
11982 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
11984 Type = ASE->getType().getNonReferenceType();
11986 QualType BaseType =
11987 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
11988 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
11989 Type = ATy->getElementType();
11991 Type = BaseType->getPointeeType();
11992 Type = Type.getNonReferenceType();
11994 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
11996 auto *VD = dyn_cast<VarDecl>(D);
11998 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11999 // A variable that appears in a private clause must not have an incomplete
12000 // type or a reference type.
12001 if (S.RequireCompleteType(ELoc, D->getType(),
12002 diag::err_omp_reduction_incomplete_type))
12004 // OpenMP [2.14.3.6, reduction clause, Restrictions]
12005 // A list item that appears in a reduction clause must not be
12006 // const-qualified.
12007 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
12008 /*AcceptIfMutable*/ false, ASE || OASE))
12011 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
12012 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
12013 // If a list-item is a reference type then it must bind to the same object
12014 // for all threads of the team.
12015 if (!ASE && !OASE) {
12017 VarDecl *VDDef = VD->getDefinition();
12018 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
12019 DSARefChecker Check(Stack);
12020 if (Check.Visit(VDDef->getInit())) {
12021 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
12022 << getOpenMPClauseName(ClauseKind) << ERange;
12023 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
12029 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
12031 // Variables with the predetermined data-sharing attributes may not be
12032 // listed in data-sharing attributes clauses, except for the cases
12033 // listed below. For these exceptions only, listing a predetermined
12034 // variable in a data-sharing attribute clause is allowed and overrides
12035 // the variable's predetermined data-sharing attributes.
12036 // OpenMP [2.14.3.6, Restrictions, p.3]
12037 // Any number of reduction clauses can be specified on the directive,
12038 // but a list item can appear only once in the reduction clauses for that
12040 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
12041 if (DVar.CKind == OMPC_reduction) {
12042 S.Diag(ELoc, diag::err_omp_once_referenced)
12043 << getOpenMPClauseName(ClauseKind);
12045 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
12048 if (DVar.CKind != OMPC_unknown) {
12049 S.Diag(ELoc, diag::err_omp_wrong_dsa)
12050 << getOpenMPClauseName(DVar.CKind)
12051 << getOpenMPClauseName(OMPC_reduction);
12052 reportOriginalDsa(S, Stack, D, DVar);
12056 // OpenMP [2.14.3.6, Restrictions, p.1]
12057 // A list item that appears in a reduction clause of a worksharing
12058 // construct must be shared in the parallel regions to which any of the
12059 // worksharing regions arising from the worksharing construct bind.
12060 if (isOpenMPWorksharingDirective(CurrDir) &&
12061 !isOpenMPParallelDirective(CurrDir) &&
12062 !isOpenMPTeamsDirective(CurrDir)) {
12063 DVar = Stack->getImplicitDSA(D, true);
12064 if (DVar.CKind != OMPC_shared) {
12065 S.Diag(ELoc, diag::err_omp_required_access)
12066 << getOpenMPClauseName(OMPC_reduction)
12067 << getOpenMPClauseName(OMPC_shared);
12068 reportOriginalDsa(S, Stack, D, DVar);
12074 // Try to find 'declare reduction' corresponding construct before using
12075 // builtin/overloaded operators.
12076 CXXCastPath BasePath;
12077 ExprResult DeclareReductionRef = buildDeclareReductionRef(
12078 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
12079 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
12080 if (DeclareReductionRef.isInvalid())
12082 if (S.CurContext->isDependentContext() &&
12083 (DeclareReductionRef.isUnset() ||
12084 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
12085 RD.push(RefExpr, DeclareReductionRef.get());
12088 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
12089 // Not allowed reduction identifier is found.
12090 S.Diag(ReductionId.getBeginLoc(),
12091 diag::err_omp_unknown_reduction_identifier)
12092 << Type << ReductionIdRange;
12096 // OpenMP [2.14.3.6, reduction clause, Restrictions]
12097 // The type of a list item that appears in a reduction clause must be valid
12098 // for the reduction-identifier. For a max or min reduction in C, the type
12099 // of the list item must be an allowed arithmetic data type: char, int,
12100 // float, double, or _Bool, possibly modified with long, short, signed, or
12101 // unsigned. For a max or min reduction in C++, the type of the list item
12102 // must be an allowed arithmetic data type: char, wchar_t, int, float,
12103 // double, or bool, possibly modified with long, short, signed, or unsigned.
12104 if (DeclareReductionRef.isUnset()) {
12105 if ((BOK == BO_GT || BOK == BO_LT) &&
12106 !(Type->isScalarType() ||
12107 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
12108 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
12109 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
12110 if (!ASE && !OASE) {
12111 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12112 VarDecl::DeclarationOnly;
12113 S.Diag(D->getLocation(),
12114 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12119 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
12120 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
12121 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
12122 << getOpenMPClauseName(ClauseKind);
12123 if (!ASE && !OASE) {
12124 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12125 VarDecl::DeclarationOnly;
12126 S.Diag(D->getLocation(),
12127 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12134 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
12135 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
12136 D->hasAttrs() ? &D->getAttrs() : nullptr);
12137 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
12138 D->hasAttrs() ? &D->getAttrs() : nullptr);
12139 QualType PrivateTy = Type;
12141 // Try if we can determine constant lengths for all array sections and avoid
12143 bool ConstantLengthOASE = false;
12145 bool SingleElement;
12146 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
12147 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
12148 Context, OASE, SingleElement, ArraySizes);
12150 // If we don't have a single element, we must emit a constant array type.
12151 if (ConstantLengthOASE && !SingleElement) {
12152 for (llvm::APSInt &Size : ArraySizes)
12153 PrivateTy = Context.getConstantArrayType(
12154 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
12158 if ((OASE && !ConstantLengthOASE) ||
12160 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
12161 if (!Context.getTargetInfo().isVLASupported()) {
12162 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
12163 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
12164 S.Diag(ELoc, diag::note_vla_unsupported);
12166 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
12167 S.targetDiag(ELoc, diag::note_vla_unsupported);
12171 // For arrays/array sections only:
12172 // Create pseudo array type for private copy. The size for this array will
12173 // be generated during codegen.
12174 // For array subscripts or single variables Private Ty is the same as Type
12175 // (type of the variable or single array element).
12176 PrivateTy = Context.getVariableArrayType(
12178 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
12179 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
12180 } else if (!ASE && !OASE &&
12181 Context.getAsArrayType(D->getType().getNonReferenceType())) {
12182 PrivateTy = D->getType().getNonReferenceType();
12185 VarDecl *PrivateVD =
12186 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
12187 D->hasAttrs() ? &D->getAttrs() : nullptr,
12188 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
12189 // Add initializer for private variable.
12190 Expr *Init = nullptr;
12191 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
12192 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
12193 if (DeclareReductionRef.isUsable()) {
12194 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
12195 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
12196 if (DRD->getInitializer()) {
12198 RHSVD->setInit(DRDRef);
12199 RHSVD->setInitStyle(VarDecl::CallInit);
12207 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
12208 if (Type->isScalarType() || Type->isAnyComplexType())
12209 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
12213 if (Type->isScalarType() || Type->isAnyComplexType()) {
12214 // '*' and '&&' reduction ops - initializer is '1'.
12215 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
12219 // '&' reduction op - initializer is '~0'.
12220 QualType OrigType = Type;
12221 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
12222 Type = ComplexTy->getElementType();
12223 if (Type->isRealFloatingType()) {
12224 llvm::APFloat InitValue =
12225 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
12227 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
12229 } else if (Type->isScalarType()) {
12230 uint64_t Size = Context.getTypeSize(Type);
12231 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
12232 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
12233 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
12235 if (Init && OrigType->isAnyComplexType()) {
12236 // Init = 0xFFFF + 0xFFFFi;
12237 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
12238 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
12245 // 'min' reduction op - initializer is 'Largest representable number in
12246 // the reduction list item type'.
12247 // 'max' reduction op - initializer is 'Least representable number in
12248 // the reduction list item type'.
12249 if (Type->isIntegerType() || Type->isPointerType()) {
12250 bool IsSigned = Type->hasSignedIntegerRepresentation();
12251 uint64_t Size = Context.getTypeSize(Type);
12253 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
12254 llvm::APInt InitValue =
12255 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
12256 : llvm::APInt::getMinValue(Size)
12257 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
12258 : llvm::APInt::getMaxValue(Size);
12259 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
12260 if (Type->isPointerType()) {
12261 // Cast to pointer type.
12262 ExprResult CastExpr = S.BuildCStyleCastExpr(
12263 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
12264 if (CastExpr.isInvalid())
12266 Init = CastExpr.get();
12268 } else if (Type->isRealFloatingType()) {
12269 llvm::APFloat InitValue = llvm::APFloat::getLargest(
12270 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
12271 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
12300 llvm_unreachable("Unexpected reduction operation");
12303 if (Init && DeclareReductionRef.isUnset())
12304 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
12306 S.ActOnUninitializedDecl(RHSVD);
12307 if (RHSVD->isInvalidDecl())
12309 if (!RHSVD->hasInit() &&
12310 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) {
12311 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
12312 << Type << ReductionIdRange;
12313 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12314 VarDecl::DeclarationOnly;
12315 S.Diag(D->getLocation(),
12316 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12320 // Store initializer for single element in private copy. Will be used during
12322 PrivateVD->setInit(RHSVD->getInit());
12323 PrivateVD->setInitStyle(RHSVD->getInitStyle());
12324 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
12325 ExprResult ReductionOp;
12326 if (DeclareReductionRef.isUsable()) {
12327 QualType RedTy = DeclareReductionRef.get()->getType();
12328 QualType PtrRedTy = Context.getPointerType(RedTy);
12329 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
12330 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
12331 if (!BasePath.empty()) {
12332 LHS = S.DefaultLvalueConversion(LHS.get());
12333 RHS = S.DefaultLvalueConversion(RHS.get());
12334 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
12335 CK_UncheckedDerivedToBase, LHS.get(),
12336 &BasePath, LHS.get()->getValueKind());
12337 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
12338 CK_UncheckedDerivedToBase, RHS.get(),
12339 &BasePath, RHS.get()->getValueKind());
12341 FunctionProtoType::ExtProtoInfo EPI;
12342 QualType Params[] = {PtrRedTy, PtrRedTy};
12343 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
12344 auto *OVE = new (Context) OpaqueValueExpr(
12345 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
12346 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
12347 Expr *Args[] = {LHS.get(), RHS.get()};
12349 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
12351 ReductionOp = S.BuildBinOp(
12352 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
12353 if (ReductionOp.isUsable()) {
12354 if (BOK != BO_LT && BOK != BO_GT) {
12356 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
12357 BO_Assign, LHSDRE, ReductionOp.get());
12359 auto *ConditionalOp = new (Context)
12360 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
12361 Type, VK_LValue, OK_Ordinary);
12363 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
12364 BO_Assign, LHSDRE, ConditionalOp);
12366 if (ReductionOp.isUsable())
12367 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
12368 /*DiscardedValue*/ false);
12370 if (!ReductionOp.isUsable())
12374 // OpenMP [2.15.4.6, Restrictions, p.2]
12375 // A list item that appears in an in_reduction clause of a task construct
12376 // must appear in a task_reduction clause of a construct associated with a
12377 // taskgroup region that includes the participating task in its taskgroup
12378 // set. The construct associated with the innermost region that meets this
12379 // condition must specify the same reduction-identifier as the in_reduction
12381 if (ClauseKind == OMPC_in_reduction) {
12382 SourceRange ParentSR;
12383 BinaryOperatorKind ParentBOK;
12384 const Expr *ParentReductionOp;
12385 Expr *ParentBOKTD, *ParentReductionOpTD;
12386 DSAStackTy::DSAVarData ParentBOKDSA =
12387 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
12389 DSAStackTy::DSAVarData ParentReductionOpDSA =
12390 Stack->getTopMostTaskgroupReductionData(
12391 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
12392 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
12393 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
12394 if (!IsParentBOK && !IsParentReductionOp) {
12395 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
12398 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
12399 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
12400 IsParentReductionOp) {
12401 bool EmitError = true;
12402 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
12403 llvm::FoldingSetNodeID RedId, ParentRedId;
12404 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
12405 DeclareReductionRef.get()->Profile(RedId, Context,
12406 /*Canonical=*/true);
12407 EmitError = RedId != ParentRedId;
12410 S.Diag(ReductionId.getBeginLoc(),
12411 diag::err_omp_reduction_identifier_mismatch)
12412 << ReductionIdRange << RefExpr->getSourceRange();
12413 S.Diag(ParentSR.getBegin(),
12414 diag::note_omp_previous_reduction_identifier)
12416 << (IsParentBOK ? ParentBOKDSA.RefExpr
12417 : ParentReductionOpDSA.RefExpr)
12418 ->getSourceRange();
12422 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
12423 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
12426 DeclRefExpr *Ref = nullptr;
12427 Expr *VarsExpr = RefExpr->IgnoreParens();
12428 if (!VD && !S.CurContext->isDependentContext()) {
12430 TransformExprToCaptures RebuildToCapture(S, D);
12432 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
12433 Ref = RebuildToCapture.getCapturedExpr();
12435 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
12437 if (!S.isOpenMPCapturedDecl(D)) {
12438 RD.ExprCaptures.emplace_back(Ref->getDecl());
12439 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
12440 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
12441 if (!RefRes.isUsable())
12443 ExprResult PostUpdateRes =
12444 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
12446 if (!PostUpdateRes.isUsable())
12448 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
12449 Stack->getCurrentDirective() == OMPD_taskgroup) {
12450 S.Diag(RefExpr->getExprLoc(),
12451 diag::err_omp_reduction_non_addressable_expression)
12452 << RefExpr->getSourceRange();
12455 RD.ExprPostUpdates.emplace_back(
12456 S.IgnoredValueConversions(PostUpdateRes.get()).get());
12460 // All reduction items are still marked as reduction (to do not increase
12461 // code base size).
12462 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
12463 if (CurrDir == OMPD_taskgroup) {
12464 if (DeclareReductionRef.isUsable())
12465 Stack->addTaskgroupReductionData(D, ReductionIdRange,
12466 DeclareReductionRef.get());
12468 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
12470 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
12471 TaskgroupDescriptor);
12473 return RD.Vars.empty();
12476 OMPClause *Sema::ActOnOpenMPReductionClause(
12477 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
12478 SourceLocation ColonLoc, SourceLocation EndLoc,
12479 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
12480 ArrayRef<Expr *> UnresolvedReductions) {
12481 ReductionData RD(VarList.size());
12482 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
12483 StartLoc, LParenLoc, ColonLoc, EndLoc,
12484 ReductionIdScopeSpec, ReductionId,
12485 UnresolvedReductions, RD))
12488 return OMPReductionClause::Create(
12489 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
12490 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
12491 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
12492 buildPreInits(Context, RD.ExprCaptures),
12493 buildPostUpdate(*this, RD.ExprPostUpdates));
12496 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
12497 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
12498 SourceLocation ColonLoc, SourceLocation EndLoc,
12499 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
12500 ArrayRef<Expr *> UnresolvedReductions) {
12501 ReductionData RD(VarList.size());
12502 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
12503 StartLoc, LParenLoc, ColonLoc, EndLoc,
12504 ReductionIdScopeSpec, ReductionId,
12505 UnresolvedReductions, RD))
12508 return OMPTaskReductionClause::Create(
12509 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
12510 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
12511 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
12512 buildPreInits(Context, RD.ExprCaptures),
12513 buildPostUpdate(*this, RD.ExprPostUpdates));
12516 OMPClause *Sema::ActOnOpenMPInReductionClause(
12517 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
12518 SourceLocation ColonLoc, SourceLocation EndLoc,
12519 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
12520 ArrayRef<Expr *> UnresolvedReductions) {
12521 ReductionData RD(VarList.size());
12522 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
12523 StartLoc, LParenLoc, ColonLoc, EndLoc,
12524 ReductionIdScopeSpec, ReductionId,
12525 UnresolvedReductions, RD))
12528 return OMPInReductionClause::Create(
12529 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
12530 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
12531 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
12532 buildPreInits(Context, RD.ExprCaptures),
12533 buildPostUpdate(*this, RD.ExprPostUpdates));
12536 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
12537 SourceLocation LinLoc) {
12538 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
12539 LinKind == OMPC_LINEAR_unknown) {
12540 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
12546 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
12547 OpenMPLinearClauseKind LinKind,
12549 const auto *VD = dyn_cast_or_null<VarDecl>(D);
12550 // A variable must not have an incomplete type or a reference type.
12551 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
12553 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
12554 !Type->isReferenceType()) {
12555 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
12556 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
12559 Type = Type.getNonReferenceType();
12561 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
12562 // A variable that is privatized must not have a const-qualified type
12563 // unless it is of class type with a mutable member. This restriction does
12564 // not apply to the firstprivate clause.
12565 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
12568 // A list item must be of integral or pointer type.
12569 Type = Type.getUnqualifiedType().getCanonicalType();
12570 const auto *Ty = Type.getTypePtrOrNull();
12571 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
12572 !Ty->isPointerType())) {
12573 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
12577 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
12578 Diag(D->getLocation(),
12579 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12587 OMPClause *Sema::ActOnOpenMPLinearClause(
12588 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
12589 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
12590 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
12591 SmallVector<Expr *, 8> Vars;
12592 SmallVector<Expr *, 8> Privates;
12593 SmallVector<Expr *, 8> Inits;
12594 SmallVector<Decl *, 4> ExprCaptures;
12595 SmallVector<Expr *, 4> ExprPostUpdates;
12596 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
12597 LinKind = OMPC_LINEAR_val;
12598 for (Expr *RefExpr : VarList) {
12599 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12600 SourceLocation ELoc;
12601 SourceRange ERange;
12602 Expr *SimpleRefExpr = RefExpr;
12603 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12605 // It will be analyzed later.
12606 Vars.push_back(RefExpr);
12607 Privates.push_back(nullptr);
12608 Inits.push_back(nullptr);
12610 ValueDecl *D = Res.first;
12614 QualType Type = D->getType();
12615 auto *VD = dyn_cast<VarDecl>(D);
12617 // OpenMP [2.14.3.7, linear clause]
12618 // A list-item cannot appear in more than one linear clause.
12619 // A list-item that appears in a linear clause cannot appear in any
12620 // other data-sharing attribute clause.
12621 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
12622 if (DVar.RefExpr) {
12623 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
12624 << getOpenMPClauseName(OMPC_linear);
12625 reportOriginalDsa(*this, DSAStack, D, DVar);
12629 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
12631 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
12633 // Build private copy of original var.
12635 buildVarDecl(*this, ELoc, Type, D->getName(),
12636 D->hasAttrs() ? &D->getAttrs() : nullptr,
12637 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
12638 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
12639 // Build var to save initial value.
12640 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
12642 DeclRefExpr *Ref = nullptr;
12643 if (!VD && !CurContext->isDependentContext()) {
12644 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
12645 if (!isOpenMPCapturedDecl(D)) {
12646 ExprCaptures.push_back(Ref->getDecl());
12647 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
12648 ExprResult RefRes = DefaultLvalueConversion(Ref);
12649 if (!RefRes.isUsable())
12651 ExprResult PostUpdateRes =
12652 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
12653 SimpleRefExpr, RefRes.get());
12654 if (!PostUpdateRes.isUsable())
12656 ExprPostUpdates.push_back(
12657 IgnoredValueConversions(PostUpdateRes.get()).get());
12661 if (LinKind == OMPC_LINEAR_uval)
12662 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
12664 InitExpr = VD ? SimpleRefExpr : Ref;
12665 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
12666 /*DirectInit=*/false);
12667 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
12669 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
12670 Vars.push_back((VD || CurContext->isDependentContext())
12671 ? RefExpr->IgnoreParens()
12673 Privates.push_back(PrivateRef);
12674 Inits.push_back(InitRef);
12680 Expr *StepExpr = Step;
12681 Expr *CalcStepExpr = nullptr;
12682 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
12683 !Step->isInstantiationDependent() &&
12684 !Step->containsUnexpandedParameterPack()) {
12685 SourceLocation StepLoc = Step->getBeginLoc();
12686 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
12687 if (Val.isInvalid())
12689 StepExpr = Val.get();
12691 // Build var to save the step value.
12693 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
12694 ExprResult SaveRef =
12695 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
12696 ExprResult CalcStep =
12697 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
12698 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
12700 // Warn about zero linear step (it would be probably better specified as
12701 // making corresponding variables 'const').
12702 llvm::APSInt Result;
12703 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
12704 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
12705 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
12706 << (Vars.size() > 1);
12707 if (!IsConstant && CalcStep.isUsable()) {
12708 // Calculate the step beforehand instead of doing this on each iteration.
12709 // (This is not used if the number of iterations may be kfold-ed).
12710 CalcStepExpr = CalcStep.get();
12714 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
12715 ColonLoc, EndLoc, Vars, Privates, Inits,
12716 StepExpr, CalcStepExpr,
12717 buildPreInits(Context, ExprCaptures),
12718 buildPostUpdate(*this, ExprPostUpdates));
12721 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
12722 Expr *NumIterations, Sema &SemaRef,
12723 Scope *S, DSAStackTy *Stack) {
12724 // Walk the vars and build update/final expressions for the CodeGen.
12725 SmallVector<Expr *, 8> Updates;
12726 SmallVector<Expr *, 8> Finals;
12727 Expr *Step = Clause.getStep();
12728 Expr *CalcStep = Clause.getCalcStep();
12729 // OpenMP [2.14.3.7, linear clause]
12730 // If linear-step is not specified it is assumed to be 1.
12732 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
12734 Step = cast<BinaryOperator>(CalcStep)->getLHS();
12735 bool HasErrors = false;
12736 auto CurInit = Clause.inits().begin();
12737 auto CurPrivate = Clause.privates().begin();
12738 OpenMPLinearClauseKind LinKind = Clause.getModifier();
12739 for (Expr *RefExpr : Clause.varlists()) {
12740 SourceLocation ELoc;
12741 SourceRange ERange;
12742 Expr *SimpleRefExpr = RefExpr;
12743 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
12744 ValueDecl *D = Res.first;
12745 if (Res.second || !D) {
12746 Updates.push_back(nullptr);
12747 Finals.push_back(nullptr);
12751 auto &&Info = Stack->isLoopControlVariable(D);
12752 // OpenMP [2.15.11, distribute simd Construct]
12753 // A list item may not appear in a linear clause, unless it is the loop
12754 // iteration variable.
12755 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
12756 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
12758 diag::err_omp_linear_distribute_var_non_loop_iteration);
12759 Updates.push_back(nullptr);
12760 Finals.push_back(nullptr);
12764 Expr *InitExpr = *CurInit;
12766 // Build privatized reference to the current linear var.
12767 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
12769 if (LinKind == OMPC_LINEAR_uval)
12770 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
12773 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
12774 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
12775 /*RefersToCapture=*/true);
12777 // Build update: Var = InitExpr + IV * Step
12781 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
12782 InitExpr, IV, Step, /* Subtract */ false);
12784 Update = *CurPrivate;
12785 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
12786 /*DiscardedValue*/ false);
12788 // Build final: Var = InitExpr + NumIterations * Step
12792 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
12793 InitExpr, NumIterations, Step, /*Subtract=*/false);
12795 Final = *CurPrivate;
12796 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
12797 /*DiscardedValue*/ false);
12799 if (!Update.isUsable() || !Final.isUsable()) {
12800 Updates.push_back(nullptr);
12801 Finals.push_back(nullptr);
12804 Updates.push_back(Update.get());
12805 Finals.push_back(Final.get());
12810 Clause.setUpdates(Updates);
12811 Clause.setFinals(Finals);
12815 OMPClause *Sema::ActOnOpenMPAlignedClause(
12816 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
12817 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
12818 SmallVector<Expr *, 8> Vars;
12819 for (Expr *RefExpr : VarList) {
12820 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12821 SourceLocation ELoc;
12822 SourceRange ERange;
12823 Expr *SimpleRefExpr = RefExpr;
12824 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12826 // It will be analyzed later.
12827 Vars.push_back(RefExpr);
12829 ValueDecl *D = Res.first;
12833 QualType QType = D->getType();
12834 auto *VD = dyn_cast<VarDecl>(D);
12836 // OpenMP [2.8.1, simd construct, Restrictions]
12837 // The type of list items appearing in the aligned clause must be
12838 // array, pointer, reference to array, or reference to pointer.
12839 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
12840 const Type *Ty = QType.getTypePtrOrNull();
12841 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
12842 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
12843 << QType << getLangOpts().CPlusPlus << ERange;
12846 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
12847 Diag(D->getLocation(),
12848 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12853 // OpenMP [2.8.1, simd construct, Restrictions]
12854 // A list-item cannot appear in more than one aligned clause.
12855 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
12856 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
12857 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
12858 << getOpenMPClauseName(OMPC_aligned);
12862 DeclRefExpr *Ref = nullptr;
12863 if (!VD && isOpenMPCapturedDecl(D))
12864 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12865 Vars.push_back(DefaultFunctionArrayConversion(
12866 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
12870 // OpenMP [2.8.1, simd construct, Description]
12871 // The parameter of the aligned clause, alignment, must be a constant
12872 // positive integer expression.
12873 // If no optional parameter is specified, implementation-defined default
12874 // alignments for SIMD instructions on the target platforms are assumed.
12875 if (Alignment != nullptr) {
12876 ExprResult AlignResult =
12877 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
12878 if (AlignResult.isInvalid())
12880 Alignment = AlignResult.get();
12885 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
12886 EndLoc, Vars, Alignment);
12889 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
12890 SourceLocation StartLoc,
12891 SourceLocation LParenLoc,
12892 SourceLocation EndLoc) {
12893 SmallVector<Expr *, 8> Vars;
12894 SmallVector<Expr *, 8> SrcExprs;
12895 SmallVector<Expr *, 8> DstExprs;
12896 SmallVector<Expr *, 8> AssignmentOps;
12897 for (Expr *RefExpr : VarList) {
12898 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
12899 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12900 // It will be analyzed later.
12901 Vars.push_back(RefExpr);
12902 SrcExprs.push_back(nullptr);
12903 DstExprs.push_back(nullptr);
12904 AssignmentOps.push_back(nullptr);
12908 SourceLocation ELoc = RefExpr->getExprLoc();
12909 // OpenMP [2.1, C/C++]
12910 // A list item is a variable name.
12911 // OpenMP [2.14.4.1, Restrictions, p.1]
12912 // A list item that appears in a copyin clause must be threadprivate.
12913 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
12914 if (!DE || !isa<VarDecl>(DE->getDecl())) {
12915 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
12916 << 0 << RefExpr->getSourceRange();
12920 Decl *D = DE->getDecl();
12921 auto *VD = cast<VarDecl>(D);
12923 QualType Type = VD->getType();
12924 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
12925 // It will be analyzed later.
12926 Vars.push_back(DE);
12927 SrcExprs.push_back(nullptr);
12928 DstExprs.push_back(nullptr);
12929 AssignmentOps.push_back(nullptr);
12933 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
12934 // A list item that appears in a copyin clause must be threadprivate.
12935 if (!DSAStack->isThreadPrivate(VD)) {
12936 Diag(ELoc, diag::err_omp_required_access)
12937 << getOpenMPClauseName(OMPC_copyin)
12938 << getOpenMPDirectiveName(OMPD_threadprivate);
12942 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12943 // A variable of class type (or array thereof) that appears in a
12944 // copyin clause requires an accessible, unambiguous copy assignment
12945 // operator for the class type.
12946 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
12948 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
12949 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
12950 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
12951 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
12953 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
12954 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
12955 DeclRefExpr *PseudoDstExpr =
12956 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
12957 // For arrays generate assignment operation for single element and replace
12958 // it by the original array element in CodeGen.
12959 ExprResult AssignmentOp =
12960 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
12962 if (AssignmentOp.isInvalid())
12964 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
12965 /*DiscardedValue*/ false);
12966 if (AssignmentOp.isInvalid())
12969 DSAStack->addDSA(VD, DE, OMPC_copyin);
12970 Vars.push_back(DE);
12971 SrcExprs.push_back(PseudoSrcExpr);
12972 DstExprs.push_back(PseudoDstExpr);
12973 AssignmentOps.push_back(AssignmentOp.get());
12979 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
12980 SrcExprs, DstExprs, AssignmentOps);
12983 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
12984 SourceLocation StartLoc,
12985 SourceLocation LParenLoc,
12986 SourceLocation EndLoc) {
12987 SmallVector<Expr *, 8> Vars;
12988 SmallVector<Expr *, 8> SrcExprs;
12989 SmallVector<Expr *, 8> DstExprs;
12990 SmallVector<Expr *, 8> AssignmentOps;
12991 for (Expr *RefExpr : VarList) {
12992 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12993 SourceLocation ELoc;
12994 SourceRange ERange;
12995 Expr *SimpleRefExpr = RefExpr;
12996 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12998 // It will be analyzed later.
12999 Vars.push_back(RefExpr);
13000 SrcExprs.push_back(nullptr);
13001 DstExprs.push_back(nullptr);
13002 AssignmentOps.push_back(nullptr);
13004 ValueDecl *D = Res.first;
13008 QualType Type = D->getType();
13009 auto *VD = dyn_cast<VarDecl>(D);
13011 // OpenMP [2.14.4.2, Restrictions, p.2]
13012 // A list item that appears in a copyprivate clause may not appear in a
13013 // private or firstprivate clause on the single construct.
13014 if (!VD || !DSAStack->isThreadPrivate(VD)) {
13015 DSAStackTy::DSAVarData DVar =
13016 DSAStack->getTopDSA(D, /*FromParent=*/false);
13017 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
13019 Diag(ELoc, diag::err_omp_wrong_dsa)
13020 << getOpenMPClauseName(DVar.CKind)
13021 << getOpenMPClauseName(OMPC_copyprivate);
13022 reportOriginalDsa(*this, DSAStack, D, DVar);
13026 // OpenMP [2.11.4.2, Restrictions, p.1]
13027 // All list items that appear in a copyprivate clause must be either
13028 // threadprivate or private in the enclosing context.
13029 if (DVar.CKind == OMPC_unknown) {
13030 DVar = DSAStack->getImplicitDSA(D, false);
13031 if (DVar.CKind == OMPC_shared) {
13032 Diag(ELoc, diag::err_omp_required_access)
13033 << getOpenMPClauseName(OMPC_copyprivate)
13034 << "threadprivate or private in the enclosing context";
13035 reportOriginalDsa(*this, DSAStack, D, DVar);
13041 // Variably modified types are not supported.
13042 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
13043 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
13044 << getOpenMPClauseName(OMPC_copyprivate) << Type
13045 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
13048 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13049 Diag(D->getLocation(),
13050 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13055 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
13056 // A variable of class type (or array thereof) that appears in a
13057 // copyin clause requires an accessible, unambiguous copy assignment
13058 // operator for the class type.
13059 Type = Context.getBaseElementType(Type.getNonReferenceType())
13060 .getUnqualifiedType();
13062 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
13063 D->hasAttrs() ? &D->getAttrs() : nullptr);
13064 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
13066 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
13067 D->hasAttrs() ? &D->getAttrs() : nullptr);
13068 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
13069 ExprResult AssignmentOp = BuildBinOp(
13070 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
13071 if (AssignmentOp.isInvalid())
13074 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
13075 if (AssignmentOp.isInvalid())
13078 // No need to mark vars as copyprivate, they are already threadprivate or
13079 // implicitly private.
13080 assert(VD || isOpenMPCapturedDecl(D));
13082 VD ? RefExpr->IgnoreParens()
13083 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
13084 SrcExprs.push_back(PseudoSrcExpr);
13085 DstExprs.push_back(PseudoDstExpr);
13086 AssignmentOps.push_back(AssignmentOp.get());
13092 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13093 Vars, SrcExprs, DstExprs, AssignmentOps);
13096 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
13097 SourceLocation StartLoc,
13098 SourceLocation LParenLoc,
13099 SourceLocation EndLoc) {
13100 if (VarList.empty())
13103 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
13107 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
13108 SourceLocation DepLoc, SourceLocation ColonLoc,
13109 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
13110 SourceLocation LParenLoc, SourceLocation EndLoc) {
13111 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
13112 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
13113 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
13114 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
13117 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
13118 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
13119 DepKind == OMPC_DEPEND_sink)) {
13120 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
13121 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
13122 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
13123 /*Last=*/OMPC_DEPEND_unknown, Except)
13124 << getOpenMPClauseName(OMPC_depend);
13127 SmallVector<Expr *, 8> Vars;
13128 DSAStackTy::OperatorOffsetTy OpsOffs;
13129 llvm::APSInt DepCounter(/*BitWidth=*/32);
13130 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
13131 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
13132 if (const Expr *OrderedCountExpr =
13133 DSAStack->getParentOrderedRegionParam().first) {
13134 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
13135 TotalDepCount.setIsUnsigned(/*Val=*/true);
13138 for (Expr *RefExpr : VarList) {
13139 assert(RefExpr && "NULL expr in OpenMP shared clause.");
13140 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
13141 // It will be analyzed later.
13142 Vars.push_back(RefExpr);
13146 SourceLocation ELoc = RefExpr->getExprLoc();
13147 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
13148 if (DepKind == OMPC_DEPEND_sink) {
13149 if (DSAStack->getParentOrderedRegionParam().first &&
13150 DepCounter >= TotalDepCount) {
13151 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
13155 // OpenMP [2.13.9, Summary]
13156 // depend(dependence-type : vec), where dependence-type is:
13157 // 'sink' and where vec is the iteration vector, which has the form:
13158 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
13159 // where n is the value specified by the ordered clause in the loop
13160 // directive, xi denotes the loop iteration variable of the i-th nested
13161 // loop associated with the loop directive, and di is a constant
13162 // non-negative integer.
13163 if (CurContext->isDependentContext()) {
13164 // It will be analyzed later.
13165 Vars.push_back(RefExpr);
13168 SimpleExpr = SimpleExpr->IgnoreImplicit();
13169 OverloadedOperatorKind OOK = OO_None;
13170 SourceLocation OOLoc;
13171 Expr *LHS = SimpleExpr;
13172 Expr *RHS = nullptr;
13173 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
13174 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
13175 OOLoc = BO->getOperatorLoc();
13176 LHS = BO->getLHS()->IgnoreParenImpCasts();
13177 RHS = BO->getRHS()->IgnoreParenImpCasts();
13178 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
13179 OOK = OCE->getOperator();
13180 OOLoc = OCE->getOperatorLoc();
13181 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
13182 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
13183 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
13184 OOK = MCE->getMethodDecl()
13187 .getCXXOverloadedOperator();
13188 OOLoc = MCE->getCallee()->getExprLoc();
13189 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
13190 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
13192 SourceLocation ELoc;
13193 SourceRange ERange;
13194 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
13196 // It will be analyzed later.
13197 Vars.push_back(RefExpr);
13199 ValueDecl *D = Res.first;
13203 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
13204 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
13208 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
13209 RHS, OMPC_depend, /*StrictlyPositive=*/false);
13210 if (RHSRes.isInvalid())
13213 if (!CurContext->isDependentContext() &&
13214 DSAStack->getParentOrderedRegionParam().first &&
13215 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
13216 const ValueDecl *VD =
13217 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
13219 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
13222 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
13225 OpsOffs.emplace_back(RHS, OOK);
13227 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
13228 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
13230 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
13231 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
13232 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
13233 << RefExpr->getSourceRange();
13236 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
13237 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
13239 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
13240 getDiagnostics().setSuppressAllDiagnostics(Suppress);
13241 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
13242 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
13243 << RefExpr->getSourceRange();
13247 Vars.push_back(RefExpr->IgnoreParenImpCasts());
13250 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
13251 TotalDepCount > VarList.size() &&
13252 DSAStack->getParentOrderedRegionParam().first &&
13253 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
13254 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
13255 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
13257 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
13261 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13262 DepKind, DepLoc, ColonLoc, Vars,
13263 TotalDepCount.getZExtValue());
13264 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
13265 DSAStack->isParentOrderedRegion())
13266 DSAStack->addDoacrossDependClause(C, OpsOffs);
13270 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
13271 SourceLocation LParenLoc,
13272 SourceLocation EndLoc) {
13273 Expr *ValExpr = Device;
13274 Stmt *HelperValStmt = nullptr;
13276 // OpenMP [2.9.1, Restrictions]
13277 // The device expression must evaluate to a non-negative integer value.
13278 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
13279 /*StrictlyPositive=*/false))
13282 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
13283 OpenMPDirectiveKind CaptureRegion =
13284 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
13285 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
13286 ValExpr = MakeFullExpr(ValExpr).get();
13287 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
13288 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13289 HelperValStmt = buildPreInits(Context, Captures);
13292 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
13293 StartLoc, LParenLoc, EndLoc);
13296 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
13297 DSAStackTy *Stack, QualType QTy,
13298 bool FullCheck = true) {
13300 if (QTy->isIncompleteType(&ND)) {
13301 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
13304 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
13305 !QTy.isTrivialType(SemaRef.Context))
13306 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
13310 /// Return true if it can be proven that the provided array expression
13311 /// (array section or array subscript) does NOT specify the whole size of the
13312 /// array whose base type is \a BaseQTy.
13313 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
13315 QualType BaseQTy) {
13316 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
13318 // If this is an array subscript, it refers to the whole size if the size of
13319 // the dimension is constant and equals 1. Also, an array section assumes the
13320 // format of an array subscript if no colon is used.
13321 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
13322 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
13323 return ATy->getSize().getSExtValue() != 1;
13324 // Size can't be evaluated statically.
13328 assert(OASE && "Expecting array section if not an array subscript.");
13329 const Expr *LowerBound = OASE->getLowerBound();
13330 const Expr *Length = OASE->getLength();
13332 // If there is a lower bound that does not evaluates to zero, we are not
13333 // covering the whole dimension.
13335 Expr::EvalResult Result;
13336 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
13337 return false; // Can't get the integer value as a constant.
13339 llvm::APSInt ConstLowerBound = Result.Val.getInt();
13340 if (ConstLowerBound.getSExtValue())
13344 // If we don't have a length we covering the whole dimension.
13348 // If the base is a pointer, we don't have a way to get the size of the
13350 if (BaseQTy->isPointerType())
13353 // We can only check if the length is the same as the size of the dimension
13354 // if we have a constant array.
13355 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
13359 Expr::EvalResult Result;
13360 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
13361 return false; // Can't get the integer value as a constant.
13363 llvm::APSInt ConstLength = Result.Val.getInt();
13364 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
13367 // Return true if it can be proven that the provided array expression (array
13368 // section or array subscript) does NOT specify a single element of the array
13369 // whose base type is \a BaseQTy.
13370 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
13372 QualType BaseQTy) {
13373 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
13375 // An array subscript always refer to a single element. Also, an array section
13376 // assumes the format of an array subscript if no colon is used.
13377 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
13380 assert(OASE && "Expecting array section if not an array subscript.");
13381 const Expr *Length = OASE->getLength();
13383 // If we don't have a length we have to check if the array has unitary size
13384 // for this dimension. Also, we should always expect a length if the base type
13387 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
13388 return ATy->getSize().getSExtValue() != 1;
13389 // We cannot assume anything.
13393 // Check if the length evaluates to 1.
13394 Expr::EvalResult Result;
13395 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
13396 return false; // Can't get the integer value as a constant.
13398 llvm::APSInt ConstLength = Result.Val.getInt();
13399 return ConstLength.getSExtValue() != 1;
13402 // Return the expression of the base of the mappable expression or null if it
13403 // cannot be determined and do all the necessary checks to see if the expression
13404 // is valid as a standalone mappable expression. In the process, record all the
13405 // components of the expression.
13406 static const Expr *checkMapClauseExpressionBase(
13407 Sema &SemaRef, Expr *E,
13408 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
13409 OpenMPClauseKind CKind, bool NoDiagnose) {
13410 SourceLocation ELoc = E->getExprLoc();
13411 SourceRange ERange = E->getSourceRange();
13413 // The base of elements of list in a map clause have to be either:
13414 // - a reference to variable or field.
13415 // - a member expression.
13416 // - an array expression.
13418 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
13419 // reference to 'r'.
13426 // #pragma omp target map (S.Arr[:12]);
13430 // We want to retrieve the member expression 'this->S';
13432 const Expr *RelevantExpr = nullptr;
13434 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
13435 // If a list item is an array section, it must specify contiguous storage.
13437 // For this restriction it is sufficient that we make sure only references
13438 // to variables or fields and array expressions, and that no array sections
13439 // exist except in the rightmost expression (unless they cover the whole
13440 // dimension of the array). E.g. these would be invalid:
13442 // r.ArrS[3:5].Arr[6:7]
13446 // but these would be valid:
13447 // r.ArrS[3].Arr[6:7]
13451 bool AllowUnitySizeArraySection = true;
13452 bool AllowWholeSizeArraySection = true;
13454 while (!RelevantExpr) {
13455 E = E->IgnoreParenImpCasts();
13457 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
13458 if (!isa<VarDecl>(CurE->getDecl()))
13461 RelevantExpr = CurE;
13463 // If we got a reference to a declaration, we should not expect any array
13464 // section before that.
13465 AllowUnitySizeArraySection = false;
13466 AllowWholeSizeArraySection = false;
13468 // Record the component.
13469 CurComponents.emplace_back(CurE, CurE->getDecl());
13470 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
13471 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
13473 if (isa<CXXThisExpr>(BaseE))
13474 // We found a base expression: this->Val.
13475 RelevantExpr = CurE;
13479 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
13481 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
13482 << CurE->getSourceRange();
13490 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
13492 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
13493 // A bit-field cannot appear in a map clause.
13495 if (FD->isBitField()) {
13497 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
13498 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
13506 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13507 // If the type of a list item is a reference to a type T then the type
13508 // will be considered to be T for all purposes of this clause.
13509 QualType CurType = BaseE->getType().getNonReferenceType();
13511 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
13512 // A list item cannot be a variable that is a member of a structure with
13515 if (CurType->isUnionType()) {
13517 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
13518 << CurE->getSourceRange();
13524 // If we got a member expression, we should not expect any array section
13527 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
13528 // If a list item is an element of a structure, only the rightmost symbol
13529 // of the variable reference can be an array section.
13531 AllowUnitySizeArraySection = false;
13532 AllowWholeSizeArraySection = false;
13534 // Record the component.
13535 CurComponents.emplace_back(CurE, FD);
13536 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
13537 E = CurE->getBase()->IgnoreParenImpCasts();
13539 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
13541 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
13542 << 0 << CurE->getSourceRange();
13548 // If we got an array subscript that express the whole dimension we
13549 // can have any array expressions before. If it only expressing part of
13550 // the dimension, we can only have unitary-size array expressions.
13551 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
13553 AllowWholeSizeArraySection = false;
13555 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
13556 Expr::EvalResult Result;
13557 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
13558 if (!Result.Val.getInt().isNullValue()) {
13559 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
13560 diag::err_omp_invalid_map_this_expr);
13561 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
13562 diag::note_omp_invalid_subscript_on_this_ptr_map);
13568 // Record the component - we don't have any declaration associated.
13569 CurComponents.emplace_back(CurE, nullptr);
13570 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
13571 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
13572 E = CurE->getBase()->IgnoreParenImpCasts();
13575 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
13577 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13578 // If the type of a list item is a reference to a type T then the type
13579 // will be considered to be T for all purposes of this clause.
13580 if (CurType->isReferenceType())
13581 CurType = CurType->getPointeeType();
13583 bool IsPointer = CurType->isAnyPointerType();
13585 if (!IsPointer && !CurType->isArrayType()) {
13586 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
13587 << 0 << CurE->getSourceRange();
13592 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
13594 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
13596 if (AllowWholeSizeArraySection) {
13597 // Any array section is currently allowed. Allowing a whole size array
13598 // section implies allowing a unity array section as well.
13600 // If this array section refers to the whole dimension we can still
13601 // accept other array sections before this one, except if the base is a
13602 // pointer. Otherwise, only unitary sections are accepted.
13603 if (NotWhole || IsPointer)
13604 AllowWholeSizeArraySection = false;
13605 } else if (AllowUnitySizeArraySection && NotUnity) {
13606 // A unity or whole array section is not allowed and that is not
13607 // compatible with the properties of the current array section.
13609 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
13610 << CurE->getSourceRange();
13614 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
13615 Expr::EvalResult ResultR;
13616 Expr::EvalResult ResultL;
13617 if (CurE->getLength()->EvaluateAsInt(ResultR,
13618 SemaRef.getASTContext())) {
13619 if (!ResultR.Val.getInt().isOneValue()) {
13620 SemaRef.Diag(CurE->getLength()->getExprLoc(),
13621 diag::err_omp_invalid_map_this_expr);
13622 SemaRef.Diag(CurE->getLength()->getExprLoc(),
13623 diag::note_omp_invalid_length_on_this_ptr_mapping);
13626 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
13627 ResultL, SemaRef.getASTContext())) {
13628 if (!ResultL.Val.getInt().isNullValue()) {
13629 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
13630 diag::err_omp_invalid_map_this_expr);
13631 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
13632 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
13638 // Record the component - we don't have any declaration associated.
13639 CurComponents.emplace_back(CurE, nullptr);
13642 // If nothing else worked, this is not a valid map clause expression.
13644 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
13651 return RelevantExpr;
13654 // Return true if expression E associated with value VD has conflicts with other
13655 // map information.
13656 static bool checkMapConflicts(
13657 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
13658 bool CurrentRegionOnly,
13659 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
13660 OpenMPClauseKind CKind) {
13662 SourceLocation ELoc = E->getExprLoc();
13663 SourceRange ERange = E->getSourceRange();
13665 // In order to easily check the conflicts we need to match each component of
13666 // the expression under test with the components of the expressions that are
13667 // already in the stack.
13669 assert(!CurComponents.empty() && "Map clause expression with no components!");
13670 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
13671 "Map clause expression with unexpected base!");
13673 // Variables to help detecting enclosing problems in data environment nests.
13674 bool IsEnclosedByDataEnvironmentExpr = false;
13675 const Expr *EnclosingExpr = nullptr;
13677 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
13678 VD, CurrentRegionOnly,
13679 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
13680 ERange, CKind, &EnclosingExpr,
13681 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
13683 OpenMPClauseKind) {
13684 assert(!StackComponents.empty() &&
13685 "Map clause expression with no components!");
13686 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
13687 "Map clause expression with unexpected base!");
13690 // The whole expression in the stack.
13691 const Expr *RE = StackComponents.front().getAssociatedExpression();
13693 // Expressions must start from the same base. Here we detect at which
13694 // point both expressions diverge from each other and see if we can
13695 // detect if the memory referred to both expressions is contiguous and
13697 auto CI = CurComponents.rbegin();
13698 auto CE = CurComponents.rend();
13699 auto SI = StackComponents.rbegin();
13700 auto SE = StackComponents.rend();
13701 for (; CI != CE && SI != SE; ++CI, ++SI) {
13703 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
13704 // At most one list item can be an array item derived from a given
13705 // variable in map clauses of the same construct.
13706 if (CurrentRegionOnly &&
13707 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
13708 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
13709 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
13710 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
13711 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
13712 diag::err_omp_multiple_array_items_in_map_clause)
13713 << CI->getAssociatedExpression()->getSourceRange();
13714 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
13715 diag::note_used_here)
13716 << SI->getAssociatedExpression()->getSourceRange();
13720 // Do both expressions have the same kind?
13721 if (CI->getAssociatedExpression()->getStmtClass() !=
13722 SI->getAssociatedExpression()->getStmtClass())
13725 // Are we dealing with different variables/fields?
13726 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
13729 // Check if the extra components of the expressions in the enclosing
13730 // data environment are redundant for the current base declaration.
13731 // If they are, the maps completely overlap, which is legal.
13732 for (; SI != SE; ++SI) {
13734 if (const auto *ASE =
13735 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
13736 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
13737 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
13738 SI->getAssociatedExpression())) {
13739 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
13741 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
13743 if (Type.isNull() || Type->isAnyPointerType() ||
13744 checkArrayExpressionDoesNotReferToWholeSize(
13745 SemaRef, SI->getAssociatedExpression(), Type))
13749 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
13750 // List items of map clauses in the same construct must not share
13751 // original storage.
13753 // If the expressions are exactly the same or one is a subset of the
13754 // other, it means they are sharing storage.
13755 if (CI == CE && SI == SE) {
13756 if (CurrentRegionOnly) {
13757 if (CKind == OMPC_map) {
13758 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
13760 assert(CKind == OMPC_to || CKind == OMPC_from);
13761 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
13764 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13765 << RE->getSourceRange();
13768 // If we find the same expression in the enclosing data environment,
13770 IsEnclosedByDataEnvironmentExpr = true;
13774 QualType DerivedType =
13775 std::prev(CI)->getAssociatedDeclaration()->getType();
13776 SourceLocation DerivedLoc =
13777 std::prev(CI)->getAssociatedExpression()->getExprLoc();
13779 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13780 // If the type of a list item is a reference to a type T then the type
13781 // will be considered to be T for all purposes of this clause.
13782 DerivedType = DerivedType.getNonReferenceType();
13784 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
13785 // A variable for which the type is pointer and an array section
13786 // derived from that variable must not appear as list items of map
13787 // clauses of the same construct.
13789 // Also, cover one of the cases in:
13790 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
13791 // If any part of the original storage of a list item has corresponding
13792 // storage in the device data environment, all of the original storage
13793 // must have corresponding storage in the device data environment.
13795 if (DerivedType->isAnyPointerType()) {
13796 if (CI == CE || SI == SE) {
13799 diag::err_omp_pointer_mapped_along_with_derived_section)
13801 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13802 << RE->getSourceRange();
13805 if (CI->getAssociatedExpression()->getStmtClass() !=
13806 SI->getAssociatedExpression()->getStmtClass() ||
13807 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
13808 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
13809 assert(CI != CE && SI != SE);
13810 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
13812 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13813 << RE->getSourceRange();
13818 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
13819 // List items of map clauses in the same construct must not share
13820 // original storage.
13822 // An expression is a subset of the other.
13823 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
13824 if (CKind == OMPC_map) {
13825 if (CI != CE || SI != SE) {
13826 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
13829 CI != CE ? CurComponents.begin() : StackComponents.begin();
13830 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
13832 while (It != End && !It->getAssociatedDeclaration())
13833 std::advance(It, 1);
13834 assert(It != End &&
13835 "Expected at least one component with the declaration.");
13836 if (It != Begin && It->getAssociatedDeclaration()
13838 .getCanonicalType()
13839 ->isAnyPointerType()) {
13840 IsEnclosedByDataEnvironmentExpr = false;
13841 EnclosingExpr = nullptr;
13845 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
13847 assert(CKind == OMPC_to || CKind == OMPC_from);
13848 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
13851 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13852 << RE->getSourceRange();
13856 // The current expression uses the same base as other expression in the
13857 // data environment but does not contain it completely.
13858 if (!CurrentRegionOnly && SI != SE)
13859 EnclosingExpr = RE;
13861 // The current expression is a subset of the expression in the data
13863 IsEnclosedByDataEnvironmentExpr |=
13864 (!CurrentRegionOnly && CI != CE && SI == SE);
13869 if (CurrentRegionOnly)
13872 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
13873 // If any part of the original storage of a list item has corresponding
13874 // storage in the device data environment, all of the original storage must
13875 // have corresponding storage in the device data environment.
13876 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
13877 // If a list item is an element of a structure, and a different element of
13878 // the structure has a corresponding list item in the device data environment
13879 // prior to a task encountering the construct associated with the map clause,
13880 // then the list item must also have a corresponding list item in the device
13881 // data environment prior to the task encountering the construct.
13883 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
13885 diag::err_omp_original_storage_is_shared_and_does_not_contain)
13887 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
13888 << EnclosingExpr->getSourceRange();
13895 // Look up the user-defined mapper given the mapper name and mapped type, and
13896 // build a reference to it.
13897 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
13898 CXXScopeSpec &MapperIdScopeSpec,
13899 const DeclarationNameInfo &MapperId,
13901 Expr *UnresolvedMapper) {
13902 if (MapperIdScopeSpec.isInvalid())
13903 return ExprError();
13904 // Find all user-defined mappers with the given MapperId.
13905 SmallVector<UnresolvedSet<8>, 4> Lookups;
13906 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
13907 Lookup.suppressDiagnostics();
13909 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
13910 NamedDecl *D = Lookup.getRepresentativeDecl();
13911 while (S && !S->isDeclScope(D))
13912 S = S->getParent();
13914 S = S->getParent();
13915 Lookups.emplace_back();
13916 Lookups.back().append(Lookup.begin(), Lookup.end());
13919 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
13920 // Extract the user-defined mappers with the given MapperId.
13921 Lookups.push_back(UnresolvedSet<8>());
13922 for (NamedDecl *D : ULE->decls()) {
13923 auto *DMD = cast<OMPDeclareMapperDecl>(D);
13924 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
13925 Lookups.back().addDecl(DMD);
13928 // Defer the lookup for dependent types. The results will be passed through
13929 // UnresolvedMapper on instantiation.
13930 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
13931 Type->isInstantiationDependentType() ||
13932 Type->containsUnexpandedParameterPack() ||
13933 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
13934 return !D->isInvalidDecl() &&
13935 (D->getType()->isDependentType() ||
13936 D->getType()->isInstantiationDependentType() ||
13937 D->getType()->containsUnexpandedParameterPack());
13939 UnresolvedSet<8> URS;
13940 for (const UnresolvedSet<8> &Set : Lookups) {
13943 URS.append(Set.begin(), Set.end());
13945 return UnresolvedLookupExpr::Create(
13946 SemaRef.Context, /*NamingClass=*/nullptr,
13947 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
13948 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
13950 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
13951 // The type must be of struct, union or class type in C and C++
13952 if (!Type->isStructureOrClassType() && !Type->isUnionType())
13953 return ExprEmpty();
13954 SourceLocation Loc = MapperId.getLoc();
13955 // Perform argument dependent lookup.
13956 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
13957 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
13958 // Return the first user-defined mapper with the desired type.
13959 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13960 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
13961 if (!D->isInvalidDecl() &&
13962 SemaRef.Context.hasSameType(D->getType(), Type))
13966 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
13967 // Find the first user-defined mapper with a type derived from the desired
13969 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13970 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
13971 if (!D->isInvalidDecl() &&
13972 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
13973 !Type.isMoreQualifiedThan(D->getType()))
13977 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
13978 /*DetectVirtual=*/false);
13979 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
13980 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
13981 VD->getType().getUnqualifiedType()))) {
13982 if (SemaRef.CheckBaseClassAccess(
13983 Loc, VD->getType(), Type, Paths.front(),
13984 /*DiagID=*/0) != Sema::AR_inaccessible) {
13985 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
13990 // Report error if a mapper is specified, but cannot be found.
13991 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
13992 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
13993 << Type << MapperId.getName();
13994 return ExprError();
13996 return ExprEmpty();
14000 // Utility struct that gathers all the related lists associated with a mappable
14002 struct MappableVarListInfo {
14003 // The list of expressions.
14004 ArrayRef<Expr *> VarList;
14005 // The list of processed expressions.
14006 SmallVector<Expr *, 16> ProcessedVarList;
14007 // The mappble components for each expression.
14008 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
14009 // The base declaration of the variable.
14010 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
14011 // The reference to the user-defined mapper associated with every expression.
14012 SmallVector<Expr *, 16> UDMapperList;
14014 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
14015 // We have a list of components and base declarations for each entry in the
14017 VarComponents.reserve(VarList.size());
14018 VarBaseDeclarations.reserve(VarList.size());
14023 // Check the validity of the provided variable list for the provided clause kind
14024 // \a CKind. In the check process the valid expressions, mappable expression
14025 // components, variables, and user-defined mappers are extracted and used to
14026 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
14027 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
14028 // and \a MapperId are expected to be valid if the clause kind is 'map'.
14029 static void checkMappableExpressionList(
14030 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
14031 MappableVarListInfo &MVLI, SourceLocation StartLoc,
14032 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
14033 ArrayRef<Expr *> UnresolvedMappers,
14034 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
14035 bool IsMapTypeImplicit = false) {
14036 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
14037 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
14038 "Unexpected clause kind with mappable expressions!");
14040 // If the identifier of user-defined mapper is not specified, it is "default".
14041 // We do not change the actual name in this clause to distinguish whether a
14042 // mapper is specified explicitly, i.e., it is not explicitly specified when
14043 // MapperId.getName() is empty.
14044 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
14045 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
14046 MapperId.setName(DeclNames.getIdentifier(
14047 &SemaRef.getASTContext().Idents.get("default")));
14050 // Iterators to find the current unresolved mapper expression.
14051 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
14052 bool UpdateUMIt = false;
14053 Expr *UnresolvedMapper = nullptr;
14055 // Keep track of the mappable components and base declarations in this clause.
14056 // Each entry in the list is going to have a list of components associated. We
14057 // record each set of the components so that we can build the clause later on.
14058 // In the end we should have the same amount of declarations and component
14061 for (Expr *RE : MVLI.VarList) {
14062 assert(RE && "Null expr in omp to/from/map clause");
14063 SourceLocation ELoc = RE->getExprLoc();
14065 // Find the current unresolved mapper expression.
14066 if (UpdateUMIt && UMIt != UMEnd) {
14070 "Expect the size of UnresolvedMappers to match with that of VarList");
14074 UnresolvedMapper = *UMIt;
14076 const Expr *VE = RE->IgnoreParenLValueCasts();
14078 if (VE->isValueDependent() || VE->isTypeDependent() ||
14079 VE->isInstantiationDependent() ||
14080 VE->containsUnexpandedParameterPack()) {
14081 // Try to find the associated user-defined mapper.
14082 ExprResult ER = buildUserDefinedMapperRef(
14083 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14084 VE->getType().getCanonicalType(), UnresolvedMapper);
14085 if (ER.isInvalid())
14087 MVLI.UDMapperList.push_back(ER.get());
14088 // We can only analyze this information once the missing information is
14090 MVLI.ProcessedVarList.push_back(RE);
14094 Expr *SimpleExpr = RE->IgnoreParenCasts();
14096 if (!RE->IgnoreParenImpCasts()->isLValue()) {
14098 diag::err_omp_expected_named_var_member_or_array_expression)
14099 << RE->getSourceRange();
14103 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
14104 ValueDecl *CurDeclaration = nullptr;
14106 // Obtain the array or member expression bases if required. Also, fill the
14107 // components array with all the components identified in the process.
14108 const Expr *BE = checkMapClauseExpressionBase(
14109 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
14113 assert(!CurComponents.empty() &&
14114 "Invalid mappable expression information.");
14116 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
14117 // Add store "this" pointer to class in DSAStackTy for future checking
14118 DSAS->addMappedClassesQualTypes(TE->getType());
14119 // Try to find the associated user-defined mapper.
14120 ExprResult ER = buildUserDefinedMapperRef(
14121 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14122 VE->getType().getCanonicalType(), UnresolvedMapper);
14123 if (ER.isInvalid())
14125 MVLI.UDMapperList.push_back(ER.get());
14126 // Skip restriction checking for variable or field declarations
14127 MVLI.ProcessedVarList.push_back(RE);
14128 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14129 MVLI.VarComponents.back().append(CurComponents.begin(),
14130 CurComponents.end());
14131 MVLI.VarBaseDeclarations.push_back(nullptr);
14135 // For the following checks, we rely on the base declaration which is
14136 // expected to be associated with the last component. The declaration is
14137 // expected to be a variable or a field (if 'this' is being mapped).
14138 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
14139 assert(CurDeclaration && "Null decl on map clause.");
14141 CurDeclaration->isCanonicalDecl() &&
14142 "Expecting components to have associated only canonical declarations.");
14144 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
14145 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
14147 assert((VD || FD) && "Only variables or fields are expected here!");
14150 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
14151 // threadprivate variables cannot appear in a map clause.
14152 // OpenMP 4.5 [2.10.5, target update Construct]
14153 // threadprivate variables cannot appear in a from clause.
14154 if (VD && DSAS->isThreadPrivate(VD)) {
14155 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
14156 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
14157 << getOpenMPClauseName(CKind);
14158 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
14162 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
14163 // A list item cannot appear in both a map clause and a data-sharing
14164 // attribute clause on the same construct.
14166 // Check conflicts with other map clause expressions. We check the conflicts
14167 // with the current construct separately from the enclosing data
14168 // environment, because the restrictions are different. We only have to
14169 // check conflicts across regions for the map clauses.
14170 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
14171 /*CurrentRegionOnly=*/true, CurComponents, CKind))
14173 if (CKind == OMPC_map &&
14174 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
14175 /*CurrentRegionOnly=*/false, CurComponents, CKind))
14178 // OpenMP 4.5 [2.10.5, target update Construct]
14179 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14180 // If the type of a list item is a reference to a type T then the type will
14181 // be considered to be T for all purposes of this clause.
14182 auto I = llvm::find_if(
14184 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
14185 return MC.getAssociatedDeclaration();
14187 assert(I != CurComponents.end() && "Null decl on map clause.");
14189 I->getAssociatedDeclaration()->getType().getNonReferenceType();
14191 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
14192 // A list item in a to or from clause must have a mappable type.
14193 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
14194 // A list item must have a mappable type.
14195 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
14199 if (CKind == OMPC_map) {
14200 // target enter data
14201 // OpenMP [2.10.2, Restrictions, p. 99]
14202 // A map-type must be specified in all map clauses and must be either
14204 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
14205 if (DKind == OMPD_target_enter_data &&
14206 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
14207 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
14208 << (IsMapTypeImplicit ? 1 : 0)
14209 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
14210 << getOpenMPDirectiveName(DKind);
14214 // target exit_data
14215 // OpenMP [2.10.3, Restrictions, p. 102]
14216 // A map-type must be specified in all map clauses and must be either
14217 // from, release, or delete.
14218 if (DKind == OMPD_target_exit_data &&
14219 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
14220 MapType == OMPC_MAP_delete)) {
14221 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
14222 << (IsMapTypeImplicit ? 1 : 0)
14223 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
14224 << getOpenMPDirectiveName(DKind);
14228 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
14229 // A list item cannot appear in both a map clause and a data-sharing
14230 // attribute clause on the same construct
14231 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
14232 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
14233 if (isOpenMPPrivate(DVar.CKind)) {
14234 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
14235 << getOpenMPClauseName(DVar.CKind)
14236 << getOpenMPClauseName(OMPC_map)
14237 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
14238 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
14244 // Try to find the associated user-defined mapper.
14245 ExprResult ER = buildUserDefinedMapperRef(
14246 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14247 Type.getCanonicalType(), UnresolvedMapper);
14248 if (ER.isInvalid())
14250 MVLI.UDMapperList.push_back(ER.get());
14252 // Save the current expression.
14253 MVLI.ProcessedVarList.push_back(RE);
14255 // Store the components in the stack so that they can be used to check
14256 // against other clauses later on.
14257 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
14258 /*WhereFoundClauseKind=*/OMPC_map);
14260 // Save the components and declaration to create the clause. For purposes of
14261 // the clause creation, any component list that has has base 'this' uses
14262 // null as base declaration.
14263 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14264 MVLI.VarComponents.back().append(CurComponents.begin(),
14265 CurComponents.end());
14266 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
14271 OMPClause *Sema::ActOnOpenMPMapClause(
14272 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
14273 ArrayRef<SourceLocation> MapTypeModifiersLoc,
14274 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
14275 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
14276 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
14277 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
14278 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
14279 OMPC_MAP_MODIFIER_unknown,
14280 OMPC_MAP_MODIFIER_unknown};
14281 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
14283 // Process map-type-modifiers, flag errors for duplicate modifiers.
14284 unsigned Count = 0;
14285 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
14286 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
14287 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
14288 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
14291 assert(Count < OMPMapClause::NumberOfModifiers &&
14292 "Modifiers exceed the allowed number of map type modifiers");
14293 Modifiers[Count] = MapTypeModifiers[I];
14294 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
14298 MappableVarListInfo MVLI(VarList);
14299 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
14300 MapperIdScopeSpec, MapperId, UnresolvedMappers,
14301 MapType, IsMapTypeImplicit);
14303 // We need to produce a map clause even if we don't have variables so that
14304 // other diagnostics related with non-existing map clauses are accurate.
14305 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
14306 MVLI.VarBaseDeclarations, MVLI.VarComponents,
14307 MVLI.UDMapperList, Modifiers, ModifiersLoc,
14308 MapperIdScopeSpec.getWithLocInContext(Context),
14309 MapperId, MapType, IsMapTypeImplicit, MapLoc);
14312 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
14313 TypeResult ParsedType) {
14314 assert(ParsedType.isUsable());
14316 QualType ReductionType = GetTypeFromParser(ParsedType.get());
14317 if (ReductionType.isNull())
14320 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
14321 // A type name in a declare reduction directive cannot be a function type, an
14322 // array type, a reference type, or a type qualified with const, volatile or
14324 if (ReductionType.hasQualifiers()) {
14325 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
14329 if (ReductionType->isFunctionType()) {
14330 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
14333 if (ReductionType->isReferenceType()) {
14334 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
14337 if (ReductionType->isArrayType()) {
14338 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
14341 return ReductionType;
14344 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
14345 Scope *S, DeclContext *DC, DeclarationName Name,
14346 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
14347 AccessSpecifier AS, Decl *PrevDeclInScope) {
14348 SmallVector<Decl *, 8> Decls;
14349 Decls.reserve(ReductionTypes.size());
14351 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
14352 forRedeclarationInCurContext());
14353 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
14354 // A reduction-identifier may not be re-declared in the current scope for the
14355 // same type or for a type that is compatible according to the base language
14357 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
14358 OMPDeclareReductionDecl *PrevDRD = nullptr;
14359 bool InCompoundScope = true;
14360 if (S != nullptr) {
14361 // Find previous declaration with the same name not referenced in other
14363 FunctionScopeInfo *ParentFn = getEnclosingFunction();
14365 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
14366 LookupName(Lookup, S);
14367 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
14368 /*AllowInlineNamespace=*/false);
14369 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
14370 LookupResult::Filter Filter = Lookup.makeFilter();
14371 while (Filter.hasNext()) {
14372 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
14373 if (InCompoundScope) {
14374 auto I = UsedAsPrevious.find(PrevDecl);
14375 if (I == UsedAsPrevious.end())
14376 UsedAsPrevious[PrevDecl] = false;
14377 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
14378 UsedAsPrevious[D] = true;
14380 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
14381 PrevDecl->getLocation();
14384 if (InCompoundScope) {
14385 for (const auto &PrevData : UsedAsPrevious) {
14386 if (!PrevData.second) {
14387 PrevDRD = PrevData.first;
14392 } else if (PrevDeclInScope != nullptr) {
14393 auto *PrevDRDInScope = PrevDRD =
14394 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
14396 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
14397 PrevDRDInScope->getLocation();
14398 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
14399 } while (PrevDRDInScope != nullptr);
14401 for (const auto &TyData : ReductionTypes) {
14402 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
14403 bool Invalid = false;
14404 if (I != PreviousRedeclTypes.end()) {
14405 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
14407 Diag(I->second, diag::note_previous_definition);
14410 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
14411 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
14412 Name, TyData.first, PrevDRD);
14414 DRD->setAccess(AS);
14415 Decls.push_back(DRD);
14417 DRD->setInvalidDecl();
14422 return DeclGroupPtrTy::make(
14423 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
14426 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
14427 auto *DRD = cast<OMPDeclareReductionDecl>(D);
14429 // Enter new function scope.
14430 PushFunctionScope();
14431 setFunctionHasBranchProtectedScope();
14432 getCurFunction()->setHasOMPDeclareReductionCombiner();
14435 PushDeclContext(S, DRD);
14439 PushExpressionEvaluationContext(
14440 ExpressionEvaluationContext::PotentiallyEvaluated);
14442 QualType ReductionType = DRD->getType();
14443 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
14444 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
14445 // uses semantics of argument handles by value, but it should be passed by
14446 // reference. C lang does not support references, so pass all parameters as
14448 // Create 'T omp_in;' variable.
14449 VarDecl *OmpInParm =
14450 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
14451 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
14452 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
14453 // uses semantics of argument handles by value, but it should be passed by
14454 // reference. C lang does not support references, so pass all parameters as
14456 // Create 'T omp_out;' variable.
14457 VarDecl *OmpOutParm =
14458 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
14459 if (S != nullptr) {
14460 PushOnScopeChains(OmpInParm, S);
14461 PushOnScopeChains(OmpOutParm, S);
14463 DRD->addDecl(OmpInParm);
14464 DRD->addDecl(OmpOutParm);
14467 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
14469 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
14470 DRD->setCombinerData(InE, OutE);
14473 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
14474 auto *DRD = cast<OMPDeclareReductionDecl>(D);
14475 DiscardCleanupsInEvaluationContext();
14476 PopExpressionEvaluationContext();
14479 PopFunctionScopeInfo();
14481 if (Combiner != nullptr)
14482 DRD->setCombiner(Combiner);
14484 DRD->setInvalidDecl();
14487 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
14488 auto *DRD = cast<OMPDeclareReductionDecl>(D);
14490 // Enter new function scope.
14491 PushFunctionScope();
14492 setFunctionHasBranchProtectedScope();
14495 PushDeclContext(S, DRD);
14499 PushExpressionEvaluationContext(
14500 ExpressionEvaluationContext::PotentiallyEvaluated);
14502 QualType ReductionType = DRD->getType();
14503 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
14504 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
14505 // uses semantics of argument handles by value, but it should be passed by
14506 // reference. C lang does not support references, so pass all parameters as
14508 // Create 'T omp_priv;' variable.
14509 VarDecl *OmpPrivParm =
14510 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
14511 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
14512 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
14513 // uses semantics of argument handles by value, but it should be passed by
14514 // reference. C lang does not support references, so pass all parameters as
14516 // Create 'T omp_orig;' variable.
14517 VarDecl *OmpOrigParm =
14518 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
14519 if (S != nullptr) {
14520 PushOnScopeChains(OmpPrivParm, S);
14521 PushOnScopeChains(OmpOrigParm, S);
14523 DRD->addDecl(OmpPrivParm);
14524 DRD->addDecl(OmpOrigParm);
14527 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
14529 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
14530 DRD->setInitializerData(OrigE, PrivE);
14531 return OmpPrivParm;
14534 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
14535 VarDecl *OmpPrivParm) {
14536 auto *DRD = cast<OMPDeclareReductionDecl>(D);
14537 DiscardCleanupsInEvaluationContext();
14538 PopExpressionEvaluationContext();
14541 PopFunctionScopeInfo();
14543 if (Initializer != nullptr) {
14544 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
14545 } else if (OmpPrivParm->hasInit()) {
14546 DRD->setInitializer(OmpPrivParm->getInit(),
14547 OmpPrivParm->isDirectInit()
14548 ? OMPDeclareReductionDecl::DirectInit
14549 : OMPDeclareReductionDecl::CopyInit);
14551 DRD->setInvalidDecl();
14555 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
14556 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
14557 for (Decl *D : DeclReductions.get()) {
14560 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
14561 /*AddToContext=*/false);
14563 D->setInvalidDecl();
14566 return DeclReductions;
14569 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
14570 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
14571 QualType T = TInfo->getType();
14572 if (D.isInvalidType())
14575 if (getLangOpts().CPlusPlus) {
14576 // Check that there are no default arguments (C++ only).
14577 CheckExtraCXXDefaultArguments(D);
14580 return CreateParsedType(T, TInfo);
14583 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
14584 TypeResult ParsedType) {
14585 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
14587 QualType MapperType = GetTypeFromParser(ParsedType.get());
14588 assert(!MapperType.isNull() && "Expect valid mapper type");
14590 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
14591 // The type must be of struct, union or class type in C and C++
14592 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
14593 Diag(TyLoc, diag::err_omp_mapper_wrong_type);
14599 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
14600 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
14601 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
14602 Decl *PrevDeclInScope) {
14603 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
14604 forRedeclarationInCurContext());
14605 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
14606 // A mapper-identifier may not be redeclared in the current scope for the
14607 // same type or for a type that is compatible according to the base language
14609 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
14610 OMPDeclareMapperDecl *PrevDMD = nullptr;
14611 bool InCompoundScope = true;
14612 if (S != nullptr) {
14613 // Find previous declaration with the same name not referenced in other
14615 FunctionScopeInfo *ParentFn = getEnclosingFunction();
14617 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
14618 LookupName(Lookup, S);
14619 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
14620 /*AllowInlineNamespace=*/false);
14621 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
14622 LookupResult::Filter Filter = Lookup.makeFilter();
14623 while (Filter.hasNext()) {
14624 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
14625 if (InCompoundScope) {
14626 auto I = UsedAsPrevious.find(PrevDecl);
14627 if (I == UsedAsPrevious.end())
14628 UsedAsPrevious[PrevDecl] = false;
14629 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
14630 UsedAsPrevious[D] = true;
14632 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
14633 PrevDecl->getLocation();
14636 if (InCompoundScope) {
14637 for (const auto &PrevData : UsedAsPrevious) {
14638 if (!PrevData.second) {
14639 PrevDMD = PrevData.first;
14644 } else if (PrevDeclInScope) {
14645 auto *PrevDMDInScope = PrevDMD =
14646 cast<OMPDeclareMapperDecl>(PrevDeclInScope);
14648 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
14649 PrevDMDInScope->getLocation();
14650 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
14651 } while (PrevDMDInScope != nullptr);
14653 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
14654 bool Invalid = false;
14655 if (I != PreviousRedeclTypes.end()) {
14656 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
14657 << MapperType << Name;
14658 Diag(I->second, diag::note_previous_definition);
14661 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
14662 MapperType, VN, PrevDMD);
14664 DMD->setAccess(AS);
14666 DMD->setInvalidDecl();
14668 // Enter new function scope.
14669 PushFunctionScope();
14670 setFunctionHasBranchProtectedScope();
14677 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
14679 QualType MapperType,
14680 SourceLocation StartLoc,
14681 DeclarationName VN) {
14682 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
14684 PushOnScopeChains(VD, S);
14687 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
14688 DMD->setMapperVarRef(MapperVarRefExpr);
14691 Sema::DeclGroupPtrTy
14692 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
14693 ArrayRef<OMPClause *> ClauseList) {
14695 PopFunctionScopeInfo();
14699 PushOnScopeChains(D, S, /*AddToContext=*/false);
14700 D->CreateClauses(Context, ClauseList);
14703 return DeclGroupPtrTy::make(DeclGroupRef(D));
14706 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
14707 SourceLocation StartLoc,
14708 SourceLocation LParenLoc,
14709 SourceLocation EndLoc) {
14710 Expr *ValExpr = NumTeams;
14711 Stmt *HelperValStmt = nullptr;
14713 // OpenMP [teams Constrcut, Restrictions]
14714 // The num_teams expression must evaluate to a positive integer value.
14715 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
14716 /*StrictlyPositive=*/true))
14719 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14720 OpenMPDirectiveKind CaptureRegion =
14721 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
14722 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14723 ValExpr = MakeFullExpr(ValExpr).get();
14724 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14725 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14726 HelperValStmt = buildPreInits(Context, Captures);
14729 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
14730 StartLoc, LParenLoc, EndLoc);
14733 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
14734 SourceLocation StartLoc,
14735 SourceLocation LParenLoc,
14736 SourceLocation EndLoc) {
14737 Expr *ValExpr = ThreadLimit;
14738 Stmt *HelperValStmt = nullptr;
14740 // OpenMP [teams Constrcut, Restrictions]
14741 // The thread_limit expression must evaluate to a positive integer value.
14742 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
14743 /*StrictlyPositive=*/true))
14746 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14747 OpenMPDirectiveKind CaptureRegion =
14748 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
14749 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14750 ValExpr = MakeFullExpr(ValExpr).get();
14751 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14752 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14753 HelperValStmt = buildPreInits(Context, Captures);
14756 return new (Context) OMPThreadLimitClause(
14757 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
14760 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
14761 SourceLocation StartLoc,
14762 SourceLocation LParenLoc,
14763 SourceLocation EndLoc) {
14764 Expr *ValExpr = Priority;
14766 // OpenMP [2.9.1, task Constrcut]
14767 // The priority-value is a non-negative numerical scalar expression.
14768 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
14769 /*StrictlyPositive=*/false))
14772 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14775 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
14776 SourceLocation StartLoc,
14777 SourceLocation LParenLoc,
14778 SourceLocation EndLoc) {
14779 Expr *ValExpr = Grainsize;
14781 // OpenMP [2.9.2, taskloop Constrcut]
14782 // The parameter of the grainsize clause must be a positive integer
14784 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
14785 /*StrictlyPositive=*/true))
14788 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14791 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
14792 SourceLocation StartLoc,
14793 SourceLocation LParenLoc,
14794 SourceLocation EndLoc) {
14795 Expr *ValExpr = NumTasks;
14797 // OpenMP [2.9.2, taskloop Constrcut]
14798 // The parameter of the num_tasks clause must be a positive integer
14800 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
14801 /*StrictlyPositive=*/true))
14804 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14807 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
14808 SourceLocation LParenLoc,
14809 SourceLocation EndLoc) {
14810 // OpenMP [2.13.2, critical construct, Description]
14811 // ... where hint-expression is an integer constant expression that evaluates
14812 // to a valid lock hint.
14813 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
14814 if (HintExpr.isInvalid())
14816 return new (Context)
14817 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
14820 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
14821 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
14822 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
14823 SourceLocation EndLoc) {
14824 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
14825 std::string Values;
14827 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
14829 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
14830 << Values << getOpenMPClauseName(OMPC_dist_schedule);
14833 Expr *ValExpr = ChunkSize;
14834 Stmt *HelperValStmt = nullptr;
14836 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
14837 !ChunkSize->isInstantiationDependent() &&
14838 !ChunkSize->containsUnexpandedParameterPack()) {
14839 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
14841 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
14842 if (Val.isInvalid())
14845 ValExpr = Val.get();
14847 // OpenMP [2.7.1, Restrictions]
14848 // chunk_size must be a loop invariant integer expression with a positive
14850 llvm::APSInt Result;
14851 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
14852 if (Result.isSigned() && !Result.isStrictlyPositive()) {
14853 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
14854 << "dist_schedule" << ChunkSize->getSourceRange();
14857 } else if (getOpenMPCaptureRegionForClause(
14858 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
14860 !CurContext->isDependentContext()) {
14861 ValExpr = MakeFullExpr(ValExpr).get();
14862 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14863 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14864 HelperValStmt = buildPreInits(Context, Captures);
14869 return new (Context)
14870 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
14871 Kind, ValExpr, HelperValStmt);
14874 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
14875 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
14876 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
14877 SourceLocation KindLoc, SourceLocation EndLoc) {
14878 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
14879 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
14881 SourceLocation Loc;
14883 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
14884 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
14885 OMPC_DEFAULTMAP_MODIFIER_tofrom);
14888 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
14889 OMPC_DEFAULTMAP_scalar);
14893 Diag(Loc, diag::err_omp_unexpected_clause_value)
14894 << Value << getOpenMPClauseName(OMPC_defaultmap);
14897 DSAStack->setDefaultDMAToFromScalar(StartLoc);
14899 return new (Context)
14900 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
14903 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
14904 DeclContext *CurLexicalContext = getCurLexicalContext();
14905 if (!CurLexicalContext->isFileContext() &&
14906 !CurLexicalContext->isExternCContext() &&
14907 !CurLexicalContext->isExternCXXContext() &&
14908 !isa<CXXRecordDecl>(CurLexicalContext) &&
14909 !isa<ClassTemplateDecl>(CurLexicalContext) &&
14910 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
14911 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
14912 Diag(Loc, diag::err_omp_region_not_file_context);
14915 ++DeclareTargetNestingLevel;
14919 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
14920 assert(DeclareTargetNestingLevel > 0 &&
14921 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
14922 --DeclareTargetNestingLevel;
14925 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
14926 CXXScopeSpec &ScopeSpec,
14927 const DeclarationNameInfo &Id,
14928 OMPDeclareTargetDeclAttr::MapTypeTy MT,
14929 NamedDeclSetType &SameDirectiveDecls) {
14930 LookupResult Lookup(*this, Id, LookupOrdinaryName);
14931 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
14933 if (Lookup.isAmbiguous())
14935 Lookup.suppressDiagnostics();
14937 if (!Lookup.isSingleResult()) {
14938 VarOrFuncDeclFilterCCC CCC(*this);
14939 if (TypoCorrection Corrected =
14940 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
14941 CTK_ErrorRecovery)) {
14942 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
14944 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
14948 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
14952 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
14953 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
14954 isa<FunctionTemplateDecl>(ND)) {
14955 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
14956 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
14957 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
14958 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
14959 cast<ValueDecl>(ND));
14961 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
14963 if (ASTMutationListener *ML = Context.getASTMutationListener())
14964 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
14965 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
14966 } else if (*Res != MT) {
14967 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
14971 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
14975 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
14976 Sema &SemaRef, Decl *D) {
14977 if (!D || !isa<VarDecl>(D))
14979 auto *VD = cast<VarDecl>(D);
14980 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
14982 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
14983 SemaRef.Diag(SL, diag::note_used_here) << SR;
14986 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
14987 Sema &SemaRef, DSAStackTy *Stack,
14989 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
14990 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
14991 /*FullCheck=*/false);
14994 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
14995 SourceLocation IdLoc) {
14996 if (!D || D->isInvalidDecl())
14998 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
14999 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
15000 if (auto *VD = dyn_cast<VarDecl>(D)) {
15001 // Only global variables can be marked as declare target.
15002 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
15003 !VD->isStaticDataMember())
15005 // 2.10.6: threadprivate variable cannot appear in a declare target
15007 if (DSAStack->isThreadPrivate(VD)) {
15008 Diag(SL, diag::err_omp_threadprivate_in_target);
15009 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
15013 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
15014 D = FTD->getTemplatedDecl();
15015 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
15016 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
15017 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
15018 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
15019 assert(IdLoc.isValid() && "Source location is expected");
15020 Diag(IdLoc, diag::err_omp_function_in_link_clause);
15021 Diag(FD->getLocation(), diag::note_defined_here) << FD;
15025 if (auto *VD = dyn_cast<ValueDecl>(D)) {
15026 // Problem if any with var declared with incomplete type will be reported
15027 // as normal, so no need to check it here.
15028 if ((E || !VD->getType()->isIncompleteType()) &&
15029 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
15031 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
15032 // Checking declaration inside declare target region.
15033 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
15034 isa<FunctionTemplateDecl>(D)) {
15035 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
15036 Context, OMPDeclareTargetDeclAttr::MT_To);
15038 if (ASTMutationListener *ML = Context.getASTMutationListener())
15039 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
15046 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
15049 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
15050 CXXScopeSpec &MapperIdScopeSpec,
15051 DeclarationNameInfo &MapperId,
15052 const OMPVarListLocTy &Locs,
15053 ArrayRef<Expr *> UnresolvedMappers) {
15054 MappableVarListInfo MVLI(VarList);
15055 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
15056 MapperIdScopeSpec, MapperId, UnresolvedMappers);
15057 if (MVLI.ProcessedVarList.empty())
15060 return OMPToClause::Create(
15061 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
15062 MVLI.VarComponents, MVLI.UDMapperList,
15063 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
15066 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
15067 CXXScopeSpec &MapperIdScopeSpec,
15068 DeclarationNameInfo &MapperId,
15069 const OMPVarListLocTy &Locs,
15070 ArrayRef<Expr *> UnresolvedMappers) {
15071 MappableVarListInfo MVLI(VarList);
15072 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
15073 MapperIdScopeSpec, MapperId, UnresolvedMappers);
15074 if (MVLI.ProcessedVarList.empty())
15077 return OMPFromClause::Create(
15078 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
15079 MVLI.VarComponents, MVLI.UDMapperList,
15080 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
15083 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
15084 const OMPVarListLocTy &Locs) {
15085 MappableVarListInfo MVLI(VarList);
15086 SmallVector<Expr *, 8> PrivateCopies;
15087 SmallVector<Expr *, 8> Inits;
15089 for (Expr *RefExpr : VarList) {
15090 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
15091 SourceLocation ELoc;
15092 SourceRange ERange;
15093 Expr *SimpleRefExpr = RefExpr;
15094 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15096 // It will be analyzed later.
15097 MVLI.ProcessedVarList.push_back(RefExpr);
15098 PrivateCopies.push_back(nullptr);
15099 Inits.push_back(nullptr);
15101 ValueDecl *D = Res.first;
15105 QualType Type = D->getType();
15106 Type = Type.getNonReferenceType().getUnqualifiedType();
15108 auto *VD = dyn_cast<VarDecl>(D);
15110 // Item should be a pointer or reference to pointer.
15111 if (!Type->isPointerType()) {
15112 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
15113 << 0 << RefExpr->getSourceRange();
15117 // Build the private variable and the expression that refers to it.
15119 buildVarDecl(*this, ELoc, Type, D->getName(),
15120 D->hasAttrs() ? &D->getAttrs() : nullptr,
15121 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
15122 if (VDPrivate->isInvalidDecl())
15125 CurContext->addDecl(VDPrivate);
15126 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
15127 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
15129 // Add temporary variable to initialize the private copy of the pointer.
15131 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
15132 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
15133 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
15134 AddInitializerToDecl(VDPrivate,
15135 DefaultLvalueConversion(VDInitRefExpr).get(),
15136 /*DirectInit=*/false);
15138 // If required, build a capture to implement the privatization initialized
15139 // with the current list item value.
15140 DeclRefExpr *Ref = nullptr;
15142 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
15143 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
15144 PrivateCopies.push_back(VDPrivateRefExpr);
15145 Inits.push_back(VDInitRefExpr);
15147 // We need to add a data sharing attribute for this variable to make sure it
15148 // is correctly captured. A variable that shows up in a use_device_ptr has
15149 // similar properties of a first private variable.
15150 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
15152 // Create a mappable component for the list item. List items in this clause
15153 // only need a component.
15154 MVLI.VarBaseDeclarations.push_back(D);
15155 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15156 MVLI.VarComponents.back().push_back(
15157 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
15160 if (MVLI.ProcessedVarList.empty())
15163 return OMPUseDevicePtrClause::Create(
15164 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
15165 MVLI.VarBaseDeclarations, MVLI.VarComponents);
15168 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
15169 const OMPVarListLocTy &Locs) {
15170 MappableVarListInfo MVLI(VarList);
15171 for (Expr *RefExpr : VarList) {
15172 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
15173 SourceLocation ELoc;
15174 SourceRange ERange;
15175 Expr *SimpleRefExpr = RefExpr;
15176 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15178 // It will be analyzed later.
15179 MVLI.ProcessedVarList.push_back(RefExpr);
15181 ValueDecl *D = Res.first;
15185 QualType Type = D->getType();
15186 // item should be a pointer or array or reference to pointer or array
15187 if (!Type.getNonReferenceType()->isPointerType() &&
15188 !Type.getNonReferenceType()->isArrayType()) {
15189 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
15190 << 0 << RefExpr->getSourceRange();
15194 // Check if the declaration in the clause does not show up in any data
15195 // sharing attribute.
15196 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
15197 if (isOpenMPPrivate(DVar.CKind)) {
15198 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
15199 << getOpenMPClauseName(DVar.CKind)
15200 << getOpenMPClauseName(OMPC_is_device_ptr)
15201 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
15202 reportOriginalDsa(*this, DSAStack, D, DVar);
15206 const Expr *ConflictExpr;
15207 if (DSAStack->checkMappableExprComponentListsForDecl(
15208 D, /*CurrentRegionOnly=*/true,
15210 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
15211 OpenMPClauseKind) -> bool {
15212 ConflictExpr = R.front().getAssociatedExpression();
15215 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
15216 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
15217 << ConflictExpr->getSourceRange();
15221 // Store the components in the stack so that they can be used to check
15222 // against other clauses later on.
15223 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
15224 DSAStack->addMappableExpressionComponents(
15225 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
15227 // Record the expression we've just processed.
15228 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
15230 // Create a mappable component for the list item. List items in this clause
15231 // only need a component. We use a null declaration to signal fields in
15233 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
15234 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
15235 "Unexpected device pointer expression!");
15236 MVLI.VarBaseDeclarations.push_back(
15237 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
15238 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15239 MVLI.VarComponents.back().push_back(MC);
15242 if (MVLI.ProcessedVarList.empty())
15245 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
15246 MVLI.VarBaseDeclarations,
15247 MVLI.VarComponents);
15250 OMPClause *Sema::ActOnOpenMPAllocateClause(
15251 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
15252 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
15254 // OpenMP [2.11.4 allocate Clause, Description]
15255 // allocator is an expression of omp_allocator_handle_t type.
15256 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
15259 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
15260 if (AllocatorRes.isInvalid())
15262 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
15263 DSAStack->getOMPAllocatorHandleT(),
15264 Sema::AA_Initializing,
15265 /*AllowExplicit=*/true);
15266 if (AllocatorRes.isInvalid())
15268 Allocator = AllocatorRes.get();
15270 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
15271 // allocate clauses that appear on a target construct or on constructs in a
15272 // target region must specify an allocator expression unless a requires
15273 // directive with the dynamic_allocators clause is present in the same
15274 // compilation unit.
15275 if (LangOpts.OpenMPIsDevice &&
15276 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
15277 targetDiag(StartLoc, diag::err_expected_allocator_expression);
15279 // Analyze and build list of variables.
15280 SmallVector<Expr *, 8> Vars;
15281 for (Expr *RefExpr : VarList) {
15282 assert(RefExpr && "NULL expr in OpenMP private clause.");
15283 SourceLocation ELoc;
15284 SourceRange ERange;
15285 Expr *SimpleRefExpr = RefExpr;
15286 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15288 // It will be analyzed later.
15289 Vars.push_back(RefExpr);
15291 ValueDecl *D = Res.first;
15295 auto *VD = dyn_cast<VarDecl>(D);
15296 DeclRefExpr *Ref = nullptr;
15297 if (!VD && !CurContext->isDependentContext())
15298 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
15299 Vars.push_back((VD || CurContext->isDependentContext())
15300 ? RefExpr->IgnoreParens()
15307 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
15308 ColonLoc, EndLoc, Vars);