Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// \brief This file implements semantic analysis for OpenMP directives and |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 11 | /// clauses. |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtCXX.h" |
| 20 | #include "clang/AST/StmtOpenMP.h" |
| 21 | #include "clang/AST/StmtVisitor.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 22 | #include "clang/Basic/OpenMPKinds.h" |
| 23 | #include "clang/Lex/Preprocessor.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Initialization.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Lookup.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Scope.h" |
| 27 | #include "clang/Sema/ScopeInfo.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 28 | #include "clang/Sema/SemaInternal.h" |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Stack of data-sharing attributes for variables |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | namespace { |
| 36 | /// \brief Default data sharing attributes, which can be applied to directive. |
| 37 | enum DefaultDataSharingAttributes { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 38 | DSA_unspecified = 0, /// \brief Data sharing attribute not specified. |
| 39 | DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. |
| 40 | DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 41 | }; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 42 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 43 | template <class T> struct MatchesAny { |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 44 | explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {} |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 45 | bool operator()(T Kind) { |
| 46 | for (auto KindEl : Arr) |
| 47 | if (KindEl == Kind) |
| 48 | return true; |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | ArrayRef<T> Arr; |
| 54 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 55 | struct MatchesAlways { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 56 | MatchesAlways() {} |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 57 | template <class T> bool operator()(T) { return true; } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause; |
| 61 | typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 62 | |
| 63 | /// \brief Stack for tracking declarations used in OpenMP directives and |
| 64 | /// clauses and their data-sharing attributes. |
| 65 | class DSAStackTy { |
| 66 | public: |
| 67 | struct DSAVarData { |
| 68 | OpenMPDirectiveKind DKind; |
| 69 | OpenMPClauseKind CKind; |
| 70 | DeclRefExpr *RefExpr; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 71 | SourceLocation ImplicitDSALoc; |
| 72 | DSAVarData() |
| 73 | : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr), |
| 74 | ImplicitDSALoc() {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 75 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 76 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 77 | private: |
| 78 | struct DSAInfo { |
| 79 | OpenMPClauseKind Attributes; |
| 80 | DeclRefExpr *RefExpr; |
| 81 | }; |
| 82 | typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 83 | typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 84 | |
| 85 | struct SharingMapTy { |
| 86 | DeclSAMapTy SharingMap; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 87 | AlignedMapTy AlignedMap; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 88 | DefaultDataSharingAttributes DefaultAttr; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 89 | SourceLocation DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 90 | OpenMPDirectiveKind Directive; |
| 91 | DeclarationNameInfo DirectiveName; |
| 92 | Scope *CurScope; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 93 | SourceLocation ConstructLoc; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 94 | SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 95 | Scope *CurScope, SourceLocation Loc) |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 96 | : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified), |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 97 | Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope), |
| 98 | ConstructLoc(Loc) {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 99 | SharingMapTy() |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 100 | : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified), |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 101 | Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr), |
| 102 | ConstructLoc() {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | typedef SmallVector<SharingMapTy, 64> StackTy; |
| 106 | |
| 107 | /// \brief Stack of used declaration and their data-sharing attributes. |
| 108 | StackTy Stack; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 109 | Sema &SemaRef; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 110 | |
| 111 | typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; |
| 112 | |
| 113 | DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 114 | |
| 115 | /// \brief Checks if the variable is a local for OpenMP region. |
| 116 | bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 117 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 118 | public: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 119 | explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 120 | |
| 121 | void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 122 | Scope *CurScope, SourceLocation Loc) { |
| 123 | Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc)); |
| 124 | Stack.back().DefaultAttrLoc = Loc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void pop() { |
| 128 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); |
| 129 | Stack.pop_back(); |
| 130 | } |
| 131 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 132 | /// \brief If 'aligned' declaration for given variable \a D was not seen yet, |
Alp Toker | 15e62a3 | 2014-06-06 12:02:07 +0000 | [diff] [blame] | 133 | /// add it and return NULL; otherwise return previous occurrence's expression |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 134 | /// for diagnostics. |
| 135 | DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE); |
| 136 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 137 | /// \brief Adds explicit data sharing attribute to the specified declaration. |
| 138 | void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A); |
| 139 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 140 | /// \brief Returns data sharing attributes from top of the stack for the |
| 141 | /// specified declaration. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 142 | DSAVarData getTopDSA(VarDecl *D, bool FromParent); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 143 | /// \brief Returns data-sharing attributes for the specified declaration. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 144 | DSAVarData getImplicitDSA(VarDecl *D, bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 145 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 146 | /// match specified \a CPred predicate in any directive which matches \a DPred |
| 147 | /// predicate. |
| 148 | template <class ClausesPredicate, class DirectivesPredicate> |
| 149 | DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 150 | DirectivesPredicate DPred, bool FromParent); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 151 | /// \brief Checks if the specified variables has data-sharing attributes which |
| 152 | /// match specified \a CPred predicate in any innermost directive which |
| 153 | /// matches \a DPred predicate. |
| 154 | template <class ClausesPredicate, class DirectivesPredicate> |
| 155 | DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 156 | DirectivesPredicate DPred, |
| 157 | bool FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 158 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 159 | /// \brief Returns currently analyzed directive. |
| 160 | OpenMPDirectiveKind getCurrentDirective() const { |
| 161 | return Stack.back().Directive; |
| 162 | } |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 163 | /// \brief Returns parent directive. |
| 164 | OpenMPDirectiveKind getParentDirective() const { |
| 165 | if (Stack.size() > 2) |
| 166 | return Stack[Stack.size() - 2].Directive; |
| 167 | return OMPD_unknown; |
| 168 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 169 | |
| 170 | /// \brief Set default data sharing attribute to none. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 171 | void setDefaultDSANone(SourceLocation Loc) { |
| 172 | Stack.back().DefaultAttr = DSA_none; |
| 173 | Stack.back().DefaultAttrLoc = Loc; |
| 174 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 175 | /// \brief Set default data sharing attribute to shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 176 | void setDefaultDSAShared(SourceLocation Loc) { |
| 177 | Stack.back().DefaultAttr = DSA_shared; |
| 178 | Stack.back().DefaultAttrLoc = Loc; |
| 179 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 180 | |
| 181 | DefaultDataSharingAttributes getDefaultDSA() const { |
| 182 | return Stack.back().DefaultAttr; |
| 183 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 184 | SourceLocation getDefaultDSALocation() const { |
| 185 | return Stack.back().DefaultAttrLoc; |
| 186 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 187 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 188 | /// \brief Checks if the specified variable is a threadprivate. |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 189 | bool isThreadPrivate(VarDecl *D) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 190 | DSAVarData DVar = getTopDSA(D, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 191 | return isOpenMPThreadPrivate(DVar.CKind); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | Scope *getCurScope() const { return Stack.back().CurScope; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 195 | Scope *getCurScope() { return Stack.back().CurScope; } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 196 | SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 197 | }; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 198 | bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { |
| 199 | return isOpenMPParallelDirective(DKind) || DKind == OMPD_task || |
| 200 | DKind == OMPD_unknown; |
| 201 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 202 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 203 | |
| 204 | DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter, |
| 205 | VarDecl *D) { |
| 206 | DSAVarData DVar; |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 207 | if (Iter == std::prev(Stack.rend())) { |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 208 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 209 | // in a region but not in construct] |
| 210 | // File-scope or namespace-scope variables referenced in called routines |
| 211 | // in the region are shared unless they appear in a threadprivate |
| 212 | // directive. |
Alexey Bataev | 750a58b | 2014-03-18 12:19:12 +0000 | [diff] [blame] | 213 | if (!D->isFunctionOrMethodVarDecl()) |
| 214 | DVar.CKind = OMPC_shared; |
| 215 | |
| 216 | // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced |
| 217 | // in a region but not in construct] |
| 218 | // Variables with static storage duration that are declared in called |
| 219 | // routines in the region are shared. |
| 220 | if (D->hasGlobalStorage()) |
| 221 | DVar.CKind = OMPC_shared; |
| 222 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 223 | return DVar; |
| 224 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 225 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 226 | DVar.DKind = Iter->Directive; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 227 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 228 | // in a Construct, C/C++, predetermined, p.1] |
| 229 | // Variables with automatic storage duration that are declared in a scope |
| 230 | // inside the construct are private. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 231 | if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() && |
| 232 | (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { |
| 233 | DVar.CKind = OMPC_private; |
| 234 | return DVar; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 237 | // Explicitly specified attributes and local variables with predetermined |
| 238 | // attributes. |
| 239 | if (Iter->SharingMap.count(D)) { |
| 240 | DVar.RefExpr = Iter->SharingMap[D].RefExpr; |
| 241 | DVar.CKind = Iter->SharingMap[D].Attributes; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 242 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 243 | return DVar; |
| 244 | } |
| 245 | |
| 246 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 247 | // in a Construct, C/C++, implicitly determined, p.1] |
| 248 | // In a parallel or task construct, the data-sharing attributes of these |
| 249 | // variables are determined by the default clause, if present. |
| 250 | switch (Iter->DefaultAttr) { |
| 251 | case DSA_shared: |
| 252 | DVar.CKind = OMPC_shared; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 253 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 254 | return DVar; |
| 255 | case DSA_none: |
| 256 | return DVar; |
| 257 | case DSA_unspecified: |
| 258 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 259 | // in a Construct, implicitly determined, p.2] |
| 260 | // In a parallel construct, if no default clause is present, these |
| 261 | // variables are shared. |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 262 | DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; |
Alexey Bataev | cefffae | 2014-06-23 08:21:53 +0000 | [diff] [blame] | 263 | if (isOpenMPParallelDirective(DVar.DKind)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 264 | DVar.CKind = OMPC_shared; |
| 265 | return DVar; |
| 266 | } |
| 267 | |
| 268 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 269 | // in a Construct, implicitly determined, p.4] |
| 270 | // In a task construct, if no default clause is present, a variable that in |
| 271 | // the enclosing context is determined to be shared by all implicit tasks |
| 272 | // bound to the current team is shared. |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 273 | if (DVar.DKind == OMPD_task) { |
| 274 | DSAVarData DVarTemp; |
Benjamin Kramer | 167e999 | 2014-03-02 12:20:24 +0000 | [diff] [blame] | 275 | for (StackTy::reverse_iterator I = std::next(Iter), |
| 276 | EE = std::prev(Stack.rend()); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 277 | I != EE; ++I) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 278 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables |
| 279 | // Referenced |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 280 | // in a Construct, implicitly determined, p.6] |
| 281 | // In a task construct, if no default clause is present, a variable |
| 282 | // whose data-sharing attribute is not determined by the rules above is |
| 283 | // firstprivate. |
| 284 | DVarTemp = getDSA(I, D); |
| 285 | if (DVarTemp.CKind != OMPC_shared) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 286 | DVar.RefExpr = nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 287 | DVar.DKind = OMPD_task; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 288 | DVar.CKind = OMPC_firstprivate; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 289 | return DVar; |
| 290 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 291 | if (isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 292 | break; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 293 | } |
| 294 | DVar.DKind = OMPD_task; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 295 | DVar.CKind = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 296 | (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 297 | return DVar; |
| 298 | } |
| 299 | } |
| 300 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 301 | // in a Construct, implicitly determined, p.3] |
| 302 | // For constructs other than task, if no default clause is present, these |
| 303 | // variables inherit their data-sharing attributes from the enclosing |
| 304 | // context. |
Benjamin Kramer | 167e999 | 2014-03-02 12:20:24 +0000 | [diff] [blame] | 305 | return getDSA(std::next(Iter), D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 308 | DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) { |
| 309 | assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); |
| 310 | auto It = Stack.back().AlignedMap.find(D); |
| 311 | if (It == Stack.back().AlignedMap.end()) { |
| 312 | assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); |
| 313 | Stack.back().AlignedMap[D] = NewDE; |
| 314 | return nullptr; |
| 315 | } else { |
| 316 | assert(It->second && "Unexpected nullptr expr in the aligned map"); |
| 317 | return It->second; |
| 318 | } |
| 319 | return nullptr; |
| 320 | } |
| 321 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 322 | void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) { |
| 323 | if (A == OMPC_threadprivate) { |
| 324 | Stack[0].SharingMap[D].Attributes = A; |
| 325 | Stack[0].SharingMap[D].RefExpr = E; |
| 326 | } else { |
| 327 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
| 328 | Stack.back().SharingMap[D].Attributes = A; |
| 329 | Stack.back().SharingMap[D].RefExpr = E; |
| 330 | } |
| 331 | } |
| 332 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 333 | bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 334 | if (Stack.size() > 2) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 335 | reverse_iterator I = Iter, E = std::prev(Stack.rend()); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 336 | Scope *TopScope = nullptr; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 337 | while (I != E && !isParallelOrTaskRegion(I->Directive)) { |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 338 | ++I; |
| 339 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 340 | if (I == E) |
| 341 | return false; |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 342 | TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 343 | Scope *CurScope = getCurScope(); |
| 344 | while (CurScope != TopScope && !CurScope->isDeclScope(D)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 345 | CurScope = CurScope->getParent(); |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 346 | } |
| 347 | return CurScope != TopScope; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 348 | } |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 349 | return false; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 352 | DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 353 | DSAVarData DVar; |
| 354 | |
| 355 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 356 | // in a Construct, C/C++, predetermined, p.1] |
| 357 | // Variables appearing in threadprivate directives are threadprivate. |
| 358 | if (D->getTLSKind() != VarDecl::TLS_None) { |
| 359 | DVar.CKind = OMPC_threadprivate; |
| 360 | return DVar; |
| 361 | } |
| 362 | if (Stack[0].SharingMap.count(D)) { |
| 363 | DVar.RefExpr = Stack[0].SharingMap[D].RefExpr; |
| 364 | DVar.CKind = OMPC_threadprivate; |
| 365 | return DVar; |
| 366 | } |
| 367 | |
| 368 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 369 | // in a Construct, C/C++, predetermined, p.1] |
| 370 | // Variables with automatic storage duration that are declared in a scope |
| 371 | // inside the construct are private. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 372 | OpenMPDirectiveKind Kind = |
| 373 | FromParent ? getParentDirective() : getCurrentDirective(); |
| 374 | auto StartI = std::next(Stack.rbegin()); |
| 375 | auto EndI = std::prev(Stack.rend()); |
| 376 | if (FromParent && StartI != EndI) { |
| 377 | StartI = std::next(StartI); |
| 378 | } |
| 379 | if (!isParallelOrTaskRegion(Kind)) { |
| 380 | if (isOpenMPLocal(D, StartI) && D->isLocalVarDecl() && |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 381 | (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 382 | DVar.CKind = OMPC_private; |
| 383 | return DVar; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 384 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 388 | // in a Construct, C/C++, predetermined, p.4] |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 389 | // Static data members are shared. |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 390 | if (D->isStaticDataMember()) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 391 | // Variables with const-qualified type having no mutable member may be |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 392 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 393 | DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate), |
| 394 | MatchesAlways(), FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 395 | if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) |
| 396 | return DVar; |
| 397 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 398 | DVar.CKind = OMPC_shared; |
| 399 | return DVar; |
| 400 | } |
| 401 | |
| 402 | QualType Type = D->getType().getNonReferenceType().getCanonicalType(); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 403 | bool IsConstant = Type.isConstant(SemaRef.getASTContext()); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 404 | while (Type->isArrayType()) { |
| 405 | QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType(); |
| 406 | Type = ElemType.getNonReferenceType().getCanonicalType(); |
| 407 | } |
| 408 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 409 | // in a Construct, C/C++, predetermined, p.6] |
| 410 | // Variables with const qualified type having no mutable member are |
| 411 | // shared. |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 412 | CXXRecordDecl *RD = |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 413 | SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 414 | if (IsConstant && |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 415 | !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 416 | // Variables with const-qualified type having no mutable member may be |
| 417 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 418 | DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate), |
| 419 | MatchesAlways(), FromParent); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 420 | if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) |
| 421 | return DVar; |
| 422 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 423 | DVar.CKind = OMPC_shared; |
| 424 | return DVar; |
| 425 | } |
| 426 | |
| 427 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 428 | // in a Construct, C/C++, predetermined, p.7] |
| 429 | // Variables with static storage duration that are declared in a scope |
| 430 | // inside the construct are shared. |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 431 | if (D->isStaticLocal()) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 432 | DVar.CKind = OMPC_shared; |
| 433 | return DVar; |
| 434 | } |
| 435 | |
| 436 | // Explicitly specified attributes and local variables with predetermined |
| 437 | // attributes. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 438 | auto I = std::prev(StartI); |
| 439 | if (I->SharingMap.count(D)) { |
| 440 | DVar.RefExpr = I->SharingMap[D].RefExpr; |
| 441 | DVar.CKind = I->SharingMap[D].Attributes; |
| 442 | DVar.ImplicitDSALoc = I->DefaultAttrLoc; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return DVar; |
| 446 | } |
| 447 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 448 | DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) { |
| 449 | auto StartI = Stack.rbegin(); |
| 450 | auto EndI = std::prev(Stack.rend()); |
| 451 | if (FromParent && StartI != EndI) { |
| 452 | StartI = std::next(StartI); |
| 453 | } |
| 454 | return getDSA(StartI, D); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 457 | template <class ClausesPredicate, class DirectivesPredicate> |
| 458 | DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 459 | DirectivesPredicate DPred, |
| 460 | bool FromParent) { |
| 461 | auto StartI = std::next(Stack.rbegin()); |
| 462 | auto EndI = std::prev(Stack.rend()); |
| 463 | if (FromParent && StartI != EndI) { |
| 464 | StartI = std::next(StartI); |
| 465 | } |
| 466 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
| 467 | if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 468 | continue; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 469 | DSAVarData DVar = getDSA(I, D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 470 | if (CPred(DVar.CKind)) |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 471 | return DVar; |
| 472 | } |
| 473 | return DSAVarData(); |
| 474 | } |
| 475 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 476 | template <class ClausesPredicate, class DirectivesPredicate> |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 477 | DSAStackTy::DSAVarData |
| 478 | DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, |
| 479 | DirectivesPredicate DPred, bool FromParent) { |
| 480 | auto StartI = std::next(Stack.rbegin()); |
| 481 | auto EndI = std::prev(Stack.rend()); |
| 482 | if (FromParent && StartI != EndI) { |
| 483 | StartI = std::next(StartI); |
| 484 | } |
| 485 | for (auto I = StartI, EE = EndI; I != EE; ++I) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 486 | if (!DPred(I->Directive)) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 487 | break; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 488 | DSAVarData DVar = getDSA(I, D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 489 | if (CPred(DVar.CKind)) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 490 | return DVar; |
| 491 | return DSAVarData(); |
| 492 | } |
| 493 | return DSAVarData(); |
| 494 | } |
| 495 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 496 | void Sema::InitDataSharingAttributesStack() { |
| 497 | VarDataSharingAttributesStack = new DSAStackTy(*this); |
| 498 | } |
| 499 | |
| 500 | #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) |
| 501 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 502 | void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 503 | |
| 504 | void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, |
| 505 | const DeclarationNameInfo &DirName, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 506 | Scope *CurScope, SourceLocation Loc) { |
| 507 | DSAStack->push(DKind, DirName, CurScope, Loc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 508 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 509 | } |
| 510 | |
| 511 | void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 512 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] |
| 513 | // A variable of class type (or array thereof) that appears in a lastprivate |
| 514 | // clause requires an accessible, unambiguous default constructor for the |
| 515 | // class type, unless the list item is also specified in a firstprivate |
| 516 | // clause. |
| 517 | if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { |
| 518 | for (auto C : D->clauses()) { |
| 519 | if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) { |
| 520 | for (auto VarRef : Clause->varlists()) { |
| 521 | if (VarRef->isValueDependent() || VarRef->isTypeDependent()) |
| 522 | continue; |
| 523 | auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl()); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 524 | auto DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 525 | if (DVar.CKind == OMPC_lastprivate) { |
| 526 | SourceLocation ELoc = VarRef->getExprLoc(); |
| 527 | auto Type = VarRef->getType(); |
| 528 | if (Type->isArrayType()) |
| 529 | Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0); |
| 530 | CXXRecordDecl *RD = |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 531 | getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
| 532 | // FIXME This code must be replaced by actual constructing of the |
| 533 | // lastprivate variable. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 534 | if (RD) { |
| 535 | CXXConstructorDecl *CD = LookupDefaultConstructor(RD); |
| 536 | PartialDiagnostic PD = |
| 537 | PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); |
| 538 | if (!CD || |
| 539 | CheckConstructorAccess( |
| 540 | ELoc, CD, InitializedEntity::InitializeTemporary(Type), |
| 541 | CD->getAccess(), PD) == AR_inaccessible || |
| 542 | CD->isDeleted()) { |
| 543 | Diag(ELoc, diag::err_omp_required_method) |
| 544 | << getOpenMPClauseName(OMPC_lastprivate) << 0; |
| 545 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 546 | VarDecl::DeclarationOnly; |
| 547 | Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl |
| 548 | : diag::note_defined_here) |
| 549 | << VD; |
| 550 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 551 | continue; |
| 552 | } |
| 553 | MarkFunctionReferenced(ELoc, CD); |
| 554 | DiagnoseUseOfDecl(CD, ELoc); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 562 | DSAStack->pop(); |
| 563 | DiscardCleanupsInEvaluationContext(); |
| 564 | PopExpressionEvaluationContext(); |
| 565 | } |
| 566 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 567 | namespace { |
| 568 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 569 | class VarDeclFilterCCC : public CorrectionCandidateCallback { |
| 570 | private: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 571 | Sema &SemaRef; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 572 | |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 573 | public: |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 574 | explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 575 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 576 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 577 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { |
| 578 | return VD->hasGlobalStorage() && |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 579 | SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), |
| 580 | SemaRef.getCurScope()); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 581 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 582 | return false; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 583 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 584 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 585 | } // namespace |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 586 | |
| 587 | ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, |
| 588 | CXXScopeSpec &ScopeSpec, |
| 589 | const DeclarationNameInfo &Id) { |
| 590 | LookupResult Lookup(*this, Id, LookupOrdinaryName); |
| 591 | LookupParsedName(Lookup, CurScope, &ScopeSpec, true); |
| 592 | |
| 593 | if (Lookup.isAmbiguous()) |
| 594 | return ExprError(); |
| 595 | |
| 596 | VarDecl *VD; |
| 597 | if (!Lookup.isSingleResult()) { |
| 598 | VarDeclFilterCCC Validator(*this); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 599 | if (TypoCorrection Corrected = |
| 600 | CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator, |
| 601 | CTK_ErrorRecovery)) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 602 | diagnoseTypo(Corrected, |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 603 | PDiag(Lookup.empty() |
| 604 | ? diag::err_undeclared_var_use_suggest |
| 605 | : diag::err_omp_expected_var_arg_suggest) |
| 606 | << Id.getName()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 607 | VD = Corrected.getCorrectionDeclAs<VarDecl>(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 608 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 609 | Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use |
| 610 | : diag::err_omp_expected_var_arg) |
| 611 | << Id.getName(); |
| 612 | return ExprError(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 613 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 614 | } else { |
| 615 | if (!(VD = Lookup.getAsSingle<VarDecl>())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 616 | Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 617 | Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); |
| 618 | return ExprError(); |
| 619 | } |
| 620 | } |
| 621 | Lookup.suppressDiagnostics(); |
| 622 | |
| 623 | // OpenMP [2.9.2, Syntax, C/C++] |
| 624 | // Variables must be file-scope, namespace-scope, or static block-scope. |
| 625 | if (!VD->hasGlobalStorage()) { |
| 626 | Diag(Id.getLoc(), diag::err_omp_global_var_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 627 | << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); |
| 628 | bool IsDecl = |
| 629 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 630 | Diag(VD->getLocation(), |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 631 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 632 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 633 | return ExprError(); |
| 634 | } |
| 635 | |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 636 | VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 637 | NamedDecl *ND = cast<NamedDecl>(CanonicalVD); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 638 | // OpenMP [2.9.2, Restrictions, C/C++, p.2] |
| 639 | // A threadprivate directive for file-scope variables must appear outside |
| 640 | // any definition or declaration. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 641 | if (CanonicalVD->getDeclContext()->isTranslationUnit() && |
| 642 | !getCurLexicalContext()->isTranslationUnit()) { |
| 643 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 644 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 645 | bool IsDecl = |
| 646 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 647 | Diag(VD->getLocation(), |
| 648 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 649 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 650 | return ExprError(); |
| 651 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 652 | // OpenMP [2.9.2, Restrictions, C/C++, p.3] |
| 653 | // A threadprivate directive for static class member variables must appear |
| 654 | // in the class definition, in the same scope in which the member |
| 655 | // variables are declared. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 656 | if (CanonicalVD->isStaticDataMember() && |
| 657 | !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { |
| 658 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 659 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 660 | bool IsDecl = |
| 661 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 662 | Diag(VD->getLocation(), |
| 663 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 664 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 665 | return ExprError(); |
| 666 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 667 | // OpenMP [2.9.2, Restrictions, C/C++, p.4] |
| 668 | // A threadprivate directive for namespace-scope variables must appear |
| 669 | // outside any definition or declaration other than the namespace |
| 670 | // definition itself. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 671 | if (CanonicalVD->getDeclContext()->isNamespace() && |
| 672 | (!getCurLexicalContext()->isFileContext() || |
| 673 | !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { |
| 674 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 675 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 676 | bool IsDecl = |
| 677 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 678 | Diag(VD->getLocation(), |
| 679 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 680 | << VD; |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 681 | return ExprError(); |
| 682 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 683 | // OpenMP [2.9.2, Restrictions, C/C++, p.6] |
| 684 | // A threadprivate directive for static block-scope variables must appear |
| 685 | // in the scope of the variable and not in a nested scope. |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 686 | if (CanonicalVD->isStaticLocal() && CurScope && |
| 687 | !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 688 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 689 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 690 | bool IsDecl = |
| 691 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 692 | Diag(VD->getLocation(), |
| 693 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 694 | << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 695 | return ExprError(); |
| 696 | } |
| 697 | |
| 698 | // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] |
| 699 | // A threadprivate directive must lexically precede all references to any |
| 700 | // of the variables in its list. |
| 701 | if (VD->isUsed()) { |
| 702 | Diag(Id.getLoc(), diag::err_omp_var_used) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 703 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 704 | return ExprError(); |
| 705 | } |
| 706 | |
| 707 | QualType ExprType = VD->getType().getNonReferenceType(); |
Alexey Bataev | d178ad4 | 2014-03-07 08:03:37 +0000 | [diff] [blame] | 708 | ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc()); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 709 | return DE; |
| 710 | } |
| 711 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 712 | Sema::DeclGroupPtrTy |
| 713 | Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, |
| 714 | ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 715 | if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 716 | CurContext->addDecl(D); |
| 717 | return DeclGroupPtrTy::make(DeclGroupRef(D)); |
| 718 | } |
| 719 | return DeclGroupPtrTy(); |
| 720 | } |
| 721 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 722 | namespace { |
| 723 | class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { |
| 724 | Sema &SemaRef; |
| 725 | |
| 726 | public: |
| 727 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
| 728 | if (auto VD = dyn_cast<VarDecl>(E->getDecl())) { |
| 729 | if (VD->hasLocalStorage()) { |
| 730 | SemaRef.Diag(E->getLocStart(), |
| 731 | diag::err_omp_local_var_in_threadprivate_init) |
| 732 | << E->getSourceRange(); |
| 733 | SemaRef.Diag(VD->getLocation(), diag::note_defined_here) |
| 734 | << VD << VD->getSourceRange(); |
| 735 | return true; |
| 736 | } |
| 737 | } |
| 738 | return false; |
| 739 | } |
| 740 | bool VisitStmt(const Stmt *S) { |
| 741 | for (auto Child : S->children()) { |
| 742 | if (Child && Visit(Child)) |
| 743 | return true; |
| 744 | } |
| 745 | return false; |
| 746 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 747 | explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 748 | }; |
| 749 | } // namespace |
| 750 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 751 | OMPThreadPrivateDecl * |
| 752 | Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 753 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 754 | for (auto &RefExpr : VarList) { |
| 755 | DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 756 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 757 | SourceLocation ILoc = DE->getExprLoc(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 758 | |
| 759 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 760 | // A threadprivate variable must not have an incomplete type. |
| 761 | if (RequireCompleteType(ILoc, VD->getType(), |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 762 | diag::err_omp_threadprivate_incomplete_type)) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 763 | continue; |
| 764 | } |
| 765 | |
| 766 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 767 | // A threadprivate variable must not have a reference type. |
| 768 | if (VD->getType()->isReferenceType()) { |
| 769 | Diag(ILoc, diag::err_omp_ref_type_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 770 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); |
| 771 | bool IsDecl = |
| 772 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 773 | Diag(VD->getLocation(), |
| 774 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 775 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 776 | continue; |
| 777 | } |
| 778 | |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 779 | // Check if this is a TLS variable. |
| 780 | if (VD->getTLSKind()) { |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 781 | Diag(ILoc, diag::err_omp_var_thread_local) << VD; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 782 | bool IsDecl = |
| 783 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 784 | Diag(VD->getLocation(), |
| 785 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 786 | << VD; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 787 | continue; |
| 788 | } |
| 789 | |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 790 | // Check if initial value of threadprivate variable reference variable with |
| 791 | // local storage (it is not supported by runtime). |
| 792 | if (auto Init = VD->getAnyInitializer()) { |
| 793 | LocalVarRefChecker Checker(*this); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 794 | if (Checker.Visit(Init)) |
| 795 | continue; |
Alexey Bataev | 18b92ee | 2014-05-28 07:40:25 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 798 | Vars.push_back(RefExpr); |
Alexey Bataev | d178ad4 | 2014-03-07 08:03:37 +0000 | [diff] [blame] | 799 | DSAStack->addDSA(VD, DE, OMPC_threadprivate); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 800 | } |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 801 | OMPThreadPrivateDecl *D = nullptr; |
Alexey Bataev | ec3da87 | 2014-01-31 05:15:34 +0000 | [diff] [blame] | 802 | if (!Vars.empty()) { |
| 803 | D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, |
| 804 | Vars); |
| 805 | D->setAccess(AS_public); |
| 806 | } |
| 807 | return D; |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 808 | } |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 809 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 810 | static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, |
| 811 | const VarDecl *VD, DSAStackTy::DSAVarData DVar, |
| 812 | bool IsLoopIterVar = false) { |
| 813 | if (DVar.RefExpr) { |
| 814 | SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
| 815 | << getOpenMPClauseName(DVar.CKind); |
| 816 | return; |
| 817 | } |
| 818 | enum { |
| 819 | PDSA_StaticMemberShared, |
| 820 | PDSA_StaticLocalVarShared, |
| 821 | PDSA_LoopIterVarPrivate, |
| 822 | PDSA_LoopIterVarLinear, |
| 823 | PDSA_LoopIterVarLastprivate, |
| 824 | PDSA_ConstVarShared, |
| 825 | PDSA_GlobalVarShared, |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 826 | PDSA_TaskVarFirstprivate, |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 827 | PDSA_LocalVarPrivate, |
| 828 | PDSA_Implicit |
| 829 | } Reason = PDSA_Implicit; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 830 | bool ReportHint = false; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 831 | auto ReportLoc = VD->getLocation(); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 832 | if (IsLoopIterVar) { |
| 833 | if (DVar.CKind == OMPC_private) |
| 834 | Reason = PDSA_LoopIterVarPrivate; |
| 835 | else if (DVar.CKind == OMPC_lastprivate) |
| 836 | Reason = PDSA_LoopIterVarLastprivate; |
| 837 | else |
| 838 | Reason = PDSA_LoopIterVarLinear; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 839 | } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) { |
| 840 | Reason = PDSA_TaskVarFirstprivate; |
| 841 | ReportLoc = DVar.ImplicitDSALoc; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 842 | } else if (VD->isStaticLocal()) |
| 843 | Reason = PDSA_StaticLocalVarShared; |
| 844 | else if (VD->isStaticDataMember()) |
| 845 | Reason = PDSA_StaticMemberShared; |
| 846 | else if (VD->isFileVarDecl()) |
| 847 | Reason = PDSA_GlobalVarShared; |
| 848 | else if (VD->getType().isConstant(SemaRef.getASTContext())) |
| 849 | Reason = PDSA_ConstVarShared; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 850 | else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 851 | ReportHint = true; |
| 852 | Reason = PDSA_LocalVarPrivate; |
| 853 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 854 | if (Reason != PDSA_Implicit) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 855 | SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 856 | << Reason << ReportHint |
| 857 | << getOpenMPDirectiveName(Stack->getCurrentDirective()); |
| 858 | } else if (DVar.ImplicitDSALoc.isValid()) { |
| 859 | SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) |
| 860 | << getOpenMPClauseName(DVar.CKind); |
| 861 | } |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 864 | namespace { |
| 865 | class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { |
| 866 | DSAStackTy *Stack; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 867 | Sema &SemaRef; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 868 | bool ErrorFound; |
| 869 | CapturedStmt *CS; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 870 | llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 871 | llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 872 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 873 | public: |
| 874 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 875 | if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 876 | // Skip internally declared variables. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 877 | if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) |
| 878 | return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 879 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 880 | auto DVar = Stack->getTopDSA(VD, false); |
| 881 | // Check if the variable has explicit DSA set and stop analysis if it so. |
| 882 | if (DVar.RefExpr) return; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 883 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 884 | auto ELoc = E->getExprLoc(); |
| 885 | auto DKind = Stack->getCurrentDirective(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 886 | // The default(none) clause requires that each variable that is referenced |
| 887 | // in the construct, and does not have a predetermined data-sharing |
| 888 | // attribute, must have its data-sharing attribute explicitly determined |
| 889 | // by being listed in a data-sharing attribute clause. |
| 890 | if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 891 | isParallelOrTaskRegion(DKind) && |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 892 | VarsWithInheritedDSA.count(VD) == 0) { |
| 893 | VarsWithInheritedDSA[VD] = E; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 894 | return; |
| 895 | } |
| 896 | |
| 897 | // OpenMP [2.9.3.6, Restrictions, p.2] |
| 898 | // A list item that appears in a reduction clause of the innermost |
| 899 | // enclosing worksharing or parallel construct may not be accessed in an |
| 900 | // explicit task. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 901 | DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 902 | [](OpenMPDirectiveKind K) -> bool { |
| 903 | return isOpenMPParallelDirective(K) || |
| 904 | isOpenMPWorksharingDirective(K); |
| 905 | }, |
| 906 | false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 907 | if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) { |
| 908 | ErrorFound = true; |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 909 | SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); |
| 910 | ReportOriginalDSA(SemaRef, Stack, VD, DVar); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 913 | |
| 914 | // Define implicit data-sharing attributes for task. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 915 | DVar = Stack->getImplicitDSA(VD, false); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 916 | if (DKind == OMPD_task && DVar.CKind != OMPC_shared) |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 917 | ImplicitFirstprivate.push_back(E); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | void VisitOMPExecutableDirective(OMPExecutableDirective *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 921 | for (auto *C : S->clauses()) { |
| 922 | // Skip analysis of arguments of implicitly defined firstprivate clause |
| 923 | // for task directives. |
| 924 | if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) |
| 925 | for (auto *CC : C->children()) { |
| 926 | if (CC) |
| 927 | Visit(CC); |
| 928 | } |
| 929 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 930 | } |
| 931 | void VisitStmt(Stmt *S) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 932 | for (auto *C : S->children()) { |
| 933 | if (C && !isa<OMPExecutableDirective>(C)) |
| 934 | Visit(C); |
| 935 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 936 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 937 | |
| 938 | bool isErrorFound() { return ErrorFound; } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 939 | ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 940 | llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() { |
| 941 | return VarsWithInheritedDSA; |
| 942 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 943 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 944 | DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) |
| 945 | : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 946 | }; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 947 | } // namespace |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 948 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 949 | void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 950 | switch (DKind) { |
| 951 | case OMPD_parallel: { |
| 952 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 953 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 954 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 955 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 956 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 957 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 958 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 959 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 960 | Params); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 961 | break; |
| 962 | } |
| 963 | case OMPD_simd: { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 964 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 965 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 966 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 967 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 968 | Params); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 969 | break; |
| 970 | } |
| 971 | case OMPD_for: { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 972 | Sema::CapturedParamNameType Params[] = { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 973 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 974 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 975 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 976 | Params); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 977 | break; |
| 978 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 979 | case OMPD_sections: { |
| 980 | Sema::CapturedParamNameType Params[] = { |
| 981 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 982 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 983 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 984 | Params); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 987 | case OMPD_section: { |
| 988 | Sema::CapturedParamNameType Params[] = { |
| 989 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 990 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 991 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 992 | Params); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 993 | break; |
| 994 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 995 | case OMPD_single: { |
| 996 | Sema::CapturedParamNameType Params[] = { |
| 997 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 998 | }; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 999 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1000 | Params); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1001 | break; |
| 1002 | } |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1003 | case OMPD_master: { |
| 1004 | Sema::CapturedParamNameType Params[] = { |
| 1005 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1006 | }; |
| 1007 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1008 | Params); |
| 1009 | break; |
| 1010 | } |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1011 | case OMPD_parallel_for: { |
| 1012 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 1013 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
| 1014 | Sema::CapturedParamNameType Params[] = { |
| 1015 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 1016 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 1017 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1018 | }; |
| 1019 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1020 | Params); |
| 1021 | break; |
| 1022 | } |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1023 | case OMPD_parallel_sections: { |
| 1024 | Sema::CapturedParamNameType Params[] = { |
| 1025 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1026 | }; |
| 1027 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1028 | Params); |
| 1029 | break; |
| 1030 | } |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1031 | case OMPD_task: { |
| 1032 | Sema::CapturedParamNameType Params[] = { |
| 1033 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1034 | }; |
| 1035 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1036 | Params); |
| 1037 | break; |
| 1038 | } |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1039 | case OMPD_taskyield: { |
| 1040 | Sema::CapturedParamNameType Params[] = { |
| 1041 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 1042 | }; |
| 1043 | ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, |
| 1044 | Params); |
| 1045 | break; |
| 1046 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1047 | case OMPD_threadprivate: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1048 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 1049 | case OMPD_unknown: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1050 | llvm_unreachable("Unknown OpenMP directive"); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1054 | bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, |
| 1055 | OpenMPDirectiveKind CurrentRegion, |
| 1056 | SourceLocation StartLoc) { |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1057 | // Allowed nesting of constructs |
| 1058 | // +------------------+-----------------+------------------------------------+ |
| 1059 | // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)| |
| 1060 | // +------------------+-----------------+------------------------------------+ |
| 1061 | // | parallel | parallel | * | |
| 1062 | // | parallel | for | * | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1063 | // | parallel | master | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1064 | // | parallel | simd | * | |
| 1065 | // | parallel | sections | * | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1066 | // | parallel | section | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1067 | // | parallel | single | * | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1068 | // | parallel | parallel for | * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1069 | // | parallel |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1070 | // | parallel | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1071 | // | parallel | taskyield | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1072 | // +------------------+-----------------+------------------------------------+ |
| 1073 | // | for | parallel | * | |
| 1074 | // | for | for | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1075 | // | for | master | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1076 | // | for | simd | * | |
| 1077 | // | for | sections | + | |
| 1078 | // | for | section | + | |
| 1079 | // | for | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1080 | // | for | parallel for | * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1081 | // | for |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1082 | // | for | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1083 | // | for | taskyield | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1084 | // +------------------+-----------------+------------------------------------+ |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1085 | // | master | parallel | * | |
| 1086 | // | master | for | + | |
| 1087 | // | master | master | * | |
| 1088 | // | master | simd | * | |
| 1089 | // | master | sections | + | |
| 1090 | // | master | section | + | |
| 1091 | // | master | single | + | |
| 1092 | // | master | parallel for | * | |
| 1093 | // | master |parallel sections| * | |
| 1094 | // | master | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1095 | // | master | taskyield | * | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1096 | // +------------------+-----------------+------------------------------------+ |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1097 | // | simd | parallel | | |
| 1098 | // | simd | for | | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1099 | // | simd | master | | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1100 | // | simd | simd | | |
| 1101 | // | simd | sections | | |
| 1102 | // | simd | section | | |
| 1103 | // | simd | single | | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1104 | // | simd | parallel for | | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1105 | // | simd |parallel sections| | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1106 | // | simd | task | | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1107 | // | simd | taskyield | | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1108 | // +------------------+-----------------+------------------------------------+ |
| 1109 | // | sections | parallel | * | |
| 1110 | // | sections | for | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1111 | // | sections | master | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1112 | // | sections | simd | * | |
| 1113 | // | sections | sections | + | |
| 1114 | // | sections | section | * | |
| 1115 | // | sections | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1116 | // | sections | parallel for | * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1117 | // | sections |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1118 | // | sections | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1119 | // | sections | taskyield | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1120 | // +------------------+-----------------+------------------------------------+ |
| 1121 | // | section | parallel | * | |
| 1122 | // | section | for | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1123 | // | section | master | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1124 | // | section | simd | * | |
| 1125 | // | section | sections | + | |
| 1126 | // | section | section | + | |
| 1127 | // | section | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1128 | // | section | parallel for | * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1129 | // | section |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1130 | // | section | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1131 | // | section | taskyield | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1132 | // +------------------+-----------------+------------------------------------+ |
| 1133 | // | single | parallel | * | |
| 1134 | // | single | for | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1135 | // | single | master | + | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1136 | // | single | simd | * | |
| 1137 | // | single | sections | + | |
| 1138 | // | single | section | + | |
| 1139 | // | single | single | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1140 | // | single | parallel for | * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1141 | // | single |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1142 | // | single | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1143 | // | single | taskyield | * | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1144 | // +------------------+-----------------+------------------------------------+ |
| 1145 | // | parallel for | parallel | * | |
| 1146 | // | parallel for | for | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1147 | // | parallel for | master | + | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1148 | // | parallel for | simd | * | |
| 1149 | // | parallel for | sections | + | |
| 1150 | // | parallel for | section | + | |
| 1151 | // | parallel for | single | + | |
| 1152 | // | parallel for | parallel for | * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1153 | // | parallel for |parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1154 | // | parallel for | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1155 | // | parallel for | taskyield | * | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1156 | // +------------------+-----------------+------------------------------------+ |
| 1157 | // | parallel sections| parallel | * | |
| 1158 | // | parallel sections| for | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1159 | // | parallel sections| master | + | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1160 | // | parallel sections| simd | * | |
| 1161 | // | parallel sections| sections | + | |
| 1162 | // | parallel sections| section | * | |
| 1163 | // | parallel sections| single | + | |
| 1164 | // | parallel sections| parallel for | * | |
| 1165 | // | parallel sections|parallel sections| * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1166 | // | parallel sections| task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1167 | // | parallel sections| taskyield | * | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1168 | // +------------------+-----------------+------------------------------------+ |
| 1169 | // | task | parallel | * | |
| 1170 | // | task | for | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1171 | // | task | master | + | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1172 | // | task | simd | * | |
| 1173 | // | task | sections | + | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1174 | // | task | section | + | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1175 | // | task | single | + | |
| 1176 | // | task | parallel for | * | |
| 1177 | // | task |parallel sections| * | |
| 1178 | // | task | task | * | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1179 | // | task | taskyield | * | |
Alexey Bataev | 18eb25e | 2014-06-30 10:22:46 +0000 | [diff] [blame] | 1180 | // +------------------+-----------------+------------------------------------+ |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1181 | if (Stack->getCurScope()) { |
| 1182 | auto ParentRegion = Stack->getParentDirective(); |
| 1183 | bool NestingProhibited = false; |
| 1184 | bool CloseNesting = true; |
| 1185 | bool ShouldBeInParallelRegion = false; |
| 1186 | if (isOpenMPSimdDirective(ParentRegion)) { |
| 1187 | // OpenMP [2.16, Nesting of Regions] |
| 1188 | // OpenMP constructs may not be nested inside a simd region. |
| 1189 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd); |
| 1190 | return true; |
| 1191 | } |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1192 | if (CurrentRegion == OMPD_section) { |
| 1193 | // OpenMP [2.7.2, sections Construct, Restrictions] |
| 1194 | // Orphaned section directives are prohibited. That is, the section |
| 1195 | // directives must appear within the sections construct and must not be |
| 1196 | // encountered elsewhere in the sections region. |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1197 | if (ParentRegion != OMPD_sections && |
| 1198 | ParentRegion != OMPD_parallel_sections) { |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1199 | SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) |
| 1200 | << (ParentRegion != OMPD_unknown) |
| 1201 | << getOpenMPDirectiveName(ParentRegion); |
| 1202 | return true; |
| 1203 | } |
| 1204 | return false; |
| 1205 | } |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1206 | if (CurrentRegion == OMPD_master) { |
| 1207 | // OpenMP [2.16, Nesting of Regions] |
| 1208 | // A master region may not be closely nested inside a worksharing, |
| 1209 | // atomic (TODO), or explicit task region. |
| 1210 | NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || |
| 1211 | ParentRegion == OMPD_task; |
| 1212 | } else if (isOpenMPWorksharingDirective(CurrentRegion) && |
| 1213 | !isOpenMPParallelDirective(CurrentRegion) && |
| 1214 | !isOpenMPSimdDirective(CurrentRegion)) { |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1215 | // OpenMP [2.16, Nesting of Regions] |
| 1216 | // A worksharing region may not be closely nested inside a worksharing, |
| 1217 | // explicit task, critical, ordered, atomic, or master region. |
| 1218 | // TODO |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1219 | NestingProhibited = (isOpenMPWorksharingDirective(ParentRegion) && |
| 1220 | !isOpenMPSimdDirective(ParentRegion)) || |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1221 | ParentRegion == OMPD_task || |
| 1222 | ParentRegion == OMPD_master; |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1223 | ShouldBeInParallelRegion = true; |
| 1224 | } |
| 1225 | if (NestingProhibited) { |
| 1226 | SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) |
Alexey Bataev | 41b9732 | 2014-07-02 03:04:53 +0000 | [diff] [blame] | 1227 | << CloseNesting << getOpenMPDirectiveName(ParentRegion) |
| 1228 | << ShouldBeInParallelRegion << getOpenMPDirectiveName(CurrentRegion); |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1229 | return true; |
| 1230 | } |
| 1231 | } |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1235 | StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, |
| 1236 | ArrayRef<OMPClause *> Clauses, |
| 1237 | Stmt *AStmt, |
| 1238 | SourceLocation StartLoc, |
| 1239 | SourceLocation EndLoc) { |
| 1240 | StmtResult Res = StmtError(); |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 1241 | if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc)) |
| 1242 | return StmtError(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1243 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1244 | llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1245 | llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1246 | bool ErrorFound = false; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1247 | if (AStmt) { |
| 1248 | assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1249 | |
| 1250 | // Check default data sharing attributes for referenced variables. |
| 1251 | DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); |
| 1252 | DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); |
| 1253 | if (DSAChecker.isErrorFound()) |
| 1254 | return StmtError(); |
| 1255 | // Generate list of implicitly defined firstprivate variables. |
| 1256 | VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); |
| 1257 | ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); |
| 1258 | |
| 1259 | if (!DSAChecker.getImplicitFirstprivate().empty()) { |
| 1260 | if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( |
| 1261 | DSAChecker.getImplicitFirstprivate(), SourceLocation(), |
| 1262 | SourceLocation(), SourceLocation())) { |
| 1263 | ClausesWithImplicit.push_back(Implicit); |
| 1264 | ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != |
| 1265 | DSAChecker.getImplicitFirstprivate().size(); |
| 1266 | } else |
| 1267 | ErrorFound = true; |
| 1268 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1269 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1270 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1271 | switch (Kind) { |
| 1272 | case OMPD_parallel: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1273 | Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1274 | EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1275 | break; |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1276 | case OMPD_simd: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1277 | Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 1278 | VarsWithInheritedDSA); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1279 | break; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1280 | case OMPD_for: |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1281 | Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, |
| 1282 | VarsWithInheritedDSA); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1283 | break; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1284 | case OMPD_sections: |
| 1285 | Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1286 | EndLoc); |
| 1287 | break; |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1288 | case OMPD_section: |
| 1289 | assert(ClausesWithImplicit.empty() && |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1290 | "No clauses are allowed for 'omp section' directive"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1291 | Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); |
| 1292 | break; |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1293 | case OMPD_single: |
| 1294 | Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1295 | EndLoc); |
| 1296 | break; |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1297 | case OMPD_master: |
| 1298 | assert(ClausesWithImplicit.empty() && |
| 1299 | "No clauses are allowed for 'omp master' directive"); |
| 1300 | Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); |
| 1301 | break; |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1302 | case OMPD_parallel_for: |
| 1303 | Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 1304 | EndLoc, VarsWithInheritedDSA); |
| 1305 | break; |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1306 | case OMPD_parallel_sections: |
| 1307 | Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, |
| 1308 | StartLoc, EndLoc); |
| 1309 | break; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1310 | case OMPD_task: |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1311 | Res = |
| 1312 | ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
| 1313 | break; |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1314 | case OMPD_taskyield: |
| 1315 | assert(ClausesWithImplicit.empty() && |
| 1316 | "No clauses are allowed for 'omp taskyield' directive"); |
| 1317 | assert(AStmt == nullptr && |
| 1318 | "No associated statement allowed for 'omp taskyield' directive"); |
| 1319 | Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); |
| 1320 | break; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1321 | case OMPD_threadprivate: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1322 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 1323 | case OMPD_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1324 | llvm_unreachable("Unknown OpenMP directive"); |
| 1325 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1326 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1327 | for (auto P : VarsWithInheritedDSA) { |
| 1328 | Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) |
| 1329 | << P.first << P.second->getSourceRange(); |
| 1330 | } |
| 1331 | if (!VarsWithInheritedDSA.empty()) |
| 1332 | return StmtError(); |
| 1333 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 1334 | if (ErrorFound) |
| 1335 | return StmtError(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1336 | return Res; |
| 1337 | } |
| 1338 | |
| 1339 | StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, |
| 1340 | Stmt *AStmt, |
| 1341 | SourceLocation StartLoc, |
| 1342 | SourceLocation EndLoc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1343 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1344 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 1345 | // 1.2.2 OpenMP Language Terminology |
| 1346 | // Structured block - An executable statement with a single entry at the |
| 1347 | // top and a single exit at the bottom. |
| 1348 | // The point of exit cannot be a branch out of the structured block. |
| 1349 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 1350 | CS->getCapturedDecl()->setNothrow(); |
| 1351 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1352 | getCurFunction()->setHasBranchProtectedScope(); |
| 1353 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1354 | return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 1355 | AStmt); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1358 | namespace { |
| 1359 | /// \brief Helper class for checking canonical form of the OpenMP loops and |
| 1360 | /// extracting iteration space of each loop in the loop nest, that will be used |
| 1361 | /// for IR generation. |
| 1362 | class OpenMPIterationSpaceChecker { |
| 1363 | /// \brief Reference to Sema. |
| 1364 | Sema &SemaRef; |
| 1365 | /// \brief A location for diagnostics (when there is no some better location). |
| 1366 | SourceLocation DefaultLoc; |
| 1367 | /// \brief A location for diagnostics (when increment is not compatible). |
| 1368 | SourceLocation ConditionLoc; |
| 1369 | /// \brief A source location for referring to condition later. |
| 1370 | SourceRange ConditionSrcRange; |
| 1371 | /// \brief Loop variable. |
| 1372 | VarDecl *Var; |
| 1373 | /// \brief Lower bound (initializer for the var). |
| 1374 | Expr *LB; |
| 1375 | /// \brief Upper bound. |
| 1376 | Expr *UB; |
| 1377 | /// \brief Loop step (increment). |
| 1378 | Expr *Step; |
| 1379 | /// \brief This flag is true when condition is one of: |
| 1380 | /// Var < UB |
| 1381 | /// Var <= UB |
| 1382 | /// UB > Var |
| 1383 | /// UB >= Var |
| 1384 | bool TestIsLessOp; |
| 1385 | /// \brief This flag is true when condition is strict ( < or > ). |
| 1386 | bool TestIsStrictOp; |
| 1387 | /// \brief This flag is true when step is subtracted on each iteration. |
| 1388 | bool SubtractStep; |
| 1389 | |
| 1390 | public: |
| 1391 | OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) |
| 1392 | : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc), |
| 1393 | ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr), |
| 1394 | UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false), |
| 1395 | SubtractStep(false) {} |
| 1396 | /// \brief Check init-expr for canonical loop form and save loop counter |
| 1397 | /// variable - #Var and its initialization value - #LB. |
| 1398 | bool CheckInit(Stmt *S); |
| 1399 | /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags |
| 1400 | /// for less/greater and for strict/non-strict comparison. |
| 1401 | bool CheckCond(Expr *S); |
| 1402 | /// \brief Check incr-expr for canonical loop form and return true if it |
| 1403 | /// does not conform, otherwise save loop step (#Step). |
| 1404 | bool CheckInc(Expr *S); |
| 1405 | /// \brief Return the loop counter variable. |
| 1406 | VarDecl *GetLoopVar() const { return Var; } |
| 1407 | /// \brief Return true if any expression is dependent. |
| 1408 | bool Dependent() const; |
| 1409 | |
| 1410 | private: |
| 1411 | /// \brief Check the right-hand side of an assignment in the increment |
| 1412 | /// expression. |
| 1413 | bool CheckIncRHS(Expr *RHS); |
| 1414 | /// \brief Helper to set loop counter variable and its initializer. |
| 1415 | bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB); |
| 1416 | /// \brief Helper to set upper bound. |
| 1417 | bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR, |
| 1418 | const SourceLocation &SL); |
| 1419 | /// \brief Helper to set loop increment. |
| 1420 | bool SetStep(Expr *NewStep, bool Subtract); |
| 1421 | }; |
| 1422 | |
| 1423 | bool OpenMPIterationSpaceChecker::Dependent() const { |
| 1424 | if (!Var) { |
| 1425 | assert(!LB && !UB && !Step); |
| 1426 | return false; |
| 1427 | } |
| 1428 | return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) || |
| 1429 | (UB && UB->isValueDependent()) || (Step && Step->isValueDependent()); |
| 1430 | } |
| 1431 | |
| 1432 | bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) { |
| 1433 | // State consistency checking to ensure correct usage. |
| 1434 | assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr && |
| 1435 | !TestIsLessOp && !TestIsStrictOp); |
| 1436 | if (!NewVar || !NewLB) |
| 1437 | return true; |
| 1438 | Var = NewVar; |
| 1439 | LB = NewLB; |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
| 1443 | bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, |
| 1444 | const SourceRange &SR, |
| 1445 | const SourceLocation &SL) { |
| 1446 | // State consistency checking to ensure correct usage. |
| 1447 | assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr && |
| 1448 | !TestIsLessOp && !TestIsStrictOp); |
| 1449 | if (!NewUB) |
| 1450 | return true; |
| 1451 | UB = NewUB; |
| 1452 | TestIsLessOp = LessOp; |
| 1453 | TestIsStrictOp = StrictOp; |
| 1454 | ConditionSrcRange = SR; |
| 1455 | ConditionLoc = SL; |
| 1456 | return false; |
| 1457 | } |
| 1458 | |
| 1459 | bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { |
| 1460 | // State consistency checking to ensure correct usage. |
| 1461 | assert(Var != nullptr && LB != nullptr && Step == nullptr); |
| 1462 | if (!NewStep) |
| 1463 | return true; |
| 1464 | if (!NewStep->isValueDependent()) { |
| 1465 | // Check that the step is integer expression. |
| 1466 | SourceLocation StepLoc = NewStep->getLocStart(); |
| 1467 | ExprResult Val = |
| 1468 | SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); |
| 1469 | if (Val.isInvalid()) |
| 1470 | return true; |
| 1471 | NewStep = Val.get(); |
| 1472 | |
| 1473 | // OpenMP [2.6, Canonical Loop Form, Restrictions] |
| 1474 | // If test-expr is of form var relational-op b and relational-op is < or |
| 1475 | // <= then incr-expr must cause var to increase on each iteration of the |
| 1476 | // loop. If test-expr is of form var relational-op b and relational-op is |
| 1477 | // > or >= then incr-expr must cause var to decrease on each iteration of |
| 1478 | // the loop. |
| 1479 | // If test-expr is of form b relational-op var and relational-op is < or |
| 1480 | // <= then incr-expr must cause var to decrease on each iteration of the |
| 1481 | // loop. If test-expr is of form b relational-op var and relational-op is |
| 1482 | // > or >= then incr-expr must cause var to increase on each iteration of |
| 1483 | // the loop. |
| 1484 | llvm::APSInt Result; |
| 1485 | bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); |
| 1486 | bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); |
| 1487 | bool IsConstNeg = |
| 1488 | IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); |
| 1489 | bool IsConstZero = IsConstant && !Result.getBoolValue(); |
| 1490 | if (UB && (IsConstZero || |
| 1491 | (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) |
| 1492 | : (!IsConstNeg || (IsUnsigned && !Subtract))))) { |
| 1493 | SemaRef.Diag(NewStep->getExprLoc(), |
| 1494 | diag::err_omp_loop_incr_not_compatible) |
| 1495 | << Var << TestIsLessOp << NewStep->getSourceRange(); |
| 1496 | SemaRef.Diag(ConditionLoc, |
| 1497 | diag::note_omp_loop_cond_requres_compatible_incr) |
| 1498 | << TestIsLessOp << ConditionSrcRange; |
| 1499 | return true; |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | Step = NewStep; |
| 1504 | SubtractStep = Subtract; |
| 1505 | return false; |
| 1506 | } |
| 1507 | |
| 1508 | bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) { |
| 1509 | // Check init-expr for canonical loop form and save loop counter |
| 1510 | // variable - #Var and its initialization value - #LB. |
| 1511 | // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: |
| 1512 | // var = lb |
| 1513 | // integer-type var = lb |
| 1514 | // random-access-iterator-type var = lb |
| 1515 | // pointer-type var = lb |
| 1516 | // |
| 1517 | if (!S) { |
| 1518 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); |
| 1519 | return true; |
| 1520 | } |
| 1521 | if (Expr *E = dyn_cast<Expr>(S)) |
| 1522 | S = E->IgnoreParens(); |
| 1523 | if (auto BO = dyn_cast<BinaryOperator>(S)) { |
| 1524 | if (BO->getOpcode() == BO_Assign) |
| 1525 | if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) |
| 1526 | return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS()); |
| 1527 | } else if (auto DS = dyn_cast<DeclStmt>(S)) { |
| 1528 | if (DS->isSingleDecl()) { |
| 1529 | if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { |
| 1530 | if (Var->hasInit()) { |
| 1531 | // Accept non-canonical init form here but emit ext. warning. |
| 1532 | if (Var->getInitStyle() != VarDecl::CInit) |
| 1533 | SemaRef.Diag(S->getLocStart(), |
| 1534 | diag::ext_omp_loop_not_canonical_init) |
| 1535 | << S->getSourceRange(); |
| 1536 | return SetVarAndLB(Var, Var->getInit()); |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) |
| 1541 | if (CE->getOperator() == OO_Equal) |
| 1542 | if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0))) |
| 1543 | return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1)); |
| 1544 | |
| 1545 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) |
| 1546 | << S->getSourceRange(); |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1550 | /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1551 | /// variable (which may be the loop variable) if possible. |
| 1552 | static const VarDecl *GetInitVarDecl(const Expr *E) { |
| 1553 | if (!E) |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 1554 | return nullptr; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1555 | E = E->IgnoreParenImpCasts(); |
| 1556 | if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) |
| 1557 | if (const CXXConstructorDecl *Ctor = CE->getConstructor()) |
| 1558 | if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 && |
| 1559 | CE->getArg(0) != nullptr) |
| 1560 | E = CE->getArg(0)->IgnoreParenImpCasts(); |
| 1561 | auto DRE = dyn_cast_or_null<DeclRefExpr>(E); |
| 1562 | if (!DRE) |
| 1563 | return nullptr; |
| 1564 | return dyn_cast<VarDecl>(DRE->getDecl()); |
| 1565 | } |
| 1566 | |
| 1567 | bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { |
| 1568 | // Check test-expr for canonical form, save upper-bound UB, flags for |
| 1569 | // less/greater and for strict/non-strict comparison. |
| 1570 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 1571 | // var relational-op b |
| 1572 | // b relational-op var |
| 1573 | // |
| 1574 | if (!S) { |
| 1575 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var; |
| 1576 | return true; |
| 1577 | } |
| 1578 | S = S->IgnoreParenImpCasts(); |
| 1579 | SourceLocation CondLoc = S->getLocStart(); |
| 1580 | if (auto BO = dyn_cast<BinaryOperator>(S)) { |
| 1581 | if (BO->isRelationalOp()) { |
| 1582 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 1583 | return SetUB(BO->getRHS(), |
| 1584 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), |
| 1585 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 1586 | BO->getSourceRange(), BO->getOperatorLoc()); |
| 1587 | if (GetInitVarDecl(BO->getRHS()) == Var) |
| 1588 | return SetUB(BO->getLHS(), |
| 1589 | (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), |
| 1590 | (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), |
| 1591 | BO->getSourceRange(), BO->getOperatorLoc()); |
| 1592 | } |
| 1593 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
| 1594 | if (CE->getNumArgs() == 2) { |
| 1595 | auto Op = CE->getOperator(); |
| 1596 | switch (Op) { |
| 1597 | case OO_Greater: |
| 1598 | case OO_GreaterEqual: |
| 1599 | case OO_Less: |
| 1600 | case OO_LessEqual: |
| 1601 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 1602 | return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, |
| 1603 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 1604 | CE->getOperatorLoc()); |
| 1605 | if (GetInitVarDecl(CE->getArg(1)) == Var) |
| 1606 | return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, |
| 1607 | Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), |
| 1608 | CE->getOperatorLoc()); |
| 1609 | break; |
| 1610 | default: |
| 1611 | break; |
| 1612 | } |
| 1613 | } |
| 1614 | } |
| 1615 | SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) |
| 1616 | << S->getSourceRange() << Var; |
| 1617 | return true; |
| 1618 | } |
| 1619 | |
| 1620 | bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { |
| 1621 | // RHS of canonical loop form increment can be: |
| 1622 | // var + incr |
| 1623 | // incr + var |
| 1624 | // var - incr |
| 1625 | // |
| 1626 | RHS = RHS->IgnoreParenImpCasts(); |
| 1627 | if (auto BO = dyn_cast<BinaryOperator>(RHS)) { |
| 1628 | if (BO->isAdditiveOp()) { |
| 1629 | bool IsAdd = BO->getOpcode() == BO_Add; |
| 1630 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 1631 | return SetStep(BO->getRHS(), !IsAdd); |
| 1632 | if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var) |
| 1633 | return SetStep(BO->getLHS(), false); |
| 1634 | } |
| 1635 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { |
| 1636 | bool IsAdd = CE->getOperator() == OO_Plus; |
| 1637 | if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { |
| 1638 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 1639 | return SetStep(CE->getArg(1), !IsAdd); |
| 1640 | if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var) |
| 1641 | return SetStep(CE->getArg(0), false); |
| 1642 | } |
| 1643 | } |
| 1644 | SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
| 1645 | << RHS->getSourceRange() << Var; |
| 1646 | return true; |
| 1647 | } |
| 1648 | |
| 1649 | bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { |
| 1650 | // Check incr-expr for canonical loop form and return true if it |
| 1651 | // does not conform. |
| 1652 | // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: |
| 1653 | // ++var |
| 1654 | // var++ |
| 1655 | // --var |
| 1656 | // var-- |
| 1657 | // var += incr |
| 1658 | // var -= incr |
| 1659 | // var = var + incr |
| 1660 | // var = incr + var |
| 1661 | // var = var - incr |
| 1662 | // |
| 1663 | if (!S) { |
| 1664 | SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var; |
| 1665 | return true; |
| 1666 | } |
| 1667 | S = S->IgnoreParens(); |
| 1668 | if (auto UO = dyn_cast<UnaryOperator>(S)) { |
| 1669 | if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var) |
| 1670 | return SetStep( |
| 1671 | SemaRef.ActOnIntegerConstant(UO->getLocStart(), |
| 1672 | (UO->isDecrementOp() ? -1 : 1)).get(), |
| 1673 | false); |
| 1674 | } else if (auto BO = dyn_cast<BinaryOperator>(S)) { |
| 1675 | switch (BO->getOpcode()) { |
| 1676 | case BO_AddAssign: |
| 1677 | case BO_SubAssign: |
| 1678 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 1679 | return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); |
| 1680 | break; |
| 1681 | case BO_Assign: |
| 1682 | if (GetInitVarDecl(BO->getLHS()) == Var) |
| 1683 | return CheckIncRHS(BO->getRHS()); |
| 1684 | break; |
| 1685 | default: |
| 1686 | break; |
| 1687 | } |
| 1688 | } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { |
| 1689 | switch (CE->getOperator()) { |
| 1690 | case OO_PlusPlus: |
| 1691 | case OO_MinusMinus: |
| 1692 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 1693 | return SetStep( |
| 1694 | SemaRef.ActOnIntegerConstant( |
| 1695 | CE->getLocStart(), |
| 1696 | ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(), |
| 1697 | false); |
| 1698 | break; |
| 1699 | case OO_PlusEqual: |
| 1700 | case OO_MinusEqual: |
| 1701 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 1702 | return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); |
| 1703 | break; |
| 1704 | case OO_Equal: |
| 1705 | if (GetInitVarDecl(CE->getArg(0)) == Var) |
| 1706 | return CheckIncRHS(CE->getArg(1)); |
| 1707 | break; |
| 1708 | default: |
| 1709 | break; |
| 1710 | } |
| 1711 | } |
| 1712 | SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) |
| 1713 | << S->getSourceRange() << Var; |
| 1714 | return true; |
| 1715 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1716 | } // namespace |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1717 | |
| 1718 | /// \brief Called on a for stmt to check and extract its iteration space |
| 1719 | /// for further processing (such as collapsing). |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1720 | static bool CheckOpenMPIterationSpace( |
| 1721 | OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, |
| 1722 | unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, |
| 1723 | Expr *NestedLoopCountExpr, |
| 1724 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1725 | // OpenMP [2.6, Canonical Loop Form] |
| 1726 | // for (init-expr; test-expr; incr-expr) structured-block |
| 1727 | auto For = dyn_cast_or_null<ForStmt>(S); |
| 1728 | if (!For) { |
| 1729 | SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 1730 | << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind) |
| 1731 | << NestedLoopCount << (CurrentNestedLoopCount > 0) |
| 1732 | << CurrentNestedLoopCount; |
| 1733 | if (NestedLoopCount > 1) |
| 1734 | SemaRef.Diag(NestedLoopCountExpr->getExprLoc(), |
| 1735 | diag::note_omp_collapse_expr) |
| 1736 | << NestedLoopCountExpr->getSourceRange(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1737 | return true; |
| 1738 | } |
| 1739 | assert(For->getBody()); |
| 1740 | |
| 1741 | OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); |
| 1742 | |
| 1743 | // Check init. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1744 | auto Init = For->getInit(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1745 | if (ISC.CheckInit(Init)) { |
| 1746 | return true; |
| 1747 | } |
| 1748 | |
| 1749 | bool HasErrors = false; |
| 1750 | |
| 1751 | // Check loop variable's type. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1752 | auto Var = ISC.GetLoopVar(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1753 | |
| 1754 | // OpenMP [2.6, Canonical Loop Form] |
| 1755 | // Var is one of the following: |
| 1756 | // A variable of signed or unsigned integer type. |
| 1757 | // For C++, a variable of a random access iterator type. |
| 1758 | // For C, a variable of a pointer type. |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1759 | auto VarType = Var->getType(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1760 | if (!VarType->isDependentType() && !VarType->isIntegerType() && |
| 1761 | !VarType->isPointerType() && |
| 1762 | !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { |
| 1763 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) |
| 1764 | << SemaRef.getLangOpts().CPlusPlus; |
| 1765 | HasErrors = true; |
| 1766 | } |
| 1767 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1768 | // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a |
| 1769 | // Construct |
| 1770 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 1771 | // parallel for construct is (are) private. |
| 1772 | // The loop iteration variable in the associated for-loop of a simd construct |
| 1773 | // with just one associated for-loop is linear with a constant-linear-step |
| 1774 | // that is the increment of the associated for-loop. |
| 1775 | // Exclude loop var from the list of variables with implicitly defined data |
| 1776 | // sharing attributes. |
| 1777 | while (VarsWithImplicitDSA.count(Var) > 0) |
| 1778 | VarsWithImplicitDSA.erase(Var); |
| 1779 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1780 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in |
| 1781 | // a Construct, C/C++]. |
Alexey Bataev | cefffae | 2014-06-23 08:21:53 +0000 | [diff] [blame] | 1782 | // The loop iteration variable in the associated for-loop of a simd construct |
| 1783 | // with just one associated for-loop may be listed in a linear clause with a |
| 1784 | // constant-linear-step that is the increment of the associated for-loop. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1785 | // The loop iteration variable(s) in the associated for-loop(s) of a for or |
| 1786 | // parallel for construct may be listed in a private or lastprivate clause. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1787 | DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1788 | auto PredeterminedCKind = |
| 1789 | isOpenMPSimdDirective(DKind) |
| 1790 | ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) |
| 1791 | : OMPC_private; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1792 | if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1793 | DVar.CKind != PredeterminedCKind) || |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1794 | (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown && |
| 1795 | DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 1796 | (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1797 | SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1798 | << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) |
| 1799 | << getOpenMPClauseName(PredeterminedCKind); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1800 | ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1801 | HasErrors = true; |
| 1802 | } else { |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1803 | // Make the loop iteration variable private (for worksharing constructs), |
| 1804 | // linear (for simd directives with the only one associated loop) or |
| 1805 | // lastprivate (for simd directives with several collapsed loops). |
| 1806 | DSA.addDSA(Var, nullptr, PredeterminedCKind); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 1809 | assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 1810 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1811 | // Check test-expr. |
| 1812 | HasErrors |= ISC.CheckCond(For->getCond()); |
| 1813 | |
| 1814 | // Check incr-expr. |
| 1815 | HasErrors |= ISC.CheckInc(For->getInc()); |
| 1816 | |
| 1817 | if (ISC.Dependent()) |
| 1818 | return HasErrors; |
| 1819 | |
| 1820 | // FIXME: Build loop's iteration space representation. |
| 1821 | return HasErrors; |
| 1822 | } |
| 1823 | |
| 1824 | /// \brief A helper routine to skip no-op (attributed, compound) stmts get the |
| 1825 | /// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt |
| 1826 | /// to get the first for loop. |
| 1827 | static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) { |
| 1828 | if (IgnoreCaptured) |
| 1829 | if (auto CapS = dyn_cast_or_null<CapturedStmt>(S)) |
| 1830 | S = CapS->getCapturedStmt(); |
| 1831 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 1832 | // All loops associated with the construct must be perfectly nested; that is, |
| 1833 | // there must be no intervening code nor any OpenMP directive between any two |
| 1834 | // loops. |
| 1835 | while (true) { |
| 1836 | if (auto AS = dyn_cast_or_null<AttributedStmt>(S)) |
| 1837 | S = AS->getSubStmt(); |
| 1838 | else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) { |
| 1839 | if (CS->size() != 1) |
| 1840 | break; |
| 1841 | S = CS->body_back(); |
| 1842 | } else |
| 1843 | break; |
| 1844 | } |
| 1845 | return S; |
| 1846 | } |
| 1847 | |
| 1848 | /// \brief Called on a for stmt to check itself and nested loops (if any). |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 1849 | /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, |
| 1850 | /// number of collapsed loops otherwise. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1851 | static unsigned |
| 1852 | CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr, |
| 1853 | Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA, |
| 1854 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 1855 | unsigned NestedLoopCount = 1; |
| 1856 | if (NestedLoopCountExpr) { |
| 1857 | // Found 'collapse' clause - calculate collapse number. |
| 1858 | llvm::APSInt Result; |
| 1859 | if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) |
| 1860 | NestedLoopCount = Result.getLimitedValue(); |
| 1861 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1862 | // This is helper routine for loop directives (e.g., 'for', 'simd', |
| 1863 | // 'for simd', etc.). |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1864 | Stmt *CurStmt = IgnoreContainerStmts(AStmt, true); |
| 1865 | for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 1866 | if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1867 | NestedLoopCount, NestedLoopCountExpr, |
| 1868 | VarsWithImplicitDSA)) |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 1869 | return 0; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1870 | // Move on to the next nested for loop, or to the loop body. |
| 1871 | CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false); |
| 1872 | } |
| 1873 | |
| 1874 | // FIXME: Build resulting iteration space for IR generation (collapsing |
| 1875 | // iteration spaces when loop count > 1 ('collapse' clause)). |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 1876 | return NestedLoopCount; |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 1879 | static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { |
Alexey Bataev | df9b159 | 2014-06-25 04:09:13 +0000 | [diff] [blame] | 1880 | auto CollapseFilter = [](const OMPClause *C) -> bool { |
| 1881 | return C->getClauseKind() == OMPC_collapse; |
| 1882 | }; |
| 1883 | OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I( |
| 1884 | Clauses, CollapseFilter); |
Alexey Bataev | e2f07d4 | 2014-06-24 12:55:56 +0000 | [diff] [blame] | 1885 | if (I) |
| 1886 | return cast<OMPCollapseClause>(*I)->getNumForLoops(); |
| 1887 | return nullptr; |
| 1888 | } |
| 1889 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1890 | StmtResult Sema::ActOnOpenMPSimdDirective( |
| 1891 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 1892 | SourceLocation EndLoc, |
| 1893 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 1894 | // In presence of clause 'collapse', it will define the nested loops number. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1895 | unsigned NestedLoopCount = |
| 1896 | CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, |
| 1897 | *DSAStack, VarsWithImplicitDSA); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 1898 | if (NestedLoopCount == 0) |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1899 | return StmtError(); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1900 | |
| 1901 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 1902 | return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 1903 | Clauses, AStmt); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1906 | StmtResult Sema::ActOnOpenMPForDirective( |
| 1907 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 1908 | SourceLocation EndLoc, |
| 1909 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1910 | // In presence of clause 'collapse', it will define the nested loops number. |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1911 | unsigned NestedLoopCount = |
| 1912 | CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, |
| 1913 | *DSAStack, VarsWithImplicitDSA); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 1914 | if (NestedLoopCount == 0) |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1915 | return StmtError(); |
| 1916 | |
| 1917 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | abfc069 | 2014-06-25 06:52:00 +0000 | [diff] [blame] | 1918 | return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, |
| 1919 | Clauses, AStmt); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1922 | StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 1923 | Stmt *AStmt, |
| 1924 | SourceLocation StartLoc, |
| 1925 | SourceLocation EndLoc) { |
| 1926 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1927 | auto BaseStmt = AStmt; |
| 1928 | while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
| 1929 | BaseStmt = CS->getCapturedStmt(); |
| 1930 | if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
| 1931 | auto S = C->children(); |
| 1932 | if (!S) |
| 1933 | return StmtError(); |
| 1934 | // All associated statements must be '#pragma omp section' except for |
| 1935 | // the first one. |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1936 | for (++S; S; ++S) { |
| 1937 | auto SectionStmt = *S; |
| 1938 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 1939 | if (SectionStmt) |
| 1940 | Diag(SectionStmt->getLocStart(), |
| 1941 | diag::err_omp_sections_substmt_not_section); |
| 1942 | return StmtError(); |
| 1943 | } |
| 1944 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1945 | } else { |
| 1946 | Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); |
| 1947 | return StmtError(); |
| 1948 | } |
| 1949 | |
| 1950 | getCurFunction()->setHasBranchProtectedScope(); |
| 1951 | |
| 1952 | return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, |
| 1953 | AStmt); |
| 1954 | } |
| 1955 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1956 | StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, |
| 1957 | SourceLocation StartLoc, |
| 1958 | SourceLocation EndLoc) { |
| 1959 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1960 | |
| 1961 | getCurFunction()->setHasBranchProtectedScope(); |
| 1962 | |
| 1963 | return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 1964 | } |
| 1965 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1966 | StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, |
| 1967 | Stmt *AStmt, |
| 1968 | SourceLocation StartLoc, |
| 1969 | SourceLocation EndLoc) { |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 1970 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1971 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1972 | getCurFunction()->setHasBranchProtectedScope(); |
Alexey Bataev | 74a05c9 | 2014-07-15 02:55:09 +0000 | [diff] [blame] | 1973 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1974 | return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 1975 | } |
| 1976 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1977 | StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, |
| 1978 | SourceLocation StartLoc, |
| 1979 | SourceLocation EndLoc) { |
| 1980 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1981 | |
| 1982 | getCurFunction()->setHasBranchProtectedScope(); |
| 1983 | |
| 1984 | return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); |
| 1985 | } |
| 1986 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1987 | StmtResult Sema::ActOnOpenMPParallelForDirective( |
| 1988 | ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, |
| 1989 | SourceLocation EndLoc, |
| 1990 | llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { |
| 1991 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 1992 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 1993 | // 1.2.2 OpenMP Language Terminology |
| 1994 | // Structured block - An executable statement with a single entry at the |
| 1995 | // top and a single exit at the bottom. |
| 1996 | // The point of exit cannot be a branch out of the structured block. |
| 1997 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 1998 | CS->getCapturedDecl()->setNothrow(); |
| 1999 | |
| 2000 | // In presence of clause 'collapse', it will define the nested loops number. |
| 2001 | unsigned NestedLoopCount = |
| 2002 | CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt, |
| 2003 | *this, *DSAStack, VarsWithImplicitDSA); |
| 2004 | if (NestedLoopCount == 0) |
| 2005 | return StmtError(); |
| 2006 | |
| 2007 | getCurFunction()->setHasBranchProtectedScope(); |
| 2008 | return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, |
| 2009 | NestedLoopCount, Clauses, AStmt); |
| 2010 | } |
| 2011 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2012 | StmtResult |
| 2013 | Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, |
| 2014 | Stmt *AStmt, SourceLocation StartLoc, |
| 2015 | SourceLocation EndLoc) { |
| 2016 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 2017 | auto BaseStmt = AStmt; |
| 2018 | while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) |
| 2019 | BaseStmt = CS->getCapturedStmt(); |
| 2020 | if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { |
| 2021 | auto S = C->children(); |
| 2022 | if (!S) |
| 2023 | return StmtError(); |
| 2024 | // All associated statements must be '#pragma omp section' except for |
| 2025 | // the first one. |
| 2026 | for (++S; S; ++S) { |
| 2027 | auto SectionStmt = *S; |
| 2028 | if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { |
| 2029 | if (SectionStmt) |
| 2030 | Diag(SectionStmt->getLocStart(), |
| 2031 | diag::err_omp_parallel_sections_substmt_not_section); |
| 2032 | return StmtError(); |
| 2033 | } |
| 2034 | } |
| 2035 | } else { |
| 2036 | Diag(AStmt->getLocStart(), |
| 2037 | diag::err_omp_parallel_sections_not_compound_stmt); |
| 2038 | return StmtError(); |
| 2039 | } |
| 2040 | |
| 2041 | getCurFunction()->setHasBranchProtectedScope(); |
| 2042 | |
| 2043 | return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc, |
| 2044 | Clauses, AStmt); |
| 2045 | } |
| 2046 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2047 | StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, |
| 2048 | Stmt *AStmt, SourceLocation StartLoc, |
| 2049 | SourceLocation EndLoc) { |
| 2050 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 2051 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 2052 | // 1.2.2 OpenMP Language Terminology |
| 2053 | // Structured block - An executable statement with a single entry at the |
| 2054 | // top and a single exit at the bottom. |
| 2055 | // The point of exit cannot be a branch out of the structured block. |
| 2056 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 2057 | CS->getCapturedDecl()->setNothrow(); |
| 2058 | |
| 2059 | getCurFunction()->setHasBranchProtectedScope(); |
| 2060 | |
| 2061 | return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); |
| 2062 | } |
| 2063 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2064 | StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, |
| 2065 | SourceLocation EndLoc) { |
| 2066 | return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); |
| 2067 | } |
| 2068 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2069 | OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2070 | SourceLocation StartLoc, |
| 2071 | SourceLocation LParenLoc, |
| 2072 | SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2073 | OMPClause *Res = nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2074 | switch (Kind) { |
| 2075 | case OMPC_if: |
| 2076 | Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 2077 | break; |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2078 | case OMPC_final: |
| 2079 | Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 2080 | break; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2081 | case OMPC_num_threads: |
| 2082 | Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 2083 | break; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2084 | case OMPC_safelen: |
| 2085 | Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 2086 | break; |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2087 | case OMPC_collapse: |
| 2088 | Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 2089 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2090 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2091 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2092 | case OMPC_schedule: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2093 | case OMPC_private: |
| 2094 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2095 | case OMPC_lastprivate: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2096 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2097 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2098 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2099 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2100 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2101 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2102 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2103 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2104 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2105 | case OMPC_mergeable: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2106 | case OMPC_threadprivate: |
| 2107 | case OMPC_unknown: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2108 | llvm_unreachable("Clause is not allowed."); |
| 2109 | } |
| 2110 | return Res; |
| 2111 | } |
| 2112 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2113 | OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc, |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2114 | SourceLocation LParenLoc, |
| 2115 | SourceLocation EndLoc) { |
| 2116 | Expr *ValExpr = Condition; |
| 2117 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 2118 | !Condition->isInstantiationDependent() && |
| 2119 | !Condition->containsUnexpandedParameterPack()) { |
| 2120 | ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2121 | Condition->getExprLoc(), Condition); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2122 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2123 | return nullptr; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2124 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2125 | ValExpr = Val.get(); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 2129 | } |
| 2130 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2131 | OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, |
| 2132 | SourceLocation StartLoc, |
| 2133 | SourceLocation LParenLoc, |
| 2134 | SourceLocation EndLoc) { |
| 2135 | Expr *ValExpr = Condition; |
| 2136 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 2137 | !Condition->isInstantiationDependent() && |
| 2138 | !Condition->containsUnexpandedParameterPack()) { |
| 2139 | ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), |
| 2140 | Condition->getExprLoc(), Condition); |
| 2141 | if (Val.isInvalid()) |
| 2142 | return nullptr; |
| 2143 | |
| 2144 | ValExpr = Val.get(); |
| 2145 | } |
| 2146 | |
| 2147 | return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 2148 | } |
| 2149 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2150 | ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, |
| 2151 | Expr *Op) { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2152 | if (!Op) |
| 2153 | return ExprError(); |
| 2154 | |
| 2155 | class IntConvertDiagnoser : public ICEConvertDiagnoser { |
| 2156 | public: |
| 2157 | IntConvertDiagnoser() |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2158 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2159 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 2160 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2161 | return S.Diag(Loc, diag::err_omp_not_integral) << T; |
| 2162 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2163 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
| 2164 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2165 | return S.Diag(Loc, diag::err_omp_incomplete_type) << T; |
| 2166 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2167 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
| 2168 | QualType T, |
| 2169 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2170 | return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; |
| 2171 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2172 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
| 2173 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2174 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2175 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2176 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2177 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 2178 | QualType T) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2179 | return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; |
| 2180 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2181 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
| 2182 | QualType ConvTy) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2183 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2184 | << ConvTy->isEnumeralType() << ConvTy; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2185 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2186 | SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, |
| 2187 | QualType) override { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2188 | llvm_unreachable("conversion functions are permitted"); |
| 2189 | } |
| 2190 | } ConvertDiagnoser; |
| 2191 | return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); |
| 2192 | } |
| 2193 | |
| 2194 | OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, |
| 2195 | SourceLocation StartLoc, |
| 2196 | SourceLocation LParenLoc, |
| 2197 | SourceLocation EndLoc) { |
| 2198 | Expr *ValExpr = NumThreads; |
| 2199 | if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() && |
| 2200 | !NumThreads->isInstantiationDependent() && |
| 2201 | !NumThreads->containsUnexpandedParameterPack()) { |
| 2202 | SourceLocation NumThreadsLoc = NumThreads->getLocStart(); |
| 2203 | ExprResult Val = |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2204 | PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2205 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2206 | return nullptr; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2207 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2208 | ValExpr = Val.get(); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2209 | |
| 2210 | // OpenMP [2.5, Restrictions] |
| 2211 | // The num_threads expression must evaluate to a positive integer value. |
| 2212 | llvm::APSInt Result; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2213 | if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() && |
| 2214 | !Result.isStrictlyPositive()) { |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2215 | Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause) |
| 2216 | << "num_threads" << NumThreads->getSourceRange(); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2217 | return nullptr; |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2221 | return new (Context) |
| 2222 | OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2225 | ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, |
| 2226 | OpenMPClauseKind CKind) { |
| 2227 | if (!E) |
| 2228 | return ExprError(); |
| 2229 | if (E->isValueDependent() || E->isTypeDependent() || |
| 2230 | E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2231 | return E; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2232 | llvm::APSInt Result; |
| 2233 | ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); |
| 2234 | if (ICE.isInvalid()) |
| 2235 | return ExprError(); |
| 2236 | if (!Result.isStrictlyPositive()) { |
| 2237 | Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) |
| 2238 | << getOpenMPClauseName(CKind) << E->getSourceRange(); |
| 2239 | return ExprError(); |
| 2240 | } |
| 2241 | return ICE; |
| 2242 | } |
| 2243 | |
| 2244 | OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, |
| 2245 | SourceLocation LParenLoc, |
| 2246 | SourceLocation EndLoc) { |
| 2247 | // OpenMP [2.8.1, simd construct, Description] |
| 2248 | // The parameter of the safelen clause must be a constant |
| 2249 | // positive integer expression. |
| 2250 | ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); |
| 2251 | if (Safelen.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2252 | return nullptr; |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2253 | return new (Context) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2254 | OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 2257 | OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, |
| 2258 | SourceLocation StartLoc, |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2259 | SourceLocation LParenLoc, |
| 2260 | SourceLocation EndLoc) { |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 2261 | // OpenMP [2.7.1, loop construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2262 | // OpenMP [2.8.1, simd construct, Description] |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 2263 | // OpenMP [2.9.6, distribute construct, Description] |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2264 | // The parameter of the collapse clause must be a constant |
| 2265 | // positive integer expression. |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 2266 | ExprResult NumForLoopsResult = |
| 2267 | VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); |
| 2268 | if (NumForLoopsResult.isInvalid()) |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2269 | return nullptr; |
| 2270 | return new (Context) |
Alexander Musman | 64d33f1 | 2014-06-04 07:53:32 +0000 | [diff] [blame] | 2271 | OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2274 | OMPClause *Sema::ActOnOpenMPSimpleClause( |
| 2275 | OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, |
| 2276 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2277 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2278 | switch (Kind) { |
| 2279 | case OMPC_default: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2280 | Res = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2281 | ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), |
| 2282 | ArgumentLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2283 | break; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2284 | case OMPC_proc_bind: |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2285 | Res = ActOnOpenMPProcBindClause( |
| 2286 | static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, |
| 2287 | LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2288 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2289 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2290 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2291 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2292 | case OMPC_safelen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2293 | case OMPC_collapse: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2294 | case OMPC_schedule: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2295 | case OMPC_private: |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2296 | case OMPC_firstprivate: |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2297 | case OMPC_lastprivate: |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2298 | case OMPC_shared: |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2299 | case OMPC_reduction: |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2300 | case OMPC_linear: |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2301 | case OMPC_aligned: |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2302 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2303 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2304 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2305 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2306 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2307 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2308 | case OMPC_threadprivate: |
| 2309 | case OMPC_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2310 | llvm_unreachable("Clause is not allowed."); |
| 2311 | } |
| 2312 | return Res; |
| 2313 | } |
| 2314 | |
| 2315 | OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, |
| 2316 | SourceLocation KindKwLoc, |
| 2317 | SourceLocation StartLoc, |
| 2318 | SourceLocation LParenLoc, |
| 2319 | SourceLocation EndLoc) { |
| 2320 | if (Kind == OMPC_DEFAULT_unknown) { |
| 2321 | std::string Values; |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 2322 | static_assert(OMPC_DEFAULT_unknown > 0, |
| 2323 | "OMPC_DEFAULT_unknown not greater than 0"); |
Ted Kremenek | 725a097 | 2014-03-21 17:34:28 +0000 | [diff] [blame] | 2324 | std::string Sep(", "); |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 2325 | for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2326 | Values += "'"; |
| 2327 | Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); |
| 2328 | Values += "'"; |
| 2329 | switch (i) { |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 2330 | case OMPC_DEFAULT_unknown - 2: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2331 | Values += " or "; |
| 2332 | break; |
Alexey Bataev | 4ca40ed | 2014-05-12 04:23:46 +0000 | [diff] [blame] | 2333 | case OMPC_DEFAULT_unknown - 1: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | default: |
| 2336 | Values += Sep; |
| 2337 | break; |
| 2338 | } |
| 2339 | } |
| 2340 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2341 | << Values << getOpenMPClauseName(OMPC_default); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2342 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2343 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2344 | switch (Kind) { |
| 2345 | case OMPC_DEFAULT_none: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2346 | DSAStack->setDefaultDSANone(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2347 | break; |
| 2348 | case OMPC_DEFAULT_shared: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2349 | DSAStack->setDefaultDSAShared(KindKwLoc); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2350 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2351 | case OMPC_DEFAULT_unknown: |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2352 | llvm_unreachable("Clause kind is not allowed."); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2353 | break; |
| 2354 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2355 | return new (Context) |
| 2356 | OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2359 | OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, |
| 2360 | SourceLocation KindKwLoc, |
| 2361 | SourceLocation StartLoc, |
| 2362 | SourceLocation LParenLoc, |
| 2363 | SourceLocation EndLoc) { |
| 2364 | if (Kind == OMPC_PROC_BIND_unknown) { |
| 2365 | std::string Values; |
| 2366 | std::string Sep(", "); |
| 2367 | for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) { |
| 2368 | Values += "'"; |
| 2369 | Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i); |
| 2370 | Values += "'"; |
| 2371 | switch (i) { |
| 2372 | case OMPC_PROC_BIND_unknown - 2: |
| 2373 | Values += " or "; |
| 2374 | break; |
| 2375 | case OMPC_PROC_BIND_unknown - 1: |
| 2376 | break; |
| 2377 | default: |
| 2378 | Values += Sep; |
| 2379 | break; |
| 2380 | } |
| 2381 | } |
| 2382 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2383 | << Values << getOpenMPClauseName(OMPC_proc_bind); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2384 | return nullptr; |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2385 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2386 | return new (Context) |
| 2387 | OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2390 | OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( |
| 2391 | OpenMPClauseKind Kind, unsigned Argument, Expr *Expr, |
| 2392 | SourceLocation StartLoc, SourceLocation LParenLoc, |
| 2393 | SourceLocation ArgumentLoc, SourceLocation CommaLoc, |
| 2394 | SourceLocation EndLoc) { |
| 2395 | OMPClause *Res = nullptr; |
| 2396 | switch (Kind) { |
| 2397 | case OMPC_schedule: |
| 2398 | Res = ActOnOpenMPScheduleClause( |
| 2399 | static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc, |
| 2400 | LParenLoc, ArgumentLoc, CommaLoc, EndLoc); |
| 2401 | break; |
| 2402 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2403 | case OMPC_final: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2404 | case OMPC_num_threads: |
| 2405 | case OMPC_safelen: |
| 2406 | case OMPC_collapse: |
| 2407 | case OMPC_default: |
| 2408 | case OMPC_proc_bind: |
| 2409 | case OMPC_private: |
| 2410 | case OMPC_firstprivate: |
| 2411 | case OMPC_lastprivate: |
| 2412 | case OMPC_shared: |
| 2413 | case OMPC_reduction: |
| 2414 | case OMPC_linear: |
| 2415 | case OMPC_aligned: |
| 2416 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2417 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2418 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2419 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2420 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2421 | case OMPC_mergeable: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2422 | case OMPC_threadprivate: |
| 2423 | case OMPC_unknown: |
| 2424 | llvm_unreachable("Clause is not allowed."); |
| 2425 | } |
| 2426 | return Res; |
| 2427 | } |
| 2428 | |
| 2429 | OMPClause *Sema::ActOnOpenMPScheduleClause( |
| 2430 | OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, |
| 2431 | SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, |
| 2432 | SourceLocation EndLoc) { |
| 2433 | if (Kind == OMPC_SCHEDULE_unknown) { |
| 2434 | std::string Values; |
| 2435 | std::string Sep(", "); |
| 2436 | for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) { |
| 2437 | Values += "'"; |
| 2438 | Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i); |
| 2439 | Values += "'"; |
| 2440 | switch (i) { |
| 2441 | case OMPC_SCHEDULE_unknown - 2: |
| 2442 | Values += " or "; |
| 2443 | break; |
| 2444 | case OMPC_SCHEDULE_unknown - 1: |
| 2445 | break; |
| 2446 | default: |
| 2447 | Values += Sep; |
| 2448 | break; |
| 2449 | } |
| 2450 | } |
| 2451 | Diag(KindLoc, diag::err_omp_unexpected_clause_value) |
| 2452 | << Values << getOpenMPClauseName(OMPC_schedule); |
| 2453 | return nullptr; |
| 2454 | } |
| 2455 | Expr *ValExpr = ChunkSize; |
| 2456 | if (ChunkSize) { |
| 2457 | if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && |
| 2458 | !ChunkSize->isInstantiationDependent() && |
| 2459 | !ChunkSize->containsUnexpandedParameterPack()) { |
| 2460 | SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); |
| 2461 | ExprResult Val = |
| 2462 | PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); |
| 2463 | if (Val.isInvalid()) |
| 2464 | return nullptr; |
| 2465 | |
| 2466 | ValExpr = Val.get(); |
| 2467 | |
| 2468 | // OpenMP [2.7.1, Restrictions] |
| 2469 | // chunk_size must be a loop invariant integer expression with a positive |
| 2470 | // value. |
| 2471 | llvm::APSInt Result; |
| 2472 | if (ValExpr->isIntegerConstantExpr(Result, Context) && |
| 2473 | Result.isSigned() && !Result.isStrictlyPositive()) { |
| 2474 | Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) |
| 2475 | << "schedule" << ChunkSize->getSourceRange(); |
| 2476 | return nullptr; |
| 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, |
| 2482 | EndLoc, Kind, ValExpr); |
| 2483 | } |
| 2484 | |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2485 | OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, |
| 2486 | SourceLocation StartLoc, |
| 2487 | SourceLocation EndLoc) { |
| 2488 | OMPClause *Res = nullptr; |
| 2489 | switch (Kind) { |
| 2490 | case OMPC_ordered: |
| 2491 | Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); |
| 2492 | break; |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2493 | case OMPC_nowait: |
| 2494 | Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); |
| 2495 | break; |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2496 | case OMPC_untied: |
| 2497 | Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); |
| 2498 | break; |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2499 | case OMPC_mergeable: |
| 2500 | Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); |
| 2501 | break; |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2502 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2503 | case OMPC_final: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2504 | case OMPC_num_threads: |
| 2505 | case OMPC_safelen: |
| 2506 | case OMPC_collapse: |
| 2507 | case OMPC_schedule: |
| 2508 | case OMPC_private: |
| 2509 | case OMPC_firstprivate: |
| 2510 | case OMPC_lastprivate: |
| 2511 | case OMPC_shared: |
| 2512 | case OMPC_reduction: |
| 2513 | case OMPC_linear: |
| 2514 | case OMPC_aligned: |
| 2515 | case OMPC_copyin: |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2516 | case OMPC_copyprivate: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2517 | case OMPC_default: |
| 2518 | case OMPC_proc_bind: |
| 2519 | case OMPC_threadprivate: |
| 2520 | case OMPC_unknown: |
| 2521 | llvm_unreachable("Clause is not allowed."); |
| 2522 | } |
| 2523 | return Res; |
| 2524 | } |
| 2525 | |
| 2526 | OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, |
| 2527 | SourceLocation EndLoc) { |
| 2528 | return new (Context) OMPOrderedClause(StartLoc, EndLoc); |
| 2529 | } |
| 2530 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2531 | OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, |
| 2532 | SourceLocation EndLoc) { |
| 2533 | return new (Context) OMPNowaitClause(StartLoc, EndLoc); |
| 2534 | } |
| 2535 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2536 | OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, |
| 2537 | SourceLocation EndLoc) { |
| 2538 | return new (Context) OMPUntiedClause(StartLoc, EndLoc); |
| 2539 | } |
| 2540 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2541 | OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, |
| 2542 | SourceLocation EndLoc) { |
| 2543 | return new (Context) OMPMergeableClause(StartLoc, EndLoc); |
| 2544 | } |
| 2545 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2546 | OMPClause *Sema::ActOnOpenMPVarListClause( |
| 2547 | OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, |
| 2548 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, |
| 2549 | SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, |
| 2550 | const DeclarationNameInfo &ReductionId) { |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2551 | OMPClause *Res = nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2552 | switch (Kind) { |
| 2553 | case OMPC_private: |
| 2554 | Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 2555 | break; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2556 | case OMPC_firstprivate: |
| 2557 | Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 2558 | break; |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2559 | case OMPC_lastprivate: |
| 2560 | Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 2561 | break; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2562 | case OMPC_shared: |
| 2563 | Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 2564 | break; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2565 | case OMPC_reduction: |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 2566 | Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, |
| 2567 | EndLoc, ReductionIdScopeSpec, ReductionId); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2568 | break; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2569 | case OMPC_linear: |
| 2570 | Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, |
| 2571 | ColonLoc, EndLoc); |
| 2572 | break; |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2573 | case OMPC_aligned: |
| 2574 | Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, |
| 2575 | ColonLoc, EndLoc); |
| 2576 | break; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2577 | case OMPC_copyin: |
| 2578 | Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 2579 | break; |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2580 | case OMPC_copyprivate: |
| 2581 | Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 2582 | break; |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2583 | case OMPC_if: |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2584 | case OMPC_final: |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2585 | case OMPC_num_threads: |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2586 | case OMPC_safelen: |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2587 | case OMPC_collapse: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2588 | case OMPC_default: |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2589 | case OMPC_proc_bind: |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2590 | case OMPC_schedule: |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2591 | case OMPC_ordered: |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2592 | case OMPC_nowait: |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2593 | case OMPC_untied: |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2594 | case OMPC_mergeable: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2595 | case OMPC_threadprivate: |
| 2596 | case OMPC_unknown: |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2597 | llvm_unreachable("Clause is not allowed."); |
| 2598 | } |
| 2599 | return Res; |
| 2600 | } |
| 2601 | |
| 2602 | OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, |
| 2603 | SourceLocation StartLoc, |
| 2604 | SourceLocation LParenLoc, |
| 2605 | SourceLocation EndLoc) { |
| 2606 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2607 | for (auto &RefExpr : VarList) { |
| 2608 | assert(RefExpr && "NULL expr in OpenMP private clause."); |
| 2609 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2610 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2611 | Vars.push_back(RefExpr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2612 | continue; |
| 2613 | } |
| 2614 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2615 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2616 | // OpenMP [2.1, C/C++] |
| 2617 | // A list item is a variable name. |
| 2618 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 2619 | // A variable that is part of another variable (as an array or |
| 2620 | // structure element) cannot appear in a private clause. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2621 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2622 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2623 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2624 | continue; |
| 2625 | } |
| 2626 | Decl *D = DE->getDecl(); |
| 2627 | VarDecl *VD = cast<VarDecl>(D); |
| 2628 | |
| 2629 | QualType Type = VD->getType(); |
| 2630 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 2631 | // It will be analyzed later. |
| 2632 | Vars.push_back(DE); |
| 2633 | continue; |
| 2634 | } |
| 2635 | |
| 2636 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 2637 | // A variable that appears in a private clause must not have an incomplete |
| 2638 | // type or a reference type. |
| 2639 | if (RequireCompleteType(ELoc, Type, |
| 2640 | diag::err_omp_private_incomplete_type)) { |
| 2641 | continue; |
| 2642 | } |
| 2643 | if (Type->isReferenceType()) { |
| 2644 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2645 | << getOpenMPClauseName(OMPC_private) << Type; |
| 2646 | bool IsDecl = |
| 2647 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 2648 | Diag(VD->getLocation(), |
| 2649 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 2650 | << VD; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2651 | continue; |
| 2652 | } |
| 2653 | |
| 2654 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] |
| 2655 | // A variable of class type (or array thereof) that appears in a private |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 2656 | // clause requires an accessible, unambiguous default constructor for the |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2657 | // class type. |
| 2658 | while (Type.getNonReferenceType()->isArrayType()) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2659 | Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr()) |
| 2660 | ->getElementType(); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2661 | } |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2662 | CXXRecordDecl *RD = getLangOpts().CPlusPlus |
| 2663 | ? Type.getNonReferenceType()->getAsCXXRecordDecl() |
| 2664 | : nullptr; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 2665 | // FIXME This code must be replaced by actual constructing/destructing of |
| 2666 | // the private variable. |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2667 | if (RD) { |
| 2668 | CXXConstructorDecl *CD = LookupDefaultConstructor(RD); |
| 2669 | PartialDiagnostic PD = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2670 | PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2671 | if (!CD || |
| 2672 | CheckConstructorAccess(ELoc, CD, |
| 2673 | InitializedEntity::InitializeTemporary(Type), |
| 2674 | CD->getAccess(), PD) == AR_inaccessible || |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2675 | CD->isDeleted()) { |
| 2676 | Diag(ELoc, diag::err_omp_required_method) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2677 | << getOpenMPClauseName(OMPC_private) << 0; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2678 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 2679 | VarDecl::DeclarationOnly; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2680 | Diag(VD->getLocation(), |
| 2681 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 2682 | << VD; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2683 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 2684 | continue; |
| 2685 | } |
| 2686 | MarkFunctionReferenced(ELoc, CD); |
| 2687 | DiagnoseUseOfDecl(CD, ELoc); |
| 2688 | |
| 2689 | CXXDestructorDecl *DD = RD->getDestructor(); |
| 2690 | if (DD) { |
| 2691 | if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || |
| 2692 | DD->isDeleted()) { |
| 2693 | Diag(ELoc, diag::err_omp_required_method) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2694 | << getOpenMPClauseName(OMPC_private) << 4; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2695 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 2696 | VarDecl::DeclarationOnly; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2697 | Diag(VD->getLocation(), |
| 2698 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 2699 | << VD; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2700 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 2701 | continue; |
| 2702 | } |
| 2703 | MarkFunctionReferenced(ELoc, DD); |
| 2704 | DiagnoseUseOfDecl(DD, ELoc); |
| 2705 | } |
| 2706 | } |
| 2707 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2708 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 2709 | // in a Construct] |
| 2710 | // Variables with the predetermined data-sharing attributes may not be |
| 2711 | // listed in data-sharing attributes clauses, except for the cases |
| 2712 | // listed below. For these exceptions only, listing a predetermined |
| 2713 | // variable in a data-sharing attribute clause is allowed and overrides |
| 2714 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2715 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2716 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2717 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 2718 | << getOpenMPClauseName(OMPC_private); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 2719 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2720 | continue; |
| 2721 | } |
| 2722 | |
| 2723 | DSAStack->addDSA(VD, DE, OMPC_private); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2724 | Vars.push_back(DE); |
| 2725 | } |
| 2726 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2727 | if (Vars.empty()) |
| 2728 | return nullptr; |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2729 | |
| 2730 | return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 2731 | } |
| 2732 | |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2733 | OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, |
| 2734 | SourceLocation StartLoc, |
| 2735 | SourceLocation LParenLoc, |
| 2736 | SourceLocation EndLoc) { |
| 2737 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2738 | bool IsImplicitClause = |
| 2739 | StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); |
| 2740 | auto ImplicitClauseLoc = DSAStack->getConstructLoc(); |
| 2741 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2742 | for (auto &RefExpr : VarList) { |
| 2743 | assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); |
| 2744 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2745 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2746 | Vars.push_back(RefExpr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2747 | continue; |
| 2748 | } |
| 2749 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2750 | SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc |
| 2751 | : RefExpr->getExprLoc(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2752 | // OpenMP [2.1, C/C++] |
| 2753 | // A list item is a variable name. |
| 2754 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 2755 | // A variable that is part of another variable (as an array or |
| 2756 | // structure element) cannot appear in a private clause. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2757 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2758 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2759 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2760 | continue; |
| 2761 | } |
| 2762 | Decl *D = DE->getDecl(); |
| 2763 | VarDecl *VD = cast<VarDecl>(D); |
| 2764 | |
| 2765 | QualType Type = VD->getType(); |
| 2766 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 2767 | // It will be analyzed later. |
| 2768 | Vars.push_back(DE); |
| 2769 | continue; |
| 2770 | } |
| 2771 | |
| 2772 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 2773 | // A variable that appears in a private clause must not have an incomplete |
| 2774 | // type or a reference type. |
| 2775 | if (RequireCompleteType(ELoc, Type, |
| 2776 | diag::err_omp_firstprivate_incomplete_type)) { |
| 2777 | continue; |
| 2778 | } |
| 2779 | if (Type->isReferenceType()) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2780 | if (IsImplicitClause) { |
| 2781 | Diag(ImplicitClauseLoc, |
| 2782 | diag::err_omp_task_predetermined_firstprivate_ref_type_arg) |
| 2783 | << Type; |
| 2784 | Diag(RefExpr->getExprLoc(), diag::note_used_here); |
| 2785 | } else { |
| 2786 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
| 2787 | << getOpenMPClauseName(OMPC_firstprivate) << Type; |
| 2788 | } |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2789 | bool IsDecl = |
| 2790 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 2791 | Diag(VD->getLocation(), |
| 2792 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 2793 | << VD; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2794 | continue; |
| 2795 | } |
| 2796 | |
| 2797 | // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] |
| 2798 | // A variable of class type (or array thereof) that appears in a private |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 2799 | // clause requires an accessible, unambiguous copy constructor for the |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2800 | // class type. |
| 2801 | Type = Context.getBaseElementType(Type); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 2802 | CXXRecordDecl *RD = getLangOpts().CPlusPlus |
| 2803 | ? Type.getNonReferenceType()->getAsCXXRecordDecl() |
| 2804 | : nullptr; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 2805 | // FIXME This code must be replaced by actual constructing/destructing of |
| 2806 | // the firstprivate variable. |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2807 | if (RD) { |
| 2808 | CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0); |
| 2809 | PartialDiagnostic PD = |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2810 | PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2811 | if (!CD || |
| 2812 | CheckConstructorAccess(ELoc, CD, |
| 2813 | InitializedEntity::InitializeTemporary(Type), |
| 2814 | CD->getAccess(), PD) == AR_inaccessible || |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2815 | CD->isDeleted()) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2816 | if (IsImplicitClause) { |
| 2817 | Diag(ImplicitClauseLoc, |
| 2818 | diag::err_omp_task_predetermined_firstprivate_required_method) |
| 2819 | << 0; |
| 2820 | Diag(RefExpr->getExprLoc(), diag::note_used_here); |
| 2821 | } else { |
| 2822 | Diag(ELoc, diag::err_omp_required_method) |
| 2823 | << getOpenMPClauseName(OMPC_firstprivate) << 1; |
| 2824 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2825 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 2826 | VarDecl::DeclarationOnly; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2827 | Diag(VD->getLocation(), |
| 2828 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 2829 | << VD; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2830 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 2831 | continue; |
| 2832 | } |
| 2833 | MarkFunctionReferenced(ELoc, CD); |
| 2834 | DiagnoseUseOfDecl(CD, ELoc); |
| 2835 | |
| 2836 | CXXDestructorDecl *DD = RD->getDestructor(); |
| 2837 | if (DD) { |
| 2838 | if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || |
| 2839 | DD->isDeleted()) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2840 | if (IsImplicitClause) { |
| 2841 | Diag(ImplicitClauseLoc, |
| 2842 | diag::err_omp_task_predetermined_firstprivate_required_method) |
| 2843 | << 1; |
| 2844 | Diag(RefExpr->getExprLoc(), diag::note_used_here); |
| 2845 | } else { |
| 2846 | Diag(ELoc, diag::err_omp_required_method) |
| 2847 | << getOpenMPClauseName(OMPC_firstprivate) << 4; |
| 2848 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2849 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 2850 | VarDecl::DeclarationOnly; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2851 | Diag(VD->getLocation(), |
| 2852 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 2853 | << VD; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2854 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 2855 | continue; |
| 2856 | } |
| 2857 | MarkFunctionReferenced(ELoc, DD); |
| 2858 | DiagnoseUseOfDecl(DD, ELoc); |
| 2859 | } |
| 2860 | } |
| 2861 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2862 | // If an implicit firstprivate variable found it was checked already. |
| 2863 | if (!IsImplicitClause) { |
| 2864 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2865 | Type = Type.getNonReferenceType().getCanonicalType(); |
| 2866 | bool IsConstant = Type.isConstant(Context); |
| 2867 | Type = Context.getBaseElementType(Type); |
| 2868 | // OpenMP [2.4.13, Data-sharing Attribute Clauses] |
| 2869 | // A list item that specifies a given variable may not appear in more |
| 2870 | // than one clause on the same directive, except that a variable may be |
| 2871 | // specified in both firstprivate and lastprivate clauses. |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2872 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2873 | DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2874 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2875 | << getOpenMPClauseName(DVar.CKind) |
| 2876 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 2877 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2878 | continue; |
| 2879 | } |
| 2880 | |
| 2881 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 2882 | // in a Construct] |
| 2883 | // Variables with the predetermined data-sharing attributes may not be |
| 2884 | // listed in data-sharing attributes clauses, except for the cases |
| 2885 | // listed below. For these exceptions only, listing a predetermined |
| 2886 | // variable in a data-sharing attribute clause is allowed and overrides |
| 2887 | // the variable's predetermined data-sharing attributes. |
| 2888 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 2889 | // in a Construct, C/C++, p.2] |
| 2890 | // Variables with const-qualified type having no mutable member may be |
| 2891 | // listed in a firstprivate clause, even if they are static data members. |
| 2892 | if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && |
| 2893 | DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { |
| 2894 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2895 | << getOpenMPClauseName(DVar.CKind) |
| 2896 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 2897 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2898 | continue; |
| 2899 | } |
| 2900 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2901 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2902 | // OpenMP [2.9.3.4, Restrictions, p.2] |
| 2903 | // A list item that is private within a parallel region must not appear |
| 2904 | // in a firstprivate clause on a worksharing construct if any of the |
| 2905 | // worksharing regions arising from the worksharing construct ever bind |
| 2906 | // to any of the parallel regions arising from the parallel construct. |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 2907 | if (isOpenMPWorksharingDirective(CurrDir) && |
| 2908 | !isOpenMPParallelDirective(CurrDir)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2909 | DVar = DSAStack->getImplicitDSA(VD, true); |
| 2910 | if (DVar.CKind != OMPC_shared && |
| 2911 | (isOpenMPParallelDirective(DVar.DKind) || |
| 2912 | DVar.DKind == OMPD_unknown)) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2913 | Diag(ELoc, diag::err_omp_required_access) |
| 2914 | << getOpenMPClauseName(OMPC_firstprivate) |
| 2915 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 2916 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2917 | continue; |
| 2918 | } |
| 2919 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2920 | // OpenMP [2.9.3.4, Restrictions, p.3] |
| 2921 | // A list item that appears in a reduction clause of a parallel construct |
| 2922 | // must not appear in a firstprivate clause on a worksharing or task |
| 2923 | // construct if any of the worksharing or task regions arising from the |
| 2924 | // worksharing or task construct ever bind to any of the parallel regions |
| 2925 | // arising from the parallel construct. |
| 2926 | // OpenMP [2.9.3.4, Restrictions, p.4] |
| 2927 | // A list item that appears in a reduction clause in worksharing |
| 2928 | // construct must not appear in a firstprivate clause in a task construct |
| 2929 | // encountered during execution of any of the worksharing regions arising |
| 2930 | // from the worksharing construct. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2931 | if (CurrDir == OMPD_task) { |
| 2932 | DVar = |
| 2933 | DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), |
| 2934 | [](OpenMPDirectiveKind K) -> bool { |
| 2935 | return isOpenMPParallelDirective(K) || |
| 2936 | isOpenMPWorksharingDirective(K); |
| 2937 | }, |
| 2938 | false); |
| 2939 | if (DVar.CKind == OMPC_reduction && |
| 2940 | (isOpenMPParallelDirective(DVar.DKind) || |
| 2941 | isOpenMPWorksharingDirective(DVar.DKind))) { |
| 2942 | Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) |
| 2943 | << getOpenMPDirectiveName(DVar.DKind); |
| 2944 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
| 2945 | continue; |
| 2946 | } |
| 2947 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | DSAStack->addDSA(VD, DE, OMPC_firstprivate); |
| 2951 | Vars.push_back(DE); |
| 2952 | } |
| 2953 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 2954 | if (Vars.empty()) |
| 2955 | return nullptr; |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2956 | |
| 2957 | return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 2958 | Vars); |
| 2959 | } |
| 2960 | |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2961 | OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, |
| 2962 | SourceLocation StartLoc, |
| 2963 | SourceLocation LParenLoc, |
| 2964 | SourceLocation EndLoc) { |
| 2965 | SmallVector<Expr *, 8> Vars; |
| 2966 | for (auto &RefExpr : VarList) { |
| 2967 | assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); |
| 2968 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 2969 | // It will be analyzed later. |
| 2970 | Vars.push_back(RefExpr); |
| 2971 | continue; |
| 2972 | } |
| 2973 | |
| 2974 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 2975 | // OpenMP [2.1, C/C++] |
| 2976 | // A list item is a variable name. |
| 2977 | // OpenMP [2.14.3.5, Restrictions, p.1] |
| 2978 | // A variable that is part of another variable (as an array or structure |
| 2979 | // element) cannot appear in a lastprivate clause. |
| 2980 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
| 2981 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 2982 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
| 2983 | continue; |
| 2984 | } |
| 2985 | Decl *D = DE->getDecl(); |
| 2986 | VarDecl *VD = cast<VarDecl>(D); |
| 2987 | |
| 2988 | QualType Type = VD->getType(); |
| 2989 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 2990 | // It will be analyzed later. |
| 2991 | Vars.push_back(DE); |
| 2992 | continue; |
| 2993 | } |
| 2994 | |
| 2995 | // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] |
| 2996 | // A variable that appears in a lastprivate clause must not have an |
| 2997 | // incomplete type or a reference type. |
| 2998 | if (RequireCompleteType(ELoc, Type, |
| 2999 | diag::err_omp_lastprivate_incomplete_type)) { |
| 3000 | continue; |
| 3001 | } |
| 3002 | if (Type->isReferenceType()) { |
| 3003 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
| 3004 | << getOpenMPClauseName(OMPC_lastprivate) << Type; |
| 3005 | bool IsDecl = |
| 3006 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3007 | Diag(VD->getLocation(), |
| 3008 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3009 | << VD; |
| 3010 | continue; |
| 3011 | } |
| 3012 | |
| 3013 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 3014 | // in a Construct] |
| 3015 | // Variables with the predetermined data-sharing attributes may not be |
| 3016 | // listed in data-sharing attributes clauses, except for the cases |
| 3017 | // listed below. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3018 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3019 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && |
| 3020 | DVar.CKind != OMPC_firstprivate && |
| 3021 | (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { |
| 3022 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 3023 | << getOpenMPClauseName(DVar.CKind) |
| 3024 | << getOpenMPClauseName(OMPC_lastprivate); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 3025 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3026 | continue; |
| 3027 | } |
| 3028 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3029 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
| 3030 | // OpenMP [2.14.3.5, Restrictions, p.2] |
| 3031 | // A list item that is private within a parallel region, or that appears in |
| 3032 | // the reduction clause of a parallel construct, must not appear in a |
| 3033 | // lastprivate clause on a worksharing construct if any of the corresponding |
| 3034 | // worksharing regions ever binds to any of the corresponding parallel |
| 3035 | // regions. |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 3036 | if (isOpenMPWorksharingDirective(CurrDir) && |
| 3037 | !isOpenMPParallelDirective(CurrDir)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3038 | DVar = DSAStack->getImplicitDSA(VD, true); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3039 | if (DVar.CKind != OMPC_shared) { |
| 3040 | Diag(ELoc, diag::err_omp_required_access) |
| 3041 | << getOpenMPClauseName(OMPC_lastprivate) |
| 3042 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 3043 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3044 | continue; |
| 3045 | } |
| 3046 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3047 | // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3048 | // A variable of class type (or array thereof) that appears in a |
| 3049 | // lastprivate clause requires an accessible, unambiguous default |
| 3050 | // constructor for the class type, unless the list item is also specified |
| 3051 | // in a firstprivate clause. |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3052 | // A variable of class type (or array thereof) that appears in a |
| 3053 | // lastprivate clause requires an accessible, unambiguous copy assignment |
| 3054 | // operator for the class type. |
| 3055 | while (Type.getNonReferenceType()->isArrayType()) |
| 3056 | Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr()) |
| 3057 | ->getElementType(); |
| 3058 | CXXRecordDecl *RD = getLangOpts().CPlusPlus |
| 3059 | ? Type.getNonReferenceType()->getAsCXXRecordDecl() |
| 3060 | : nullptr; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3061 | // FIXME This code must be replaced by actual copying and destructing of the |
| 3062 | // lastprivate variable. |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3063 | if (RD) { |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3064 | CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); |
| 3065 | DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3066 | if (MD) { |
| 3067 | if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || |
| 3068 | MD->isDeleted()) { |
| 3069 | Diag(ELoc, diag::err_omp_required_method) |
| 3070 | << getOpenMPClauseName(OMPC_lastprivate) << 2; |
| 3071 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 3072 | VarDecl::DeclarationOnly; |
| 3073 | Diag(VD->getLocation(), |
| 3074 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3075 | << VD; |
| 3076 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 3077 | continue; |
| 3078 | } |
| 3079 | MarkFunctionReferenced(ELoc, MD); |
| 3080 | DiagnoseUseOfDecl(MD, ELoc); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3081 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3082 | |
| 3083 | CXXDestructorDecl *DD = RD->getDestructor(); |
| 3084 | if (DD) { |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3085 | PartialDiagnostic PD = |
| 3086 | PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3087 | if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || |
| 3088 | DD->isDeleted()) { |
| 3089 | Diag(ELoc, diag::err_omp_required_method) |
| 3090 | << getOpenMPClauseName(OMPC_lastprivate) << 4; |
| 3091 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 3092 | VarDecl::DeclarationOnly; |
| 3093 | Diag(VD->getLocation(), |
| 3094 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3095 | << VD; |
| 3096 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 3097 | continue; |
| 3098 | } |
| 3099 | MarkFunctionReferenced(ELoc, DD); |
| 3100 | DiagnoseUseOfDecl(DD, ELoc); |
| 3101 | } |
| 3102 | } |
| 3103 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3104 | if (DVar.CKind != OMPC_firstprivate) |
| 3105 | DSAStack->addDSA(VD, DE, OMPC_lastprivate); |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 3106 | Vars.push_back(DE); |
| 3107 | } |
| 3108 | |
| 3109 | if (Vars.empty()) |
| 3110 | return nullptr; |
| 3111 | |
| 3112 | return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 3113 | Vars); |
| 3114 | } |
| 3115 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3116 | OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, |
| 3117 | SourceLocation StartLoc, |
| 3118 | SourceLocation LParenLoc, |
| 3119 | SourceLocation EndLoc) { |
| 3120 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3121 | for (auto &RefExpr : VarList) { |
| 3122 | assert(RefExpr && "NULL expr in OpenMP shared clause."); |
| 3123 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3124 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3125 | Vars.push_back(RefExpr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3126 | continue; |
| 3127 | } |
| 3128 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3129 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3130 | // OpenMP [2.1, C/C++] |
| 3131 | // A list item is a variable name. |
Alexey Bataev | d4dbdf5 | 2014-03-06 12:27:56 +0000 | [diff] [blame] | 3132 | // OpenMP [2.14.3.2, Restrictions, p.1] |
| 3133 | // A variable that is part of another variable (as an array or structure |
| 3134 | // element) cannot appear in a shared unless it is a static data member |
| 3135 | // of a C++ class. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3136 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3137 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3138 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3139 | continue; |
| 3140 | } |
| 3141 | Decl *D = DE->getDecl(); |
| 3142 | VarDecl *VD = cast<VarDecl>(D); |
| 3143 | |
| 3144 | QualType Type = VD->getType(); |
| 3145 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 3146 | // It will be analyzed later. |
| 3147 | Vars.push_back(DE); |
| 3148 | continue; |
| 3149 | } |
| 3150 | |
| 3151 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 3152 | // in a Construct] |
| 3153 | // Variables with the predetermined data-sharing attributes may not be |
| 3154 | // listed in data-sharing attributes clauses, except for the cases |
| 3155 | // listed below. For these exceptions only, listing a predetermined |
| 3156 | // variable in a data-sharing attribute clause is allowed and overrides |
| 3157 | // the variable's predetermined data-sharing attributes. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3158 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3159 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && |
| 3160 | DVar.RefExpr) { |
| 3161 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 3162 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 3163 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3164 | continue; |
| 3165 | } |
| 3166 | |
| 3167 | DSAStack->addDSA(VD, DE, OMPC_shared); |
| 3168 | Vars.push_back(DE); |
| 3169 | } |
| 3170 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3171 | if (Vars.empty()) |
| 3172 | return nullptr; |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3173 | |
| 3174 | return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 3175 | } |
| 3176 | |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3177 | namespace { |
| 3178 | class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { |
| 3179 | DSAStackTy *Stack; |
| 3180 | |
| 3181 | public: |
| 3182 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 3183 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3184 | DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3185 | if (DVar.CKind == OMPC_shared && !DVar.RefExpr) |
| 3186 | return false; |
| 3187 | if (DVar.CKind != OMPC_unknown) |
| 3188 | return true; |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3189 | DSAStackTy::DSAVarData DVarPrivate = |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3190 | Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3191 | if (DVarPrivate.CKind != OMPC_unknown) |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3192 | return true; |
| 3193 | return false; |
| 3194 | } |
| 3195 | return false; |
| 3196 | } |
| 3197 | bool VisitStmt(Stmt *S) { |
| 3198 | for (auto Child : S->children()) { |
| 3199 | if (Child && Visit(Child)) |
| 3200 | return true; |
| 3201 | } |
| 3202 | return false; |
| 3203 | } |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3204 | explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3205 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3206 | } // namespace |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3207 | |
| 3208 | OMPClause *Sema::ActOnOpenMPReductionClause( |
| 3209 | ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, |
| 3210 | SourceLocation ColonLoc, SourceLocation EndLoc, |
| 3211 | CXXScopeSpec &ReductionIdScopeSpec, |
| 3212 | const DeclarationNameInfo &ReductionId) { |
| 3213 | // TODO: Allow scope specification search when 'declare reduction' is |
| 3214 | // supported. |
| 3215 | assert(ReductionIdScopeSpec.isEmpty() && |
| 3216 | "No support for scoped reduction identifiers yet."); |
| 3217 | |
| 3218 | auto DN = ReductionId.getName(); |
| 3219 | auto OOK = DN.getCXXOverloadedOperator(); |
| 3220 | BinaryOperatorKind BOK = BO_Comma; |
| 3221 | |
| 3222 | // OpenMP [2.14.3.6, reduction clause] |
| 3223 | // C |
| 3224 | // reduction-identifier is either an identifier or one of the following |
| 3225 | // operators: +, -, *, &, |, ^, && and || |
| 3226 | // C++ |
| 3227 | // reduction-identifier is either an id-expression or one of the following |
| 3228 | // operators: +, -, *, &, |, ^, && and || |
| 3229 | // FIXME: Only 'min' and 'max' identifiers are supported for now. |
| 3230 | switch (OOK) { |
| 3231 | case OO_Plus: |
| 3232 | case OO_Minus: |
| 3233 | BOK = BO_AddAssign; |
| 3234 | break; |
| 3235 | case OO_Star: |
| 3236 | BOK = BO_MulAssign; |
| 3237 | break; |
| 3238 | case OO_Amp: |
| 3239 | BOK = BO_AndAssign; |
| 3240 | break; |
| 3241 | case OO_Pipe: |
| 3242 | BOK = BO_OrAssign; |
| 3243 | break; |
| 3244 | case OO_Caret: |
| 3245 | BOK = BO_XorAssign; |
| 3246 | break; |
| 3247 | case OO_AmpAmp: |
| 3248 | BOK = BO_LAnd; |
| 3249 | break; |
| 3250 | case OO_PipePipe: |
| 3251 | BOK = BO_LOr; |
| 3252 | break; |
| 3253 | default: |
| 3254 | if (auto II = DN.getAsIdentifierInfo()) { |
| 3255 | if (II->isStr("max")) |
| 3256 | BOK = BO_GT; |
| 3257 | else if (II->isStr("min")) |
| 3258 | BOK = BO_LT; |
| 3259 | } |
| 3260 | break; |
| 3261 | } |
| 3262 | SourceRange ReductionIdRange; |
| 3263 | if (ReductionIdScopeSpec.isValid()) { |
| 3264 | ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); |
| 3265 | } |
| 3266 | ReductionIdRange.setEnd(ReductionId.getEndLoc()); |
| 3267 | if (BOK == BO_Comma) { |
| 3268 | // Not allowed reduction identifier is found. |
| 3269 | Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier) |
| 3270 | << ReductionIdRange; |
| 3271 | return nullptr; |
| 3272 | } |
| 3273 | |
| 3274 | SmallVector<Expr *, 8> Vars; |
| 3275 | for (auto RefExpr : VarList) { |
| 3276 | assert(RefExpr && "nullptr expr in OpenMP reduction clause."); |
| 3277 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 3278 | // It will be analyzed later. |
| 3279 | Vars.push_back(RefExpr); |
| 3280 | continue; |
| 3281 | } |
| 3282 | |
| 3283 | if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || |
| 3284 | RefExpr->isInstantiationDependent() || |
| 3285 | RefExpr->containsUnexpandedParameterPack()) { |
| 3286 | // It will be analyzed later. |
| 3287 | Vars.push_back(RefExpr); |
| 3288 | continue; |
| 3289 | } |
| 3290 | |
| 3291 | auto ELoc = RefExpr->getExprLoc(); |
| 3292 | auto ERange = RefExpr->getSourceRange(); |
| 3293 | // OpenMP [2.1, C/C++] |
| 3294 | // A list item is a variable or array section, subject to the restrictions |
| 3295 | // specified in Section 2.4 on page 42 and in each of the sections |
| 3296 | // describing clauses and directives for which a list appears. |
| 3297 | // OpenMP [2.14.3.3, Restrictions, p.1] |
| 3298 | // A variable that is part of another variable (as an array or |
| 3299 | // structure element) cannot appear in a private clause. |
| 3300 | auto DE = dyn_cast<DeclRefExpr>(RefExpr); |
| 3301 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 3302 | Diag(ELoc, diag::err_omp_expected_var_name) << ERange; |
| 3303 | continue; |
| 3304 | } |
| 3305 | auto D = DE->getDecl(); |
| 3306 | auto VD = cast<VarDecl>(D); |
| 3307 | auto Type = VD->getType(); |
| 3308 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 3309 | // A variable that appears in a private clause must not have an incomplete |
| 3310 | // type or a reference type. |
| 3311 | if (RequireCompleteType(ELoc, Type, |
| 3312 | diag::err_omp_reduction_incomplete_type)) |
| 3313 | continue; |
| 3314 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 3315 | // Arrays may not appear in a reduction clause. |
| 3316 | if (Type.getNonReferenceType()->isArrayType()) { |
| 3317 | Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange; |
| 3318 | bool IsDecl = |
| 3319 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3320 | Diag(VD->getLocation(), |
| 3321 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3322 | << VD; |
| 3323 | continue; |
| 3324 | } |
| 3325 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 3326 | // A list item that appears in a reduction clause must not be |
| 3327 | // const-qualified. |
| 3328 | if (Type.getNonReferenceType().isConstant(Context)) { |
| 3329 | Diag(ELoc, diag::err_omp_const_variable) |
| 3330 | << getOpenMPClauseName(OMPC_reduction) << Type << ERange; |
| 3331 | bool IsDecl = |
| 3332 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3333 | Diag(VD->getLocation(), |
| 3334 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3335 | << VD; |
| 3336 | continue; |
| 3337 | } |
| 3338 | // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] |
| 3339 | // If a list-item is a reference type then it must bind to the same object |
| 3340 | // for all threads of the team. |
| 3341 | VarDecl *VDDef = VD->getDefinition(); |
| 3342 | if (Type->isReferenceType() && VDDef) { |
| 3343 | DSARefChecker Check(DSAStack); |
| 3344 | if (Check.Visit(VDDef->getInit())) { |
| 3345 | Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; |
| 3346 | Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; |
| 3347 | continue; |
| 3348 | } |
| 3349 | } |
| 3350 | // OpenMP [2.14.3.6, reduction clause, Restrictions] |
| 3351 | // The type of a list item that appears in a reduction clause must be valid |
| 3352 | // for the reduction-identifier. For a max or min reduction in C, the type |
| 3353 | // of the list item must be an allowed arithmetic data type: char, int, |
| 3354 | // float, double, or _Bool, possibly modified with long, short, signed, or |
| 3355 | // unsigned. For a max or min reduction in C++, the type of the list item |
| 3356 | // must be an allowed arithmetic data type: char, wchar_t, int, float, |
| 3357 | // double, or bool, possibly modified with long, short, signed, or unsigned. |
| 3358 | if ((BOK == BO_GT || BOK == BO_LT) && |
| 3359 | !(Type->isScalarType() || |
| 3360 | (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { |
| 3361 | Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) |
| 3362 | << getLangOpts().CPlusPlus; |
| 3363 | bool IsDecl = |
| 3364 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3365 | Diag(VD->getLocation(), |
| 3366 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3367 | << VD; |
| 3368 | continue; |
| 3369 | } |
| 3370 | if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && |
| 3371 | !getLangOpts().CPlusPlus && Type->isFloatingType()) { |
| 3372 | Diag(ELoc, diag::err_omp_clause_floating_type_arg); |
| 3373 | bool IsDecl = |
| 3374 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3375 | Diag(VD->getLocation(), |
| 3376 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3377 | << VD; |
| 3378 | continue; |
| 3379 | } |
| 3380 | bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); |
| 3381 | getDiagnostics().setSuppressAllDiagnostics(true); |
| 3382 | ExprResult ReductionOp = |
| 3383 | BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK, |
| 3384 | RefExpr, RefExpr); |
| 3385 | getDiagnostics().setSuppressAllDiagnostics(Suppress); |
| 3386 | if (ReductionOp.isInvalid()) { |
| 3387 | Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3388 | << ReductionIdRange; |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3389 | bool IsDecl = |
| 3390 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3391 | Diag(VD->getLocation(), |
| 3392 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3393 | << VD; |
| 3394 | continue; |
| 3395 | } |
| 3396 | |
| 3397 | // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 3398 | // in a Construct] |
| 3399 | // Variables with the predetermined data-sharing attributes may not be |
| 3400 | // listed in data-sharing attributes clauses, except for the cases |
| 3401 | // listed below. For these exceptions only, listing a predetermined |
| 3402 | // variable in a data-sharing attribute clause is allowed and overrides |
| 3403 | // the variable's predetermined data-sharing attributes. |
| 3404 | // OpenMP [2.14.3.6, Restrictions, p.3] |
| 3405 | // Any number of reduction clauses can be specified on the directive, |
| 3406 | // but a list item can appear only once in the reduction clauses for that |
| 3407 | // directive. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3408 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3409 | if (DVar.CKind == OMPC_reduction) { |
| 3410 | Diag(ELoc, diag::err_omp_once_referenced) |
| 3411 | << getOpenMPClauseName(OMPC_reduction); |
| 3412 | if (DVar.RefExpr) { |
| 3413 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); |
| 3414 | } |
| 3415 | } else if (DVar.CKind != OMPC_unknown) { |
| 3416 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 3417 | << getOpenMPClauseName(DVar.CKind) |
| 3418 | << getOpenMPClauseName(OMPC_reduction); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 3419 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3420 | continue; |
| 3421 | } |
| 3422 | |
| 3423 | // OpenMP [2.14.3.6, Restrictions, p.1] |
| 3424 | // A list item that appears in a reduction clause of a worksharing |
| 3425 | // construct must be shared in the parallel regions to which any of the |
| 3426 | // worksharing regions arising from the worksharing construct bind. |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3427 | OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); |
Alexey Bataev | 549210e | 2014-06-24 04:39:47 +0000 | [diff] [blame] | 3428 | if (isOpenMPWorksharingDirective(CurrDir) && |
| 3429 | !isOpenMPParallelDirective(CurrDir)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3430 | DVar = DSAStack->getImplicitDSA(VD, true); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3431 | if (DVar.CKind != OMPC_shared) { |
| 3432 | Diag(ELoc, diag::err_omp_required_access) |
| 3433 | << getOpenMPClauseName(OMPC_reduction) |
| 3434 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 3435 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3436 | continue; |
| 3437 | } |
| 3438 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3439 | |
| 3440 | CXXRecordDecl *RD = getLangOpts().CPlusPlus |
| 3441 | ? Type.getNonReferenceType()->getAsCXXRecordDecl() |
| 3442 | : nullptr; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3443 | // FIXME This code must be replaced by actual constructing/destructing of |
| 3444 | // the reduction variable. |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3445 | if (RD) { |
| 3446 | CXXConstructorDecl *CD = LookupDefaultConstructor(RD); |
| 3447 | PartialDiagnostic PD = |
| 3448 | PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3449 | if (!CD || |
| 3450 | CheckConstructorAccess(ELoc, CD, |
| 3451 | InitializedEntity::InitializeTemporary(Type), |
| 3452 | CD->getAccess(), PD) == AR_inaccessible || |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 3453 | CD->isDeleted()) { |
| 3454 | Diag(ELoc, diag::err_omp_required_method) |
| 3455 | << getOpenMPClauseName(OMPC_reduction) << 0; |
| 3456 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 3457 | VarDecl::DeclarationOnly; |
| 3458 | Diag(VD->getLocation(), |
| 3459 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3460 | << VD; |
| 3461 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 3462 | continue; |
| 3463 | } |
| 3464 | MarkFunctionReferenced(ELoc, CD); |
| 3465 | DiagnoseUseOfDecl(CD, ELoc); |
| 3466 | |
| 3467 | CXXDestructorDecl *DD = RD->getDestructor(); |
| 3468 | if (DD) { |
| 3469 | if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || |
| 3470 | DD->isDeleted()) { |
| 3471 | Diag(ELoc, diag::err_omp_required_method) |
| 3472 | << getOpenMPClauseName(OMPC_reduction) << 4; |
| 3473 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 3474 | VarDecl::DeclarationOnly; |
| 3475 | Diag(VD->getLocation(), |
| 3476 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3477 | << VD; |
| 3478 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 3479 | continue; |
| 3480 | } |
| 3481 | MarkFunctionReferenced(ELoc, DD); |
| 3482 | DiagnoseUseOfDecl(DD, ELoc); |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | DSAStack->addDSA(VD, DE, OMPC_reduction); |
| 3487 | Vars.push_back(DE); |
| 3488 | } |
| 3489 | |
| 3490 | if (Vars.empty()) |
| 3491 | return nullptr; |
| 3492 | |
| 3493 | return OMPReductionClause::Create( |
| 3494 | Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, |
| 3495 | ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId); |
| 3496 | } |
| 3497 | |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3498 | OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step, |
| 3499 | SourceLocation StartLoc, |
| 3500 | SourceLocation LParenLoc, |
| 3501 | SourceLocation ColonLoc, |
| 3502 | SourceLocation EndLoc) { |
| 3503 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3504 | for (auto &RefExpr : VarList) { |
| 3505 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
| 3506 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3507 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3508 | Vars.push_back(RefExpr); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3509 | continue; |
| 3510 | } |
| 3511 | |
| 3512 | // OpenMP [2.14.3.7, linear clause] |
| 3513 | // A list item that appears in a linear clause is subject to the private |
| 3514 | // clause semantics described in Section 2.14.3.3 on page 159 except as |
| 3515 | // noted. In addition, the value of the new list item on each iteration |
| 3516 | // of the associated loop(s) corresponds to the value of the original |
| 3517 | // list item before entering the construct plus the logical number of |
| 3518 | // the iteration times linear-step. |
| 3519 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3520 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3521 | // OpenMP [2.1, C/C++] |
| 3522 | // A list item is a variable name. |
| 3523 | // OpenMP [2.14.3.3, Restrictions, p.1] |
| 3524 | // A variable that is part of another variable (as an array or |
| 3525 | // structure element) cannot appear in a private clause. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3526 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3527 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3528 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3529 | continue; |
| 3530 | } |
| 3531 | |
| 3532 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 3533 | |
| 3534 | // OpenMP [2.14.3.7, linear clause] |
| 3535 | // A list-item cannot appear in more than one linear clause. |
| 3536 | // A list-item that appears in a linear clause cannot appear in any |
| 3537 | // other data-sharing attribute clause. |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3538 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3539 | if (DVar.RefExpr) { |
| 3540 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 3541 | << getOpenMPClauseName(OMPC_linear); |
Alexey Bataev | 7ff5524 | 2014-06-19 09:13:45 +0000 | [diff] [blame] | 3542 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3543 | continue; |
| 3544 | } |
| 3545 | |
| 3546 | QualType QType = VD->getType(); |
| 3547 | if (QType->isDependentType() || QType->isInstantiationDependentType()) { |
| 3548 | // It will be analyzed later. |
| 3549 | Vars.push_back(DE); |
| 3550 | continue; |
| 3551 | } |
| 3552 | |
| 3553 | // A variable must not have an incomplete type or a reference type. |
| 3554 | if (RequireCompleteType(ELoc, QType, |
| 3555 | diag::err_omp_linear_incomplete_type)) { |
| 3556 | continue; |
| 3557 | } |
| 3558 | if (QType->isReferenceType()) { |
| 3559 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
| 3560 | << getOpenMPClauseName(OMPC_linear) << QType; |
| 3561 | bool IsDecl = |
| 3562 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3563 | Diag(VD->getLocation(), |
| 3564 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3565 | << VD; |
| 3566 | continue; |
| 3567 | } |
| 3568 | |
| 3569 | // A list item must not be const-qualified. |
| 3570 | if (QType.isConstant(Context)) { |
| 3571 | Diag(ELoc, diag::err_omp_const_variable) |
| 3572 | << getOpenMPClauseName(OMPC_linear); |
| 3573 | bool IsDecl = |
| 3574 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3575 | Diag(VD->getLocation(), |
| 3576 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3577 | << VD; |
| 3578 | continue; |
| 3579 | } |
| 3580 | |
| 3581 | // A list item must be of integral or pointer type. |
| 3582 | QType = QType.getUnqualifiedType().getCanonicalType(); |
| 3583 | const Type *Ty = QType.getTypePtrOrNull(); |
| 3584 | if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && |
| 3585 | !Ty->isPointerType())) { |
| 3586 | Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType; |
| 3587 | bool IsDecl = |
| 3588 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3589 | Diag(VD->getLocation(), |
| 3590 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3591 | << VD; |
| 3592 | continue; |
| 3593 | } |
| 3594 | |
| 3595 | DSAStack->addDSA(VD, DE, OMPC_linear); |
| 3596 | Vars.push_back(DE); |
| 3597 | } |
| 3598 | |
| 3599 | if (Vars.empty()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 3600 | return nullptr; |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3601 | |
| 3602 | Expr *StepExpr = Step; |
| 3603 | if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && |
| 3604 | !Step->isInstantiationDependent() && |
| 3605 | !Step->containsUnexpandedParameterPack()) { |
| 3606 | SourceLocation StepLoc = Step->getLocStart(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3607 | ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3608 | if (Val.isInvalid()) |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 3609 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3610 | StepExpr = Val.get(); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 3611 | |
| 3612 | // Warn about zero linear step (it would be probably better specified as |
| 3613 | // making corresponding variables 'const'). |
| 3614 | llvm::APSInt Result; |
| 3615 | if (StepExpr->isIntegerConstantExpr(Result, Context) && |
| 3616 | !Result.isNegative() && !Result.isStrictlyPositive()) |
| 3617 | Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] |
| 3618 | << (Vars.size() > 1); |
| 3619 | } |
| 3620 | |
| 3621 | return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc, |
| 3622 | Vars, StepExpr); |
| 3623 | } |
| 3624 | |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 3625 | OMPClause *Sema::ActOnOpenMPAlignedClause( |
| 3626 | ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, |
| 3627 | SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { |
| 3628 | |
| 3629 | SmallVector<Expr *, 8> Vars; |
| 3630 | for (auto &RefExpr : VarList) { |
| 3631 | assert(RefExpr && "NULL expr in OpenMP aligned clause."); |
| 3632 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 3633 | // It will be analyzed later. |
| 3634 | Vars.push_back(RefExpr); |
| 3635 | continue; |
| 3636 | } |
| 3637 | |
| 3638 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 3639 | // OpenMP [2.1, C/C++] |
| 3640 | // A list item is a variable name. |
| 3641 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
| 3642 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 3643 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
| 3644 | continue; |
| 3645 | } |
| 3646 | |
| 3647 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 3648 | |
| 3649 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 3650 | // The type of list items appearing in the aligned clause must be |
| 3651 | // array, pointer, reference to array, or reference to pointer. |
| 3652 | QualType QType = DE->getType() |
| 3653 | .getNonReferenceType() |
| 3654 | .getUnqualifiedType() |
| 3655 | .getCanonicalType(); |
| 3656 | const Type *Ty = QType.getTypePtrOrNull(); |
| 3657 | if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() && |
| 3658 | !Ty->isPointerType())) { |
| 3659 | Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) |
| 3660 | << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange(); |
| 3661 | bool IsDecl = |
| 3662 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 3663 | Diag(VD->getLocation(), |
| 3664 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3665 | << VD; |
| 3666 | continue; |
| 3667 | } |
| 3668 | |
| 3669 | // OpenMP [2.8.1, simd construct, Restrictions] |
| 3670 | // A list-item cannot appear in more than one aligned clause. |
| 3671 | if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) { |
| 3672 | Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange(); |
| 3673 | Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) |
| 3674 | << getOpenMPClauseName(OMPC_aligned); |
| 3675 | continue; |
| 3676 | } |
| 3677 | |
| 3678 | Vars.push_back(DE); |
| 3679 | } |
| 3680 | |
| 3681 | // OpenMP [2.8.1, simd construct, Description] |
| 3682 | // The parameter of the aligned clause, alignment, must be a constant |
| 3683 | // positive integer expression. |
| 3684 | // If no optional parameter is specified, implementation-defined default |
| 3685 | // alignments for SIMD instructions on the target platforms are assumed. |
| 3686 | if (Alignment != nullptr) { |
| 3687 | ExprResult AlignResult = |
| 3688 | VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); |
| 3689 | if (AlignResult.isInvalid()) |
| 3690 | return nullptr; |
| 3691 | Alignment = AlignResult.get(); |
| 3692 | } |
| 3693 | if (Vars.empty()) |
| 3694 | return nullptr; |
| 3695 | |
| 3696 | return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, |
| 3697 | EndLoc, Vars, Alignment); |
| 3698 | } |
| 3699 | |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3700 | OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, |
| 3701 | SourceLocation StartLoc, |
| 3702 | SourceLocation LParenLoc, |
| 3703 | SourceLocation EndLoc) { |
| 3704 | SmallVector<Expr *, 8> Vars; |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3705 | for (auto &RefExpr : VarList) { |
| 3706 | assert(RefExpr && "NULL expr in OpenMP copyin clause."); |
| 3707 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3708 | // It will be analyzed later. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3709 | Vars.push_back(RefExpr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3710 | continue; |
| 3711 | } |
| 3712 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3713 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3714 | // OpenMP [2.1, C/C++] |
| 3715 | // A list item is a variable name. |
| 3716 | // OpenMP [2.14.4.1, Restrictions, p.1] |
| 3717 | // A list item that appears in a copyin clause must be threadprivate. |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3718 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3719 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3720 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3721 | continue; |
| 3722 | } |
| 3723 | |
| 3724 | Decl *D = DE->getDecl(); |
| 3725 | VarDecl *VD = cast<VarDecl>(D); |
| 3726 | |
| 3727 | QualType Type = VD->getType(); |
| 3728 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 3729 | // It will be analyzed later. |
| 3730 | Vars.push_back(DE); |
| 3731 | continue; |
| 3732 | } |
| 3733 | |
| 3734 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] |
| 3735 | // A list item that appears in a copyin clause must be threadprivate. |
| 3736 | if (!DSAStack->isThreadPrivate(VD)) { |
| 3737 | Diag(ELoc, diag::err_omp_required_access) |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3738 | << getOpenMPClauseName(OMPC_copyin) |
| 3739 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3740 | continue; |
| 3741 | } |
| 3742 | |
| 3743 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 3744 | // A variable of class type (or array thereof) that appears in a |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3745 | // copyin clause requires an accessible, unambiguous copy assignment |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3746 | // operator for the class type. |
| 3747 | Type = Context.getBaseElementType(Type); |
Alexander Musman | cb7f9c4 | 2014-05-15 13:04:49 +0000 | [diff] [blame] | 3748 | CXXRecordDecl *RD = |
| 3749 | getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 3750 | // FIXME This code must be replaced by actual assignment of the |
| 3751 | // threadprivate variable. |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3752 | if (RD) { |
| 3753 | CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); |
| 3754 | DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 3755 | if (MD) { |
| 3756 | if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || |
| 3757 | MD->isDeleted()) { |
| 3758 | Diag(ELoc, diag::err_omp_required_method) |
| 3759 | << getOpenMPClauseName(OMPC_copyin) << 2; |
| 3760 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 3761 | VarDecl::DeclarationOnly; |
| 3762 | Diag(VD->getLocation(), |
| 3763 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3764 | << VD; |
| 3765 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 3766 | continue; |
| 3767 | } |
| 3768 | MarkFunctionReferenced(ELoc, MD); |
| 3769 | DiagnoseUseOfDecl(MD, ELoc); |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3770 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | DSAStack->addDSA(VD, DE, OMPC_copyin); |
| 3774 | Vars.push_back(DE); |
| 3775 | } |
| 3776 | |
Alexey Bataev | ed09d24 | 2014-05-28 05:53:51 +0000 | [diff] [blame] | 3777 | if (Vars.empty()) |
| 3778 | return nullptr; |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 3779 | |
| 3780 | return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 3781 | } |
| 3782 | |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 3783 | OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, |
| 3784 | SourceLocation StartLoc, |
| 3785 | SourceLocation LParenLoc, |
| 3786 | SourceLocation EndLoc) { |
| 3787 | SmallVector<Expr *, 8> Vars; |
| 3788 | for (auto &RefExpr : VarList) { |
| 3789 | assert(RefExpr && "NULL expr in OpenMP copyprivate clause."); |
| 3790 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 3791 | // It will be analyzed later. |
| 3792 | Vars.push_back(RefExpr); |
| 3793 | continue; |
| 3794 | } |
| 3795 | |
| 3796 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 3797 | // OpenMP [2.1, C/C++] |
| 3798 | // A list item is a variable name. |
| 3799 | // OpenMP [2.14.4.1, Restrictions, p.1] |
| 3800 | // A list item that appears in a copyin clause must be threadprivate. |
| 3801 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
| 3802 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 3803 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
| 3804 | continue; |
| 3805 | } |
| 3806 | |
| 3807 | Decl *D = DE->getDecl(); |
| 3808 | VarDecl *VD = cast<VarDecl>(D); |
| 3809 | |
| 3810 | QualType Type = VD->getType(); |
| 3811 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 3812 | // It will be analyzed later. |
| 3813 | Vars.push_back(DE); |
| 3814 | continue; |
| 3815 | } |
| 3816 | |
| 3817 | // OpenMP [2.14.4.2, Restrictions, p.2] |
| 3818 | // A list item that appears in a copyprivate clause may not appear in a |
| 3819 | // private or firstprivate clause on the single construct. |
| 3820 | if (!DSAStack->isThreadPrivate(VD)) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3821 | auto DVar = DSAStack->getTopDSA(VD, false); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 3822 | if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown && |
| 3823 | !(DVar.CKind == OMPC_private && !DVar.RefExpr)) { |
| 3824 | Diag(ELoc, diag::err_omp_wrong_dsa) |
| 3825 | << getOpenMPClauseName(DVar.CKind) |
| 3826 | << getOpenMPClauseName(OMPC_copyprivate); |
| 3827 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
| 3828 | continue; |
| 3829 | } |
| 3830 | |
| 3831 | // OpenMP [2.11.4.2, Restrictions, p.1] |
| 3832 | // All list items that appear in a copyprivate clause must be either |
| 3833 | // threadprivate or private in the enclosing context. |
| 3834 | if (DVar.CKind == OMPC_unknown) { |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3835 | DVar = DSAStack->getImplicitDSA(VD, false); |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 3836 | if (DVar.CKind == OMPC_shared) { |
| 3837 | Diag(ELoc, diag::err_omp_required_access) |
| 3838 | << getOpenMPClauseName(OMPC_copyprivate) |
| 3839 | << "threadprivate or private in the enclosing context"; |
| 3840 | ReportOriginalDSA(*this, DSAStack, VD, DVar); |
| 3841 | continue; |
| 3842 | } |
| 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 3847 | // A variable of class type (or array thereof) that appears in a |
| 3848 | // copyin clause requires an accessible, unambiguous copy assignment |
| 3849 | // operator for the class type. |
| 3850 | Type = Context.getBaseElementType(Type); |
| 3851 | CXXRecordDecl *RD = |
| 3852 | getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
| 3853 | // FIXME This code must be replaced by actual assignment of the |
| 3854 | // threadprivate variable. |
| 3855 | if (RD) { |
| 3856 | CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); |
| 3857 | DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); |
| 3858 | if (MD) { |
| 3859 | if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || |
| 3860 | MD->isDeleted()) { |
| 3861 | Diag(ELoc, diag::err_omp_required_method) |
| 3862 | << getOpenMPClauseName(OMPC_copyprivate) << 2; |
| 3863 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 3864 | VarDecl::DeclarationOnly; |
| 3865 | Diag(VD->getLocation(), |
| 3866 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 3867 | << VD; |
| 3868 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 3869 | continue; |
| 3870 | } |
| 3871 | MarkFunctionReferenced(ELoc, MD); |
| 3872 | DiagnoseUseOfDecl(MD, ELoc); |
| 3873 | } |
| 3874 | } |
| 3875 | |
| 3876 | // No need to mark vars as copyprivate, they are already threadprivate or |
| 3877 | // implicitly private. |
| 3878 | Vars.push_back(DE); |
| 3879 | } |
| 3880 | |
| 3881 | if (Vars.empty()) |
| 3882 | return nullptr; |
| 3883 | |
| 3884 | return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 3885 | } |
| 3886 | |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 3887 | #undef DSAStack |