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 "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclOpenMP.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/AST/StmtOpenMP.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/Basic/OpenMPKinds.h"
24 #include "clang/Basic/TargetInfo.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "clang/Sema/Initialization.h"
27 #include "clang/Sema/Lookup.h"
28 #include "clang/Sema/Scope.h"
29 #include "clang/Sema/ScopeInfo.h"
30 #include "clang/Sema/SemaInternal.h"
31 using namespace clang;
33 //===----------------------------------------------------------------------===//
34 // Stack of data-sharing attributes for variables
35 //===----------------------------------------------------------------------===//
38 /// \brief Default data sharing attributes, which can be applied to directive.
39 enum DefaultDataSharingAttributes {
40 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
41 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
42 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
45 template <class T> struct MatchesAny {
46 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
47 bool operator()(T Kind) {
48 for (auto KindEl : Arr)
57 struct MatchesAlways {
59 template <class T> bool operator()(T) { return true; }
62 typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
63 typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
65 /// \brief Stack for tracking declarations used in OpenMP directives and
66 /// clauses and their data-sharing attributes.
70 OpenMPDirectiveKind DKind;
71 OpenMPClauseKind CKind;
73 SourceLocation ImplicitDSALoc;
75 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
81 OpenMPClauseKind Attributes;
84 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
85 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
86 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
89 DeclSAMapTy SharingMap;
90 AlignedMapTy AlignedMap;
91 LoopControlVariablesSetTy LCVSet;
92 DefaultDataSharingAttributes DefaultAttr;
93 SourceLocation DefaultAttrLoc;
94 OpenMPDirectiveKind Directive;
95 DeclarationNameInfo DirectiveName;
97 SourceLocation ConstructLoc;
100 unsigned CollapseNumber;
101 SourceLocation InnerTeamsRegionLoc;
102 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
103 Scope *CurScope, SourceLocation Loc)
104 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
105 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
106 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
107 CollapseNumber(1), InnerTeamsRegionLoc() {}
109 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
110 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
111 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
112 CollapseNumber(1), InnerTeamsRegionLoc() {}
115 typedef SmallVector<SharingMapTy, 64> StackTy;
117 /// \brief Stack of used declaration and their data-sharing attributes.
119 /// \brief true, if check for DSA must be from parent directive, false, if
120 /// from current directive.
121 OpenMPClauseKind ClauseKindMode;
124 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
126 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
128 /// \brief Checks if the variable is a local for OpenMP region.
129 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
132 explicit DSAStackTy(Sema &S)
133 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S) {}
135 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
136 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
138 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
139 Scope *CurScope, SourceLocation Loc) {
140 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
141 Stack.back().DefaultAttrLoc = Loc;
145 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
149 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
150 /// add it and return NULL; otherwise return previous occurrence's expression
152 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
154 /// \brief Register specified variable as loop control variable.
155 void addLoopControlVariable(VarDecl *D);
156 /// \brief Check if the specified variable is a loop control variable for
158 bool isLoopControlVariable(VarDecl *D);
160 /// \brief Adds explicit data sharing attribute to the specified declaration.
161 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
163 /// \brief Returns data sharing attributes from top of the stack for the
164 /// specified declaration.
165 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
166 /// \brief Returns data-sharing attributes for the specified declaration.
167 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
168 /// \brief Checks if the specified variables has data-sharing attributes which
169 /// match specified \a CPred predicate in any directive which matches \a DPred
171 template <class ClausesPredicate, class DirectivesPredicate>
172 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
173 DirectivesPredicate DPred, bool FromParent);
174 /// \brief Checks if the specified variables has data-sharing attributes which
175 /// match specified \a CPred predicate in any innermost directive which
176 /// matches \a DPred predicate.
177 template <class ClausesPredicate, class DirectivesPredicate>
178 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
179 DirectivesPredicate DPred,
181 /// \brief Checks if the specified variables has explicit data-sharing
182 /// attributes which match specified \a CPred predicate at the specified
184 bool hasExplicitDSA(VarDecl *D,
185 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
187 /// \brief Finds a directive which matches specified \a DPred predicate.
188 template <class NamedDirectivesPredicate>
189 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
191 /// \brief Returns currently analyzed directive.
192 OpenMPDirectiveKind getCurrentDirective() const {
193 return Stack.back().Directive;
195 /// \brief Returns parent directive.
196 OpenMPDirectiveKind getParentDirective() const {
197 if (Stack.size() > 2)
198 return Stack[Stack.size() - 2].Directive;
202 /// \brief Set default data sharing attribute to none.
203 void setDefaultDSANone(SourceLocation Loc) {
204 Stack.back().DefaultAttr = DSA_none;
205 Stack.back().DefaultAttrLoc = Loc;
207 /// \brief Set default data sharing attribute to shared.
208 void setDefaultDSAShared(SourceLocation Loc) {
209 Stack.back().DefaultAttr = DSA_shared;
210 Stack.back().DefaultAttrLoc = Loc;
213 DefaultDataSharingAttributes getDefaultDSA() const {
214 return Stack.back().DefaultAttr;
216 SourceLocation getDefaultDSALocation() const {
217 return Stack.back().DefaultAttrLoc;
220 /// \brief Checks if the specified variable is a threadprivate.
221 bool isThreadPrivate(VarDecl *D) {
222 DSAVarData DVar = getTopDSA(D, false);
223 return isOpenMPThreadPrivate(DVar.CKind);
226 /// \brief Marks current region as ordered (it has an 'ordered' clause).
227 void setOrderedRegion(bool IsOrdered = true) {
228 Stack.back().OrderedRegion = IsOrdered;
230 /// \brief Returns true, if parent region is ordered (has associated
231 /// 'ordered' clause), false - otherwise.
232 bool isParentOrderedRegion() const {
233 if (Stack.size() > 2)
234 return Stack[Stack.size() - 2].OrderedRegion;
237 /// \brief Marks current region as nowait (it has a 'nowait' clause).
238 void setNowaitRegion(bool IsNowait = true) {
239 Stack.back().NowaitRegion = IsNowait;
241 /// \brief Returns true, if parent region is nowait (has associated
242 /// 'nowait' clause), false - otherwise.
243 bool isParentNowaitRegion() const {
244 if (Stack.size() > 2)
245 return Stack[Stack.size() - 2].NowaitRegion;
249 /// \brief Set collapse value for the region.
250 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
251 /// \brief Return collapse value for region.
252 unsigned getCollapseNumber() const {
253 return Stack.back().CollapseNumber;
256 /// \brief Marks current target region as one with closely nested teams
258 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
259 if (Stack.size() > 2)
260 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
262 /// \brief Returns true, if current region has closely nested teams region.
263 bool hasInnerTeamsRegion() const {
264 return getInnerTeamsRegionLoc().isValid();
266 /// \brief Returns location of the nested teams region (if any).
267 SourceLocation getInnerTeamsRegionLoc() const {
268 if (Stack.size() > 1)
269 return Stack.back().InnerTeamsRegionLoc;
270 return SourceLocation();
273 Scope *getCurScope() const { return Stack.back().CurScope; }
274 Scope *getCurScope() { return Stack.back().CurScope; }
275 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
277 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
278 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
279 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
283 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
285 D = D->getCanonicalDecl();
287 if (Iter == std::prev(Stack.rend())) {
288 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
289 // in a region but not in construct]
290 // File-scope or namespace-scope variables referenced in called routines
291 // in the region are shared unless they appear in a threadprivate
293 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
294 DVar.CKind = OMPC_shared;
296 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
297 // in a region but not in construct]
298 // Variables with static storage duration that are declared in called
299 // routines in the region are shared.
300 if (D->hasGlobalStorage())
301 DVar.CKind = OMPC_shared;
306 DVar.DKind = Iter->Directive;
307 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
308 // in a Construct, C/C++, predetermined, p.1]
309 // Variables with automatic storage duration that are declared in a scope
310 // inside the construct are private.
311 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
312 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
313 DVar.CKind = OMPC_private;
317 // Explicitly specified attributes and local variables with predetermined
319 if (Iter->SharingMap.count(D)) {
320 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
321 DVar.CKind = Iter->SharingMap[D].Attributes;
322 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
326 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
327 // in a Construct, C/C++, implicitly determined, p.1]
328 // In a parallel or task construct, the data-sharing attributes of these
329 // variables are determined by the default clause, if present.
330 switch (Iter->DefaultAttr) {
332 DVar.CKind = OMPC_shared;
333 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
337 case DSA_unspecified:
338 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
339 // in a Construct, implicitly determined, p.2]
340 // In a parallel construct, if no default clause is present, these
341 // variables are shared.
342 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
343 if (isOpenMPParallelDirective(DVar.DKind) ||
344 isOpenMPTeamsDirective(DVar.DKind)) {
345 DVar.CKind = OMPC_shared;
349 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
350 // in a Construct, implicitly determined, p.4]
351 // In a task construct, if no default clause is present, a variable that in
352 // the enclosing context is determined to be shared by all implicit tasks
353 // bound to the current team is shared.
354 if (DVar.DKind == OMPD_task) {
356 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
360 // in a Construct, implicitly determined, p.6]
361 // In a task construct, if no default clause is present, a variable
362 // whose data-sharing attribute is not determined by the rules above is
364 DVarTemp = getDSA(I, D);
365 if (DVarTemp.CKind != OMPC_shared) {
366 DVar.RefExpr = nullptr;
367 DVar.DKind = OMPD_task;
368 DVar.CKind = OMPC_firstprivate;
371 if (isParallelOrTaskRegion(I->Directive))
374 DVar.DKind = OMPD_task;
376 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
380 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
381 // in a Construct, implicitly determined, p.3]
382 // For constructs other than task, if no default clause is present, these
383 // variables inherit their data-sharing attributes from the enclosing
385 return getDSA(std::next(Iter), D);
388 DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
389 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
390 D = D->getCanonicalDecl();
391 auto It = Stack.back().AlignedMap.find(D);
392 if (It == Stack.back().AlignedMap.end()) {
393 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
394 Stack.back().AlignedMap[D] = NewDE;
397 assert(It->second && "Unexpected nullptr expr in the aligned map");
403 void DSAStackTy::addLoopControlVariable(VarDecl *D) {
404 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
405 D = D->getCanonicalDecl();
406 Stack.back().LCVSet.insert(D);
409 bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
410 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
411 D = D->getCanonicalDecl();
412 return Stack.back().LCVSet.count(D) > 0;
415 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
416 D = D->getCanonicalDecl();
417 if (A == OMPC_threadprivate) {
418 Stack[0].SharingMap[D].Attributes = A;
419 Stack[0].SharingMap[D].RefExpr = E;
421 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
422 Stack.back().SharingMap[D].Attributes = A;
423 Stack.back().SharingMap[D].RefExpr = E;
427 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
428 D = D->getCanonicalDecl();
429 if (Stack.size() > 2) {
430 reverse_iterator I = Iter, E = std::prev(Stack.rend());
431 Scope *TopScope = nullptr;
432 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
437 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
438 Scope *CurScope = getCurScope();
439 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
440 CurScope = CurScope->getParent();
442 return CurScope != TopScope;
447 /// \brief Build a variable declaration for OpenMP loop iteration variable.
448 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
450 DeclContext *DC = SemaRef.CurContext;
451 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
452 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
454 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
459 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
461 bool RefersToCapture = false) {
463 D->markUsed(S.Context);
464 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
465 SourceLocation(), D, RefersToCapture, Loc, Ty,
469 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
470 D = D->getCanonicalDecl();
473 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
474 // in a Construct, C/C++, predetermined, p.1]
475 // Variables appearing in threadprivate directives are threadprivate.
476 if ((D->getTLSKind() != VarDecl::TLS_None &&
477 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
478 SemaRef.getLangOpts().OpenMPUseTLS &&
479 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
480 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
481 !D->isLocalVarDecl())) {
482 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
486 if (Stack[0].SharingMap.count(D)) {
487 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
488 DVar.CKind = OMPC_threadprivate;
492 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
493 // in a Construct, C/C++, predetermined, p.1]
494 // Variables with automatic storage duration that are declared in a scope
495 // inside the construct are private.
496 OpenMPDirectiveKind Kind =
497 FromParent ? getParentDirective() : getCurrentDirective();
498 auto StartI = std::next(Stack.rbegin());
499 auto EndI = std::prev(Stack.rend());
500 if (FromParent && StartI != EndI) {
501 StartI = std::next(StartI);
503 if (!isParallelOrTaskRegion(Kind)) {
504 if (isOpenMPLocal(D, StartI) &&
505 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
506 D->getStorageClass() == SC_None)) ||
507 isa<ParmVarDecl>(D))) {
508 DVar.CKind = OMPC_private;
512 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
513 // in a Construct, C/C++, predetermined, p.4]
514 // Static data members are shared.
515 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
516 // in a Construct, C/C++, predetermined, p.7]
517 // Variables with static storage duration that are declared in a scope
518 // inside the construct are shared.
519 if (D->isStaticDataMember() || D->isStaticLocal()) {
520 DSAVarData DVarTemp =
521 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
522 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
525 DVar.CKind = OMPC_shared;
530 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
531 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
532 Type = SemaRef.getASTContext().getBaseElementType(Type);
533 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
534 // in a Construct, C/C++, predetermined, p.6]
535 // Variables with const qualified type having no mutable member are
538 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
540 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
541 // Variables with const-qualified type having no mutable member may be
542 // listed in a firstprivate clause, even if they are static data members.
543 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
544 MatchesAlways(), FromParent);
545 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
548 DVar.CKind = OMPC_shared;
552 // Explicitly specified attributes and local variables with predetermined
554 auto I = std::prev(StartI);
555 if (I->SharingMap.count(D)) {
556 DVar.RefExpr = I->SharingMap[D].RefExpr;
557 DVar.CKind = I->SharingMap[D].Attributes;
558 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
564 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
565 D = D->getCanonicalDecl();
566 auto StartI = Stack.rbegin();
567 auto EndI = std::prev(Stack.rend());
568 if (FromParent && StartI != EndI) {
569 StartI = std::next(StartI);
571 return getDSA(StartI, D);
574 template <class ClausesPredicate, class DirectivesPredicate>
575 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
576 DirectivesPredicate DPred,
578 D = D->getCanonicalDecl();
579 auto StartI = std::next(Stack.rbegin());
580 auto EndI = std::prev(Stack.rend());
581 if (FromParent && StartI != EndI) {
582 StartI = std::next(StartI);
584 for (auto I = StartI, EE = EndI; I != EE; ++I) {
585 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
587 DSAVarData DVar = getDSA(I, D);
588 if (CPred(DVar.CKind))
594 template <class ClausesPredicate, class DirectivesPredicate>
595 DSAStackTy::DSAVarData
596 DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
597 DirectivesPredicate DPred, bool FromParent) {
598 D = D->getCanonicalDecl();
599 auto StartI = std::next(Stack.rbegin());
600 auto EndI = std::prev(Stack.rend());
601 if (FromParent && StartI != EndI) {
602 StartI = std::next(StartI);
604 for (auto I = StartI, EE = EndI; I != EE; ++I) {
605 if (!DPred(I->Directive))
607 DSAVarData DVar = getDSA(I, D);
608 if (CPred(DVar.CKind))
615 bool DSAStackTy::hasExplicitDSA(
616 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
618 if (CPred(ClauseKindMode))
620 if (isClauseParsingMode())
622 D = D->getCanonicalDecl();
623 auto StartI = Stack.rbegin();
624 auto EndI = std::prev(Stack.rend());
625 if (std::distance(StartI, EndI) <= (int)Level)
627 std::advance(StartI, Level);
628 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
629 CPred(StartI->SharingMap[D].Attributes);
632 template <class NamedDirectivesPredicate>
633 bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
634 auto StartI = std::next(Stack.rbegin());
635 auto EndI = std::prev(Stack.rend());
636 if (FromParent && StartI != EndI) {
637 StartI = std::next(StartI);
639 for (auto I = StartI, EE = EndI; I != EE; ++I) {
640 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
646 void Sema::InitDataSharingAttributesStack() {
647 VarDataSharingAttributesStack = new DSAStackTy(*this);
650 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
652 bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
653 assert(LangOpts.OpenMP && "OpenMP is not allowed");
654 VD = VD->getCanonicalDecl();
655 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
656 if (DSAStack->isLoopControlVariable(VD) ||
657 (VD->hasLocalStorage() &&
658 isParallelOrTaskRegion(DSAStack->getCurrentDirective())))
660 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
661 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
663 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
664 DSAStack->isClauseParsingMode());
665 return DVarPrivate.CKind != OMPC_unknown;
670 bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
671 assert(LangOpts.OpenMP && "OpenMP is not allowed");
672 return DSAStack->hasExplicitDSA(
673 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
676 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
678 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
679 const DeclarationNameInfo &DirName,
680 Scope *CurScope, SourceLocation Loc) {
681 DSAStack->push(DKind, DirName, CurScope, Loc);
682 PushExpressionEvaluationContext(PotentiallyEvaluated);
685 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
686 DSAStack->setClauseParsingMode(K);
689 void Sema::EndOpenMPClause() {
690 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
693 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
694 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
695 // A variable of class type (or array thereof) that appears in a lastprivate
696 // clause requires an accessible, unambiguous default constructor for the
697 // class type, unless the list item is also specified in a firstprivate
699 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
700 for (auto *C : D->clauses()) {
701 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
702 SmallVector<Expr *, 8> PrivateCopies;
703 for (auto *DE : Clause->varlists()) {
704 if (DE->isValueDependent() || DE->isTypeDependent()) {
705 PrivateCopies.push_back(nullptr);
708 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
709 QualType Type = VD->getType();
710 auto DVar = DSAStack->getTopDSA(VD, false);
711 if (DVar.CKind == OMPC_lastprivate) {
712 // Generate helper private variable and initialize it with the
713 // default value. The address of the original variable is replaced
714 // by the address of the new private variable in CodeGen. This new
715 // variable is not added to IdResolver, so the code in the OpenMP
716 // region uses original variable for proper diagnostics.
718 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
720 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
721 if (VDPrivate->isInvalidDecl())
723 PrivateCopies.push_back(buildDeclRefExpr(
724 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
726 // The variable is also a firstprivate, so initialization sequence
727 // for private copy is generated already.
728 PrivateCopies.push_back(nullptr);
731 // Set initializers to private copies if no errors were found.
732 if (PrivateCopies.size() == Clause->varlist_size()) {
733 Clause->setPrivateCopies(PrivateCopies);
740 DiscardCleanupsInEvaluationContext();
741 PopExpressionEvaluationContext();
744 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
745 Expr *NumIterations, Sema &SemaRef,
750 class VarDeclFilterCCC : public CorrectionCandidateCallback {
755 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
756 bool ValidateCandidate(const TypoCorrection &Candidate) override {
757 NamedDecl *ND = Candidate.getCorrectionDecl();
758 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
759 return VD->hasGlobalStorage() &&
760 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
761 SemaRef.getCurScope());
768 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
769 CXXScopeSpec &ScopeSpec,
770 const DeclarationNameInfo &Id) {
771 LookupResult Lookup(*this, Id, LookupOrdinaryName);
772 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
774 if (Lookup.isAmbiguous())
778 if (!Lookup.isSingleResult()) {
779 if (TypoCorrection Corrected = CorrectTypo(
780 Id, LookupOrdinaryName, CurScope, nullptr,
781 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
782 diagnoseTypo(Corrected,
784 ? diag::err_undeclared_var_use_suggest
785 : diag::err_omp_expected_var_arg_suggest)
787 VD = Corrected.getCorrectionDeclAs<VarDecl>();
789 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
790 : diag::err_omp_expected_var_arg)
795 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
796 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
797 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
801 Lookup.suppressDiagnostics();
803 // OpenMP [2.9.2, Syntax, C/C++]
804 // Variables must be file-scope, namespace-scope, or static block-scope.
805 if (!VD->hasGlobalStorage()) {
806 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
807 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
809 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
810 Diag(VD->getLocation(),
811 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
816 VarDecl *CanonicalVD = VD->getCanonicalDecl();
817 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
818 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
819 // A threadprivate directive for file-scope variables must appear outside
820 // any definition or declaration.
821 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
822 !getCurLexicalContext()->isTranslationUnit()) {
823 Diag(Id.getLoc(), diag::err_omp_var_scope)
824 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
827 Diag(VD->getLocation(),
828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
832 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
833 // A threadprivate directive for static class member variables must appear
834 // in the class definition, in the same scope in which the member
835 // variables are declared.
836 if (CanonicalVD->isStaticDataMember() &&
837 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
838 Diag(Id.getLoc(), diag::err_omp_var_scope)
839 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
841 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
842 Diag(VD->getLocation(),
843 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
847 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
848 // A threadprivate directive for namespace-scope variables must appear
849 // outside any definition or declaration other than the namespace
850 // definition itself.
851 if (CanonicalVD->getDeclContext()->isNamespace() &&
852 (!getCurLexicalContext()->isFileContext() ||
853 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
854 Diag(Id.getLoc(), diag::err_omp_var_scope)
855 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
857 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
858 Diag(VD->getLocation(),
859 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
863 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
864 // A threadprivate directive for static block-scope variables must appear
865 // in the scope of the variable and not in a nested scope.
866 if (CanonicalVD->isStaticLocal() && CurScope &&
867 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
868 Diag(Id.getLoc(), diag::err_omp_var_scope)
869 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
871 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
872 Diag(VD->getLocation(),
873 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
878 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
879 // A threadprivate directive must lexically precede all references to any
880 // of the variables in its list.
881 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
882 Diag(Id.getLoc(), diag::err_omp_var_used)
883 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
887 QualType ExprType = VD->getType().getNonReferenceType();
888 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
893 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
894 ArrayRef<Expr *> VarList) {
895 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
896 CurContext->addDecl(D);
897 return DeclGroupPtrTy::make(DeclGroupRef(D));
899 return DeclGroupPtrTy();
903 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
907 bool VisitDeclRefExpr(const DeclRefExpr *E) {
908 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
909 if (VD->hasLocalStorage()) {
910 SemaRef.Diag(E->getLocStart(),
911 diag::err_omp_local_var_in_threadprivate_init)
912 << E->getSourceRange();
913 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
914 << VD << VD->getSourceRange();
920 bool VisitStmt(const Stmt *S) {
921 for (auto Child : S->children()) {
922 if (Child && Visit(Child))
927 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
931 OMPThreadPrivateDecl *
932 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
933 SmallVector<Expr *, 8> Vars;
934 for (auto &RefExpr : VarList) {
935 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
936 VarDecl *VD = cast<VarDecl>(DE->getDecl());
937 SourceLocation ILoc = DE->getExprLoc();
939 QualType QType = VD->getType();
940 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
941 // It will be analyzed later.
946 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
947 // A threadprivate variable must not have an incomplete type.
948 if (RequireCompleteType(ILoc, VD->getType(),
949 diag::err_omp_threadprivate_incomplete_type)) {
953 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
954 // A threadprivate variable must not have a reference type.
955 if (VD->getType()->isReferenceType()) {
956 Diag(ILoc, diag::err_omp_ref_type_arg)
957 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
959 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
960 Diag(VD->getLocation(),
961 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
966 // Check if this is a TLS variable. If TLS is not being supported, produce
967 // the corresponding diagnostic.
968 if ((VD->getTLSKind() != VarDecl::TLS_None &&
969 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
970 getLangOpts().OpenMPUseTLS &&
971 getASTContext().getTargetInfo().isTLSSupported())) ||
972 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
973 !VD->isLocalVarDecl())) {
974 Diag(ILoc, diag::err_omp_var_thread_local)
975 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
977 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
978 Diag(VD->getLocation(),
979 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
984 // Check if initial value of threadprivate variable reference variable with
985 // local storage (it is not supported by runtime).
986 if (auto Init = VD->getAnyInitializer()) {
987 LocalVarRefChecker Checker(*this);
988 if (Checker.Visit(Init))
992 Vars.push_back(RefExpr);
993 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
994 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
995 Context, SourceRange(Loc, Loc)));
996 if (auto *ML = Context.getASTMutationListener())
997 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
999 OMPThreadPrivateDecl *D = nullptr;
1000 if (!Vars.empty()) {
1001 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1003 D->setAccess(AS_public);
1008 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1009 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1010 bool IsLoopIterVar = false) {
1012 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1013 << getOpenMPClauseName(DVar.CKind);
1017 PDSA_StaticMemberShared,
1018 PDSA_StaticLocalVarShared,
1019 PDSA_LoopIterVarPrivate,
1020 PDSA_LoopIterVarLinear,
1021 PDSA_LoopIterVarLastprivate,
1022 PDSA_ConstVarShared,
1023 PDSA_GlobalVarShared,
1024 PDSA_TaskVarFirstprivate,
1025 PDSA_LocalVarPrivate,
1027 } Reason = PDSA_Implicit;
1028 bool ReportHint = false;
1029 auto ReportLoc = VD->getLocation();
1030 if (IsLoopIterVar) {
1031 if (DVar.CKind == OMPC_private)
1032 Reason = PDSA_LoopIterVarPrivate;
1033 else if (DVar.CKind == OMPC_lastprivate)
1034 Reason = PDSA_LoopIterVarLastprivate;
1036 Reason = PDSA_LoopIterVarLinear;
1037 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1038 Reason = PDSA_TaskVarFirstprivate;
1039 ReportLoc = DVar.ImplicitDSALoc;
1040 } else if (VD->isStaticLocal())
1041 Reason = PDSA_StaticLocalVarShared;
1042 else if (VD->isStaticDataMember())
1043 Reason = PDSA_StaticMemberShared;
1044 else if (VD->isFileVarDecl())
1045 Reason = PDSA_GlobalVarShared;
1046 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1047 Reason = PDSA_ConstVarShared;
1048 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1050 Reason = PDSA_LocalVarPrivate;
1052 if (Reason != PDSA_Implicit) {
1053 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1054 << Reason << ReportHint
1055 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1056 } else if (DVar.ImplicitDSALoc.isValid()) {
1057 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1058 << getOpenMPClauseName(DVar.CKind);
1063 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1068 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1069 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
1072 void VisitDeclRefExpr(DeclRefExpr *E) {
1073 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1074 // Skip internally declared variables.
1075 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1078 auto DVar = Stack->getTopDSA(VD, false);
1079 // Check if the variable has explicit DSA set and stop analysis if it so.
1080 if (DVar.RefExpr) return;
1082 auto ELoc = E->getExprLoc();
1083 auto DKind = Stack->getCurrentDirective();
1084 // The default(none) clause requires that each variable that is referenced
1085 // in the construct, and does not have a predetermined data-sharing
1086 // attribute, must have its data-sharing attribute explicitly determined
1087 // by being listed in a data-sharing attribute clause.
1088 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1089 isParallelOrTaskRegion(DKind) &&
1090 VarsWithInheritedDSA.count(VD) == 0) {
1091 VarsWithInheritedDSA[VD] = E;
1095 // OpenMP [2.9.3.6, Restrictions, p.2]
1096 // A list item that appears in a reduction clause of the innermost
1097 // enclosing worksharing or parallel construct may not be accessed in an
1099 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
1100 [](OpenMPDirectiveKind K) -> bool {
1101 return isOpenMPParallelDirective(K) ||
1102 isOpenMPWorksharingDirective(K) ||
1103 isOpenMPTeamsDirective(K);
1106 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1108 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1109 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1113 // Define implicit data-sharing attributes for task.
1114 DVar = Stack->getImplicitDSA(VD, false);
1115 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1116 ImplicitFirstprivate.push_back(E);
1119 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1120 for (auto *C : S->clauses()) {
1121 // Skip analysis of arguments of implicitly defined firstprivate clause
1122 // for task directives.
1123 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1124 for (auto *CC : C->children()) {
1130 void VisitStmt(Stmt *S) {
1131 for (auto *C : S->children()) {
1132 if (C && !isa<OMPExecutableDirective>(C))
1137 bool isErrorFound() { return ErrorFound; }
1138 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
1139 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1140 return VarsWithInheritedDSA;
1143 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1144 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1148 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1150 case OMPD_parallel: {
1151 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1152 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1153 Sema::CapturedParamNameType Params[] = {
1154 std::make_pair(".global_tid.", KmpInt32PtrTy),
1155 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1156 std::make_pair(StringRef(), QualType()) // __context with shared vars
1158 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1163 Sema::CapturedParamNameType Params[] = {
1164 std::make_pair(StringRef(), QualType()) // __context with shared vars
1166 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1171 Sema::CapturedParamNameType Params[] = {
1172 std::make_pair(StringRef(), QualType()) // __context with shared vars
1174 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1178 case OMPD_for_simd: {
1179 Sema::CapturedParamNameType Params[] = {
1180 std::make_pair(StringRef(), QualType()) // __context with shared vars
1182 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1186 case OMPD_sections: {
1187 Sema::CapturedParamNameType Params[] = {
1188 std::make_pair(StringRef(), QualType()) // __context with shared vars
1190 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1194 case OMPD_section: {
1195 Sema::CapturedParamNameType Params[] = {
1196 std::make_pair(StringRef(), QualType()) // __context with shared vars
1198 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1203 Sema::CapturedParamNameType Params[] = {
1204 std::make_pair(StringRef(), QualType()) // __context with shared vars
1206 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1211 Sema::CapturedParamNameType Params[] = {
1212 std::make_pair(StringRef(), QualType()) // __context with shared vars
1214 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1218 case OMPD_critical: {
1219 Sema::CapturedParamNameType Params[] = {
1220 std::make_pair(StringRef(), QualType()) // __context with shared vars
1222 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1226 case OMPD_parallel_for: {
1227 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1228 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1229 Sema::CapturedParamNameType Params[] = {
1230 std::make_pair(".global_tid.", KmpInt32PtrTy),
1231 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1232 std::make_pair(StringRef(), QualType()) // __context with shared vars
1234 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1238 case OMPD_parallel_for_simd: {
1239 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1240 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1241 Sema::CapturedParamNameType Params[] = {
1242 std::make_pair(".global_tid.", KmpInt32PtrTy),
1243 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1244 std::make_pair(StringRef(), QualType()) // __context with shared vars
1246 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1250 case OMPD_parallel_sections: {
1251 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1252 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1253 Sema::CapturedParamNameType Params[] = {
1254 std::make_pair(".global_tid.", KmpInt32PtrTy),
1255 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1256 std::make_pair(StringRef(), QualType()) // __context with shared vars
1258 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1263 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1264 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1265 FunctionProtoType::ExtProtoInfo EPI;
1266 EPI.Variadic = true;
1267 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1268 Sema::CapturedParamNameType Params[] = {
1269 std::make_pair(".global_tid.", KmpInt32Ty),
1270 std::make_pair(".part_id.", KmpInt32Ty),
1271 std::make_pair(".privates.",
1272 Context.VoidPtrTy.withConst().withRestrict()),
1275 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1276 std::make_pair(StringRef(), QualType()) // __context with shared vars
1278 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1280 // Mark this captured region as inlined, because we don't use outlined
1281 // function directly.
1282 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1283 AlwaysInlineAttr::CreateImplicit(
1284 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1287 case OMPD_ordered: {
1288 Sema::CapturedParamNameType Params[] = {
1289 std::make_pair(StringRef(), QualType()) // __context with shared vars
1291 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1296 Sema::CapturedParamNameType Params[] = {
1297 std::make_pair(StringRef(), QualType()) // __context with shared vars
1299 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1304 Sema::CapturedParamNameType Params[] = {
1305 std::make_pair(StringRef(), QualType()) // __context with shared vars
1307 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1312 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1313 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1314 Sema::CapturedParamNameType Params[] = {
1315 std::make_pair(".global_tid.", KmpInt32PtrTy),
1316 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1317 std::make_pair(StringRef(), QualType()) // __context with shared vars
1319 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1323 case OMPD_taskgroup: {
1324 Sema::CapturedParamNameType Params[] = {
1325 std::make_pair(StringRef(), QualType()) // __context with shared vars
1327 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1331 case OMPD_threadprivate:
1332 case OMPD_taskyield:
1335 case OMPD_cancellation_point:
1338 llvm_unreachable("OpenMP Directive is not allowed");
1340 llvm_unreachable("Unknown OpenMP directive");
1344 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1345 ArrayRef<OMPClause *> Clauses) {
1346 if (!S.isUsable()) {
1347 ActOnCapturedRegionError();
1350 // This is required for proper codegen.
1351 for (auto *Clause : Clauses) {
1352 if (isOpenMPPrivate(Clause->getClauseKind()) ||
1353 Clause->getClauseKind() == OMPC_copyprivate) {
1354 // Mark all variables in private list clauses as used in inner region.
1355 for (auto *VarRef : Clause->children()) {
1356 if (auto *E = cast_or_null<Expr>(VarRef)) {
1357 MarkDeclarationsReferencedInExpr(E);
1360 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1361 Clause->getClauseKind() == OMPC_schedule) {
1362 // Mark all variables in private list clauses as used in inner region.
1363 // Required for proper codegen of combined directives.
1364 // TODO: add processing for other clauses.
1365 if (auto *E = cast_or_null<Expr>(
1366 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1367 MarkDeclarationsReferencedInExpr(E);
1371 return ActOnCapturedRegionEnd(S.get());
1374 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1375 OpenMPDirectiveKind CurrentRegion,
1376 const DeclarationNameInfo &CurrentName,
1377 OpenMPDirectiveKind CancelRegion,
1378 SourceLocation StartLoc) {
1379 // Allowed nesting of constructs
1380 // +------------------+-----------------+------------------------------------+
1381 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1382 // +------------------+-----------------+------------------------------------+
1383 // | parallel | parallel | * |
1384 // | parallel | for | * |
1385 // | parallel | for simd | * |
1386 // | parallel | master | * |
1387 // | parallel | critical | * |
1388 // | parallel | simd | * |
1389 // | parallel | sections | * |
1390 // | parallel | section | + |
1391 // | parallel | single | * |
1392 // | parallel | parallel for | * |
1393 // | parallel |parallel for simd| * |
1394 // | parallel |parallel sections| * |
1395 // | parallel | task | * |
1396 // | parallel | taskyield | * |
1397 // | parallel | barrier | * |
1398 // | parallel | taskwait | * |
1399 // | parallel | taskgroup | * |
1400 // | parallel | flush | * |
1401 // | parallel | ordered | + |
1402 // | parallel | atomic | * |
1403 // | parallel | target | * |
1404 // | parallel | teams | + |
1405 // | parallel | cancellation | |
1407 // | parallel | cancel | ! |
1408 // +------------------+-----------------+------------------------------------+
1409 // | for | parallel | * |
1410 // | for | for | + |
1411 // | for | for simd | + |
1412 // | for | master | + |
1413 // | for | critical | * |
1414 // | for | simd | * |
1415 // | for | sections | + |
1416 // | for | section | + |
1417 // | for | single | + |
1418 // | for | parallel for | * |
1419 // | for |parallel for simd| * |
1420 // | for |parallel sections| * |
1421 // | for | task | * |
1422 // | for | taskyield | * |
1423 // | for | barrier | + |
1424 // | for | taskwait | * |
1425 // | for | taskgroup | * |
1426 // | for | flush | * |
1427 // | for | ordered | * (if construct is ordered) |
1428 // | for | atomic | * |
1429 // | for | target | * |
1430 // | for | teams | + |
1431 // | for | cancellation | |
1433 // | for | cancel | ! |
1434 // +------------------+-----------------+------------------------------------+
1435 // | master | parallel | * |
1436 // | master | for | + |
1437 // | master | for simd | + |
1438 // | master | master | * |
1439 // | master | critical | * |
1440 // | master | simd | * |
1441 // | master | sections | + |
1442 // | master | section | + |
1443 // | master | single | + |
1444 // | master | parallel for | * |
1445 // | master |parallel for simd| * |
1446 // | master |parallel sections| * |
1447 // | master | task | * |
1448 // | master | taskyield | * |
1449 // | master | barrier | + |
1450 // | master | taskwait | * |
1451 // | master | taskgroup | * |
1452 // | master | flush | * |
1453 // | master | ordered | + |
1454 // | master | atomic | * |
1455 // | master | target | * |
1456 // | master | teams | + |
1457 // | master | cancellation | |
1459 // | master | cancel | |
1460 // +------------------+-----------------+------------------------------------+
1461 // | critical | parallel | * |
1462 // | critical | for | + |
1463 // | critical | for simd | + |
1464 // | critical | master | * |
1465 // | critical | critical | * (should have different names) |
1466 // | critical | simd | * |
1467 // | critical | sections | + |
1468 // | critical | section | + |
1469 // | critical | single | + |
1470 // | critical | parallel for | * |
1471 // | critical |parallel for simd| * |
1472 // | critical |parallel sections| * |
1473 // | critical | task | * |
1474 // | critical | taskyield | * |
1475 // | critical | barrier | + |
1476 // | critical | taskwait | * |
1477 // | critical | taskgroup | * |
1478 // | critical | ordered | + |
1479 // | critical | atomic | * |
1480 // | critical | target | * |
1481 // | critical | teams | + |
1482 // | critical | cancellation | |
1484 // | critical | cancel | |
1485 // +------------------+-----------------+------------------------------------+
1486 // | simd | parallel | |
1488 // | simd | for simd | |
1489 // | simd | master | |
1490 // | simd | critical | |
1491 // | simd | simd | |
1492 // | simd | sections | |
1493 // | simd | section | |
1494 // | simd | single | |
1495 // | simd | parallel for | |
1496 // | simd |parallel for simd| |
1497 // | simd |parallel sections| |
1498 // | simd | task | |
1499 // | simd | taskyield | |
1500 // | simd | barrier | |
1501 // | simd | taskwait | |
1502 // | simd | taskgroup | |
1503 // | simd | flush | |
1504 // | simd | ordered | |
1505 // | simd | atomic | |
1506 // | simd | target | |
1507 // | simd | teams | |
1508 // | simd | cancellation | |
1510 // | simd | cancel | |
1511 // +------------------+-----------------+------------------------------------+
1512 // | for simd | parallel | |
1513 // | for simd | for | |
1514 // | for simd | for simd | |
1515 // | for simd | master | |
1516 // | for simd | critical | |
1517 // | for simd | simd | |
1518 // | for simd | sections | |
1519 // | for simd | section | |
1520 // | for simd | single | |
1521 // | for simd | parallel for | |
1522 // | for simd |parallel for simd| |
1523 // | for simd |parallel sections| |
1524 // | for simd | task | |
1525 // | for simd | taskyield | |
1526 // | for simd | barrier | |
1527 // | for simd | taskwait | |
1528 // | for simd | taskgroup | |
1529 // | for simd | flush | |
1530 // | for simd | ordered | |
1531 // | for simd | atomic | |
1532 // | for simd | target | |
1533 // | for simd | teams | |
1534 // | for simd | cancellation | |
1536 // | for simd | cancel | |
1537 // +------------------+-----------------+------------------------------------+
1538 // | parallel for simd| parallel | |
1539 // | parallel for simd| for | |
1540 // | parallel for simd| for simd | |
1541 // | parallel for simd| master | |
1542 // | parallel for simd| critical | |
1543 // | parallel for simd| simd | |
1544 // | parallel for simd| sections | |
1545 // | parallel for simd| section | |
1546 // | parallel for simd| single | |
1547 // | parallel for simd| parallel for | |
1548 // | parallel for simd|parallel for simd| |
1549 // | parallel for simd|parallel sections| |
1550 // | parallel for simd| task | |
1551 // | parallel for simd| taskyield | |
1552 // | parallel for simd| barrier | |
1553 // | parallel for simd| taskwait | |
1554 // | parallel for simd| taskgroup | |
1555 // | parallel for simd| flush | |
1556 // | parallel for simd| ordered | |
1557 // | parallel for simd| atomic | |
1558 // | parallel for simd| target | |
1559 // | parallel for simd| teams | |
1560 // | parallel for simd| cancellation | |
1562 // | parallel for simd| cancel | |
1563 // +------------------+-----------------+------------------------------------+
1564 // | sections | parallel | * |
1565 // | sections | for | + |
1566 // | sections | for simd | + |
1567 // | sections | master | + |
1568 // | sections | critical | * |
1569 // | sections | simd | * |
1570 // | sections | sections | + |
1571 // | sections | section | * |
1572 // | sections | single | + |
1573 // | sections | parallel for | * |
1574 // | sections |parallel for simd| * |
1575 // | sections |parallel sections| * |
1576 // | sections | task | * |
1577 // | sections | taskyield | * |
1578 // | sections | barrier | + |
1579 // | sections | taskwait | * |
1580 // | sections | taskgroup | * |
1581 // | sections | flush | * |
1582 // | sections | ordered | + |
1583 // | sections | atomic | * |
1584 // | sections | target | * |
1585 // | sections | teams | + |
1586 // | sections | cancellation | |
1588 // | sections | cancel | ! |
1589 // +------------------+-----------------+------------------------------------+
1590 // | section | parallel | * |
1591 // | section | for | + |
1592 // | section | for simd | + |
1593 // | section | master | + |
1594 // | section | critical | * |
1595 // | section | simd | * |
1596 // | section | sections | + |
1597 // | section | section | + |
1598 // | section | single | + |
1599 // | section | parallel for | * |
1600 // | section |parallel for simd| * |
1601 // | section |parallel sections| * |
1602 // | section | task | * |
1603 // | section | taskyield | * |
1604 // | section | barrier | + |
1605 // | section | taskwait | * |
1606 // | section | taskgroup | * |
1607 // | section | flush | * |
1608 // | section | ordered | + |
1609 // | section | atomic | * |
1610 // | section | target | * |
1611 // | section | teams | + |
1612 // | section | cancellation | |
1614 // | section | cancel | ! |
1615 // +------------------+-----------------+------------------------------------+
1616 // | single | parallel | * |
1617 // | single | for | + |
1618 // | single | for simd | + |
1619 // | single | master | + |
1620 // | single | critical | * |
1621 // | single | simd | * |
1622 // | single | sections | + |
1623 // | single | section | + |
1624 // | single | single | + |
1625 // | single | parallel for | * |
1626 // | single |parallel for simd| * |
1627 // | single |parallel sections| * |
1628 // | single | task | * |
1629 // | single | taskyield | * |
1630 // | single | barrier | + |
1631 // | single | taskwait | * |
1632 // | single | taskgroup | * |
1633 // | single | flush | * |
1634 // | single | ordered | + |
1635 // | single | atomic | * |
1636 // | single | target | * |
1637 // | single | teams | + |
1638 // | single | cancellation | |
1640 // | single | cancel | |
1641 // +------------------+-----------------+------------------------------------+
1642 // | parallel for | parallel | * |
1643 // | parallel for | for | + |
1644 // | parallel for | for simd | + |
1645 // | parallel for | master | + |
1646 // | parallel for | critical | * |
1647 // | parallel for | simd | * |
1648 // | parallel for | sections | + |
1649 // | parallel for | section | + |
1650 // | parallel for | single | + |
1651 // | parallel for | parallel for | * |
1652 // | parallel for |parallel for simd| * |
1653 // | parallel for |parallel sections| * |
1654 // | parallel for | task | * |
1655 // | parallel for | taskyield | * |
1656 // | parallel for | barrier | + |
1657 // | parallel for | taskwait | * |
1658 // | parallel for | taskgroup | * |
1659 // | parallel for | flush | * |
1660 // | parallel for | ordered | * (if construct is ordered) |
1661 // | parallel for | atomic | * |
1662 // | parallel for | target | * |
1663 // | parallel for | teams | + |
1664 // | parallel for | cancellation | |
1666 // | parallel for | cancel | ! |
1667 // +------------------+-----------------+------------------------------------+
1668 // | parallel sections| parallel | * |
1669 // | parallel sections| for | + |
1670 // | parallel sections| for simd | + |
1671 // | parallel sections| master | + |
1672 // | parallel sections| critical | + |
1673 // | parallel sections| simd | * |
1674 // | parallel sections| sections | + |
1675 // | parallel sections| section | * |
1676 // | parallel sections| single | + |
1677 // | parallel sections| parallel for | * |
1678 // | parallel sections|parallel for simd| * |
1679 // | parallel sections|parallel sections| * |
1680 // | parallel sections| task | * |
1681 // | parallel sections| taskyield | * |
1682 // | parallel sections| barrier | + |
1683 // | parallel sections| taskwait | * |
1684 // | parallel sections| taskgroup | * |
1685 // | parallel sections| flush | * |
1686 // | parallel sections| ordered | + |
1687 // | parallel sections| atomic | * |
1688 // | parallel sections| target | * |
1689 // | parallel sections| teams | + |
1690 // | parallel sections| cancellation | |
1692 // | parallel sections| cancel | ! |
1693 // +------------------+-----------------+------------------------------------+
1694 // | task | parallel | * |
1695 // | task | for | + |
1696 // | task | for simd | + |
1697 // | task | master | + |
1698 // | task | critical | * |
1699 // | task | simd | * |
1700 // | task | sections | + |
1701 // | task | section | + |
1702 // | task | single | + |
1703 // | task | parallel for | * |
1704 // | task |parallel for simd| * |
1705 // | task |parallel sections| * |
1706 // | task | task | * |
1707 // | task | taskyield | * |
1708 // | task | barrier | + |
1709 // | task | taskwait | * |
1710 // | task | taskgroup | * |
1711 // | task | flush | * |
1712 // | task | ordered | + |
1713 // | task | atomic | * |
1714 // | task | target | * |
1715 // | task | teams | + |
1716 // | task | cancellation | |
1718 // | task | cancel | ! |
1719 // +------------------+-----------------+------------------------------------+
1720 // | ordered | parallel | * |
1721 // | ordered | for | + |
1722 // | ordered | for simd | + |
1723 // | ordered | master | * |
1724 // | ordered | critical | * |
1725 // | ordered | simd | * |
1726 // | ordered | sections | + |
1727 // | ordered | section | + |
1728 // | ordered | single | + |
1729 // | ordered | parallel for | * |
1730 // | ordered |parallel for simd| * |
1731 // | ordered |parallel sections| * |
1732 // | ordered | task | * |
1733 // | ordered | taskyield | * |
1734 // | ordered | barrier | + |
1735 // | ordered | taskwait | * |
1736 // | ordered | taskgroup | * |
1737 // | ordered | flush | * |
1738 // | ordered | ordered | + |
1739 // | ordered | atomic | * |
1740 // | ordered | target | * |
1741 // | ordered | teams | + |
1742 // | ordered | cancellation | |
1744 // | ordered | cancel | |
1745 // +------------------+-----------------+------------------------------------+
1746 // | atomic | parallel | |
1747 // | atomic | for | |
1748 // | atomic | for simd | |
1749 // | atomic | master | |
1750 // | atomic | critical | |
1751 // | atomic | simd | |
1752 // | atomic | sections | |
1753 // | atomic | section | |
1754 // | atomic | single | |
1755 // | atomic | parallel for | |
1756 // | atomic |parallel for simd| |
1757 // | atomic |parallel sections| |
1758 // | atomic | task | |
1759 // | atomic | taskyield | |
1760 // | atomic | barrier | |
1761 // | atomic | taskwait | |
1762 // | atomic | taskgroup | |
1763 // | atomic | flush | |
1764 // | atomic | ordered | |
1765 // | atomic | atomic | |
1766 // | atomic | target | |
1767 // | atomic | teams | |
1768 // | atomic | cancellation | |
1770 // | atomic | cancel | |
1771 // +------------------+-----------------+------------------------------------+
1772 // | target | parallel | * |
1773 // | target | for | * |
1774 // | target | for simd | * |
1775 // | target | master | * |
1776 // | target | critical | * |
1777 // | target | simd | * |
1778 // | target | sections | * |
1779 // | target | section | * |
1780 // | target | single | * |
1781 // | target | parallel for | * |
1782 // | target |parallel for simd| * |
1783 // | target |parallel sections| * |
1784 // | target | task | * |
1785 // | target | taskyield | * |
1786 // | target | barrier | * |
1787 // | target | taskwait | * |
1788 // | target | taskgroup | * |
1789 // | target | flush | * |
1790 // | target | ordered | * |
1791 // | target | atomic | * |
1792 // | target | target | * |
1793 // | target | teams | * |
1794 // | target | cancellation | |
1796 // | target | cancel | |
1797 // +------------------+-----------------+------------------------------------+
1798 // | teams | parallel | * |
1799 // | teams | for | + |
1800 // | teams | for simd | + |
1801 // | teams | master | + |
1802 // | teams | critical | + |
1803 // | teams | simd | + |
1804 // | teams | sections | + |
1805 // | teams | section | + |
1806 // | teams | single | + |
1807 // | teams | parallel for | * |
1808 // | teams |parallel for simd| * |
1809 // | teams |parallel sections| * |
1810 // | teams | task | + |
1811 // | teams | taskyield | + |
1812 // | teams | barrier | + |
1813 // | teams | taskwait | + |
1814 // | teams | taskgroup | + |
1815 // | teams | flush | + |
1816 // | teams | ordered | + |
1817 // | teams | atomic | + |
1818 // | teams | target | + |
1819 // | teams | teams | + |
1820 // | teams | cancellation | |
1822 // | teams | cancel | |
1823 // +------------------+-----------------+------------------------------------+
1824 if (Stack->getCurScope()) {
1825 auto ParentRegion = Stack->getParentDirective();
1826 bool NestingProhibited = false;
1827 bool CloseNesting = true;
1830 ShouldBeInParallelRegion,
1831 ShouldBeInOrderedRegion,
1832 ShouldBeInTargetRegion
1833 } Recommend = NoRecommend;
1834 if (isOpenMPSimdDirective(ParentRegion)) {
1835 // OpenMP [2.16, Nesting of Regions]
1836 // OpenMP constructs may not be nested inside a simd region.
1837 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1840 if (ParentRegion == OMPD_atomic) {
1841 // OpenMP [2.16, Nesting of Regions]
1842 // OpenMP constructs may not be nested inside an atomic region.
1843 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1846 if (CurrentRegion == OMPD_section) {
1847 // OpenMP [2.7.2, sections Construct, Restrictions]
1848 // Orphaned section directives are prohibited. That is, the section
1849 // directives must appear within the sections construct and must not be
1850 // encountered elsewhere in the sections region.
1851 if (ParentRegion != OMPD_sections &&
1852 ParentRegion != OMPD_parallel_sections) {
1853 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1854 << (ParentRegion != OMPD_unknown)
1855 << getOpenMPDirectiveName(ParentRegion);
1860 // Allow some constructs to be orphaned (they could be used in functions,
1861 // called from OpenMP regions with the required preconditions).
1862 if (ParentRegion == OMPD_unknown)
1864 if (CurrentRegion == OMPD_cancellation_point ||
1865 CurrentRegion == OMPD_cancel) {
1866 // OpenMP [2.16, Nesting of Regions]
1867 // A cancellation point construct for which construct-type-clause is
1868 // taskgroup must be nested inside a task construct. A cancellation
1869 // point construct for which construct-type-clause is not taskgroup must
1870 // be closely nested inside an OpenMP construct that matches the type
1871 // specified in construct-type-clause.
1872 // A cancel construct for which construct-type-clause is taskgroup must be
1873 // nested inside a task construct. A cancel construct for which
1874 // construct-type-clause is not taskgroup must be closely nested inside an
1875 // OpenMP construct that matches the type specified in
1876 // construct-type-clause.
1878 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1879 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1880 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1881 (CancelRegion == OMPD_sections &&
1882 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1883 } else if (CurrentRegion == OMPD_master) {
1884 // OpenMP [2.16, Nesting of Regions]
1885 // A master region may not be closely nested inside a worksharing,
1886 // atomic, or explicit task region.
1887 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1888 ParentRegion == OMPD_task;
1889 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1890 // OpenMP [2.16, Nesting of Regions]
1891 // A critical region may not be nested (closely or otherwise) inside a
1892 // critical region with the same name. Note that this restriction is not
1893 // sufficient to prevent deadlock.
1894 SourceLocation PreviousCriticalLoc;
1896 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1897 OpenMPDirectiveKind K,
1898 const DeclarationNameInfo &DNI,
1901 if (K == OMPD_critical &&
1902 DNI.getName() == CurrentName.getName()) {
1903 PreviousCriticalLoc = Loc;
1908 false /* skip top directive */);
1910 SemaRef.Diag(StartLoc,
1911 diag::err_omp_prohibited_region_critical_same_name)
1912 << CurrentName.getName();
1913 if (PreviousCriticalLoc.isValid())
1914 SemaRef.Diag(PreviousCriticalLoc,
1915 diag::note_omp_previous_critical_region);
1918 } else if (CurrentRegion == OMPD_barrier) {
1919 // OpenMP [2.16, Nesting of Regions]
1920 // A barrier region may not be closely nested inside a worksharing,
1921 // explicit task, critical, ordered, atomic, or master region.
1923 isOpenMPWorksharingDirective(ParentRegion) ||
1924 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1925 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1926 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1927 !isOpenMPParallelDirective(CurrentRegion)) {
1928 // OpenMP [2.16, Nesting of Regions]
1929 // A worksharing region may not be closely nested inside a worksharing,
1930 // explicit task, critical, ordered, atomic, or master region.
1932 isOpenMPWorksharingDirective(ParentRegion) ||
1933 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1934 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1935 Recommend = ShouldBeInParallelRegion;
1936 } else if (CurrentRegion == OMPD_ordered) {
1937 // OpenMP [2.16, Nesting of Regions]
1938 // An ordered region may not be closely nested inside a critical,
1939 // atomic, or explicit task region.
1940 // An ordered region must be closely nested inside a loop region (or
1941 // parallel loop region) with an ordered clause.
1942 NestingProhibited = ParentRegion == OMPD_critical ||
1943 ParentRegion == OMPD_task ||
1944 !Stack->isParentOrderedRegion();
1945 Recommend = ShouldBeInOrderedRegion;
1946 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1947 // OpenMP [2.16, Nesting of Regions]
1948 // If specified, a teams construct must be contained within a target
1950 NestingProhibited = ParentRegion != OMPD_target;
1951 Recommend = ShouldBeInTargetRegion;
1952 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1954 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1955 // OpenMP [2.16, Nesting of Regions]
1956 // distribute, parallel, parallel sections, parallel workshare, and the
1957 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1958 // constructs that can be closely nested in the teams region.
1959 // TODO: add distribute directive.
1960 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1961 Recommend = ShouldBeInParallelRegion;
1963 if (NestingProhibited) {
1964 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
1965 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1966 << getOpenMPDirectiveName(CurrentRegion);
1973 StmtResult Sema::ActOnOpenMPExecutableDirective(
1974 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1975 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1976 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
1977 StmtResult Res = StmtError();
1978 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1982 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1983 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
1984 bool ErrorFound = false;
1985 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1987 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1989 // Check default data sharing attributes for referenced variables.
1990 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1991 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1992 if (DSAChecker.isErrorFound())
1994 // Generate list of implicitly defined firstprivate variables.
1995 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
1997 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1998 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1999 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2000 SourceLocation(), SourceLocation())) {
2001 ClausesWithImplicit.push_back(Implicit);
2002 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2003 DSAChecker.getImplicitFirstprivate().size();
2011 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2015 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2016 VarsWithInheritedDSA);
2019 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2020 VarsWithInheritedDSA);
2023 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2024 EndLoc, VarsWithInheritedDSA);
2027 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2031 assert(ClausesWithImplicit.empty() &&
2032 "No clauses are allowed for 'omp section' directive");
2033 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2036 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2040 assert(ClausesWithImplicit.empty() &&
2041 "No clauses are allowed for 'omp master' directive");
2042 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2045 assert(ClausesWithImplicit.empty() &&
2046 "No clauses are allowed for 'omp critical' directive");
2047 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2049 case OMPD_parallel_for:
2050 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2051 EndLoc, VarsWithInheritedDSA);
2053 case OMPD_parallel_for_simd:
2054 Res = ActOnOpenMPParallelForSimdDirective(
2055 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2057 case OMPD_parallel_sections:
2058 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2063 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2065 case OMPD_taskyield:
2066 assert(ClausesWithImplicit.empty() &&
2067 "No clauses are allowed for 'omp taskyield' directive");
2068 assert(AStmt == nullptr &&
2069 "No associated statement allowed for 'omp taskyield' directive");
2070 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2073 assert(ClausesWithImplicit.empty() &&
2074 "No clauses are allowed for 'omp barrier' directive");
2075 assert(AStmt == nullptr &&
2076 "No associated statement allowed for 'omp barrier' directive");
2077 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2080 assert(ClausesWithImplicit.empty() &&
2081 "No clauses are allowed for 'omp taskwait' directive");
2082 assert(AStmt == nullptr &&
2083 "No associated statement allowed for 'omp taskwait' directive");
2084 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2086 case OMPD_taskgroup:
2087 assert(ClausesWithImplicit.empty() &&
2088 "No clauses are allowed for 'omp taskgroup' directive");
2089 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2092 assert(AStmt == nullptr &&
2093 "No associated statement allowed for 'omp flush' directive");
2094 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2097 assert(ClausesWithImplicit.empty() &&
2098 "No clauses are allowed for 'omp ordered' directive");
2099 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2102 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2107 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2110 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2113 case OMPD_cancellation_point:
2114 assert(ClausesWithImplicit.empty() &&
2115 "No clauses are allowed for 'omp cancellation point' directive");
2116 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2117 "cancellation point' directive");
2118 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2121 assert(ClausesWithImplicit.empty() &&
2122 "No clauses are allowed for 'omp cancel' directive");
2123 assert(AStmt == nullptr &&
2124 "No associated statement allowed for 'omp cancel' directive");
2125 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2127 case OMPD_threadprivate:
2128 llvm_unreachable("OpenMP Directive is not allowed");
2130 llvm_unreachable("Unknown OpenMP directive");
2133 for (auto P : VarsWithInheritedDSA) {
2134 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2135 << P.first << P.second->getSourceRange();
2137 if (!VarsWithInheritedDSA.empty())
2145 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2147 SourceLocation StartLoc,
2148 SourceLocation EndLoc) {
2149 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2150 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2151 // 1.2.2 OpenMP Language Terminology
2152 // Structured block - An executable statement with a single entry at the
2153 // top and a single exit at the bottom.
2154 // The point of exit cannot be a branch out of the structured block.
2155 // longjmp() and throw() must not violate the entry/exit criteria.
2156 CS->getCapturedDecl()->setNothrow();
2158 getCurFunction()->setHasBranchProtectedScope();
2160 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2165 /// \brief Helper class for checking canonical form of the OpenMP loops and
2166 /// extracting iteration space of each loop in the loop nest, that will be used
2167 /// for IR generation.
2168 class OpenMPIterationSpaceChecker {
2169 /// \brief Reference to Sema.
2171 /// \brief A location for diagnostics (when there is no some better location).
2172 SourceLocation DefaultLoc;
2173 /// \brief A location for diagnostics (when increment is not compatible).
2174 SourceLocation ConditionLoc;
2175 /// \brief A source location for referring to loop init later.
2176 SourceRange InitSrcRange;
2177 /// \brief A source location for referring to condition later.
2178 SourceRange ConditionSrcRange;
2179 /// \brief A source location for referring to increment later.
2180 SourceRange IncrementSrcRange;
2181 /// \brief Loop variable.
2183 /// \brief Reference to loop variable.
2184 DeclRefExpr *VarRef;
2185 /// \brief Lower bound (initializer for the var).
2187 /// \brief Upper bound.
2189 /// \brief Loop step (increment).
2191 /// \brief This flag is true when condition is one of:
2197 /// \brief This flag is true when condition is strict ( < or > ).
2198 bool TestIsStrictOp;
2199 /// \brief This flag is true when step is subtracted on each iteration.
2203 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2204 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
2205 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2206 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
2207 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2208 TestIsStrictOp(false), SubtractStep(false) {}
2209 /// \brief Check init-expr for canonical loop form and save loop counter
2210 /// variable - #Var and its initialization value - #LB.
2211 bool CheckInit(Stmt *S, bool EmitDiags = true);
2212 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2213 /// for less/greater and for strict/non-strict comparison.
2214 bool CheckCond(Expr *S);
2215 /// \brief Check incr-expr for canonical loop form and return true if it
2216 /// does not conform, otherwise save loop step (#Step).
2217 bool CheckInc(Expr *S);
2218 /// \brief Return the loop counter variable.
2219 VarDecl *GetLoopVar() const { return Var; }
2220 /// \brief Return the reference expression to loop counter variable.
2221 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
2222 /// \brief Source range of the loop init.
2223 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2224 /// \brief Source range of the loop condition.
2225 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2226 /// \brief Source range of the loop increment.
2227 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2228 /// \brief True if the step should be subtracted.
2229 bool ShouldSubtractStep() const { return SubtractStep; }
2230 /// \brief Build the expression to calculate the number of iterations.
2231 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
2232 /// \brief Build the precondition expression for the loops.
2233 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
2234 /// \brief Build reference expression to the counter be used for codegen.
2235 Expr *BuildCounterVar() const;
2236 /// \brief Build initization of the counter be used for codegen.
2237 Expr *BuildCounterInit() const;
2238 /// \brief Build step of the counter be used for codegen.
2239 Expr *BuildCounterStep() const;
2240 /// \brief Return true if any expression is dependent.
2241 bool Dependent() const;
2244 /// \brief Check the right-hand side of an assignment in the increment
2246 bool CheckIncRHS(Expr *RHS);
2247 /// \brief Helper to set loop counter variable and its initializer.
2248 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
2249 /// \brief Helper to set upper bound.
2250 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2251 const SourceLocation &SL);
2252 /// \brief Helper to set loop increment.
2253 bool SetStep(Expr *NewStep, bool Subtract);
2256 bool OpenMPIterationSpaceChecker::Dependent() const {
2258 assert(!LB && !UB && !Step);
2261 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2262 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2265 bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2266 DeclRefExpr *NewVarRefExpr,
2268 // State consistency checking to ensure correct usage.
2269 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2270 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
2271 if (!NewVar || !NewLB)
2274 VarRef = NewVarRefExpr;
2279 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2280 const SourceRange &SR,
2281 const SourceLocation &SL) {
2282 // State consistency checking to ensure correct usage.
2283 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2284 !TestIsLessOp && !TestIsStrictOp);
2288 TestIsLessOp = LessOp;
2289 TestIsStrictOp = StrictOp;
2290 ConditionSrcRange = SR;
2295 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2296 // State consistency checking to ensure correct usage.
2297 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2300 if (!NewStep->isValueDependent()) {
2301 // Check that the step is integer expression.
2302 SourceLocation StepLoc = NewStep->getLocStart();
2304 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2305 if (Val.isInvalid())
2307 NewStep = Val.get();
2309 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2310 // If test-expr is of form var relational-op b and relational-op is < or
2311 // <= then incr-expr must cause var to increase on each iteration of the
2312 // loop. If test-expr is of form var relational-op b and relational-op is
2313 // > or >= then incr-expr must cause var to decrease on each iteration of
2315 // If test-expr is of form b relational-op var and relational-op is < or
2316 // <= then incr-expr must cause var to decrease on each iteration of the
2317 // loop. If test-expr is of form b relational-op var and relational-op is
2318 // > or >= then incr-expr must cause var to increase on each iteration of
2320 llvm::APSInt Result;
2321 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2322 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2324 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
2326 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
2327 bool IsConstZero = IsConstant && !Result.getBoolValue();
2328 if (UB && (IsConstZero ||
2329 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
2330 : (IsConstPos || (IsUnsigned && !Subtract))))) {
2331 SemaRef.Diag(NewStep->getExprLoc(),
2332 diag::err_omp_loop_incr_not_compatible)
2333 << Var << TestIsLessOp << NewStep->getSourceRange();
2334 SemaRef.Diag(ConditionLoc,
2335 diag::note_omp_loop_cond_requres_compatible_incr)
2336 << TestIsLessOp << ConditionSrcRange;
2339 if (TestIsLessOp == Subtract) {
2340 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2342 Subtract = !Subtract;
2347 SubtractStep = Subtract;
2351 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
2352 // Check init-expr for canonical loop form and save loop counter
2353 // variable - #Var and its initialization value - #LB.
2354 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2356 // integer-type var = lb
2357 // random-access-iterator-type var = lb
2358 // pointer-type var = lb
2362 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2366 InitSrcRange = S->getSourceRange();
2367 if (Expr *E = dyn_cast<Expr>(S))
2368 S = E->IgnoreParens();
2369 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2370 if (BO->getOpcode() == BO_Assign)
2371 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
2372 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2374 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2375 if (DS->isSingleDecl()) {
2376 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2377 if (Var->hasInit()) {
2378 // Accept non-canonical init form here but emit ext. warning.
2379 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
2380 SemaRef.Diag(S->getLocStart(),
2381 diag::ext_omp_loop_not_canonical_init)
2382 << S->getSourceRange();
2383 return SetVarAndLB(Var, nullptr, Var->getInit());
2387 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2388 if (CE->getOperator() == OO_Equal)
2389 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
2390 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2394 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2395 << S->getSourceRange();
2400 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
2401 /// variable (which may be the loop variable) if possible.
2402 static const VarDecl *GetInitVarDecl(const Expr *E) {
2405 E = E->IgnoreParenImpCasts();
2406 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2407 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2408 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2409 CE->getArg(0) != nullptr)
2410 E = CE->getArg(0)->IgnoreParenImpCasts();
2411 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2414 return dyn_cast<VarDecl>(DRE->getDecl());
2417 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2418 // Check test-expr for canonical form, save upper-bound UB, flags for
2419 // less/greater and for strict/non-strict comparison.
2420 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2421 // var relational-op b
2422 // b relational-op var
2425 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2428 S = S->IgnoreParenImpCasts();
2429 SourceLocation CondLoc = S->getLocStart();
2430 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2431 if (BO->isRelationalOp()) {
2432 if (GetInitVarDecl(BO->getLHS()) == Var)
2433 return SetUB(BO->getRHS(),
2434 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2435 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2436 BO->getSourceRange(), BO->getOperatorLoc());
2437 if (GetInitVarDecl(BO->getRHS()) == Var)
2438 return SetUB(BO->getLHS(),
2439 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2440 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2441 BO->getSourceRange(), BO->getOperatorLoc());
2443 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2444 if (CE->getNumArgs() == 2) {
2445 auto Op = CE->getOperator();
2448 case OO_GreaterEqual:
2451 if (GetInitVarDecl(CE->getArg(0)) == Var)
2452 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2453 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2454 CE->getOperatorLoc());
2455 if (GetInitVarDecl(CE->getArg(1)) == Var)
2456 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2457 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2458 CE->getOperatorLoc());
2465 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2466 << S->getSourceRange() << Var;
2470 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2471 // RHS of canonical loop form increment can be:
2476 RHS = RHS->IgnoreParenImpCasts();
2477 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2478 if (BO->isAdditiveOp()) {
2479 bool IsAdd = BO->getOpcode() == BO_Add;
2480 if (GetInitVarDecl(BO->getLHS()) == Var)
2481 return SetStep(BO->getRHS(), !IsAdd);
2482 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2483 return SetStep(BO->getLHS(), false);
2485 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2486 bool IsAdd = CE->getOperator() == OO_Plus;
2487 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2488 if (GetInitVarDecl(CE->getArg(0)) == Var)
2489 return SetStep(CE->getArg(1), !IsAdd);
2490 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2491 return SetStep(CE->getArg(0), false);
2494 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2495 << RHS->getSourceRange() << Var;
2499 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2500 // Check incr-expr for canonical loop form and return true if it
2501 // does not conform.
2502 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2514 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2517 IncrementSrcRange = S->getSourceRange();
2518 S = S->IgnoreParens();
2519 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2520 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2522 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2523 (UO->isDecrementOp() ? -1 : 1)).get(),
2525 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2526 switch (BO->getOpcode()) {
2529 if (GetInitVarDecl(BO->getLHS()) == Var)
2530 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2533 if (GetInitVarDecl(BO->getLHS()) == Var)
2534 return CheckIncRHS(BO->getRHS());
2539 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2540 switch (CE->getOperator()) {
2543 if (GetInitVarDecl(CE->getArg(0)) == Var)
2545 SemaRef.ActOnIntegerConstant(
2547 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2552 if (GetInitVarDecl(CE->getArg(0)) == Var)
2553 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2556 if (GetInitVarDecl(CE->getArg(0)) == Var)
2557 return CheckIncRHS(CE->getArg(1));
2563 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2564 << S->getSourceRange() << Var;
2568 /// \brief Build the expression to calculate the number of iterations.
2570 OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2571 const bool LimitedType) const {
2573 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2574 SemaRef.getLangOpts().CPlusPlus) {
2576 Expr *Upper = TestIsLessOp ? UB : LB;
2577 Expr *Lower = TestIsLessOp ? LB : UB;
2579 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2581 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2582 // BuildBinOp already emitted error, this one is to point user to upper
2583 // and lower bound, and to tell what is passed to 'operator-'.
2584 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2585 << Upper->getSourceRange() << Lower->getSourceRange();
2590 if (!Diff.isUsable())
2593 // Upper - Lower [- 1]
2595 Diff = SemaRef.BuildBinOp(
2596 S, DefaultLoc, BO_Sub, Diff.get(),
2597 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2598 if (!Diff.isUsable())
2601 // Upper - Lower [- 1] + Step
2602 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2603 Step->IgnoreImplicit());
2604 if (!Diff.isUsable())
2607 // Parentheses (for dumping/debugging purposes only).
2608 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2609 if (!Diff.isUsable())
2612 // (Upper - Lower [- 1] + Step) / Step
2613 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2614 Step->IgnoreImplicit());
2615 if (!Diff.isUsable())
2618 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2620 auto &C = SemaRef.Context;
2621 QualType Type = Diff.get()->getType();
2622 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2623 if (NewSize != C.getTypeSize(Type)) {
2624 if (NewSize < C.getTypeSize(Type)) {
2625 assert(NewSize == 64 && "incorrect loop var size");
2626 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2627 << InitSrcRange << ConditionSrcRange;
2629 QualType NewType = C.getIntTypeForBitwidth(
2630 NewSize, Type->hasSignedIntegerRepresentation());
2631 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2632 Sema::AA_Converting, true);
2633 if (!Diff.isUsable())
2641 Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2642 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2643 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2644 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2645 auto CondExpr = SemaRef.BuildBinOp(
2646 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2647 : (TestIsStrictOp ? BO_GT : BO_GE),
2649 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2650 // Otherwise use original loop conditon and evaluate it in runtime.
2651 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2654 /// \brief Build reference expression to the counter be used for codegen.
2655 Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2656 return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc);
2659 /// \brief Build initization of the counter be used for codegen.
2660 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2662 /// \brief Build step of the counter be used for codegen.
2663 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2665 /// \brief Iteration space of a single for loop.
2666 struct LoopIterationSpace {
2667 /// \brief Condition of the loop.
2669 /// \brief This expression calculates the number of iterations in the loop.
2670 /// It is always possible to calculate it before starting the loop.
2671 Expr *NumIterations;
2672 /// \brief The loop counter variable.
2674 /// \brief This is initializer for the initial value of #CounterVar.
2676 /// \brief This is step for the #CounterVar used to generate its update:
2677 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2679 /// \brief Should step be subtracted?
2681 /// \brief Source range of the loop init.
2682 SourceRange InitSrcRange;
2683 /// \brief Source range of the loop condition.
2684 SourceRange CondSrcRange;
2685 /// \brief Source range of the loop increment.
2686 SourceRange IncSrcRange;
2691 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2692 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2693 assert(Init && "Expected loop in canonical form.");
2694 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2695 if (CollapseIteration > 0 &&
2696 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2697 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2698 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2699 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2701 DSAStack->setCollapseNumber(CollapseIteration - 1);
2705 /// \brief Called on a for stmt to check and extract its iteration space
2706 /// for further processing (such as collapsing).
2707 static bool CheckOpenMPIterationSpace(
2708 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2709 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2710 Expr *NestedLoopCountExpr,
2711 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2712 LoopIterationSpace &ResultIterSpace) {
2713 // OpenMP [2.6, Canonical Loop Form]
2714 // for (init-expr; test-expr; incr-expr) structured-block
2715 auto For = dyn_cast_or_null<ForStmt>(S);
2717 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
2718 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2719 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2720 << CurrentNestedLoopCount;
2721 if (NestedLoopCount > 1)
2722 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2723 diag::note_omp_collapse_expr)
2724 << NestedLoopCountExpr->getSourceRange();
2727 assert(For->getBody());
2729 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2732 auto Init = For->getInit();
2733 if (ISC.CheckInit(Init)) {
2737 bool HasErrors = false;
2739 // Check loop variable's type.
2740 auto Var = ISC.GetLoopVar();
2742 // OpenMP [2.6, Canonical Loop Form]
2743 // Var is one of the following:
2744 // A variable of signed or unsigned integer type.
2745 // For C++, a variable of a random access iterator type.
2746 // For C, a variable of a pointer type.
2747 auto VarType = Var->getType();
2748 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2749 !VarType->isPointerType() &&
2750 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2751 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2752 << SemaRef.getLangOpts().CPlusPlus;
2756 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2758 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2759 // parallel for construct is (are) private.
2760 // The loop iteration variable in the associated for-loop of a simd construct
2761 // with just one associated for-loop is linear with a constant-linear-step
2762 // that is the increment of the associated for-loop.
2763 // Exclude loop var from the list of variables with implicitly defined data
2764 // sharing attributes.
2765 VarsWithImplicitDSA.erase(Var);
2767 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2768 // a Construct, C/C++].
2769 // The loop iteration variable in the associated for-loop of a simd construct
2770 // with just one associated for-loop may be listed in a linear clause with a
2771 // constant-linear-step that is the increment of the associated for-loop.
2772 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2773 // parallel for construct may be listed in a private or lastprivate clause.
2774 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
2775 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2776 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2777 // declared in the loop and it is predetermined as a private.
2778 auto PredeterminedCKind =
2779 isOpenMPSimdDirective(DKind)
2780 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2782 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
2783 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
2784 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2785 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2786 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2787 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2788 DVar.RefExpr != nullptr)) {
2789 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
2790 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2791 << getOpenMPClauseName(PredeterminedCKind);
2792 if (DVar.RefExpr == nullptr)
2793 DVar.CKind = PredeterminedCKind;
2794 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
2796 } else if (LoopVarRefExpr != nullptr) {
2797 // Make the loop iteration variable private (for worksharing constructs),
2798 // linear (for simd directives with the only one associated loop) or
2799 // lastprivate (for simd directives with several collapsed loops).
2800 if (DVar.CKind == OMPC_unknown)
2801 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2802 /*FromParent=*/false);
2803 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
2806 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
2809 HasErrors |= ISC.CheckCond(For->getCond());
2812 HasErrors |= ISC.CheckInc(For->getInc());
2814 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
2817 // Build the loop's iteration space representation.
2818 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
2819 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2820 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
2821 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2822 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2823 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2824 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2825 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2826 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2827 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2829 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2830 ResultIterSpace.NumIterations == nullptr ||
2831 ResultIterSpace.CounterVar == nullptr ||
2832 ResultIterSpace.CounterInit == nullptr ||
2833 ResultIterSpace.CounterStep == nullptr);
2838 /// \brief Build 'VarRef = Start + Iter * Step'.
2839 static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2840 SourceLocation Loc, ExprResult VarRef,
2841 ExprResult Start, ExprResult Iter,
2842 ExprResult Step, bool Subtract) {
2843 // Add parentheses (for debugging purposes only).
2844 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2845 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2849 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2850 Step.get()->IgnoreImplicit());
2851 if (!Update.isUsable())
2854 // Build 'VarRef = Start + Iter * Step'.
2855 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2856 Start.get()->IgnoreImplicit(), Update.get());
2857 if (!Update.isUsable())
2860 Update = SemaRef.PerformImplicitConversion(
2861 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2862 if (!Update.isUsable())
2865 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2869 /// \brief Convert integer expression \a E to make it have at least \a Bits
2871 static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2875 auto &C = SemaRef.Context;
2876 QualType OldType = E->getType();
2877 unsigned HasBits = C.getTypeSize(OldType);
2878 if (HasBits >= Bits)
2879 return ExprResult(E);
2880 // OK to convert to signed, because new type has more bits than old.
2881 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2882 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2886 /// \brief Check if the given expression \a E is a constant integer that fits
2887 /// into \a Bits bits.
2888 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2891 llvm::APSInt Result;
2892 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2893 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2897 /// \brief Called on a for stmt to check itself and nested loops (if any).
2898 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2899 /// number of collapsed loops otherwise.
2901 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2902 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2903 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2904 OMPLoopDirective::HelperExprs &Built) {
2905 unsigned NestedLoopCount = 1;
2906 if (NestedLoopCountExpr) {
2907 // Found 'collapse' clause - calculate collapse number.
2908 llvm::APSInt Result;
2909 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2910 NestedLoopCount = Result.getLimitedValue();
2912 // This is helper routine for loop directives (e.g., 'for', 'simd',
2913 // 'for simd', etc.).
2914 SmallVector<LoopIterationSpace, 4> IterSpaces;
2915 IterSpaces.resize(NestedLoopCount);
2916 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
2917 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
2918 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
2919 NestedLoopCount, NestedLoopCountExpr,
2920 VarsWithImplicitDSA, IterSpaces[Cnt]))
2922 // Move on to the next nested for loop, or to the loop body.
2923 // OpenMP [2.8.1, simd construct, Restrictions]
2924 // All loops associated with the construct must be perfectly nested; that
2925 // is, there must be no intervening code nor any OpenMP directive between
2927 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
2930 Built.clear(/* size */ NestedLoopCount);
2932 if (SemaRef.CurContext->isDependentContext())
2933 return NestedLoopCount;
2935 // An example of what is generated for the following code:
2937 // #pragma omp simd collapse(2)
2938 // for (i = 0; i < NI; ++i)
2939 // for (j = J0; j < NJ; j+=2) {
2943 // We generate the code below.
2944 // Note: the loop body may be outlined in CodeGen.
2945 // Note: some counters may be C++ classes, operator- is used to find number of
2946 // iterations and operator+= to calculate counter value.
2947 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2948 // or i64 is currently supported).
2950 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2951 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2952 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2953 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2954 // // similar updates for vars in clauses (e.g. 'linear')
2955 // <loop body (using local i and j)>
2957 // i = NI; // assign final values of counters
2961 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2962 // the iteration counts of the collapsed for loops.
2963 // Precondition tests if there is at least one iteration (all conditions are
2965 auto PreCond = ExprResult(IterSpaces[0].PreCond);
2966 auto N0 = IterSpaces[0].NumIterations;
2967 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2968 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2970 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2971 return NestedLoopCount;
2973 auto &C = SemaRef.Context;
2974 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2976 Scope *CurScope = DSA.getCurScope();
2977 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2978 if (PreCond.isUsable()) {
2979 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
2980 PreCond.get(), IterSpaces[Cnt].PreCond);
2982 auto N = IterSpaces[Cnt].NumIterations;
2983 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2984 if (LastIteration32.isUsable())
2985 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2986 LastIteration32.get(), N);
2987 if (LastIteration64.isUsable())
2988 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2989 LastIteration64.get(), N);
2992 // Choose either the 32-bit or 64-bit version.
2993 ExprResult LastIteration = LastIteration64;
2994 if (LastIteration32.isUsable() &&
2995 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2996 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2999 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3000 LastIteration64.get(), SemaRef)))
3001 LastIteration = LastIteration32;
3003 if (!LastIteration.isUsable())
3006 // Save the number of iterations.
3007 ExprResult NumIterations = LastIteration;
3009 LastIteration = SemaRef.BuildBinOp(
3010 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3011 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3012 if (!LastIteration.isUsable())
3016 // Calculate the last iteration number beforehand instead of doing this on
3017 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3018 llvm::APSInt Result;
3020 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3021 ExprResult CalcLastIteration;
3023 SourceLocation SaveLoc;
3025 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
3026 ".omp.last.iteration");
3027 ExprResult SaveRef = buildDeclRefExpr(
3028 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
3029 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3030 SaveRef.get(), LastIteration.get());
3031 LastIteration = SaveRef;
3033 // Prepare SaveRef + 1.
3034 NumIterations = SemaRef.BuildBinOp(
3035 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3036 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3037 if (!NumIterations.isUsable())
3041 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3043 QualType VType = LastIteration.get()->getType();
3044 // Build variables passed into runtime, nesessary for worksharing directives.
3045 ExprResult LB, UB, IL, ST, EUB;
3046 if (isOpenMPWorksharingDirective(DKind)) {
3047 // Lower bound variable, initialized with zero.
3048 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3049 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
3050 SemaRef.AddInitializerToDecl(
3051 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3052 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3054 // Upper bound variable, initialized with last iteration number.
3055 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3056 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
3057 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3058 /*DirectInit*/ false,
3059 /*TypeMayContainAuto*/ false);
3061 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3062 // This will be used to implement clause 'lastprivate'.
3063 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
3064 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3065 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
3066 SemaRef.AddInitializerToDecl(
3067 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3068 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3070 // Stride variable returned by runtime (we initialize it to 1 by default).
3071 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3072 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
3073 SemaRef.AddInitializerToDecl(
3074 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3075 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3077 // Build expression: UB = min(UB, LastIteration)
3078 // It is nesessary for CodeGen of directives with static scheduling.
3079 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3080 UB.get(), LastIteration.get());
3081 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3082 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3083 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3085 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3088 // Build the iteration variable and its initialization before loop.
3092 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3093 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
3094 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3096 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3097 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3098 Init = SemaRef.ActOnFinishFullExpr(Init.get());
3101 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
3102 SourceLocation CondLoc;
3104 isOpenMPWorksharingDirective(DKind)
3105 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3106 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3107 NumIterations.get());
3109 // Loop increment (IV = IV + 1)
3110 SourceLocation IncLoc;
3112 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3113 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3114 if (!Inc.isUsable())
3116 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
3117 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3118 if (!Inc.isUsable())
3121 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3122 // Used for directives with static scheduling.
3123 ExprResult NextLB, NextUB;
3124 if (isOpenMPWorksharingDirective(DKind)) {
3126 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3127 if (!NextLB.isUsable())
3131 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3132 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3133 if (!NextLB.isUsable())
3136 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3137 if (!NextUB.isUsable())
3141 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3142 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3143 if (!NextUB.isUsable())
3147 // Build updates and final values of the loop counters.
3148 bool HasErrors = false;
3149 Built.Counters.resize(NestedLoopCount);
3150 Built.Updates.resize(NestedLoopCount);
3151 Built.Finals.resize(NestedLoopCount);
3154 // Go from inner nested loop to outer.
3155 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3156 LoopIterationSpace &IS = IterSpaces[Cnt];
3157 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3158 // Build: Iter = (IV / Div) % IS.NumIters
3159 // where Div is product of previous iterations' IS.NumIters.
3161 if (Div.isUsable()) {
3163 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3166 assert((Cnt == (int)NestedLoopCount - 1) &&
3167 "unusable div expected on first iteration only");
3170 if (Cnt != 0 && Iter.isUsable())
3171 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3173 if (!Iter.isUsable()) {
3178 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3179 auto *CounterVar = buildDeclRefExpr(
3180 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3181 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3182 /*RefersToCapture=*/true);
3184 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
3185 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3186 if (!Update.isUsable()) {
3191 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3192 ExprResult Final = BuildCounterUpdate(
3193 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
3194 IS.NumIterations, IS.CounterStep, IS.Subtract);
3195 if (!Final.isUsable()) {
3200 // Build Div for the next iteration: Div <- Div * IS.NumIters
3203 Div = IS.NumIterations;
3205 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3208 // Add parentheses (for debugging purposes only).
3210 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3211 if (!Div.isUsable()) {
3216 if (!Update.isUsable() || !Final.isUsable()) {
3221 Built.Counters[Cnt] = IS.CounterVar;
3222 Built.Updates[Cnt] = Update.get();
3223 Built.Finals[Cnt] = Final.get();
3231 Built.IterationVarRef = IV.get();
3232 Built.LastIteration = LastIteration.get();
3233 Built.NumIterations = NumIterations.get();
3234 Built.CalcLastIteration = CalcLastIteration.get();
3235 Built.PreCond = PreCond.get();
3236 Built.Cond = Cond.get();
3237 Built.Init = Init.get();
3238 Built.Inc = Inc.get();
3239 Built.LB = LB.get();
3240 Built.UB = UB.get();
3241 Built.IL = IL.get();
3242 Built.ST = ST.get();
3243 Built.EUB = EUB.get();
3244 Built.NLB = NextLB.get();
3245 Built.NUB = NextUB.get();
3247 return NestedLoopCount;
3250 static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
3251 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
3252 return C->getClauseKind() == OMPC_collapse;
3254 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
3255 Clauses, std::move(CollapseFilter));
3257 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3261 StmtResult Sema::ActOnOpenMPSimdDirective(
3262 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3263 SourceLocation EndLoc,
3264 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3265 OMPLoopDirective::HelperExprs B;
3266 // In presence of clause 'collapse', it will define the nested loops number.
3267 unsigned NestedLoopCount =
3268 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
3269 *DSAStack, VarsWithImplicitDSA, B);
3270 if (NestedLoopCount == 0)
3273 assert((CurContext->isDependentContext() || B.builtAll()) &&
3274 "omp simd loop exprs were not built");
3276 if (!CurContext->isDependentContext()) {
3277 // Finalize the clauses that need pre-built expressions for CodeGen.
3278 for (auto C : Clauses) {
3279 if (auto LC = dyn_cast<OMPLinearClause>(C))
3280 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3281 B.NumIterations, *this, CurScope))
3286 getCurFunction()->setHasBranchProtectedScope();
3287 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3291 StmtResult Sema::ActOnOpenMPForDirective(
3292 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3293 SourceLocation EndLoc,
3294 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3295 OMPLoopDirective::HelperExprs B;
3296 // In presence of clause 'collapse', it will define the nested loops number.
3297 unsigned NestedLoopCount =
3298 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
3299 *DSAStack, VarsWithImplicitDSA, B);
3300 if (NestedLoopCount == 0)
3303 assert((CurContext->isDependentContext() || B.builtAll()) &&
3304 "omp for loop exprs were not built");
3306 getCurFunction()->setHasBranchProtectedScope();
3307 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3311 StmtResult Sema::ActOnOpenMPForSimdDirective(
3312 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3313 SourceLocation EndLoc,
3314 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3315 OMPLoopDirective::HelperExprs B;
3316 // In presence of clause 'collapse', it will define the nested loops number.
3317 unsigned NestedLoopCount =
3318 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
3319 *this, *DSAStack, VarsWithImplicitDSA, B);
3320 if (NestedLoopCount == 0)
3323 assert((CurContext->isDependentContext() || B.builtAll()) &&
3324 "omp for simd loop exprs were not built");
3326 if (!CurContext->isDependentContext()) {
3327 // Finalize the clauses that need pre-built expressions for CodeGen.
3328 for (auto C : Clauses) {
3329 if (auto LC = dyn_cast<OMPLinearClause>(C))
3330 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3331 B.NumIterations, *this, CurScope))
3336 getCurFunction()->setHasBranchProtectedScope();
3337 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3341 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3343 SourceLocation StartLoc,
3344 SourceLocation EndLoc) {
3345 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3346 auto BaseStmt = AStmt;
3347 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3348 BaseStmt = CS->getCapturedStmt();
3349 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3350 auto S = C->children();
3353 // All associated statements must be '#pragma omp section' except for
3355 for (Stmt *SectionStmt : ++S) {
3356 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3358 Diag(SectionStmt->getLocStart(),
3359 diag::err_omp_sections_substmt_not_section);
3364 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3368 getCurFunction()->setHasBranchProtectedScope();
3370 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3374 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3375 SourceLocation StartLoc,
3376 SourceLocation EndLoc) {
3377 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3379 getCurFunction()->setHasBranchProtectedScope();
3381 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3384 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3386 SourceLocation StartLoc,
3387 SourceLocation EndLoc) {
3388 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3390 getCurFunction()->setHasBranchProtectedScope();
3392 // OpenMP [2.7.3, single Construct, Restrictions]
3393 // The copyprivate clause must not be used with the nowait clause.
3394 OMPClause *Nowait = nullptr;
3395 OMPClause *Copyprivate = nullptr;
3396 for (auto *Clause : Clauses) {
3397 if (Clause->getClauseKind() == OMPC_nowait)
3399 else if (Clause->getClauseKind() == OMPC_copyprivate)
3400 Copyprivate = Clause;
3401 if (Copyprivate && Nowait) {
3402 Diag(Copyprivate->getLocStart(),
3403 diag::err_omp_single_copyprivate_with_nowait);
3404 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3409 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3412 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3413 SourceLocation StartLoc,
3414 SourceLocation EndLoc) {
3415 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3417 getCurFunction()->setHasBranchProtectedScope();
3419 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3423 Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3424 Stmt *AStmt, SourceLocation StartLoc,
3425 SourceLocation EndLoc) {
3426 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3428 getCurFunction()->setHasBranchProtectedScope();
3430 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3434 StmtResult Sema::ActOnOpenMPParallelForDirective(
3435 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3436 SourceLocation EndLoc,
3437 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3438 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3439 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3440 // 1.2.2 OpenMP Language Terminology
3441 // Structured block - An executable statement with a single entry at the
3442 // top and a single exit at the bottom.
3443 // The point of exit cannot be a branch out of the structured block.
3444 // longjmp() and throw() must not violate the entry/exit criteria.
3445 CS->getCapturedDecl()->setNothrow();
3447 OMPLoopDirective::HelperExprs B;
3448 // In presence of clause 'collapse', it will define the nested loops number.
3449 unsigned NestedLoopCount =
3450 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
3451 *this, *DSAStack, VarsWithImplicitDSA, B);
3452 if (NestedLoopCount == 0)
3455 assert((CurContext->isDependentContext() || B.builtAll()) &&
3456 "omp parallel for loop exprs were not built");
3458 getCurFunction()->setHasBranchProtectedScope();
3459 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3460 NestedLoopCount, Clauses, AStmt, B);
3463 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3464 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3465 SourceLocation EndLoc,
3466 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3467 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3468 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3469 // 1.2.2 OpenMP Language Terminology
3470 // Structured block - An executable statement with a single entry at the
3471 // top and a single exit at the bottom.
3472 // The point of exit cannot be a branch out of the structured block.
3473 // longjmp() and throw() must not violate the entry/exit criteria.
3474 CS->getCapturedDecl()->setNothrow();
3476 OMPLoopDirective::HelperExprs B;
3477 // In presence of clause 'collapse', it will define the nested loops number.
3478 unsigned NestedLoopCount =
3479 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
3480 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
3481 if (NestedLoopCount == 0)
3484 if (!CurContext->isDependentContext()) {
3485 // Finalize the clauses that need pre-built expressions for CodeGen.
3486 for (auto C : Clauses) {
3487 if (auto LC = dyn_cast<OMPLinearClause>(C))
3488 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3489 B.NumIterations, *this, CurScope))
3494 getCurFunction()->setHasBranchProtectedScope();
3495 return OMPParallelForSimdDirective::Create(
3496 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
3500 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3501 Stmt *AStmt, SourceLocation StartLoc,
3502 SourceLocation EndLoc) {
3503 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3504 auto BaseStmt = AStmt;
3505 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3506 BaseStmt = CS->getCapturedStmt();
3507 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3508 auto S = C->children();
3511 // All associated statements must be '#pragma omp section' except for
3513 for (Stmt *SectionStmt : ++S) {
3514 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3516 Diag(SectionStmt->getLocStart(),
3517 diag::err_omp_parallel_sections_substmt_not_section);
3522 Diag(AStmt->getLocStart(),
3523 diag::err_omp_parallel_sections_not_compound_stmt);
3527 getCurFunction()->setHasBranchProtectedScope();
3529 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3533 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3534 Stmt *AStmt, SourceLocation StartLoc,
3535 SourceLocation EndLoc) {
3536 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3537 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3538 // 1.2.2 OpenMP Language Terminology
3539 // Structured block - An executable statement with a single entry at the
3540 // top and a single exit at the bottom.
3541 // The point of exit cannot be a branch out of the structured block.
3542 // longjmp() and throw() must not violate the entry/exit criteria.
3543 CS->getCapturedDecl()->setNothrow();
3545 getCurFunction()->setHasBranchProtectedScope();
3547 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3550 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3551 SourceLocation EndLoc) {
3552 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3555 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3556 SourceLocation EndLoc) {
3557 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3560 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3561 SourceLocation EndLoc) {
3562 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3565 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3566 SourceLocation StartLoc,
3567 SourceLocation EndLoc) {
3568 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3570 getCurFunction()->setHasBranchProtectedScope();
3572 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3575 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3576 SourceLocation StartLoc,
3577 SourceLocation EndLoc) {
3578 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3579 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3582 StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3583 SourceLocation StartLoc,
3584 SourceLocation EndLoc) {
3585 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3587 getCurFunction()->setHasBranchProtectedScope();
3589 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3593 /// \brief Helper class for checking expression in 'omp atomic [update]'
3595 class OpenMPAtomicUpdateChecker {
3596 /// \brief Error results for atomic update expressions.
3597 enum ExprAnalysisErrorCode {
3598 /// \brief A statement is not an expression statement.
3600 /// \brief Expression is not builtin binary or unary operation.
3601 NotABinaryOrUnaryExpression,
3602 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3603 NotAnUnaryIncDecExpression,
3604 /// \brief An expression is not of scalar type.
3606 /// \brief A binary operation is not an assignment operation.
3608 /// \brief RHS part of the binary operation is not a binary expression.
3609 NotABinaryExpression,
3610 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3613 /// \brief RHS binary operation does not have reference to the updated LHS
3615 NotAnUpdateExpression,
3616 /// \brief No errors is found.
3619 /// \brief Reference to Sema.
3621 /// \brief A location for note diagnostics (when error is found).
3622 SourceLocation NoteLoc;
3623 /// \brief 'x' lvalue part of the source atomic expression.
3625 /// \brief 'expr' rvalue part of the source atomic expression.
3627 /// \brief Helper expression of the form
3628 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3629 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3631 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3632 /// important for non-associative operations.
3633 bool IsXLHSInRHSPart;
3634 BinaryOperatorKind Op;
3635 SourceLocation OpLoc;
3636 /// \brief true if the source expression is a postfix unary operation, false
3637 /// if it is a prefix unary operation.
3638 bool IsPostfixUpdate;
3641 OpenMPAtomicUpdateChecker(Sema &SemaRef)
3642 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
3643 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
3644 /// \brief Check specified statement that it is suitable for 'atomic update'
3645 /// constructs and extract 'x', 'expr' and Operation from the original
3646 /// expression. If DiagId and NoteId == 0, then only check is performed
3647 /// without error notification.
3648 /// \param DiagId Diagnostic which should be emitted if error is found.
3649 /// \param NoteId Diagnostic note for the main error message.
3650 /// \return true if statement is not an update expression, false otherwise.
3651 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
3652 /// \brief Return the 'x' lvalue part of the source atomic expression.
3653 Expr *getX() const { return X; }
3654 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3655 Expr *getExpr() const { return E; }
3656 /// \brief Return the update expression used in calculation of the updated
3657 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3658 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3659 Expr *getUpdateExpr() const { return UpdateExpr; }
3660 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3661 /// false otherwise.
3662 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3664 /// \brief true if the source expression is a postfix unary operation, false
3665 /// if it is a prefix unary operation.
3666 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3669 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3670 unsigned NoteId = 0);
3674 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3675 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3676 ExprAnalysisErrorCode ErrorFound = NoError;
3677 SourceLocation ErrorLoc, NoteLoc;
3678 SourceRange ErrorRange, NoteRange;
3679 // Allowed constructs are:
3680 // x = x binop expr;
3681 // x = expr binop x;
3682 if (AtomicBinOp->getOpcode() == BO_Assign) {
3683 X = AtomicBinOp->getLHS();
3684 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3685 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3686 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3687 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3688 AtomicInnerBinOp->isBitwiseOp()) {
3689 Op = AtomicInnerBinOp->getOpcode();
3690 OpLoc = AtomicInnerBinOp->getOperatorLoc();
3691 auto *LHS = AtomicInnerBinOp->getLHS();
3692 auto *RHS = AtomicInnerBinOp->getRHS();
3693 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3694 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3695 /*Canonical=*/true);
3696 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3697 /*Canonical=*/true);
3698 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3699 /*Canonical=*/true);
3702 IsXLHSInRHSPart = true;
3703 } else if (XId == RHSId) {
3705 IsXLHSInRHSPart = false;
3707 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3708 ErrorRange = AtomicInnerBinOp->getSourceRange();
3709 NoteLoc = X->getExprLoc();
3710 NoteRange = X->getSourceRange();
3711 ErrorFound = NotAnUpdateExpression;
3714 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3715 ErrorRange = AtomicInnerBinOp->getSourceRange();
3716 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3717 NoteRange = SourceRange(NoteLoc, NoteLoc);
3718 ErrorFound = NotABinaryOperator;
3721 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3722 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3723 ErrorFound = NotABinaryExpression;
3726 ErrorLoc = AtomicBinOp->getExprLoc();
3727 ErrorRange = AtomicBinOp->getSourceRange();
3728 NoteLoc = AtomicBinOp->getOperatorLoc();
3729 NoteRange = SourceRange(NoteLoc, NoteLoc);
3730 ErrorFound = NotAnAssignmentOp;
3732 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
3733 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3734 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3736 } else if (SemaRef.CurContext->isDependentContext())
3737 E = X = UpdateExpr = nullptr;
3738 return ErrorFound != NoError;
3741 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3743 ExprAnalysisErrorCode ErrorFound = NoError;
3744 SourceLocation ErrorLoc, NoteLoc;
3745 SourceRange ErrorRange, NoteRange;
3746 // Allowed constructs are:
3752 // x = x binop expr;
3753 // x = expr binop x;
3754 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3755 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3756 if (AtomicBody->getType()->isScalarType() ||
3757 AtomicBody->isInstantiationDependent()) {
3758 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3759 AtomicBody->IgnoreParenImpCasts())) {
3760 // Check for Compound Assignment Operation
3761 Op = BinaryOperator::getOpForCompoundAssignment(
3762 AtomicCompAssignOp->getOpcode());
3763 OpLoc = AtomicCompAssignOp->getOperatorLoc();
3764 E = AtomicCompAssignOp->getRHS();
3765 X = AtomicCompAssignOp->getLHS();
3766 IsXLHSInRHSPart = true;
3767 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3768 AtomicBody->IgnoreParenImpCasts())) {
3769 // Check for Binary Operation
3770 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3772 } else if (auto *AtomicUnaryOp =
3773 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3774 // Check for Unary Operation
3775 if (AtomicUnaryOp->isIncrementDecrementOp()) {
3776 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
3777 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3778 OpLoc = AtomicUnaryOp->getOperatorLoc();
3779 X = AtomicUnaryOp->getSubExpr();
3780 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3781 IsXLHSInRHSPart = true;
3783 ErrorFound = NotAnUnaryIncDecExpression;
3784 ErrorLoc = AtomicUnaryOp->getExprLoc();
3785 ErrorRange = AtomicUnaryOp->getSourceRange();
3786 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3787 NoteRange = SourceRange(NoteLoc, NoteLoc);
3790 ErrorFound = NotABinaryOrUnaryExpression;
3791 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3792 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3795 ErrorFound = NotAScalarType;
3796 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3797 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3800 ErrorFound = NotAnExpression;
3801 NoteLoc = ErrorLoc = S->getLocStart();
3802 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3804 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
3805 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3806 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3808 } else if (SemaRef.CurContext->isDependentContext())
3809 E = X = UpdateExpr = nullptr;
3810 if (ErrorFound == NoError && E && X) {
3811 // Build an update expression of form 'OpaqueValueExpr(x) binop
3812 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3813 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3814 auto *OVEX = new (SemaRef.getASTContext())
3815 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3816 auto *OVEExpr = new (SemaRef.getASTContext())
3817 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3819 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3820 IsXLHSInRHSPart ? OVEExpr : OVEX);
3821 if (Update.isInvalid())
3823 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3825 if (Update.isInvalid())
3827 UpdateExpr = Update.get();
3829 return ErrorFound != NoError;
3832 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3834 SourceLocation StartLoc,
3835 SourceLocation EndLoc) {
3836 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3837 auto CS = cast<CapturedStmt>(AStmt);
3838 // 1.2.2 OpenMP Language Terminology
3839 // Structured block - An executable statement with a single entry at the
3840 // top and a single exit at the bottom.
3841 // The point of exit cannot be a branch out of the structured block.
3842 // longjmp() and throw() must not violate the entry/exit criteria.
3843 OpenMPClauseKind AtomicKind = OMPC_unknown;
3844 SourceLocation AtomicKindLoc;
3845 for (auto *C : Clauses) {
3846 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
3847 C->getClauseKind() == OMPC_update ||
3848 C->getClauseKind() == OMPC_capture) {
3849 if (AtomicKind != OMPC_unknown) {
3850 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3851 << SourceRange(C->getLocStart(), C->getLocEnd());
3852 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3853 << getOpenMPClauseName(AtomicKind);
3855 AtomicKind = C->getClauseKind();
3856 AtomicKindLoc = C->getLocStart();
3861 auto Body = CS->getCapturedStmt();
3862 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3863 Body = EWC->getSubExpr();
3869 bool IsXLHSInRHSPart = false;
3870 bool IsPostfixUpdate = false;
3871 // OpenMP [2.12.6, atomic Construct]
3872 // In the next expressions:
3873 // * x and v (as applicable) are both l-value expressions with scalar type.
3874 // * During the execution of an atomic region, multiple syntactic
3875 // occurrences of x must designate the same storage location.
3876 // * Neither of v and expr (as applicable) may access the storage location
3878 // * Neither of x and expr (as applicable) may access the storage location
3880 // * expr is an expression with scalar type.
3881 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3882 // * binop, binop=, ++, and -- are not overloaded operators.
3883 // * The expression x binop expr must be numerically equivalent to x binop
3884 // (expr). This requirement is satisfied if the operators in expr have
3885 // precedence greater than binop, or by using parentheses around expr or
3886 // subexpressions of expr.
3887 // * The expression expr binop x must be numerically equivalent to (expr)
3888 // binop x. This requirement is satisfied if the operators in expr have
3889 // precedence equal to or greater than binop, or by using parentheses around
3890 // expr or subexpressions of expr.
3891 // * For forms that allow multiple occurrences of x, the number of times
3892 // that x is evaluated is unspecified.
3893 if (AtomicKind == OMPC_read) {
3900 } ErrorFound = NoError;
3901 SourceLocation ErrorLoc, NoteLoc;
3902 SourceRange ErrorRange, NoteRange;
3903 // If clause is read:
3905 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3907 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3908 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3909 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3910 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3911 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3912 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3913 if (!X->isLValue() || !V->isLValue()) {
3914 auto NotLValueExpr = X->isLValue() ? V : X;
3915 ErrorFound = NotAnLValue;
3916 ErrorLoc = AtomicBinOp->getExprLoc();
3917 ErrorRange = AtomicBinOp->getSourceRange();
3918 NoteLoc = NotLValueExpr->getExprLoc();
3919 NoteRange = NotLValueExpr->getSourceRange();
3921 } else if (!X->isInstantiationDependent() ||
3922 !V->isInstantiationDependent()) {
3923 auto NotScalarExpr =
3924 (X->isInstantiationDependent() || X->getType()->isScalarType())
3927 ErrorFound = NotAScalarType;
3928 ErrorLoc = AtomicBinOp->getExprLoc();
3929 ErrorRange = AtomicBinOp->getSourceRange();
3930 NoteLoc = NotScalarExpr->getExprLoc();
3931 NoteRange = NotScalarExpr->getSourceRange();
3934 ErrorFound = NotAnAssignmentOp;
3935 ErrorLoc = AtomicBody->getExprLoc();
3936 ErrorRange = AtomicBody->getSourceRange();
3937 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3938 : AtomicBody->getExprLoc();
3939 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3940 : AtomicBody->getSourceRange();
3943 ErrorFound = NotAnExpression;
3944 NoteLoc = ErrorLoc = Body->getLocStart();
3945 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3947 if (ErrorFound != NoError) {
3948 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3950 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3953 } else if (CurContext->isDependentContext())
3955 } else if (AtomicKind == OMPC_write) {
3962 } ErrorFound = NoError;
3963 SourceLocation ErrorLoc, NoteLoc;
3964 SourceRange ErrorRange, NoteRange;
3965 // If clause is write:
3967 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3969 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3970 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3971 X = AtomicBinOp->getLHS();
3972 E = AtomicBinOp->getRHS();
3973 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3974 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3975 if (!X->isLValue()) {
3976 ErrorFound = NotAnLValue;
3977 ErrorLoc = AtomicBinOp->getExprLoc();
3978 ErrorRange = AtomicBinOp->getSourceRange();
3979 NoteLoc = X->getExprLoc();
3980 NoteRange = X->getSourceRange();
3982 } else if (!X->isInstantiationDependent() ||
3983 !E->isInstantiationDependent()) {
3984 auto NotScalarExpr =
3985 (X->isInstantiationDependent() || X->getType()->isScalarType())
3988 ErrorFound = NotAScalarType;
3989 ErrorLoc = AtomicBinOp->getExprLoc();
3990 ErrorRange = AtomicBinOp->getSourceRange();
3991 NoteLoc = NotScalarExpr->getExprLoc();
3992 NoteRange = NotScalarExpr->getSourceRange();
3995 ErrorFound = NotAnAssignmentOp;
3996 ErrorLoc = AtomicBody->getExprLoc();
3997 ErrorRange = AtomicBody->getSourceRange();
3998 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3999 : AtomicBody->getExprLoc();
4000 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4001 : AtomicBody->getSourceRange();
4004 ErrorFound = NotAnExpression;
4005 NoteLoc = ErrorLoc = Body->getLocStart();
4006 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4008 if (ErrorFound != NoError) {
4009 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4011 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4014 } else if (CurContext->isDependentContext())
4016 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
4017 // If clause is update:
4023 // x = x binop expr;
4024 // x = expr binop x;
4025 OpenMPAtomicUpdateChecker Checker(*this);
4026 if (Checker.checkStatement(
4027 Body, (AtomicKind == OMPC_update)
4028 ? diag::err_omp_atomic_update_not_expression_statement
4029 : diag::err_omp_atomic_not_expression_statement,
4030 diag::note_omp_atomic_update))
4032 if (!CurContext->isDependentContext()) {
4033 E = Checker.getExpr();
4035 UE = Checker.getUpdateExpr();
4036 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4038 } else if (AtomicKind == OMPC_capture) {
4041 NotACompoundStatement,
4042 NotTwoSubstatements,
4043 NotASpecificExpression,
4045 } ErrorFound = NoError;
4046 SourceLocation ErrorLoc, NoteLoc;
4047 SourceRange ErrorRange, NoteRange;
4048 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4049 // If clause is a capture:
4054 // v = x binop= expr;
4055 // v = x = x binop expr;
4056 // v = x = expr binop x;
4058 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4059 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4060 V = AtomicBinOp->getLHS();
4061 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4062 OpenMPAtomicUpdateChecker Checker(*this);
4063 if (Checker.checkStatement(
4064 Body, diag::err_omp_atomic_capture_not_expression_statement,
4065 diag::note_omp_atomic_update))
4067 E = Checker.getExpr();
4069 UE = Checker.getUpdateExpr();
4070 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4071 IsPostfixUpdate = Checker.isPostfixUpdate();
4073 ErrorLoc = AtomicBody->getExprLoc();
4074 ErrorRange = AtomicBody->getSourceRange();
4075 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4076 : AtomicBody->getExprLoc();
4077 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4078 : AtomicBody->getSourceRange();
4079 ErrorFound = NotAnAssignmentOp;
4081 if (ErrorFound != NoError) {
4082 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4084 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4086 } else if (CurContext->isDependentContext()) {
4087 UE = V = E = X = nullptr;
4090 // If clause is a capture:
4091 // { v = x; x = expr; }
4096 // { v = x; x binop= expr; }
4097 // { v = x; x = x binop expr; }
4098 // { v = x; x = expr binop x; }
4103 // { x binop= expr; v = x; }
4104 // { x = x binop expr; v = x; }
4105 // { x = expr binop x; v = x; }
4106 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4107 // Check that this is { expr1; expr2; }
4108 if (CS->size() == 2) {
4109 auto *First = CS->body_front();
4110 auto *Second = CS->body_back();
4111 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4112 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4113 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4114 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4115 // Need to find what subexpression is 'v' and what is 'x'.
4116 OpenMPAtomicUpdateChecker Checker(*this);
4117 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4118 BinaryOperator *BinOp = nullptr;
4119 if (IsUpdateExprFound) {
4120 BinOp = dyn_cast<BinaryOperator>(First);
4121 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4123 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4128 // { v = x; x binop= expr; }
4129 // { v = x; x = x binop expr; }
4130 // { v = x; x = expr binop x; }
4131 // Check that the first expression has form v = x.
4132 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4133 llvm::FoldingSetNodeID XId, PossibleXId;
4134 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4135 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4136 IsUpdateExprFound = XId == PossibleXId;
4137 if (IsUpdateExprFound) {
4138 V = BinOp->getLHS();
4140 E = Checker.getExpr();
4141 UE = Checker.getUpdateExpr();
4142 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4143 IsPostfixUpdate = true;
4146 if (!IsUpdateExprFound) {
4147 IsUpdateExprFound = !Checker.checkStatement(First);
4149 if (IsUpdateExprFound) {
4150 BinOp = dyn_cast<BinaryOperator>(Second);
4151 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4153 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4158 // { x binop= expr; v = x; }
4159 // { x = x binop expr; v = x; }
4160 // { x = expr binop x; v = x; }
4161 // Check that the second expression has form v = x.
4162 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4163 llvm::FoldingSetNodeID XId, PossibleXId;
4164 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4165 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4166 IsUpdateExprFound = XId == PossibleXId;
4167 if (IsUpdateExprFound) {
4168 V = BinOp->getLHS();
4170 E = Checker.getExpr();
4171 UE = Checker.getUpdateExpr();
4172 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4173 IsPostfixUpdate = false;
4177 if (!IsUpdateExprFound) {
4178 // { v = x; x = expr; }
4179 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4180 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4181 ErrorFound = NotAnAssignmentOp;
4182 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4183 : First->getLocStart();
4184 NoteRange = ErrorRange = FirstBinOp
4185 ? FirstBinOp->getSourceRange()
4186 : SourceRange(ErrorLoc, ErrorLoc);
4188 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4189 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4190 ErrorFound = NotAnAssignmentOp;
4191 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4192 : Second->getLocStart();
4193 NoteRange = ErrorRange = SecondBinOp
4194 ? SecondBinOp->getSourceRange()
4195 : SourceRange(ErrorLoc, ErrorLoc);
4197 auto *PossibleXRHSInFirst =
4198 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4199 auto *PossibleXLHSInSecond =
4200 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4201 llvm::FoldingSetNodeID X1Id, X2Id;
4202 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4203 PossibleXLHSInSecond->Profile(X2Id, Context,
4204 /*Canonical=*/true);
4205 IsUpdateExprFound = X1Id == X2Id;
4206 if (IsUpdateExprFound) {
4207 V = FirstBinOp->getLHS();
4208 X = SecondBinOp->getLHS();
4209 E = SecondBinOp->getRHS();
4211 IsXLHSInRHSPart = false;
4212 IsPostfixUpdate = true;
4214 ErrorFound = NotASpecificExpression;
4215 ErrorLoc = FirstBinOp->getExprLoc();
4216 ErrorRange = FirstBinOp->getSourceRange();
4217 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4218 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4224 NoteLoc = ErrorLoc = Body->getLocStart();
4225 NoteRange = ErrorRange =
4226 SourceRange(Body->getLocStart(), Body->getLocStart());
4227 ErrorFound = NotTwoSubstatements;
4230 NoteLoc = ErrorLoc = Body->getLocStart();
4231 NoteRange = ErrorRange =
4232 SourceRange(Body->getLocStart(), Body->getLocStart());
4233 ErrorFound = NotACompoundStatement;
4235 if (ErrorFound != NoError) {
4236 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4238 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4240 } else if (CurContext->isDependentContext()) {
4241 UE = V = E = X = nullptr;
4246 getCurFunction()->setHasBranchProtectedScope();
4248 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4249 X, V, E, UE, IsXLHSInRHSPart,
4253 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4255 SourceLocation StartLoc,
4256 SourceLocation EndLoc) {
4257 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4259 // OpenMP [2.16, Nesting of Regions]
4260 // If specified, a teams construct must be contained within a target
4261 // construct. That target construct must contain no statements or directives
4262 // outside of the teams construct.
4263 if (DSAStack->hasInnerTeamsRegion()) {
4264 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4265 bool OMPTeamsFound = true;
4266 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4267 auto I = CS->body_begin();
4268 while (I != CS->body_end()) {
4269 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4270 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4271 OMPTeamsFound = false;
4276 assert(I != CS->body_end() && "Not found statement");
4279 if (!OMPTeamsFound) {
4280 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4281 Diag(DSAStack->getInnerTeamsRegionLoc(),
4282 diag::note_omp_nested_teams_construct_here);
4283 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4284 << isa<OMPExecutableDirective>(S);
4289 getCurFunction()->setHasBranchProtectedScope();
4291 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4294 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4295 Stmt *AStmt, SourceLocation StartLoc,
4296 SourceLocation EndLoc) {
4297 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4298 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4299 // 1.2.2 OpenMP Language Terminology
4300 // Structured block - An executable statement with a single entry at the
4301 // top and a single exit at the bottom.
4302 // The point of exit cannot be a branch out of the structured block.
4303 // longjmp() and throw() must not violate the entry/exit criteria.
4304 CS->getCapturedDecl()->setNothrow();
4306 getCurFunction()->setHasBranchProtectedScope();
4308 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4312 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4313 SourceLocation EndLoc,
4314 OpenMPDirectiveKind CancelRegion) {
4315 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4316 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4317 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4318 << getOpenMPDirectiveName(CancelRegion);
4321 if (DSAStack->isParentNowaitRegion()) {
4322 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4325 if (DSAStack->isParentOrderedRegion()) {
4326 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4329 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4333 StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4334 SourceLocation EndLoc,
4335 OpenMPDirectiveKind CancelRegion) {
4336 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4337 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4338 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4339 << getOpenMPDirectiveName(CancelRegion);
4342 if (DSAStack->isParentNowaitRegion()) {
4343 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4346 if (DSAStack->isParentOrderedRegion()) {
4347 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4350 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4353 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
4354 SourceLocation StartLoc,
4355 SourceLocation LParenLoc,
4356 SourceLocation EndLoc) {
4357 OMPClause *Res = nullptr;
4360 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4363 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4365 case OMPC_num_threads:
4366 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4369 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4372 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4375 case OMPC_proc_bind:
4378 case OMPC_firstprivate:
4379 case OMPC_lastprivate:
4381 case OMPC_reduction:
4385 case OMPC_copyprivate:
4389 case OMPC_mergeable:
4390 case OMPC_threadprivate:
4399 llvm_unreachable("Clause is not allowed.");
4404 OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
4405 SourceLocation LParenLoc,
4406 SourceLocation EndLoc) {
4407 Expr *ValExpr = Condition;
4408 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4409 !Condition->isInstantiationDependent() &&
4410 !Condition->containsUnexpandedParameterPack()) {
4411 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4412 Condition->getExprLoc(), Condition);
4413 if (Val.isInvalid())
4416 ValExpr = Val.get();
4419 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4422 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4423 SourceLocation StartLoc,
4424 SourceLocation LParenLoc,
4425 SourceLocation EndLoc) {
4426 Expr *ValExpr = Condition;
4427 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4428 !Condition->isInstantiationDependent() &&
4429 !Condition->containsUnexpandedParameterPack()) {
4430 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4431 Condition->getExprLoc(), Condition);
4432 if (Val.isInvalid())
4435 ValExpr = Val.get();
4438 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4440 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4445 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4447 IntConvertDiagnoser()
4448 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
4449 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4450 QualType T) override {
4451 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4453 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4454 QualType T) override {
4455 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4457 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4459 QualType ConvTy) override {
4460 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4462 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4463 QualType ConvTy) override {
4464 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
4465 << ConvTy->isEnumeralType() << ConvTy;
4467 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4468 QualType T) override {
4469 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4471 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4472 QualType ConvTy) override {
4473 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
4474 << ConvTy->isEnumeralType() << ConvTy;
4476 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4477 QualType) override {
4478 llvm_unreachable("conversion functions are permitted");
4481 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4484 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4485 SourceLocation StartLoc,
4486 SourceLocation LParenLoc,
4487 SourceLocation EndLoc) {
4488 Expr *ValExpr = NumThreads;
4489 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
4490 !NumThreads->containsUnexpandedParameterPack()) {
4491 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4493 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
4494 if (Val.isInvalid())
4497 ValExpr = Val.get();
4499 // OpenMP [2.5, Restrictions]
4500 // The num_threads expression must evaluate to a positive integer value.
4501 llvm::APSInt Result;
4502 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4503 !Result.isStrictlyPositive()) {
4504 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4505 << "num_threads" << NumThreads->getSourceRange();
4510 return new (Context)
4511 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4514 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4515 OpenMPClauseKind CKind) {
4518 if (E->isValueDependent() || E->isTypeDependent() ||
4519 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4521 llvm::APSInt Result;
4522 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4523 if (ICE.isInvalid())
4525 if (!Result.isStrictlyPositive()) {
4526 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4527 << getOpenMPClauseName(CKind) << E->getSourceRange();
4530 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4531 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4532 << E->getSourceRange();
4535 if (CKind == OMPC_collapse) {
4536 DSAStack->setCollapseNumber(Result.getExtValue());
4541 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4542 SourceLocation LParenLoc,
4543 SourceLocation EndLoc) {
4544 // OpenMP [2.8.1, simd construct, Description]
4545 // The parameter of the safelen clause must be a constant
4546 // positive integer expression.
4547 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4548 if (Safelen.isInvalid())
4550 return new (Context)
4551 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
4554 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4555 SourceLocation StartLoc,
4556 SourceLocation LParenLoc,
4557 SourceLocation EndLoc) {
4558 // OpenMP [2.7.1, loop construct, Description]
4559 // OpenMP [2.8.1, simd construct, Description]
4560 // OpenMP [2.9.6, distribute construct, Description]
4561 // The parameter of the collapse clause must be a constant
4562 // positive integer expression.
4563 ExprResult NumForLoopsResult =
4564 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4565 if (NumForLoopsResult.isInvalid())
4567 return new (Context)
4568 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
4571 OMPClause *Sema::ActOnOpenMPSimpleClause(
4572 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4573 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
4574 OMPClause *Res = nullptr;
4578 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4579 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
4581 case OMPC_proc_bind:
4582 Res = ActOnOpenMPProcBindClause(
4583 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4588 case OMPC_num_threads:
4593 case OMPC_firstprivate:
4594 case OMPC_lastprivate:
4596 case OMPC_reduction:
4600 case OMPC_copyprivate:
4604 case OMPC_mergeable:
4605 case OMPC_threadprivate:
4614 llvm_unreachable("Clause is not allowed.");
4619 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4620 SourceLocation KindKwLoc,
4621 SourceLocation StartLoc,
4622 SourceLocation LParenLoc,
4623 SourceLocation EndLoc) {
4624 if (Kind == OMPC_DEFAULT_unknown) {
4626 static_assert(OMPC_DEFAULT_unknown > 0,
4627 "OMPC_DEFAULT_unknown not greater than 0");
4628 std::string Sep(", ");
4629 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
4631 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4634 case OMPC_DEFAULT_unknown - 2:
4637 case OMPC_DEFAULT_unknown - 1:
4644 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
4645 << Values << getOpenMPClauseName(OMPC_default);
4649 case OMPC_DEFAULT_none:
4650 DSAStack->setDefaultDSANone(KindKwLoc);
4652 case OMPC_DEFAULT_shared:
4653 DSAStack->setDefaultDSAShared(KindKwLoc);
4655 case OMPC_DEFAULT_unknown:
4656 llvm_unreachable("Clause kind is not allowed.");
4659 return new (Context)
4660 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
4663 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4664 SourceLocation KindKwLoc,
4665 SourceLocation StartLoc,
4666 SourceLocation LParenLoc,
4667 SourceLocation EndLoc) {
4668 if (Kind == OMPC_PROC_BIND_unknown) {
4670 std::string Sep(", ");
4671 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4673 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4676 case OMPC_PROC_BIND_unknown - 2:
4679 case OMPC_PROC_BIND_unknown - 1:
4686 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
4687 << Values << getOpenMPClauseName(OMPC_proc_bind);
4690 return new (Context)
4691 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
4694 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4695 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4696 SourceLocation StartLoc, SourceLocation LParenLoc,
4697 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4698 SourceLocation EndLoc) {
4699 OMPClause *Res = nullptr;
4702 Res = ActOnOpenMPScheduleClause(
4703 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4704 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4708 case OMPC_num_threads:
4712 case OMPC_proc_bind:
4714 case OMPC_firstprivate:
4715 case OMPC_lastprivate:
4717 case OMPC_reduction:
4721 case OMPC_copyprivate:
4725 case OMPC_mergeable:
4726 case OMPC_threadprivate:
4735 llvm_unreachable("Clause is not allowed.");
4740 OMPClause *Sema::ActOnOpenMPScheduleClause(
4741 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4742 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4743 SourceLocation EndLoc) {
4744 if (Kind == OMPC_SCHEDULE_unknown) {
4746 std::string Sep(", ");
4747 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4749 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4752 case OMPC_SCHEDULE_unknown - 2:
4755 case OMPC_SCHEDULE_unknown - 1:
4762 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4763 << Values << getOpenMPClauseName(OMPC_schedule);
4766 Expr *ValExpr = ChunkSize;
4767 Expr *HelperValExpr = nullptr;
4769 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4770 !ChunkSize->isInstantiationDependent() &&
4771 !ChunkSize->containsUnexpandedParameterPack()) {
4772 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4774 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4775 if (Val.isInvalid())
4778 ValExpr = Val.get();
4780 // OpenMP [2.7.1, Restrictions]
4781 // chunk_size must be a loop invariant integer expression with a positive
4783 llvm::APSInt Result;
4784 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4785 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4786 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4787 << "schedule" << ChunkSize->getSourceRange();
4790 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4791 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4792 ChunkSize->getType(), ".chunk.");
4793 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4794 ChunkSize->getExprLoc(),
4795 /*RefersToCapture=*/true);
4796 HelperValExpr = ImpVarRef;
4801 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
4802 EndLoc, Kind, ValExpr, HelperValExpr);
4805 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4806 SourceLocation StartLoc,
4807 SourceLocation EndLoc) {
4808 OMPClause *Res = nullptr;
4811 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4814 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4817 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4819 case OMPC_mergeable:
4820 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4823 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4826 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4829 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4832 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4835 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4839 case OMPC_num_threads:
4844 case OMPC_firstprivate:
4845 case OMPC_lastprivate:
4847 case OMPC_reduction:
4851 case OMPC_copyprivate:
4853 case OMPC_proc_bind:
4854 case OMPC_threadprivate:
4858 llvm_unreachable("Clause is not allowed.");
4863 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4864 SourceLocation EndLoc) {
4865 DSAStack->setOrderedRegion();
4866 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
4869 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4870 SourceLocation EndLoc) {
4871 DSAStack->setNowaitRegion();
4872 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4875 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4876 SourceLocation EndLoc) {
4877 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4880 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4881 SourceLocation EndLoc) {
4882 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4885 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4886 SourceLocation EndLoc) {
4887 return new (Context) OMPReadClause(StartLoc, EndLoc);
4890 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4891 SourceLocation EndLoc) {
4892 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4895 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4896 SourceLocation EndLoc) {
4897 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4900 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4901 SourceLocation EndLoc) {
4902 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4905 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4906 SourceLocation EndLoc) {
4907 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4910 OMPClause *Sema::ActOnOpenMPVarListClause(
4911 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4912 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4913 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
4914 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
4915 SourceLocation DepLoc) {
4916 OMPClause *Res = nullptr;
4919 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4921 case OMPC_firstprivate:
4922 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4924 case OMPC_lastprivate:
4925 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4928 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4930 case OMPC_reduction:
4931 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4932 EndLoc, ReductionIdScopeSpec, ReductionId);
4935 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4939 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4943 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4945 case OMPC_copyprivate:
4946 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4949 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4952 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
4957 case OMPC_num_threads:
4961 case OMPC_proc_bind:
4966 case OMPC_mergeable:
4967 case OMPC_threadprivate:
4974 llvm_unreachable("Clause is not allowed.");
4979 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4980 SourceLocation StartLoc,
4981 SourceLocation LParenLoc,
4982 SourceLocation EndLoc) {
4983 SmallVector<Expr *, 8> Vars;
4984 SmallVector<Expr *, 8> PrivateCopies;
4985 for (auto &RefExpr : VarList) {
4986 assert(RefExpr && "NULL expr in OpenMP private clause.");
4987 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4988 // It will be analyzed later.
4989 Vars.push_back(RefExpr);
4990 PrivateCopies.push_back(nullptr);
4994 SourceLocation ELoc = RefExpr->getExprLoc();
4995 // OpenMP [2.1, C/C++]
4996 // A list item is a variable name.
4997 // OpenMP [2.9.3.3, Restrictions, p.1]
4998 // A variable that is part of another variable (as an array or
4999 // structure element) cannot appear in a private clause.
5000 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5001 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5002 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5005 Decl *D = DE->getDecl();
5006 VarDecl *VD = cast<VarDecl>(D);
5008 QualType Type = VD->getType();
5009 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5010 // It will be analyzed later.
5012 PrivateCopies.push_back(nullptr);
5016 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5017 // A variable that appears in a private clause must not have an incomplete
5018 // type or a reference type.
5019 if (RequireCompleteType(ELoc, Type,
5020 diag::err_omp_private_incomplete_type)) {
5023 if (Type->isReferenceType()) {
5024 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5025 << getOpenMPClauseName(OMPC_private) << Type;
5027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5028 Diag(VD->getLocation(),
5029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5034 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5036 // Variables with the predetermined data-sharing attributes may not be
5037 // listed in data-sharing attributes clauses, except for the cases
5038 // listed below. For these exceptions only, listing a predetermined
5039 // variable in a data-sharing attribute clause is allowed and overrides
5040 // the variable's predetermined data-sharing attributes.
5041 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5042 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
5043 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5044 << getOpenMPClauseName(OMPC_private);
5045 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5049 // Variably modified types are not supported for tasks.
5050 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
5051 DSAStack->getCurrentDirective() == OMPD_task) {
5052 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5053 << getOpenMPClauseName(OMPC_private) << Type
5054 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5056 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5057 Diag(VD->getLocation(),
5058 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5063 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5064 // A variable of class type (or array thereof) that appears in a private
5065 // clause requires an accessible, unambiguous default constructor for the
5067 // Generate helper private variable and initialize it with the default
5068 // value. The address of the original variable is replaced by the address of
5069 // the new private variable in CodeGen. This new variable is not added to
5070 // IdResolver, so the code in the OpenMP region uses original variable for
5071 // proper diagnostics.
5072 Type = Type.getUnqualifiedType();
5073 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
5074 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
5075 if (VDPrivate->isInvalidDecl())
5077 auto VDPrivateRefExpr = buildDeclRefExpr(
5078 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
5080 DSAStack->addDSA(VD, DE, OMPC_private);
5082 PrivateCopies.push_back(VDPrivateRefExpr);
5088 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5093 class DiagsUninitializedSeveretyRAII {
5095 DiagnosticsEngine &Diags;
5096 SourceLocation SavedLoc;
5100 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5102 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5104 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5105 /*Map*/ diag::Severity::Ignored, Loc);
5108 ~DiagsUninitializedSeveretyRAII() {
5110 Diags.popMappings(SavedLoc);
5115 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5116 SourceLocation StartLoc,
5117 SourceLocation LParenLoc,
5118 SourceLocation EndLoc) {
5119 SmallVector<Expr *, 8> Vars;
5120 SmallVector<Expr *, 8> PrivateCopies;
5121 SmallVector<Expr *, 8> Inits;
5122 bool IsImplicitClause =
5123 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5124 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5126 for (auto &RefExpr : VarList) {
5127 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5128 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5129 // It will be analyzed later.
5130 Vars.push_back(RefExpr);
5131 PrivateCopies.push_back(nullptr);
5132 Inits.push_back(nullptr);
5136 SourceLocation ELoc =
5137 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
5138 // OpenMP [2.1, C/C++]
5139 // A list item is a variable name.
5140 // OpenMP [2.9.3.3, Restrictions, p.1]
5141 // A variable that is part of another variable (as an array or
5142 // structure element) cannot appear in a private clause.
5143 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5144 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5145 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5148 Decl *D = DE->getDecl();
5149 VarDecl *VD = cast<VarDecl>(D);
5151 QualType Type = VD->getType();
5152 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5153 // It will be analyzed later.
5155 PrivateCopies.push_back(nullptr);
5156 Inits.push_back(nullptr);
5160 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5161 // A variable that appears in a private clause must not have an incomplete
5162 // type or a reference type.
5163 if (RequireCompleteType(ELoc, Type,
5164 diag::err_omp_firstprivate_incomplete_type)) {
5167 if (Type->isReferenceType()) {
5168 if (IsImplicitClause) {
5169 Diag(ImplicitClauseLoc,
5170 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5172 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5174 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5175 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5178 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5179 Diag(VD->getLocation(),
5180 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5185 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5186 // A variable of class type (or array thereof) that appears in a private
5187 // clause requires an accessible, unambiguous copy constructor for the
5189 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
5191 // If an implicit firstprivate variable found it was checked already.
5192 if (!IsImplicitClause) {
5193 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5194 bool IsConstant = ElemType.isConstant(Context);
5195 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5196 // A list item that specifies a given variable may not appear in more
5197 // than one clause on the same directive, except that a variable may be
5198 // specified in both firstprivate and lastprivate clauses.
5199 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
5200 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
5201 Diag(ELoc, diag::err_omp_wrong_dsa)
5202 << getOpenMPClauseName(DVar.CKind)
5203 << getOpenMPClauseName(OMPC_firstprivate);
5204 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5208 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5210 // Variables with the predetermined data-sharing attributes may not be
5211 // listed in data-sharing attributes clauses, except for the cases
5212 // listed below. For these exceptions only, listing a predetermined
5213 // variable in a data-sharing attribute clause is allowed and overrides
5214 // the variable's predetermined data-sharing attributes.
5215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5216 // in a Construct, C/C++, p.2]
5217 // Variables with const-qualified type having no mutable member may be
5218 // listed in a firstprivate clause, even if they are static data members.
5219 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5220 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5221 Diag(ELoc, diag::err_omp_wrong_dsa)
5222 << getOpenMPClauseName(DVar.CKind)
5223 << getOpenMPClauseName(OMPC_firstprivate);
5224 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5228 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5229 // OpenMP [2.9.3.4, Restrictions, p.2]
5230 // A list item that is private within a parallel region must not appear
5231 // in a firstprivate clause on a worksharing construct if any of the
5232 // worksharing regions arising from the worksharing construct ever bind
5233 // to any of the parallel regions arising from the parallel construct.
5234 if (isOpenMPWorksharingDirective(CurrDir) &&
5235 !isOpenMPParallelDirective(CurrDir)) {
5236 DVar = DSAStack->getImplicitDSA(VD, true);
5237 if (DVar.CKind != OMPC_shared &&
5238 (isOpenMPParallelDirective(DVar.DKind) ||
5239 DVar.DKind == OMPD_unknown)) {
5240 Diag(ELoc, diag::err_omp_required_access)
5241 << getOpenMPClauseName(OMPC_firstprivate)
5242 << getOpenMPClauseName(OMPC_shared);
5243 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5247 // OpenMP [2.9.3.4, Restrictions, p.3]
5248 // A list item that appears in a reduction clause of a parallel construct
5249 // must not appear in a firstprivate clause on a worksharing or task
5250 // construct if any of the worksharing or task regions arising from the
5251 // worksharing or task construct ever bind to any of the parallel regions
5252 // arising from the parallel construct.
5253 // OpenMP [2.9.3.4, Restrictions, p.4]
5254 // A list item that appears in a reduction clause in worksharing
5255 // construct must not appear in a firstprivate clause in a task construct
5256 // encountered during execution of any of the worksharing regions arising
5257 // from the worksharing construct.
5258 if (CurrDir == OMPD_task) {
5260 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5261 [](OpenMPDirectiveKind K) -> bool {
5262 return isOpenMPParallelDirective(K) ||
5263 isOpenMPWorksharingDirective(K);
5266 if (DVar.CKind == OMPC_reduction &&
5267 (isOpenMPParallelDirective(DVar.DKind) ||
5268 isOpenMPWorksharingDirective(DVar.DKind))) {
5269 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5270 << getOpenMPDirectiveName(DVar.DKind);
5271 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5277 // Variably modified types are not supported for tasks.
5278 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
5279 DSAStack->getCurrentDirective() == OMPD_task) {
5280 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5281 << getOpenMPClauseName(OMPC_firstprivate) << Type
5282 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5284 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5285 Diag(VD->getLocation(),
5286 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5291 Type = Type.getUnqualifiedType();
5292 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
5293 // Generate helper private variable and initialize it with the value of the
5294 // original variable. The address of the original variable is replaced by
5295 // the address of the new private variable in the CodeGen. This new variable
5296 // is not added to IdResolver, so the code in the OpenMP region uses
5297 // original variable for proper diagnostics and variable capturing.
5298 Expr *VDInitRefExpr = nullptr;
5299 // For arrays generate initializer for single element and replace it by the
5300 // original array element in CodeGen.
5301 if (Type->isArrayType()) {
5303 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5304 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
5305 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
5306 ElemType = ElemType.getUnqualifiedType();
5307 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5308 ".firstprivate.temp");
5309 InitializedEntity Entity =
5310 InitializedEntity::InitializeVariable(VDInitTemp);
5311 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5313 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5314 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5315 if (Result.isInvalid())
5316 VDPrivate->setInvalidDecl();
5318 VDPrivate->setInit(Result.getAs<Expr>());
5321 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
5323 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
5324 AddInitializerToDecl(VDPrivate,
5325 DefaultLvalueConversion(VDInitRefExpr).get(),
5326 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
5328 if (VDPrivate->isInvalidDecl()) {
5329 if (IsImplicitClause) {
5330 Diag(DE->getExprLoc(),
5331 diag::note_omp_task_predetermined_firstprivate_here);
5335 CurContext->addDecl(VDPrivate);
5336 auto VDPrivateRefExpr = buildDeclRefExpr(
5337 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
5338 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5340 PrivateCopies.push_back(VDPrivateRefExpr);
5341 Inits.push_back(VDInitRefExpr);
5347 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
5348 Vars, PrivateCopies, Inits);
5351 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5352 SourceLocation StartLoc,
5353 SourceLocation LParenLoc,
5354 SourceLocation EndLoc) {
5355 SmallVector<Expr *, 8> Vars;
5356 SmallVector<Expr *, 8> SrcExprs;
5357 SmallVector<Expr *, 8> DstExprs;
5358 SmallVector<Expr *, 8> AssignmentOps;
5359 for (auto &RefExpr : VarList) {
5360 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5361 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5362 // It will be analyzed later.
5363 Vars.push_back(RefExpr);
5364 SrcExprs.push_back(nullptr);
5365 DstExprs.push_back(nullptr);
5366 AssignmentOps.push_back(nullptr);
5370 SourceLocation ELoc = RefExpr->getExprLoc();
5371 // OpenMP [2.1, C/C++]
5372 // A list item is a variable name.
5373 // OpenMP [2.14.3.5, Restrictions, p.1]
5374 // A variable that is part of another variable (as an array or structure
5375 // element) cannot appear in a lastprivate clause.
5376 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5377 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5378 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5381 Decl *D = DE->getDecl();
5382 VarDecl *VD = cast<VarDecl>(D);
5384 QualType Type = VD->getType();
5385 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5386 // It will be analyzed later.
5388 SrcExprs.push_back(nullptr);
5389 DstExprs.push_back(nullptr);
5390 AssignmentOps.push_back(nullptr);
5394 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5395 // A variable that appears in a lastprivate clause must not have an
5396 // incomplete type or a reference type.
5397 if (RequireCompleteType(ELoc, Type,
5398 diag::err_omp_lastprivate_incomplete_type)) {
5401 if (Type->isReferenceType()) {
5402 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5403 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5405 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5406 Diag(VD->getLocation(),
5407 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5412 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5414 // Variables with the predetermined data-sharing attributes may not be
5415 // listed in data-sharing attributes clauses, except for the cases
5417 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5418 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5419 DVar.CKind != OMPC_firstprivate &&
5420 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5421 Diag(ELoc, diag::err_omp_wrong_dsa)
5422 << getOpenMPClauseName(DVar.CKind)
5423 << getOpenMPClauseName(OMPC_lastprivate);
5424 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5428 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5429 // OpenMP [2.14.3.5, Restrictions, p.2]
5430 // A list item that is private within a parallel region, or that appears in
5431 // the reduction clause of a parallel construct, must not appear in a
5432 // lastprivate clause on a worksharing construct if any of the corresponding
5433 // worksharing regions ever binds to any of the corresponding parallel
5435 DSAStackTy::DSAVarData TopDVar = DVar;
5436 if (isOpenMPWorksharingDirective(CurrDir) &&
5437 !isOpenMPParallelDirective(CurrDir)) {
5438 DVar = DSAStack->getImplicitDSA(VD, true);
5439 if (DVar.CKind != OMPC_shared) {
5440 Diag(ELoc, diag::err_omp_required_access)
5441 << getOpenMPClauseName(OMPC_lastprivate)
5442 << getOpenMPClauseName(OMPC_shared);
5443 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5447 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
5448 // A variable of class type (or array thereof) that appears in a
5449 // lastprivate clause requires an accessible, unambiguous default
5450 // constructor for the class type, unless the list item is also specified
5451 // in a firstprivate clause.
5452 // A variable of class type (or array thereof) that appears in a
5453 // lastprivate clause requires an accessible, unambiguous copy assignment
5454 // operator for the class type.
5455 Type = Context.getBaseElementType(Type).getNonReferenceType();
5456 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
5457 Type.getUnqualifiedType(), ".lastprivate.src");
5458 auto *PseudoSrcExpr = buildDeclRefExpr(
5459 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
5461 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
5462 auto *PseudoDstExpr =
5463 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
5464 // For arrays generate assignment operation for single element and replace
5465 // it by the original array element in CodeGen.
5466 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5467 PseudoDstExpr, PseudoSrcExpr);
5468 if (AssignmentOp.isInvalid())
5470 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5471 /*DiscardedValue=*/true);
5472 if (AssignmentOp.isInvalid())
5475 if (TopDVar.CKind != OMPC_firstprivate)
5476 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
5478 SrcExprs.push_back(PseudoSrcExpr);
5479 DstExprs.push_back(PseudoDstExpr);
5480 AssignmentOps.push_back(AssignmentOp.get());
5486 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
5487 Vars, SrcExprs, DstExprs, AssignmentOps);
5490 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5491 SourceLocation StartLoc,
5492 SourceLocation LParenLoc,
5493 SourceLocation EndLoc) {
5494 SmallVector<Expr *, 8> Vars;
5495 for (auto &RefExpr : VarList) {
5496 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5497 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5498 // It will be analyzed later.
5499 Vars.push_back(RefExpr);
5503 SourceLocation ELoc = RefExpr->getExprLoc();
5504 // OpenMP [2.1, C/C++]
5505 // A list item is a variable name.
5506 // OpenMP [2.14.3.2, Restrictions, p.1]
5507 // A variable that is part of another variable (as an array or structure
5508 // element) cannot appear in a shared unless it is a static data member
5510 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5511 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5512 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5515 Decl *D = DE->getDecl();
5516 VarDecl *VD = cast<VarDecl>(D);
5518 QualType Type = VD->getType();
5519 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5520 // It will be analyzed later.
5525 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5527 // Variables with the predetermined data-sharing attributes may not be
5528 // listed in data-sharing attributes clauses, except for the cases
5529 // listed below. For these exceptions only, listing a predetermined
5530 // variable in a data-sharing attribute clause is allowed and overrides
5531 // the variable's predetermined data-sharing attributes.
5532 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5533 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5535 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5536 << getOpenMPClauseName(OMPC_shared);
5537 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5541 DSAStack->addDSA(VD, DE, OMPC_shared);
5548 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5552 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5556 bool VisitDeclRefExpr(DeclRefExpr *E) {
5557 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
5558 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
5559 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5561 if (DVar.CKind != OMPC_unknown)
5563 DSAStackTy::DSAVarData DVarPrivate =
5564 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
5565 if (DVarPrivate.CKind != OMPC_unknown)
5571 bool VisitStmt(Stmt *S) {
5572 for (auto Child : S->children()) {
5573 if (Child && Visit(Child))
5578 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
5582 OMPClause *Sema::ActOnOpenMPReductionClause(
5583 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5584 SourceLocation ColonLoc, SourceLocation EndLoc,
5585 CXXScopeSpec &ReductionIdScopeSpec,
5586 const DeclarationNameInfo &ReductionId) {
5587 // TODO: Allow scope specification search when 'declare reduction' is
5589 assert(ReductionIdScopeSpec.isEmpty() &&
5590 "No support for scoped reduction identifiers yet.");
5592 auto DN = ReductionId.getName();
5593 auto OOK = DN.getCXXOverloadedOperator();
5594 BinaryOperatorKind BOK = BO_Comma;
5596 // OpenMP [2.14.3.6, reduction clause]
5598 // reduction-identifier is either an identifier or one of the following
5599 // operators: +, -, *, &, |, ^, && and ||
5601 // reduction-identifier is either an id-expression or one of the following
5602 // operators: +, -, *, &, |, ^, && and ||
5603 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5630 case OO_Array_Delete:
5639 case OO_GreaterEqual:
5644 case OO_PercentEqual:
5649 case OO_GreaterGreater:
5650 case OO_LessLessEqual:
5651 case OO_GreaterGreaterEqual:
5653 case OO_ExclaimEqual:
5661 case OO_Conditional:
5662 case NUM_OVERLOADED_OPERATORS:
5663 llvm_unreachable("Unexpected reduction identifier");
5665 if (auto II = DN.getAsIdentifierInfo()) {
5666 if (II->isStr("max"))
5668 else if (II->isStr("min"))
5673 SourceRange ReductionIdRange;
5674 if (ReductionIdScopeSpec.isValid()) {
5675 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5677 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5678 if (BOK == BO_Comma) {
5679 // Not allowed reduction identifier is found.
5680 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5681 << ReductionIdRange;
5685 SmallVector<Expr *, 8> Vars;
5686 SmallVector<Expr *, 8> LHSs;
5687 SmallVector<Expr *, 8> RHSs;
5688 SmallVector<Expr *, 8> ReductionOps;
5689 for (auto RefExpr : VarList) {
5690 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5691 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5692 // It will be analyzed later.
5693 Vars.push_back(RefExpr);
5694 LHSs.push_back(nullptr);
5695 RHSs.push_back(nullptr);
5696 ReductionOps.push_back(nullptr);
5700 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5701 RefExpr->isInstantiationDependent() ||
5702 RefExpr->containsUnexpandedParameterPack()) {
5703 // It will be analyzed later.
5704 Vars.push_back(RefExpr);
5705 LHSs.push_back(nullptr);
5706 RHSs.push_back(nullptr);
5707 ReductionOps.push_back(nullptr);
5711 auto ELoc = RefExpr->getExprLoc();
5712 auto ERange = RefExpr->getSourceRange();
5713 // OpenMP [2.1, C/C++]
5714 // A list item is a variable or array section, subject to the restrictions
5715 // specified in Section 2.4 on page 42 and in each of the sections
5716 // describing clauses and directives for which a list appears.
5717 // OpenMP [2.14.3.3, Restrictions, p.1]
5718 // A variable that is part of another variable (as an array or
5719 // structure element) cannot appear in a private clause.
5720 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5721 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5722 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5725 auto D = DE->getDecl();
5726 auto VD = cast<VarDecl>(D);
5727 auto Type = VD->getType();
5728 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5729 // A variable that appears in a private clause must not have an incomplete
5730 // type or a reference type.
5731 if (RequireCompleteType(ELoc, Type,
5732 diag::err_omp_reduction_incomplete_type))
5734 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5735 // Arrays may not appear in a reduction clause.
5736 if (Type.getNonReferenceType()->isArrayType()) {
5737 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5739 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5740 Diag(VD->getLocation(),
5741 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5745 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5746 // A list item that appears in a reduction clause must not be
5748 if (Type.getNonReferenceType().isConstant(Context)) {
5749 Diag(ELoc, diag::err_omp_const_variable)
5750 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5752 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5753 Diag(VD->getLocation(),
5754 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5758 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5759 // If a list-item is a reference type then it must bind to the same object
5760 // for all threads of the team.
5761 VarDecl *VDDef = VD->getDefinition();
5762 if (Type->isReferenceType() && VDDef) {
5763 DSARefChecker Check(DSAStack);
5764 if (Check.Visit(VDDef->getInit())) {
5765 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5766 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5770 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5771 // The type of a list item that appears in a reduction clause must be valid
5772 // for the reduction-identifier. For a max or min reduction in C, the type
5773 // of the list item must be an allowed arithmetic data type: char, int,
5774 // float, double, or _Bool, possibly modified with long, short, signed, or
5775 // unsigned. For a max or min reduction in C++, the type of the list item
5776 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5777 // double, or bool, possibly modified with long, short, signed, or unsigned.
5778 if ((BOK == BO_GT || BOK == BO_LT) &&
5779 !(Type->isScalarType() ||
5780 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5781 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5782 << getLangOpts().CPlusPlus;
5784 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5785 Diag(VD->getLocation(),
5786 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5790 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5791 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5792 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5794 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5795 Diag(VD->getLocation(),
5796 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5800 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5802 // Variables with the predetermined data-sharing attributes may not be
5803 // listed in data-sharing attributes clauses, except for the cases
5804 // listed below. For these exceptions only, listing a predetermined
5805 // variable in a data-sharing attribute clause is allowed and overrides
5806 // the variable's predetermined data-sharing attributes.
5807 // OpenMP [2.14.3.6, Restrictions, p.3]
5808 // Any number of reduction clauses can be specified on the directive,
5809 // but a list item can appear only once in the reduction clauses for that
5811 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5812 if (DVar.CKind == OMPC_reduction) {
5813 Diag(ELoc, diag::err_omp_once_referenced)
5814 << getOpenMPClauseName(OMPC_reduction);
5816 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5818 } else if (DVar.CKind != OMPC_unknown) {
5819 Diag(ELoc, diag::err_omp_wrong_dsa)
5820 << getOpenMPClauseName(DVar.CKind)
5821 << getOpenMPClauseName(OMPC_reduction);
5822 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5826 // OpenMP [2.14.3.6, Restrictions, p.1]
5827 // A list item that appears in a reduction clause of a worksharing
5828 // construct must be shared in the parallel regions to which any of the
5829 // worksharing regions arising from the worksharing construct bind.
5830 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5831 if (isOpenMPWorksharingDirective(CurrDir) &&
5832 !isOpenMPParallelDirective(CurrDir)) {
5833 DVar = DSAStack->getImplicitDSA(VD, true);
5834 if (DVar.CKind != OMPC_shared) {
5835 Diag(ELoc, diag::err_omp_required_access)
5836 << getOpenMPClauseName(OMPC_reduction)
5837 << getOpenMPClauseName(OMPC_shared);
5838 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5842 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
5843 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5844 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
5845 // Add initializer for private variable.
5846 Expr *Init = nullptr;
5852 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5853 if (Type->isScalarType() || Type->isAnyComplexType()) {
5854 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
5859 if (Type->isScalarType() || Type->isAnyComplexType()) {
5860 // '*' and '&&' reduction ops - initializer is '1'.
5861 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5865 // '&' reduction op - initializer is '~0'.
5866 QualType OrigType = Type;
5867 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5868 Type = ComplexTy->getElementType();
5870 if (Type->isRealFloatingType()) {
5871 llvm::APFloat InitValue =
5872 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5874 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5876 } else if (Type->isScalarType()) {
5877 auto Size = Context.getTypeSize(Type);
5878 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5879 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5880 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5882 if (Init && OrigType->isAnyComplexType()) {
5883 // Init = 0xFFFF + 0xFFFFi;
5884 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
5885 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
5892 // 'min' reduction op - initializer is 'Largest representable number in
5893 // the reduction list item type'.
5894 // 'max' reduction op - initializer is 'Least representable number in
5895 // the reduction list item type'.
5896 if (Type->isIntegerType() || Type->isPointerType()) {
5897 bool IsSigned = Type->hasSignedIntegerRepresentation();
5898 auto Size = Context.getTypeSize(Type);
5900 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
5901 llvm::APInt InitValue =
5903 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
5904 : llvm::APInt::getMinValue(Size)
5905 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
5906 : llvm::APInt::getMaxValue(Size);
5907 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5908 if (Type->isPointerType()) {
5909 // Cast to pointer type.
5910 auto CastExpr = BuildCStyleCastExpr(
5911 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
5912 SourceLocation(), Init);
5913 if (CastExpr.isInvalid())
5915 Init = CastExpr.get();
5917 } else if (Type->isRealFloatingType()) {
5918 llvm::APFloat InitValue = llvm::APFloat::getLargest(
5919 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
5920 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5948 llvm_unreachable("Unexpected reduction operation");
5951 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
5952 /*TypeMayContainAuto=*/false);
5954 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
5956 if (!RHSVD->hasInit()) {
5957 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
5958 << ReductionIdRange;
5960 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5961 Diag(VD->getLocation(),
5962 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5966 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
5967 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
5968 ExprResult ReductionOp =
5969 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
5971 if (ReductionOp.isUsable()) {
5972 if (BOK != BO_LT && BOK != BO_GT) {
5974 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5975 BO_Assign, LHSDRE, ReductionOp.get());
5977 auto *ConditionalOp = new (Context) ConditionalOperator(
5978 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
5979 RHSDRE, Type, VK_LValue, OK_Ordinary);
5981 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5982 BO_Assign, LHSDRE, ConditionalOp);
5984 if (ReductionOp.isUsable()) {
5985 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
5988 if (ReductionOp.isInvalid())
5991 DSAStack->addDSA(VD, DE, OMPC_reduction);
5993 LHSs.push_back(LHSDRE);
5994 RHSs.push_back(RHSDRE);
5995 ReductionOps.push_back(ReductionOp.get());
6001 return OMPReductionClause::Create(
6002 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
6003 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6004 RHSs, ReductionOps);
6007 OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6008 SourceLocation StartLoc,
6009 SourceLocation LParenLoc,
6010 SourceLocation ColonLoc,
6011 SourceLocation EndLoc) {
6012 SmallVector<Expr *, 8> Vars;
6013 SmallVector<Expr *, 8> Inits;
6014 for (auto &RefExpr : VarList) {
6015 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6016 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6017 // It will be analyzed later.
6018 Vars.push_back(RefExpr);
6019 Inits.push_back(nullptr);
6023 // OpenMP [2.14.3.7, linear clause]
6024 // A list item that appears in a linear clause is subject to the private
6025 // clause semantics described in Section 2.14.3.3 on page 159 except as
6026 // noted. In addition, the value of the new list item on each iteration
6027 // of the associated loop(s) corresponds to the value of the original
6028 // list item before entering the construct plus the logical number of
6029 // the iteration times linear-step.
6031 SourceLocation ELoc = RefExpr->getExprLoc();
6032 // OpenMP [2.1, C/C++]
6033 // A list item is a variable name.
6034 // OpenMP [2.14.3.3, Restrictions, p.1]
6035 // A variable that is part of another variable (as an array or
6036 // structure element) cannot appear in a private clause.
6037 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6038 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6039 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6043 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6045 // OpenMP [2.14.3.7, linear clause]
6046 // A list-item cannot appear in more than one linear clause.
6047 // A list-item that appears in a linear clause cannot appear in any
6048 // other data-sharing attribute clause.
6049 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6051 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6052 << getOpenMPClauseName(OMPC_linear);
6053 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6057 QualType QType = VD->getType();
6058 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6059 // It will be analyzed later.
6061 Inits.push_back(nullptr);
6065 // A variable must not have an incomplete type or a reference type.
6066 if (RequireCompleteType(ELoc, QType,
6067 diag::err_omp_linear_incomplete_type)) {
6070 if (QType->isReferenceType()) {
6071 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6072 << getOpenMPClauseName(OMPC_linear) << QType;
6074 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6075 Diag(VD->getLocation(),
6076 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6081 // A list item must not be const-qualified.
6082 if (QType.isConstant(Context)) {
6083 Diag(ELoc, diag::err_omp_const_variable)
6084 << getOpenMPClauseName(OMPC_linear);
6086 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6087 Diag(VD->getLocation(),
6088 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6093 // A list item must be of integral or pointer type.
6094 QType = QType.getUnqualifiedType().getCanonicalType();
6095 const Type *Ty = QType.getTypePtrOrNull();
6096 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6097 !Ty->isPointerType())) {
6098 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6100 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6101 Diag(VD->getLocation(),
6102 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6107 // Build var to save initial value.
6108 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
6109 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6110 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
6111 auto InitRef = buildDeclRefExpr(
6112 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
6113 DSAStack->addDSA(VD, DE, OMPC_linear);
6115 Inits.push_back(InitRef);
6121 Expr *StepExpr = Step;
6122 Expr *CalcStepExpr = nullptr;
6123 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6124 !Step->isInstantiationDependent() &&
6125 !Step->containsUnexpandedParameterPack()) {
6126 SourceLocation StepLoc = Step->getLocStart();
6127 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
6128 if (Val.isInvalid())
6130 StepExpr = Val.get();
6132 // Build var to save the step value.
6134 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
6135 ExprResult SaveRef =
6136 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
6137 ExprResult CalcStep =
6138 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6140 // Warn about zero linear step (it would be probably better specified as
6141 // making corresponding variables 'const').
6142 llvm::APSInt Result;
6143 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6144 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
6145 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6146 << (Vars.size() > 1);
6147 if (!IsConstant && CalcStep.isUsable()) {
6148 // Calculate the step beforehand instead of doing this on each iteration.
6149 // (This is not used if the number of iterations may be kfold-ed).
6150 CalcStepExpr = CalcStep.get();
6154 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
6155 Vars, Inits, StepExpr, CalcStepExpr);
6158 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6159 Expr *NumIterations, Sema &SemaRef,
6161 // Walk the vars and build update/final expressions for the CodeGen.
6162 SmallVector<Expr *, 8> Updates;
6163 SmallVector<Expr *, 8> Finals;
6164 Expr *Step = Clause.getStep();
6165 Expr *CalcStep = Clause.getCalcStep();
6166 // OpenMP [2.14.3.7, linear clause]
6167 // If linear-step is not specified it is assumed to be 1.
6168 if (Step == nullptr)
6169 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6171 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6172 bool HasErrors = false;
6173 auto CurInit = Clause.inits().begin();
6174 for (auto &RefExpr : Clause.varlists()) {
6175 Expr *InitExpr = *CurInit;
6177 // Build privatized reference to the current linear var.
6178 auto DE = cast<DeclRefExpr>(RefExpr);
6180 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6181 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6182 /*RefersToCapture=*/true);
6184 // Build update: Var = InitExpr + IV * Step
6186 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6187 InitExpr, IV, Step, /* Subtract */ false);
6188 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6190 // Build final: Var = InitExpr + NumIterations * Step
6192 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6193 InitExpr, NumIterations, Step, /* Subtract */ false);
6194 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6195 if (!Update.isUsable() || !Final.isUsable()) {
6196 Updates.push_back(nullptr);
6197 Finals.push_back(nullptr);
6200 Updates.push_back(Update.get());
6201 Finals.push_back(Final.get());
6205 Clause.setUpdates(Updates);
6206 Clause.setFinals(Finals);
6210 OMPClause *Sema::ActOnOpenMPAlignedClause(
6211 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6212 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6214 SmallVector<Expr *, 8> Vars;
6215 for (auto &RefExpr : VarList) {
6216 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6217 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6218 // It will be analyzed later.
6219 Vars.push_back(RefExpr);
6223 SourceLocation ELoc = RefExpr->getExprLoc();
6224 // OpenMP [2.1, C/C++]
6225 // A list item is a variable name.
6226 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6227 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6228 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6232 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6234 // OpenMP [2.8.1, simd construct, Restrictions]
6235 // The type of list items appearing in the aligned clause must be
6236 // array, pointer, reference to array, or reference to pointer.
6237 QualType QType = VD->getType();
6238 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
6239 const Type *Ty = QType.getTypePtrOrNull();
6240 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6241 !Ty->isPointerType())) {
6242 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6243 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6245 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6246 Diag(VD->getLocation(),
6247 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6252 // OpenMP [2.8.1, simd construct, Restrictions]
6253 // A list-item cannot appear in more than one aligned clause.
6254 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6255 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6256 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6257 << getOpenMPClauseName(OMPC_aligned);
6264 // OpenMP [2.8.1, simd construct, Description]
6265 // The parameter of the aligned clause, alignment, must be a constant
6266 // positive integer expression.
6267 // If no optional parameter is specified, implementation-defined default
6268 // alignments for SIMD instructions on the target platforms are assumed.
6269 if (Alignment != nullptr) {
6270 ExprResult AlignResult =
6271 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6272 if (AlignResult.isInvalid())
6274 Alignment = AlignResult.get();
6279 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6280 EndLoc, Vars, Alignment);
6283 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6284 SourceLocation StartLoc,
6285 SourceLocation LParenLoc,
6286 SourceLocation EndLoc) {
6287 SmallVector<Expr *, 8> Vars;
6288 SmallVector<Expr *, 8> SrcExprs;
6289 SmallVector<Expr *, 8> DstExprs;
6290 SmallVector<Expr *, 8> AssignmentOps;
6291 for (auto &RefExpr : VarList) {
6292 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6293 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6294 // It will be analyzed later.
6295 Vars.push_back(RefExpr);
6296 SrcExprs.push_back(nullptr);
6297 DstExprs.push_back(nullptr);
6298 AssignmentOps.push_back(nullptr);
6302 SourceLocation ELoc = RefExpr->getExprLoc();
6303 // OpenMP [2.1, C/C++]
6304 // A list item is a variable name.
6305 // OpenMP [2.14.4.1, Restrictions, p.1]
6306 // A list item that appears in a copyin clause must be threadprivate.
6307 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6308 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6309 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6313 Decl *D = DE->getDecl();
6314 VarDecl *VD = cast<VarDecl>(D);
6316 QualType Type = VD->getType();
6317 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6318 // It will be analyzed later.
6320 SrcExprs.push_back(nullptr);
6321 DstExprs.push_back(nullptr);
6322 AssignmentOps.push_back(nullptr);
6326 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6327 // A list item that appears in a copyin clause must be threadprivate.
6328 if (!DSAStack->isThreadPrivate(VD)) {
6329 Diag(ELoc, diag::err_omp_required_access)
6330 << getOpenMPClauseName(OMPC_copyin)
6331 << getOpenMPDirectiveName(OMPD_threadprivate);
6335 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6336 // A variable of class type (or array thereof) that appears in a
6337 // copyin clause requires an accessible, unambiguous copy assignment
6338 // operator for the class type.
6339 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
6340 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
6341 ElemType.getUnqualifiedType(), ".copyin.src");
6342 auto *PseudoSrcExpr = buildDeclRefExpr(
6343 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6345 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
6346 auto *PseudoDstExpr =
6347 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
6348 // For arrays generate assignment operation for single element and replace
6349 // it by the original array element in CodeGen.
6350 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6351 PseudoDstExpr, PseudoSrcExpr);
6352 if (AssignmentOp.isInvalid())
6354 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6355 /*DiscardedValue=*/true);
6356 if (AssignmentOp.isInvalid())
6359 DSAStack->addDSA(VD, DE, OMPC_copyin);
6361 SrcExprs.push_back(PseudoSrcExpr);
6362 DstExprs.push_back(PseudoDstExpr);
6363 AssignmentOps.push_back(AssignmentOp.get());
6369 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6370 SrcExprs, DstExprs, AssignmentOps);
6373 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6374 SourceLocation StartLoc,
6375 SourceLocation LParenLoc,
6376 SourceLocation EndLoc) {
6377 SmallVector<Expr *, 8> Vars;
6378 SmallVector<Expr *, 8> SrcExprs;
6379 SmallVector<Expr *, 8> DstExprs;
6380 SmallVector<Expr *, 8> AssignmentOps;
6381 for (auto &RefExpr : VarList) {
6382 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6383 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6384 // It will be analyzed later.
6385 Vars.push_back(RefExpr);
6386 SrcExprs.push_back(nullptr);
6387 DstExprs.push_back(nullptr);
6388 AssignmentOps.push_back(nullptr);
6392 SourceLocation ELoc = RefExpr->getExprLoc();
6393 // OpenMP [2.1, C/C++]
6394 // A list item is a variable name.
6395 // OpenMP [2.14.4.1, Restrictions, p.1]
6396 // A list item that appears in a copyin clause must be threadprivate.
6397 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6398 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6399 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6403 Decl *D = DE->getDecl();
6404 VarDecl *VD = cast<VarDecl>(D);
6406 QualType Type = VD->getType();
6407 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6408 // It will be analyzed later.
6410 SrcExprs.push_back(nullptr);
6411 DstExprs.push_back(nullptr);
6412 AssignmentOps.push_back(nullptr);
6416 // OpenMP [2.14.4.2, Restrictions, p.2]
6417 // A list item that appears in a copyprivate clause may not appear in a
6418 // private or firstprivate clause on the single construct.
6419 if (!DSAStack->isThreadPrivate(VD)) {
6420 auto DVar = DSAStack->getTopDSA(VD, false);
6421 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6423 Diag(ELoc, diag::err_omp_wrong_dsa)
6424 << getOpenMPClauseName(DVar.CKind)
6425 << getOpenMPClauseName(OMPC_copyprivate);
6426 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6430 // OpenMP [2.11.4.2, Restrictions, p.1]
6431 // All list items that appear in a copyprivate clause must be either
6432 // threadprivate or private in the enclosing context.
6433 if (DVar.CKind == OMPC_unknown) {
6434 DVar = DSAStack->getImplicitDSA(VD, false);
6435 if (DVar.CKind == OMPC_shared) {
6436 Diag(ELoc, diag::err_omp_required_access)
6437 << getOpenMPClauseName(OMPC_copyprivate)
6438 << "threadprivate or private in the enclosing context";
6439 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6445 // Variably modified types are not supported.
6446 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
6447 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6448 << getOpenMPClauseName(OMPC_copyprivate) << Type
6449 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6451 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6452 Diag(VD->getLocation(),
6453 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6458 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6459 // A variable of class type (or array thereof) that appears in a
6460 // copyin clause requires an accessible, unambiguous copy assignment
6461 // operator for the class type.
6462 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6464 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
6465 auto *PseudoSrcExpr =
6466 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
6468 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
6469 auto *PseudoDstExpr =
6470 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
6471 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6472 PseudoDstExpr, PseudoSrcExpr);
6473 if (AssignmentOp.isInvalid())
6475 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6476 /*DiscardedValue=*/true);
6477 if (AssignmentOp.isInvalid())
6480 // No need to mark vars as copyprivate, they are already threadprivate or
6481 // implicitly private.
6483 SrcExprs.push_back(PseudoSrcExpr);
6484 DstExprs.push_back(PseudoDstExpr);
6485 AssignmentOps.push_back(AssignmentOp.get());
6491 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6492 Vars, SrcExprs, DstExprs, AssignmentOps);
6495 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6496 SourceLocation StartLoc,
6497 SourceLocation LParenLoc,
6498 SourceLocation EndLoc) {
6499 if (VarList.empty())
6502 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6506 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6507 SourceLocation DepLoc, SourceLocation ColonLoc,
6508 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6509 SourceLocation LParenLoc, SourceLocation EndLoc) {
6510 if (DepKind == OMPC_DEPEND_unknown) {
6512 std::string Sep(", ");
6513 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6515 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6518 case OMPC_DEPEND_unknown - 2:
6521 case OMPC_DEPEND_unknown - 1:
6528 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6529 << Values << getOpenMPClauseName(OMPC_depend);
6532 SmallVector<Expr *, 8> Vars;
6533 for (auto &RefExpr : VarList) {
6534 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6535 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6536 // It will be analyzed later.
6537 Vars.push_back(RefExpr);
6541 SourceLocation ELoc = RefExpr->getExprLoc();
6542 // OpenMP [2.11.1.1, Restrictions, p.3]
6543 // A variable that is part of another variable (such as a field of a
6544 // structure) but is not an array element or an array section cannot appear
6545 // in a depend clause.
6546 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6547 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6548 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6549 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6550 (DE && !isa<VarDecl>(DE->getDecl())) ||
6551 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6552 !ASE->getBase()->getType()->isArrayType())) {
6553 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6554 << RefExpr->getSourceRange();
6558 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6564 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6565 DepLoc, ColonLoc, Vars);