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