1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \brief This file implements semantic analysis for OpenMP directives and
13 //===----------------------------------------------------------------------===//
15 #include "TreeTransform.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/CXXInheritance.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "clang/AST/StmtCXX.h"
23 #include "clang/AST/StmtOpenMP.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/AST/TypeOrdering.h"
26 #include "clang/Basic/OpenMPKinds.h"
27 #include "clang/Basic/TargetInfo.h"
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Sema/Initialization.h"
30 #include "clang/Sema/Lookup.h"
31 #include "clang/Sema/Scope.h"
32 #include "clang/Sema/ScopeInfo.h"
33 #include "clang/Sema/SemaInternal.h"
34 using namespace clang;
36 //===----------------------------------------------------------------------===//
37 // Stack of data-sharing attributes for variables
38 //===----------------------------------------------------------------------===//
41 /// \brief Default data sharing attributes, which can be applied to directive.
42 enum DefaultDataSharingAttributes {
43 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
48 /// \brief Stack for tracking declarations used in OpenMP directives and
49 /// clauses and their data-sharing attributes.
50 class DSAStackTy final {
52 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
57 SourceLocation ImplicitDSALoc;
60 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
64 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
71 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
73 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
75 /// Struct that associates a component with the clause kind where they are
77 struct MappedExprComponentTy {
78 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
79 OpenMPClauseKind Kind = OMPC_unknown;
81 typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy>
82 MappedExprComponentsTy;
83 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
85 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
88 struct SharingMapTy final {
89 DeclSAMapTy SharingMap;
90 AlignedMapTy AlignedMap;
91 MappedExprComponentsTy MappedExprComponents;
92 LoopControlVariablesMapTy LCVMap;
93 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
94 SourceLocation DefaultAttrLoc;
95 OpenMPDirectiveKind Directive = OMPD_unknown;
96 DeclarationNameInfo DirectiveName;
97 Scope *CurScope = nullptr;
98 SourceLocation ConstructLoc;
99 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
100 /// get the data (loop counters etc.) about enclosing loop-based construct.
101 /// This data is required during codegen.
102 DoacrossDependMapTy DoacrossDepends;
103 /// \brief first argument (Expr *) contains optional argument of the
104 /// 'ordered' clause, the second one is true if the regions has 'ordered'
105 /// clause, false otherwise.
106 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
107 bool NowaitRegion = false;
108 bool CancelRegion = false;
109 unsigned AssociatedLoops = 1;
110 SourceLocation InnerTeamsRegionLoc;
111 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
112 Scope *CurScope, SourceLocation Loc)
113 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
118 typedef SmallVector<SharingMapTy, 4> StackTy;
120 /// \brief Stack of used declaration and their data-sharing attributes.
121 DeclSAMapTy Threadprivates;
122 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
123 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
124 /// \brief true, if check for DSA must be from parent directive, false, if
125 /// from current directive.
126 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
128 bool ForceCapturing = false;
129 CriticalsWithHintsTy Criticals;
131 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
133 DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D);
135 /// \brief Checks if the variable is a local for OpenMP region.
136 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
138 bool isStackEmpty() const {
139 return Stack.empty() ||
140 Stack.back().second != CurrentNonCapturingFunctionScope ||
141 Stack.back().first.empty();
145 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
147 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
148 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
150 bool isForceVarCapturing() const { return ForceCapturing; }
151 void setForceVarCapturing(bool V) { ForceCapturing = V; }
153 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
154 Scope *CurScope, SourceLocation Loc) {
156 Stack.back().second != CurrentNonCapturingFunctionScope)
157 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
158 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
159 Stack.back().first.back().DefaultAttrLoc = Loc;
163 assert(!Stack.back().first.empty() &&
164 "Data-sharing attributes stack is empty!");
165 Stack.back().first.pop_back();
168 /// Start new OpenMP region stack in new non-capturing function.
169 void pushFunction() {
170 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
171 assert(!isa<CapturingScopeInfo>(CurFnScope));
172 CurrentNonCapturingFunctionScope = CurFnScope;
174 /// Pop region stack for non-capturing function.
175 void popFunction(const FunctionScopeInfo *OldFSI) {
176 if (!Stack.empty() && Stack.back().second == OldFSI) {
177 assert(Stack.back().first.empty());
180 CurrentNonCapturingFunctionScope = nullptr;
181 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
182 if (!isa<CapturingScopeInfo>(FSI)) {
183 CurrentNonCapturingFunctionScope = FSI;
189 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
190 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
192 const std::pair<OMPCriticalDirective *, llvm::APSInt>
193 getCriticalWithHint(const DeclarationNameInfo &Name) const {
194 auto I = Criticals.find(Name.getAsString());
195 if (I != Criticals.end())
197 return std::make_pair(nullptr, llvm::APSInt());
199 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
200 /// add it and return NULL; otherwise return previous occurrence's expression
202 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
204 /// \brief Register specified variable as loop control variable.
205 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
206 /// \brief Check if the specified variable is a loop control variable for
208 /// \return The index of the loop control variable in the list of associated
209 /// for-loops (from outer to inner).
210 LCDeclInfo isLoopControlVariable(ValueDecl *D);
211 /// \brief Check if the specified variable is a loop control variable for
213 /// \return The index of the loop control variable in the list of associated
214 /// for-loops (from outer to inner).
215 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
216 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
217 /// parent directive.
218 ValueDecl *getParentLoopControlVariable(unsigned I);
220 /// \brief Adds explicit data sharing attribute to the specified declaration.
221 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
222 DeclRefExpr *PrivateCopy = nullptr);
224 /// \brief Returns data sharing attributes from top of the stack for the
225 /// specified declaration.
226 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
227 /// \brief Returns data-sharing attributes for the specified declaration.
228 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
229 /// \brief Checks if the specified variables has data-sharing attributes which
230 /// match specified \a CPred predicate in any directive which matches \a DPred
232 DSAVarData hasDSA(ValueDecl *D,
233 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
234 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
236 /// \brief Checks if the specified variables has data-sharing attributes which
237 /// match specified \a CPred predicate in any innermost directive which
238 /// matches \a DPred predicate.
240 hasInnermostDSA(ValueDecl *D,
241 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
242 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
244 /// \brief Checks if the specified variables has explicit data-sharing
245 /// attributes which match specified \a CPred predicate at the specified
247 bool hasExplicitDSA(ValueDecl *D,
248 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
249 unsigned Level, bool NotLastprivate = false);
251 /// \brief Returns true if the directive at level \Level matches in the
252 /// specified \a DPred predicate.
253 bool hasExplicitDirective(
254 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
257 /// \brief Finds a directive which matches specified \a DPred predicate.
258 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
259 const DeclarationNameInfo &,
260 SourceLocation)> &DPred,
263 /// \brief Returns currently analyzed directive.
264 OpenMPDirectiveKind getCurrentDirective() const {
265 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
267 /// \brief Returns parent directive.
268 OpenMPDirectiveKind getParentDirective() const {
269 if (isStackEmpty() || Stack.back().first.size() == 1)
271 return std::next(Stack.back().first.rbegin())->Directive;
274 /// \brief Set default data sharing attribute to none.
275 void setDefaultDSANone(SourceLocation Loc) {
276 assert(!isStackEmpty());
277 Stack.back().first.back().DefaultAttr = DSA_none;
278 Stack.back().first.back().DefaultAttrLoc = Loc;
280 /// \brief Set default data sharing attribute to shared.
281 void setDefaultDSAShared(SourceLocation Loc) {
282 assert(!isStackEmpty());
283 Stack.back().first.back().DefaultAttr = DSA_shared;
284 Stack.back().first.back().DefaultAttrLoc = Loc;
287 DefaultDataSharingAttributes getDefaultDSA() const {
288 return isStackEmpty() ? DSA_unspecified
289 : Stack.back().first.back().DefaultAttr;
291 SourceLocation getDefaultDSALocation() const {
292 return isStackEmpty() ? SourceLocation()
293 : Stack.back().first.back().DefaultAttrLoc;
296 /// \brief Checks if the specified variable is a threadprivate.
297 bool isThreadPrivate(VarDecl *D) {
298 DSAVarData DVar = getTopDSA(D, false);
299 return isOpenMPThreadPrivate(DVar.CKind);
302 /// \brief Marks current region as ordered (it has an 'ordered' clause).
303 void setOrderedRegion(bool IsOrdered, Expr *Param) {
304 assert(!isStackEmpty());
305 Stack.back().first.back().OrderedRegion.setInt(IsOrdered);
306 Stack.back().first.back().OrderedRegion.setPointer(Param);
308 /// \brief Returns true, if parent region is ordered (has associated
309 /// 'ordered' clause), false - otherwise.
310 bool isParentOrderedRegion() const {
311 if (isStackEmpty() || Stack.back().first.size() == 1)
313 return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt();
315 /// \brief Returns optional parameter for the ordered region.
316 Expr *getParentOrderedRegionParam() const {
317 if (isStackEmpty() || Stack.back().first.size() == 1)
319 return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer();
321 /// \brief Marks current region as nowait (it has a 'nowait' clause).
322 void setNowaitRegion(bool IsNowait = true) {
323 assert(!isStackEmpty());
324 Stack.back().first.back().NowaitRegion = IsNowait;
326 /// \brief Returns true, if parent region is nowait (has associated
327 /// 'nowait' clause), false - otherwise.
328 bool isParentNowaitRegion() const {
329 if (isStackEmpty() || Stack.back().first.size() == 1)
331 return std::next(Stack.back().first.rbegin())->NowaitRegion;
333 /// \brief Marks parent region as cancel region.
334 void setParentCancelRegion(bool Cancel = true) {
335 if (!isStackEmpty() && Stack.back().first.size() > 1) {
336 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
337 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
340 /// \brief Return true if current region has inner cancel construct.
341 bool isCancelRegion() const {
342 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
345 /// \brief Set collapse value for the region.
346 void setAssociatedLoops(unsigned Val) {
347 assert(!isStackEmpty());
348 Stack.back().first.back().AssociatedLoops = Val;
350 /// \brief Return collapse value for region.
351 unsigned getAssociatedLoops() const {
352 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
355 /// \brief Marks current target region as one with closely nested teams
357 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
358 if (!isStackEmpty() && Stack.back().first.size() > 1) {
359 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
363 /// \brief Returns true, if current region has closely nested teams region.
364 bool hasInnerTeamsRegion() const {
365 return getInnerTeamsRegionLoc().isValid();
367 /// \brief Returns location of the nested teams region (if any).
368 SourceLocation getInnerTeamsRegionLoc() const {
369 return isStackEmpty() ? SourceLocation()
370 : Stack.back().first.back().InnerTeamsRegionLoc;
373 Scope *getCurScope() const {
374 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
376 Scope *getCurScope() {
377 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
379 SourceLocation getConstructLoc() {
380 return isStackEmpty() ? SourceLocation()
381 : Stack.back().first.back().ConstructLoc;
384 /// Do the check specified in \a Check to all component lists and return true
385 /// if any issue is found.
386 bool checkMappableExprComponentListsForDecl(
387 ValueDecl *VD, bool CurrentRegionOnly,
388 const llvm::function_ref<
389 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
390 OpenMPClauseKind)> &Check) {
393 auto SI = Stack.back().first.rbegin();
394 auto SE = Stack.back().first.rend();
399 if (CurrentRegionOnly) {
405 for (; SI != SE; ++SI) {
406 auto MI = SI->MappedExprComponents.find(VD);
407 if (MI != SI->MappedExprComponents.end())
408 for (auto &L : MI->second.Components)
409 if (Check(L, MI->second.Kind))
415 /// Do the check specified in \a Check to all component lists at a given level
416 /// and return true if any issue is found.
417 bool checkMappableExprComponentListsForDeclAtLevel(
418 ValueDecl *VD, unsigned Level,
419 const llvm::function_ref<
420 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
421 OpenMPClauseKind)> &Check) {
425 auto StartI = Stack.back().first.begin();
426 auto EndI = Stack.back().first.end();
427 if (std::distance(StartI, EndI) <= (int)Level)
429 std::advance(StartI, Level);
431 auto MI = StartI->MappedExprComponents.find(VD);
432 if (MI != StartI->MappedExprComponents.end())
433 for (auto &L : MI->second.Components)
434 if (Check(L, MI->second.Kind))
439 /// Create a new mappable expression component list associated with a given
440 /// declaration and initialize it with the provided list of components.
441 void addMappableExpressionComponents(
443 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
444 OpenMPClauseKind WhereFoundClauseKind) {
445 assert(!isStackEmpty() &&
446 "Not expecting to retrieve components from a empty stack!");
447 auto &MEC = Stack.back().first.back().MappedExprComponents[VD];
448 // Create new entry and append the new components there.
449 MEC.Components.resize(MEC.Components.size() + 1);
450 MEC.Components.back().append(Components.begin(), Components.end());
451 MEC.Kind = WhereFoundClauseKind;
454 unsigned getNestingLevel() const {
455 assert(!isStackEmpty());
456 return Stack.back().first.size() - 1;
458 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
459 assert(!isStackEmpty() && Stack.back().first.size() > 1);
460 auto &StackElem = *std::next(Stack.back().first.rbegin());
461 assert(isOpenMPWorksharingDirective(StackElem.Directive));
462 StackElem.DoacrossDepends.insert({C, OpsOffs});
464 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
465 getDoacrossDependClauses() const {
466 assert(!isStackEmpty());
467 auto &StackElem = Stack.back().first.back();
468 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
469 auto &Ref = StackElem.DoacrossDepends;
470 return llvm::make_range(Ref.begin(), Ref.end());
472 return llvm::make_range(StackElem.DoacrossDepends.end(),
473 StackElem.DoacrossDepends.end());
476 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
477 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
478 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
482 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
483 auto *VD = dyn_cast<VarDecl>(D);
484 auto *FD = dyn_cast<FieldDecl>(D);
486 VD = VD->getCanonicalDecl();
490 FD = FD->getCanonicalDecl();
496 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter,
498 D = getCanonicalDecl(D);
499 auto *VD = dyn_cast<VarDecl>(D);
500 auto *FD = dyn_cast<FieldDecl>(D);
502 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
503 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
504 // in a region but not in construct]
505 // File-scope or namespace-scope variables referenced in called routines
506 // in the region are shared unless they appear in a threadprivate
508 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
509 DVar.CKind = OMPC_shared;
511 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
512 // in a region but not in construct]
513 // Variables with static storage duration that are declared in called
514 // routines in the region are shared.
515 if (VD && VD->hasGlobalStorage())
516 DVar.CKind = OMPC_shared;
518 // Non-static data members are shared by default.
520 DVar.CKind = OMPC_shared;
525 DVar.DKind = Iter->Directive;
526 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
527 // in a Construct, C/C++, predetermined, p.1]
528 // Variables with automatic storage duration that are declared in a scope
529 // inside the construct are private.
530 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
531 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
532 DVar.CKind = OMPC_private;
536 // Explicitly specified attributes and local variables with predetermined
538 if (Iter->SharingMap.count(D)) {
539 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
540 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
541 DVar.CKind = Iter->SharingMap[D].Attributes;
542 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
546 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
547 // in a Construct, C/C++, implicitly determined, p.1]
548 // In a parallel or task construct, the data-sharing attributes of these
549 // variables are determined by the default clause, if present.
550 switch (Iter->DefaultAttr) {
552 DVar.CKind = OMPC_shared;
553 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
557 case DSA_unspecified:
558 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
559 // in a Construct, implicitly determined, p.2]
560 // In a parallel construct, if no default clause is present, these
561 // variables are shared.
562 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
563 if (isOpenMPParallelDirective(DVar.DKind) ||
564 isOpenMPTeamsDirective(DVar.DKind)) {
565 DVar.CKind = OMPC_shared;
569 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
570 // in a Construct, implicitly determined, p.4]
571 // In a task construct, if no default clause is present, a variable that in
572 // the enclosing context is determined to be shared by all implicit tasks
573 // bound to the current team is shared.
574 if (isOpenMPTaskingDirective(DVar.DKind)) {
576 auto I = Iter, E = Stack.back().first.rend();
579 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
580 // Referenced in a Construct, implicitly determined, p.6]
581 // In a task construct, if no default clause is present, a variable
582 // whose data-sharing attribute is not determined by the rules above is
584 DVarTemp = getDSA(I, D);
585 if (DVarTemp.CKind != OMPC_shared) {
586 DVar.RefExpr = nullptr;
587 DVar.CKind = OMPC_firstprivate;
590 } while (I != E && !isParallelOrTaskRegion(I->Directive));
592 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
596 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
597 // in a Construct, implicitly determined, p.3]
598 // For constructs other than task, if no default clause is present, these
599 // variables inherit their data-sharing attributes from the enclosing
601 return getDSA(++Iter, D);
604 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
605 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
606 D = getCanonicalDecl(D);
607 auto &StackElem = Stack.back().first.back();
608 auto It = StackElem.AlignedMap.find(D);
609 if (It == StackElem.AlignedMap.end()) {
610 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
611 StackElem.AlignedMap[D] = NewDE;
614 assert(It->second && "Unexpected nullptr expr in the aligned map");
620 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
621 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
622 D = getCanonicalDecl(D);
623 auto &StackElem = Stack.back().first.back();
624 StackElem.LCVMap.insert(
625 {D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)});
628 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
629 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
630 D = getCanonicalDecl(D);
631 auto &StackElem = Stack.back().first.back();
632 auto It = StackElem.LCVMap.find(D);
633 if (It != StackElem.LCVMap.end())
638 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
639 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
640 "Data-sharing attributes stack is empty");
641 D = getCanonicalDecl(D);
642 auto &StackElem = *std::next(Stack.back().first.rbegin());
643 auto It = StackElem.LCVMap.find(D);
644 if (It != StackElem.LCVMap.end())
649 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
650 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
651 "Data-sharing attributes stack is empty");
652 auto &StackElem = *std::next(Stack.back().first.rbegin());
653 if (StackElem.LCVMap.size() < I)
655 for (auto &Pair : StackElem.LCVMap)
656 if (Pair.second.first == I)
661 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
662 DeclRefExpr *PrivateCopy) {
663 D = getCanonicalDecl(D);
664 if (A == OMPC_threadprivate) {
665 auto &Data = Threadprivates[D];
667 Data.RefExpr.setPointer(E);
668 Data.PrivateCopy = nullptr;
670 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
671 auto &Data = Stack.back().first.back().SharingMap[D];
672 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
673 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
674 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
675 (isLoopControlVariable(D).first && A == OMPC_private));
676 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
677 Data.RefExpr.setInt(/*IntVal=*/true);
680 const bool IsLastprivate =
681 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
683 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
684 Data.PrivateCopy = PrivateCopy;
686 auto &Data = Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
688 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
689 Data.PrivateCopy = nullptr;
694 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
695 D = D->getCanonicalDecl();
696 if (!isStackEmpty() && Stack.back().first.size() > 1) {
697 reverse_iterator I = Iter, E = Stack.back().first.rend();
698 Scope *TopScope = nullptr;
699 while (I != E && !isParallelOrTaskRegion(I->Directive))
703 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
704 Scope *CurScope = getCurScope();
705 while (CurScope != TopScope && !CurScope->isDeclScope(D))
706 CurScope = CurScope->getParent();
707 return CurScope != TopScope;
712 /// \brief Build a variable declaration for OpenMP loop iteration variable.
713 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
714 StringRef Name, const AttrVec *Attrs = nullptr) {
715 DeclContext *DC = SemaRef.CurContext;
716 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
717 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
719 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
721 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
729 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
731 bool RefersToCapture = false) {
733 D->markUsed(S.Context);
734 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
735 SourceLocation(), D, RefersToCapture, Loc, Ty,
739 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
740 D = getCanonicalDecl(D);
743 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
744 // in a Construct, C/C++, predetermined, p.1]
745 // Variables appearing in threadprivate directives are threadprivate.
746 auto *VD = dyn_cast<VarDecl>(D);
747 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
748 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
749 SemaRef.getLangOpts().OpenMPUseTLS &&
750 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
751 (VD && VD->getStorageClass() == SC_Register &&
752 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
753 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
757 auto TI = Threadprivates.find(D);
758 if (TI != Threadprivates.end()) {
759 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
760 DVar.CKind = OMPC_threadprivate;
765 // Not in OpenMP execution region and top scope was already checked.
768 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
769 // in a Construct, C/C++, predetermined, p.4]
770 // Static data members are shared.
771 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
772 // in a Construct, C/C++, predetermined, p.7]
773 // Variables with static storage duration that are declared in a scope
774 // inside the construct are shared.
775 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
776 if (VD && VD->isStaticDataMember()) {
777 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
778 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
781 DVar.CKind = OMPC_shared;
785 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
786 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
787 Type = SemaRef.getASTContext().getBaseElementType(Type);
788 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
789 // in a Construct, C/C++, predetermined, p.6]
790 // Variables with const qualified type having no mutable member are
793 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
794 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
795 if (auto *CTD = CTSD->getSpecializedTemplate())
796 RD = CTD->getTemplatedDecl();
798 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
799 RD->hasMutableFields())) {
800 // Variables with const-qualified type having no mutable member may be
801 // listed in a firstprivate clause, even if they are static data members.
802 DSAVarData DVarTemp = hasDSA(
803 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
804 MatchesAlways, FromParent);
805 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
808 DVar.CKind = OMPC_shared;
812 // Explicitly specified attributes and local variables with predetermined
814 auto StartI = std::next(Stack.back().first.rbegin());
815 auto EndI = Stack.back().first.rend();
816 if (FromParent && StartI != EndI)
817 StartI = std::next(StartI);
818 auto I = std::prev(StartI);
819 if (I->SharingMap.count(D)) {
820 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
821 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
822 DVar.CKind = I->SharingMap[D].Attributes;
823 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
829 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
831 if (isStackEmpty()) {
832 StackTy::reverse_iterator I;
835 D = getCanonicalDecl(D);
836 auto StartI = Stack.back().first.rbegin();
837 auto EndI = Stack.back().first.rend();
838 if (FromParent && StartI != EndI)
839 StartI = std::next(StartI);
840 return getDSA(StartI, D);
843 DSAStackTy::DSAVarData
844 DSAStackTy::hasDSA(ValueDecl *D,
845 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
846 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
850 D = getCanonicalDecl(D);
851 auto I = (FromParent && Stack.back().first.size() > 1)
852 ? std::next(Stack.back().first.rbegin())
853 : Stack.back().first.rbegin();
854 auto EndI = Stack.back().first.rend();
855 while (std::distance(I, EndI) > 1) {
857 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
859 DSAVarData DVar = getDSA(I, D);
860 if (CPred(DVar.CKind))
866 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
867 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
868 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
872 D = getCanonicalDecl(D);
873 auto StartI = std::next(Stack.back().first.rbegin());
874 auto EndI = Stack.back().first.rend();
875 if (FromParent && StartI != EndI)
876 StartI = std::next(StartI);
877 if (StartI == EndI || !DPred(StartI->Directive))
879 DSAVarData DVar = getDSA(StartI, D);
880 return CPred(DVar.CKind) ? DVar : DSAVarData();
883 bool DSAStackTy::hasExplicitDSA(
884 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
885 unsigned Level, bool NotLastprivate) {
886 if (CPred(ClauseKindMode))
890 D = getCanonicalDecl(D);
891 auto StartI = Stack.back().first.begin();
892 auto EndI = Stack.back().first.end();
893 if (std::distance(StartI, EndI) <= (int)Level)
895 std::advance(StartI, Level);
896 return (StartI->SharingMap.count(D) > 0) &&
897 StartI->SharingMap[D].RefExpr.getPointer() &&
898 CPred(StartI->SharingMap[D].Attributes) &&
899 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
902 bool DSAStackTy::hasExplicitDirective(
903 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
907 auto StartI = Stack.back().first.begin();
908 auto EndI = Stack.back().first.end();
909 if (std::distance(StartI, EndI) <= (int)Level)
911 std::advance(StartI, Level);
912 return DPred(StartI->Directive);
915 bool DSAStackTy::hasDirective(
916 const llvm::function_ref<bool(OpenMPDirectiveKind,
917 const DeclarationNameInfo &, SourceLocation)>
920 // We look only in the enclosing region.
923 auto StartI = std::next(Stack.back().first.rbegin());
924 auto EndI = Stack.back().first.rend();
925 if (FromParent && StartI != EndI)
926 StartI = std::next(StartI);
927 for (auto I = StartI, EE = EndI; I != EE; ++I) {
928 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
934 void Sema::InitDataSharingAttributesStack() {
935 VarDataSharingAttributesStack = new DSAStackTy(*this);
938 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
940 void Sema::pushOpenMPFunctionRegion() {
941 DSAStack->pushFunction();
944 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
945 DSAStack->popFunction(OldFSI);
948 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
949 assert(LangOpts.OpenMP && "OpenMP is not allowed");
951 auto &Ctx = getASTContext();
954 // Find the directive that is associated with the provided scope.
955 auto Ty = D->getType();
957 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
958 // This table summarizes how a given variable should be passed to the device
959 // given its type and the clauses where it appears. This table is based on
960 // the description in OpenMP 4.5 [2.10.4, target Construct] and
961 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
963 // =========================================================================
964 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
965 // | |(tofrom:scalar)| | pvt | | | |
966 // =========================================================================
967 // | scl | | | | - | | bycopy|
968 // | scl | | - | x | - | - | bycopy|
969 // | scl | | x | - | - | - | null |
970 // | scl | x | | | - | | byref |
971 // | scl | x | - | x | - | - | bycopy|
972 // | scl | x | x | - | - | - | null |
973 // | scl | | - | - | - | x | byref |
974 // | scl | x | - | - | - | x | byref |
976 // | agg | n.a. | | | - | | byref |
977 // | agg | n.a. | - | x | - | - | byref |
978 // | agg | n.a. | x | - | - | - | null |
979 // | agg | n.a. | - | - | - | x | byref |
980 // | agg | n.a. | - | - | - | x[] | byref |
982 // | ptr | n.a. | | | - | | bycopy|
983 // | ptr | n.a. | - | x | - | - | bycopy|
984 // | ptr | n.a. | x | - | - | - | null |
985 // | ptr | n.a. | - | - | - | x | byref |
986 // | ptr | n.a. | - | - | - | x[] | bycopy|
987 // | ptr | n.a. | - | - | x | | bycopy|
988 // | ptr | n.a. | - | - | x | x | bycopy|
989 // | ptr | n.a. | - | - | x | x[] | bycopy|
990 // =========================================================================
996 // - - invalid in this combination
997 // [] - mapped with an array section
998 // byref - should be mapped by reference
999 // byval - should be mapped by value
1000 // null - initialize a local variable to null on the device
1003 // - All scalar declarations that show up in a map clause have to be passed
1004 // by reference, because they may have been mapped in the enclosing data
1006 // - If the scalar value does not fit the size of uintptr, it has to be
1007 // passed by reference, regardless the result in the table above.
1008 // - For pointers mapped by value that have either an implicit map or an
1009 // array section, the runtime library may pass the NULL value to the
1010 // device instead of the value passed to it by the compiler.
1012 if (Ty->isReferenceType())
1013 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1015 // Locate map clauses and see if the variable being captured is referred to
1016 // in any of those clauses. Here we only care about variables, not fields,
1017 // because fields are part of aggregates.
1018 bool IsVariableUsedInMapClause = false;
1019 bool IsVariableAssociatedWithSection = false;
1021 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1022 D, Level, [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
1024 OpenMPClauseKind WhereFoundClauseKind) {
1025 // Only the map clause information influences how a variable is
1026 // captured. E.g. is_device_ptr does not require changing the default
1028 if (WhereFoundClauseKind != OMPC_map)
1031 auto EI = MapExprComponents.rbegin();
1032 auto EE = MapExprComponents.rend();
1034 assert(EI != EE && "Invalid map expression!");
1036 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1037 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1043 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1044 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1045 isa<MemberExpr>(EI->getAssociatedExpression())) {
1046 IsVariableAssociatedWithSection = true;
1047 // There is nothing more we need to know about this variable.
1051 // Keep looking for more map info.
1055 if (IsVariableUsedInMapClause) {
1056 // If variable is identified in a map clause it is always captured by
1057 // reference except if it is a pointer that is dereferenced somehow.
1058 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1060 // By default, all the data that has a scalar type is mapped by copy.
1061 IsByRef = !Ty->isScalarType();
1065 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1066 IsByRef = !DSAStack->hasExplicitDSA(
1067 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1068 Level, /*NotLastprivate=*/true);
1071 // When passing data by copy, we need to make sure it fits the uintptr size
1072 // and alignment, because the runtime library only deals with uintptr types.
1073 // If it does not fit the uintptr size, we need to pass the data by reference
1076 (Ctx.getTypeSizeInChars(Ty) >
1077 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1078 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1085 unsigned Sema::getOpenMPNestingLevel() const {
1086 assert(getLangOpts().OpenMP);
1087 return DSAStack->getNestingLevel();
1090 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
1091 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1092 D = getCanonicalDecl(D);
1094 // If we are attempting to capture a global variable in a directive with
1095 // 'target' we return true so that this global is also mapped to the device.
1097 // FIXME: If the declaration is enclosed in a 'declare target' directive,
1098 // then it should not be captured. Therefore, an extra check has to be
1099 // inserted here once support for 'declare target' is added.
1101 auto *VD = dyn_cast<VarDecl>(D);
1102 if (VD && !VD->hasLocalStorage()) {
1103 if (DSAStack->getCurrentDirective() == OMPD_target &&
1104 !DSAStack->isClauseParsingMode())
1106 if (DSAStack->hasDirective(
1107 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1108 SourceLocation) -> bool {
1109 return isOpenMPTargetExecutionDirective(K);
1115 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1116 (!DSAStack->isClauseParsingMode() ||
1117 DSAStack->getParentDirective() != OMPD_unknown)) {
1118 auto &&Info = DSAStack->isLoopControlVariable(D);
1120 (VD && VD->hasLocalStorage() &&
1121 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
1122 (VD && DSAStack->isForceVarCapturing()))
1123 return VD ? VD : Info.second;
1124 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1125 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1126 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1127 DVarPrivate = DSAStack->hasDSA(
1128 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1129 DSAStack->isClauseParsingMode());
1130 if (DVarPrivate.CKind != OMPC_unknown)
1131 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1136 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
1137 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1138 return DSAStack->hasExplicitDSA(
1139 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
1142 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
1143 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1144 // Return true if the current level is no longer enclosed in a target region.
1146 auto *VD = dyn_cast<VarDecl>(D);
1147 return VD && !VD->hasLocalStorage() &&
1148 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1152 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1154 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1155 const DeclarationNameInfo &DirName,
1156 Scope *CurScope, SourceLocation Loc) {
1157 DSAStack->push(DKind, DirName, CurScope, Loc);
1158 PushExpressionEvaluationContext(
1159 ExpressionEvaluationContext::PotentiallyEvaluated);
1162 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1163 DSAStack->setClauseParsingMode(K);
1166 void Sema::EndOpenMPClause() {
1167 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1170 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1171 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1172 // A variable of class type (or array thereof) that appears in a lastprivate
1173 // clause requires an accessible, unambiguous default constructor for the
1174 // class type, unless the list item is also specified in a firstprivate
1176 if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1177 for (auto *C : D->clauses()) {
1178 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1179 SmallVector<Expr *, 8> PrivateCopies;
1180 for (auto *DE : Clause->varlists()) {
1181 if (DE->isValueDependent() || DE->isTypeDependent()) {
1182 PrivateCopies.push_back(nullptr);
1185 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1186 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1187 QualType Type = VD->getType().getNonReferenceType();
1188 auto DVar = DSAStack->getTopDSA(VD, false);
1189 if (DVar.CKind == OMPC_lastprivate) {
1190 // Generate helper private variable and initialize it with the
1191 // default value. The address of the original variable is replaced
1192 // by the address of the new private variable in CodeGen. This new
1193 // variable is not added to IdResolver, so the code in the OpenMP
1194 // region uses original variable for proper diagnostics.
1195 auto *VDPrivate = buildVarDecl(
1196 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1197 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
1198 ActOnUninitializedDecl(VDPrivate);
1199 if (VDPrivate->isInvalidDecl())
1201 PrivateCopies.push_back(buildDeclRefExpr(
1202 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1204 // The variable is also a firstprivate, so initialization sequence
1205 // for private copy is generated already.
1206 PrivateCopies.push_back(nullptr);
1209 // Set initializers to private copies if no errors were found.
1210 if (PrivateCopies.size() == Clause->varlist_size())
1211 Clause->setPrivateCopies(PrivateCopies);
1217 DiscardCleanupsInEvaluationContext();
1218 PopExpressionEvaluationContext();
1221 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1222 Expr *NumIterations, Sema &SemaRef,
1223 Scope *S, DSAStackTy *Stack);
1227 class VarDeclFilterCCC : public CorrectionCandidateCallback {
1232 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1233 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1234 NamedDecl *ND = Candidate.getCorrectionDecl();
1235 if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1236 return VD->hasGlobalStorage() &&
1237 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1238 SemaRef.getCurScope());
1244 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1249 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1250 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1251 NamedDecl *ND = Candidate.getCorrectionDecl();
1252 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1253 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1254 SemaRef.getCurScope());
1262 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1263 CXXScopeSpec &ScopeSpec,
1264 const DeclarationNameInfo &Id) {
1265 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1266 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1268 if (Lookup.isAmbiguous())
1272 if (!Lookup.isSingleResult()) {
1273 if (TypoCorrection Corrected = CorrectTypo(
1274 Id, LookupOrdinaryName, CurScope, nullptr,
1275 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1276 diagnoseTypo(Corrected,
1277 PDiag(Lookup.empty()
1278 ? diag::err_undeclared_var_use_suggest
1279 : diag::err_omp_expected_var_arg_suggest)
1281 VD = Corrected.getCorrectionDeclAs<VarDecl>();
1283 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1284 : diag::err_omp_expected_var_arg)
1289 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1290 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1291 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1295 Lookup.suppressDiagnostics();
1297 // OpenMP [2.9.2, Syntax, C/C++]
1298 // Variables must be file-scope, namespace-scope, or static block-scope.
1299 if (!VD->hasGlobalStorage()) {
1300 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1301 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1303 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1304 Diag(VD->getLocation(),
1305 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1310 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1311 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
1312 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1313 // A threadprivate directive for file-scope variables must appear outside
1314 // any definition or declaration.
1315 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1316 !getCurLexicalContext()->isTranslationUnit()) {
1317 Diag(Id.getLoc(), diag::err_omp_var_scope)
1318 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1320 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1321 Diag(VD->getLocation(),
1322 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1326 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1327 // A threadprivate directive for static class member variables must appear
1328 // in the class definition, in the same scope in which the member
1329 // variables are declared.
1330 if (CanonicalVD->isStaticDataMember() &&
1331 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1332 Diag(Id.getLoc(), diag::err_omp_var_scope)
1333 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1335 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1336 Diag(VD->getLocation(),
1337 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1341 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1342 // A threadprivate directive for namespace-scope variables must appear
1343 // outside any definition or declaration other than the namespace
1344 // definition itself.
1345 if (CanonicalVD->getDeclContext()->isNamespace() &&
1346 (!getCurLexicalContext()->isFileContext() ||
1347 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1348 Diag(Id.getLoc(), diag::err_omp_var_scope)
1349 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1351 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1352 Diag(VD->getLocation(),
1353 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1357 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1358 // A threadprivate directive for static block-scope variables must appear
1359 // in the scope of the variable and not in a nested scope.
1360 if (CanonicalVD->isStaticLocal() && CurScope &&
1361 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1362 Diag(Id.getLoc(), diag::err_omp_var_scope)
1363 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1365 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1366 Diag(VD->getLocation(),
1367 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1372 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1373 // A threadprivate directive must lexically precede all references to any
1374 // of the variables in its list.
1375 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1376 Diag(Id.getLoc(), diag::err_omp_var_used)
1377 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1381 QualType ExprType = VD->getType().getNonReferenceType();
1382 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1383 SourceLocation(), VD,
1384 /*RefersToEnclosingVariableOrCapture=*/false,
1385 Id.getLoc(), ExprType, VK_LValue);
1388 Sema::DeclGroupPtrTy
1389 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1390 ArrayRef<Expr *> VarList) {
1391 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1392 CurContext->addDecl(D);
1393 return DeclGroupPtrTy::make(DeclGroupRef(D));
1399 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1403 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1404 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1405 if (VD->hasLocalStorage()) {
1406 SemaRef.Diag(E->getLocStart(),
1407 diag::err_omp_local_var_in_threadprivate_init)
1408 << E->getSourceRange();
1409 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1410 << VD << VD->getSourceRange();
1416 bool VisitStmt(const Stmt *S) {
1417 for (auto Child : S->children()) {
1418 if (Child && Visit(Child))
1423 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1427 OMPThreadPrivateDecl *
1428 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1429 SmallVector<Expr *, 8> Vars;
1430 for (auto &RefExpr : VarList) {
1431 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1432 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1433 SourceLocation ILoc = DE->getExprLoc();
1435 // Mark variable as used.
1436 VD->setReferenced();
1437 VD->markUsed(Context);
1439 QualType QType = VD->getType();
1440 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1441 // It will be analyzed later.
1446 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1447 // A threadprivate variable must not have an incomplete type.
1448 if (RequireCompleteType(ILoc, VD->getType(),
1449 diag::err_omp_threadprivate_incomplete_type)) {
1453 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1454 // A threadprivate variable must not have a reference type.
1455 if (VD->getType()->isReferenceType()) {
1456 Diag(ILoc, diag::err_omp_ref_type_arg)
1457 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1459 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1460 Diag(VD->getLocation(),
1461 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1466 // Check if this is a TLS variable. If TLS is not being supported, produce
1467 // the corresponding diagnostic.
1468 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1469 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1470 getLangOpts().OpenMPUseTLS &&
1471 getASTContext().getTargetInfo().isTLSSupported())) ||
1472 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1473 !VD->isLocalVarDecl())) {
1474 Diag(ILoc, diag::err_omp_var_thread_local)
1475 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1477 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1478 Diag(VD->getLocation(),
1479 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1484 // Check if initial value of threadprivate variable reference variable with
1485 // local storage (it is not supported by runtime).
1486 if (auto Init = VD->getAnyInitializer()) {
1487 LocalVarRefChecker Checker(*this);
1488 if (Checker.Visit(Init))
1492 Vars.push_back(RefExpr);
1493 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1494 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1495 Context, SourceRange(Loc, Loc)));
1496 if (auto *ML = Context.getASTMutationListener())
1497 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1499 OMPThreadPrivateDecl *D = nullptr;
1500 if (!Vars.empty()) {
1501 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1503 D->setAccess(AS_public);
1508 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1509 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1510 bool IsLoopIterVar = false) {
1512 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1513 << getOpenMPClauseName(DVar.CKind);
1517 PDSA_StaticMemberShared,
1518 PDSA_StaticLocalVarShared,
1519 PDSA_LoopIterVarPrivate,
1520 PDSA_LoopIterVarLinear,
1521 PDSA_LoopIterVarLastprivate,
1522 PDSA_ConstVarShared,
1523 PDSA_GlobalVarShared,
1524 PDSA_TaskVarFirstprivate,
1525 PDSA_LocalVarPrivate,
1527 } Reason = PDSA_Implicit;
1528 bool ReportHint = false;
1529 auto ReportLoc = D->getLocation();
1530 auto *VD = dyn_cast<VarDecl>(D);
1531 if (IsLoopIterVar) {
1532 if (DVar.CKind == OMPC_private)
1533 Reason = PDSA_LoopIterVarPrivate;
1534 else if (DVar.CKind == OMPC_lastprivate)
1535 Reason = PDSA_LoopIterVarLastprivate;
1537 Reason = PDSA_LoopIterVarLinear;
1538 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1539 DVar.CKind == OMPC_firstprivate) {
1540 Reason = PDSA_TaskVarFirstprivate;
1541 ReportLoc = DVar.ImplicitDSALoc;
1542 } else if (VD && VD->isStaticLocal())
1543 Reason = PDSA_StaticLocalVarShared;
1544 else if (VD && VD->isStaticDataMember())
1545 Reason = PDSA_StaticMemberShared;
1546 else if (VD && VD->isFileVarDecl())
1547 Reason = PDSA_GlobalVarShared;
1548 else if (D->getType().isConstant(SemaRef.getASTContext()))
1549 Reason = PDSA_ConstVarShared;
1550 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1552 Reason = PDSA_LocalVarPrivate;
1554 if (Reason != PDSA_Implicit) {
1555 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1556 << Reason << ReportHint
1557 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1558 } else if (DVar.ImplicitDSALoc.isValid()) {
1559 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1560 << getOpenMPClauseName(DVar.CKind);
1565 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1570 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1571 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1574 void VisitDeclRefExpr(DeclRefExpr *E) {
1575 if (E->isTypeDependent() || E->isValueDependent() ||
1576 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1578 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1579 // Skip internally declared variables.
1580 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1583 auto DVar = Stack->getTopDSA(VD, false);
1584 // Check if the variable has explicit DSA set and stop analysis if it so.
1588 auto ELoc = E->getExprLoc();
1589 auto DKind = Stack->getCurrentDirective();
1590 // The default(none) clause requires that each variable that is referenced
1591 // in the construct, and does not have a predetermined data-sharing
1592 // attribute, must have its data-sharing attribute explicitly determined
1593 // by being listed in a data-sharing attribute clause.
1594 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1595 isParallelOrTaskRegion(DKind) &&
1596 VarsWithInheritedDSA.count(VD) == 0) {
1597 VarsWithInheritedDSA[VD] = E;
1601 // OpenMP [2.9.3.6, Restrictions, p.2]
1602 // A list item that appears in a reduction clause of the innermost
1603 // enclosing worksharing or parallel construct may not be accessed in an
1605 DVar = Stack->hasInnermostDSA(
1606 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1607 [](OpenMPDirectiveKind K) -> bool {
1608 return isOpenMPParallelDirective(K) ||
1609 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1612 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1614 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1615 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1619 // Define implicit data-sharing attributes for task.
1620 DVar = Stack->getImplicitDSA(VD, false);
1621 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1622 !Stack->isLoopControlVariable(VD).first)
1623 ImplicitFirstprivate.push_back(E);
1626 void VisitMemberExpr(MemberExpr *E) {
1627 if (E->isTypeDependent() || E->isValueDependent() ||
1628 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1630 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1631 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1632 auto DVar = Stack->getTopDSA(FD, false);
1633 // Check if the variable has explicit DSA set and stop analysis if it
1638 auto ELoc = E->getExprLoc();
1639 auto DKind = Stack->getCurrentDirective();
1640 // OpenMP [2.9.3.6, Restrictions, p.2]
1641 // A list item that appears in a reduction clause of the innermost
1642 // enclosing worksharing or parallel construct may not be accessed in
1643 // an explicit task.
1644 DVar = Stack->hasInnermostDSA(
1645 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1646 [](OpenMPDirectiveKind K) -> bool {
1647 return isOpenMPParallelDirective(K) ||
1648 isOpenMPWorksharingDirective(K) ||
1649 isOpenMPTeamsDirective(K);
1652 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1654 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1655 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1659 // Define implicit data-sharing attributes for task.
1660 DVar = Stack->getImplicitDSA(FD, false);
1661 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1662 !Stack->isLoopControlVariable(FD).first)
1663 ImplicitFirstprivate.push_back(E);
1666 Visit(E->getBase());
1668 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1669 for (auto *C : S->clauses()) {
1670 // Skip analysis of arguments of implicitly defined firstprivate clause
1671 // for task directives.
1672 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1673 for (auto *CC : C->children()) {
1679 void VisitStmt(Stmt *S) {
1680 for (auto *C : S->children()) {
1681 if (C && !isa<OMPExecutableDirective>(C))
1686 bool isErrorFound() { return ErrorFound; }
1687 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
1688 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
1689 return VarsWithInheritedDSA;
1692 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1693 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1697 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1700 case OMPD_parallel_for:
1701 case OMPD_parallel_for_simd:
1702 case OMPD_parallel_sections:
1704 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1705 QualType KmpInt32PtrTy =
1706 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1707 Sema::CapturedParamNameType Params[] = {
1708 std::make_pair(".global_tid.", KmpInt32PtrTy),
1709 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1710 std::make_pair(StringRef(), QualType()) // __context with shared vars
1712 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1716 case OMPD_target_teams:
1717 case OMPD_target_parallel: {
1718 Sema::CapturedParamNameType ParamsTarget[] = {
1719 std::make_pair(StringRef(), QualType()) // __context with shared vars
1721 // Start a captured region for 'target' with no implicit parameters.
1722 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1724 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1725 QualType KmpInt32PtrTy =
1726 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1727 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
1728 std::make_pair(".global_tid.", KmpInt32PtrTy),
1729 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1730 std::make_pair(StringRef(), QualType()) // __context with shared vars
1732 // Start a captured region for 'teams' or 'parallel'. Both regions have
1733 // the same implicit parameters.
1734 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1735 ParamsTeamsOrParallel);
1746 case OMPD_taskgroup:
1747 case OMPD_distribute:
1750 case OMPD_target_data:
1752 case OMPD_target_parallel_for:
1753 case OMPD_target_parallel_for_simd:
1754 case OMPD_target_simd: {
1755 Sema::CapturedParamNameType Params[] = {
1756 std::make_pair(StringRef(), QualType()) // __context with shared vars
1758 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1763 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1764 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1765 FunctionProtoType::ExtProtoInfo EPI;
1766 EPI.Variadic = true;
1767 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1768 Sema::CapturedParamNameType Params[] = {
1769 std::make_pair(".global_tid.", KmpInt32Ty),
1770 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1771 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1772 std::make_pair(".copy_fn.",
1773 Context.getPointerType(CopyFnType).withConst()),
1774 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1775 std::make_pair(StringRef(), QualType()) // __context with shared vars
1777 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1779 // Mark this captured region as inlined, because we don't use outlined
1780 // function directly.
1781 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1782 AlwaysInlineAttr::CreateImplicit(
1783 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1787 case OMPD_taskloop_simd: {
1788 QualType KmpInt32Ty =
1789 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1790 QualType KmpUInt64Ty =
1791 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1792 QualType KmpInt64Ty =
1793 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1794 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1795 FunctionProtoType::ExtProtoInfo EPI;
1796 EPI.Variadic = true;
1797 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1798 Sema::CapturedParamNameType Params[] = {
1799 std::make_pair(".global_tid.", KmpInt32Ty),
1800 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1801 std::make_pair(".privates.",
1802 Context.VoidPtrTy.withConst().withRestrict()),
1805 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1806 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1807 std::make_pair(".lb.", KmpUInt64Ty),
1808 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1809 std::make_pair(".liter.", KmpInt32Ty),
1810 std::make_pair(".reductions.",
1811 Context.VoidPtrTy.withConst().withRestrict()),
1812 std::make_pair(StringRef(), QualType()) // __context with shared vars
1814 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1816 // Mark this captured region as inlined, because we don't use outlined
1817 // function directly.
1818 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1819 AlwaysInlineAttr::CreateImplicit(
1820 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1823 case OMPD_distribute_parallel_for_simd:
1824 case OMPD_distribute_simd:
1825 case OMPD_distribute_parallel_for:
1826 case OMPD_teams_distribute:
1827 case OMPD_teams_distribute_simd:
1828 case OMPD_teams_distribute_parallel_for_simd:
1829 case OMPD_teams_distribute_parallel_for:
1830 case OMPD_target_teams_distribute:
1831 case OMPD_target_teams_distribute_parallel_for:
1832 case OMPD_target_teams_distribute_parallel_for_simd:
1833 case OMPD_target_teams_distribute_simd: {
1834 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1835 QualType KmpInt32PtrTy =
1836 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1837 Sema::CapturedParamNameType Params[] = {
1838 std::make_pair(".global_tid.", KmpInt32PtrTy),
1839 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1840 std::make_pair(".previous.lb.", Context.getSizeType()),
1841 std::make_pair(".previous.ub.", Context.getSizeType()),
1842 std::make_pair(StringRef(), QualType()) // __context with shared vars
1844 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1848 case OMPD_threadprivate:
1849 case OMPD_taskyield:
1852 case OMPD_cancellation_point:
1855 case OMPD_target_enter_data:
1856 case OMPD_target_exit_data:
1857 case OMPD_declare_reduction:
1858 case OMPD_declare_simd:
1859 case OMPD_declare_target:
1860 case OMPD_end_declare_target:
1861 case OMPD_target_update:
1862 llvm_unreachable("OpenMP Directive is not allowed");
1864 llvm_unreachable("Unknown OpenMP directive");
1868 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
1869 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
1870 getOpenMPCaptureRegions(CaptureRegions, DKind);
1871 return CaptureRegions.size();
1874 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1875 Expr *CaptureExpr, bool WithInit,
1876 bool AsExpression) {
1877 assert(CaptureExpr);
1878 ASTContext &C = S.getASTContext();
1879 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
1880 QualType Ty = Init->getType();
1881 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1882 if (S.getLangOpts().CPlusPlus)
1883 Ty = C.getLValueReferenceType(Ty);
1885 Ty = C.getPointerType(Ty);
1887 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1888 if (!Res.isUsable())
1894 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
1895 CaptureExpr->getLocStart());
1897 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
1898 S.CurContext->addHiddenDecl(CED);
1899 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
1903 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1905 OMPCapturedExprDecl *CD;
1906 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1907 CD = cast<OMPCapturedExprDecl>(VD);
1909 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1910 /*AsExpression=*/false);
1911 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1912 CaptureExpr->getExprLoc());
1915 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1918 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1919 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1920 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1921 CaptureExpr->getExprLoc());
1923 ExprResult Res = Ref;
1924 if (!S.getLangOpts().CPlusPlus &&
1925 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1926 Ref->getType()->isPointerType())
1927 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1928 if (!Res.isUsable())
1930 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
1934 // OpenMP directives parsed in this section are represented as a
1935 // CapturedStatement with an associated statement. If a syntax error
1936 // is detected during the parsing of the associated statement, the
1937 // compiler must abort processing and close the CapturedStatement.
1939 // Combined directives such as 'target parallel' have more than one
1940 // nested CapturedStatements. This RAII ensures that we unwind out
1941 // of all the nested CapturedStatements when an error is found.
1942 class CaptureRegionUnwinderRAII {
1946 OpenMPDirectiveKind DKind;
1949 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
1950 OpenMPDirectiveKind DKind)
1951 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
1952 ~CaptureRegionUnwinderRAII() {
1954 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
1955 while (--ThisCaptureLevel >= 0)
1956 S.ActOnCapturedRegionError();
1962 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1963 ArrayRef<OMPClause *> Clauses) {
1964 bool ErrorFound = false;
1965 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
1966 *this, ErrorFound, DSAStack->getCurrentDirective());
1967 if (!S.isUsable()) {
1972 OMPOrderedClause *OC = nullptr;
1973 OMPScheduleClause *SC = nullptr;
1974 SmallVector<OMPLinearClause *, 4> LCs;
1975 SmallVector<OMPClauseWithPreInit *, 8> PICs;
1976 // This is required for proper codegen.
1977 for (auto *Clause : Clauses) {
1978 if (isOpenMPPrivate(Clause->getClauseKind()) ||
1979 Clause->getClauseKind() == OMPC_copyprivate ||
1980 (getLangOpts().OpenMPUseTLS &&
1981 getASTContext().getTargetInfo().isTLSSupported() &&
1982 Clause->getClauseKind() == OMPC_copyin)) {
1983 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
1984 // Mark all variables in private list clauses as used in inner region.
1985 for (auto *VarRef : Clause->children()) {
1986 if (auto *E = cast_or_null<Expr>(VarRef)) {
1987 MarkDeclarationsReferencedInExpr(E);
1990 DSAStack->setForceVarCapturing(/*V=*/false);
1991 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
1992 if (auto *C = OMPClauseWithPreInit::get(Clause))
1994 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1995 if (auto *E = C->getPostUpdateExpr())
1996 MarkDeclarationsReferencedInExpr(E);
1999 if (Clause->getClauseKind() == OMPC_schedule)
2000 SC = cast<OMPScheduleClause>(Clause);
2001 else if (Clause->getClauseKind() == OMPC_ordered)
2002 OC = cast<OMPOrderedClause>(Clause);
2003 else if (Clause->getClauseKind() == OMPC_linear)
2004 LCs.push_back(cast<OMPLinearClause>(Clause));
2006 // OpenMP, 2.7.1 Loop Construct, Restrictions
2007 // The nonmonotonic modifier cannot be specified if an ordered clause is
2010 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2011 SC->getSecondScheduleModifier() ==
2012 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2014 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2015 ? SC->getFirstScheduleModifierLoc()
2016 : SC->getSecondScheduleModifierLoc(),
2017 diag::err_omp_schedule_nonmonotonic_ordered)
2018 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2021 if (!LCs.empty() && OC && OC->getNumForLoops()) {
2022 for (auto *C : LCs) {
2023 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
2024 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2028 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2029 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2030 OC->getNumForLoops()) {
2031 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
2032 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2039 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2040 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
2041 for (auto ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
2042 // Mark all variables in private list clauses as used in inner region.
2043 // Required for proper codegen of combined directives.
2044 // TODO: add processing for other clauses.
2045 if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
2046 for (auto *C : PICs) {
2047 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2048 // Find the particular capture region for the clause if the
2049 // directive is a combined one with multiple capture regions.
2050 // If the directive is not a combined one, the capture region
2051 // associated with the clause is OMPD_unknown and is generated
2053 if (CaptureRegion == ThisCaptureRegion ||
2054 CaptureRegion == OMPD_unknown) {
2055 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
2056 for (auto *D : DS->decls())
2057 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2062 SR = ActOnCapturedRegionEnd(SR.get());
2067 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2068 OpenMPDirectiveKind CancelRegion,
2069 SourceLocation StartLoc) {
2070 // CancelRegion is only needed for cancel and cancellation_point.
2071 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2074 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2075 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2078 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2079 << getOpenMPDirectiveName(CancelRegion);
2083 static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
2084 OpenMPDirectiveKind CurrentRegion,
2085 const DeclarationNameInfo &CurrentName,
2086 OpenMPDirectiveKind CancelRegion,
2087 SourceLocation StartLoc) {
2088 if (Stack->getCurScope()) {
2089 auto ParentRegion = Stack->getParentDirective();
2090 auto OffendingRegion = ParentRegion;
2091 bool NestingProhibited = false;
2092 bool CloseNesting = true;
2093 bool OrphanSeen = false;
2096 ShouldBeInParallelRegion,
2097 ShouldBeInOrderedRegion,
2098 ShouldBeInTargetRegion,
2099 ShouldBeInTeamsRegion
2100 } Recommend = NoRecommend;
2101 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
2102 // OpenMP [2.16, Nesting of Regions]
2103 // OpenMP constructs may not be nested inside a simd region.
2104 // OpenMP [2.8.1,simd Construct, Restrictions]
2105 // An ordered construct with the simd clause is the only OpenMP
2106 // construct that can appear in the simd region.
2107 // Allowing a SIMD construct nested in another SIMD construct is an
2108 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2110 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2111 ? diag::err_omp_prohibited_region_simd
2112 : diag::warn_omp_nesting_simd);
2113 return CurrentRegion != OMPD_simd;
2115 if (ParentRegion == OMPD_atomic) {
2116 // OpenMP [2.16, Nesting of Regions]
2117 // OpenMP constructs may not be nested inside an atomic region.
2118 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2121 if (CurrentRegion == OMPD_section) {
2122 // OpenMP [2.7.2, sections Construct, Restrictions]
2123 // Orphaned section directives are prohibited. That is, the section
2124 // directives must appear within the sections construct and must not be
2125 // encountered elsewhere in the sections region.
2126 if (ParentRegion != OMPD_sections &&
2127 ParentRegion != OMPD_parallel_sections) {
2128 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2129 << (ParentRegion != OMPD_unknown)
2130 << getOpenMPDirectiveName(ParentRegion);
2135 // Allow some constructs (except teams) to be orphaned (they could be
2136 // used in functions, called from OpenMP regions with the required
2138 if (ParentRegion == OMPD_unknown &&
2139 !isOpenMPNestingTeamsDirective(CurrentRegion))
2141 if (CurrentRegion == OMPD_cancellation_point ||
2142 CurrentRegion == OMPD_cancel) {
2143 // OpenMP [2.16, Nesting of Regions]
2144 // A cancellation point construct for which construct-type-clause is
2145 // taskgroup must be nested inside a task construct. A cancellation
2146 // point construct for which construct-type-clause is not taskgroup must
2147 // be closely nested inside an OpenMP construct that matches the type
2148 // specified in construct-type-clause.
2149 // A cancel construct for which construct-type-clause is taskgroup must be
2150 // nested inside a task construct. A cancel construct for which
2151 // construct-type-clause is not taskgroup must be closely nested inside an
2152 // OpenMP construct that matches the type specified in
2153 // construct-type-clause.
2155 !((CancelRegion == OMPD_parallel &&
2156 (ParentRegion == OMPD_parallel ||
2157 ParentRegion == OMPD_target_parallel)) ||
2158 (CancelRegion == OMPD_for &&
2159 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2160 ParentRegion == OMPD_target_parallel_for)) ||
2161 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2162 (CancelRegion == OMPD_sections &&
2163 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2164 ParentRegion == OMPD_parallel_sections)));
2165 } else if (CurrentRegion == OMPD_master) {
2166 // OpenMP [2.16, Nesting of Regions]
2167 // A master region may not be closely nested inside a worksharing,
2168 // atomic, or explicit task region.
2169 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2170 isOpenMPTaskingDirective(ParentRegion);
2171 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2172 // OpenMP [2.16, Nesting of Regions]
2173 // A critical region may not be nested (closely or otherwise) inside a
2174 // critical region with the same name. Note that this restriction is not
2175 // sufficient to prevent deadlock.
2176 SourceLocation PreviousCriticalLoc;
2177 bool DeadLock = Stack->hasDirective(
2178 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2179 const DeclarationNameInfo &DNI,
2180 SourceLocation Loc) -> bool {
2181 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2182 PreviousCriticalLoc = Loc;
2187 false /* skip top directive */);
2189 SemaRef.Diag(StartLoc,
2190 diag::err_omp_prohibited_region_critical_same_name)
2191 << CurrentName.getName();
2192 if (PreviousCriticalLoc.isValid())
2193 SemaRef.Diag(PreviousCriticalLoc,
2194 diag::note_omp_previous_critical_region);
2197 } else if (CurrentRegion == OMPD_barrier) {
2198 // OpenMP [2.16, Nesting of Regions]
2199 // A barrier region may not be closely nested inside a worksharing,
2200 // explicit task, critical, ordered, atomic, or master region.
2201 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2202 isOpenMPTaskingDirective(ParentRegion) ||
2203 ParentRegion == OMPD_master ||
2204 ParentRegion == OMPD_critical ||
2205 ParentRegion == OMPD_ordered;
2206 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
2207 !isOpenMPParallelDirective(CurrentRegion) &&
2208 !isOpenMPTeamsDirective(CurrentRegion)) {
2209 // OpenMP [2.16, Nesting of Regions]
2210 // A worksharing region may not be closely nested inside a worksharing,
2211 // explicit task, critical, ordered, atomic, or master region.
2212 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2213 isOpenMPTaskingDirective(ParentRegion) ||
2214 ParentRegion == OMPD_master ||
2215 ParentRegion == OMPD_critical ||
2216 ParentRegion == OMPD_ordered;
2217 Recommend = ShouldBeInParallelRegion;
2218 } else if (CurrentRegion == OMPD_ordered) {
2219 // OpenMP [2.16, Nesting of Regions]
2220 // An ordered region may not be closely nested inside a critical,
2221 // atomic, or explicit task region.
2222 // An ordered region must be closely nested inside a loop region (or
2223 // parallel loop region) with an ordered clause.
2224 // OpenMP [2.8.1,simd Construct, Restrictions]
2225 // An ordered construct with the simd clause is the only OpenMP construct
2226 // that can appear in the simd region.
2227 NestingProhibited = ParentRegion == OMPD_critical ||
2228 isOpenMPTaskingDirective(ParentRegion) ||
2229 !(isOpenMPSimdDirective(ParentRegion) ||
2230 Stack->isParentOrderedRegion());
2231 Recommend = ShouldBeInOrderedRegion;
2232 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
2233 // OpenMP [2.16, Nesting of Regions]
2234 // If specified, a teams construct must be contained within a target
2236 NestingProhibited = ParentRegion != OMPD_target;
2237 OrphanSeen = ParentRegion == OMPD_unknown;
2238 Recommend = ShouldBeInTargetRegion;
2239 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2241 if (!NestingProhibited &&
2242 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2243 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2244 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
2245 // OpenMP [2.16, Nesting of Regions]
2246 // distribute, parallel, parallel sections, parallel workshare, and the
2247 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2248 // constructs that can be closely nested in the teams region.
2249 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2250 !isOpenMPDistributeDirective(CurrentRegion);
2251 Recommend = ShouldBeInParallelRegion;
2253 if (!NestingProhibited &&
2254 isOpenMPNestingDistributeDirective(CurrentRegion)) {
2255 // OpenMP 4.5 [2.17 Nesting of Regions]
2256 // The region associated with the distribute construct must be strictly
2257 // nested inside a teams region
2259 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
2260 Recommend = ShouldBeInTeamsRegion;
2262 if (!NestingProhibited &&
2263 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2264 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2265 // OpenMP 4.5 [2.17 Nesting of Regions]
2266 // If a target, target update, target data, target enter data, or
2267 // target exit data construct is encountered during execution of a
2268 // target region, the behavior is unspecified.
2269 NestingProhibited = Stack->hasDirective(
2270 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2271 SourceLocation) -> bool {
2272 if (isOpenMPTargetExecutionDirective(K)) {
2273 OffendingRegion = K;
2278 false /* don't skip top directive */);
2279 CloseNesting = false;
2281 if (NestingProhibited) {
2283 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2284 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2286 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2287 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2288 << Recommend << getOpenMPDirectiveName(CurrentRegion);
2296 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2297 ArrayRef<OMPClause *> Clauses,
2298 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2299 bool ErrorFound = false;
2300 unsigned NamedModifiersNumber = 0;
2301 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2303 SmallVector<SourceLocation, 4> NameModifierLoc;
2304 for (const auto *C : Clauses) {
2305 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2306 // At most one if clause without a directive-name-modifier can appear on
2308 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2309 if (FoundNameModifiers[CurNM]) {
2310 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2311 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2312 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2314 } else if (CurNM != OMPD_unknown) {
2315 NameModifierLoc.push_back(IC->getNameModifierLoc());
2316 ++NamedModifiersNumber;
2318 FoundNameModifiers[CurNM] = IC;
2319 if (CurNM == OMPD_unknown)
2321 // Check if the specified name modifier is allowed for the current
2323 // At most one if clause with the particular directive-name-modifier can
2324 // appear on the directive.
2325 bool MatchFound = false;
2326 for (auto NM : AllowedNameModifiers) {
2333 S.Diag(IC->getNameModifierLoc(),
2334 diag::err_omp_wrong_if_directive_name_modifier)
2335 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2340 // If any if clause on the directive includes a directive-name-modifier then
2341 // all if clauses on the directive must include a directive-name-modifier.
2342 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2343 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2344 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2345 diag::err_omp_no_more_if_clause);
2348 std::string Sep(", ");
2349 unsigned AllowedCnt = 0;
2350 unsigned TotalAllowedNum =
2351 AllowedNameModifiers.size() - NamedModifiersNumber;
2352 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2354 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2355 if (!FoundNameModifiers[NM]) {
2357 Values += getOpenMPDirectiveName(NM);
2359 if (AllowedCnt + 2 == TotalAllowedNum)
2361 else if (AllowedCnt + 1 != TotalAllowedNum)
2366 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2367 diag::err_omp_unnamed_if_clause)
2368 << (TotalAllowedNum > 1) << Values;
2370 for (auto Loc : NameModifierLoc) {
2371 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2378 StmtResult Sema::ActOnOpenMPExecutableDirective(
2379 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2380 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2381 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
2382 StmtResult Res = StmtError();
2383 // First check CancelRegion which is then used in checkNestingOfRegions.
2384 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
2385 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2389 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
2390 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
2391 bool ErrorFound = false;
2392 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
2394 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2396 // Check default data sharing attributes for referenced variables.
2397 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2398 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
2400 while (--ThisCaptureLevel >= 0)
2401 S = cast<CapturedStmt>(S)->getCapturedStmt();
2402 DSAChecker.Visit(S);
2403 if (DSAChecker.isErrorFound())
2405 // Generate list of implicitly defined firstprivate variables.
2406 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
2408 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2409 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2410 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2411 SourceLocation(), SourceLocation())) {
2412 ClausesWithImplicit.push_back(Implicit);
2413 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2414 DSAChecker.getImplicitFirstprivate().size();
2420 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
2423 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2425 AllowedNameModifiers.push_back(OMPD_parallel);
2428 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2429 VarsWithInheritedDSA);
2432 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2433 VarsWithInheritedDSA);
2436 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2437 EndLoc, VarsWithInheritedDSA);
2440 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2444 assert(ClausesWithImplicit.empty() &&
2445 "No clauses are allowed for 'omp section' directive");
2446 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2449 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2453 assert(ClausesWithImplicit.empty() &&
2454 "No clauses are allowed for 'omp master' directive");
2455 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2458 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2461 case OMPD_parallel_for:
2462 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2463 EndLoc, VarsWithInheritedDSA);
2464 AllowedNameModifiers.push_back(OMPD_parallel);
2466 case OMPD_parallel_for_simd:
2467 Res = ActOnOpenMPParallelForSimdDirective(
2468 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2469 AllowedNameModifiers.push_back(OMPD_parallel);
2471 case OMPD_parallel_sections:
2472 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2474 AllowedNameModifiers.push_back(OMPD_parallel);
2478 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2479 AllowedNameModifiers.push_back(OMPD_task);
2481 case OMPD_taskyield:
2482 assert(ClausesWithImplicit.empty() &&
2483 "No clauses are allowed for 'omp taskyield' directive");
2484 assert(AStmt == nullptr &&
2485 "No associated statement allowed for 'omp taskyield' directive");
2486 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2489 assert(ClausesWithImplicit.empty() &&
2490 "No clauses are allowed for 'omp barrier' directive");
2491 assert(AStmt == nullptr &&
2492 "No associated statement allowed for 'omp barrier' directive");
2493 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2496 assert(ClausesWithImplicit.empty() &&
2497 "No clauses are allowed for 'omp taskwait' directive");
2498 assert(AStmt == nullptr &&
2499 "No associated statement allowed for 'omp taskwait' directive");
2500 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2502 case OMPD_taskgroup:
2503 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
2507 assert(AStmt == nullptr &&
2508 "No associated statement allowed for 'omp flush' directive");
2509 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2512 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2516 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2521 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2524 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2526 AllowedNameModifiers.push_back(OMPD_target);
2528 case OMPD_target_parallel:
2529 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2531 AllowedNameModifiers.push_back(OMPD_target);
2532 AllowedNameModifiers.push_back(OMPD_parallel);
2534 case OMPD_target_parallel_for:
2535 Res = ActOnOpenMPTargetParallelForDirective(
2536 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2537 AllowedNameModifiers.push_back(OMPD_target);
2538 AllowedNameModifiers.push_back(OMPD_parallel);
2540 case OMPD_cancellation_point:
2541 assert(ClausesWithImplicit.empty() &&
2542 "No clauses are allowed for 'omp cancellation point' directive");
2543 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2544 "cancellation point' directive");
2545 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2548 assert(AStmt == nullptr &&
2549 "No associated statement allowed for 'omp cancel' directive");
2550 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2552 AllowedNameModifiers.push_back(OMPD_cancel);
2554 case OMPD_target_data:
2555 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2557 AllowedNameModifiers.push_back(OMPD_target_data);
2559 case OMPD_target_enter_data:
2560 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2562 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2564 case OMPD_target_exit_data:
2565 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2567 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2570 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2571 EndLoc, VarsWithInheritedDSA);
2572 AllowedNameModifiers.push_back(OMPD_taskloop);
2574 case OMPD_taskloop_simd:
2575 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2576 EndLoc, VarsWithInheritedDSA);
2577 AllowedNameModifiers.push_back(OMPD_taskloop);
2579 case OMPD_distribute:
2580 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2581 EndLoc, VarsWithInheritedDSA);
2583 case OMPD_target_update:
2584 assert(!AStmt && "Statement is not allowed for target update");
2586 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
2587 AllowedNameModifiers.push_back(OMPD_target_update);
2589 case OMPD_distribute_parallel_for:
2590 Res = ActOnOpenMPDistributeParallelForDirective(
2591 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2592 AllowedNameModifiers.push_back(OMPD_parallel);
2594 case OMPD_distribute_parallel_for_simd:
2595 Res = ActOnOpenMPDistributeParallelForSimdDirective(
2596 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2597 AllowedNameModifiers.push_back(OMPD_parallel);
2599 case OMPD_distribute_simd:
2600 Res = ActOnOpenMPDistributeSimdDirective(
2601 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2603 case OMPD_target_parallel_for_simd:
2604 Res = ActOnOpenMPTargetParallelForSimdDirective(
2605 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2606 AllowedNameModifiers.push_back(OMPD_target);
2607 AllowedNameModifiers.push_back(OMPD_parallel);
2609 case OMPD_target_simd:
2610 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2611 EndLoc, VarsWithInheritedDSA);
2612 AllowedNameModifiers.push_back(OMPD_target);
2614 case OMPD_teams_distribute:
2615 Res = ActOnOpenMPTeamsDistributeDirective(
2616 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2618 case OMPD_teams_distribute_simd:
2619 Res = ActOnOpenMPTeamsDistributeSimdDirective(
2620 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2622 case OMPD_teams_distribute_parallel_for_simd:
2623 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
2624 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2625 AllowedNameModifiers.push_back(OMPD_parallel);
2627 case OMPD_teams_distribute_parallel_for:
2628 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
2629 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2630 AllowedNameModifiers.push_back(OMPD_parallel);
2632 case OMPD_target_teams:
2633 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
2635 AllowedNameModifiers.push_back(OMPD_target);
2637 case OMPD_target_teams_distribute:
2638 Res = ActOnOpenMPTargetTeamsDistributeDirective(
2639 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2640 AllowedNameModifiers.push_back(OMPD_target);
2642 case OMPD_target_teams_distribute_parallel_for:
2643 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
2644 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2645 AllowedNameModifiers.push_back(OMPD_target);
2646 AllowedNameModifiers.push_back(OMPD_parallel);
2648 case OMPD_target_teams_distribute_parallel_for_simd:
2649 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
2650 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2651 AllowedNameModifiers.push_back(OMPD_target);
2652 AllowedNameModifiers.push_back(OMPD_parallel);
2654 case OMPD_target_teams_distribute_simd:
2655 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
2656 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2657 AllowedNameModifiers.push_back(OMPD_target);
2659 case OMPD_declare_target:
2660 case OMPD_end_declare_target:
2661 case OMPD_threadprivate:
2662 case OMPD_declare_reduction:
2663 case OMPD_declare_simd:
2664 llvm_unreachable("OpenMP Directive is not allowed");
2666 llvm_unreachable("Unknown OpenMP directive");
2669 for (auto P : VarsWithInheritedDSA) {
2670 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2671 << P.first << P.second->getSourceRange();
2673 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2675 if (!AllowedNameModifiers.empty())
2676 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2684 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
2685 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
2686 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
2687 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
2688 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
2689 assert(Aligneds.size() == Alignments.size());
2690 assert(Linears.size() == LinModifiers.size());
2691 assert(Linears.size() == Steps.size());
2692 if (!DG || DG.get().isNull())
2693 return DeclGroupPtrTy();
2695 if (!DG.get().isSingleDecl()) {
2696 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
2699 auto *ADecl = DG.get().getSingleDecl();
2700 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
2701 ADecl = FTD->getTemplatedDecl();
2703 auto *FD = dyn_cast<FunctionDecl>(ADecl);
2705 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
2706 return DeclGroupPtrTy();
2709 // OpenMP [2.8.2, declare simd construct, Description]
2710 // The parameter of the simdlen clause must be a constant positive integer
2714 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
2715 // OpenMP [2.8.2, declare simd construct, Description]
2716 // The special this pointer can be used as if was one of the arguments to the
2717 // function in any of the linear, aligned, or uniform clauses.
2718 // The uniform clause declares one or more arguments to have an invariant
2719 // value for all concurrent invocations of the function in the execution of a
2720 // single SIMD loop.
2721 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
2722 Expr *UniformedLinearThis = nullptr;
2723 for (auto *E : Uniforms) {
2724 E = E->IgnoreParenImpCasts();
2725 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2726 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
2727 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2728 FD->getParamDecl(PVD->getFunctionScopeIndex())
2729 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
2730 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
2733 if (isa<CXXThisExpr>(E)) {
2734 UniformedLinearThis = E;
2737 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2738 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2740 // OpenMP [2.8.2, declare simd construct, Description]
2741 // The aligned clause declares that the object to which each list item points
2742 // is aligned to the number of bytes expressed in the optional parameter of
2743 // the aligned clause.
2744 // The special this pointer can be used as if was one of the arguments to the
2745 // function in any of the linear, aligned, or uniform clauses.
2746 // The type of list items appearing in the aligned clause must be array,
2747 // pointer, reference to array, or reference to pointer.
2748 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
2749 Expr *AlignedThis = nullptr;
2750 for (auto *E : Aligneds) {
2751 E = E->IgnoreParenImpCasts();
2752 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2753 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2754 auto *CanonPVD = PVD->getCanonicalDecl();
2755 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2756 FD->getParamDecl(PVD->getFunctionScopeIndex())
2757 ->getCanonicalDecl() == CanonPVD) {
2758 // OpenMP [2.8.1, simd construct, Restrictions]
2759 // A list-item cannot appear in more than one aligned clause.
2760 if (AlignedArgs.count(CanonPVD) > 0) {
2761 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
2762 << 1 << E->getSourceRange();
2763 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
2764 diag::note_omp_explicit_dsa)
2765 << getOpenMPClauseName(OMPC_aligned);
2768 AlignedArgs[CanonPVD] = E;
2769 QualType QTy = PVD->getType()
2770 .getNonReferenceType()
2771 .getUnqualifiedType()
2772 .getCanonicalType();
2773 const Type *Ty = QTy.getTypePtrOrNull();
2774 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
2775 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
2776 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
2777 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
2782 if (isa<CXXThisExpr>(E)) {
2784 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
2785 << 2 << E->getSourceRange();
2786 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
2787 << getOpenMPClauseName(OMPC_aligned);
2792 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2793 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2795 // The optional parameter of the aligned clause, alignment, must be a constant
2796 // positive integer expression. If no optional parameter is specified,
2797 // implementation-defined default alignments for SIMD instructions on the
2798 // target platforms are assumed.
2799 SmallVector<Expr *, 4> NewAligns;
2800 for (auto *E : Alignments) {
2803 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
2804 NewAligns.push_back(Align.get());
2806 // OpenMP [2.8.2, declare simd construct, Description]
2807 // The linear clause declares one or more list items to be private to a SIMD
2808 // lane and to have a linear relationship with respect to the iteration space
2810 // The special this pointer can be used as if was one of the arguments to the
2811 // function in any of the linear, aligned, or uniform clauses.
2812 // When a linear-step expression is specified in a linear clause it must be
2813 // either a constant integer expression or an integer-typed parameter that is
2814 // specified in a uniform clause on the directive.
2815 llvm::DenseMap<Decl *, Expr *> LinearArgs;
2816 const bool IsUniformedThis = UniformedLinearThis != nullptr;
2817 auto MI = LinModifiers.begin();
2818 for (auto *E : Linears) {
2819 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
2821 E = E->IgnoreParenImpCasts();
2822 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2823 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2824 auto *CanonPVD = PVD->getCanonicalDecl();
2825 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2826 FD->getParamDecl(PVD->getFunctionScopeIndex())
2827 ->getCanonicalDecl() == CanonPVD) {
2828 // OpenMP [2.15.3.7, linear Clause, Restrictions]
2829 // A list-item cannot appear in more than one linear clause.
2830 if (LinearArgs.count(CanonPVD) > 0) {
2831 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2832 << getOpenMPClauseName(OMPC_linear)
2833 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
2834 Diag(LinearArgs[CanonPVD]->getExprLoc(),
2835 diag::note_omp_explicit_dsa)
2836 << getOpenMPClauseName(OMPC_linear);
2839 // Each argument can appear in at most one uniform or linear clause.
2840 if (UniformedArgs.count(CanonPVD) > 0) {
2841 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2842 << getOpenMPClauseName(OMPC_linear)
2843 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
2844 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
2845 diag::note_omp_explicit_dsa)
2846 << getOpenMPClauseName(OMPC_uniform);
2849 LinearArgs[CanonPVD] = E;
2850 if (E->isValueDependent() || E->isTypeDependent() ||
2851 E->isInstantiationDependent() ||
2852 E->containsUnexpandedParameterPack())
2854 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
2855 PVD->getOriginalType());
2859 if (isa<CXXThisExpr>(E)) {
2860 if (UniformedLinearThis) {
2861 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2862 << getOpenMPClauseName(OMPC_linear)
2863 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
2864 << E->getSourceRange();
2865 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
2866 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
2870 UniformedLinearThis = E;
2871 if (E->isValueDependent() || E->isTypeDependent() ||
2872 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
2874 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
2878 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2879 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2881 Expr *Step = nullptr;
2882 Expr *NewStep = nullptr;
2883 SmallVector<Expr *, 4> NewSteps;
2884 for (auto *E : Steps) {
2885 // Skip the same step expression, it was checked already.
2886 if (Step == E || !E) {
2887 NewSteps.push_back(E ? NewStep : nullptr);
2891 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
2892 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2893 auto *CanonPVD = PVD->getCanonicalDecl();
2894 if (UniformedArgs.count(CanonPVD) == 0) {
2895 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
2896 << Step->getSourceRange();
2897 } else if (E->isValueDependent() || E->isTypeDependent() ||
2898 E->isInstantiationDependent() ||
2899 E->containsUnexpandedParameterPack() ||
2900 CanonPVD->getType()->hasIntegerRepresentation())
2901 NewSteps.push_back(Step);
2903 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
2904 << Step->getSourceRange();
2909 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2910 !Step->isInstantiationDependent() &&
2911 !Step->containsUnexpandedParameterPack()) {
2912 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
2915 NewStep = VerifyIntegerConstantExpression(NewStep).get();
2917 NewSteps.push_back(NewStep);
2919 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
2920 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
2921 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
2922 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
2923 const_cast<Expr **>(Linears.data()), Linears.size(),
2924 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
2925 NewSteps.data(), NewSteps.size(), SR);
2926 ADecl->addAttr(NewAttr);
2927 return ConvertDeclToDeclGroup(ADecl);
2930 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2932 SourceLocation StartLoc,
2933 SourceLocation EndLoc) {
2937 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2938 // 1.2.2 OpenMP Language Terminology
2939 // Structured block - An executable statement with a single entry at the
2940 // top and a single exit at the bottom.
2941 // The point of exit cannot be a branch out of the structured block.
2942 // longjmp() and throw() must not violate the entry/exit criteria.
2943 CS->getCapturedDecl()->setNothrow();
2945 getCurFunction()->setHasBranchProtectedScope();
2947 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2948 DSAStack->isCancelRegion());
2952 /// \brief Helper class for checking canonical form of the OpenMP loops and
2953 /// extracting iteration space of each loop in the loop nest, that will be used
2954 /// for IR generation.
2955 class OpenMPIterationSpaceChecker {
2956 /// \brief Reference to Sema.
2958 /// \brief A location for diagnostics (when there is no some better location).
2959 SourceLocation DefaultLoc;
2960 /// \brief A location for diagnostics (when increment is not compatible).
2961 SourceLocation ConditionLoc;
2962 /// \brief A source location for referring to loop init later.
2963 SourceRange InitSrcRange;
2964 /// \brief A source location for referring to condition later.
2965 SourceRange ConditionSrcRange;
2966 /// \brief A source location for referring to increment later.
2967 SourceRange IncrementSrcRange;
2968 /// \brief Loop variable.
2969 ValueDecl *LCDecl = nullptr;
2970 /// \brief Reference to loop variable.
2971 Expr *LCRef = nullptr;
2972 /// \brief Lower bound (initializer for the var).
2974 /// \brief Upper bound.
2976 /// \brief Loop step (increment).
2977 Expr *Step = nullptr;
2978 /// \brief This flag is true when condition is one of:
2983 bool TestIsLessOp = false;
2984 /// \brief This flag is true when condition is strict ( < or > ).
2985 bool TestIsStrictOp = false;
2986 /// \brief This flag is true when step is subtracted on each iteration.
2987 bool SubtractStep = false;
2990 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2991 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
2992 /// \brief Check init-expr for canonical loop form and save loop counter
2993 /// variable - #Var and its initialization value - #LB.
2994 bool CheckInit(Stmt *S, bool EmitDiags = true);
2995 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2996 /// for less/greater and for strict/non-strict comparison.
2997 bool CheckCond(Expr *S);
2998 /// \brief Check incr-expr for canonical loop form and return true if it
2999 /// does not conform, otherwise save loop step (#Step).
3000 bool CheckInc(Expr *S);
3001 /// \brief Return the loop counter variable.
3002 ValueDecl *GetLoopDecl() const { return LCDecl; }
3003 /// \brief Return the reference expression to loop counter variable.
3004 Expr *GetLoopDeclRefExpr() const { return LCRef; }
3005 /// \brief Source range of the loop init.
3006 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3007 /// \brief Source range of the loop condition.
3008 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3009 /// \brief Source range of the loop increment.
3010 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3011 /// \brief True if the step should be subtracted.
3012 bool ShouldSubtractStep() const { return SubtractStep; }
3013 /// \brief Build the expression to calculate the number of iterations.
3015 BuildNumIterations(Scope *S, const bool LimitedType,
3016 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3017 /// \brief Build the precondition expression for the loops.
3018 Expr *BuildPreCond(Scope *S, Expr *Cond,
3019 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3020 /// \brief Build reference expression to the counter be used for codegen.
3021 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3022 DSAStackTy &DSA) const;
3023 /// \brief Build reference expression to the private counter be used for
3025 Expr *BuildPrivateCounterVar() const;
3026 /// \brief Build initialization of the counter be used for codegen.
3027 Expr *BuildCounterInit() const;
3028 /// \brief Build step of the counter be used for codegen.
3029 Expr *BuildCounterStep() const;
3030 /// \brief Return true if any expression is dependent.
3031 bool Dependent() const;
3034 /// \brief Check the right-hand side of an assignment in the increment
3036 bool CheckIncRHS(Expr *RHS);
3037 /// \brief Helper to set loop counter variable and its initializer.
3038 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3039 /// \brief Helper to set upper bound.
3040 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
3042 /// \brief Helper to set loop increment.
3043 bool SetStep(Expr *NewStep, bool Subtract);
3046 bool OpenMPIterationSpaceChecker::Dependent() const {
3048 assert(!LB && !UB && !Step);
3051 return LCDecl->getType()->isDependentType() ||
3052 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3053 (Step && Step->isValueDependent());
3056 static Expr *getExprAsWritten(Expr *E) {
3057 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3058 E = ExprTemp->getSubExpr();
3060 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3061 E = MTE->GetTemporaryExpr();
3063 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3064 E = Binder->getSubExpr();
3066 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3067 E = ICE->getSubExprAsWritten();
3068 return E->IgnoreParens();
3071 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3074 // State consistency checking to ensure correct usage.
3075 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
3076 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3077 if (!NewLCDecl || !NewLB)
3079 LCDecl = getCanonicalDecl(NewLCDecl);
3080 LCRef = NewLCRefExpr;
3081 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3082 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3083 if ((Ctor->isCopyOrMoveConstructor() ||
3084 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3085 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3086 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
3091 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
3092 SourceRange SR, SourceLocation SL) {
3093 // State consistency checking to ensure correct usage.
3094 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3095 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3099 TestIsLessOp = LessOp;
3100 TestIsStrictOp = StrictOp;
3101 ConditionSrcRange = SR;
3106 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3107 // State consistency checking to ensure correct usage.
3108 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
3111 if (!NewStep->isValueDependent()) {
3112 // Check that the step is integer expression.
3113 SourceLocation StepLoc = NewStep->getLocStart();
3115 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3116 if (Val.isInvalid())
3118 NewStep = Val.get();
3120 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3121 // If test-expr is of form var relational-op b and relational-op is < or
3122 // <= then incr-expr must cause var to increase on each iteration of the
3123 // loop. If test-expr is of form var relational-op b and relational-op is
3124 // > or >= then incr-expr must cause var to decrease on each iteration of
3126 // If test-expr is of form b relational-op var and relational-op is < or
3127 // <= then incr-expr must cause var to decrease on each iteration of the
3128 // loop. If test-expr is of form b relational-op var and relational-op is
3129 // > or >= then incr-expr must cause var to increase on each iteration of
3131 llvm::APSInt Result;
3132 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3133 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3135 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
3137 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
3138 bool IsConstZero = IsConstant && !Result.getBoolValue();
3139 if (UB && (IsConstZero ||
3140 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
3141 : (IsConstPos || (IsUnsigned && !Subtract))))) {
3142 SemaRef.Diag(NewStep->getExprLoc(),
3143 diag::err_omp_loop_incr_not_compatible)
3144 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
3145 SemaRef.Diag(ConditionLoc,
3146 diag::note_omp_loop_cond_requres_compatible_incr)
3147 << TestIsLessOp << ConditionSrcRange;
3150 if (TestIsLessOp == Subtract) {
3152 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3154 Subtract = !Subtract;
3159 SubtractStep = Subtract;
3163 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
3164 // Check init-expr for canonical loop form and save loop counter
3165 // variable - #Var and its initialization value - #LB.
3166 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3168 // integer-type var = lb
3169 // random-access-iterator-type var = lb
3170 // pointer-type var = lb
3174 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3178 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3179 if (!ExprTemp->cleanupsHaveSideEffects())
3180 S = ExprTemp->getSubExpr();
3182 InitSrcRange = S->getSourceRange();
3183 if (Expr *E = dyn_cast<Expr>(S))
3184 S = E->IgnoreParens();
3185 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3186 if (BO->getOpcode() == BO_Assign) {
3187 auto *LHS = BO->getLHS()->IgnoreParens();
3188 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3189 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3190 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3191 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3192 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3194 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3195 if (ME->isArrow() &&
3196 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3197 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3200 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
3201 if (DS->isSingleDecl()) {
3202 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
3203 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
3204 // Accept non-canonical init form here but emit ext. warning.
3205 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
3206 SemaRef.Diag(S->getLocStart(),
3207 diag::ext_omp_loop_not_canonical_init)
3208 << S->getSourceRange();
3209 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
3213 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3214 if (CE->getOperator() == OO_Equal) {
3215 auto *LHS = CE->getArg(0);
3216 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3217 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3218 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3219 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3220 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3222 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3223 if (ME->isArrow() &&
3224 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3225 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3230 if (Dependent() || SemaRef.CurContext->isDependentContext())
3233 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3234 << S->getSourceRange();
3239 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
3240 /// variable (which may be the loop variable) if possible.
3241 static const ValueDecl *GetInitLCDecl(Expr *E) {
3244 E = getExprAsWritten(E);
3245 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3246 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3247 if ((Ctor->isCopyOrMoveConstructor() ||
3248 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3249 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3250 E = CE->getArg(0)->IgnoreParenImpCasts();
3251 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3252 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3253 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3254 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3255 return getCanonicalDecl(ME->getMemberDecl());
3256 return getCanonicalDecl(VD);
3259 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3260 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3261 return getCanonicalDecl(ME->getMemberDecl());
3265 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3266 // Check test-expr for canonical form, save upper-bound UB, flags for
3267 // less/greater and for strict/non-strict comparison.
3268 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3269 // var relational-op b
3270 // b relational-op var
3273 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
3276 S = getExprAsWritten(S);
3277 SourceLocation CondLoc = S->getLocStart();
3278 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3279 if (BO->isRelationalOp()) {
3280 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3281 return SetUB(BO->getRHS(),
3282 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3283 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3284 BO->getSourceRange(), BO->getOperatorLoc());
3285 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
3286 return SetUB(BO->getLHS(),
3287 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3288 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3289 BO->getSourceRange(), BO->getOperatorLoc());
3291 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3292 if (CE->getNumArgs() == 2) {
3293 auto Op = CE->getOperator();
3296 case OO_GreaterEqual:
3299 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3300 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3301 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3302 CE->getOperatorLoc());
3303 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
3304 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3305 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3306 CE->getOperatorLoc());
3313 if (Dependent() || SemaRef.CurContext->isDependentContext())
3315 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3316 << S->getSourceRange() << LCDecl;
3320 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3321 // RHS of canonical loop form increment can be:
3326 RHS = RHS->IgnoreParenImpCasts();
3327 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
3328 if (BO->isAdditiveOp()) {
3329 bool IsAdd = BO->getOpcode() == BO_Add;
3330 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3331 return SetStep(BO->getRHS(), !IsAdd);
3332 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
3333 return SetStep(BO->getLHS(), false);
3335 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3336 bool IsAdd = CE->getOperator() == OO_Plus;
3337 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3338 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3339 return SetStep(CE->getArg(1), !IsAdd);
3340 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
3341 return SetStep(CE->getArg(0), false);
3344 if (Dependent() || SemaRef.CurContext->isDependentContext())
3346 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3347 << RHS->getSourceRange() << LCDecl;
3351 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3352 // Check incr-expr for canonical loop form and return true if it
3353 // does not conform.
3354 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3366 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
3369 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3370 if (!ExprTemp->cleanupsHaveSideEffects())
3371 S = ExprTemp->getSubExpr();
3373 IncrementSrcRange = S->getSourceRange();
3374 S = S->IgnoreParens();
3375 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
3376 if (UO->isIncrementDecrementOp() &&
3377 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
3378 return SetStep(SemaRef
3379 .ActOnIntegerConstant(UO->getLocStart(),
3380 (UO->isDecrementOp() ? -1 : 1))
3383 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3384 switch (BO->getOpcode()) {
3387 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3388 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3391 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3392 return CheckIncRHS(BO->getRHS());
3397 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3398 switch (CE->getOperator()) {
3401 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3402 return SetStep(SemaRef
3403 .ActOnIntegerConstant(
3405 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
3411 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3412 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3415 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3416 return CheckIncRHS(CE->getArg(1));
3422 if (Dependent() || SemaRef.CurContext->isDependentContext())
3424 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3425 << S->getSourceRange() << LCDecl;
3430 tryBuildCapture(Sema &SemaRef, Expr *Capture,
3431 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3432 if (SemaRef.CurContext->isDependentContext())
3433 return ExprResult(Capture);
3434 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3435 return SemaRef.PerformImplicitConversion(
3436 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3437 /*AllowExplicit=*/true);
3438 auto I = Captures.find(Capture);
3439 if (I != Captures.end())
3440 return buildCapture(SemaRef, Capture, I->second);
3441 DeclRefExpr *Ref = nullptr;
3442 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3443 Captures[Capture] = Ref;
3447 /// \brief Build the expression to calculate the number of iterations.
3448 Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3449 Scope *S, const bool LimitedType,
3450 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3452 auto VarType = LCDecl->getType().getNonReferenceType();
3453 if (VarType->isIntegerType() || VarType->isPointerType() ||
3454 SemaRef.getLangOpts().CPlusPlus) {
3456 auto *UBExpr = TestIsLessOp ? UB : LB;
3457 auto *LBExpr = TestIsLessOp ? LB : UB;
3458 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3459 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
3460 if (!Upper || !Lower)
3463 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3465 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
3466 // BuildBinOp already emitted error, this one is to point user to upper
3467 // and lower bound, and to tell what is passed to 'operator-'.
3468 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3469 << Upper->getSourceRange() << Lower->getSourceRange();
3474 if (!Diff.isUsable())
3477 // Upper - Lower [- 1]
3479 Diff = SemaRef.BuildBinOp(
3480 S, DefaultLoc, BO_Sub, Diff.get(),
3481 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3482 if (!Diff.isUsable())
3485 // Upper - Lower [- 1] + Step
3486 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3487 if (!NewStep.isUsable())
3489 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
3490 if (!Diff.isUsable())
3493 // Parentheses (for dumping/debugging purposes only).
3494 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3495 if (!Diff.isUsable())
3498 // (Upper - Lower [- 1] + Step) / Step
3499 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
3500 if (!Diff.isUsable())
3503 // OpenMP runtime requires 32-bit or 64-bit loop variables.
3504 QualType Type = Diff.get()->getType();
3505 auto &C = SemaRef.Context;
3506 bool UseVarType = VarType->hasIntegerRepresentation() &&
3507 C.getTypeSize(Type) > C.getTypeSize(VarType);
3508 if (!Type->isIntegerType() || UseVarType) {
3510 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3511 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3512 : Type->hasSignedIntegerRepresentation();
3513 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3514 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3515 Diff = SemaRef.PerformImplicitConversion(
3516 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3517 if (!Diff.isUsable())
3522 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3523 if (NewSize != C.getTypeSize(Type)) {
3524 if (NewSize < C.getTypeSize(Type)) {
3525 assert(NewSize == 64 && "incorrect loop var size");
3526 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3527 << InitSrcRange << ConditionSrcRange;
3529 QualType NewType = C.getIntTypeForBitwidth(
3530 NewSize, Type->hasSignedIntegerRepresentation() ||
3531 C.getTypeSize(Type) < NewSize);
3532 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3533 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3534 Sema::AA_Converting, true);
3535 if (!Diff.isUsable())
3544 Expr *OpenMPIterationSpaceChecker::BuildPreCond(
3545 Scope *S, Expr *Cond,
3546 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3547 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3548 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3549 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
3551 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
3552 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
3553 if (!NewLB.isUsable() || !NewUB.isUsable())
3556 auto CondExpr = SemaRef.BuildBinOp(
3557 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3558 : (TestIsStrictOp ? BO_GT : BO_GE),
3559 NewLB.get(), NewUB.get());
3560 if (CondExpr.isUsable()) {
3561 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
3562 SemaRef.Context.BoolTy))
3563 CondExpr = SemaRef.PerformImplicitConversion(
3564 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3565 /*AllowExplicit=*/true);
3567 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3568 // Otherwise use original loop conditon and evaluate it in runtime.
3569 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3572 /// \brief Build reference expression to the counter be used for codegen.
3573 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
3574 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
3575 auto *VD = dyn_cast<VarDecl>(LCDecl);
3577 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
3578 auto *Ref = buildDeclRefExpr(
3579 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
3580 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
3581 // If the loop control decl is explicitly marked as private, do not mark it
3582 // as captured again.
3583 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
3584 Captures.insert(std::make_pair(LCRef, Ref));
3587 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
3591 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3592 if (LCDecl && !LCDecl->isInvalidDecl()) {
3593 auto Type = LCDecl->getType().getNonReferenceType();
3595 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
3596 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
3597 if (PrivateVar->isInvalidDecl())
3599 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3604 /// \brief Build initialization of the counter to be used for codegen.
3605 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3607 /// \brief Build step of the counter be used for codegen.
3608 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3610 /// \brief Iteration space of a single for loop.
3611 struct LoopIterationSpace final {
3612 /// \brief Condition of the loop.
3613 Expr *PreCond = nullptr;
3614 /// \brief This expression calculates the number of iterations in the loop.
3615 /// It is always possible to calculate it before starting the loop.
3616 Expr *NumIterations = nullptr;
3617 /// \brief The loop counter variable.
3618 Expr *CounterVar = nullptr;
3619 /// \brief Private loop counter variable.
3620 Expr *PrivateCounterVar = nullptr;
3621 /// \brief This is initializer for the initial value of #CounterVar.
3622 Expr *CounterInit = nullptr;
3623 /// \brief This is step for the #CounterVar used to generate its update:
3624 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3625 Expr *CounterStep = nullptr;
3626 /// \brief Should step be subtracted?
3627 bool Subtract = false;
3628 /// \brief Source range of the loop init.
3629 SourceRange InitSrcRange;
3630 /// \brief Source range of the loop condition.
3631 SourceRange CondSrcRange;
3632 /// \brief Source range of the loop increment.
3633 SourceRange IncSrcRange;
3638 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3639 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3640 assert(Init && "Expected loop in canonical form.");
3641 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3642 if (AssociatedLoops > 0 &&
3643 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3644 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3645 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3646 if (auto *D = ISC.GetLoopDecl()) {
3647 auto *VD = dyn_cast<VarDecl>(D);
3649 if (auto *Private = IsOpenMPCapturedDecl(D))
3652 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
3653 /*WithInit=*/false);
3654 VD = cast<VarDecl>(Ref->getDecl());
3657 DSAStack->addLoopControlVariable(D, VD);
3660 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
3664 /// \brief Called on a for stmt to check and extract its iteration space
3665 /// for further processing (such as collapsing).
3666 static bool CheckOpenMPIterationSpace(
3667 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3668 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
3669 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
3670 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
3671 LoopIterationSpace &ResultIterSpace,
3672 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3673 // OpenMP [2.6, Canonical Loop Form]
3674 // for (init-expr; test-expr; incr-expr) structured-block
3675 auto *For = dyn_cast_or_null<ForStmt>(S);
3677 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
3678 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3679 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3680 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3681 if (NestedLoopCount > 1) {
3682 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3683 SemaRef.Diag(DSA.getConstructLoc(),
3684 diag::note_omp_collapse_ordered_expr)
3685 << 2 << CollapseLoopCountExpr->getSourceRange()
3686 << OrderedLoopCountExpr->getSourceRange();
3687 else if (CollapseLoopCountExpr)
3688 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3689 diag::note_omp_collapse_ordered_expr)
3690 << 0 << CollapseLoopCountExpr->getSourceRange();
3692 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3693 diag::note_omp_collapse_ordered_expr)
3694 << 1 << OrderedLoopCountExpr->getSourceRange();
3698 assert(For->getBody());
3700 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3703 auto Init = For->getInit();
3704 if (ISC.CheckInit(Init))
3707 bool HasErrors = false;
3709 // Check loop variable's type.
3710 if (auto *LCDecl = ISC.GetLoopDecl()) {
3711 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
3713 // OpenMP [2.6, Canonical Loop Form]
3714 // Var is one of the following:
3715 // A variable of signed or unsigned integer type.
3716 // For C++, a variable of a random access iterator type.
3717 // For C, a variable of a pointer type.
3718 auto VarType = LCDecl->getType().getNonReferenceType();
3719 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3720 !VarType->isPointerType() &&
3721 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3722 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3723 << SemaRef.getLangOpts().CPlusPlus;
3727 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
3729 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3730 // parallel for construct is (are) private.
3731 // The loop iteration variable in the associated for-loop of a simd
3732 // construct with just one associated for-loop is linear with a
3733 // constant-linear-step that is the increment of the associated for-loop.
3734 // Exclude loop var from the list of variables with implicitly defined data
3735 // sharing attributes.
3736 VarsWithImplicitDSA.erase(LCDecl);
3738 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3739 // in a Construct, C/C++].
3740 // The loop iteration variable in the associated for-loop of a simd
3741 // construct with just one associated for-loop may be listed in a linear
3742 // clause with a constant-linear-step that is the increment of the
3743 // associated for-loop.
3744 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3745 // parallel for construct may be listed in a private or lastprivate clause.
3746 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
3747 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3748 // declared in the loop and it is predetermined as a private.
3749 auto PredeterminedCKind =
3750 isOpenMPSimdDirective(DKind)
3751 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3753 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3754 DVar.CKind != PredeterminedCKind) ||
3755 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
3756 isOpenMPDistributeDirective(DKind)) &&
3757 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3758 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3759 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3760 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
3761 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3762 << getOpenMPClauseName(PredeterminedCKind);
3763 if (DVar.RefExpr == nullptr)
3764 DVar.CKind = PredeterminedCKind;
3765 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
3767 } else if (LoopDeclRefExpr != nullptr) {
3768 // Make the loop iteration variable private (for worksharing constructs),
3769 // linear (for simd directives with the only one associated loop) or
3770 // lastprivate (for simd directives with several collapsed or ordered
3772 if (DVar.CKind == OMPC_unknown)
3773 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
3774 [](OpenMPDirectiveKind) -> bool { return true; },
3775 /*FromParent=*/false);
3776 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
3779 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
3782 HasErrors |= ISC.CheckCond(For->getCond());
3785 HasErrors |= ISC.CheckInc(For->getInc());
3788 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
3791 // Build the loop's iteration space representation.
3792 ResultIterSpace.PreCond =
3793 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
3794 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3796 (isOpenMPWorksharingDirective(DKind) ||
3797 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
3799 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
3800 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
3801 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3802 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3803 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3804 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3805 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3806 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3808 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3809 ResultIterSpace.NumIterations == nullptr ||
3810 ResultIterSpace.CounterVar == nullptr ||
3811 ResultIterSpace.PrivateCounterVar == nullptr ||
3812 ResultIterSpace.CounterInit == nullptr ||
3813 ResultIterSpace.CounterStep == nullptr);
3818 /// \brief Build 'VarRef = Start.
3820 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
3822 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3823 // Build 'VarRef = Start.
3824 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
3825 if (!NewStart.isUsable())
3827 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
3828 VarRef.get()->getType())) {
3829 NewStart = SemaRef.PerformImplicitConversion(
3830 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3831 /*AllowExplicit=*/true);
3832 if (!NewStart.isUsable())
3837 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3841 /// \brief Build 'VarRef = Start + Iter * Step'.
3843 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
3844 ExprResult VarRef, ExprResult Start, ExprResult Iter,
3845 ExprResult Step, bool Subtract,
3846 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
3847 // Add parentheses (for debugging purposes only).
3848 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3849 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3853 ExprResult NewStep = Step;
3855 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
3856 if (NewStep.isInvalid())
3859 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
3860 if (!Update.isUsable())
3863 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
3864 // 'VarRef = Start (+|-) Iter * Step'.
3865 ExprResult NewStart = Start;
3867 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
3868 if (NewStart.isInvalid())
3871 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
3872 ExprResult SavedUpdate = Update;
3873 ExprResult UpdateVal;
3874 if (VarRef.get()->getType()->isOverloadableType() ||
3875 NewStart.get()->getType()->isOverloadableType() ||
3876 Update.get()->getType()->isOverloadableType()) {
3877 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3878 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
3880 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3881 if (Update.isUsable()) {
3883 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
3884 VarRef.get(), SavedUpdate.get());
3885 if (UpdateVal.isUsable()) {
3886 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
3890 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3893 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
3894 if (!Update.isUsable() || !UpdateVal.isUsable()) {
3895 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
3896 NewStart.get(), SavedUpdate.get());
3897 if (!Update.isUsable())
3900 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
3901 VarRef.get()->getType())) {
3902 Update = SemaRef.PerformImplicitConversion(
3903 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3904 if (!Update.isUsable())
3908 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3913 /// \brief Convert integer expression \a E to make it have at least \a Bits
3915 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
3918 auto &C = SemaRef.Context;
3919 QualType OldType = E->getType();
3920 unsigned HasBits = C.getTypeSize(OldType);
3921 if (HasBits >= Bits)
3922 return ExprResult(E);
3923 // OK to convert to signed, because new type has more bits than old.
3924 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3925 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3929 /// \brief Check if the given expression \a E is a constant integer that fits
3930 /// into \a Bits bits.
3931 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3934 llvm::APSInt Result;
3935 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3936 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3940 /// Build preinits statement for the given declarations.
3941 static Stmt *buildPreInits(ASTContext &Context,
3942 SmallVectorImpl<Decl *> &PreInits) {
3943 if (!PreInits.empty()) {
3944 return new (Context) DeclStmt(
3945 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
3946 SourceLocation(), SourceLocation());
3951 /// Build preinits statement for the given declarations.
3952 static Stmt *buildPreInits(ASTContext &Context,
3953 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3954 if (!Captures.empty()) {
3955 SmallVector<Decl *, 16> PreInits;
3956 for (auto &Pair : Captures)
3957 PreInits.push_back(Pair.second->getDecl());
3958 return buildPreInits(Context, PreInits);
3963 /// Build postupdate expression for the given list of postupdates expressions.
3964 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
3965 Expr *PostUpdate = nullptr;
3966 if (!PostUpdates.empty()) {
3967 for (auto *E : PostUpdates) {
3968 Expr *ConvE = S.BuildCStyleCastExpr(
3970 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
3973 PostUpdate = PostUpdate
3974 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
3983 /// \brief Called on a for stmt to check itself and nested loops (if any).
3984 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3985 /// number of collapsed loops otherwise.
3987 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3988 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3990 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
3991 OMPLoopDirective::HelperExprs &Built) {
3992 unsigned NestedLoopCount = 1;
3993 if (CollapseLoopCountExpr) {
3994 // Found 'collapse' clause - calculate collapse number.
3995 llvm::APSInt Result;
3996 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3997 NestedLoopCount = Result.getLimitedValue();
3999 if (OrderedLoopCountExpr) {
4000 // Found 'ordered' clause - calculate collapse number.
4001 llvm::APSInt Result;
4002 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4003 if (Result.getLimitedValue() < NestedLoopCount) {
4004 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4005 diag::err_omp_wrong_ordered_loop_count)
4006 << OrderedLoopCountExpr->getSourceRange();
4007 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4008 diag::note_collapse_loop_count)
4009 << CollapseLoopCountExpr->getSourceRange();
4011 NestedLoopCount = Result.getLimitedValue();
4014 // This is helper routine for loop directives (e.g., 'for', 'simd',
4015 // 'for simd', etc.).
4016 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
4017 SmallVector<LoopIterationSpace, 4> IterSpaces;
4018 IterSpaces.resize(NestedLoopCount);
4019 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
4020 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
4021 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
4022 NestedLoopCount, CollapseLoopCountExpr,
4023 OrderedLoopCountExpr, VarsWithImplicitDSA,
4024 IterSpaces[Cnt], Captures))
4026 // Move on to the next nested for loop, or to the loop body.
4027 // OpenMP [2.8.1, simd construct, Restrictions]
4028 // All loops associated with the construct must be perfectly nested; that
4029 // is, there must be no intervening code nor any OpenMP directive between
4031 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4034 Built.clear(/* size */ NestedLoopCount);
4036 if (SemaRef.CurContext->isDependentContext())
4037 return NestedLoopCount;
4039 // An example of what is generated for the following code:
4041 // #pragma omp simd collapse(2) ordered(2)
4042 // for (i = 0; i < NI; ++i)
4043 // for (k = 0; k < NK; ++k)
4044 // for (j = J0; j < NJ; j+=2) {
4048 // We generate the code below.
4049 // Note: the loop body may be outlined in CodeGen.
4050 // Note: some counters may be C++ classes, operator- is used to find number of
4051 // iterations and operator+= to calculate counter value.
4052 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4053 // or i64 is currently supported).
4055 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4056 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4057 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4058 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4059 // // similar updates for vars in clauses (e.g. 'linear')
4060 // <loop body (using local i and j)>
4062 // i = NI; // assign final values of counters
4066 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4067 // the iteration counts of the collapsed for loops.
4068 // Precondition tests if there is at least one iteration (all conditions are
4070 auto PreCond = ExprResult(IterSpaces[0].PreCond);
4071 auto N0 = IterSpaces[0].NumIterations;
4072 ExprResult LastIteration32 = WidenIterationCount(
4073 32 /* Bits */, SemaRef
4074 .PerformImplicitConversion(
4075 N0->IgnoreImpCasts(), N0->getType(),
4076 Sema::AA_Converting, /*AllowExplicit=*/true)
4079 ExprResult LastIteration64 = WidenIterationCount(
4080 64 /* Bits */, SemaRef
4081 .PerformImplicitConversion(
4082 N0->IgnoreImpCasts(), N0->getType(),
4083 Sema::AA_Converting, /*AllowExplicit=*/true)
4087 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4088 return NestedLoopCount;
4090 auto &C = SemaRef.Context;
4091 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4093 Scope *CurScope = DSA.getCurScope();
4094 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
4095 if (PreCond.isUsable()) {
4097 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4098 PreCond.get(), IterSpaces[Cnt].PreCond);
4100 auto N = IterSpaces[Cnt].NumIterations;
4101 SourceLocation Loc = N->getExprLoc();
4102 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4103 if (LastIteration32.isUsable())
4104 LastIteration32 = SemaRef.BuildBinOp(
4105 CurScope, Loc, BO_Mul, LastIteration32.get(),
4107 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4108 Sema::AA_Converting,
4109 /*AllowExplicit=*/true)
4111 if (LastIteration64.isUsable())
4112 LastIteration64 = SemaRef.BuildBinOp(
4113 CurScope, Loc, BO_Mul, LastIteration64.get(),
4115 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4116 Sema::AA_Converting,
4117 /*AllowExplicit=*/true)
4121 // Choose either the 32-bit or 64-bit version.
4122 ExprResult LastIteration = LastIteration64;
4123 if (LastIteration32.isUsable() &&
4124 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4125 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4128 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4129 LastIteration64.get(), SemaRef)))
4130 LastIteration = LastIteration32;
4131 QualType VType = LastIteration.get()->getType();
4132 QualType RealVType = VType;
4133 QualType StrideVType = VType;
4134 if (isOpenMPTaskLoopDirective(DKind)) {
4136 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4138 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4141 if (!LastIteration.isUsable())
4144 // Save the number of iterations.
4145 ExprResult NumIterations = LastIteration;
4147 LastIteration = SemaRef.BuildBinOp(
4148 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4149 LastIteration.get(),
4150 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4151 if (!LastIteration.isUsable())
4155 // Calculate the last iteration number beforehand instead of doing this on
4156 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4157 llvm::APSInt Result;
4159 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4160 ExprResult CalcLastIteration;
4162 ExprResult SaveRef =
4163 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
4164 LastIteration = SaveRef;
4166 // Prepare SaveRef + 1.
4167 NumIterations = SemaRef.BuildBinOp(
4168 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
4169 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4170 if (!NumIterations.isUsable())
4174 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4176 // Build variables passed into runtime, necessary for worksharing directives.
4177 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
4178 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4179 isOpenMPDistributeDirective(DKind)) {
4180 // Lower bound variable, initialized with zero.
4181 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4182 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
4183 SemaRef.AddInitializerToDecl(LBDecl,
4184 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4185 /*DirectInit*/ false);
4187 // Upper bound variable, initialized with last iteration number.
4188 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4189 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
4190 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4191 /*DirectInit*/ false);
4193 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4194 // This will be used to implement clause 'lastprivate'.
4195 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
4196 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4197 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
4198 SemaRef.AddInitializerToDecl(ILDecl,
4199 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4200 /*DirectInit*/ false);
4202 // Stride variable returned by runtime (we initialize it to 1 by default).
4204 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4205 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
4206 SemaRef.AddInitializerToDecl(STDecl,
4207 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4208 /*DirectInit*/ false);
4210 // Build expression: UB = min(UB, LastIteration)
4211 // It is necessary for CodeGen of directives with static scheduling.
4212 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4213 UB.get(), LastIteration.get());
4214 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4215 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4216 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4218 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4220 // If we have a combined directive that combines 'distribute', 'for' or
4221 // 'simd' we need to be able to access the bounds of the schedule of the
4222 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4223 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4224 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4226 // Lower bound variable, initialized with zero.
4227 VarDecl *CombLBDecl =
4228 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4229 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4230 SemaRef.AddInitializerToDecl(
4231 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4232 /*DirectInit*/ false);
4234 // Upper bound variable, initialized with last iteration number.
4235 VarDecl *CombUBDecl =
4236 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4237 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4238 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4239 /*DirectInit*/ false);
4241 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4242 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4243 ExprResult CombCondOp =
4244 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4245 LastIteration.get(), CombUB.get());
4246 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4248 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4250 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
4251 // We expect to have at least 2 more parameters than the 'parallel'
4252 // directive does - the lower and upper bounds of the previous schedule.
4253 assert(CD->getNumParams() >= 4 &&
4254 "Unexpected number of parameters in loop combined directive");
4256 // Set the proper type for the bounds given what we learned from the
4258 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4259 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4261 // Previous lower and upper bounds are obtained from the region
4264 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4266 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4270 // Build the iteration variable and its initialization before loop.
4272 ExprResult Init, CombInit;
4274 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4275 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
4277 (isOpenMPWorksharingDirective(DKind) ||
4278 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4280 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4281 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4282 Init = SemaRef.ActOnFinishFullExpr(Init.get());
4284 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4286 (isOpenMPWorksharingDirective(DKind) ||
4287 isOpenMPTaskLoopDirective(DKind) ||
4288 isOpenMPDistributeDirective(DKind))
4290 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4292 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4293 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4297 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
4298 SourceLocation CondLoc;
4300 (isOpenMPWorksharingDirective(DKind) ||
4301 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4302 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4303 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4304 NumIterations.get());
4305 ExprResult CombCond;
4306 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4308 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
4310 // Loop increment (IV = IV + 1)
4311 SourceLocation IncLoc;
4313 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4314 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4315 if (!Inc.isUsable())
4317 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
4318 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4319 if (!Inc.isUsable())
4322 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4323 // Used for directives with static scheduling.
4324 // In combined construct, add combined version that use CombLB and CombUB
4325 // base variables for the update
4326 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
4327 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4328 isOpenMPDistributeDirective(DKind)) {
4330 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4331 if (!NextLB.isUsable())
4335 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4336 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4337 if (!NextLB.isUsable())
4340 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4341 if (!NextUB.isUsable())
4345 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4346 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4347 if (!NextUB.isUsable())
4349 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4351 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
4352 if (!NextLB.isUsable())
4355 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
4357 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
4358 if (!CombNextLB.isUsable())
4362 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
4363 if (!CombNextUB.isUsable())
4366 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
4368 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
4369 if (!CombNextUB.isUsable())
4374 // Create increment expression for distribute loop when combined in a same
4375 // directive with for as IV = IV + ST; ensure upper bound expression based
4376 // on PrevUB instead of NumIterations - used to implement 'for' when found
4377 // in combination with 'distribute', like in 'distribute parallel for'
4378 SourceLocation DistIncLoc;
4379 ExprResult DistCond, DistInc, PrevEUB;
4380 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4381 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
4382 assert(DistCond.isUsable() && "distribute cond expr was not built");
4385 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
4386 assert(DistInc.isUsable() && "distribute inc expr was not built");
4387 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
4389 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
4390 assert(DistInc.isUsable() && "distribute inc expr was not built");
4392 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
4394 SourceLocation DistEUBLoc;
4395 ExprResult IsUBGreater =
4396 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
4397 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4398 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
4399 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
4401 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
4404 // Build updates and final values of the loop counters.
4405 bool HasErrors = false;
4406 Built.Counters.resize(NestedLoopCount);
4407 Built.Inits.resize(NestedLoopCount);
4408 Built.Updates.resize(NestedLoopCount);
4409 Built.Finals.resize(NestedLoopCount);
4410 SmallVector<Expr *, 4> LoopMultipliers;
4413 // Go from inner nested loop to outer.
4414 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4415 LoopIterationSpace &IS = IterSpaces[Cnt];
4416 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4417 // Build: Iter = (IV / Div) % IS.NumIters
4418 // where Div is product of previous iterations' IS.NumIters.
4420 if (Div.isUsable()) {
4422 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4425 assert((Cnt == (int)NestedLoopCount - 1) &&
4426 "unusable div expected on first iteration only");
4429 if (Cnt != 0 && Iter.isUsable())
4430 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4432 if (!Iter.isUsable()) {
4437 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4438 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4439 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4440 IS.CounterVar->getExprLoc(),
4441 /*RefersToCapture=*/true);
4442 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4443 IS.CounterInit, Captures);
4444 if (!Init.isUsable()) {
4448 ExprResult Update = BuildCounterUpdate(
4449 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4450 IS.CounterStep, IS.Subtract, &Captures);
4451 if (!Update.isUsable()) {
4456 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4457 ExprResult Final = BuildCounterUpdate(
4458 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
4459 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
4460 if (!Final.isUsable()) {
4465 // Build Div for the next iteration: Div <- Div * IS.NumIters
4468 Div = IS.NumIterations;
4470 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4473 // Add parentheses (for debugging purposes only).
4475 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
4476 if (!Div.isUsable()) {
4480 LoopMultipliers.push_back(Div.get());
4482 if (!Update.isUsable() || !Final.isUsable()) {
4487 Built.Counters[Cnt] = IS.CounterVar;
4488 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
4489 Built.Inits[Cnt] = Init.get();
4490 Built.Updates[Cnt] = Update.get();
4491 Built.Finals[Cnt] = Final.get();
4499 Built.IterationVarRef = IV.get();
4500 Built.LastIteration = LastIteration.get();
4501 Built.NumIterations = NumIterations.get();
4502 Built.CalcLastIteration =
4503 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
4504 Built.PreCond = PreCond.get();
4505 Built.PreInits = buildPreInits(C, Captures);
4506 Built.Cond = Cond.get();
4507 Built.Init = Init.get();
4508 Built.Inc = Inc.get();
4509 Built.LB = LB.get();
4510 Built.UB = UB.get();
4511 Built.IL = IL.get();
4512 Built.ST = ST.get();
4513 Built.EUB = EUB.get();
4514 Built.NLB = NextLB.get();
4515 Built.NUB = NextUB.get();
4516 Built.PrevLB = PrevLB.get();
4517 Built.PrevUB = PrevUB.get();
4518 Built.DistInc = DistInc.get();
4519 Built.PrevEUB = PrevEUB.get();
4520 Built.DistCombinedFields.LB = CombLB.get();
4521 Built.DistCombinedFields.UB = CombUB.get();
4522 Built.DistCombinedFields.EUB = CombEUB.get();
4523 Built.DistCombinedFields.Init = CombInit.get();
4524 Built.DistCombinedFields.Cond = CombCond.get();
4525 Built.DistCombinedFields.NLB = CombNextLB.get();
4526 Built.DistCombinedFields.NUB = CombNextUB.get();
4528 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
4529 // Fill data for doacross depend clauses.
4530 for (auto Pair : DSA.getDoacrossDependClauses()) {
4531 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4532 Pair.first->setCounterValue(CounterVal);
4534 if (NestedLoopCount != Pair.second.size() ||
4535 NestedLoopCount != LoopMultipliers.size() + 1) {
4536 // Erroneous case - clause has some problems.
4537 Pair.first->setCounterValue(CounterVal);
4540 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
4541 auto I = Pair.second.rbegin();
4542 auto IS = IterSpaces.rbegin();
4543 auto ILM = LoopMultipliers.rbegin();
4544 Expr *UpCounterVal = CounterVal;
4545 Expr *Multiplier = nullptr;
4546 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4548 assert(IS->CounterStep);
4549 Expr *NormalizedOffset =
4551 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
4552 I->first, IS->CounterStep)
4557 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
4558 NormalizedOffset, Multiplier)
4561 assert(I->second == OO_Plus || I->second == OO_Minus);
4562 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
4563 UpCounterVal = SemaRef
4564 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
4565 UpCounterVal, NormalizedOffset)
4573 Pair.first->setCounterValue(UpCounterVal);
4577 return NestedLoopCount;
4580 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
4581 auto CollapseClauses =
4582 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4583 if (CollapseClauses.begin() != CollapseClauses.end())
4584 return (*CollapseClauses.begin())->getNumForLoops();
4588 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
4589 auto OrderedClauses =
4590 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4591 if (OrderedClauses.begin() != OrderedClauses.end())
4592 return (*OrderedClauses.begin())->getNumForLoops();
4596 static bool checkSimdlenSafelenSpecified(Sema &S,
4597 const ArrayRef<OMPClause *> Clauses) {
4598 OMPSafelenClause *Safelen = nullptr;
4599 OMPSimdlenClause *Simdlen = nullptr;
4601 for (auto *Clause : Clauses) {
4602 if (Clause->getClauseKind() == OMPC_safelen)
4603 Safelen = cast<OMPSafelenClause>(Clause);
4604 else if (Clause->getClauseKind() == OMPC_simdlen)
4605 Simdlen = cast<OMPSimdlenClause>(Clause);
4606 if (Safelen && Simdlen)
4610 if (Simdlen && Safelen) {
4611 llvm::APSInt SimdlenRes, SafelenRes;
4612 auto SimdlenLength = Simdlen->getSimdlen();
4613 auto SafelenLength = Safelen->getSafelen();
4614 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
4615 SimdlenLength->isInstantiationDependent() ||
4616 SimdlenLength->containsUnexpandedParameterPack())
4618 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
4619 SafelenLength->isInstantiationDependent() ||
4620 SafelenLength->containsUnexpandedParameterPack())
4622 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
4623 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
4624 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
4625 // If both simdlen and safelen clauses are specified, the value of the
4626 // simdlen parameter must be less than or equal to the value of the safelen
4628 if (SimdlenRes > SafelenRes) {
4629 S.Diag(SimdlenLength->getExprLoc(),
4630 diag::err_omp_wrong_simdlen_safelen_values)
4631 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
4638 StmtResult Sema::ActOnOpenMPSimdDirective(
4639 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4640 SourceLocation EndLoc,
4641 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4645 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4646 OMPLoopDirective::HelperExprs B;
4647 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4648 // define the nested loops number.
4649 unsigned NestedLoopCount = CheckOpenMPLoop(
4650 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4651 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
4652 if (NestedLoopCount == 0)
4655 assert((CurContext->isDependentContext() || B.builtAll()) &&
4656 "omp simd loop exprs were not built");
4658 if (!CurContext->isDependentContext()) {
4659 // Finalize the clauses that need pre-built expressions for CodeGen.
4660 for (auto C : Clauses) {
4661 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4662 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4663 B.NumIterations, *this, CurScope,
4669 if (checkSimdlenSafelenSpecified(*this, Clauses))
4672 getCurFunction()->setHasBranchProtectedScope();
4673 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4677 StmtResult Sema::ActOnOpenMPForDirective(
4678 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4679 SourceLocation EndLoc,
4680 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4684 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4685 OMPLoopDirective::HelperExprs B;
4686 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4687 // define the nested loops number.
4688 unsigned NestedLoopCount = CheckOpenMPLoop(
4689 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4690 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
4691 if (NestedLoopCount == 0)
4694 assert((CurContext->isDependentContext() || B.builtAll()) &&
4695 "omp for loop exprs were not built");
4697 if (!CurContext->isDependentContext()) {
4698 // Finalize the clauses that need pre-built expressions for CodeGen.
4699 for (auto C : Clauses) {
4700 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4701 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4702 B.NumIterations, *this, CurScope,
4708 getCurFunction()->setHasBranchProtectedScope();
4709 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4710 Clauses, AStmt, B, DSAStack->isCancelRegion());
4713 StmtResult Sema::ActOnOpenMPForSimdDirective(
4714 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4715 SourceLocation EndLoc,
4716 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4720 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4721 OMPLoopDirective::HelperExprs B;
4722 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4723 // define the nested loops number.
4724 unsigned NestedLoopCount =
4725 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4726 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4727 VarsWithImplicitDSA, B);
4728 if (NestedLoopCount == 0)
4731 assert((CurContext->isDependentContext() || B.builtAll()) &&
4732 "omp for simd loop exprs were not built");
4734 if (!CurContext->isDependentContext()) {
4735 // Finalize the clauses that need pre-built expressions for CodeGen.
4736 for (auto C : Clauses) {
4737 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4738 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4739 B.NumIterations, *this, CurScope,
4745 if (checkSimdlenSafelenSpecified(*this, Clauses))
4748 getCurFunction()->setHasBranchProtectedScope();
4749 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4753 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4755 SourceLocation StartLoc,
4756 SourceLocation EndLoc) {
4760 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4761 auto BaseStmt = AStmt;
4762 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4763 BaseStmt = CS->getCapturedStmt();
4764 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4765 auto S = C->children();
4766 if (S.begin() == S.end())
4768 // All associated statements must be '#pragma omp section' except for
4770 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
4771 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4773 Diag(SectionStmt->getLocStart(),
4774 diag::err_omp_sections_substmt_not_section);
4777 cast<OMPSectionDirective>(SectionStmt)
4778 ->setHasCancel(DSAStack->isCancelRegion());
4781 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4785 getCurFunction()->setHasBranchProtectedScope();
4787 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4788 DSAStack->isCancelRegion());
4791 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4792 SourceLocation StartLoc,
4793 SourceLocation EndLoc) {
4797 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4799 getCurFunction()->setHasBranchProtectedScope();
4800 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
4802 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4803 DSAStack->isCancelRegion());
4806 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4808 SourceLocation StartLoc,
4809 SourceLocation EndLoc) {
4813 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4815 getCurFunction()->setHasBranchProtectedScope();
4817 // OpenMP [2.7.3, single Construct, Restrictions]
4818 // The copyprivate clause must not be used with the nowait clause.
4819 OMPClause *Nowait = nullptr;
4820 OMPClause *Copyprivate = nullptr;
4821 for (auto *Clause : Clauses) {
4822 if (Clause->getClauseKind() == OMPC_nowait)
4824 else if (Clause->getClauseKind() == OMPC_copyprivate)
4825 Copyprivate = Clause;
4826 if (Copyprivate && Nowait) {
4827 Diag(Copyprivate->getLocStart(),
4828 diag::err_omp_single_copyprivate_with_nowait);
4829 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4834 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4837 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4838 SourceLocation StartLoc,
4839 SourceLocation EndLoc) {
4843 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4845 getCurFunction()->setHasBranchProtectedScope();
4847 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4850 StmtResult Sema::ActOnOpenMPCriticalDirective(
4851 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4852 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
4856 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4858 bool ErrorFound = false;
4860 SourceLocation HintLoc;
4861 bool DependentHint = false;
4862 for (auto *C : Clauses) {
4863 if (C->getClauseKind() == OMPC_hint) {
4864 if (!DirName.getName()) {
4865 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4868 Expr *E = cast<OMPHintClause>(C)->getHint();
4869 if (E->isTypeDependent() || E->isValueDependent() ||
4870 E->isInstantiationDependent())
4871 DependentHint = true;
4873 Hint = E->EvaluateKnownConstInt(Context);
4874 HintLoc = C->getLocStart();
4880 auto Pair = DSAStack->getCriticalWithHint(DirName);
4881 if (Pair.first && DirName.getName() && !DependentHint) {
4882 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4883 Diag(StartLoc, diag::err_omp_critical_with_hint);
4884 if (HintLoc.isValid()) {
4885 Diag(HintLoc, diag::note_omp_critical_hint_here)
4886 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4888 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4889 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4890 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4892 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4893 /*Radix=*/10, /*Signed=*/false);
4895 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4899 getCurFunction()->setHasBranchProtectedScope();
4901 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4903 if (!Pair.first && DirName.getName() && !DependentHint)
4904 DSAStack->addCriticalWithHint(Dir, Hint);
4908 StmtResult Sema::ActOnOpenMPParallelForDirective(
4909 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4910 SourceLocation EndLoc,
4911 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4915 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4916 // 1.2.2 OpenMP Language Terminology
4917 // Structured block - An executable statement with a single entry at the
4918 // top and a single exit at the bottom.
4919 // The point of exit cannot be a branch out of the structured block.
4920 // longjmp() and throw() must not violate the entry/exit criteria.
4921 CS->getCapturedDecl()->setNothrow();
4923 OMPLoopDirective::HelperExprs B;
4924 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4925 // define the nested loops number.
4926 unsigned NestedLoopCount =
4927 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4928 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4929 VarsWithImplicitDSA, B);
4930 if (NestedLoopCount == 0)
4933 assert((CurContext->isDependentContext() || B.builtAll()) &&
4934 "omp parallel for loop exprs were not built");
4936 if (!CurContext->isDependentContext()) {
4937 // Finalize the clauses that need pre-built expressions for CodeGen.
4938 for (auto C : Clauses) {
4939 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4940 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4941 B.NumIterations, *this, CurScope,
4947 getCurFunction()->setHasBranchProtectedScope();
4948 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
4949 NestedLoopCount, Clauses, AStmt, B,
4950 DSAStack->isCancelRegion());
4953 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4954 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4955 SourceLocation EndLoc,
4956 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4960 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4961 // 1.2.2 OpenMP Language Terminology
4962 // Structured block - An executable statement with a single entry at the
4963 // top and a single exit at the bottom.
4964 // The point of exit cannot be a branch out of the structured block.
4965 // longjmp() and throw() must not violate the entry/exit criteria.
4966 CS->getCapturedDecl()->setNothrow();
4968 OMPLoopDirective::HelperExprs B;
4969 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4970 // define the nested loops number.
4971 unsigned NestedLoopCount =
4972 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4973 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4974 VarsWithImplicitDSA, B);
4975 if (NestedLoopCount == 0)
4978 if (!CurContext->isDependentContext()) {
4979 // Finalize the clauses that need pre-built expressions for CodeGen.
4980 for (auto C : Clauses) {
4981 if (auto *LC = dyn_cast<OMPLinearClause>(C))
4982 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4983 B.NumIterations, *this, CurScope,
4989 if (checkSimdlenSafelenSpecified(*this, Clauses))
4992 getCurFunction()->setHasBranchProtectedScope();
4993 return OMPParallelForSimdDirective::Create(
4994 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
4998 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4999 Stmt *AStmt, SourceLocation StartLoc,
5000 SourceLocation EndLoc) {
5004 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5005 auto BaseStmt = AStmt;
5006 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5007 BaseStmt = CS->getCapturedStmt();
5008 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5009 auto S = C->children();
5010 if (S.begin() == S.end())
5012 // All associated statements must be '#pragma omp section' except for
5014 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5015 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5017 Diag(SectionStmt->getLocStart(),
5018 diag::err_omp_parallel_sections_substmt_not_section);
5021 cast<OMPSectionDirective>(SectionStmt)
5022 ->setHasCancel(DSAStack->isCancelRegion());
5025 Diag(AStmt->getLocStart(),
5026 diag::err_omp_parallel_sections_not_compound_stmt);
5030 getCurFunction()->setHasBranchProtectedScope();
5032 return OMPParallelSectionsDirective::Create(
5033 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
5036 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5037 Stmt *AStmt, SourceLocation StartLoc,
5038 SourceLocation EndLoc) {
5042 auto *CS = cast<CapturedStmt>(AStmt);
5043 // 1.2.2 OpenMP Language Terminology
5044 // Structured block - An executable statement with a single entry at the
5045 // top and a single exit at the bottom.
5046 // The point of exit cannot be a branch out of the structured block.
5047 // longjmp() and throw() must not violate the entry/exit criteria.
5048 CS->getCapturedDecl()->setNothrow();
5050 getCurFunction()->setHasBranchProtectedScope();
5052 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5053 DSAStack->isCancelRegion());
5056 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5057 SourceLocation EndLoc) {
5058 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5061 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5062 SourceLocation EndLoc) {
5063 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5066 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5067 SourceLocation EndLoc) {
5068 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5071 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5073 SourceLocation StartLoc,
5074 SourceLocation EndLoc) {
5078 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5080 getCurFunction()->setHasBranchProtectedScope();
5082 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
5086 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5087 SourceLocation StartLoc,
5088 SourceLocation EndLoc) {
5089 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5090 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5093 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5095 SourceLocation StartLoc,
5096 SourceLocation EndLoc) {
5097 OMPClause *DependFound = nullptr;
5098 OMPClause *DependSourceClause = nullptr;
5099 OMPClause *DependSinkClause = nullptr;
5100 bool ErrorFound = false;
5101 OMPThreadsClause *TC = nullptr;
5102 OMPSIMDClause *SC = nullptr;
5103 for (auto *C : Clauses) {
5104 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5106 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5107 if (DependSourceClause) {
5108 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5109 << getOpenMPDirectiveName(OMPD_ordered)
5110 << getOpenMPClauseName(OMPC_depend) << 2;
5113 DependSourceClause = C;
5114 if (DependSinkClause) {
5115 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5119 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5120 if (DependSourceClause) {
5121 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5125 DependSinkClause = C;
5127 } else if (C->getClauseKind() == OMPC_threads)
5128 TC = cast<OMPThreadsClause>(C);
5129 else if (C->getClauseKind() == OMPC_simd)
5130 SC = cast<OMPSIMDClause>(C);
5132 if (!ErrorFound && !SC &&
5133 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
5134 // OpenMP [2.8.1,simd Construct, Restrictions]
5135 // An ordered construct with the simd clause is the only OpenMP construct
5136 // that can appear in the simd region.
5137 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5139 } else if (DependFound && (TC || SC)) {
5140 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5141 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5143 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5144 Diag(DependFound->getLocStart(),
5145 diag::err_omp_ordered_directive_without_param);
5147 } else if (TC || Clauses.empty()) {
5148 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5149 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5150 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5152 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5156 if ((!AStmt && !DependFound) || ErrorFound)
5160 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5162 getCurFunction()->setHasBranchProtectedScope();
5165 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5169 /// \brief Helper class for checking expression in 'omp atomic [update]'
5171 class OpenMPAtomicUpdateChecker {
5172 /// \brief Error results for atomic update expressions.
5173 enum ExprAnalysisErrorCode {
5174 /// \brief A statement is not an expression statement.
5176 /// \brief Expression is not builtin binary or unary operation.
5177 NotABinaryOrUnaryExpression,
5178 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5179 NotAnUnaryIncDecExpression,
5180 /// \brief An expression is not of scalar type.
5182 /// \brief A binary operation is not an assignment operation.
5184 /// \brief RHS part of the binary operation is not a binary expression.
5185 NotABinaryExpression,
5186 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5189 /// \brief RHS binary operation does not have reference to the updated LHS
5191 NotAnUpdateExpression,
5192 /// \brief No errors is found.
5195 /// \brief Reference to Sema.
5197 /// \brief A location for note diagnostics (when error is found).
5198 SourceLocation NoteLoc;
5199 /// \brief 'x' lvalue part of the source atomic expression.
5201 /// \brief 'expr' rvalue part of the source atomic expression.
5203 /// \brief Helper expression of the form
5204 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5205 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5207 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5208 /// important for non-associative operations.
5209 bool IsXLHSInRHSPart;
5210 BinaryOperatorKind Op;
5211 SourceLocation OpLoc;
5212 /// \brief true if the source expression is a postfix unary operation, false
5213 /// if it is a prefix unary operation.
5214 bool IsPostfixUpdate;
5217 OpenMPAtomicUpdateChecker(Sema &SemaRef)
5218 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
5219 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
5220 /// \brief Check specified statement that it is suitable for 'atomic update'
5221 /// constructs and extract 'x', 'expr' and Operation from the original
5222 /// expression. If DiagId and NoteId == 0, then only check is performed
5223 /// without error notification.
5224 /// \param DiagId Diagnostic which should be emitted if error is found.
5225 /// \param NoteId Diagnostic note for the main error message.
5226 /// \return true if statement is not an update expression, false otherwise.
5227 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
5228 /// \brief Return the 'x' lvalue part of the source atomic expression.
5229 Expr *getX() const { return X; }
5230 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5231 Expr *getExpr() const { return E; }
5232 /// \brief Return the update expression used in calculation of the updated
5233 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5234 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5235 Expr *getUpdateExpr() const { return UpdateExpr; }
5236 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5237 /// false otherwise.
5238 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5240 /// \brief true if the source expression is a postfix unary operation, false
5241 /// if it is a prefix unary operation.
5242 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5245 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5246 unsigned NoteId = 0);
5250 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5251 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5252 ExprAnalysisErrorCode ErrorFound = NoError;
5253 SourceLocation ErrorLoc, NoteLoc;
5254 SourceRange ErrorRange, NoteRange;
5255 // Allowed constructs are:
5256 // x = x binop expr;
5257 // x = expr binop x;
5258 if (AtomicBinOp->getOpcode() == BO_Assign) {
5259 X = AtomicBinOp->getLHS();
5260 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5261 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5262 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5263 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5264 AtomicInnerBinOp->isBitwiseOp()) {
5265 Op = AtomicInnerBinOp->getOpcode();
5266 OpLoc = AtomicInnerBinOp->getOperatorLoc();
5267 auto *LHS = AtomicInnerBinOp->getLHS();
5268 auto *RHS = AtomicInnerBinOp->getRHS();
5269 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5270 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5271 /*Canonical=*/true);
5272 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5273 /*Canonical=*/true);
5274 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5275 /*Canonical=*/true);
5278 IsXLHSInRHSPart = true;
5279 } else if (XId == RHSId) {
5281 IsXLHSInRHSPart = false;
5283 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5284 ErrorRange = AtomicInnerBinOp->getSourceRange();
5285 NoteLoc = X->getExprLoc();
5286 NoteRange = X->getSourceRange();
5287 ErrorFound = NotAnUpdateExpression;
5290 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5291 ErrorRange = AtomicInnerBinOp->getSourceRange();
5292 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5293 NoteRange = SourceRange(NoteLoc, NoteLoc);
5294 ErrorFound = NotABinaryOperator;
5297 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5298 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5299 ErrorFound = NotABinaryExpression;
5302 ErrorLoc = AtomicBinOp->getExprLoc();
5303 ErrorRange = AtomicBinOp->getSourceRange();
5304 NoteLoc = AtomicBinOp->getOperatorLoc();
5305 NoteRange = SourceRange(NoteLoc, NoteLoc);
5306 ErrorFound = NotAnAssignmentOp;
5308 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5309 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5310 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5312 } else if (SemaRef.CurContext->isDependentContext())
5313 E = X = UpdateExpr = nullptr;
5314 return ErrorFound != NoError;
5317 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5319 ExprAnalysisErrorCode ErrorFound = NoError;
5320 SourceLocation ErrorLoc, NoteLoc;
5321 SourceRange ErrorRange, NoteRange;
5322 // Allowed constructs are:
5328 // x = x binop expr;
5329 // x = expr binop x;
5330 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5331 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5332 if (AtomicBody->getType()->isScalarType() ||
5333 AtomicBody->isInstantiationDependent()) {
5334 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5335 AtomicBody->IgnoreParenImpCasts())) {
5336 // Check for Compound Assignment Operation
5337 Op = BinaryOperator::getOpForCompoundAssignment(
5338 AtomicCompAssignOp->getOpcode());
5339 OpLoc = AtomicCompAssignOp->getOperatorLoc();
5340 E = AtomicCompAssignOp->getRHS();
5341 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
5342 IsXLHSInRHSPart = true;
5343 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5344 AtomicBody->IgnoreParenImpCasts())) {
5345 // Check for Binary Operation
5346 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5348 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
5349 AtomicBody->IgnoreParenImpCasts())) {
5350 // Check for Unary Operation
5351 if (AtomicUnaryOp->isIncrementDecrementOp()) {
5352 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
5353 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5354 OpLoc = AtomicUnaryOp->getOperatorLoc();
5355 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
5356 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5357 IsXLHSInRHSPart = true;
5359 ErrorFound = NotAnUnaryIncDecExpression;
5360 ErrorLoc = AtomicUnaryOp->getExprLoc();
5361 ErrorRange = AtomicUnaryOp->getSourceRange();
5362 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5363 NoteRange = SourceRange(NoteLoc, NoteLoc);
5365 } else if (!AtomicBody->isInstantiationDependent()) {
5366 ErrorFound = NotABinaryOrUnaryExpression;
5367 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5368 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5371 ErrorFound = NotAScalarType;
5372 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5373 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5376 ErrorFound = NotAnExpression;
5377 NoteLoc = ErrorLoc = S->getLocStart();
5378 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5380 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5381 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5382 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5384 } else if (SemaRef.CurContext->isDependentContext())
5385 E = X = UpdateExpr = nullptr;
5386 if (ErrorFound == NoError && E && X) {
5387 // Build an update expression of form 'OpaqueValueExpr(x) binop
5388 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5389 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5390 auto *OVEX = new (SemaRef.getASTContext())
5391 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5392 auto *OVEExpr = new (SemaRef.getASTContext())
5393 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5395 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5396 IsXLHSInRHSPart ? OVEExpr : OVEX);
5397 if (Update.isInvalid())
5399 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5401 if (Update.isInvalid())
5403 UpdateExpr = Update.get();
5405 return ErrorFound != NoError;
5408 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5410 SourceLocation StartLoc,
5411 SourceLocation EndLoc) {
5415 auto *CS = cast<CapturedStmt>(AStmt);
5416 // 1.2.2 OpenMP Language Terminology
5417 // Structured block - An executable statement with a single entry at the
5418 // top and a single exit at the bottom.
5419 // The point of exit cannot be a branch out of the structured block.
5420 // longjmp() and throw() must not violate the entry/exit criteria.
5421 OpenMPClauseKind AtomicKind = OMPC_unknown;
5422 SourceLocation AtomicKindLoc;
5423 for (auto *C : Clauses) {
5424 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
5425 C->getClauseKind() == OMPC_update ||
5426 C->getClauseKind() == OMPC_capture) {
5427 if (AtomicKind != OMPC_unknown) {
5428 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5429 << SourceRange(C->getLocStart(), C->getLocEnd());
5430 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5431 << getOpenMPClauseName(AtomicKind);
5433 AtomicKind = C->getClauseKind();
5434 AtomicKindLoc = C->getLocStart();
5439 auto Body = CS->getCapturedStmt();
5440 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5441 Body = EWC->getSubExpr();
5447 bool IsXLHSInRHSPart = false;
5448 bool IsPostfixUpdate = false;
5449 // OpenMP [2.12.6, atomic Construct]
5450 // In the next expressions:
5451 // * x and v (as applicable) are both l-value expressions with scalar type.
5452 // * During the execution of an atomic region, multiple syntactic
5453 // occurrences of x must designate the same storage location.
5454 // * Neither of v and expr (as applicable) may access the storage location
5456 // * Neither of x and expr (as applicable) may access the storage location
5458 // * expr is an expression with scalar type.
5459 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5460 // * binop, binop=, ++, and -- are not overloaded operators.
5461 // * The expression x binop expr must be numerically equivalent to x binop
5462 // (expr). This requirement is satisfied if the operators in expr have
5463 // precedence greater than binop, or by using parentheses around expr or
5464 // subexpressions of expr.
5465 // * The expression expr binop x must be numerically equivalent to (expr)
5466 // binop x. This requirement is satisfied if the operators in expr have
5467 // precedence equal to or greater than binop, or by using parentheses around
5468 // expr or subexpressions of expr.
5469 // * For forms that allow multiple occurrences of x, the number of times
5470 // that x is evaluated is unspecified.
5471 if (AtomicKind == OMPC_read) {
5478 } ErrorFound = NoError;
5479 SourceLocation ErrorLoc, NoteLoc;
5480 SourceRange ErrorRange, NoteRange;
5481 // If clause is read:
5483 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5485 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5486 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5487 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5488 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5489 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5490 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5491 if (!X->isLValue() || !V->isLValue()) {
5492 auto NotLValueExpr = X->isLValue() ? V : X;
5493 ErrorFound = NotAnLValue;
5494 ErrorLoc = AtomicBinOp->getExprLoc();
5495 ErrorRange = AtomicBinOp->getSourceRange();
5496 NoteLoc = NotLValueExpr->getExprLoc();
5497 NoteRange = NotLValueExpr->getSourceRange();
5499 } else if (!X->isInstantiationDependent() ||
5500 !V->isInstantiationDependent()) {
5501 auto NotScalarExpr =
5502 (X->isInstantiationDependent() || X->getType()->isScalarType())
5505 ErrorFound = NotAScalarType;
5506 ErrorLoc = AtomicBinOp->getExprLoc();
5507 ErrorRange = AtomicBinOp->getSourceRange();
5508 NoteLoc = NotScalarExpr->getExprLoc();
5509 NoteRange = NotScalarExpr->getSourceRange();
5511 } else if (!AtomicBody->isInstantiationDependent()) {
5512 ErrorFound = NotAnAssignmentOp;
5513 ErrorLoc = AtomicBody->getExprLoc();
5514 ErrorRange = AtomicBody->getSourceRange();
5515 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5516 : AtomicBody->getExprLoc();
5517 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5518 : AtomicBody->getSourceRange();
5521 ErrorFound = NotAnExpression;
5522 NoteLoc = ErrorLoc = Body->getLocStart();
5523 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5525 if (ErrorFound != NoError) {
5526 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5528 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5531 } else if (CurContext->isDependentContext())
5533 } else if (AtomicKind == OMPC_write) {
5540 } ErrorFound = NoError;
5541 SourceLocation ErrorLoc, NoteLoc;
5542 SourceRange ErrorRange, NoteRange;
5543 // If clause is write:
5545 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5547 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5548 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5549 X = AtomicBinOp->getLHS();
5550 E = AtomicBinOp->getRHS();
5551 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5552 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5553 if (!X->isLValue()) {
5554 ErrorFound = NotAnLValue;
5555 ErrorLoc = AtomicBinOp->getExprLoc();
5556 ErrorRange = AtomicBinOp->getSourceRange();
5557 NoteLoc = X->getExprLoc();
5558 NoteRange = X->getSourceRange();
5560 } else if (!X->isInstantiationDependent() ||
5561 !E->isInstantiationDependent()) {
5562 auto NotScalarExpr =
5563 (X->isInstantiationDependent() || X->getType()->isScalarType())
5566 ErrorFound = NotAScalarType;
5567 ErrorLoc = AtomicBinOp->getExprLoc();
5568 ErrorRange = AtomicBinOp->getSourceRange();
5569 NoteLoc = NotScalarExpr->getExprLoc();
5570 NoteRange = NotScalarExpr->getSourceRange();
5572 } else if (!AtomicBody->isInstantiationDependent()) {
5573 ErrorFound = NotAnAssignmentOp;
5574 ErrorLoc = AtomicBody->getExprLoc();
5575 ErrorRange = AtomicBody->getSourceRange();
5576 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5577 : AtomicBody->getExprLoc();
5578 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5579 : AtomicBody->getSourceRange();
5582 ErrorFound = NotAnExpression;
5583 NoteLoc = ErrorLoc = Body->getLocStart();
5584 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5586 if (ErrorFound != NoError) {
5587 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5589 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5592 } else if (CurContext->isDependentContext())
5594 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
5595 // If clause is update:
5601 // x = x binop expr;
5602 // x = expr binop x;
5603 OpenMPAtomicUpdateChecker Checker(*this);
5604 if (Checker.checkStatement(
5605 Body, (AtomicKind == OMPC_update)
5606 ? diag::err_omp_atomic_update_not_expression_statement
5607 : diag::err_omp_atomic_not_expression_statement,
5608 diag::note_omp_atomic_update))
5610 if (!CurContext->isDependentContext()) {
5611 E = Checker.getExpr();
5613 UE = Checker.getUpdateExpr();
5614 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5616 } else if (AtomicKind == OMPC_capture) {
5619 NotACompoundStatement,
5620 NotTwoSubstatements,
5621 NotASpecificExpression,
5623 } ErrorFound = NoError;
5624 SourceLocation ErrorLoc, NoteLoc;
5625 SourceRange ErrorRange, NoteRange;
5626 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5627 // If clause is a capture:
5632 // v = x binop= expr;
5633 // v = x = x binop expr;
5634 // v = x = expr binop x;
5636 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5637 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5638 V = AtomicBinOp->getLHS();
5639 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5640 OpenMPAtomicUpdateChecker Checker(*this);
5641 if (Checker.checkStatement(
5642 Body, diag::err_omp_atomic_capture_not_expression_statement,
5643 diag::note_omp_atomic_update))
5645 E = Checker.getExpr();
5647 UE = Checker.getUpdateExpr();
5648 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5649 IsPostfixUpdate = Checker.isPostfixUpdate();
5650 } else if (!AtomicBody->isInstantiationDependent()) {
5651 ErrorLoc = AtomicBody->getExprLoc();
5652 ErrorRange = AtomicBody->getSourceRange();
5653 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5654 : AtomicBody->getExprLoc();
5655 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5656 : AtomicBody->getSourceRange();
5657 ErrorFound = NotAnAssignmentOp;
5659 if (ErrorFound != NoError) {
5660 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5662 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5664 } else if (CurContext->isDependentContext()) {
5665 UE = V = E = X = nullptr;
5668 // If clause is a capture:
5669 // { v = x; x = expr; }
5674 // { v = x; x binop= expr; }
5675 // { v = x; x = x binop expr; }
5676 // { v = x; x = expr binop x; }
5681 // { x binop= expr; v = x; }
5682 // { x = x binop expr; v = x; }
5683 // { x = expr binop x; v = x; }
5684 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5685 // Check that this is { expr1; expr2; }
5686 if (CS->size() == 2) {
5687 auto *First = CS->body_front();
5688 auto *Second = CS->body_back();
5689 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5690 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5691 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5692 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5693 // Need to find what subexpression is 'v' and what is 'x'.
5694 OpenMPAtomicUpdateChecker Checker(*this);
5695 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5696 BinaryOperator *BinOp = nullptr;
5697 if (IsUpdateExprFound) {
5698 BinOp = dyn_cast<BinaryOperator>(First);
5699 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5701 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5706 // { v = x; x binop= expr; }
5707 // { v = x; x = x binop expr; }
5708 // { v = x; x = expr binop x; }
5709 // Check that the first expression has form v = x.
5710 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5711 llvm::FoldingSetNodeID XId, PossibleXId;
5712 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5713 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5714 IsUpdateExprFound = XId == PossibleXId;
5715 if (IsUpdateExprFound) {
5716 V = BinOp->getLHS();
5718 E = Checker.getExpr();
5719 UE = Checker.getUpdateExpr();
5720 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5721 IsPostfixUpdate = true;
5724 if (!IsUpdateExprFound) {
5725 IsUpdateExprFound = !Checker.checkStatement(First);
5727 if (IsUpdateExprFound) {
5728 BinOp = dyn_cast<BinaryOperator>(Second);
5729 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5731 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5736 // { x binop= expr; v = x; }
5737 // { x = x binop expr; v = x; }
5738 // { x = expr binop x; v = x; }
5739 // Check that the second expression has form v = x.
5740 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5741 llvm::FoldingSetNodeID XId, PossibleXId;
5742 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5743 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5744 IsUpdateExprFound = XId == PossibleXId;
5745 if (IsUpdateExprFound) {
5746 V = BinOp->getLHS();
5748 E = Checker.getExpr();
5749 UE = Checker.getUpdateExpr();
5750 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5751 IsPostfixUpdate = false;
5755 if (!IsUpdateExprFound) {
5756 // { v = x; x = expr; }
5757 auto *FirstExpr = dyn_cast<Expr>(First);
5758 auto *SecondExpr = dyn_cast<Expr>(Second);
5759 if (!FirstExpr || !SecondExpr ||
5760 !(FirstExpr->isInstantiationDependent() ||
5761 SecondExpr->isInstantiationDependent())) {
5762 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5763 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
5764 ErrorFound = NotAnAssignmentOp;
5765 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5766 : First->getLocStart();
5767 NoteRange = ErrorRange = FirstBinOp
5768 ? FirstBinOp->getSourceRange()
5769 : SourceRange(ErrorLoc, ErrorLoc);
5771 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5772 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5773 ErrorFound = NotAnAssignmentOp;
5774 NoteLoc = ErrorLoc = SecondBinOp
5775 ? SecondBinOp->getOperatorLoc()
5776 : Second->getLocStart();
5777 NoteRange = ErrorRange =
5778 SecondBinOp ? SecondBinOp->getSourceRange()
5779 : SourceRange(ErrorLoc, ErrorLoc);
5781 auto *PossibleXRHSInFirst =
5782 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5783 auto *PossibleXLHSInSecond =
5784 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5785 llvm::FoldingSetNodeID X1Id, X2Id;
5786 PossibleXRHSInFirst->Profile(X1Id, Context,
5787 /*Canonical=*/true);
5788 PossibleXLHSInSecond->Profile(X2Id, Context,
5789 /*Canonical=*/true);
5790 IsUpdateExprFound = X1Id == X2Id;
5791 if (IsUpdateExprFound) {
5792 V = FirstBinOp->getLHS();
5793 X = SecondBinOp->getLHS();
5794 E = SecondBinOp->getRHS();
5796 IsXLHSInRHSPart = false;
5797 IsPostfixUpdate = true;
5799 ErrorFound = NotASpecificExpression;
5800 ErrorLoc = FirstBinOp->getExprLoc();
5801 ErrorRange = FirstBinOp->getSourceRange();
5802 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5803 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5810 NoteLoc = ErrorLoc = Body->getLocStart();
5811 NoteRange = ErrorRange =
5812 SourceRange(Body->getLocStart(), Body->getLocStart());
5813 ErrorFound = NotTwoSubstatements;
5816 NoteLoc = ErrorLoc = Body->getLocStart();
5817 NoteRange = ErrorRange =
5818 SourceRange(Body->getLocStart(), Body->getLocStart());
5819 ErrorFound = NotACompoundStatement;
5821 if (ErrorFound != NoError) {
5822 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5824 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5826 } else if (CurContext->isDependentContext()) {
5827 UE = V = E = X = nullptr;
5832 getCurFunction()->setHasBranchProtectedScope();
5834 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5835 X, V, E, UE, IsXLHSInRHSPart,
5839 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5841 SourceLocation StartLoc,
5842 SourceLocation EndLoc) {
5846 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5847 // 1.2.2 OpenMP Language Terminology
5848 // Structured block - An executable statement with a single entry at the
5849 // top and a single exit at the bottom.
5850 // The point of exit cannot be a branch out of the structured block.
5851 // longjmp() and throw() must not violate the entry/exit criteria.
5852 CS->getCapturedDecl()->setNothrow();
5854 // OpenMP [2.16, Nesting of Regions]
5855 // If specified, a teams construct must be contained within a target
5856 // construct. That target construct must contain no statements or directives
5857 // outside of the teams construct.
5858 if (DSAStack->hasInnerTeamsRegion()) {
5859 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5860 bool OMPTeamsFound = true;
5861 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5862 auto I = CS->body_begin();
5863 while (I != CS->body_end()) {
5864 auto *OED = dyn_cast<OMPExecutableDirective>(*I);
5865 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5866 OMPTeamsFound = false;
5871 assert(I != CS->body_end() && "Not found statement");
5874 auto *OED = dyn_cast<OMPExecutableDirective>(S);
5875 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
5877 if (!OMPTeamsFound) {
5878 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5879 Diag(DSAStack->getInnerTeamsRegionLoc(),
5880 diag::note_omp_nested_teams_construct_here);
5881 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5882 << isa<OMPExecutableDirective>(S);
5887 getCurFunction()->setHasBranchProtectedScope();
5889 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5893 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5894 Stmt *AStmt, SourceLocation StartLoc,
5895 SourceLocation EndLoc) {
5899 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5900 // 1.2.2 OpenMP Language Terminology
5901 // Structured block - An executable statement with a single entry at the
5902 // top and a single exit at the bottom.
5903 // The point of exit cannot be a branch out of the structured block.
5904 // longjmp() and throw() must not violate the entry/exit criteria.
5905 CS->getCapturedDecl()->setNothrow();
5907 getCurFunction()->setHasBranchProtectedScope();
5909 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5913 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5914 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5915 SourceLocation EndLoc,
5916 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5920 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5921 // 1.2.2 OpenMP Language Terminology
5922 // Structured block - An executable statement with a single entry at the
5923 // top and a single exit at the bottom.
5924 // The point of exit cannot be a branch out of the structured block.
5925 // longjmp() and throw() must not violate the entry/exit criteria.
5926 CS->getCapturedDecl()->setNothrow();
5928 OMPLoopDirective::HelperExprs B;
5929 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5930 // define the nested loops number.
5931 unsigned NestedLoopCount =
5932 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5933 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5934 VarsWithImplicitDSA, B);
5935 if (NestedLoopCount == 0)
5938 assert((CurContext->isDependentContext() || B.builtAll()) &&
5939 "omp target parallel for loop exprs were not built");
5941 if (!CurContext->isDependentContext()) {
5942 // Finalize the clauses that need pre-built expressions for CodeGen.
5943 for (auto C : Clauses) {
5944 if (auto *LC = dyn_cast<OMPLinearClause>(C))
5945 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5946 B.NumIterations, *this, CurScope,
5952 getCurFunction()->setHasBranchProtectedScope();
5953 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5954 NestedLoopCount, Clauses, AStmt,
5955 B, DSAStack->isCancelRegion());
5958 /// Check for existence of a map clause in the list of clauses.
5959 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
5960 const OpenMPClauseKind K) {
5961 return llvm::any_of(
5962 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
5965 template <typename... Params>
5966 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
5967 const Params... ClauseTypes) {
5968 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
5971 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5973 SourceLocation StartLoc,
5974 SourceLocation EndLoc) {
5978 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5980 // OpenMP [2.10.1, Restrictions, p. 97]
5981 // At least one map clause must appear on the directive.
5982 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
5983 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
5984 << "'map' or 'use_device_ptr'"
5985 << getOpenMPDirectiveName(OMPD_target_data);
5989 getCurFunction()->setHasBranchProtectedScope();
5991 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5996 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5997 SourceLocation StartLoc,
5998 SourceLocation EndLoc) {
5999 // OpenMP [2.10.2, Restrictions, p. 99]
6000 // At least one map clause must appear on the directive.
6001 if (!hasClauses(Clauses, OMPC_map)) {
6002 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6003 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
6007 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6012 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6013 SourceLocation StartLoc,
6014 SourceLocation EndLoc) {
6015 // OpenMP [2.10.3, Restrictions, p. 102]
6016 // At least one map clause must appear on the directive.
6017 if (!hasClauses(Clauses, OMPC_map)) {
6018 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6019 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
6023 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6026 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6027 SourceLocation StartLoc,
6028 SourceLocation EndLoc) {
6029 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
6030 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6033 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6036 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6037 Stmt *AStmt, SourceLocation StartLoc,
6038 SourceLocation EndLoc) {
6042 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6043 // 1.2.2 OpenMP Language Terminology
6044 // Structured block - An executable statement with a single entry at the
6045 // top and a single exit at the bottom.
6046 // The point of exit cannot be a branch out of the structured block.
6047 // longjmp() and throw() must not violate the entry/exit criteria.
6048 CS->getCapturedDecl()->setNothrow();
6050 getCurFunction()->setHasBranchProtectedScope();
6052 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6056 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6057 SourceLocation EndLoc,
6058 OpenMPDirectiveKind CancelRegion) {
6059 if (DSAStack->isParentNowaitRegion()) {
6060 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6063 if (DSAStack->isParentOrderedRegion()) {
6064 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6067 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6071 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6072 SourceLocation StartLoc,
6073 SourceLocation EndLoc,
6074 OpenMPDirectiveKind CancelRegion) {
6075 if (DSAStack->isParentNowaitRegion()) {
6076 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6079 if (DSAStack->isParentOrderedRegion()) {
6080 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6083 DSAStack->setParentCancelRegion(/*Cancel=*/true);
6084 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6088 static bool checkGrainsizeNumTasksClauses(Sema &S,
6089 ArrayRef<OMPClause *> Clauses) {
6090 OMPClause *PrevClause = nullptr;
6091 bool ErrorFound = false;
6092 for (auto *C : Clauses) {
6093 if (C->getClauseKind() == OMPC_grainsize ||
6094 C->getClauseKind() == OMPC_num_tasks) {
6097 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6098 S.Diag(C->getLocStart(),
6099 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6100 << getOpenMPClauseName(C->getClauseKind())
6101 << getOpenMPClauseName(PrevClause->getClauseKind());
6102 S.Diag(PrevClause->getLocStart(),
6103 diag::note_omp_previous_grainsize_num_tasks)
6104 << getOpenMPClauseName(PrevClause->getClauseKind());
6112 static bool checkReductionClauseWithNogroup(Sema &S,
6113 ArrayRef<OMPClause *> Clauses) {
6114 OMPClause *ReductionClause = nullptr;
6115 OMPClause *NogroupClause = nullptr;
6116 for (auto *C : Clauses) {
6117 if (C->getClauseKind() == OMPC_reduction) {
6118 ReductionClause = C;
6123 if (C->getClauseKind() == OMPC_nogroup) {
6125 if (ReductionClause)
6130 if (ReductionClause && NogroupClause) {
6131 S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup)
6132 << SourceRange(NogroupClause->getLocStart(),
6133 NogroupClause->getLocEnd());
6139 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6140 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6141 SourceLocation EndLoc,
6142 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6146 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6147 OMPLoopDirective::HelperExprs B;
6148 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6149 // define the nested loops number.
6150 unsigned NestedLoopCount =
6151 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
6152 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6153 VarsWithImplicitDSA, B);
6154 if (NestedLoopCount == 0)
6157 assert((CurContext->isDependentContext() || B.builtAll()) &&
6158 "omp for loop exprs were not built");
6160 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6161 // The grainsize clause and num_tasks clause are mutually exclusive and may
6162 // not appear on the same taskloop directive.
6163 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6165 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6166 // If a reduction clause is present on the taskloop directive, the nogroup
6167 // clause must not be specified.
6168 if (checkReductionClauseWithNogroup(*this, Clauses))
6171 getCurFunction()->setHasBranchProtectedScope();
6172 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6173 NestedLoopCount, Clauses, AStmt, B);
6176 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6177 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6178 SourceLocation EndLoc,
6179 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6183 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6184 OMPLoopDirective::HelperExprs B;
6185 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6186 // define the nested loops number.
6187 unsigned NestedLoopCount =
6188 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6189 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6190 VarsWithImplicitDSA, B);
6191 if (NestedLoopCount == 0)
6194 assert((CurContext->isDependentContext() || B.builtAll()) &&
6195 "omp for loop exprs were not built");
6197 if (!CurContext->isDependentContext()) {
6198 // Finalize the clauses that need pre-built expressions for CodeGen.
6199 for (auto C : Clauses) {
6200 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6201 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6202 B.NumIterations, *this, CurScope,
6208 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6209 // The grainsize clause and num_tasks clause are mutually exclusive and may
6210 // not appear on the same taskloop directive.
6211 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6213 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6214 // If a reduction clause is present on the taskloop directive, the nogroup
6215 // clause must not be specified.
6216 if (checkReductionClauseWithNogroup(*this, Clauses))
6219 getCurFunction()->setHasBranchProtectedScope();
6220 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6221 NestedLoopCount, Clauses, AStmt, B);
6224 StmtResult Sema::ActOnOpenMPDistributeDirective(
6225 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6226 SourceLocation EndLoc,
6227 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6231 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6232 OMPLoopDirective::HelperExprs B;
6233 // In presence of clause 'collapse' with number of loops, it will
6234 // define the nested loops number.
6235 unsigned NestedLoopCount =
6236 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6237 nullptr /*ordered not a clause on distribute*/, AStmt,
6238 *this, *DSAStack, VarsWithImplicitDSA, B);
6239 if (NestedLoopCount == 0)
6242 assert((CurContext->isDependentContext() || B.builtAll()) &&
6243 "omp for loop exprs were not built");
6245 getCurFunction()->setHasBranchProtectedScope();
6246 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6247 NestedLoopCount, Clauses, AStmt, B);
6250 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
6251 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6252 SourceLocation EndLoc,
6253 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6257 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6258 // 1.2.2 OpenMP Language Terminology
6259 // Structured block - An executable statement with a single entry at the
6260 // top and a single exit at the bottom.
6261 // The point of exit cannot be a branch out of the structured block.
6262 // longjmp() and throw() must not violate the entry/exit criteria.
6263 CS->getCapturedDecl()->setNothrow();
6265 OMPLoopDirective::HelperExprs B;
6266 // In presence of clause 'collapse' with number of loops, it will
6267 // define the nested loops number.
6268 unsigned NestedLoopCount = CheckOpenMPLoop(
6269 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
6270 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6271 VarsWithImplicitDSA, B);
6272 if (NestedLoopCount == 0)
6275 assert((CurContext->isDependentContext() || B.builtAll()) &&
6276 "omp for loop exprs were not built");
6278 getCurFunction()->setHasBranchProtectedScope();
6279 return OMPDistributeParallelForDirective::Create(
6280 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6283 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
6284 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6285 SourceLocation EndLoc,
6286 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6290 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6291 // 1.2.2 OpenMP Language Terminology
6292 // Structured block - An executable statement with a single entry at the
6293 // top and a single exit at the bottom.
6294 // The point of exit cannot be a branch out of the structured block.
6295 // longjmp() and throw() must not violate the entry/exit criteria.
6296 CS->getCapturedDecl()->setNothrow();
6298 OMPLoopDirective::HelperExprs B;
6299 // In presence of clause 'collapse' with number of loops, it will
6300 // define the nested loops number.
6301 unsigned NestedLoopCount = CheckOpenMPLoop(
6302 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
6303 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6304 VarsWithImplicitDSA, B);
6305 if (NestedLoopCount == 0)
6308 assert((CurContext->isDependentContext() || B.builtAll()) &&
6309 "omp for loop exprs were not built");
6311 if (checkSimdlenSafelenSpecified(*this, Clauses))
6314 getCurFunction()->setHasBranchProtectedScope();
6315 return OMPDistributeParallelForSimdDirective::Create(
6316 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6319 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
6320 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6321 SourceLocation EndLoc,
6322 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6326 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6327 // 1.2.2 OpenMP Language Terminology
6328 // Structured block - An executable statement with a single entry at the
6329 // top and a single exit at the bottom.
6330 // The point of exit cannot be a branch out of the structured block.
6331 // longjmp() and throw() must not violate the entry/exit criteria.
6332 CS->getCapturedDecl()->setNothrow();
6334 OMPLoopDirective::HelperExprs B;
6335 // In presence of clause 'collapse' with number of loops, it will
6336 // define the nested loops number.
6337 unsigned NestedLoopCount =
6338 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
6339 nullptr /*ordered not a clause on distribute*/, AStmt,
6340 *this, *DSAStack, VarsWithImplicitDSA, B);
6341 if (NestedLoopCount == 0)
6344 assert((CurContext->isDependentContext() || B.builtAll()) &&
6345 "omp for loop exprs were not built");
6347 if (checkSimdlenSafelenSpecified(*this, Clauses))
6350 getCurFunction()->setHasBranchProtectedScope();
6351 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
6352 NestedLoopCount, Clauses, AStmt, B);
6355 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
6356 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6357 SourceLocation EndLoc,
6358 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6362 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6363 // 1.2.2 OpenMP Language Terminology
6364 // Structured block - An executable statement with a single entry at the
6365 // top and a single exit at the bottom.
6366 // The point of exit cannot be a branch out of the structured block.
6367 // longjmp() and throw() must not violate the entry/exit criteria.
6368 CS->getCapturedDecl()->setNothrow();
6370 OMPLoopDirective::HelperExprs B;
6371 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6372 // define the nested loops number.
6373 unsigned NestedLoopCount = CheckOpenMPLoop(
6374 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
6375 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6376 VarsWithImplicitDSA, B);
6377 if (NestedLoopCount == 0)
6380 assert((CurContext->isDependentContext() || B.builtAll()) &&
6381 "omp target parallel for simd loop exprs were not built");
6383 if (!CurContext->isDependentContext()) {
6384 // Finalize the clauses that need pre-built expressions for CodeGen.
6385 for (auto C : Clauses) {
6386 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6387 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6388 B.NumIterations, *this, CurScope,
6393 if (checkSimdlenSafelenSpecified(*this, Clauses))
6396 getCurFunction()->setHasBranchProtectedScope();
6397 return OMPTargetParallelForSimdDirective::Create(
6398 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6401 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
6402 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6403 SourceLocation EndLoc,
6404 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6408 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6409 // 1.2.2 OpenMP Language Terminology
6410 // Structured block - An executable statement with a single entry at the
6411 // top and a single exit at the bottom.
6412 // The point of exit cannot be a branch out of the structured block.
6413 // longjmp() and throw() must not violate the entry/exit criteria.
6414 CS->getCapturedDecl()->setNothrow();
6416 OMPLoopDirective::HelperExprs B;
6417 // In presence of clause 'collapse' with number of loops, it will define the
6418 // nested loops number.
6419 unsigned NestedLoopCount =
6420 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
6421 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6422 VarsWithImplicitDSA, B);
6423 if (NestedLoopCount == 0)
6426 assert((CurContext->isDependentContext() || B.builtAll()) &&
6427 "omp target simd loop exprs were not built");
6429 if (!CurContext->isDependentContext()) {
6430 // Finalize the clauses that need pre-built expressions for CodeGen.
6431 for (auto C : Clauses) {
6432 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6433 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6434 B.NumIterations, *this, CurScope,
6440 if (checkSimdlenSafelenSpecified(*this, Clauses))
6443 getCurFunction()->setHasBranchProtectedScope();
6444 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
6445 NestedLoopCount, Clauses, AStmt, B);
6448 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
6449 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6450 SourceLocation EndLoc,
6451 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6455 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6456 // 1.2.2 OpenMP Language Terminology
6457 // Structured block - An executable statement with a single entry at the
6458 // top and a single exit at the bottom.
6459 // The point of exit cannot be a branch out of the structured block.
6460 // longjmp() and throw() must not violate the entry/exit criteria.
6461 CS->getCapturedDecl()->setNothrow();
6463 OMPLoopDirective::HelperExprs B;
6464 // In presence of clause 'collapse' with number of loops, it will
6465 // define the nested loops number.
6466 unsigned NestedLoopCount =
6467 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
6468 nullptr /*ordered not a clause on distribute*/, AStmt,
6469 *this, *DSAStack, VarsWithImplicitDSA, B);
6470 if (NestedLoopCount == 0)
6473 assert((CurContext->isDependentContext() || B.builtAll()) &&
6474 "omp teams distribute loop exprs were not built");
6476 getCurFunction()->setHasBranchProtectedScope();
6477 return OMPTeamsDistributeDirective::Create(
6478 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6481 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
6482 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6483 SourceLocation EndLoc,
6484 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6488 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6489 // 1.2.2 OpenMP Language Terminology
6490 // Structured block - An executable statement with a single entry at the
6491 // top and a single exit at the bottom.
6492 // The point of exit cannot be a branch out of the structured block.
6493 // longjmp() and throw() must not violate the entry/exit criteria.
6494 CS->getCapturedDecl()->setNothrow();
6496 OMPLoopDirective::HelperExprs B;
6497 // In presence of clause 'collapse' with number of loops, it will
6498 // define the nested loops number.
6499 unsigned NestedLoopCount = CheckOpenMPLoop(
6500 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
6501 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6502 VarsWithImplicitDSA, B);
6504 if (NestedLoopCount == 0)
6507 assert((CurContext->isDependentContext() || B.builtAll()) &&
6508 "omp teams distribute simd loop exprs were not built");
6510 if (!CurContext->isDependentContext()) {
6511 // Finalize the clauses that need pre-built expressions for CodeGen.
6512 for (auto C : Clauses) {
6513 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6514 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6515 B.NumIterations, *this, CurScope,
6521 if (checkSimdlenSafelenSpecified(*this, Clauses))
6524 getCurFunction()->setHasBranchProtectedScope();
6525 return OMPTeamsDistributeSimdDirective::Create(
6526 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6529 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6530 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6531 SourceLocation EndLoc,
6532 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6536 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6537 // 1.2.2 OpenMP Language Terminology
6538 // Structured block - An executable statement with a single entry at the
6539 // top and a single exit at the bottom.
6540 // The point of exit cannot be a branch out of the structured block.
6541 // longjmp() and throw() must not violate the entry/exit criteria.
6542 CS->getCapturedDecl()->setNothrow();
6544 OMPLoopDirective::HelperExprs B;
6545 // In presence of clause 'collapse' with number of loops, it will
6546 // define the nested loops number.
6547 auto NestedLoopCount = CheckOpenMPLoop(
6548 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
6549 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6550 VarsWithImplicitDSA, B);
6552 if (NestedLoopCount == 0)
6555 assert((CurContext->isDependentContext() || B.builtAll()) &&
6556 "omp for loop exprs were not built");
6558 if (!CurContext->isDependentContext()) {
6559 // Finalize the clauses that need pre-built expressions for CodeGen.
6560 for (auto C : Clauses) {
6561 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6562 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6563 B.NumIterations, *this, CurScope,
6569 if (checkSimdlenSafelenSpecified(*this, Clauses))
6572 getCurFunction()->setHasBranchProtectedScope();
6573 return OMPTeamsDistributeParallelForSimdDirective::Create(
6574 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6577 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
6578 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6579 SourceLocation EndLoc,
6580 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6584 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6585 // 1.2.2 OpenMP Language Terminology
6586 // Structured block - An executable statement with a single entry at the
6587 // top and a single exit at the bottom.
6588 // The point of exit cannot be a branch out of the structured block.
6589 // longjmp() and throw() must not violate the entry/exit criteria.
6590 CS->getCapturedDecl()->setNothrow();
6592 OMPLoopDirective::HelperExprs B;
6593 // In presence of clause 'collapse' with number of loops, it will
6594 // define the nested loops number.
6595 unsigned NestedLoopCount = CheckOpenMPLoop(
6596 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
6597 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6598 VarsWithImplicitDSA, B);
6600 if (NestedLoopCount == 0)
6603 assert((CurContext->isDependentContext() || B.builtAll()) &&
6604 "omp for loop exprs were not built");
6606 if (!CurContext->isDependentContext()) {
6607 // Finalize the clauses that need pre-built expressions for CodeGen.
6608 for (auto C : Clauses) {
6609 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6610 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6611 B.NumIterations, *this, CurScope,
6617 getCurFunction()->setHasBranchProtectedScope();
6618 return OMPTeamsDistributeParallelForDirective::Create(
6619 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6622 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
6624 SourceLocation StartLoc,
6625 SourceLocation EndLoc) {
6629 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6630 // 1.2.2 OpenMP Language Terminology
6631 // Structured block - An executable statement with a single entry at the
6632 // top and a single exit at the bottom.
6633 // The point of exit cannot be a branch out of the structured block.
6634 // longjmp() and throw() must not violate the entry/exit criteria.
6635 CS->getCapturedDecl()->setNothrow();
6637 getCurFunction()->setHasBranchProtectedScope();
6639 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
6643 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
6644 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6645 SourceLocation EndLoc,
6646 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6650 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6651 // 1.2.2 OpenMP Language Terminology
6652 // Structured block - An executable statement with a single entry at the
6653 // top and a single exit at the bottom.
6654 // The point of exit cannot be a branch out of the structured block.
6655 // longjmp() and throw() must not violate the entry/exit criteria.
6656 CS->getCapturedDecl()->setNothrow();
6658 OMPLoopDirective::HelperExprs B;
6659 // In presence of clause 'collapse' with number of loops, it will
6660 // define the nested loops number.
6661 auto NestedLoopCount = CheckOpenMPLoop(
6662 OMPD_target_teams_distribute,
6663 getCollapseNumberExpr(Clauses),
6664 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6665 VarsWithImplicitDSA, B);
6666 if (NestedLoopCount == 0)
6669 assert((CurContext->isDependentContext() || B.builtAll()) &&
6670 "omp target teams distribute loop exprs were not built");
6672 getCurFunction()->setHasBranchProtectedScope();
6673 return OMPTargetTeamsDistributeDirective::Create(
6674 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6677 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6678 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6679 SourceLocation EndLoc,
6680 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6684 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6685 // 1.2.2 OpenMP Language Terminology
6686 // Structured block - An executable statement with a single entry at the
6687 // top and a single exit at the bottom.
6688 // The point of exit cannot be a branch out of the structured block.
6689 // longjmp() and throw() must not violate the entry/exit criteria.
6690 CS->getCapturedDecl()->setNothrow();
6692 OMPLoopDirective::HelperExprs B;
6693 // In presence of clause 'collapse' with number of loops, it will
6694 // define the nested loops number.
6695 auto NestedLoopCount = CheckOpenMPLoop(
6696 OMPD_target_teams_distribute_parallel_for,
6697 getCollapseNumberExpr(Clauses),
6698 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6699 VarsWithImplicitDSA, B);
6700 if (NestedLoopCount == 0)
6703 assert((CurContext->isDependentContext() || B.builtAll()) &&
6704 "omp target teams distribute parallel for loop exprs were not built");
6706 if (!CurContext->isDependentContext()) {
6707 // Finalize the clauses that need pre-built expressions for CodeGen.
6708 for (auto C : Clauses) {
6709 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6710 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6711 B.NumIterations, *this, CurScope,
6717 getCurFunction()->setHasBranchProtectedScope();
6718 return OMPTargetTeamsDistributeParallelForDirective::Create(
6719 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6722 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6723 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6724 SourceLocation EndLoc,
6725 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6729 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6730 // 1.2.2 OpenMP Language Terminology
6731 // Structured block - An executable statement with a single entry at the
6732 // top and a single exit at the bottom.
6733 // The point of exit cannot be a branch out of the structured block.
6734 // longjmp() and throw() must not violate the entry/exit criteria.
6735 CS->getCapturedDecl()->setNothrow();
6737 OMPLoopDirective::HelperExprs B;
6738 // In presence of clause 'collapse' with number of loops, it will
6739 // define the nested loops number.
6740 auto NestedLoopCount = CheckOpenMPLoop(
6741 OMPD_target_teams_distribute_parallel_for_simd,
6742 getCollapseNumberExpr(Clauses),
6743 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6744 VarsWithImplicitDSA, B);
6745 if (NestedLoopCount == 0)
6748 assert((CurContext->isDependentContext() || B.builtAll()) &&
6749 "omp target teams distribute parallel for simd loop exprs were not "
6752 if (!CurContext->isDependentContext()) {
6753 // Finalize the clauses that need pre-built expressions for CodeGen.
6754 for (auto C : Clauses) {
6755 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6756 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6757 B.NumIterations, *this, CurScope,
6763 getCurFunction()->setHasBranchProtectedScope();
6764 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
6765 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6768 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
6769 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6770 SourceLocation EndLoc,
6771 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6775 auto *CS = cast<CapturedStmt>(AStmt);
6776 // 1.2.2 OpenMP Language Terminology
6777 // Structured block - An executable statement with a single entry at the
6778 // top and a single exit at the bottom.
6779 // The point of exit cannot be a branch out of the structured block.
6780 // longjmp() and throw() must not violate the entry/exit criteria.
6781 CS->getCapturedDecl()->setNothrow();
6783 OMPLoopDirective::HelperExprs B;
6784 // In presence of clause 'collapse' with number of loops, it will
6785 // define the nested loops number.
6786 auto NestedLoopCount = CheckOpenMPLoop(
6787 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
6788 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6789 VarsWithImplicitDSA, B);
6790 if (NestedLoopCount == 0)
6793 assert((CurContext->isDependentContext() || B.builtAll()) &&
6794 "omp target teams distribute simd loop exprs were not built");
6796 getCurFunction()->setHasBranchProtectedScope();
6797 return OMPTargetTeamsDistributeSimdDirective::Create(
6798 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6801 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
6802 SourceLocation StartLoc,
6803 SourceLocation LParenLoc,
6804 SourceLocation EndLoc) {
6805 OMPClause *Res = nullptr;
6808 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6810 case OMPC_num_threads:
6811 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6814 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6817 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6820 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6823 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6826 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6828 case OMPC_num_teams:
6829 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6831 case OMPC_thread_limit:
6832 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6835 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6837 case OMPC_grainsize:
6838 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6840 case OMPC_num_tasks:
6841 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6844 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6848 case OMPC_proc_bind:
6851 case OMPC_firstprivate:
6852 case OMPC_lastprivate:
6854 case OMPC_reduction:
6855 case OMPC_task_reduction:
6859 case OMPC_copyprivate:
6862 case OMPC_mergeable:
6863 case OMPC_threadprivate:
6875 case OMPC_dist_schedule:
6876 case OMPC_defaultmap:
6881 case OMPC_use_device_ptr:
6882 case OMPC_is_device_ptr:
6883 llvm_unreachable("Clause is not allowed.");
6888 // An OpenMP directive such as 'target parallel' has two captured regions:
6889 // for the 'target' and 'parallel' respectively. This function returns
6890 // the region in which to capture expressions associated with a clause.
6891 // A return value of OMPD_unknown signifies that the expression should not
6893 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
6894 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
6895 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
6896 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
6901 case OMPD_target_parallel:
6902 // If this clause applies to the nested 'parallel' region, capture within
6903 // the 'target' region, otherwise do not capture.
6904 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
6905 CaptureRegion = OMPD_target;
6909 case OMPD_parallel_sections:
6910 case OMPD_parallel_for:
6911 case OMPD_parallel_for_simd:
6913 case OMPD_target_simd:
6914 case OMPD_target_parallel_for:
6915 case OMPD_target_parallel_for_simd:
6916 case OMPD_target_teams:
6917 case OMPD_target_teams_distribute:
6918 case OMPD_target_teams_distribute_simd:
6919 case OMPD_target_teams_distribute_parallel_for:
6920 case OMPD_target_teams_distribute_parallel_for_simd:
6921 case OMPD_teams_distribute_parallel_for:
6922 case OMPD_teams_distribute_parallel_for_simd:
6923 case OMPD_distribute_parallel_for:
6924 case OMPD_distribute_parallel_for_simd:
6927 case OMPD_taskloop_simd:
6928 case OMPD_target_data:
6929 case OMPD_target_enter_data:
6930 case OMPD_target_exit_data:
6931 case OMPD_target_update:
6932 // Do not capture if-clause expressions.
6934 case OMPD_threadprivate:
6935 case OMPD_taskyield:
6938 case OMPD_cancellation_point:
6940 case OMPD_declare_reduction:
6941 case OMPD_declare_simd:
6942 case OMPD_declare_target:
6943 case OMPD_end_declare_target:
6953 case OMPD_taskgroup:
6954 case OMPD_distribute:
6957 case OMPD_distribute_simd:
6958 case OMPD_teams_distribute:
6959 case OMPD_teams_distribute_simd:
6960 llvm_unreachable("Unexpected OpenMP directive with if-clause");
6962 llvm_unreachable("Unknown OpenMP directive");
6965 case OMPC_num_threads:
6967 case OMPD_target_parallel:
6968 CaptureRegion = OMPD_target;
6972 case OMPD_parallel_sections:
6973 case OMPD_parallel_for:
6974 case OMPD_parallel_for_simd:
6976 case OMPD_target_simd:
6977 case OMPD_target_parallel_for:
6978 case OMPD_target_parallel_for_simd:
6979 case OMPD_target_teams:
6980 case OMPD_target_teams_distribute:
6981 case OMPD_target_teams_distribute_simd:
6982 case OMPD_target_teams_distribute_parallel_for:
6983 case OMPD_target_teams_distribute_parallel_for_simd:
6984 case OMPD_teams_distribute_parallel_for:
6985 case OMPD_teams_distribute_parallel_for_simd:
6986 case OMPD_distribute_parallel_for:
6987 case OMPD_distribute_parallel_for_simd:
6990 case OMPD_taskloop_simd:
6991 case OMPD_target_data:
6992 case OMPD_target_enter_data:
6993 case OMPD_target_exit_data:
6994 case OMPD_target_update:
6995 // Do not capture num_threads-clause expressions.
6997 case OMPD_threadprivate:
6998 case OMPD_taskyield:
7001 case OMPD_cancellation_point:
7003 case OMPD_declare_reduction:
7004 case OMPD_declare_simd:
7005 case OMPD_declare_target:
7006 case OMPD_end_declare_target:
7016 case OMPD_taskgroup:
7017 case OMPD_distribute:
7020 case OMPD_distribute_simd:
7021 case OMPD_teams_distribute:
7022 case OMPD_teams_distribute_simd:
7023 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
7025 llvm_unreachable("Unknown OpenMP directive");
7028 case OMPC_num_teams:
7030 case OMPD_target_teams:
7031 CaptureRegion = OMPD_target;
7035 case OMPD_parallel_sections:
7036 case OMPD_parallel_for:
7037 case OMPD_parallel_for_simd:
7039 case OMPD_target_simd:
7040 case OMPD_target_parallel:
7041 case OMPD_target_parallel_for:
7042 case OMPD_target_parallel_for_simd:
7043 case OMPD_target_teams_distribute:
7044 case OMPD_target_teams_distribute_simd:
7045 case OMPD_target_teams_distribute_parallel_for:
7046 case OMPD_target_teams_distribute_parallel_for_simd:
7047 case OMPD_teams_distribute_parallel_for:
7048 case OMPD_teams_distribute_parallel_for_simd:
7049 case OMPD_distribute_parallel_for:
7050 case OMPD_distribute_parallel_for_simd:
7053 case OMPD_taskloop_simd:
7054 case OMPD_target_data:
7055 case OMPD_target_enter_data:
7056 case OMPD_target_exit_data:
7057 case OMPD_target_update:
7059 case OMPD_teams_distribute:
7060 case OMPD_teams_distribute_simd:
7061 // Do not capture num_teams-clause expressions.
7063 case OMPD_threadprivate:
7064 case OMPD_taskyield:
7067 case OMPD_cancellation_point:
7069 case OMPD_declare_reduction:
7070 case OMPD_declare_simd:
7071 case OMPD_declare_target:
7072 case OMPD_end_declare_target:
7081 case OMPD_taskgroup:
7082 case OMPD_distribute:
7085 case OMPD_distribute_simd:
7086 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
7088 llvm_unreachable("Unknown OpenMP directive");
7091 case OMPC_thread_limit:
7093 case OMPD_target_teams:
7094 CaptureRegion = OMPD_target;
7098 case OMPD_parallel_sections:
7099 case OMPD_parallel_for:
7100 case OMPD_parallel_for_simd:
7102 case OMPD_target_simd:
7103 case OMPD_target_parallel:
7104 case OMPD_target_parallel_for:
7105 case OMPD_target_parallel_for_simd:
7106 case OMPD_target_teams_distribute:
7107 case OMPD_target_teams_distribute_simd:
7108 case OMPD_target_teams_distribute_parallel_for:
7109 case OMPD_target_teams_distribute_parallel_for_simd:
7110 case OMPD_teams_distribute_parallel_for:
7111 case OMPD_teams_distribute_parallel_for_simd:
7112 case OMPD_distribute_parallel_for:
7113 case OMPD_distribute_parallel_for_simd:
7116 case OMPD_taskloop_simd:
7117 case OMPD_target_data:
7118 case OMPD_target_enter_data:
7119 case OMPD_target_exit_data:
7120 case OMPD_target_update:
7122 case OMPD_teams_distribute:
7123 case OMPD_teams_distribute_simd:
7124 // Do not capture thread_limit-clause expressions.
7126 case OMPD_threadprivate:
7127 case OMPD_taskyield:
7130 case OMPD_cancellation_point:
7132 case OMPD_declare_reduction:
7133 case OMPD_declare_simd:
7134 case OMPD_declare_target:
7135 case OMPD_end_declare_target:
7144 case OMPD_taskgroup:
7145 case OMPD_distribute:
7148 case OMPD_distribute_simd:
7149 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
7151 llvm_unreachable("Unknown OpenMP directive");
7155 case OMPC_dist_schedule:
7156 case OMPC_firstprivate:
7157 case OMPC_lastprivate:
7158 case OMPC_reduction:
7159 case OMPC_task_reduction:
7162 case OMPC_proc_bind:
7171 case OMPC_copyprivate:
7175 case OMPC_mergeable:
7176 case OMPC_threadprivate:
7189 case OMPC_grainsize:
7191 case OMPC_num_tasks:
7193 case OMPC_defaultmap:
7198 case OMPC_use_device_ptr:
7199 case OMPC_is_device_ptr:
7200 llvm_unreachable("Unexpected OpenMP clause.");
7202 return CaptureRegion;
7205 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7206 Expr *Condition, SourceLocation StartLoc,
7207 SourceLocation LParenLoc,
7208 SourceLocation NameModifierLoc,
7209 SourceLocation ColonLoc,
7210 SourceLocation EndLoc) {
7211 Expr *ValExpr = Condition;
7212 Stmt *HelperValStmt = nullptr;
7213 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
7214 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7215 !Condition->isInstantiationDependent() &&
7216 !Condition->containsUnexpandedParameterPack()) {
7217 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7218 if (Val.isInvalid())
7221 ValExpr = MakeFullExpr(Val.get()).get();
7223 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
7225 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
7226 if (CaptureRegion != OMPD_unknown) {
7227 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7228 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7229 HelperValStmt = buildPreInits(Context, Captures);
7233 return new (Context)
7234 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
7235 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
7238 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7239 SourceLocation StartLoc,
7240 SourceLocation LParenLoc,
7241 SourceLocation EndLoc) {
7242 Expr *ValExpr = Condition;
7243 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7244 !Condition->isInstantiationDependent() &&
7245 !Condition->containsUnexpandedParameterPack()) {
7246 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7247 if (Val.isInvalid())
7250 ValExpr = MakeFullExpr(Val.get()).get();
7253 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7255 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7260 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7262 IntConvertDiagnoser()
7263 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
7264 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7265 QualType T) override {
7266 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7268 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7269 QualType T) override {
7270 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7272 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7274 QualType ConvTy) override {
7275 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7277 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7278 QualType ConvTy) override {
7279 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7280 << ConvTy->isEnumeralType() << ConvTy;
7282 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7283 QualType T) override {
7284 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7286 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7287 QualType ConvTy) override {
7288 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7289 << ConvTy->isEnumeralType() << ConvTy;
7291 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7292 QualType) override {
7293 llvm_unreachable("conversion functions are permitted");
7296 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7299 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
7300 OpenMPClauseKind CKind,
7301 bool StrictlyPositive) {
7302 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7303 !ValExpr->isInstantiationDependent()) {
7304 SourceLocation Loc = ValExpr->getExprLoc();
7306 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7307 if (Value.isInvalid())
7310 ValExpr = Value.get();
7311 // The expression must evaluate to a non-negative integer value.
7312 llvm::APSInt Result;
7313 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
7314 Result.isSigned() &&
7315 !((!StrictlyPositive && Result.isNonNegative()) ||
7316 (StrictlyPositive && Result.isStrictlyPositive()))) {
7317 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
7318 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7319 << ValExpr->getSourceRange();
7326 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7327 SourceLocation StartLoc,
7328 SourceLocation LParenLoc,
7329 SourceLocation EndLoc) {
7330 Expr *ValExpr = NumThreads;
7331 Stmt *HelperValStmt = nullptr;
7332 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
7334 // OpenMP [2.5, Restrictions]
7335 // The num_threads expression must evaluate to a positive integer value.
7336 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7337 /*StrictlyPositive=*/true))
7340 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
7341 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
7342 if (CaptureRegion != OMPD_unknown) {
7343 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7344 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7345 HelperValStmt = buildPreInits(Context, Captures);
7348 return new (Context) OMPNumThreadsClause(
7349 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
7352 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
7353 OpenMPClauseKind CKind,
7354 bool StrictlyPositive) {
7357 if (E->isValueDependent() || E->isTypeDependent() ||
7358 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7360 llvm::APSInt Result;
7361 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7362 if (ICE.isInvalid())
7364 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7365 (!StrictlyPositive && !Result.isNonNegative())) {
7366 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
7367 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7368 << E->getSourceRange();
7371 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7372 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7373 << E->getSourceRange();
7376 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7377 DSAStack->setAssociatedLoops(Result.getExtValue());
7378 else if (CKind == OMPC_ordered)
7379 DSAStack->setAssociatedLoops(Result.getExtValue());
7383 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7384 SourceLocation LParenLoc,
7385 SourceLocation EndLoc) {
7386 // OpenMP [2.8.1, simd construct, Description]
7387 // The parameter of the safelen clause must be a constant
7388 // positive integer expression.
7389 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7390 if (Safelen.isInvalid())
7392 return new (Context)
7393 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
7396 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7397 SourceLocation LParenLoc,
7398 SourceLocation EndLoc) {
7399 // OpenMP [2.8.1, simd construct, Description]
7400 // The parameter of the simdlen clause must be a constant
7401 // positive integer expression.
7402 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7403 if (Simdlen.isInvalid())
7405 return new (Context)
7406 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7409 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7410 SourceLocation StartLoc,
7411 SourceLocation LParenLoc,
7412 SourceLocation EndLoc) {
7413 // OpenMP [2.7.1, loop construct, Description]
7414 // OpenMP [2.8.1, simd construct, Description]
7415 // OpenMP [2.9.6, distribute construct, Description]
7416 // The parameter of the collapse clause must be a constant
7417 // positive integer expression.
7418 ExprResult NumForLoopsResult =
7419 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7420 if (NumForLoopsResult.isInvalid())
7422 return new (Context)
7423 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
7426 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7427 SourceLocation EndLoc,
7428 SourceLocation LParenLoc,
7429 Expr *NumForLoops) {
7430 // OpenMP [2.7.1, loop construct, Description]
7431 // OpenMP [2.8.1, simd construct, Description]
7432 // OpenMP [2.9.6, distribute construct, Description]
7433 // The parameter of the ordered clause must be a constant
7434 // positive integer expression if any.
7435 if (NumForLoops && LParenLoc.isValid()) {
7436 ExprResult NumForLoopsResult =
7437 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7438 if (NumForLoopsResult.isInvalid())
7440 NumForLoops = NumForLoopsResult.get();
7442 NumForLoops = nullptr;
7443 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
7444 return new (Context)
7445 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7448 OMPClause *Sema::ActOnOpenMPSimpleClause(
7449 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7450 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
7451 OMPClause *Res = nullptr;
7455 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7456 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
7458 case OMPC_proc_bind:
7459 Res = ActOnOpenMPProcBindClause(
7460 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7465 case OMPC_num_threads:
7471 case OMPC_firstprivate:
7472 case OMPC_lastprivate:
7474 case OMPC_reduction:
7475 case OMPC_task_reduction:
7479 case OMPC_copyprivate:
7483 case OMPC_mergeable:
7484 case OMPC_threadprivate:
7496 case OMPC_num_teams:
7497 case OMPC_thread_limit:
7499 case OMPC_grainsize:
7501 case OMPC_num_tasks:
7503 case OMPC_dist_schedule:
7504 case OMPC_defaultmap:
7509 case OMPC_use_device_ptr:
7510 case OMPC_is_device_ptr:
7511 llvm_unreachable("Clause is not allowed.");
7517 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7518 ArrayRef<unsigned> Exclude = llvm::None) {
7520 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7521 unsigned Skipped = Exclude.size();
7522 auto S = Exclude.begin(), E = Exclude.end();
7523 for (unsigned i = First; i < Last; ++i) {
7524 if (std::find(S, E, i) != E) {
7529 Values += getOpenMPSimpleClauseTypeName(K, i);
7531 if (i == Bound - Skipped)
7533 else if (i != Bound + 1 - Skipped)
7539 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7540 SourceLocation KindKwLoc,
7541 SourceLocation StartLoc,
7542 SourceLocation LParenLoc,
7543 SourceLocation EndLoc) {
7544 if (Kind == OMPC_DEFAULT_unknown) {
7545 static_assert(OMPC_DEFAULT_unknown > 0,
7546 "OMPC_DEFAULT_unknown not greater than 0");
7547 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7548 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7549 /*Last=*/OMPC_DEFAULT_unknown)
7550 << getOpenMPClauseName(OMPC_default);
7554 case OMPC_DEFAULT_none:
7555 DSAStack->setDefaultDSANone(KindKwLoc);
7557 case OMPC_DEFAULT_shared:
7558 DSAStack->setDefaultDSAShared(KindKwLoc);
7560 case OMPC_DEFAULT_unknown:
7561 llvm_unreachable("Clause kind is not allowed.");
7564 return new (Context)
7565 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7568 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7569 SourceLocation KindKwLoc,
7570 SourceLocation StartLoc,
7571 SourceLocation LParenLoc,
7572 SourceLocation EndLoc) {
7573 if (Kind == OMPC_PROC_BIND_unknown) {
7574 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7575 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7576 /*Last=*/OMPC_PROC_BIND_unknown)
7577 << getOpenMPClauseName(OMPC_proc_bind);
7580 return new (Context)
7581 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7584 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
7585 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
7586 SourceLocation StartLoc, SourceLocation LParenLoc,
7587 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
7588 SourceLocation EndLoc) {
7589 OMPClause *Res = nullptr;
7592 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7593 assert(Argument.size() == NumberOfElements &&
7594 ArgumentLoc.size() == NumberOfElements);
7595 Res = ActOnOpenMPScheduleClause(
7596 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7597 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7598 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7599 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7600 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
7603 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7604 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7605 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7608 case OMPC_dist_schedule:
7609 Res = ActOnOpenMPDistScheduleClause(
7610 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7611 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7613 case OMPC_defaultmap:
7614 enum { Modifier, DefaultmapKind };
7615 Res = ActOnOpenMPDefaultmapClause(
7616 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7617 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7618 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
7622 case OMPC_num_threads:
7627 case OMPC_proc_bind:
7629 case OMPC_firstprivate:
7630 case OMPC_lastprivate:
7632 case OMPC_reduction:
7633 case OMPC_task_reduction:
7637 case OMPC_copyprivate:
7641 case OMPC_mergeable:
7642 case OMPC_threadprivate:
7654 case OMPC_num_teams:
7655 case OMPC_thread_limit:
7657 case OMPC_grainsize:
7659 case OMPC_num_tasks:
7665 case OMPC_use_device_ptr:
7666 case OMPC_is_device_ptr:
7667 llvm_unreachable("Clause is not allowed.");
7672 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7673 OpenMPScheduleClauseModifier M2,
7674 SourceLocation M1Loc, SourceLocation M2Loc) {
7675 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7676 SmallVector<unsigned, 2> Excluded;
7677 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7678 Excluded.push_back(M2);
7679 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7680 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7681 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7682 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7683 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7684 << getListOfPossibleValues(OMPC_schedule,
7685 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7686 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7688 << getOpenMPClauseName(OMPC_schedule);
7694 OMPClause *Sema::ActOnOpenMPScheduleClause(
7695 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
7696 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
7697 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7698 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7699 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7700 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7702 // OpenMP, 2.7.1, Loop Construct, Restrictions
7703 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7705 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7706 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7707 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7708 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7709 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7710 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7711 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7712 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7715 if (Kind == OMPC_SCHEDULE_unknown) {
7717 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7718 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7719 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7720 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7723 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7724 /*Last=*/OMPC_SCHEDULE_unknown);
7726 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7727 << Values << getOpenMPClauseName(OMPC_schedule);
7730 // OpenMP, 2.7.1, Loop Construct, Restrictions
7731 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7732 // schedule(guided).
7733 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7734 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7735 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7736 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7737 diag::err_omp_schedule_nonmonotonic_static);
7740 Expr *ValExpr = ChunkSize;
7741 Stmt *HelperValStmt = nullptr;
7743 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7744 !ChunkSize->isInstantiationDependent() &&
7745 !ChunkSize->containsUnexpandedParameterPack()) {
7746 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7748 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7749 if (Val.isInvalid())
7752 ValExpr = Val.get();
7754 // OpenMP [2.7.1, Restrictions]
7755 // chunk_size must be a loop invariant integer expression with a positive
7757 llvm::APSInt Result;
7758 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7759 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7760 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
7761 << "schedule" << 1 << ChunkSize->getSourceRange();
7764 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7765 !CurContext->isDependentContext()) {
7766 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7767 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7768 HelperValStmt = buildPreInits(Context, Captures);
7773 return new (Context)
7774 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
7775 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
7778 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7779 SourceLocation StartLoc,
7780 SourceLocation EndLoc) {
7781 OMPClause *Res = nullptr;
7784 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7787 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7790 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7792 case OMPC_mergeable:
7793 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7796 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7799 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7802 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7805 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7808 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7811 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7814 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7817 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7821 case OMPC_num_threads:
7827 case OMPC_firstprivate:
7828 case OMPC_lastprivate:
7830 case OMPC_reduction:
7831 case OMPC_task_reduction:
7835 case OMPC_copyprivate:
7837 case OMPC_proc_bind:
7838 case OMPC_threadprivate:
7843 case OMPC_num_teams:
7844 case OMPC_thread_limit:
7846 case OMPC_grainsize:
7847 case OMPC_num_tasks:
7849 case OMPC_dist_schedule:
7850 case OMPC_defaultmap:
7855 case OMPC_use_device_ptr:
7856 case OMPC_is_device_ptr:
7857 llvm_unreachable("Clause is not allowed.");
7862 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7863 SourceLocation EndLoc) {
7864 DSAStack->setNowaitRegion();
7865 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7868 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7869 SourceLocation EndLoc) {
7870 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7873 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7874 SourceLocation EndLoc) {
7875 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7878 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7879 SourceLocation EndLoc) {
7880 return new (Context) OMPReadClause(StartLoc, EndLoc);
7883 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7884 SourceLocation EndLoc) {
7885 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7888 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7889 SourceLocation EndLoc) {
7890 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7893 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7894 SourceLocation EndLoc) {
7895 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7898 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7899 SourceLocation EndLoc) {
7900 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7903 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7904 SourceLocation EndLoc) {
7905 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7908 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7909 SourceLocation EndLoc) {
7910 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7913 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7914 SourceLocation EndLoc) {
7915 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7918 OMPClause *Sema::ActOnOpenMPVarListClause(
7919 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7920 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7921 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
7922 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
7923 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7924 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7925 SourceLocation DepLinMapLoc) {
7926 OMPClause *Res = nullptr;
7929 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7931 case OMPC_firstprivate:
7932 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7934 case OMPC_lastprivate:
7935 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7938 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7940 case OMPC_reduction:
7941 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7942 EndLoc, ReductionIdScopeSpec, ReductionId);
7944 case OMPC_task_reduction:
7945 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7946 EndLoc, ReductionIdScopeSpec,
7950 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
7951 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
7954 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7958 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7960 case OMPC_copyprivate:
7961 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7964 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7967 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7968 StartLoc, LParenLoc, EndLoc);
7971 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7972 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7976 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
7979 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
7981 case OMPC_use_device_ptr:
7982 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
7984 case OMPC_is_device_ptr:
7985 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
7989 case OMPC_num_threads:
7994 case OMPC_proc_bind:
7999 case OMPC_mergeable:
8000 case OMPC_threadprivate:
8009 case OMPC_num_teams:
8010 case OMPC_thread_limit:
8012 case OMPC_grainsize:
8014 case OMPC_num_tasks:
8016 case OMPC_dist_schedule:
8017 case OMPC_defaultmap:
8020 llvm_unreachable("Clause is not allowed.");
8025 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
8026 ExprObjectKind OK, SourceLocation Loc) {
8027 ExprResult Res = BuildDeclRefExpr(
8028 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8029 if (!Res.isUsable())
8031 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8032 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8033 if (!Res.isUsable())
8036 if (VK != VK_LValue && Res.get()->isGLValue()) {
8037 Res = DefaultLvalueConversion(Res.get());
8038 if (!Res.isUsable())
8044 static std::pair<ValueDecl *, bool>
8045 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8046 SourceRange &ERange, bool AllowArraySection = false) {
8047 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8048 RefExpr->containsUnexpandedParameterPack())
8049 return std::make_pair(nullptr, true);
8051 // OpenMP [3.1, C/C++]
8052 // A list item is a variable name.
8053 // OpenMP [2.9.3.3, Restrictions, p.1]
8054 // A variable that is part of another variable (as an array or
8055 // structure element) cannot appear in a private clause.
8056 RefExpr = RefExpr->IgnoreParens();
8061 } IsArrayExpr = NoArrayExpr;
8062 if (AllowArraySection) {
8063 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8064 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8065 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8066 Base = TempASE->getBase()->IgnoreParenImpCasts();
8068 IsArrayExpr = ArraySubscript;
8069 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8070 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8071 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8072 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8073 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8074 Base = TempASE->getBase()->IgnoreParenImpCasts();
8076 IsArrayExpr = OMPArraySection;
8079 ELoc = RefExpr->getExprLoc();
8080 ERange = RefExpr->getSourceRange();
8081 RefExpr = RefExpr->IgnoreParenImpCasts();
8082 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8083 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8084 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8085 (S.getCurrentThisType().isNull() || !ME ||
8086 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8087 !isa<FieldDecl>(ME->getMemberDecl()))) {
8088 if (IsArrayExpr != NoArrayExpr)
8089 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8094 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8095 : diag::err_omp_expected_var_name_member_expr)
8096 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8098 return std::make_pair(nullptr, false);
8100 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8103 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8104 SourceLocation StartLoc,
8105 SourceLocation LParenLoc,
8106 SourceLocation EndLoc) {
8107 SmallVector<Expr *, 8> Vars;
8108 SmallVector<Expr *, 8> PrivateCopies;
8109 for (auto &RefExpr : VarList) {
8110 assert(RefExpr && "NULL expr in OpenMP private clause.");
8111 SourceLocation ELoc;
8113 Expr *SimpleRefExpr = RefExpr;
8114 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8116 // It will be analyzed later.
8117 Vars.push_back(RefExpr);
8118 PrivateCopies.push_back(nullptr);
8120 ValueDecl *D = Res.first;
8124 QualType Type = D->getType();
8125 auto *VD = dyn_cast<VarDecl>(D);
8127 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8128 // A variable that appears in a private clause must not have an incomplete
8129 // type or a reference type.
8130 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
8132 Type = Type.getNonReferenceType();
8134 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8136 // Variables with the predetermined data-sharing attributes may not be
8137 // listed in data-sharing attributes clauses, except for the cases
8138 // listed below. For these exceptions only, listing a predetermined
8139 // variable in a data-sharing attribute clause is allowed and overrides
8140 // the variable's predetermined data-sharing attributes.
8141 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8142 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
8143 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8144 << getOpenMPClauseName(OMPC_private);
8145 ReportOriginalDSA(*this, DSAStack, D, DVar);
8149 auto CurrDir = DSAStack->getCurrentDirective();
8150 // Variably modified types are not supported for tasks.
8151 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
8152 isOpenMPTaskingDirective(CurrDir)) {
8153 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8154 << getOpenMPClauseName(OMPC_private) << Type
8155 << getOpenMPDirectiveName(CurrDir);
8158 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8159 Diag(D->getLocation(),
8160 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8165 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8166 // A list item cannot appear in both a map clause and a data-sharing
8167 // attribute clause on the same construct
8168 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
8169 CurrDir == OMPD_target_teams ||
8170 CurrDir == OMPD_target_teams_distribute ||
8171 CurrDir == OMPD_target_teams_distribute_parallel_for ||
8172 CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
8173 CurrDir == OMPD_target_teams_distribute_simd ||
8174 CurrDir == OMPD_target_parallel_for_simd ||
8175 CurrDir == OMPD_target_parallel_for) {
8176 OpenMPClauseKind ConflictKind;
8177 if (DSAStack->checkMappableExprComponentListsForDecl(
8178 VD, /*CurrentRegionOnly=*/true,
8179 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8180 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8181 ConflictKind = WhereFoundClauseKind;
8184 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
8185 << getOpenMPClauseName(OMPC_private)
8186 << getOpenMPClauseName(ConflictKind)
8187 << getOpenMPDirectiveName(CurrDir);
8188 ReportOriginalDSA(*this, DSAStack, D, DVar);
8193 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8194 // A variable of class type (or array thereof) that appears in a private
8195 // clause requires an accessible, unambiguous default constructor for the
8197 // Generate helper private variable and initialize it with the default
8198 // value. The address of the original variable is replaced by the address of
8199 // the new private variable in CodeGen. This new variable is not added to
8200 // IdResolver, so the code in the OpenMP region uses original variable for
8201 // proper diagnostics.
8202 Type = Type.getUnqualifiedType();
8203 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8204 D->hasAttrs() ? &D->getAttrs() : nullptr);
8205 ActOnUninitializedDecl(VDPrivate);
8206 if (VDPrivate->isInvalidDecl())
8208 auto VDPrivateRefExpr = buildDeclRefExpr(
8209 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
8211 DeclRefExpr *Ref = nullptr;
8212 if (!VD && !CurContext->isDependentContext())
8213 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8214 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
8215 Vars.push_back((VD || CurContext->isDependentContext())
8216 ? RefExpr->IgnoreParens()
8218 PrivateCopies.push_back(VDPrivateRefExpr);
8224 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8229 class DiagsUninitializedSeveretyRAII {
8231 DiagnosticsEngine &Diags;
8232 SourceLocation SavedLoc;
8236 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8238 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8240 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8241 /*Map*/ diag::Severity::Ignored, Loc);
8244 ~DiagsUninitializedSeveretyRAII() {
8246 Diags.popMappings(SavedLoc);
8251 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8252 SourceLocation StartLoc,
8253 SourceLocation LParenLoc,
8254 SourceLocation EndLoc) {
8255 SmallVector<Expr *, 8> Vars;
8256 SmallVector<Expr *, 8> PrivateCopies;
8257 SmallVector<Expr *, 8> Inits;
8258 SmallVector<Decl *, 4> ExprCaptures;
8259 bool IsImplicitClause =
8260 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8261 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8263 for (auto &RefExpr : VarList) {
8264 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
8265 SourceLocation ELoc;
8267 Expr *SimpleRefExpr = RefExpr;
8268 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8270 // It will be analyzed later.
8271 Vars.push_back(RefExpr);
8272 PrivateCopies.push_back(nullptr);
8273 Inits.push_back(nullptr);
8275 ValueDecl *D = Res.first;
8279 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
8280 QualType Type = D->getType();
8281 auto *VD = dyn_cast<VarDecl>(D);
8283 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8284 // A variable that appears in a private clause must not have an incomplete
8285 // type or a reference type.
8286 if (RequireCompleteType(ELoc, Type,
8287 diag::err_omp_firstprivate_incomplete_type))
8289 Type = Type.getNonReferenceType();
8291 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8292 // A variable of class type (or array thereof) that appears in a private
8293 // clause requires an accessible, unambiguous copy constructor for the
8295 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
8297 // If an implicit firstprivate variable found it was checked already.
8298 DSAStackTy::DSAVarData TopDVar;
8299 if (!IsImplicitClause) {
8300 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8302 bool IsConstant = ElemType.isConstant(Context);
8303 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8304 // A list item that specifies a given variable may not appear in more
8305 // than one clause on the same directive, except that a variable may be
8306 // specified in both firstprivate and lastprivate clauses.
8307 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
8308 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
8309 Diag(ELoc, diag::err_omp_wrong_dsa)
8310 << getOpenMPClauseName(DVar.CKind)
8311 << getOpenMPClauseName(OMPC_firstprivate);
8312 ReportOriginalDSA(*this, DSAStack, D, DVar);
8316 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8318 // Variables with the predetermined data-sharing attributes may not be
8319 // listed in data-sharing attributes clauses, except for the cases
8320 // listed below. For these exceptions only, listing a predetermined
8321 // variable in a data-sharing attribute clause is allowed and overrides
8322 // the variable's predetermined data-sharing attributes.
8323 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8324 // in a Construct, C/C++, p.2]
8325 // Variables with const-qualified type having no mutable member may be
8326 // listed in a firstprivate clause, even if they are static data members.
8327 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
8328 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8329 Diag(ELoc, diag::err_omp_wrong_dsa)
8330 << getOpenMPClauseName(DVar.CKind)
8331 << getOpenMPClauseName(OMPC_firstprivate);
8332 ReportOriginalDSA(*this, DSAStack, D, DVar);
8336 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8337 // OpenMP [2.9.3.4, Restrictions, p.2]
8338 // A list item that is private within a parallel region must not appear
8339 // in a firstprivate clause on a worksharing construct if any of the
8340 // worksharing regions arising from the worksharing construct ever bind
8341 // to any of the parallel regions arising from the parallel construct.
8342 if (isOpenMPWorksharingDirective(CurrDir) &&
8343 !isOpenMPParallelDirective(CurrDir) &&
8344 !isOpenMPTeamsDirective(CurrDir)) {
8345 DVar = DSAStack->getImplicitDSA(D, true);
8346 if (DVar.CKind != OMPC_shared &&
8347 (isOpenMPParallelDirective(DVar.DKind) ||
8348 DVar.DKind == OMPD_unknown)) {
8349 Diag(ELoc, diag::err_omp_required_access)
8350 << getOpenMPClauseName(OMPC_firstprivate)
8351 << getOpenMPClauseName(OMPC_shared);
8352 ReportOriginalDSA(*this, DSAStack, D, DVar);
8356 // OpenMP [2.9.3.4, Restrictions, p.3]
8357 // A list item that appears in a reduction clause of a parallel construct
8358 // must not appear in a firstprivate clause on a worksharing or task
8359 // construct if any of the worksharing or task regions arising from the
8360 // worksharing or task construct ever bind to any of the parallel regions
8361 // arising from the parallel construct.
8362 // OpenMP [2.9.3.4, Restrictions, p.4]
8363 // A list item that appears in a reduction clause in worksharing
8364 // construct must not appear in a firstprivate clause in a task construct
8365 // encountered during execution of any of the worksharing regions arising
8366 // from the worksharing construct.
8367 if (isOpenMPTaskingDirective(CurrDir)) {
8368 DVar = DSAStack->hasInnermostDSA(
8369 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8370 [](OpenMPDirectiveKind K) -> bool {
8371 return isOpenMPParallelDirective(K) ||
8372 isOpenMPWorksharingDirective(K);
8375 if (DVar.CKind == OMPC_reduction &&
8376 (isOpenMPParallelDirective(DVar.DKind) ||
8377 isOpenMPWorksharingDirective(DVar.DKind))) {
8378 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8379 << getOpenMPDirectiveName(DVar.DKind);
8380 ReportOriginalDSA(*this, DSAStack, D, DVar);
8385 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8386 // A list item that is private within a teams region must not appear in a
8387 // firstprivate clause on a distribute construct if any of the distribute
8388 // regions arising from the distribute construct ever bind to any of the
8389 // teams regions arising from the teams construct.
8390 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8391 // A list item that appears in a reduction clause of a teams construct
8392 // must not appear in a firstprivate clause on a distribute construct if
8393 // any of the distribute regions arising from the distribute construct
8394 // ever bind to any of the teams regions arising from the teams construct.
8395 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8396 // A list item may appear in a firstprivate or lastprivate clause but not
8398 if (CurrDir == OMPD_distribute) {
8399 DVar = DSAStack->hasInnermostDSA(
8400 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8401 [](OpenMPDirectiveKind K) -> bool {
8402 return isOpenMPTeamsDirective(K);
8405 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8406 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
8407 ReportOriginalDSA(*this, DSAStack, D, DVar);
8410 DVar = DSAStack->hasInnermostDSA(
8411 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8412 [](OpenMPDirectiveKind K) -> bool {
8413 return isOpenMPTeamsDirective(K);
8416 if (DVar.CKind == OMPC_reduction &&
8417 isOpenMPTeamsDirective(DVar.DKind)) {
8418 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
8419 ReportOriginalDSA(*this, DSAStack, D, DVar);
8422 DVar = DSAStack->getTopDSA(D, false);
8423 if (DVar.CKind == OMPC_lastprivate) {
8424 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8425 ReportOriginalDSA(*this, DSAStack, D, DVar);
8429 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8430 // A list item cannot appear in both a map clause and a data-sharing
8431 // attribute clause on the same construct
8432 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
8433 CurrDir == OMPD_target_teams ||
8434 CurrDir == OMPD_target_teams_distribute ||
8435 CurrDir == OMPD_target_teams_distribute_parallel_for ||
8436 CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
8437 CurrDir == OMPD_target_teams_distribute_simd ||
8438 CurrDir == OMPD_target_parallel_for_simd ||
8439 CurrDir == OMPD_target_parallel_for) {
8440 OpenMPClauseKind ConflictKind;
8441 if (DSAStack->checkMappableExprComponentListsForDecl(
8442 VD, /*CurrentRegionOnly=*/true,
8443 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8444 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8445 ConflictKind = WhereFoundClauseKind;
8448 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
8449 << getOpenMPClauseName(OMPC_firstprivate)
8450 << getOpenMPClauseName(ConflictKind)
8451 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8452 ReportOriginalDSA(*this, DSAStack, D, DVar);
8458 // Variably modified types are not supported for tasks.
8459 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
8460 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
8461 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8462 << getOpenMPClauseName(OMPC_firstprivate) << Type
8463 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8466 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8467 Diag(D->getLocation(),
8468 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8473 Type = Type.getUnqualifiedType();
8474 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8475 D->hasAttrs() ? &D->getAttrs() : nullptr);
8476 // Generate helper private variable and initialize it with the value of the
8477 // original variable. The address of the original variable is replaced by
8478 // the address of the new private variable in the CodeGen. This new variable
8479 // is not added to IdResolver, so the code in the OpenMP region uses
8480 // original variable for proper diagnostics and variable capturing.
8481 Expr *VDInitRefExpr = nullptr;
8482 // For arrays generate initializer for single element and replace it by the
8483 // original array element in CodeGen.
8484 if (Type->isArrayType()) {
8486 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
8487 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
8488 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
8489 ElemType = ElemType.getUnqualifiedType();
8490 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
8491 ".firstprivate.temp");
8492 InitializedEntity Entity =
8493 InitializedEntity::InitializeVariable(VDInitTemp);
8494 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8496 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8497 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8498 if (Result.isInvalid())
8499 VDPrivate->setInvalidDecl();
8501 VDPrivate->setInit(Result.getAs<Expr>());
8502 // Remove temp variable declaration.
8503 Context.Deallocate(VDInitTemp);
8505 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8506 ".firstprivate.temp");
8507 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8508 RefExpr->getExprLoc());
8509 AddInitializerToDecl(VDPrivate,
8510 DefaultLvalueConversion(VDInitRefExpr).get(),
8511 /*DirectInit=*/false);
8513 if (VDPrivate->isInvalidDecl()) {
8514 if (IsImplicitClause) {
8515 Diag(RefExpr->getExprLoc(),
8516 diag::note_omp_task_predetermined_firstprivate_here);
8520 CurContext->addDecl(VDPrivate);
8521 auto VDPrivateRefExpr = buildDeclRefExpr(
8522 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8523 RefExpr->getExprLoc());
8524 DeclRefExpr *Ref = nullptr;
8525 if (!VD && !CurContext->isDependentContext()) {
8526 if (TopDVar.CKind == OMPC_lastprivate)
8527 Ref = TopDVar.PrivateCopy;
8529 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8530 if (!IsOpenMPCapturedDecl(D))
8531 ExprCaptures.push_back(Ref->getDecl());
8534 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
8535 Vars.push_back((VD || CurContext->isDependentContext())
8536 ? RefExpr->IgnoreParens()
8538 PrivateCopies.push_back(VDPrivateRefExpr);
8539 Inits.push_back(VDInitRefExpr);
8545 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8546 Vars, PrivateCopies, Inits,
8547 buildPreInits(Context, ExprCaptures));
8550 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8551 SourceLocation StartLoc,
8552 SourceLocation LParenLoc,
8553 SourceLocation EndLoc) {
8554 SmallVector<Expr *, 8> Vars;
8555 SmallVector<Expr *, 8> SrcExprs;
8556 SmallVector<Expr *, 8> DstExprs;
8557 SmallVector<Expr *, 8> AssignmentOps;
8558 SmallVector<Decl *, 4> ExprCaptures;
8559 SmallVector<Expr *, 4> ExprPostUpdates;
8560 for (auto &RefExpr : VarList) {
8561 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
8562 SourceLocation ELoc;
8564 Expr *SimpleRefExpr = RefExpr;
8565 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8567 // It will be analyzed later.
8568 Vars.push_back(RefExpr);
8569 SrcExprs.push_back(nullptr);
8570 DstExprs.push_back(nullptr);
8571 AssignmentOps.push_back(nullptr);
8573 ValueDecl *D = Res.first;
8577 QualType Type = D->getType();
8578 auto *VD = dyn_cast<VarDecl>(D);
8580 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8581 // A variable that appears in a lastprivate clause must not have an
8582 // incomplete type or a reference type.
8583 if (RequireCompleteType(ELoc, Type,
8584 diag::err_omp_lastprivate_incomplete_type))
8586 Type = Type.getNonReferenceType();
8588 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8590 // Variables with the predetermined data-sharing attributes may not be
8591 // listed in data-sharing attributes clauses, except for the cases
8593 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8594 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8595 DVar.CKind != OMPC_firstprivate &&
8596 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8597 Diag(ELoc, diag::err_omp_wrong_dsa)
8598 << getOpenMPClauseName(DVar.CKind)
8599 << getOpenMPClauseName(OMPC_lastprivate);
8600 ReportOriginalDSA(*this, DSAStack, D, DVar);
8604 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8605 // OpenMP [2.14.3.5, Restrictions, p.2]
8606 // A list item that is private within a parallel region, or that appears in
8607 // the reduction clause of a parallel construct, must not appear in a
8608 // lastprivate clause on a worksharing construct if any of the corresponding
8609 // worksharing regions ever binds to any of the corresponding parallel
8611 DSAStackTy::DSAVarData TopDVar = DVar;
8612 if (isOpenMPWorksharingDirective(CurrDir) &&
8613 !isOpenMPParallelDirective(CurrDir) &&
8614 !isOpenMPTeamsDirective(CurrDir)) {
8615 DVar = DSAStack->getImplicitDSA(D, true);
8616 if (DVar.CKind != OMPC_shared) {
8617 Diag(ELoc, diag::err_omp_required_access)
8618 << getOpenMPClauseName(OMPC_lastprivate)
8619 << getOpenMPClauseName(OMPC_shared);
8620 ReportOriginalDSA(*this, DSAStack, D, DVar);
8625 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8626 // A list item may appear in a firstprivate or lastprivate clause but not
8628 if (CurrDir == OMPD_distribute) {
8629 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8630 if (DVar.CKind == OMPC_firstprivate) {
8631 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8632 ReportOriginalDSA(*this, DSAStack, D, DVar);
8637 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
8638 // A variable of class type (or array thereof) that appears in a
8639 // lastprivate clause requires an accessible, unambiguous default
8640 // constructor for the class type, unless the list item is also specified
8641 // in a firstprivate clause.
8642 // A variable of class type (or array thereof) that appears in a
8643 // lastprivate clause requires an accessible, unambiguous copy assignment
8644 // operator for the class type.
8645 Type = Context.getBaseElementType(Type).getNonReferenceType();
8646 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
8647 Type.getUnqualifiedType(), ".lastprivate.src",
8648 D->hasAttrs() ? &D->getAttrs() : nullptr);
8649 auto *PseudoSrcExpr =
8650 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
8652 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
8653 D->hasAttrs() ? &D->getAttrs() : nullptr);
8654 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
8655 // For arrays generate assignment operation for single element and replace
8656 // it by the original array element in CodeGen.
8657 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
8658 PseudoDstExpr, PseudoSrcExpr);
8659 if (AssignmentOp.isInvalid())
8661 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
8662 /*DiscardedValue=*/true);
8663 if (AssignmentOp.isInvalid())
8666 DeclRefExpr *Ref = nullptr;
8667 if (!VD && !CurContext->isDependentContext()) {
8668 if (TopDVar.CKind == OMPC_firstprivate)
8669 Ref = TopDVar.PrivateCopy;
8671 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8672 if (!IsOpenMPCapturedDecl(D))
8673 ExprCaptures.push_back(Ref->getDecl());
8675 if (TopDVar.CKind == OMPC_firstprivate ||
8676 (!IsOpenMPCapturedDecl(D) &&
8677 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
8678 ExprResult RefRes = DefaultLvalueConversion(Ref);
8679 if (!RefRes.isUsable())
8681 ExprResult PostUpdateRes =
8682 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8684 if (!PostUpdateRes.isUsable())
8686 ExprPostUpdates.push_back(
8687 IgnoredValueConversions(PostUpdateRes.get()).get());
8690 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8691 Vars.push_back((VD || CurContext->isDependentContext())
8692 ? RefExpr->IgnoreParens()
8694 SrcExprs.push_back(PseudoSrcExpr);
8695 DstExprs.push_back(PseudoDstExpr);
8696 AssignmentOps.push_back(AssignmentOp.get());
8702 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8703 Vars, SrcExprs, DstExprs, AssignmentOps,
8704 buildPreInits(Context, ExprCaptures),
8705 buildPostUpdate(*this, ExprPostUpdates));
8708 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8709 SourceLocation StartLoc,
8710 SourceLocation LParenLoc,
8711 SourceLocation EndLoc) {
8712 SmallVector<Expr *, 8> Vars;
8713 for (auto &RefExpr : VarList) {
8714 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
8715 SourceLocation ELoc;
8717 Expr *SimpleRefExpr = RefExpr;
8718 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8720 // It will be analyzed later.
8721 Vars.push_back(RefExpr);
8723 ValueDecl *D = Res.first;
8727 auto *VD = dyn_cast<VarDecl>(D);
8728 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8730 // Variables with the predetermined data-sharing attributes may not be
8731 // listed in data-sharing attributes clauses, except for the cases
8732 // listed below. For these exceptions only, listing a predetermined
8733 // variable in a data-sharing attribute clause is allowed and overrides
8734 // the variable's predetermined data-sharing attributes.
8735 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8736 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8738 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8739 << getOpenMPClauseName(OMPC_shared);
8740 ReportOriginalDSA(*this, DSAStack, D, DVar);
8744 DeclRefExpr *Ref = nullptr;
8745 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
8746 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8747 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
8748 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8749 ? RefExpr->IgnoreParens()
8756 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8760 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8764 bool VisitDeclRefExpr(DeclRefExpr *E) {
8765 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
8766 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
8767 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8769 if (DVar.CKind != OMPC_unknown)
8771 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8772 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8774 if (DVarPrivate.CKind != OMPC_unknown)
8780 bool VisitStmt(Stmt *S) {
8781 for (auto Child : S->children()) {
8782 if (Child && Visit(Child))
8787 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
8792 // Transform MemberExpression for specified FieldDecl of current class to
8793 // DeclRefExpr to specified OMPCapturedExprDecl.
8794 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8795 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8797 DeclRefExpr *CapturedExpr;
8800 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8801 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8803 ExprResult TransformMemberExpr(MemberExpr *E) {
8804 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8805 E->getMemberDecl() == Field) {
8806 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
8807 return CapturedExpr;
8809 return BaseTransform::TransformMemberExpr(E);
8811 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8815 template <typename T>
8816 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8817 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8818 for (auto &Set : Lookups) {
8819 for (auto *D : Set) {
8820 if (auto Res = Gen(cast<ValueDecl>(D)))
8828 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8829 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8830 const DeclarationNameInfo &ReductionId, QualType Ty,
8831 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8832 if (ReductionIdScopeSpec.isInvalid())
8834 SmallVector<UnresolvedSet<8>, 4> Lookups;
8836 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8837 Lookup.suppressDiagnostics();
8838 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8839 auto *D = Lookup.getRepresentativeDecl();
8842 } while (S && !S->isDeclScope(D));
8845 Lookups.push_back(UnresolvedSet<8>());
8846 Lookups.back().append(Lookup.begin(), Lookup.end());
8849 } else if (auto *ULE =
8850 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8851 Lookups.push_back(UnresolvedSet<8>());
8852 Decl *PrevD = nullptr;
8853 for (auto *D : ULE->decls()) {
8855 Lookups.push_back(UnresolvedSet<8>());
8856 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8857 Lookups.back().addDecl(DRD);
8861 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8862 Ty->containsUnexpandedParameterPack() ||
8863 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8864 return !D->isInvalidDecl() &&
8865 (D->getType()->isDependentType() ||
8866 D->getType()->isInstantiationDependentType() ||
8867 D->getType()->containsUnexpandedParameterPack());
8869 UnresolvedSet<8> ResSet;
8870 for (auto &Set : Lookups) {
8871 ResSet.append(Set.begin(), Set.end());
8872 // The last item marks the end of all declarations at the specified scope.
8873 ResSet.addDecl(Set[Set.size() - 1]);
8875 return UnresolvedLookupExpr::Create(
8876 SemaRef.Context, /*NamingClass=*/nullptr,
8877 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8878 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8880 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8881 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8882 if (!D->isInvalidDecl() &&
8883 SemaRef.Context.hasSameType(D->getType(), Ty))
8887 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8888 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8889 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8890 if (!D->isInvalidDecl() &&
8891 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8892 !Ty.isMoreQualifiedThan(D->getType()))
8896 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8897 /*DetectVirtual=*/false);
8898 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8899 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8900 VD->getType().getUnqualifiedType()))) {
8901 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8903 Sema::AR_inaccessible) {
8904 SemaRef.BuildBasePathArray(Paths, BasePath);
8905 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8910 if (ReductionIdScopeSpec.isSet()) {
8911 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8918 /// Data for the reduction-based clauses.
8919 struct ReductionData {
8920 /// List of original reduction items.
8921 SmallVector<Expr *, 8> Vars;
8922 /// List of private copies of the reduction items.
8923 SmallVector<Expr *, 8> Privates;
8924 /// LHS expressions for the reduction_op expressions.
8925 SmallVector<Expr *, 8> LHSs;
8926 /// RHS expressions for the reduction_op expressions.
8927 SmallVector<Expr *, 8> RHSs;
8928 /// Reduction operation expression.
8929 SmallVector<Expr *, 8> ReductionOps;
8930 /// List of captures for clause.
8931 SmallVector<Decl *, 4> ExprCaptures;
8932 /// List of postupdate expressions.
8933 SmallVector<Expr *, 4> ExprPostUpdates;
8934 ReductionData() = delete;
8935 /// Reserves required memory for the reduction data.
8936 ReductionData(unsigned Size) {
8938 Privates.reserve(Size);
8941 ReductionOps.reserve(Size);
8942 ExprCaptures.reserve(Size);
8943 ExprPostUpdates.reserve(Size);
8945 /// Stores reduction item and reduction operation only (required for dependent
8946 /// reduction item).
8947 void push(Expr *Item, Expr *ReductionOp) {
8948 Vars.emplace_back(Item);
8949 Privates.emplace_back(nullptr);
8950 LHSs.emplace_back(nullptr);
8951 RHSs.emplace_back(nullptr);
8952 ReductionOps.emplace_back(ReductionOp);
8954 /// Stores reduction data.
8955 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS,
8956 Expr *ReductionOp) {
8957 Vars.emplace_back(Item);
8958 Privates.emplace_back(Private);
8959 LHSs.emplace_back(LHS);
8960 RHSs.emplace_back(RHS);
8961 ReductionOps.emplace_back(ReductionOp);
8966 static bool ActOnOMPReductionKindClause(
8967 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
8968 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8969 SourceLocation ColonLoc, SourceLocation EndLoc,
8970 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8971 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
8972 auto DN = ReductionId.getName();
8973 auto OOK = DN.getCXXOverloadedOperator();
8974 BinaryOperatorKind BOK = BO_Comma;
8976 ASTContext &Context = S.Context;
8977 // OpenMP [2.14.3.6, reduction clause]
8979 // reduction-identifier is either an identifier or one of the following
8980 // operators: +, -, *, &, |, ^, && and ||
8982 // reduction-identifier is either an id-expression or one of the following
8983 // operators: +, -, *, &, |, ^, && and ||
8984 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9011 case OO_Array_Delete:
9020 case OO_GreaterEqual:
9025 case OO_PercentEqual:
9030 case OO_GreaterGreater:
9031 case OO_LessLessEqual:
9032 case OO_GreaterGreaterEqual:
9034 case OO_ExclaimEqual:
9042 case OO_Conditional:
9044 case NUM_OVERLOADED_OPERATORS:
9045 llvm_unreachable("Unexpected reduction identifier");
9047 if (auto II = DN.getAsIdentifierInfo()) {
9048 if (II->isStr("max"))
9050 else if (II->isStr("min"))
9055 SourceRange ReductionIdRange;
9056 if (ReductionIdScopeSpec.isValid())
9057 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
9058 ReductionIdRange.setEnd(ReductionId.getEndLoc());
9060 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9061 bool FirstIter = true;
9062 for (auto RefExpr : VarList) {
9063 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
9064 // OpenMP [2.1, C/C++]
9065 // A list item is a variable or array section, subject to the restrictions
9066 // specified in Section 2.4 on page 42 and in each of the sections
9067 // describing clauses and directives for which a list appears.
9068 // OpenMP [2.14.3.3, Restrictions, p.1]
9069 // A variable that is part of another variable (as an array or
9070 // structure element) cannot appear in a private clause.
9071 if (!FirstIter && IR != ER)
9074 SourceLocation ELoc;
9076 Expr *SimpleRefExpr = RefExpr;
9077 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
9078 /*AllowArraySection=*/true);
9080 // Try to find 'declare reduction' corresponding construct before using
9081 // builtin/overloaded operators.
9082 QualType Type = Context.DependentTy;
9083 CXXCastPath BasePath;
9084 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9085 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
9086 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9087 Expr *ReductionOp = nullptr;
9088 if (S.CurContext->isDependentContext() &&
9089 (DeclareReductionRef.isUnset() ||
9090 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9091 ReductionOp = DeclareReductionRef.get();
9092 // It will be analyzed later.
9093 RD.push(RefExpr, ReductionOp);
9095 ValueDecl *D = Res.first;
9100 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9101 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9103 Type = ASE->getType().getNonReferenceType();
9105 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9106 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9107 Type = ATy->getElementType();
9109 Type = BaseType->getPointeeType();
9110 Type = Type.getNonReferenceType();
9112 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9113 auto *VD = dyn_cast<VarDecl>(D);
9115 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9116 // A variable that appears in a private clause must not have an incomplete
9117 // type or a reference type.
9118 if (S.RequireCompleteType(ELoc, Type,
9119 diag::err_omp_reduction_incomplete_type))
9121 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9122 // A list item that appears in a reduction clause must not be
9124 if (Type.getNonReferenceType().isConstant(Context)) {
9125 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
9126 if (!ASE && !OASE) {
9127 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
9128 VarDecl::DeclarationOnly;
9129 S.Diag(D->getLocation(),
9130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9135 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9136 // If a list-item is a reference type then it must bind to the same object
9137 // for all threads of the team.
9138 if (!ASE && !OASE && VD) {
9139 VarDecl *VDDef = VD->getDefinition();
9140 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
9141 DSARefChecker Check(Stack);
9142 if (Check.Visit(VDDef->getInit())) {
9143 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
9144 << getOpenMPClauseName(ClauseKind) << ERange;
9145 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9151 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9153 // Variables with the predetermined data-sharing attributes may not be
9154 // listed in data-sharing attributes clauses, except for the cases
9155 // listed below. For these exceptions only, listing a predetermined
9156 // variable in a data-sharing attribute clause is allowed and overrides
9157 // the variable's predetermined data-sharing attributes.
9158 // OpenMP [2.14.3.6, Restrictions, p.3]
9159 // Any number of reduction clauses can be specified on the directive,
9160 // but a list item can appear only once in the reduction clauses for that
9162 DSAStackTy::DSAVarData DVar;
9163 DVar = Stack->getTopDSA(D, false);
9164 if (DVar.CKind == OMPC_reduction) {
9165 S.Diag(ELoc, diag::err_omp_once_referenced)
9166 << getOpenMPClauseName(ClauseKind);
9168 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
9169 } else if (DVar.CKind != OMPC_unknown) {
9170 S.Diag(ELoc, diag::err_omp_wrong_dsa)
9171 << getOpenMPClauseName(DVar.CKind)
9172 << getOpenMPClauseName(OMPC_reduction);
9173 ReportOriginalDSA(S, Stack, D, DVar);
9177 // OpenMP [2.14.3.6, Restrictions, p.1]
9178 // A list item that appears in a reduction clause of a worksharing
9179 // construct must be shared in the parallel regions to which any of the
9180 // worksharing regions arising from the worksharing construct bind.
9181 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
9182 if (isOpenMPWorksharingDirective(CurrDir) &&
9183 !isOpenMPParallelDirective(CurrDir) &&
9184 !isOpenMPTeamsDirective(CurrDir)) {
9185 DVar = Stack->getImplicitDSA(D, true);
9186 if (DVar.CKind != OMPC_shared) {
9187 S.Diag(ELoc, diag::err_omp_required_access)
9188 << getOpenMPClauseName(OMPC_reduction)
9189 << getOpenMPClauseName(OMPC_shared);
9190 ReportOriginalDSA(S, Stack, D, DVar);
9195 // Try to find 'declare reduction' corresponding construct before using
9196 // builtin/overloaded operators.
9197 CXXCastPath BasePath;
9198 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9199 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
9200 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9201 if (DeclareReductionRef.isInvalid())
9203 if (S.CurContext->isDependentContext() &&
9204 (DeclareReductionRef.isUnset() ||
9205 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9206 RD.push(RefExpr, DeclareReductionRef.get());
9209 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9210 // Not allowed reduction identifier is found.
9211 S.Diag(ReductionId.getLocStart(),
9212 diag::err_omp_unknown_reduction_identifier)
9213 << Type << ReductionIdRange;
9217 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9218 // The type of a list item that appears in a reduction clause must be valid
9219 // for the reduction-identifier. For a max or min reduction in C, the type
9220 // of the list item must be an allowed arithmetic data type: char, int,
9221 // float, double, or _Bool, possibly modified with long, short, signed, or
9222 // unsigned. For a max or min reduction in C++, the type of the list item
9223 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9224 // double, or bool, possibly modified with long, short, signed, or unsigned.
9225 if (DeclareReductionRef.isUnset()) {
9226 if ((BOK == BO_GT || BOK == BO_LT) &&
9227 !(Type->isScalarType() ||
9228 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9229 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9230 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
9231 if (!ASE && !OASE) {
9232 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
9233 VarDecl::DeclarationOnly;
9234 S.Diag(D->getLocation(),
9235 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9240 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9241 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
9242 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
9243 << getOpenMPClauseName(ClauseKind);
9244 if (!ASE && !OASE) {
9245 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
9246 VarDecl::DeclarationOnly;
9247 S.Diag(D->getLocation(),
9248 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9255 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
9256 auto *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
9257 D->hasAttrs() ? &D->getAttrs() : nullptr);
9258 auto *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
9259 D->hasAttrs() ? &D->getAttrs() : nullptr);
9260 auto PrivateTy = Type;
9263 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
9264 // For arrays/array sections only:
9265 // Create pseudo array type for private copy. The size for this array will
9266 // be generated during codegen.
9267 // For array subscripts or single variables Private Ty is the same as Type
9268 // (type of the variable or single array element).
9269 PrivateTy = Context.getVariableArrayType(
9271 new (Context) OpaqueValueExpr(SourceLocation(), Context.getSizeType(),
9273 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
9274 } else if (!ASE && !OASE &&
9275 Context.getAsArrayType(D->getType().getNonReferenceType()))
9276 PrivateTy = D->getType().getNonReferenceType();
9278 auto *PrivateVD = buildVarDecl(S, ELoc, PrivateTy, D->getName(),
9279 D->hasAttrs() ? &D->getAttrs() : nullptr);
9280 // Add initializer for private variable.
9281 Expr *Init = nullptr;
9282 auto *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
9283 auto *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
9284 if (DeclareReductionRef.isUsable()) {
9285 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9286 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9287 if (DRD->getInitializer()) {
9289 RHSVD->setInit(DRDRef);
9290 RHSVD->setInitStyle(VarDecl::CallInit);
9298 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9299 if (Type->isScalarType() || Type->isAnyComplexType())
9300 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9304 if (Type->isScalarType() || Type->isAnyComplexType()) {
9305 // '*' and '&&' reduction ops - initializer is '1'.
9306 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
9310 // '&' reduction op - initializer is '~0'.
9311 QualType OrigType = Type;
9312 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9313 Type = ComplexTy->getElementType();
9314 if (Type->isRealFloatingType()) {
9315 llvm::APFloat InitValue =
9316 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9318 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9320 } else if (Type->isScalarType()) {
9321 auto Size = Context.getTypeSize(Type);
9322 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9323 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9324 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9326 if (Init && OrigType->isAnyComplexType()) {
9327 // Init = 0xFFFF + 0xFFFFi;
9328 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9329 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9336 // 'min' reduction op - initializer is 'Largest representable number in
9337 // the reduction list item type'.
9338 // 'max' reduction op - initializer is 'Least representable number in
9339 // the reduction list item type'.
9340 if (Type->isIntegerType() || Type->isPointerType()) {
9341 bool IsSigned = Type->hasSignedIntegerRepresentation();
9342 auto Size = Context.getTypeSize(Type);
9344 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9345 llvm::APInt InitValue =
9346 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9347 : llvm::APInt::getMinValue(Size)
9348 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9349 : llvm::APInt::getMaxValue(Size);
9350 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9351 if (Type->isPointerType()) {
9352 // Cast to pointer type.
9353 auto CastExpr = S.BuildCStyleCastExpr(
9354 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9355 SourceLocation(), Init);
9356 if (CastExpr.isInvalid())
9358 Init = CastExpr.get();
9360 } else if (Type->isRealFloatingType()) {
9361 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9362 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9363 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9391 llvm_unreachable("Unexpected reduction operation");
9394 if (Init && DeclareReductionRef.isUnset())
9395 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
9397 S.ActOnUninitializedDecl(RHSVD);
9398 if (RHSVD->isInvalidDecl())
9400 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
9401 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
9402 << Type << ReductionIdRange;
9403 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
9404 VarDecl::DeclarationOnly;
9405 S.Diag(D->getLocation(),
9406 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9410 // Store initializer for single element in private copy. Will be used during
9412 PrivateVD->setInit(RHSVD->getInit());
9413 PrivateVD->setInitStyle(RHSVD->getInitStyle());
9414 auto *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
9415 ExprResult ReductionOp;
9416 if (DeclareReductionRef.isUsable()) {
9417 QualType RedTy = DeclareReductionRef.get()->getType();
9418 QualType PtrRedTy = Context.getPointerType(RedTy);
9419 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9420 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9421 if (!BasePath.empty()) {
9422 LHS = S.DefaultLvalueConversion(LHS.get());
9423 RHS = S.DefaultLvalueConversion(RHS.get());
9424 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9425 CK_UncheckedDerivedToBase, LHS.get(),
9426 &BasePath, LHS.get()->getValueKind());
9427 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9428 CK_UncheckedDerivedToBase, RHS.get(),
9429 &BasePath, RHS.get()->getValueKind());
9431 FunctionProtoType::ExtProtoInfo EPI;
9432 QualType Params[] = {PtrRedTy, PtrRedTy};
9433 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9434 auto *OVE = new (Context) OpaqueValueExpr(
9435 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9436 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
9437 Expr *Args[] = {LHS.get(), RHS.get()};
9438 ReductionOp = new (Context)
9439 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9441 ReductionOp = S.BuildBinOp(
9442 Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9443 if (ReductionOp.isUsable()) {
9444 if (BOK != BO_LT && BOK != BO_GT) {
9446 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
9447 BO_Assign, LHSDRE, ReductionOp.get());
9449 auto *ConditionalOp = new (Context) ConditionalOperator(
9450 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9451 RHSDRE, Type, VK_LValue, OK_Ordinary);
9453 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
9454 BO_Assign, LHSDRE, ConditionalOp);
9456 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
9458 if (ReductionOp.isInvalid())
9462 DeclRefExpr *Ref = nullptr;
9463 Expr *VarsExpr = RefExpr->IgnoreParens();
9464 if (!VD && !S.CurContext->isDependentContext()) {
9466 TransformExprToCaptures RebuildToCapture(S, D);
9468 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9469 Ref = RebuildToCapture.getCapturedExpr();
9471 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
9473 if (!S.IsOpenMPCapturedDecl(D)) {
9474 RD.ExprCaptures.emplace_back(Ref->getDecl());
9475 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9476 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
9477 if (!RefRes.isUsable())
9479 ExprResult PostUpdateRes =
9480 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9482 if (!PostUpdateRes.isUsable())
9484 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
9485 Stack->getCurrentDirective() == OMPD_taskgroup) {
9486 S.Diag(RefExpr->getExprLoc(),
9487 diag::err_omp_reduction_non_addressable_expression)
9488 << RefExpr->getSourceRange();
9491 RD.ExprPostUpdates.emplace_back(
9492 S.IgnoredValueConversions(PostUpdateRes.get()).get());
9496 // All reduction items are still marked as reduction (to do not increase
9498 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9499 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get());
9501 return RD.Vars.empty();
9504 OMPClause *Sema::ActOnOpenMPReductionClause(
9505 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9506 SourceLocation ColonLoc, SourceLocation EndLoc,
9507 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9508 ArrayRef<Expr *> UnresolvedReductions) {
9509 ReductionData RD(VarList.size());
9511 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
9512 StartLoc, LParenLoc, ColonLoc, EndLoc,
9513 ReductionIdScopeSpec, ReductionId,
9514 UnresolvedReductions, RD))
9517 return OMPReductionClause::Create(
9518 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
9519 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
9520 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
9521 buildPreInits(Context, RD.ExprCaptures),
9522 buildPostUpdate(*this, RD.ExprPostUpdates));
9525 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
9526 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9527 SourceLocation ColonLoc, SourceLocation EndLoc,
9528 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9529 ArrayRef<Expr *> UnresolvedReductions) {
9530 ReductionData RD(VarList.size());
9532 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction,
9533 VarList, StartLoc, LParenLoc, ColonLoc,
9534 EndLoc, ReductionIdScopeSpec, ReductionId,
9535 UnresolvedReductions, RD))
9538 return OMPTaskReductionClause::Create(
9539 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
9540 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
9541 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
9542 buildPreInits(Context, RD.ExprCaptures),
9543 buildPostUpdate(*this, RD.ExprPostUpdates));
9546 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9547 SourceLocation LinLoc) {
9548 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9549 LinKind == OMPC_LINEAR_unknown) {
9550 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9556 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9557 OpenMPLinearClauseKind LinKind,
9559 auto *VD = dyn_cast_or_null<VarDecl>(D);
9560 // A variable must not have an incomplete type or a reference type.
9561 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9563 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9564 !Type->isReferenceType()) {
9565 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9566 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9569 Type = Type.getNonReferenceType();
9571 // A list item must not be const-qualified.
9572 if (Type.isConstant(Context)) {
9573 Diag(ELoc, diag::err_omp_const_variable)
9574 << getOpenMPClauseName(OMPC_linear);
9578 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9579 Diag(D->getLocation(),
9580 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9586 // A list item must be of integral or pointer type.
9587 Type = Type.getUnqualifiedType().getCanonicalType();
9588 const auto *Ty = Type.getTypePtrOrNull();
9589 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9590 !Ty->isPointerType())) {
9591 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9595 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9596 Diag(D->getLocation(),
9597 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9605 OMPClause *Sema::ActOnOpenMPLinearClause(
9606 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9607 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9608 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9609 SmallVector<Expr *, 8> Vars;
9610 SmallVector<Expr *, 8> Privates;
9611 SmallVector<Expr *, 8> Inits;
9612 SmallVector<Decl *, 4> ExprCaptures;
9613 SmallVector<Expr *, 4> ExprPostUpdates;
9614 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
9615 LinKind = OMPC_LINEAR_val;
9616 for (auto &RefExpr : VarList) {
9617 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9618 SourceLocation ELoc;
9620 Expr *SimpleRefExpr = RefExpr;
9621 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9622 /*AllowArraySection=*/false);
9624 // It will be analyzed later.
9625 Vars.push_back(RefExpr);
9626 Privates.push_back(nullptr);
9627 Inits.push_back(nullptr);
9629 ValueDecl *D = Res.first;
9633 QualType Type = D->getType();
9634 auto *VD = dyn_cast<VarDecl>(D);
9636 // OpenMP [2.14.3.7, linear clause]
9637 // A list-item cannot appear in more than one linear clause.
9638 // A list-item that appears in a linear clause cannot appear in any
9639 // other data-sharing attribute clause.
9640 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9642 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9643 << getOpenMPClauseName(OMPC_linear);
9644 ReportOriginalDSA(*this, DSAStack, D, DVar);
9648 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
9650 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
9652 // Build private copy of original var.
9653 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9654 D->hasAttrs() ? &D->getAttrs() : nullptr);
9655 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
9656 // Build var to save initial value.
9657 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
9659 DeclRefExpr *Ref = nullptr;
9660 if (!VD && !CurContext->isDependentContext()) {
9661 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9662 if (!IsOpenMPCapturedDecl(D)) {
9663 ExprCaptures.push_back(Ref->getDecl());
9664 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9665 ExprResult RefRes = DefaultLvalueConversion(Ref);
9666 if (!RefRes.isUsable())
9668 ExprResult PostUpdateRes =
9669 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9670 SimpleRefExpr, RefRes.get());
9671 if (!PostUpdateRes.isUsable())
9673 ExprPostUpdates.push_back(
9674 IgnoredValueConversions(PostUpdateRes.get()).get());
9678 if (LinKind == OMPC_LINEAR_uval)
9679 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
9681 InitExpr = VD ? SimpleRefExpr : Ref;
9682 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
9683 /*DirectInit=*/false);
9684 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9686 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9687 Vars.push_back((VD || CurContext->isDependentContext())
9688 ? RefExpr->IgnoreParens()
9690 Privates.push_back(PrivateRef);
9691 Inits.push_back(InitRef);
9697 Expr *StepExpr = Step;
9698 Expr *CalcStepExpr = nullptr;
9699 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9700 !Step->isInstantiationDependent() &&
9701 !Step->containsUnexpandedParameterPack()) {
9702 SourceLocation StepLoc = Step->getLocStart();
9703 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
9704 if (Val.isInvalid())
9706 StepExpr = Val.get();
9708 // Build var to save the step value.
9710 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
9711 ExprResult SaveRef =
9712 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
9713 ExprResult CalcStep =
9714 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
9715 CalcStep = ActOnFinishFullExpr(CalcStep.get());
9717 // Warn about zero linear step (it would be probably better specified as
9718 // making corresponding variables 'const').
9719 llvm::APSInt Result;
9720 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9721 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
9722 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9723 << (Vars.size() > 1);
9724 if (!IsConstant && CalcStep.isUsable()) {
9725 // Calculate the step beforehand instead of doing this on each iteration.
9726 // (This is not used if the number of iterations may be kfold-ed).
9727 CalcStepExpr = CalcStep.get();
9731 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9732 ColonLoc, EndLoc, Vars, Privates, Inits,
9733 StepExpr, CalcStepExpr,
9734 buildPreInits(Context, ExprCaptures),
9735 buildPostUpdate(*this, ExprPostUpdates));
9738 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9739 Expr *NumIterations, Sema &SemaRef,
9740 Scope *S, DSAStackTy *Stack) {
9741 // Walk the vars and build update/final expressions for the CodeGen.
9742 SmallVector<Expr *, 8> Updates;
9743 SmallVector<Expr *, 8> Finals;
9744 Expr *Step = Clause.getStep();
9745 Expr *CalcStep = Clause.getCalcStep();
9746 // OpenMP [2.14.3.7, linear clause]
9747 // If linear-step is not specified it is assumed to be 1.
9748 if (Step == nullptr)
9749 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
9750 else if (CalcStep) {
9751 Step = cast<BinaryOperator>(CalcStep)->getLHS();
9753 bool HasErrors = false;
9754 auto CurInit = Clause.inits().begin();
9755 auto CurPrivate = Clause.privates().begin();
9756 auto LinKind = Clause.getModifier();
9757 for (auto &RefExpr : Clause.varlists()) {
9758 SourceLocation ELoc;
9760 Expr *SimpleRefExpr = RefExpr;
9761 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9762 /*AllowArraySection=*/false);
9763 ValueDecl *D = Res.first;
9764 if (Res.second || !D) {
9765 Updates.push_back(nullptr);
9766 Finals.push_back(nullptr);
9770 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9771 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9774 auto &&Info = Stack->isLoopControlVariable(D);
9775 Expr *InitExpr = *CurInit;
9777 // Build privatized reference to the current linear var.
9778 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
9780 if (LinKind == OMPC_LINEAR_uval)
9781 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9784 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9785 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9786 /*RefersToCapture=*/true);
9788 // Build update: Var = InitExpr + IV * Step
9792 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9793 InitExpr, IV, Step, /* Subtract */ false);
9795 Update = *CurPrivate;
9796 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9797 /*DiscardedValue=*/true);
9799 // Build final: Var = InitExpr + NumIterations * Step
9802 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9803 InitExpr, NumIterations, Step,
9804 /* Subtract */ false);
9806 Final = *CurPrivate;
9807 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9808 /*DiscardedValue=*/true);
9810 if (!Update.isUsable() || !Final.isUsable()) {
9811 Updates.push_back(nullptr);
9812 Finals.push_back(nullptr);
9815 Updates.push_back(Update.get());
9816 Finals.push_back(Final.get());
9821 Clause.setUpdates(Updates);
9822 Clause.setFinals(Finals);
9826 OMPClause *Sema::ActOnOpenMPAlignedClause(
9827 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9828 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9830 SmallVector<Expr *, 8> Vars;
9831 for (auto &RefExpr : VarList) {
9832 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9833 SourceLocation ELoc;
9835 Expr *SimpleRefExpr = RefExpr;
9836 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9837 /*AllowArraySection=*/false);
9839 // It will be analyzed later.
9840 Vars.push_back(RefExpr);
9842 ValueDecl *D = Res.first;
9846 QualType QType = D->getType();
9847 auto *VD = dyn_cast<VarDecl>(D);
9849 // OpenMP [2.8.1, simd construct, Restrictions]
9850 // The type of list items appearing in the aligned clause must be
9851 // array, pointer, reference to array, or reference to pointer.
9852 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
9853 const Type *Ty = QType.getTypePtrOrNull();
9854 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
9855 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
9856 << QType << getLangOpts().CPlusPlus << ERange;
9859 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9860 Diag(D->getLocation(),
9861 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9866 // OpenMP [2.8.1, simd construct, Restrictions]
9867 // A list-item cannot appear in more than one aligned clause.
9868 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
9869 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
9870 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9871 << getOpenMPClauseName(OMPC_aligned);
9875 DeclRefExpr *Ref = nullptr;
9876 if (!VD && IsOpenMPCapturedDecl(D))
9877 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9878 Vars.push_back(DefaultFunctionArrayConversion(
9879 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9883 // OpenMP [2.8.1, simd construct, Description]
9884 // The parameter of the aligned clause, alignment, must be a constant
9885 // positive integer expression.
9886 // If no optional parameter is specified, implementation-defined default
9887 // alignments for SIMD instructions on the target platforms are assumed.
9888 if (Alignment != nullptr) {
9889 ExprResult AlignResult =
9890 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9891 if (AlignResult.isInvalid())
9893 Alignment = AlignResult.get();
9898 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9899 EndLoc, Vars, Alignment);
9902 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9903 SourceLocation StartLoc,
9904 SourceLocation LParenLoc,
9905 SourceLocation EndLoc) {
9906 SmallVector<Expr *, 8> Vars;
9907 SmallVector<Expr *, 8> SrcExprs;
9908 SmallVector<Expr *, 8> DstExprs;
9909 SmallVector<Expr *, 8> AssignmentOps;
9910 for (auto &RefExpr : VarList) {
9911 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9912 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
9913 // It will be analyzed later.
9914 Vars.push_back(RefExpr);
9915 SrcExprs.push_back(nullptr);
9916 DstExprs.push_back(nullptr);
9917 AssignmentOps.push_back(nullptr);
9921 SourceLocation ELoc = RefExpr->getExprLoc();
9922 // OpenMP [2.1, C/C++]
9923 // A list item is a variable name.
9924 // OpenMP [2.14.4.1, Restrictions, p.1]
9925 // A list item that appears in a copyin clause must be threadprivate.
9926 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
9927 if (!DE || !isa<VarDecl>(DE->getDecl())) {
9928 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9929 << 0 << RefExpr->getSourceRange();
9933 Decl *D = DE->getDecl();
9934 VarDecl *VD = cast<VarDecl>(D);
9936 QualType Type = VD->getType();
9937 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9938 // It will be analyzed later.
9940 SrcExprs.push_back(nullptr);
9941 DstExprs.push_back(nullptr);
9942 AssignmentOps.push_back(nullptr);
9946 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9947 // A list item that appears in a copyin clause must be threadprivate.
9948 if (!DSAStack->isThreadPrivate(VD)) {
9949 Diag(ELoc, diag::err_omp_required_access)
9950 << getOpenMPClauseName(OMPC_copyin)
9951 << getOpenMPDirectiveName(OMPD_threadprivate);
9955 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9956 // A variable of class type (or array thereof) that appears in a
9957 // copyin clause requires an accessible, unambiguous copy assignment
9958 // operator for the class type.
9959 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
9961 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9962 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9963 auto *PseudoSrcExpr = buildDeclRefExpr(
9964 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9966 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9967 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9968 auto *PseudoDstExpr =
9969 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
9970 // For arrays generate assignment operation for single element and replace
9971 // it by the original array element in CodeGen.
9972 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9973 PseudoDstExpr, PseudoSrcExpr);
9974 if (AssignmentOp.isInvalid())
9976 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9977 /*DiscardedValue=*/true);
9978 if (AssignmentOp.isInvalid())
9981 DSAStack->addDSA(VD, DE, OMPC_copyin);
9983 SrcExprs.push_back(PseudoSrcExpr);
9984 DstExprs.push_back(PseudoDstExpr);
9985 AssignmentOps.push_back(AssignmentOp.get());
9991 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9992 SrcExprs, DstExprs, AssignmentOps);
9995 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9996 SourceLocation StartLoc,
9997 SourceLocation LParenLoc,
9998 SourceLocation EndLoc) {
9999 SmallVector<Expr *, 8> Vars;
10000 SmallVector<Expr *, 8> SrcExprs;
10001 SmallVector<Expr *, 8> DstExprs;
10002 SmallVector<Expr *, 8> AssignmentOps;
10003 for (auto &RefExpr : VarList) {
10004 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10005 SourceLocation ELoc;
10006 SourceRange ERange;
10007 Expr *SimpleRefExpr = RefExpr;
10008 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10009 /*AllowArraySection=*/false);
10011 // It will be analyzed later.
10012 Vars.push_back(RefExpr);
10013 SrcExprs.push_back(nullptr);
10014 DstExprs.push_back(nullptr);
10015 AssignmentOps.push_back(nullptr);
10017 ValueDecl *D = Res.first;
10021 QualType Type = D->getType();
10022 auto *VD = dyn_cast<VarDecl>(D);
10024 // OpenMP [2.14.4.2, Restrictions, p.2]
10025 // A list item that appears in a copyprivate clause may not appear in a
10026 // private or firstprivate clause on the single construct.
10027 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10028 auto DVar = DSAStack->getTopDSA(D, false);
10029 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10031 Diag(ELoc, diag::err_omp_wrong_dsa)
10032 << getOpenMPClauseName(DVar.CKind)
10033 << getOpenMPClauseName(OMPC_copyprivate);
10034 ReportOriginalDSA(*this, DSAStack, D, DVar);
10038 // OpenMP [2.11.4.2, Restrictions, p.1]
10039 // All list items that appear in a copyprivate clause must be either
10040 // threadprivate or private in the enclosing context.
10041 if (DVar.CKind == OMPC_unknown) {
10042 DVar = DSAStack->getImplicitDSA(D, false);
10043 if (DVar.CKind == OMPC_shared) {
10044 Diag(ELoc, diag::err_omp_required_access)
10045 << getOpenMPClauseName(OMPC_copyprivate)
10046 << "threadprivate or private in the enclosing context";
10047 ReportOriginalDSA(*this, DSAStack, D, DVar);
10053 // Variably modified types are not supported.
10054 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
10055 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10056 << getOpenMPClauseName(OMPC_copyprivate) << Type
10057 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10060 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10061 Diag(D->getLocation(),
10062 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10067 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10068 // A variable of class type (or array thereof) that appears in a
10069 // copyin clause requires an accessible, unambiguous copy assignment
10070 // operator for the class type.
10071 Type = Context.getBaseElementType(Type.getNonReferenceType())
10072 .getUnqualifiedType();
10074 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10075 D->hasAttrs() ? &D->getAttrs() : nullptr);
10076 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
10078 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10079 D->hasAttrs() ? &D->getAttrs() : nullptr);
10080 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
10081 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10082 PseudoDstExpr, PseudoSrcExpr);
10083 if (AssignmentOp.isInvalid())
10085 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
10086 /*DiscardedValue=*/true);
10087 if (AssignmentOp.isInvalid())
10090 // No need to mark vars as copyprivate, they are already threadprivate or
10091 // implicitly private.
10092 assert(VD || IsOpenMPCapturedDecl(D));
10094 VD ? RefExpr->IgnoreParens()
10095 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
10096 SrcExprs.push_back(PseudoSrcExpr);
10097 DstExprs.push_back(PseudoDstExpr);
10098 AssignmentOps.push_back(AssignmentOp.get());
10104 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10105 Vars, SrcExprs, DstExprs, AssignmentOps);
10108 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10109 SourceLocation StartLoc,
10110 SourceLocation LParenLoc,
10111 SourceLocation EndLoc) {
10112 if (VarList.empty())
10115 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10119 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10120 SourceLocation DepLoc, SourceLocation ColonLoc,
10121 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10122 SourceLocation LParenLoc, SourceLocation EndLoc) {
10123 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
10124 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
10125 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
10126 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
10129 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
10130 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10131 DepKind == OMPC_DEPEND_sink)) {
10132 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
10133 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
10134 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10135 /*Last=*/OMPC_DEPEND_unknown, Except)
10136 << getOpenMPClauseName(OMPC_depend);
10139 SmallVector<Expr *, 8> Vars;
10140 DSAStackTy::OperatorOffsetTy OpsOffs;
10141 llvm::APSInt DepCounter(/*BitWidth=*/32);
10142 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10143 if (DepKind == OMPC_DEPEND_sink) {
10144 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10145 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10146 TotalDepCount.setIsUnsigned(/*Val=*/true);
10149 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10150 DSAStack->getParentOrderedRegionParam()) {
10151 for (auto &RefExpr : VarList) {
10152 assert(RefExpr && "NULL expr in OpenMP shared clause.");
10153 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
10154 // It will be analyzed later.
10155 Vars.push_back(RefExpr);
10159 SourceLocation ELoc = RefExpr->getExprLoc();
10160 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10161 if (DepKind == OMPC_DEPEND_sink) {
10162 if (DepCounter >= TotalDepCount) {
10163 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10167 // OpenMP [2.13.9, Summary]
10168 // depend(dependence-type : vec), where dependence-type is:
10169 // 'sink' and where vec is the iteration vector, which has the form:
10170 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10171 // where n is the value specified by the ordered clause in the loop
10172 // directive, xi denotes the loop iteration variable of the i-th nested
10173 // loop associated with the loop directive, and di is a constant
10174 // non-negative integer.
10175 if (CurContext->isDependentContext()) {
10176 // It will be analyzed later.
10177 Vars.push_back(RefExpr);
10180 SimpleExpr = SimpleExpr->IgnoreImplicit();
10181 OverloadedOperatorKind OOK = OO_None;
10182 SourceLocation OOLoc;
10183 Expr *LHS = SimpleExpr;
10184 Expr *RHS = nullptr;
10185 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10186 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10187 OOLoc = BO->getOperatorLoc();
10188 LHS = BO->getLHS()->IgnoreParenImpCasts();
10189 RHS = BO->getRHS()->IgnoreParenImpCasts();
10190 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10191 OOK = OCE->getOperator();
10192 OOLoc = OCE->getOperatorLoc();
10193 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10194 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10195 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10196 OOK = MCE->getMethodDecl()
10199 .getCXXOverloadedOperator();
10200 OOLoc = MCE->getCallee()->getExprLoc();
10201 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10202 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10204 SourceLocation ELoc;
10205 SourceRange ERange;
10206 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10207 /*AllowArraySection=*/false);
10209 // It will be analyzed later.
10210 Vars.push_back(RefExpr);
10212 ValueDecl *D = Res.first;
10216 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10217 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10221 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10222 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10223 if (RHSRes.isInvalid())
10226 if (!CurContext->isDependentContext() &&
10227 DSAStack->getParentOrderedRegionParam() &&
10228 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10229 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10230 << DSAStack->getParentLoopControlVariable(
10231 DepCounter.getZExtValue());
10234 OpsOffs.push_back({RHS, OOK});
10236 // OpenMP [2.11.1.1, Restrictions, p.3]
10237 // A variable that is part of another variable (such as a field of a
10238 // structure) but is not an array element or an array section cannot
10239 // appear in a depend clause.
10240 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10241 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10242 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10243 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10244 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
10248 .getNonReferenceType()
10249 ->isPointerType() &&
10250 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
10251 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10252 << 0 << RefExpr->getSourceRange();
10256 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10259 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10260 TotalDepCount > VarList.size() &&
10261 DSAStack->getParentOrderedRegionParam()) {
10262 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10263 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10265 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10269 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10270 DepKind, DepLoc, ColonLoc, Vars);
10271 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10272 DSAStack->addDoacrossDependClause(C, OpsOffs);
10276 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10277 SourceLocation LParenLoc,
10278 SourceLocation EndLoc) {
10279 Expr *ValExpr = Device;
10281 // OpenMP [2.9.1, Restrictions]
10282 // The device expression must evaluate to a non-negative integer value.
10283 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10284 /*StrictlyPositive=*/false))
10287 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10290 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10291 DSAStackTy *Stack, CXXRecordDecl *RD) {
10292 if (!RD || RD->isInvalidDecl())
10295 auto QTy = SemaRef.Context.getRecordType(RD);
10296 if (RD->isDynamicClass()) {
10297 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10298 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10302 bool IsCorrect = true;
10303 for (auto *I : DC->decls()) {
10305 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10306 if (MD->isStatic()) {
10307 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10308 SemaRef.Diag(MD->getLocation(),
10309 diag::note_omp_static_member_in_target);
10312 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10313 if (VD->isStaticDataMember()) {
10314 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10315 SemaRef.Diag(VD->getLocation(),
10316 diag::note_omp_static_member_in_target);
10323 for (auto &I : RD->bases()) {
10324 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10325 I.getType()->getAsCXXRecordDecl()))
10331 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10332 DSAStackTy *Stack, QualType QTy) {
10334 if (QTy->isIncompleteType(&ND)) {
10335 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10337 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10338 if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10344 /// \brief Return true if it can be proven that the provided array expression
10345 /// (array section or array subscript) does NOT specify the whole size of the
10346 /// array whose base type is \a BaseQTy.
10347 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10349 QualType BaseQTy) {
10350 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10352 // If this is an array subscript, it refers to the whole size if the size of
10353 // the dimension is constant and equals 1. Also, an array section assumes the
10354 // format of an array subscript if no colon is used.
10355 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10356 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10357 return ATy->getSize().getSExtValue() != 1;
10358 // Size can't be evaluated statically.
10362 assert(OASE && "Expecting array section if not an array subscript.");
10363 auto *LowerBound = OASE->getLowerBound();
10364 auto *Length = OASE->getLength();
10366 // If there is a lower bound that does not evaluates to zero, we are not
10367 // covering the whole dimension.
10369 llvm::APSInt ConstLowerBound;
10370 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10371 return false; // Can't get the integer value as a constant.
10372 if (ConstLowerBound.getSExtValue())
10376 // If we don't have a length we covering the whole dimension.
10380 // If the base is a pointer, we don't have a way to get the size of the
10382 if (BaseQTy->isPointerType())
10385 // We can only check if the length is the same as the size of the dimension
10386 // if we have a constant array.
10387 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10391 llvm::APSInt ConstLength;
10392 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10393 return false; // Can't get the integer value as a constant.
10395 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10398 // Return true if it can be proven that the provided array expression (array
10399 // section or array subscript) does NOT specify a single element of the array
10400 // whose base type is \a BaseQTy.
10401 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10403 QualType BaseQTy) {
10404 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10406 // An array subscript always refer to a single element. Also, an array section
10407 // assumes the format of an array subscript if no colon is used.
10408 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10411 assert(OASE && "Expecting array section if not an array subscript.");
10412 auto *Length = OASE->getLength();
10414 // If we don't have a length we have to check if the array has unitary size
10415 // for this dimension. Also, we should always expect a length if the base type
10418 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10419 return ATy->getSize().getSExtValue() != 1;
10420 // We cannot assume anything.
10424 // Check if the length evaluates to 1.
10425 llvm::APSInt ConstLength;
10426 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10427 return false; // Can't get the integer value as a constant.
10429 return ConstLength.getSExtValue() != 1;
10432 // Return the expression of the base of the mappable expression or null if it
10433 // cannot be determined and do all the necessary checks to see if the expression
10434 // is valid as a standalone mappable expression. In the process, record all the
10435 // components of the expression.
10436 static Expr *CheckMapClauseExpressionBase(
10437 Sema &SemaRef, Expr *E,
10438 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10439 OpenMPClauseKind CKind) {
10440 SourceLocation ELoc = E->getExprLoc();
10441 SourceRange ERange = E->getSourceRange();
10443 // The base of elements of list in a map clause have to be either:
10444 // - a reference to variable or field.
10445 // - a member expression.
10446 // - an array expression.
10448 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10449 // reference to 'r'.
10456 // #pragma omp target map (S.Arr[:12]);
10460 // We want to retrieve the member expression 'this->S';
10462 Expr *RelevantExpr = nullptr;
10464 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10465 // If a list item is an array section, it must specify contiguous storage.
10467 // For this restriction it is sufficient that we make sure only references
10468 // to variables or fields and array expressions, and that no array sections
10469 // exist except in the rightmost expression (unless they cover the whole
10470 // dimension of the array). E.g. these would be invalid:
10472 // r.ArrS[3:5].Arr[6:7]
10476 // but these would be valid:
10477 // r.ArrS[3].Arr[6:7]
10481 bool AllowUnitySizeArraySection = true;
10482 bool AllowWholeSizeArraySection = true;
10484 while (!RelevantExpr) {
10485 E = E->IgnoreParenImpCasts();
10487 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10488 if (!isa<VarDecl>(CurE->getDecl()))
10491 RelevantExpr = CurE;
10493 // If we got a reference to a declaration, we should not expect any array
10494 // section before that.
10495 AllowUnitySizeArraySection = false;
10496 AllowWholeSizeArraySection = false;
10498 // Record the component.
10499 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10500 CurE, CurE->getDecl()));
10504 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10505 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10507 if (isa<CXXThisExpr>(BaseE))
10508 // We found a base expression: this->Val.
10509 RelevantExpr = CurE;
10513 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10514 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10515 << CurE->getSourceRange();
10519 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10521 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10522 // A bit-field cannot appear in a map clause.
10524 if (FD->isBitField()) {
10525 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10526 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
10530 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10531 // If the type of a list item is a reference to a type T then the type
10532 // will be considered to be T for all purposes of this clause.
10533 QualType CurType = BaseE->getType().getNonReferenceType();
10535 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10536 // A list item cannot be a variable that is a member of a structure with
10539 if (auto *RT = CurType->getAs<RecordType>())
10540 if (RT->isUnionType()) {
10541 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10542 << CurE->getSourceRange();
10546 // If we got a member expression, we should not expect any array section
10549 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10550 // If a list item is an element of a structure, only the rightmost symbol
10551 // of the variable reference can be an array section.
10553 AllowUnitySizeArraySection = false;
10554 AllowWholeSizeArraySection = false;
10556 // Record the component.
10557 CurComponents.push_back(
10558 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
10562 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10563 E = CurE->getBase()->IgnoreParenImpCasts();
10565 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10566 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10567 << 0 << CurE->getSourceRange();
10571 // If we got an array subscript that express the whole dimension we
10572 // can have any array expressions before. If it only expressing part of
10573 // the dimension, we can only have unitary-size array expressions.
10574 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10576 AllowWholeSizeArraySection = false;
10578 // Record the component - we don't have any declaration associated.
10579 CurComponents.push_back(
10580 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
10584 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
10585 E = CurE->getBase()->IgnoreParenImpCasts();
10588 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10590 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10591 // If the type of a list item is a reference to a type T then the type
10592 // will be considered to be T for all purposes of this clause.
10593 if (CurType->isReferenceType())
10594 CurType = CurType->getPointeeType();
10596 bool IsPointer = CurType->isAnyPointerType();
10598 if (!IsPointer && !CurType->isArrayType()) {
10599 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10600 << 0 << CurE->getSourceRange();
10605 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10607 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10609 if (AllowWholeSizeArraySection) {
10610 // Any array section is currently allowed. Allowing a whole size array
10611 // section implies allowing a unity array section as well.
10613 // If this array section refers to the whole dimension we can still
10614 // accept other array sections before this one, except if the base is a
10615 // pointer. Otherwise, only unitary sections are accepted.
10616 if (NotWhole || IsPointer)
10617 AllowWholeSizeArraySection = false;
10618 } else if (AllowUnitySizeArraySection && NotUnity) {
10619 // A unity or whole array section is not allowed and that is not
10620 // compatible with the properties of the current array section.
10622 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10623 << CurE->getSourceRange();
10627 // Record the component - we don't have any declaration associated.
10628 CurComponents.push_back(
10629 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
10633 // If nothing else worked, this is not a valid map clause expression.
10635 diag::err_omp_expected_named_var_member_or_array_expression)
10640 return RelevantExpr;
10643 // Return true if expression E associated with value VD has conflicts with other
10644 // map information.
10645 static bool CheckMapConflicts(
10646 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10647 bool CurrentRegionOnly,
10648 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10649 OpenMPClauseKind CKind) {
10651 SourceLocation ELoc = E->getExprLoc();
10652 SourceRange ERange = E->getSourceRange();
10654 // In order to easily check the conflicts we need to match each component of
10655 // the expression under test with the components of the expressions that are
10656 // already in the stack.
10658 assert(!CurComponents.empty() && "Map clause expression with no components!");
10659 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
10660 "Map clause expression with unexpected base!");
10662 // Variables to help detecting enclosing problems in data environment nests.
10663 bool IsEnclosedByDataEnvironmentExpr = false;
10664 const Expr *EnclosingExpr = nullptr;
10666 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10667 VD, CurrentRegionOnly,
10668 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10670 OpenMPClauseKind) -> bool {
10672 assert(!StackComponents.empty() &&
10673 "Map clause expression with no components!");
10674 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
10675 "Map clause expression with unexpected base!");
10677 // The whole expression in the stack.
10678 auto *RE = StackComponents.front().getAssociatedExpression();
10680 // Expressions must start from the same base. Here we detect at which
10681 // point both expressions diverge from each other and see if we can
10682 // detect if the memory referred to both expressions is contiguous and
10684 auto CI = CurComponents.rbegin();
10685 auto CE = CurComponents.rend();
10686 auto SI = StackComponents.rbegin();
10687 auto SE = StackComponents.rend();
10688 for (; CI != CE && SI != SE; ++CI, ++SI) {
10690 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10691 // At most one list item can be an array item derived from a given
10692 // variable in map clauses of the same construct.
10693 if (CurrentRegionOnly &&
10694 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10695 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10696 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10697 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10698 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
10699 diag::err_omp_multiple_array_items_in_map_clause)
10700 << CI->getAssociatedExpression()->getSourceRange();
10701 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10702 diag::note_used_here)
10703 << SI->getAssociatedExpression()->getSourceRange();
10707 // Do both expressions have the same kind?
10708 if (CI->getAssociatedExpression()->getStmtClass() !=
10709 SI->getAssociatedExpression()->getStmtClass())
10712 // Are we dealing with different variables/fields?
10713 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
10716 // Check if the extra components of the expressions in the enclosing
10717 // data environment are redundant for the current base declaration.
10718 // If they are, the maps completely overlap, which is legal.
10719 for (; SI != SE; ++SI) {
10722 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
10723 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
10724 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
10725 SI->getAssociatedExpression())) {
10726 auto *E = OASE->getBase()->IgnoreParenImpCasts();
10728 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10730 if (Type.isNull() || Type->isAnyPointerType() ||
10731 CheckArrayExpressionDoesNotReferToWholeSize(
10732 SemaRef, SI->getAssociatedExpression(), Type))
10736 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10737 // List items of map clauses in the same construct must not share
10738 // original storage.
10740 // If the expressions are exactly the same or one is a subset of the
10741 // other, it means they are sharing storage.
10742 if (CI == CE && SI == SE) {
10743 if (CurrentRegionOnly) {
10744 if (CKind == OMPC_map)
10745 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10747 assert(CKind == OMPC_to || CKind == OMPC_from);
10748 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10751 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10752 << RE->getSourceRange();
10755 // If we find the same expression in the enclosing data environment,
10757 IsEnclosedByDataEnvironmentExpr = true;
10762 QualType DerivedType =
10763 std::prev(CI)->getAssociatedDeclaration()->getType();
10764 SourceLocation DerivedLoc =
10765 std::prev(CI)->getAssociatedExpression()->getExprLoc();
10767 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10768 // If the type of a list item is a reference to a type T then the type
10769 // will be considered to be T for all purposes of this clause.
10770 DerivedType = DerivedType.getNonReferenceType();
10772 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10773 // A variable for which the type is pointer and an array section
10774 // derived from that variable must not appear as list items of map
10775 // clauses of the same construct.
10777 // Also, cover one of the cases in:
10778 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10779 // If any part of the original storage of a list item has corresponding
10780 // storage in the device data environment, all of the original storage
10781 // must have corresponding storage in the device data environment.
10783 if (DerivedType->isAnyPointerType()) {
10784 if (CI == CE || SI == SE) {
10787 diag::err_omp_pointer_mapped_along_with_derived_section)
10790 assert(CI != CE && SI != SE);
10791 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10794 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10795 << RE->getSourceRange();
10799 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10800 // List items of map clauses in the same construct must not share
10801 // original storage.
10803 // An expression is a subset of the other.
10804 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10805 if (CKind == OMPC_map)
10806 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10808 assert(CKind == OMPC_to || CKind == OMPC_from);
10809 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10812 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10813 << RE->getSourceRange();
10817 // The current expression uses the same base as other expression in the
10818 // data environment but does not contain it completely.
10819 if (!CurrentRegionOnly && SI != SE)
10820 EnclosingExpr = RE;
10822 // The current expression is a subset of the expression in the data
10824 IsEnclosedByDataEnvironmentExpr |=
10825 (!CurrentRegionOnly && CI != CE && SI == SE);
10830 if (CurrentRegionOnly)
10833 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10834 // If any part of the original storage of a list item has corresponding
10835 // storage in the device data environment, all of the original storage must
10836 // have corresponding storage in the device data environment.
10837 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10838 // If a list item is an element of a structure, and a different element of
10839 // the structure has a corresponding list item in the device data environment
10840 // prior to a task encountering the construct associated with the map clause,
10841 // then the list item must also have a corresponding list item in the device
10842 // data environment prior to the task encountering the construct.
10844 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10846 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10848 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10849 << EnclosingExpr->getSourceRange();
10857 // Utility struct that gathers all the related lists associated with a mappable
10859 struct MappableVarListInfo final {
10860 // The list of expressions.
10861 ArrayRef<Expr *> VarList;
10862 // The list of processed expressions.
10863 SmallVector<Expr *, 16> ProcessedVarList;
10864 // The mappble components for each expression.
10865 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10866 // The base declaration of the variable.
10867 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10869 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10870 // We have a list of components and base declarations for each entry in the
10872 VarComponents.reserve(VarList.size());
10873 VarBaseDeclarations.reserve(VarList.size());
10878 // Check the validity of the provided variable list for the provided clause kind
10879 // \a CKind. In the check process the valid expressions, and mappable expression
10880 // components and variables are extracted and used to fill \a Vars,
10881 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10882 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10884 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10885 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10886 SourceLocation StartLoc,
10887 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10888 bool IsMapTypeImplicit = false) {
10889 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10890 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
10891 "Unexpected clause kind with mappable expressions!");
10893 // Keep track of the mappable components and base declarations in this clause.
10894 // Each entry in the list is going to have a list of components associated. We
10895 // record each set of the components so that we can build the clause later on.
10896 // In the end we should have the same amount of declarations and component
10899 for (auto &RE : MVLI.VarList) {
10900 assert(RE && "Null expr in omp to/from/map clause");
10901 SourceLocation ELoc = RE->getExprLoc();
10903 auto *VE = RE->IgnoreParenLValueCasts();
10905 if (VE->isValueDependent() || VE->isTypeDependent() ||
10906 VE->isInstantiationDependent() ||
10907 VE->containsUnexpandedParameterPack()) {
10908 // We can only analyze this information once the missing information is
10910 MVLI.ProcessedVarList.push_back(RE);
10914 auto *SimpleExpr = RE->IgnoreParenCasts();
10916 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10918 diag::err_omp_expected_named_var_member_or_array_expression)
10919 << RE->getSourceRange();
10923 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10924 ValueDecl *CurDeclaration = nullptr;
10926 // Obtain the array or member expression bases if required. Also, fill the
10927 // components array with all the components identified in the process.
10929 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
10933 assert(!CurComponents.empty() &&
10934 "Invalid mappable expression information.");
10936 // For the following checks, we rely on the base declaration which is
10937 // expected to be associated with the last component. The declaration is
10938 // expected to be a variable or a field (if 'this' is being mapped).
10939 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10940 assert(CurDeclaration && "Null decl on map clause.");
10942 CurDeclaration->isCanonicalDecl() &&
10943 "Expecting components to have associated only canonical declarations.");
10945 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10946 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
10948 assert((VD || FD) && "Only variables or fields are expected here!");
10951 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10952 // threadprivate variables cannot appear in a map clause.
10953 // OpenMP 4.5 [2.10.5, target update Construct]
10954 // threadprivate variables cannot appear in a from clause.
10955 if (VD && DSAS->isThreadPrivate(VD)) {
10956 auto DVar = DSAS->getTopDSA(VD, false);
10957 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10958 << getOpenMPClauseName(CKind);
10959 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
10963 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10964 // A list item cannot appear in both a map clause and a data-sharing
10965 // attribute clause on the same construct.
10967 // Check conflicts with other map clause expressions. We check the conflicts
10968 // with the current construct separately from the enclosing data
10969 // environment, because the restrictions are different. We only have to
10970 // check conflicts across regions for the map clauses.
10971 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10972 /*CurrentRegionOnly=*/true, CurComponents, CKind))
10974 if (CKind == OMPC_map &&
10975 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10976 /*CurrentRegionOnly=*/false, CurComponents, CKind))
10979 // OpenMP 4.5 [2.10.5, target update Construct]
10980 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10981 // If the type of a list item is a reference to a type T then the type will
10982 // be considered to be T for all purposes of this clause.
10983 QualType Type = CurDeclaration->getType().getNonReferenceType();
10985 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10986 // A list item in a to or from clause must have a mappable type.
10987 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10988 // A list item must have a mappable type.
10989 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10993 if (CKind == OMPC_map) {
10994 // target enter data
10995 // OpenMP [2.10.2, Restrictions, p. 99]
10996 // A map-type must be specified in all map clauses and must be either
10998 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10999 if (DKind == OMPD_target_enter_data &&
11000 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11001 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11002 << (IsMapTypeImplicit ? 1 : 0)
11003 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11004 << getOpenMPDirectiveName(DKind);
11008 // target exit_data
11009 // OpenMP [2.10.3, Restrictions, p. 102]
11010 // A map-type must be specified in all map clauses and must be either
11011 // from, release, or delete.
11012 if (DKind == OMPD_target_exit_data &&
11013 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11014 MapType == OMPC_MAP_delete)) {
11015 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11016 << (IsMapTypeImplicit ? 1 : 0)
11017 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11018 << getOpenMPDirectiveName(DKind);
11022 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11023 // A list item cannot appear in both a map clause and a data-sharing
11024 // attribute clause on the same construct
11025 if ((DKind == OMPD_target || DKind == OMPD_target_teams ||
11026 DKind == OMPD_target_teams_distribute ||
11027 DKind == OMPD_target_teams_distribute_parallel_for ||
11028 DKind == OMPD_target_teams_distribute_parallel_for_simd ||
11029 DKind == OMPD_target_teams_distribute_simd) && VD) {
11030 auto DVar = DSAS->getTopDSA(VD, false);
11031 if (isOpenMPPrivate(DVar.CKind)) {
11032 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11033 << getOpenMPClauseName(DVar.CKind)
11034 << getOpenMPClauseName(OMPC_map)
11035 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11036 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11042 // Save the current expression.
11043 MVLI.ProcessedVarList.push_back(RE);
11045 // Store the components in the stack so that they can be used to check
11046 // against other clauses later on.
11047 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
11048 /*WhereFoundClauseKind=*/OMPC_map);
11050 // Save the components and declaration to create the clause. For purposes of
11051 // the clause creation, any component list that has has base 'this' uses
11052 // null as base declaration.
11053 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11054 MVLI.VarComponents.back().append(CurComponents.begin(),
11055 CurComponents.end());
11056 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11062 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11063 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11064 SourceLocation MapLoc, SourceLocation ColonLoc,
11065 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11066 SourceLocation LParenLoc, SourceLocation EndLoc) {
11067 MappableVarListInfo MVLI(VarList);
11068 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11069 MapType, IsMapTypeImplicit);
11071 // We need to produce a map clause even if we don't have variables so that
11072 // other diagnostics related with non-existing map clauses are accurate.
11073 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11074 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11075 MVLI.VarComponents, MapTypeModifier, MapType,
11076 IsMapTypeImplicit, MapLoc);
11079 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11080 TypeResult ParsedType) {
11081 assert(ParsedType.isUsable());
11083 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11084 if (ReductionType.isNull())
11087 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11088 // A type name in a declare reduction directive cannot be a function type, an
11089 // array type, a reference type, or a type qualified with const, volatile or
11091 if (ReductionType.hasQualifiers()) {
11092 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11096 if (ReductionType->isFunctionType()) {
11097 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11100 if (ReductionType->isReferenceType()) {
11101 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11104 if (ReductionType->isArrayType()) {
11105 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11108 return ReductionType;
11111 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11112 Scope *S, DeclContext *DC, DeclarationName Name,
11113 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11114 AccessSpecifier AS, Decl *PrevDeclInScope) {
11115 SmallVector<Decl *, 8> Decls;
11116 Decls.reserve(ReductionTypes.size());
11118 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11120 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11121 // A reduction-identifier may not be re-declared in the current scope for the
11122 // same type or for a type that is compatible according to the base language
11124 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11125 OMPDeclareReductionDecl *PrevDRD = nullptr;
11126 bool InCompoundScope = true;
11127 if (S != nullptr) {
11128 // Find previous declaration with the same name not referenced in other
11130 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11132 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11133 LookupName(Lookup, S);
11134 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11135 /*AllowInlineNamespace=*/false);
11136 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11137 auto Filter = Lookup.makeFilter();
11138 while (Filter.hasNext()) {
11139 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11140 if (InCompoundScope) {
11141 auto I = UsedAsPrevious.find(PrevDecl);
11142 if (I == UsedAsPrevious.end())
11143 UsedAsPrevious[PrevDecl] = false;
11144 if (auto *D = PrevDecl->getPrevDeclInScope())
11145 UsedAsPrevious[D] = true;
11147 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11148 PrevDecl->getLocation();
11151 if (InCompoundScope) {
11152 for (auto &PrevData : UsedAsPrevious) {
11153 if (!PrevData.second) {
11154 PrevDRD = PrevData.first;
11159 } else if (PrevDeclInScope != nullptr) {
11160 auto *PrevDRDInScope = PrevDRD =
11161 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11163 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11164 PrevDRDInScope->getLocation();
11165 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11166 } while (PrevDRDInScope != nullptr);
11168 for (auto &TyData : ReductionTypes) {
11169 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11170 bool Invalid = false;
11171 if (I != PreviousRedeclTypes.end()) {
11172 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11174 Diag(I->second, diag::note_previous_definition);
11177 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11178 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11179 Name, TyData.first, PrevDRD);
11181 DRD->setAccess(AS);
11182 Decls.push_back(DRD);
11184 DRD->setInvalidDecl();
11189 return DeclGroupPtrTy::make(
11190 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11193 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11194 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11196 // Enter new function scope.
11197 PushFunctionScope();
11198 getCurFunction()->setHasBranchProtectedScope();
11199 getCurFunction()->setHasOMPDeclareReductionCombiner();
11202 PushDeclContext(S, DRD);
11206 PushExpressionEvaluationContext(
11207 ExpressionEvaluationContext::PotentiallyEvaluated);
11209 QualType ReductionType = DRD->getType();
11210 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11211 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11212 // uses semantics of argument handles by value, but it should be passed by
11213 // reference. C lang does not support references, so pass all parameters as
11215 // Create 'T omp_in;' variable.
11217 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
11218 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11219 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11220 // uses semantics of argument handles by value, but it should be passed by
11221 // reference. C lang does not support references, so pass all parameters as
11223 // Create 'T omp_out;' variable.
11225 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11226 if (S != nullptr) {
11227 PushOnScopeChains(OmpInParm, S);
11228 PushOnScopeChains(OmpOutParm, S);
11230 DRD->addDecl(OmpInParm);
11231 DRD->addDecl(OmpOutParm);
11235 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11236 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11237 DiscardCleanupsInEvaluationContext();
11238 PopExpressionEvaluationContext();
11241 PopFunctionScopeInfo();
11243 if (Combiner != nullptr)
11244 DRD->setCombiner(Combiner);
11246 DRD->setInvalidDecl();
11249 void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11250 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11252 // Enter new function scope.
11253 PushFunctionScope();
11254 getCurFunction()->setHasBranchProtectedScope();
11257 PushDeclContext(S, DRD);
11261 PushExpressionEvaluationContext(
11262 ExpressionEvaluationContext::PotentiallyEvaluated);
11264 QualType ReductionType = DRD->getType();
11265 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11266 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11267 // uses semantics of argument handles by value, but it should be passed by
11268 // reference. C lang does not support references, so pass all parameters as
11270 // Create 'T omp_priv;' variable.
11271 auto *OmpPrivParm =
11272 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
11273 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11274 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11275 // uses semantics of argument handles by value, but it should be passed by
11276 // reference. C lang does not support references, so pass all parameters as
11278 // Create 'T omp_orig;' variable.
11279 auto *OmpOrigParm =
11280 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
11281 if (S != nullptr) {
11282 PushOnScopeChains(OmpPrivParm, S);
11283 PushOnScopeChains(OmpOrigParm, S);
11285 DRD->addDecl(OmpPrivParm);
11286 DRD->addDecl(OmpOrigParm);
11290 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11291 Expr *Initializer) {
11292 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11293 DiscardCleanupsInEvaluationContext();
11294 PopExpressionEvaluationContext();
11297 PopFunctionScopeInfo();
11299 if (Initializer != nullptr)
11300 DRD->setInitializer(Initializer);
11302 DRD->setInvalidDecl();
11305 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11306 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11307 for (auto *D : DeclReductions.get()) {
11309 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11311 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11313 D->setInvalidDecl();
11315 return DeclReductions;
11318 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11319 SourceLocation StartLoc,
11320 SourceLocation LParenLoc,
11321 SourceLocation EndLoc) {
11322 Expr *ValExpr = NumTeams;
11323 Stmt *HelperValStmt = nullptr;
11324 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
11326 // OpenMP [teams Constrcut, Restrictions]
11327 // The num_teams expression must evaluate to a positive integer value.
11328 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11329 /*StrictlyPositive=*/true))
11332 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
11333 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
11334 if (CaptureRegion != OMPD_unknown) {
11335 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11336 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11337 HelperValStmt = buildPreInits(Context, Captures);
11340 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
11341 StartLoc, LParenLoc, EndLoc);
11344 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11345 SourceLocation StartLoc,
11346 SourceLocation LParenLoc,
11347 SourceLocation EndLoc) {
11348 Expr *ValExpr = ThreadLimit;
11349 Stmt *HelperValStmt = nullptr;
11350 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
11352 // OpenMP [teams Constrcut, Restrictions]
11353 // The thread_limit expression must evaluate to a positive integer value.
11354 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11355 /*StrictlyPositive=*/true))
11358 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
11359 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
11360 if (CaptureRegion != OMPD_unknown) {
11361 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11362 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11363 HelperValStmt = buildPreInits(Context, Captures);
11366 return new (Context) OMPThreadLimitClause(
11367 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
11370 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11371 SourceLocation StartLoc,
11372 SourceLocation LParenLoc,
11373 SourceLocation EndLoc) {
11374 Expr *ValExpr = Priority;
11376 // OpenMP [2.9.1, task Constrcut]
11377 // The priority-value is a non-negative numerical scalar expression.
11378 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11379 /*StrictlyPositive=*/false))
11382 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11385 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11386 SourceLocation StartLoc,
11387 SourceLocation LParenLoc,
11388 SourceLocation EndLoc) {
11389 Expr *ValExpr = Grainsize;
11391 // OpenMP [2.9.2, taskloop Constrcut]
11392 // The parameter of the grainsize clause must be a positive integer
11394 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11395 /*StrictlyPositive=*/true))
11398 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11401 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11402 SourceLocation StartLoc,
11403 SourceLocation LParenLoc,
11404 SourceLocation EndLoc) {
11405 Expr *ValExpr = NumTasks;
11407 // OpenMP [2.9.2, taskloop Constrcut]
11408 // The parameter of the num_tasks clause must be a positive integer
11410 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11411 /*StrictlyPositive=*/true))
11414 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11417 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11418 SourceLocation LParenLoc,
11419 SourceLocation EndLoc) {
11420 // OpenMP [2.13.2, critical construct, Description]
11421 // ... where hint-expression is an integer constant expression that evaluates
11422 // to a valid lock hint.
11423 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11424 if (HintExpr.isInvalid())
11426 return new (Context)
11427 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11430 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11431 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11432 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11433 SourceLocation EndLoc) {
11434 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11435 std::string Values;
11437 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11439 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11440 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11443 Expr *ValExpr = ChunkSize;
11444 Stmt *HelperValStmt = nullptr;
11446 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11447 !ChunkSize->isInstantiationDependent() &&
11448 !ChunkSize->containsUnexpandedParameterPack()) {
11449 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11451 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11452 if (Val.isInvalid())
11455 ValExpr = Val.get();
11457 // OpenMP [2.7.1, Restrictions]
11458 // chunk_size must be a loop invariant integer expression with a positive
11460 llvm::APSInt Result;
11461 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11462 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11463 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11464 << "dist_schedule" << ChunkSize->getSourceRange();
11467 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11468 !CurContext->isDependentContext()) {
11469 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11470 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11471 HelperValStmt = buildPreInits(Context, Captures);
11476 return new (Context)
11477 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
11478 Kind, ValExpr, HelperValStmt);
11481 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11482 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11483 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11484 SourceLocation KindLoc, SourceLocation EndLoc) {
11485 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11486 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
11488 SourceLocation Loc;
11490 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11491 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11492 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11495 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11496 OMPC_DEFAULTMAP_scalar);
11500 Diag(Loc, diag::err_omp_unexpected_clause_value)
11501 << Value << getOpenMPClauseName(OMPC_defaultmap);
11505 return new (Context)
11506 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11509 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11510 DeclContext *CurLexicalContext = getCurLexicalContext();
11511 if (!CurLexicalContext->isFileContext() &&
11512 !CurLexicalContext->isExternCContext() &&
11513 !CurLexicalContext->isExternCXXContext()) {
11514 Diag(Loc, diag::err_omp_region_not_file_context);
11517 if (IsInOpenMPDeclareTargetContext) {
11518 Diag(Loc, diag::err_omp_enclosed_declare_target);
11522 IsInOpenMPDeclareTargetContext = true;
11526 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11527 assert(IsInOpenMPDeclareTargetContext &&
11528 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11530 IsInOpenMPDeclareTargetContext = false;
11533 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
11534 CXXScopeSpec &ScopeSpec,
11535 const DeclarationNameInfo &Id,
11536 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11537 NamedDeclSetType &SameDirectiveDecls) {
11538 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11539 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11541 if (Lookup.isAmbiguous())
11543 Lookup.suppressDiagnostics();
11545 if (!Lookup.isSingleResult()) {
11546 if (TypoCorrection Corrected =
11547 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11548 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11549 CTK_ErrorRecovery)) {
11550 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11552 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11556 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11560 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11561 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11562 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11563 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11565 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11566 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11568 if (ASTMutationListener *ML = Context.getASTMutationListener())
11569 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11570 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11571 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11572 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11576 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11579 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11580 Sema &SemaRef, Decl *D) {
11583 Decl *LD = nullptr;
11584 if (isa<TagDecl>(D)) {
11585 LD = cast<TagDecl>(D)->getDefinition();
11586 } else if (isa<VarDecl>(D)) {
11587 LD = cast<VarDecl>(D)->getDefinition();
11589 // If this is an implicit variable that is legal and we do not need to do
11591 if (cast<VarDecl>(D)->isImplicit()) {
11592 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11593 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11595 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11596 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11600 } else if (isa<FunctionDecl>(D)) {
11601 const FunctionDecl *FD = nullptr;
11602 if (cast<FunctionDecl>(D)->hasBody(FD))
11603 LD = const_cast<FunctionDecl *>(FD);
11605 // If the definition is associated with the current declaration in the
11606 // target region (it can be e.g. a lambda) that is legal and we do not need
11607 // to do anything else.
11609 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11610 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11612 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11613 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11619 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11620 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11621 // Outlined declaration is not declared target.
11622 if (LD->isOutOfLine()) {
11623 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11624 SemaRef.Diag(SL, diag::note_used_here) << SR;
11626 DeclContext *DC = LD->getDeclContext();
11628 if (isa<FunctionDecl>(DC) &&
11629 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11631 DC = DC->getParent();
11636 // Is not declared in target context.
11637 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11638 SemaRef.Diag(SL, diag::note_used_here) << SR;
11640 // Mark decl as declared target to prevent further diagnostic.
11641 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11642 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11644 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11645 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11649 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11650 Sema &SemaRef, DSAStackTy *Stack,
11652 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11654 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11659 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11660 if (!D || D->isInvalidDecl())
11662 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11663 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11664 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11665 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11666 if (DSAStack->isThreadPrivate(VD)) {
11667 Diag(SL, diag::err_omp_threadprivate_in_target);
11668 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11672 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11673 // Problem if any with var declared with incomplete type will be reported
11674 // as normal, so no need to check it here.
11675 if ((E || !VD->getType()->isIncompleteType()) &&
11676 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11677 // Mark decl as declared target to prevent further diagnostic.
11678 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
11679 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11680 Context, OMPDeclareTargetDeclAttr::MT_To);
11682 if (ASTMutationListener *ML = Context.getASTMutationListener())
11683 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
11689 // Checking declaration inside declare target region.
11690 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11691 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
11692 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11693 Context, OMPDeclareTargetDeclAttr::MT_To);
11695 if (ASTMutationListener *ML = Context.getASTMutationListener())
11696 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11700 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11703 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11704 SourceLocation StartLoc,
11705 SourceLocation LParenLoc,
11706 SourceLocation EndLoc) {
11707 MappableVarListInfo MVLI(VarList);
11708 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11709 if (MVLI.ProcessedVarList.empty())
11712 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11713 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11714 MVLI.VarComponents);
11717 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11718 SourceLocation StartLoc,
11719 SourceLocation LParenLoc,
11720 SourceLocation EndLoc) {
11721 MappableVarListInfo MVLI(VarList);
11722 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11723 if (MVLI.ProcessedVarList.empty())
11726 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11727 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11728 MVLI.VarComponents);
11731 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11732 SourceLocation StartLoc,
11733 SourceLocation LParenLoc,
11734 SourceLocation EndLoc) {
11735 MappableVarListInfo MVLI(VarList);
11736 SmallVector<Expr *, 8> PrivateCopies;
11737 SmallVector<Expr *, 8> Inits;
11739 for (auto &RefExpr : VarList) {
11740 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11741 SourceLocation ELoc;
11742 SourceRange ERange;
11743 Expr *SimpleRefExpr = RefExpr;
11744 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11746 // It will be analyzed later.
11747 MVLI.ProcessedVarList.push_back(RefExpr);
11748 PrivateCopies.push_back(nullptr);
11749 Inits.push_back(nullptr);
11751 ValueDecl *D = Res.first;
11755 QualType Type = D->getType();
11756 Type = Type.getNonReferenceType().getUnqualifiedType();
11758 auto *VD = dyn_cast<VarDecl>(D);
11760 // Item should be a pointer or reference to pointer.
11761 if (!Type->isPointerType()) {
11762 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11763 << 0 << RefExpr->getSourceRange();
11767 // Build the private variable and the expression that refers to it.
11768 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
11769 D->hasAttrs() ? &D->getAttrs() : nullptr);
11770 if (VDPrivate->isInvalidDecl())
11773 CurContext->addDecl(VDPrivate);
11774 auto VDPrivateRefExpr = buildDeclRefExpr(
11775 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11777 // Add temporary variable to initialize the private copy of the pointer.
11779 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
11780 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11781 RefExpr->getExprLoc());
11782 AddInitializerToDecl(VDPrivate,
11783 DefaultLvalueConversion(VDInitRefExpr).get(),
11784 /*DirectInit=*/false);
11786 // If required, build a capture to implement the privatization initialized
11787 // with the current list item value.
11788 DeclRefExpr *Ref = nullptr;
11790 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11791 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
11792 PrivateCopies.push_back(VDPrivateRefExpr);
11793 Inits.push_back(VDInitRefExpr);
11795 // We need to add a data sharing attribute for this variable to make sure it
11796 // is correctly captured. A variable that shows up in a use_device_ptr has
11797 // similar properties of a first private variable.
11798 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
11800 // Create a mappable component for the list item. List items in this clause
11801 // only need a component.
11802 MVLI.VarBaseDeclarations.push_back(D);
11803 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11804 MVLI.VarComponents.back().push_back(
11805 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
11808 if (MVLI.ProcessedVarList.empty())
11811 return OMPUseDevicePtrClause::Create(
11812 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
11813 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
11816 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11817 SourceLocation StartLoc,
11818 SourceLocation LParenLoc,
11819 SourceLocation EndLoc) {
11820 MappableVarListInfo MVLI(VarList);
11821 for (auto &RefExpr : VarList) {
11822 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
11823 SourceLocation ELoc;
11824 SourceRange ERange;
11825 Expr *SimpleRefExpr = RefExpr;
11826 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11828 // It will be analyzed later.
11829 MVLI.ProcessedVarList.push_back(RefExpr);
11831 ValueDecl *D = Res.first;
11835 QualType Type = D->getType();
11836 // item should be a pointer or array or reference to pointer or array
11837 if (!Type.getNonReferenceType()->isPointerType() &&
11838 !Type.getNonReferenceType()->isArrayType()) {
11839 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11840 << 0 << RefExpr->getSourceRange();
11844 // Check if the declaration in the clause does not show up in any data
11845 // sharing attribute.
11846 auto DVar = DSAStack->getTopDSA(D, false);
11847 if (isOpenMPPrivate(DVar.CKind)) {
11848 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11849 << getOpenMPClauseName(DVar.CKind)
11850 << getOpenMPClauseName(OMPC_is_device_ptr)
11851 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11852 ReportOriginalDSA(*this, DSAStack, D, DVar);
11856 Expr *ConflictExpr;
11857 if (DSAStack->checkMappableExprComponentListsForDecl(
11858 D, /*CurrentRegionOnly=*/true,
11860 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
11861 OpenMPClauseKind) -> bool {
11862 ConflictExpr = R.front().getAssociatedExpression();
11865 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
11866 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
11867 << ConflictExpr->getSourceRange();
11871 // Store the components in the stack so that they can be used to check
11872 // against other clauses later on.
11873 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
11874 DSAStack->addMappableExpressionComponents(
11875 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
11877 // Record the expression we've just processed.
11878 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
11880 // Create a mappable component for the list item. List items in this clause
11881 // only need a component. We use a null declaration to signal fields in
11883 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
11884 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
11885 "Unexpected device pointer expression!");
11886 MVLI.VarBaseDeclarations.push_back(
11887 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
11888 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11889 MVLI.VarComponents.back().push_back(MC);
11892 if (MVLI.ProcessedVarList.empty())
11895 return OMPIsDevicePtrClause::Create(
11896 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
11897 MVLI.VarBaseDeclarations, MVLI.VarComponents);